source: src/Graph/CheckAgainstAdjacencyFile.cpp@ 3501d2

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 3501d2 was 3501d2, checked in by Frederik Heber <heber@…>, 12 years ago

REFACTOR: CheckAgainstAdjacencyFile now gets File in cstor.

  • we cannot have multiple files, but we may have multiple fragmentations. Hence, CheckAgainstAdjacencyFile should get and parse the File in its cstor and get a set of atoms in its operator() to check against.
  • Property mode set to 100644
File size: 8.5 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * CheckAgainstAdjacencyFile.cpp
25 *
26 * Created on: Mar 3, 2011
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include "CodePatterns/MemDebug.hpp"
36
37#include <iostream>
38#include <map>
39#include <set>
40#include <utility>
41
42#include "CheckAgainstAdjacencyFile.hpp"
43
44#include "Atom/atom.hpp"
45#include "Bond/bond.hpp"
46#include "CodePatterns/Assert.hpp"
47#include "CodePatterns/Log.hpp"
48#include "CodePatterns/Range.hpp"
49#include "Descriptors/AtomIdDescriptor.hpp"
50#include "Helpers/defs.hpp"
51#include "World.hpp"
52
53/** Constructor of class CheckAgainstAdjacencyFile.
54 *
55 * \param File file to parser
56 */
57CheckAgainstAdjacencyFile::CheckAgainstAdjacencyFile(std::istream &File) :
58 status(true),
59 NonMatchNumber(0)
60{
61 ParseInExternalMap(File);
62}
63
64CheckAgainstAdjacencyFile::~CheckAgainstAdjacencyFile()
65{
66 ExternalAtomBondMap.clear();
67 InternalAtomBondMap.clear();
68}
69
70/** Parses the bond partners of each atom from an external file into \a AtomBondMap.
71 *
72 * @param File file to parse
73 * @return true - everything ok, false - error while parsing
74 */
75bool CheckAgainstAdjacencyFile::ParseInExternalMap(std::istream &File)
76{
77 if (File.fail()) {
78 LOG(1, "STATUS: Adjacency file not found." << endl);
79 return false;
80 }
81
82 ExternalAtomBondMap.clear();
83 char buffer[MAXSTRINGSIZE];
84 int tmp;
85 // Parse the file line by line and count the bonds
86 while (!File.eof()) {
87 File.getline(buffer, MAXSTRINGSIZE);
88 stringstream line;
89 line.str(buffer);
90 int AtomNr = -1;
91 line >> AtomNr;
92 // parse into structure
93 if (AtomNr > 0) {
94 const atom *Walker = World::getInstance().getAtom(AtomById(AtomNr-1));
95 ASSERT(Walker != NULL,
96 "CheckAgainstAdjacencyFile::ParseInExternalMap() - there is no atom with id "+toString(AtomNr-1)+".");
97 if (Walker == NULL)
98 return false;
99 // parse bond partner ids associated to AtomNr
100 while (line >> ws >> tmp) {
101 LOG(3, "INFO: Recognized bond partner " << tmp-1);
102 ExternalAtomBondMap.insert( std::make_pair(Walker->getId(), tmp-1) );
103 }
104 } else {
105 if (AtomNr != -1) {
106 ELOG(2, AtomNr << " is negative.");
107 return false;
108 }
109 }
110 }
111 return true;
112}
113
114/** Fills the InternalAtomBondMap from the atoms given by the two iterators.
115 *
116 * @param AtomMapBegin iterator pointing to begin of map (think of World's SelectionIterator)
117 * @param AtomMapEnd iterator pointing past end of map (think of World's SelectionIterator)
118 */
119void CheckAgainstAdjacencyFile::CreateInternalMap(World::AtomSet::const_iterator AtomMapBegin, World::AtomSet::const_iterator AtomMapEnd)
120{
121 InternalAtomBondMap.clear();
122 // go through each atom in the list
123 for (World::AtomSet::const_iterator iter = AtomMapBegin; iter != AtomMapEnd; ++iter) {
124 const atom *Walker = iter->second;
125 const atomId_t WalkerId = Walker->getId();
126 ASSERT(WalkerId != (size_t)-1,
127 "CheckAgainstAdjacencyFile::CreateInternalMap() - Walker has no id.");
128 const BondList& ListOfBonds = Walker->getListOfBonds();
129 // go through each of its bonds
130 for (BondList::const_iterator Runner = ListOfBonds.begin();
131 Runner != ListOfBonds.end();
132 ++Runner) {
133 const atomId_t id = (*Runner)->GetOtherAtom(Walker)->getId();
134 ASSERT(id != (size_t)-1,
135 "CheckAgainstAdjacencyFile::CreateInternalMap() - OtherAtom has not id.");
136 InternalAtomBondMap.insert( std::make_pair(WalkerId, id) );
137 }
138 }
139}
140
141/** Checks contents of adjacency file against bond structure in structure molecule.
142 * \return true - structure is equal, false - not equivalence
143 */
144bool CheckAgainstAdjacencyFile::operator()(World::AtomSet::const_iterator AtomMapBegin, World::AtomSet::const_iterator AtomMapEnd)
145{
146 LOG(0, "STATUS: Looking at bond structure stored in adjacency file and comparing to present one ... ");
147
148 bool status = true;
149
150 if (InternalAtomBondMap.empty())
151 CreateInternalMap(AtomMapBegin, AtomMapEnd);
152
153 status = status && CompareInternalExternalMap();
154
155 if (status) { // if equal we parse the KeySetFile
156 LOG(0, "STATUS: Equal.");
157 } else
158 LOG(0, "STATUS: Not equal by " << NonMatchNumber << " atoms.");
159 return status;
160}
161
162CheckAgainstAdjacencyFile::KeysSet CheckAgainstAdjacencyFile::getKeys(const CheckAgainstAdjacencyFile::AtomBondRange &_range) const
163{
164 KeysSet Keys;
165 for (AtomBondMap::const_iterator iter = _range.first;
166 iter != _range.second;
167 ++iter) {
168 Keys.insert( iter->first );
169 }
170 return Keys;
171}
172
173CheckAgainstAdjacencyFile::ValuesSet CheckAgainstAdjacencyFile::getValues(const CheckAgainstAdjacencyFile::AtomBondRange&_range) const
174{
175 ValuesSet Values;
176 for (AtomBondMap::const_iterator iter = _range.first;
177 iter != _range.second;
178 ++iter) {
179 Values.insert( iter->second );
180 }
181 return Values;
182}
183
184/** Counts the number of mismatching items in each set.
185 *
186 * @param firstset first set
187 * @param secondset second set
188 * @return number of items that don't match between first and second set
189 */
190template <class T>
191size_t getMismatchingItems(const T &firstset, const T &secondset)
192{
193 size_t Mismatch = 0;
194 typename T::const_iterator firstiter = firstset.begin();
195 typename T::const_iterator seconditer = secondset.begin();
196 for (; (firstiter != firstset.end()) && (seconditer != secondset.end());
197 ++firstiter, ++seconditer) {
198 if (*firstiter != *seconditer)
199 ++Mismatch;
200 }
201 return Mismatch;
202}
203
204/** Compares InternalAtomBondMap and ExternalAtomBondMap and sets NonMatchNumber.
205 *
206 * @return true - both maps are the same, false - both maps diverge by NonMatchNumber counts.
207 */
208bool CheckAgainstAdjacencyFile::CompareInternalExternalMap()
209{
210 NonMatchNumber = 0;
211 // check whether sizes match
212 if (ExternalAtomBondMap.size() != InternalAtomBondMap.size()) {
213 NonMatchNumber = abs((int)ExternalAtomBondMap.size() - (int)InternalAtomBondMap.size());
214 LOG(2, "INFO: " << NonMatchNumber << " entries don't match.");
215 return false;
216 }
217 // extract keys and check whether they match
218 const AtomBondRange Intrange(InternalAtomBondMap.begin(), InternalAtomBondMap.end());
219 const AtomBondRange Extrange(ExternalAtomBondMap.begin(), ExternalAtomBondMap.end());
220 KeysSet InternalKeys( getKeys(Intrange) );
221 KeysSet ExternalKeys( getKeys(Extrange) );
222
223// std::cout << "InternalKeys: " << InternalKeys << std::endl;
224// std::cout << "ExternalKeys: " << ExternalKeys << std::endl;
225
226 // check for same amount of keys
227 if (InternalKeys.size() != ExternalKeys.size()) {
228 NonMatchNumber = abs((int)ExternalKeys.size() - (int)InternalKeys.size());
229 LOG(2, "INFO: Number of keys don't match: "
230 << InternalKeys.size() << " != " << ExternalKeys.size());
231 return false;
232 }
233
234 // check items against one another
235 NonMatchNumber = getMismatchingItems(InternalKeys, ExternalKeys);
236
237 if (NonMatchNumber != 0) {
238 LOG(2, "INFO: " << NonMatchNumber << " keys are not the same.");
239 return false;
240 }
241
242 // now check each map per key
243 for (KeysSet::const_iterator keyIter = InternalKeys.begin();
244 keyIter != InternalKeys.end();
245 ++keyIter) {
246// std::cout << "Current key is " << *keyIter << std::endl;
247 const AtomBondRange IntRange( InternalAtomBondMap.equal_range(*keyIter) );
248 const AtomBondRange ExtRange( ExternalAtomBondMap.equal_range(*keyIter) );
249 ValuesSet InternalValues( getValues(IntRange) );
250 ValuesSet ExternalValues( getValues(ExtRange) );
251// std::cout << "InternalValues: " << InternalValues << std::endl;
252// std::cout << "ExternalValues: " << ExternalValues << std::endl;
253 NonMatchNumber += getMismatchingItems(InternalValues, ExternalValues);
254 }
255 if (NonMatchNumber != 0) {
256 LOG(2, "INFO: " << NonMatchNumber << " keys are not the same.");
257 return false;
258 } else {
259 LOG(2, "INFO: All keys are the same.");
260 return true;
261 }
262}
Note: See TracBrowser for help on using the repository browser.