source: src/molecule_fragmentation.cpp@ b8b75d

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 b8b75d was 93d120, checked in by Frederik Heber <heber@…>, 15 years ago

Fixed broken test "Fragmentation - Fragmentation".

  • BUGFIX: Code was split into CombineAllOrderListIntoOne() and FreeAllOrdersList(). In the first the combining is done and because RootStack is needed, we push_back() all pop_front() all RootKeyNr. However, the loop test was still .empty(), which never happened and an error was thrown as same keysets were tried to be inserted (assumed). Now, we store the last RootKeyNr by back() and compare the current to it. Test runs through fine.
  • Property mode set to 100644
File size: 77.0 KB
Line 
1/*
2 * molecule_fragmentation.cpp
3 *
4 * Created on: Oct 5, 2009
5 * Author: heber
6 */
7
8#include "atom.hpp"
9#include "bond.hpp"
10#include "config.hpp"
11#include "element.hpp"
12#include "helpers.hpp"
13#include "lists.hpp"
14#include "memoryallocator.hpp"
15#include "molecule.hpp"
16#include "periodentafel.hpp"
17
18/************************************* Functions for class molecule *********************************/
19
20
21/** Estimates by educated guessing (using upper limit) the expected number of fragments.
22 * The upper limit is
23 * \f[
24 * n = N \cdot C^k
25 * \f]
26 * where \f$C=2^c\f$ and c is the maximum bond degree over N number of atoms.
27 * \param *out output stream for debugging
28 * \param order bond order k
29 * \return number n of fragments
30 */
31int molecule::GuesstimateFragmentCount(ofstream *out, int order)
32{
33 int c = 0;
34 int FragmentCount;
35 // get maximum bond degree
36 atom *Walker = start;
37 while (Walker->next != end) {
38 Walker = Walker->next;
39 c = (NumberOfBondsPerAtom[Walker->nr] > c) ? NumberOfBondsPerAtom[Walker->nr] : c;
40 }
41 FragmentCount = NoNonHydrogen*(1 << (c*order));
42 *out << Verbose(1) << "Upper limit for this subgraph is " << FragmentCount << " for " << NoNonHydrogen << " non-H atoms with maximum bond degree of " << c << "." << endl;
43 return FragmentCount;
44};
45
46/** Scans a single line for number and puts them into \a KeySet.
47 * \param *out output stream for debugging
48 * \param *buffer buffer to scan
49 * \param &CurrentSet filled KeySet on return
50 * \return true - at least one valid atom id parsed, false - CurrentSet is empty
51 */
52bool ScanBufferIntoKeySet(ofstream *out, char *buffer, KeySet &CurrentSet)
53{
54 stringstream line;
55 int AtomNr;
56 int status = 0;
57
58 line.str(buffer);
59 while (!line.eof()) {
60 line >> AtomNr;
61 if (AtomNr >= 0) {
62 CurrentSet.insert(AtomNr); // insert at end, hence in same order as in file!
63 status++;
64 } // else it's "-1" or else and thus must not be added
65 }
66 *out << Verbose(1) << "The scanned KeySet is ";
67 for(KeySet::iterator runner = CurrentSet.begin(); runner != CurrentSet.end(); runner++) {
68 *out << (*runner) << "\t";
69 }
70 *out << endl;
71 return (status != 0);
72};
73
74/** Parses the KeySet file and fills \a *FragmentList from the known molecule structure.
75 * Does two-pass scanning:
76 * -# Scans the keyset file and initialises a temporary graph
77 * -# Scans TEFactors file and sets the TEFactor of each key set in the temporary graph accordingly
78 * Finally, the temporary graph is inserted into the given \a FragmentList for return.
79 * \param *out output stream for debugging
80 * \param *path path to file
81 * \param *FragmentList empty, filled on return
82 * \return true - parsing successfully, false - failure on parsing (FragmentList will be NULL)
83 */
84bool ParseKeySetFile(ofstream *out, char *path, Graph *&FragmentList)
85{
86 bool status = true;
87 ifstream InputFile;
88 stringstream line;
89 GraphTestPair testGraphInsert;
90 int NumberOfFragments = 0;
91 char *filename = Malloc<char>(MAXSTRINGSIZE, "molecule::ParseKeySetFile - filename");
92
93 if (FragmentList == NULL) { // check list pointer
94 FragmentList = new Graph;
95 }
96
97 // 1st pass: open file and read
98 *out << Verbose(1) << "Parsing the KeySet file ... " << endl;
99 sprintf(filename, "%s/%s%s", path, FRAGMENTPREFIX, KEYSETFILE);
100 InputFile.open(filename);
101 if (InputFile != NULL) {
102 // each line represents a new fragment
103 char *buffer = Malloc<char>(MAXSTRINGSIZE, "molecule::ParseKeySetFile - *buffer");
104 // 1. parse keysets and insert into temp. graph
105 while (!InputFile.eof()) {
106 InputFile.getline(buffer, MAXSTRINGSIZE);
107 KeySet CurrentSet;
108 if ((strlen(buffer) > 0) && (ScanBufferIntoKeySet(out, buffer, CurrentSet))) { // if at least one valid atom was added, write config
109 testGraphInsert = FragmentList->insert(GraphPair (CurrentSet,pair<int,double>(NumberOfFragments++,1))); // store fragment number and current factor
110 if (!testGraphInsert.second) {
111 cerr << "KeySet file must be corrupt as there are two equal key sets therein!" << endl;
112 }
113 }
114 }
115 // 2. Free and done
116 InputFile.close();
117 InputFile.clear();
118 Free(&buffer);
119 *out << Verbose(1) << "done." << endl;
120 } else {
121 *out << Verbose(1) << "File " << filename << " not found." << endl;
122 status = false;
123 }
124
125 return status;
126};
127
128/** Parses the TE factors file and fills \a *FragmentList from the known molecule structure.
129 * -# Scans TEFactors file and sets the TEFactor of each key set in the temporary graph accordingly
130 * \param *out output stream for debugging
131 * \param *path path to file
132 * \param *FragmentList graph whose nodes's TE factors are set on return
133 * \return true - parsing successfully, false - failure on parsing
134 */
135bool ParseTEFactorsFile(ofstream *out, char *path, Graph *FragmentList)
136{
137 bool status = true;
138 ifstream InputFile;
139 stringstream line;
140 GraphTestPair testGraphInsert;
141 int NumberOfFragments = 0;
142 double TEFactor;
143 char *filename = Malloc<char>(MAXSTRINGSIZE, "molecule::ParseTEFactorsFile - filename");
144
145 if (FragmentList == NULL) { // check list pointer
146 FragmentList = new Graph;
147 }
148
149 // 2nd pass: open TEFactors file and read
150 *out << Verbose(1) << "Parsing the TEFactors file ... " << endl;
151 sprintf(filename, "%s/%s%s", path, FRAGMENTPREFIX, TEFACTORSFILE);
152 InputFile.open(filename);
153 if (InputFile != NULL) {
154 // 3. add found TEFactors to each keyset
155 NumberOfFragments = 0;
156 for(Graph::iterator runner = FragmentList->begin();runner != FragmentList->end(); runner++) {
157 if (!InputFile.eof()) {
158 InputFile >> TEFactor;
159 (*runner).second.second = TEFactor;
160 *out << Verbose(2) << "Setting " << ++NumberOfFragments << " fragment's TEFactor to " << (*runner).second.second << "." << endl;
161 } else {
162 status = false;
163 break;
164 }
165 }
166 // 4. Free and done
167 InputFile.close();
168 *out << Verbose(1) << "done." << endl;
169 } else {
170 *out << Verbose(1) << "File " << filename << " not found." << endl;
171 status = false;
172 }
173
174 // free memory
175 Free(&filename);
176
177 return status;
178};
179
180/** Stores key sets to file.
181 * \param *out output stream for debugging
182 * \param KeySetList Graph with Keysets
183 * \param *path path to file
184 * \return true - file written successfully, false - writing failed
185 */
186bool StoreKeySetFile(ofstream *out, Graph &KeySetList, char *path)
187{
188 ofstream output;
189 bool status = true;
190 string line;
191
192 // open KeySet file
193 line = path;
194 line.append("/");
195 line += FRAGMENTPREFIX;
196 line += KEYSETFILE;
197 output.open(line.c_str(), ios::out);
198 *out << Verbose(1) << "Saving key sets of the total graph ... ";
199 if(output != NULL) {
200 for(Graph::iterator runner = KeySetList.begin(); runner != KeySetList.end(); runner++) {
201 for (KeySet::iterator sprinter = (*runner).first.begin();sprinter != (*runner).first.end(); sprinter++) {
202 if (sprinter != (*runner).first.begin())
203 output << "\t";
204 output << *sprinter;
205 }
206 output << endl;
207 }
208 *out << "done." << endl;
209 } else {
210 cerr << "Unable to open " << line << " for writing keysets!" << endl;
211 status = false;
212 }
213 output.close();
214 output.clear();
215
216 return status;
217};
218
219
220/** Stores TEFactors to file.
221 * \param *out output stream for debugging
222 * \param KeySetList Graph with factors
223 * \param *path path to file
224 * \return true - file written successfully, false - writing failed
225 */
226bool StoreTEFactorsFile(ofstream *out, Graph &KeySetList, char *path)
227{
228 ofstream output;
229 bool status = true;
230 string line;
231
232 // open TEFactors file
233 line = path;
234 line.append("/");
235 line += FRAGMENTPREFIX;
236 line += TEFACTORSFILE;
237 output.open(line.c_str(), ios::out);
238 *out << Verbose(1) << "Saving TEFactors of the total graph ... ";
239 if(output != NULL) {
240 for(Graph::iterator runner = KeySetList.begin(); runner != KeySetList.end(); runner++)
241 output << (*runner).second.second << endl;
242 *out << Verbose(1) << "done." << endl;
243 } else {
244 *out << Verbose(1) << "failed to open " << line << "." << endl;
245 status = false;
246 }
247 output.close();
248
249 return status;
250};
251
252/** For a given graph, sorts KeySets into a (index, keyset) map.
253 * \param *GlobalKeySetList list of keysets with global ids (valid in "this" molecule) needed for adaptive increase
254 * \return map from index to keyset
255 */
256map<int,KeySet> * GraphToIndexedKeySet(Graph *GlobalKeySetList)
257{
258 map<int,KeySet> *IndexKeySetList = new map<int,KeySet>;
259 for(Graph::iterator runner = GlobalKeySetList->begin(); runner != GlobalKeySetList->end(); runner++) {
260 IndexKeySetList->insert( pair<int,KeySet>(runner->second.first,runner->first) );
261 }
262 return IndexKeySetList;
263};
264
265/** Inserts a (\a No, \a value) pair into the list, overwriting present one.
266 * Note if values are equal, No will decided on which is first
267 * \param *out output stream for debugging
268 * \param &AdaptiveCriteriaList list to insert into
269 * \param &IndexedKeySetList list to find key set for a given index \a No
270 * \param FragOrder current bond order of fragment
271 * \param No index of keyset
272 * \param value energy value
273 */
274void InsertIntoAdaptiveCriteriaList(ofstream *out, map<int, pair<double,int> > *AdaptiveCriteriaList, map<int,KeySet> &IndexKeySetList, int FragOrder, int No, double Value)
275{
276 map<int,KeySet>::iterator marker = IndexKeySetList.find(No); // find keyset to Frag No.
277 if (marker != IndexKeySetList.end()) { // if found
278 Value *= 1 + MYEPSILON*(*((*marker).second.begin())); // in case of equal energies this makes them not equal without changing anything actually
279 // as the smallest number in each set has always been the root (we use global id to keep the doubles away), seek smallest and insert into AtomMask
280 pair <map<int, pair<double,int> >::iterator, bool> InsertedElement = AdaptiveCriteriaList->insert( make_pair(*((*marker).second.begin()), pair<double,int>( fabs(Value), FragOrder) ));
281 map<int, pair<double,int> >::iterator PresentItem = InsertedElement.first;
282 if (!InsertedElement.second) { // this root is already present
283 if ((*PresentItem).second.second < FragOrder) // if order there is lower, update entry with higher-order term
284 //if ((*PresentItem).second.first < (*runner).first) // as higher-order terms are not always better, we skip this part (which would always include this site into adaptive increase)
285 { // if value is smaller, update value and order
286 (*PresentItem).second.first = fabs(Value);
287 (*PresentItem).second.second = FragOrder;
288 *out << Verbose(2) << "Updated element (" << (*PresentItem).first << ",[" << (*PresentItem).second.first << "," << (*PresentItem).second.second << "])." << endl;
289 } else {
290 *out << Verbose(2) << "Did not update element " << (*PresentItem).first << " as " << FragOrder << " is less than or equal to " << (*PresentItem).second.second << "." << endl;
291 }
292 } else {
293 *out << Verbose(2) << "Inserted element (" << (*PresentItem).first << ",[" << (*PresentItem).second.first << "," << (*PresentItem).second.second << "])." << endl;
294 }
295 } else {
296 *out << Verbose(1) << "No Fragment under No. " << No << "found." << endl;
297 }
298};
299
300/** Scans the adaptive order file and insert (index, value) into map.
301 * \param *out output stream for debugging
302 * \param *path path to ENERGYPERFRAGMENT file (may be NULL if Order is non-negative)
303 * \param &IndexedKeySetList list to find key set for a given index \a No
304 * \return adaptive criteria list from file
305 */
306map<int, pair<double,int> > * ScanAdaptiveFileIntoMap(ofstream *out, char *path, map<int,KeySet> &IndexKeySetList)
307{
308 map<int, pair<double,int> > *AdaptiveCriteriaList = new map<int, pair<double,int> >;
309 int No = 0, FragOrder = 0;
310 double Value = 0.;
311 char *buffer = Malloc<char>(MAXSTRINGSIZE, "molecule::CheckOrderAtSite: *buffer");
312 sprintf(buffer, "%s/%s%s.dat", path, FRAGMENTPREFIX, ENERGYPERFRAGMENT);
313 ifstream InputFile(buffer, ios::in);
314
315 if (CountLinesinFile(InputFile) > 0) {
316 // each line represents a fragment root (Atom::nr) id and its energy contribution
317 InputFile.getline(buffer, MAXSTRINGSIZE); // skip comment lines
318 InputFile.getline(buffer, MAXSTRINGSIZE);
319 while(!InputFile.eof()) {
320 InputFile.getline(buffer, MAXSTRINGSIZE);
321 if (strlen(buffer) > 2) {
322 //*out << Verbose(2) << "Scanning: " << buffer << endl;
323 stringstream line(buffer);
324 line >> FragOrder;
325 line >> ws >> No;
326 line >> ws >> Value; // skip time entry
327 line >> ws >> Value;
328 No -= 1; // indices start at 1 in file, not 0
329 //*out << Verbose(2) << " - yields (" << No << "," << Value << ", " << FragOrder << ")" << endl;
330
331 // clean the list of those entries that have been superceded by higher order terms already
332 InsertIntoAdaptiveCriteriaList(out, AdaptiveCriteriaList, IndexKeySetList, FragOrder, No, Value);
333 }
334 }
335 // close and done
336 InputFile.close();
337 InputFile.clear();
338 }
339 Free(&buffer);
340
341 return AdaptiveCriteriaList;
342};
343
344/** Maps adaptive criteria list back onto (Value, (Root Nr., Order))
345 * (i.e. sorted by value to pick the highest ones)
346 * \param *out output stream for debugging
347 * \param &AdaptiveCriteriaList list to insert into
348 * \param *mol molecule with atoms
349 * \return remapped list
350 */
351map<double, pair<int,int> > * ReMapAdaptiveCriteriaListToValue(ofstream *out, map<int, pair<double,int> > *AdaptiveCriteriaList, molecule *mol)
352{
353 atom *Walker = mol->start;
354 map<double, pair<int,int> > *FinalRootCandidates = new map<double, pair<int,int> > ;
355 *out << Verbose(1) << "Root candidate list is: " << endl;
356 for(map<int, pair<double,int> >::iterator runner = AdaptiveCriteriaList->begin(); runner != AdaptiveCriteriaList->end(); runner++) {
357 Walker = mol->FindAtom((*runner).first);
358 if (Walker != NULL) {
359 //if ((*runner).second.second >= Walker->AdaptiveOrder) { // only insert if this is an "active" root site for the current order
360 if (!Walker->MaxOrder) {
361 *out << Verbose(2) << "(" << (*runner).first << ",[" << (*runner).second.first << "," << (*runner).second.second << "])" << endl;
362 FinalRootCandidates->insert( make_pair( (*runner).second.first, pair<int,int>((*runner).first, (*runner).second.second) ) );
363 } else {
364 *out << Verbose(2) << "Excluding (" << *Walker << ", " << (*runner).first << ",[" << (*runner).second.first << "," << (*runner).second.second << "]), as it has reached its maximum order." << endl;
365 }
366 } else {
367 cerr << "Atom No. " << (*runner).second.first << " was not found in this molecule." << endl;
368 }
369 }
370 return FinalRootCandidates;
371};
372
373/** Marks all candidate sites for update if below adaptive threshold.
374 * Picks a given number of highest values and set *AtomMask to true.
375 * \param *out output stream for debugging
376 * \param *AtomMask defines true/false per global Atom::nr to mask in/out each nuclear site, used to activate given number of site to increment order adaptively
377 * \param FinalRootCandidates list candidates to check
378 * \param Order desired order
379 * \param *mol molecule with atoms
380 * \return true - if update is necessary, false - not
381 */
382bool MarkUpdateCandidates(ofstream *out, bool *AtomMask, map<double, pair<int,int> > &FinalRootCandidates, int Order, molecule *mol)
383{
384 atom *Walker = mol->start;
385 int No = -1;
386 bool status = false;
387 for(map<double, pair<int,int> >::iterator runner = FinalRootCandidates.upper_bound(pow(10.,Order)); runner != FinalRootCandidates.end(); runner++) {
388 No = (*runner).second.first;
389 Walker = mol->FindAtom(No);
390 //if (Walker->AdaptiveOrder < MinimumRingSize[Walker->nr]) {
391 *out << Verbose(2) << "Root " << No << " is still above threshold (10^{" << Order <<"}: " << runner->first << ", setting entry " << No << " of Atom mask to true." << endl;
392 AtomMask[No] = true;
393 status = true;
394 //} else
395 //*out << Verbose(2) << "Root " << No << " is still above threshold (10^{" << Order <<"}: " << runner->first << ", however MinimumRingSize of " << MinimumRingSize[Walker->nr] << " does not allow further adaptive increase." << endl;
396 }
397 return status;
398};
399
400/** print atom mask for debugging.
401 * \param *out output stream for debugging
402 * \param *AtomMask defines true/false per global Atom::nr to mask in/out each nuclear site, used to activate given number of site to increment order adaptively
403 * \param AtomCount number of entries in \a *AtomMask
404 */
405void PrintAtomMask(ofstream *out, bool *AtomMask, int AtomCount)
406{
407 *out << " ";
408 for(int i=0;i<AtomCount;i++)
409 *out << (i % 10);
410 *out << endl << "Atom mask is: ";
411 for(int i=0;i<AtomCount;i++)
412 *out << (AtomMask[i] ? "t" : "f");
413 *out << endl;
414};
415
416/** Checks whether the OrderAtSite is still below \a Order at some site.
417 * \param *out output stream for debugging
418 * \param *AtomMask defines true/false per global Atom::nr to mask in/out each nuclear site, used to activate given number of site to increment order adaptively
419 * \param *GlobalKeySetList list of keysets with global ids (valid in "this" molecule) needed for adaptive increase
420 * \param Order desired Order if positive, desired exponent in threshold criteria if negative (0 is single-step)
421 * \param *MinimumRingSize array of max. possible order to avoid loops
422 * \param *path path to ENERGYPERFRAGMENT file (may be NULL if Order is non-negative)
423 * \return true - needs further fragmentation, false - does not need fragmentation
424 */
425bool molecule::CheckOrderAtSite(ofstream *out, bool *AtomMask, Graph *GlobalKeySetList, int Order, int *MinimumRingSize, char *path)
426{
427 atom *Walker = start;
428 bool status = false;
429
430 // initialize mask list
431 for(int i=AtomCount;i--;)
432 AtomMask[i] = false;
433
434 if (Order < 0) { // adaptive increase of BondOrder per site
435 if (AtomMask[AtomCount] == true) // break after one step
436 return false;
437
438 // transmorph graph keyset list into indexed KeySetList
439 if (GlobalKeySetList == NULL) {
440 cout << Verbose(1) << "ERROR: Given global key set list (graph) is NULL!" << endl;
441 return false;
442 }
443 map<int,KeySet> *IndexKeySetList = GraphToIndexedKeySet(GlobalKeySetList);
444
445 // parse the EnergyPerFragment file
446 map<int, pair<double,int> > *AdaptiveCriteriaList = ScanAdaptiveFileIntoMap(out, path, *IndexKeySetList); // (Root No., (Value, Order)) !
447 if (AdaptiveCriteriaList->empty()) {
448 cerr << "Unable to parse file, incrementing all." << endl;
449 while (Walker->next != end) {
450 Walker = Walker->next;
451 #ifdef ADDHYDROGEN
452 if (Walker->type->Z != 1) // skip hydrogen
453 #endif
454 {
455 AtomMask[Walker->nr] = true; // include all (non-hydrogen) atoms
456 status = true;
457 }
458 }
459 }
460 // then map back onto (Value, (Root Nr., Order)) (i.e. sorted by value to pick the highest ones)
461 map<double, pair<int,int> > *FinalRootCandidates = ReMapAdaptiveCriteriaListToValue(out, AdaptiveCriteriaList, this);
462
463 // pick the ones still below threshold and mark as to be adaptively updated
464 MarkUpdateCandidates(out, AtomMask, *FinalRootCandidates, Order, this);
465
466 Free(&IndexKeySetList);
467 Free(&AdaptiveCriteriaList);
468 Free(&FinalRootCandidates);
469 } else { // global increase of Bond Order
470 while (Walker->next != end) {
471 Walker = Walker->next;
472 #ifdef ADDHYDROGEN
473 if (Walker->type->Z != 1) // skip hydrogen
474 #endif
475 {
476 AtomMask[Walker->nr] = true; // include all (non-hydrogen) atoms
477 if ((Order != 0) && (Walker->AdaptiveOrder < Order)) // && (Walker->AdaptiveOrder < MinimumRingSize[Walker->nr]))
478 status = true;
479 }
480 }
481 if ((Order == 0) && (AtomMask[AtomCount] == false)) // single stepping, just check
482 status = true;
483
484 if (!status) {
485 if (Order == 0)
486 *out << Verbose(1) << "Single stepping done." << endl;
487 else
488 *out << Verbose(1) << "Order at every site is already equal or above desired order " << Order << "." << endl;
489 }
490 }
491
492 PrintAtomMask(out, AtomMask, AtomCount); // for debugging
493
494 return status;
495};
496
497/** Create a SortIndex to map from atomic labels to the sequence in which the atoms are given in the config file.
498 * \param *out output stream for debugging
499 * \param *&SortIndex Mapping array of size molecule::AtomCount
500 * \return true - success, false - failure of SortIndex alloc
501 */
502bool molecule::CreateMappingLabelsToConfigSequence(ofstream *out, int *&SortIndex)
503{
504 if (SortIndex != NULL) {
505 *out << Verbose(1) << "SortIndex is " << SortIndex << " and not NULL as expected." << endl;
506 return false;
507 }
508 SortIndex = Malloc<int>(AtomCount, "molecule::CreateMappingLabelsToConfigSequence: *SortIndex");
509 for(int i=AtomCount;i--;)
510 SortIndex[i] = -1;
511
512 int AtomNo = 0;
513 SetIndexedArrayForEachAtomTo( SortIndex, &atom::nr, &IncrementalAbsoluteValue, AtomNo );
514
515 return true;
516};
517
518/** Performs a many-body bond order analysis for a given bond order.
519 * -# parses adjacency, keysets and orderatsite files
520 * -# performs DFS to find connected subgraphs (to leave this in was a design decision: might be useful later)
521 * -# RootStack is created for every subgraph (here, later we implement the "update 10 sites with highest energ
522y contribution", and that's why this consciously not done in the following loop)
523 * -# in a loop over all subgraphs
524 * -# calls FragmentBOSSANOVA with this RootStack and within the subgraph molecule structure
525 * -# creates molecule (fragment)s from the returned keysets (StoreFragmentFromKeySet)
526 * -# combines the generated molecule lists from all subgraphs
527 * -# saves to disk: fragment configs, adjacency, orderatsite, keyset files
528 * Note that as we split "this" molecule up into a list of subgraphs, i.e. a MoleculeListClass, we have two sets
529 * of vertex indices: Global always means the index in "this" molecule, whereas local refers to the molecule or
530 * subgraph in the MoleculeListClass.
531 * \param *out output stream for debugging
532 * \param Order up to how many neighbouring bonds a fragment contains in BondOrderScheme::BottumUp scheme
533 * \param *configuration configuration for writing config files for each fragment
534 * \return 1 - continue, 2 - stop (no fragmentation occured)
535 */
536int molecule::FragmentMolecule(ofstream *out, int Order, config *configuration)
537{
538 MoleculeListClass *BondFragments = NULL;
539 int *SortIndex = NULL;
540 int *MinimumRingSize = new int[AtomCount];
541 int FragmentCounter;
542 MoleculeLeafClass *MolecularWalker = NULL;
543 MoleculeLeafClass *Subgraphs = NULL; // list of subgraphs from DFS analysis
544 fstream File;
545 bool FragmentationToDo = true;
546 class StackClass<bond *> *BackEdgeStack = NULL, *LocalBackEdgeStack = NULL;
547 bool CheckOrder = false;
548 Graph **FragmentList = NULL;
549 Graph *ParsedFragmentList = NULL;
550 Graph TotalGraph; // graph with all keysets however local numbers
551 int TotalNumberOfKeySets = 0;
552 atom **ListOfAtoms = NULL;
553 atom ***ListOfLocalAtoms = NULL;
554 bool *AtomMask = NULL;
555
556 *out << endl;
557#ifdef ADDHYDROGEN
558 *out << Verbose(0) << "I will treat hydrogen special and saturate dangling bonds with it." << endl;
559#else
560 *out << Verbose(0) << "Hydrogen is treated just like the rest of the lot." << endl;
561#endif
562
563 // ++++++++++++++++++++++++++++ INITIAL STUFF: Bond structure analysis, file parsing, ... ++++++++++++++++++++++++++++++++++++++++++
564
565 // ===== 1. Check whether bond structure is same as stored in files ====
566
567 // fill the adjacency list
568 CreateListOfBondsPerAtom(out);
569
570 // create lookup table for Atom::nr
571 FragmentationToDo = FragmentationToDo && CreateFatherLookupTable(out, start, end, ListOfAtoms, AtomCount);
572
573 // === compare it with adjacency file ===
574 FragmentationToDo = FragmentationToDo && CheckAdjacencyFileAgainstMolecule(out, configuration->configpath, ListOfAtoms);
575 Free(&ListOfAtoms);
576
577 // ===== 2. perform a DFS analysis to gather info on cyclic structure and a list of disconnected subgraphs =====
578 Subgraphs = DepthFirstSearchAnalysis(out, BackEdgeStack);
579 // fill the bond structure of the individually stored subgraphs
580 Subgraphs->next->FillBondStructureFromReference(out, this, (FragmentCounter = 0), ListOfLocalAtoms, false); // we want to keep the created ListOfLocalAtoms
581 // analysis of the cycles (print rings, get minimum cycle length) for each subgraph
582 for(int i=AtomCount;i--;)
583 MinimumRingSize[i] = AtomCount;
584 MolecularWalker = Subgraphs;
585 FragmentCounter = 0;
586 while (MolecularWalker->next != NULL) {
587 MolecularWalker = MolecularWalker->next;
588 *out << Verbose(0) << "Analysing the cycles of subgraph " << MolecularWalker->Leaf << " with nr. " << FragmentCounter << "." << endl;
589 LocalBackEdgeStack = new StackClass<bond *> (MolecularWalker->Leaf->BondCount);
590// // check the list of local atoms for debugging
591// *out << Verbose(0) << "ListOfLocalAtoms for this subgraph is:" << endl;
592// for (int i=0;i<AtomCount;i++)
593// if (ListOfLocalAtoms[FragmentCounter][i] == NULL)
594// *out << "\tNULL";
595// else
596// *out << "\t" << ListOfLocalAtoms[FragmentCounter][i]->Name;
597 *out << Verbose(0) << "Gathering local back edges for subgraph " << MolecularWalker->Leaf << " with nr. " << FragmentCounter << "." << endl;
598 MolecularWalker->Leaf->PickLocalBackEdges(out, ListOfLocalAtoms[FragmentCounter++], BackEdgeStack, LocalBackEdgeStack);
599 *out << Verbose(0) << "Analysing the cycles of subgraph " << MolecularWalker->Leaf << " with nr. " << FragmentCounter << "." << endl;
600 MolecularWalker->Leaf->CyclicStructureAnalysis(out, LocalBackEdgeStack, MinimumRingSize);
601 *out << Verbose(0) << "Done with Analysing the cycles of subgraph " << MolecularWalker->Leaf << " with nr. " << FragmentCounter << "." << endl;
602 delete(LocalBackEdgeStack);
603 }
604
605 // ===== 3. if structure still valid, parse key set file and others =====
606 FragmentationToDo = FragmentationToDo && ParseKeySetFile(out, configuration->configpath, ParsedFragmentList);
607
608 // ===== 4. check globally whether there's something to do actually (first adaptivity check)
609 FragmentationToDo = FragmentationToDo && ParseOrderAtSiteFromFile(out, configuration->configpath);
610
611 // =================================== Begin of FRAGMENTATION ===============================
612 // ===== 6a. assign each keyset to its respective subgraph =====
613 Subgraphs->next->AssignKeySetsToFragment(out, this, ParsedFragmentList, ListOfLocalAtoms, FragmentList, (FragmentCounter = 0), true);
614
615 // ===== 6b. prepare and go into the adaptive (Order<0), single-step (Order==0) or incremental (Order>0) cycle
616 KeyStack *RootStack = new KeyStack[Subgraphs->next->Count()];
617 AtomMask = new bool[AtomCount+1];
618 AtomMask[AtomCount] = false;
619 FragmentationToDo = false; // if CheckOrderAtSite just ones recommends fragmentation, we will save fragments afterwards
620 while ((CheckOrder = CheckOrderAtSite(out, AtomMask, ParsedFragmentList, Order, MinimumRingSize, configuration->configpath))) {
621 FragmentationToDo = FragmentationToDo || CheckOrder;
622 AtomMask[AtomCount] = true; // last plus one entry is used as marker that we have been through this loop once already in CheckOrderAtSite()
623 // ===== 6b. fill RootStack for each subgraph (second adaptivity check) =====
624 Subgraphs->next->FillRootStackForSubgraphs(out, RootStack, AtomMask, (FragmentCounter = 0));
625
626 // ===== 7. fill the bond fragment list =====
627 FragmentCounter = 0;
628 MolecularWalker = Subgraphs;
629 while (MolecularWalker->next != NULL) {
630 MolecularWalker = MolecularWalker->next;
631 *out << Verbose(1) << "Fragmenting subgraph " << MolecularWalker << "." << endl;
632 //MolecularWalker->Leaf->OutputListOfBonds(out); // output ListOfBondsPerAtom for debugging
633 if (MolecularWalker->Leaf->first->next != MolecularWalker->Leaf->last) {
634 // call BOSSANOVA method
635 *out << Verbose(0) << endl << " ========== BOND ENERGY of subgraph " << FragmentCounter << " ========================= " << endl;
636 MolecularWalker->Leaf->FragmentBOSSANOVA(out, FragmentList[FragmentCounter], RootStack[FragmentCounter], MinimumRingSize);
637 } else {
638 cerr << "Subgraph " << MolecularWalker << " has no atoms!" << endl;
639 }
640 FragmentCounter++; // next fragment list
641 }
642 }
643 delete[](RootStack);
644 delete[](AtomMask);
645 delete(ParsedFragmentList);
646 delete[](MinimumRingSize);
647
648
649 // ==================================== End of FRAGMENTATION ============================================
650
651 // ===== 8a. translate list into global numbers (i.e. ones that are valid in "this" molecule, not in MolecularWalker->Leaf)
652 Subgraphs->next->TranslateIndicesToGlobalIDs(out, FragmentList, (FragmentCounter = 0), TotalNumberOfKeySets, TotalGraph);
653
654 // free subgraph memory again
655 FragmentCounter = 0;
656 if (Subgraphs != NULL) {
657 while (Subgraphs->next != NULL) {
658 Subgraphs = Subgraphs->next;
659 delete(FragmentList[FragmentCounter++]);
660 delete(Subgraphs->previous);
661 }
662 delete(Subgraphs);
663 }
664 Free(&FragmentList);
665
666 // ===== 8b. gather keyset lists (graphs) from all subgraphs and transform into MoleculeListClass =====
667 //if (FragmentationToDo) { // we should always store the fragments again as coordination might have changed slightly without changing bond structure
668 // allocate memory for the pointer array and transmorph graphs into full molecular fragments
669 BondFragments = new MoleculeListClass();
670 int k=0;
671 for(Graph::iterator runner = TotalGraph.begin(); runner != TotalGraph.end(); runner++) {
672 KeySet test = (*runner).first;
673 *out << "Fragment No." << (*runner).second.first << " with TEFactor " << (*runner).second.second << "." << endl;
674 BondFragments->insert(StoreFragmentFromKeySet(out, test, configuration));
675 k++;
676 }
677 *out << k << "/" << BondFragments->ListOfMolecules.size() << " fragments generated from the keysets." << endl;
678
679 // ===== 9. Save fragments' configuration and keyset files et al to disk ===
680 if (BondFragments->ListOfMolecules.size() != 0) {
681 // create the SortIndex from BFS labels to order in the config file
682 CreateMappingLabelsToConfigSequence(out, SortIndex);
683
684 *out << Verbose(1) << "Writing " << BondFragments->ListOfMolecules.size() << " possible bond fragmentation configs" << endl;
685 if (BondFragments->OutputConfigForListOfFragments(out, configuration, SortIndex))
686 *out << Verbose(1) << "All configs written." << endl;
687 else
688 *out << Verbose(1) << "Some config writing failed." << endl;
689
690 // store force index reference file
691 BondFragments->StoreForcesFile(out, configuration->configpath, SortIndex);
692
693 // store keysets file
694 StoreKeySetFile(out, TotalGraph, configuration->configpath);
695
696 // store Adjacency file
697 StoreAdjacencyToFile(out, configuration->configpath);
698
699 // store Hydrogen saturation correction file
700 BondFragments->AddHydrogenCorrection(out, configuration->configpath);
701
702 // store adaptive orders into file
703 StoreOrderAtSiteFile(out, configuration->configpath);
704
705 // restore orbital and Stop values
706 CalculateOrbitals(*configuration);
707
708 // free memory for bond part
709 *out << Verbose(1) << "Freeing bond memory" << endl;
710 delete(FragmentList); // remove bond molecule from memory
711 Free(&SortIndex);
712 } else
713 *out << Verbose(1) << "FragmentList is zero on return, splitting failed." << endl;
714 //} else
715 // *out << Verbose(1) << "No fragments to store." << endl;
716 *out << Verbose(0) << "End of bond fragmentation." << endl;
717
718 return ((int)(!FragmentationToDo)+1); // 1 - continue, 2 - stop (no fragmentation occured)
719};
720
721
722/** Stores pairs (Atom::nr, Atom::AdaptiveOrder) into file.
723 * Atoms not present in the file get "-1".
724 * \param *out output stream for debugging
725 * \param *path path to file ORDERATSITEFILE
726 * \return true - file writable, false - not writable
727 */
728bool molecule::StoreOrderAtSiteFile(ofstream *out, char *path)
729{
730 stringstream line;
731 ofstream file;
732
733 line << path << "/" << FRAGMENTPREFIX << ORDERATSITEFILE;
734 file.open(line.str().c_str());
735 *out << Verbose(1) << "Writing OrderAtSite " << ORDERATSITEFILE << " ... " << endl;
736 if (file != NULL) {
737 ActOnAllAtoms( &atom::OutputOrder, &file );
738 file.close();
739 *out << Verbose(1) << "done." << endl;
740 return true;
741 } else {
742 *out << Verbose(1) << "failed to open file " << line.str() << "." << endl;
743 return false;
744 }
745};
746
747/** Parses pairs(Atom::nr, Atom::AdaptiveOrder) from file and stores in molecule's Atom's.
748 * Atoms not present in the file get "0".
749 * \param *out output stream for debugging
750 * \param *path path to file ORDERATSITEFILEe
751 * \return true - file found and scanned, false - file not found
752 * \sa ParseKeySetFile() and CheckAdjacencyFileAgainstMolecule() as this is meant to be used in conjunction with the two
753 */
754bool molecule::ParseOrderAtSiteFromFile(ofstream *out, char *path)
755{
756 unsigned char *OrderArray = Malloc<unsigned char>(AtomCount, "molecule::ParseOrderAtSiteFromFile - *OrderArray");
757 bool *MaxArray = Malloc<bool>(AtomCount, "molecule::ParseOrderAtSiteFromFile - *MaxArray");
758 bool status;
759 int AtomNr, value;
760 stringstream line;
761 ifstream file;
762
763 *out << Verbose(1) << "Begin of ParseOrderAtSiteFromFile" << endl;
764 for(int i=AtomCount;i--;)
765 OrderArray[i] = 0;
766 line << path << "/" << FRAGMENTPREFIX << ORDERATSITEFILE;
767 file.open(line.str().c_str());
768 if (file != NULL) {
769 for (int i=AtomCount;i--;) { // initialise with 0
770 OrderArray[i] = 0;
771 MaxArray[i] = 0;
772 }
773 while (!file.eof()) { // parse from file
774 AtomNr = -1;
775 file >> AtomNr;
776 if (AtomNr != -1) { // test whether we really parsed something (this is necessary, otherwise last atom is set twice and to 0 on second time)
777 file >> value;
778 OrderArray[AtomNr] = value;
779 file >> value;
780 MaxArray[AtomNr] = value;
781 //*out << Verbose(2) << "AtomNr " << AtomNr << " with order " << (int)OrderArray[AtomNr] << " and max order set to " << (int)MaxArray[AtomNr] << "." << endl;
782 }
783 }
784 file.close();
785
786 // set atom values
787 SetAtomValueToIndexedArray( OrderArray, &atom::nr, &atom::AdaptiveOrder );
788 SetAtomValueToIndexedArray( MaxArray, &atom::nr, &atom::MaxOrder );
789
790 *out << Verbose(1) << "done." << endl;
791 status = true;
792 } else {
793 *out << Verbose(1) << "failed to open file " << line.str() << "." << endl;
794 status = false;
795 }
796 Free(&OrderArray);
797 Free(&MaxArray);
798
799 *out << Verbose(1) << "End of ParseOrderAtSiteFromFile" << endl;
800 return status;
801};
802
803
804
805/** Looks through a StackClass<atom *> and returns the likeliest removal candiate.
806 * \param *out output stream for debugging messages
807 * \param *&Leaf KeySet to look through
808 * \param *&ShortestPathList list of the shortest path to decide which atom to suggest as removal candidate in the end
809 * \param index of the atom suggested for removal
810 */
811int molecule::LookForRemovalCandidate(ofstream *&out, KeySet *&Leaf, int *&ShortestPathList)
812{
813 atom *Runner = NULL;
814 int SP, Removal;
815
816 *out << Verbose(2) << "Looking for removal candidate." << endl;
817 SP = -1; //0; // not -1, so that Root is never removed
818 Removal = -1;
819 for (KeySet::iterator runner = Leaf->begin(); runner != Leaf->end(); runner++) {
820 Runner = FindAtom((*runner));
821 if (Runner->type->Z != 1) { // skip all those added hydrogens when re-filling snake stack
822 if (ShortestPathList[(*runner)] > SP) { // remove the oldest one with longest shortest path
823 SP = ShortestPathList[(*runner)];
824 Removal = (*runner);
825 }
826 }
827 }
828 return Removal;
829};
830
831/** Initializes some value for putting fragment of \a *mol into \a *Leaf.
832 * \param *mol total molecule
833 * \param *Leaf fragment molecule
834 * \param &Leaflet pointer to KeySet structure
835 * \param **SonList list which atom of \a *Leaf is a son of which atom in \a *mol
836 * \return number of atoms in fragment
837 */
838int StoreFragmentFromKeySet_Init(molecule *mol, molecule *Leaf, KeySet &Leaflet, atom **SonList)
839{
840 atom *FatherOfRunner = NULL;
841
842 Leaf->BondDistance = mol->BondDistance;
843 for(int i=NDIM*2;i--;)
844 Leaf->cell_size[i] = mol->cell_size[i];
845
846 // initialise SonList (indicates when we need to replace a bond with hydrogen instead)
847 for(int i=mol->AtomCount;i--;)
848 SonList[i] = NULL;
849
850 // first create the minimal set of atoms from the KeySet
851 int size = 0;
852 for(KeySet::iterator runner = Leaflet.begin(); runner != Leaflet.end(); runner++) {
853 FatherOfRunner = mol->FindAtom((*runner)); // find the id
854 SonList[FatherOfRunner->nr] = Leaf->AddCopyAtom(FatherOfRunner);
855 size++;
856 }
857 return size;
858};
859
860/** Creates an induced subgraph out of a fragmental key set, adding bonds and hydrogens (if treated specially).
861 * \param *out output stream for debugging messages
862 * \param *mol total molecule
863 * \param *Leaf fragment molecule
864 * \param IsAngstroem whether we have Ansgtroem or bohrradius
865 * \param **SonList list which atom of \a *Leaf is a son of which atom in \a *mol
866 */
867void CreateInducedSubgraphOfFragment(ofstream *out, molecule *mol, molecule *Leaf, atom **SonList, bool IsAngstroem)
868{
869 bool LonelyFlag = false;
870 atom *OtherFather = NULL;
871 atom *FatherOfRunner = NULL;
872 Leaf->CountAtoms(out);
873
874 atom *Runner = Leaf->start;
875 while (Runner->next != Leaf->end) {
876 Runner = Runner->next;
877 LonelyFlag = true;
878 FatherOfRunner = Runner->father;
879 if (SonList[FatherOfRunner->nr] != NULL) { // check if this, our father, is present in list
880 // create all bonds
881 for (int i=0;i<mol->NumberOfBondsPerAtom[FatherOfRunner->nr];i++) { // go through every bond of father
882 OtherFather = mol->ListOfBondsPerAtom[FatherOfRunner->nr][i]->GetOtherAtom(FatherOfRunner);
883// *out << Verbose(2) << "Father " << *FatherOfRunner << " of son " << *SonList[FatherOfRunner->nr] << " is bound to " << *OtherFather;
884 if (SonList[OtherFather->nr] != NULL) {
885// *out << ", whose son is " << *SonList[OtherFather->nr] << "." << endl;
886 if (OtherFather->nr > FatherOfRunner->nr) { // add bond (nr check is for adding only one of both variants: ab, ba)
887// *out << Verbose(3) << "Adding Bond: ";
888// *out <<
889 Leaf->AddBond(Runner, SonList[OtherFather->nr], mol->ListOfBondsPerAtom[FatherOfRunner->nr][i]->BondDegree);
890// *out << "." << endl;
891 //NumBonds[Runner->nr]++;
892 } else {
893// *out << Verbose(3) << "Not adding bond, labels in wrong order." << endl;
894 }
895 LonelyFlag = false;
896 } else {
897// *out << ", who has no son in this fragment molecule." << endl;
898#ifdef ADDHYDROGEN
899 //*out << Verbose(3) << "Adding Hydrogen to " << Runner->Name << " and a bond in between." << endl;
900 if(!Leaf->AddHydrogenReplacementAtom(out, mol->ListOfBondsPerAtom[FatherOfRunner->nr][i], Runner, FatherOfRunner, OtherFather, mol->ListOfBondsPerAtom[FatherOfRunner->nr],mol->NumberOfBondsPerAtom[FatherOfRunner->nr], IsAngstroem))
901 exit(1);
902#endif
903 //NumBonds[Runner->nr] += ListOfBondsPerAtom[FatherOfRunner->nr][i]->BondDegree;
904 }
905 }
906 } else {
907 *out << Verbose(0) << "ERROR: Son " << Runner->Name << " has father " << FatherOfRunner->Name << " but its entry in SonList is " << SonList[FatherOfRunner->nr] << "!" << endl;
908 }
909 if ((LonelyFlag) && (Leaf->AtomCount > 1)) {
910 *out << Verbose(0) << *Runner << "has got bonds only to hydrogens!" << endl;
911 }
912#ifdef ADDHYDROGEN
913 while ((Runner->next != Leaf->end) && (Runner->next->type->Z == 1)) // skip added hydrogen
914 Runner = Runner->next;
915#endif
916 }
917};
918
919/** Stores a fragment from \a KeySet into \a molecule.
920 * First creates the minimal set of atoms from the KeySet, then creates the bond structure from the complete
921 * molecule and adds missing hydrogen where bonds were cut.
922 * \param *out output stream for debugging messages
923 * \param &Leaflet pointer to KeySet structure
924 * \param IsAngstroem whether we have Ansgtroem or bohrradius
925 * \return pointer to constructed molecule
926 */
927molecule * molecule::StoreFragmentFromKeySet(ofstream *out, KeySet &Leaflet, bool IsAngstroem)
928{
929 atom **SonList = Malloc<atom*>(AtomCount, "molecule::StoreFragmentFromStack: **SonList");
930 molecule *Leaf = new molecule(elemente);
931
932// *out << Verbose(1) << "Begin of StoreFragmentFromKeyset." << endl;
933 StoreFragmentFromKeySet_Init(this, Leaf, Leaflet, SonList);
934 // create the bonds between all: Make it an induced subgraph and add hydrogen
935// *out << Verbose(2) << "Creating bonds from father graph (i.e. induced subgraph creation)." << endl;
936 CreateInducedSubgraphOfFragment(out, this, Leaf, SonList, IsAngstroem);
937
938 Leaf->CreateListOfBondsPerAtom(out);
939 //Leaflet->Leaf->ScanForPeriodicCorrection(out);
940 Free(&SonList);
941// *out << Verbose(1) << "End of StoreFragmentFromKeyset." << endl;
942 return Leaf;
943};
944
945
946/** Clears the touched list
947 * \param *out output stream for debugging
948 * \param verbosity verbosity level
949 * \param *&TouchedList touched list
950 * \param SubOrder current suborder
951 * \param TouchedIndex currently touched
952 */
953void SPFragmentGenerator_ClearingTouched(ofstream *out, int verbosity, int *&TouchedList, int SubOrder, int &TouchedIndex)
954{
955 *out << Verbose(1+verbosity) << "Clearing touched list." << endl;
956 for (TouchedIndex=SubOrder+1;TouchedIndex--;) // empty touched list
957 TouchedList[TouchedIndex] = -1;
958 TouchedIndex = 0;
959
960}
961
962/** Adds the current combination of the power set to the snake stack.
963 * \param *out output stream for debugging
964 * \param verbosity verbosity level
965 * \param CurrentCombination
966 * \param SetDimension maximum number of bits in power set
967 * \param *FragmentSet snake stack to remove from
968 * \param *&TouchedList touched list
969 * \param TouchedIndex currently touched
970 * \return number of set bits
971 */
972int AddPowersetToSnakeStack(ofstream *out, int verbosity, int CurrentCombination, int SetDimension, KeySet *FragmentSet, bond **BondsSet, int *&TouchedList, int &TouchedIndex)
973{
974 atom *OtherWalker = NULL;
975 bool bit = false;
976 KeySetTestPair TestKeySetInsert;
977
978 int Added = 0;
979 for (int j=0;j<SetDimension;j++) { // pull out every bit by shifting
980 bit = ((CurrentCombination & (1 << j)) != 0); // mask the bit for the j-th bond
981 if (bit) { // if bit is set, we add this bond partner
982 OtherWalker = BondsSet[j]->rightatom; // rightatom is always the one more distant, i.e. the one to add
983 //*out << Verbose(1+verbosity) << "Current Bond is " << ListOfBondsPerAtom[Walker->nr][CurrentCombination] << ", checking on " << *OtherWalker << "." << endl;
984 *out << Verbose(2+verbosity) << "Adding " << *OtherWalker << " with nr " << OtherWalker->nr << "." << endl;
985 TestKeySetInsert = FragmentSet->insert(OtherWalker->nr);
986 if (TestKeySetInsert.second) {
987 TouchedList[TouchedIndex++] = OtherWalker->nr; // note as added
988 Added++;
989 } else {
990 *out << Verbose(2+verbosity) << "This was item was already present in the keyset." << endl;
991 }
992 } else {
993 *out << Verbose(2+verbosity) << "Not adding." << endl;
994 }
995 }
996 return Added;
997};
998
999/** Counts the number of elements in a power set.
1000 * \param *SetFirst
1001 * \param *SetLast
1002 * \param *&TouchedList touched list
1003 * \param TouchedIndex currently touched
1004 * \return number of elements
1005 */
1006int CountSetMembers(bond *SetFirst, bond *SetLast, int *&TouchedList, int TouchedIndex)
1007{
1008 int SetDimension = 0;
1009 bond *Binder = SetFirst; // start node for this level
1010 while (Binder->next != SetLast) { // compare to end node of this level
1011 Binder = Binder->next;
1012 for (int k=TouchedIndex;k--;) {
1013 if (Binder->Contains(TouchedList[k])) // if we added this very endpiece
1014 SetDimension++;
1015 }
1016 }
1017 return SetDimension;
1018};
1019
1020/** Counts the number of elements in a power set.
1021 * \param *BondsList bonds list to fill
1022 * \param *SetFirst
1023 * \param *SetLast
1024 * \param *&TouchedList touched list
1025 * \param TouchedIndex currently touched
1026 * \return number of elements
1027 */
1028int FillBondsList(bond **BondsList, bond *SetFirst, bond *SetLast, int *&TouchedList, int TouchedIndex)
1029{
1030 int SetDimension = 0;
1031 bond *Binder = SetFirst; // start node for this level
1032 while (Binder->next != SetLast) { // compare to end node of this level
1033 Binder = Binder->next;
1034 for (int k=0;k<TouchedIndex;k++) {
1035 if (Binder->leftatom->nr == TouchedList[k]) // leftatom is always the close one
1036 BondsList[SetDimension++] = Binder;
1037 }
1038 }
1039 return SetDimension;
1040};
1041
1042/** Remove all items that were added on this SP level.
1043 * \param *out output stream for debugging
1044 * \param verbosity verbosity level
1045 * \param *FragmentSet snake stack to remove from
1046 * \param *&TouchedList touched list
1047 * \param TouchedIndex currently touched
1048 */
1049void RemoveAllTouchedFromSnakeStack(ofstream *out, int verbosity, KeySet *FragmentSet, int *&TouchedList, int &TouchedIndex)
1050{
1051 int Removal = 0;
1052 for(int j=0;j<TouchedIndex;j++) {
1053 Removal = TouchedList[j];
1054 *out << Verbose(2+verbosity) << "Removing item nr. " << Removal << " from snake stack." << endl;
1055 FragmentSet->erase(Removal);
1056 TouchedList[j] = -1;
1057 }
1058 *out << Verbose(2) << "Remaining local nr.s on snake stack are: ";
1059 for(KeySet::iterator runner = FragmentSet->begin(); runner != FragmentSet->end(); runner++)
1060 *out << (*runner) << " ";
1061 *out << endl;
1062 TouchedIndex = 0; // set Index to 0 for list of atoms added on this level
1063};
1064
1065/** From a given set of Bond sorted by Shortest Path distance, create all possible fragments of size \a SetDimension.
1066 * -# loops over every possible combination (2^dimension of edge set)
1067 * -# inserts current set, if there's still space left
1068 * -# yes: calls SPFragmentGenerator with structure, created new edge list and size respective to root dist
1069ance+1
1070 * -# no: stores fragment into keyset list by calling InsertFragmentIntoGraph
1071 * -# removes all items added into the snake stack (in UniqueFragments structure) added during level (root
1072distance) and current set
1073 * \param *out output stream for debugging
1074 * \param FragmentSearch UniqueFragments structure with all values needed
1075 * \param RootDistance current shortest path level, whose set of edges is represented by **BondsSet
1076 * \param SetDimension Number of possible bonds on this level (i.e. size of the array BondsSet[])
1077 * \param SubOrder remaining number of allowed vertices to add
1078 */
1079void molecule::SPFragmentGenerator(ofstream *out, struct UniqueFragments *FragmentSearch, int RootDistance, bond **BondsSet, int SetDimension, int SubOrder)
1080{
1081 int verbosity = 0; //FragmentSearch->ANOVAOrder-SubOrder;
1082 int NumCombinations;
1083 int bits, TouchedIndex, SubSetDimension, SP, Added;
1084 int SpaceLeft;
1085 int *TouchedList = Malloc<int>(SubOrder + 1, "molecule::SPFragmentGenerator: *TouchedList");
1086 bond **BondsList = NULL;
1087 KeySetTestPair TestKeySetInsert;
1088
1089 NumCombinations = 1 << SetDimension;
1090
1091 // Hier muessen von 1 bis NumberOfBondsPerAtom[Walker->nr] alle Kombinationen
1092 // von Endstuecken (aus den Bonds) hinzugefuegt werden und fuer verbleibende ANOVAOrder
1093 // rekursiv GraphCrawler in der naechsten Ebene aufgerufen werden
1094
1095 *out << Verbose(1+verbosity) << "Begin of SPFragmentGenerator." << endl;
1096 *out << Verbose(1+verbosity) << "We are " << RootDistance << " away from Root, which is " << *FragmentSearch->Root << ", SubOrder is " << SubOrder << ", SetDimension is " << SetDimension << " and this means " << NumCombinations-1 << " combination(s)." << endl;
1097
1098 // initialised touched list (stores added atoms on this level)
1099 SPFragmentGenerator_ClearingTouched(out, verbosity, TouchedList, SubOrder, TouchedIndex);
1100
1101 // create every possible combination of the endpieces
1102 *out << Verbose(1+verbosity) << "Going through all combinations of the power set." << endl;
1103 for (int i=1;i<NumCombinations;i++) { // sweep through all power set combinations (skip empty set!)
1104 // count the set bit of i
1105 bits = 0;
1106 for (int j=SetDimension;j--;)
1107 bits += (i & (1 << j)) >> j;
1108
1109 *out << Verbose(1+verbosity) << "Current set is " << Binary(i | (1 << SetDimension)) << ", number of bits is " << bits << "." << endl;
1110 if (bits <= SubOrder) { // if not greater than additional atoms allowed on stack, continue
1111 // --1-- add this set of the power set of bond partners to the snake stack
1112 Added = AddPowersetToSnakeStack(out, verbosity, i, SetDimension, FragmentSearch->FragmentSet, BondsSet, TouchedList, TouchedIndex);
1113
1114 SpaceLeft = SubOrder - Added ;// SubOrder - bits; // due to item's maybe being already present, this does not work anymore
1115 if (SpaceLeft > 0) {
1116 *out << Verbose(1+verbosity) << "There's still some space left on stack: " << SpaceLeft << "." << endl;
1117 if (SubOrder > 1) { // Due to Added above we have to check extra whether we're not already reaching beyond the desired Order
1118 // --2-- look at all added end pieces of this combination, construct bond subsets and sweep through a power set of these by recursion
1119 SP = RootDistance+1; // this is the next level
1120
1121 // first count the members in the subset
1122 SubSetDimension = CountSetMembers(FragmentSearch->BondsPerSPList[2*SP], FragmentSearch->BondsPerSPList[2*SP+1], TouchedList, TouchedIndex);
1123
1124 // then allocate and fill the list
1125 BondsList = Malloc<bond*>(SubSetDimension, "molecule::SPFragmentGenerator: **BondsList");
1126 SubSetDimension = FillBondsList(BondsList, FragmentSearch->BondsPerSPList[2*SP], FragmentSearch->BondsPerSPList[2*SP+1], TouchedList, TouchedIndex);
1127
1128 // then iterate
1129 *out << Verbose(2+verbosity) << "Calling subset generator " << SP << " away from root " << *FragmentSearch->Root << " with sub set dimension " << SubSetDimension << "." << endl;
1130 SPFragmentGenerator(out, FragmentSearch, SP, BondsList, SubSetDimension, SubOrder-bits);
1131
1132 Free(&BondsList);
1133 }
1134 } else {
1135 // --2-- otherwise store the complete fragment
1136 *out << Verbose(1+verbosity) << "Enough items on stack for a fragment!" << endl;
1137 // store fragment as a KeySet
1138 *out << Verbose(2) << "Found a new fragment[" << FragmentSearch->FragmentCounter << "], local nr.s are: ";
1139 for(KeySet::iterator runner = FragmentSearch->FragmentSet->begin(); runner != FragmentSearch->FragmentSet->end(); runner++)
1140 *out << (*runner) << " ";
1141 *out << endl;
1142 //if (!CheckForConnectedSubgraph(out, FragmentSearch->FragmentSet))
1143 //*out << Verbose(0) << "ERROR: The found fragment is not a connected subgraph!" << endl;
1144 InsertFragmentIntoGraph(out, FragmentSearch);
1145 }
1146
1147 // --3-- remove all added items in this level from snake stack
1148 *out << Verbose(1+verbosity) << "Removing all items that were added on this SP level " << RootDistance << "." << endl;
1149 RemoveAllTouchedFromSnakeStack(out, verbosity, FragmentSearch->FragmentSet, TouchedList, TouchedIndex);
1150 } else {
1151 *out << Verbose(2+verbosity) << "More atoms to add for this set (" << bits << ") than space left on stack " << SubOrder << ", skipping this set." << endl;
1152 }
1153 }
1154 Free(&TouchedList);
1155 *out << Verbose(1+verbosity) << "End of SPFragmentGenerator, " << RootDistance << " away from Root " << *FragmentSearch->Root << " and SubOrder is " << SubOrder << "." << endl;
1156};
1157
1158/** Allocates memory for UniqueFragments::BondsPerSPList.
1159 * \param *out output stream
1160 * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
1161 * \param FragmentSearch UniqueFragments
1162 * \sa FreeSPList()
1163 */
1164void InitialiseSPList(ofstream *out, int Order, struct UniqueFragments &FragmentSearch)
1165{
1166 FragmentSearch.BondsPerSPList = Malloc<bond*>(Order * 2, "molecule::PowerSetGenerator: ***BondsPerSPList");
1167 FragmentSearch.BondsPerSPCount = Malloc<int>(Order, "molecule::PowerSetGenerator: *BondsPerSPCount");
1168 for (int i=Order;i--;) {
1169 FragmentSearch.BondsPerSPList[2*i] = new bond(); // start node
1170 FragmentSearch.BondsPerSPList[2*i+1] = new bond(); // end node
1171 FragmentSearch.BondsPerSPList[2*i]->next = FragmentSearch.BondsPerSPList[2*i+1]; // intertwine these two
1172 FragmentSearch.BondsPerSPList[2*i+1]->previous = FragmentSearch.BondsPerSPList[2*i];
1173 FragmentSearch.BondsPerSPCount[i] = 0;
1174 }
1175};
1176
1177/** Free's memory for for UniqueFragments::BondsPerSPList.
1178 * \param *out output stream
1179 * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
1180 * \param FragmentSearch UniqueFragments\
1181 * \sa InitialiseSPList()
1182 */
1183void FreeSPList(ofstream *out, int Order, struct UniqueFragments &FragmentSearch)
1184{
1185 Free(&FragmentSearch.BondsPerSPCount);
1186 for (int i=Order;i--;) {
1187 delete(FragmentSearch.BondsPerSPList[2*i]);
1188 delete(FragmentSearch.BondsPerSPList[2*i+1]);
1189 }
1190 Free(&FragmentSearch.BondsPerSPList);
1191};
1192
1193/** Sets FragmenSearch to initial value.
1194 * Sets UniqueFragments::ShortestPathList entries to zero, UniqueFragments::BondsPerSPCount to zero (except zero level to 1) and
1195 * adds initial bond UniqueFragments::Root to UniqueFragments::Root to UniqueFragments::BondsPerSPList
1196 * \param *out output stream
1197 * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
1198 * \param FragmentSearch UniqueFragments
1199 * \sa FreeSPList()
1200 */
1201void SetSPList(ofstream *out, int Order, struct UniqueFragments &FragmentSearch)
1202{
1203 // prepare Label and SP arrays of the BFS search
1204 FragmentSearch.ShortestPathList[FragmentSearch.Root->nr] = 0;
1205
1206 // prepare root level (SP = 0) and a loop bond denoting Root
1207 for (int i=Order;i--;)
1208 FragmentSearch.BondsPerSPCount[i] = 0;
1209 FragmentSearch.BondsPerSPCount[0] = 1;
1210 bond *Binder = new bond(FragmentSearch.Root, FragmentSearch.Root);
1211 add(Binder, FragmentSearch.BondsPerSPList[1]);
1212};
1213
1214/** Resets UniqueFragments::ShortestPathList and cleans bonds from UniqueFragments::BondsPerSPList.
1215 * \param *out output stream
1216 * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
1217 * \param FragmentSearch UniqueFragments
1218 * \sa InitialiseSPList()
1219 */
1220void ResetSPList(ofstream *out, int Order, struct UniqueFragments &FragmentSearch)
1221{
1222 bond *Binder = NULL;
1223 *out << Verbose(0) << "Free'ing all found lists. and resetting index lists" << endl;
1224 for(int i=Order;i--;) {
1225 *out << Verbose(1) << "Current SP level is " << i << ": ";
1226 Binder = FragmentSearch.BondsPerSPList[2*i];
1227 while (Binder->next != FragmentSearch.BondsPerSPList[2*i+1]) {
1228 Binder = Binder->next;
1229 // *out << "Removing atom " << Binder->leftatom->nr << " and " << Binder->rightatom->nr << "." << endl; // make sure numbers are local
1230 FragmentSearch.ShortestPathList[Binder->leftatom->nr] = -1;
1231 FragmentSearch.ShortestPathList[Binder->rightatom->nr] = -1;
1232 }
1233 // delete added bonds
1234 cleanup(FragmentSearch.BondsPerSPList[2*i], FragmentSearch.BondsPerSPList[2*i+1]);
1235 // also start and end node
1236 *out << "cleaned." << endl;
1237 }
1238};
1239
1240
1241/** Fills the Bonds per Shortest Path List and set the vertex labels.
1242 * \param *out output stream
1243 * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
1244 * \param FragmentSearch UniqueFragments
1245 * \param *mol molecule with atoms and bonds
1246 * \param RestrictedKeySet Restricted vertex set to use in context of molecule
1247 */
1248void FillSPListandLabelVertices(ofstream *out, int Order, struct UniqueFragments &FragmentSearch, molecule *mol, KeySet RestrictedKeySet)
1249{
1250 // Actually, we should construct a spanning tree vom the root atom and select all edges therefrom and put them into
1251 // according shortest path lists. However, we don't. Rather we fill these lists right away, as they do form a spanning
1252 // tree already sorted into various SP levels. That's why we just do loops over the depth (CurrentSP) and breadth
1253 // (EdgeinSPLevel) of this tree ...
1254 // In another picture, the bonds always contain a direction by rightatom being the one more distant from root and hence
1255 // naturally leftatom forming its predecessor, preventing the BFS"seeker" from continuing in the wrong direction.
1256 int AtomKeyNr = -1;
1257 atom *Walker = NULL;
1258 atom *OtherWalker = NULL;
1259 atom *Predecessor = NULL;
1260 bond *Binder = NULL;
1261 bond *CurrentEdge = NULL;
1262 int RootKeyNr = FragmentSearch.Root->GetTrueFather()->nr;
1263 int RemainingWalkers = -1;
1264 int SP = -1;
1265
1266 *out << Verbose(0) << "Starting BFS analysis ..." << endl;
1267 for (SP = 0; SP < (Order-1); SP++) {
1268 *out << Verbose(1) << "New SP level reached: " << SP << ", creating new SP list with " << FragmentSearch.BondsPerSPCount[SP] << " item(s)";
1269 if (SP > 0) {
1270 *out << ", old level closed with " << FragmentSearch.BondsPerSPCount[SP-1] << " item(s)." << endl;
1271 FragmentSearch.BondsPerSPCount[SP] = 0;
1272 } else
1273 *out << "." << endl;
1274
1275 RemainingWalkers = FragmentSearch.BondsPerSPCount[SP];
1276 CurrentEdge = FragmentSearch.BondsPerSPList[2*SP]; /// start of this SP level's list
1277 while (CurrentEdge->next != FragmentSearch.BondsPerSPList[2*SP+1]) { /// end of this SP level's list
1278 CurrentEdge = CurrentEdge->next;
1279 RemainingWalkers--;
1280 Walker = CurrentEdge->rightatom; // rightatom is always the one more distant
1281 Predecessor = CurrentEdge->leftatom; // ... and leftatom is predecessor
1282 AtomKeyNr = Walker->nr;
1283 *out << Verbose(0) << "Current Walker is: " << *Walker << " with nr " << Walker->nr << " and SP of " << SP << ", with " << RemainingWalkers << " remaining walkers on this level." << endl;
1284 // check for new sp level
1285 // go through all its bonds
1286 *out << Verbose(1) << "Going through all bonds of Walker." << endl;
1287 for (int i=0;i<mol->NumberOfBondsPerAtom[AtomKeyNr];i++) {
1288 Binder = mol->ListOfBondsPerAtom[AtomKeyNr][i];
1289 OtherWalker = Binder->GetOtherAtom(Walker);
1290 if ((RestrictedKeySet.find(OtherWalker->nr) != RestrictedKeySet.end())
1291 #ifdef ADDHYDROGEN
1292 && (OtherWalker->type->Z != 1)
1293 #endif
1294 ) { // skip hydrogens and restrict to fragment
1295 *out << Verbose(2) << "Current partner is " << *OtherWalker << " with nr " << OtherWalker->nr << " in bond " << *Binder << "." << endl;
1296 // set the label if not set (and push on root stack as well)
1297 if ((OtherWalker != Predecessor) && (OtherWalker->GetTrueFather()->nr > RootKeyNr)) { // only pass through those with label bigger than Root's
1298 FragmentSearch.ShortestPathList[OtherWalker->nr] = SP+1;
1299 *out << Verbose(3) << "Set Shortest Path to " << FragmentSearch.ShortestPathList[OtherWalker->nr] << "." << endl;
1300 // add the bond in between to the SP list
1301 Binder = new bond(Walker, OtherWalker); // create a new bond in such a manner, that bond::rightatom is always the one more distant
1302 add(Binder, FragmentSearch.BondsPerSPList[2*(SP+1)+1]);
1303 FragmentSearch.BondsPerSPCount[SP+1]++;
1304 *out << Verbose(3) << "Added its bond to SP list, having now " << FragmentSearch.BondsPerSPCount[SP+1] << " item(s)." << endl;
1305 } else {
1306 if (OtherWalker != Predecessor)
1307 *out << Verbose(3) << "Not passing on, as index of " << *OtherWalker << " " << OtherWalker->GetTrueFather()->nr << " is smaller than that of Root " << RootKeyNr << "." << endl;
1308 else
1309 *out << Verbose(3) << "This is my predecessor " << *Predecessor << "." << endl;
1310 }
1311 } else *out << Verbose(2) << "Is not in the restricted keyset or skipping hydrogen " << *OtherWalker << "." << endl;
1312 }
1313 }
1314 }
1315};
1316
1317/** prints the Bonds per Shortest Path list in UniqueFragments.
1318 * \param *out output stream
1319 * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
1320 * \param FragmentSearch UniqueFragments
1321 */
1322void OutputSPList(ofstream *out, int Order, struct UniqueFragments &FragmentSearch)
1323{
1324 bond *Binder = NULL;
1325 *out << Verbose(0) << "Printing all found lists." << endl;
1326 for(int i=1;i<Order;i++) { // skip the root edge in the printing
1327 Binder = FragmentSearch.BondsPerSPList[2*i];
1328 *out << Verbose(1) << "Current SP level is " << i << "." << endl;
1329 while (Binder->next != FragmentSearch.BondsPerSPList[2*i+1]) {
1330 Binder = Binder->next;
1331 *out << Verbose(2) << *Binder << endl;
1332 }
1333 }
1334};
1335
1336/** Simply counts all bonds in all UniqueFragments::BondsPerSPList lists.
1337 * \param *out output stream
1338 * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
1339 * \param FragmentSearch UniqueFragments
1340 */
1341int CountNumbersInBondsList(ofstream *out, int Order, struct UniqueFragments &FragmentSearch)
1342{
1343 bond *Binder = NULL;
1344 int SP = -1; // the Root <-> Root edge must be subtracted!
1345 for(int i=Order;i--;) { // sum up all found edges
1346 Binder = FragmentSearch.BondsPerSPList[2*i];
1347 while (Binder->next != FragmentSearch.BondsPerSPList[2*i+1]) {
1348 Binder = Binder->next;
1349 SP++;
1350 }
1351 }
1352 return SP;
1353};
1354
1355/** Creates a list of all unique fragments of certain vertex size from a given graph \a Fragment for a given root vertex in the context of \a this molecule.
1356 * -# initialises UniqueFragments structure
1357 * -# fills edge list via BFS
1358 * -# creates the fragment by calling recursive function SPFragmentGenerator with UniqueFragments structure, 0 as
1359 root distance, the edge set, its dimension and the current suborder
1360 * -# Free'ing structure
1361 * Note that we may use the fact that the atoms are SP-ordered on the atomstack. I.e. when popping always the last, we first get all
1362 * with SP of 2, then those with SP of 3, then those with SP of 4 and so on.
1363 * \param *out output stream for debugging
1364 * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
1365 * \param FragmentSearch UniqueFragments structure containing TEFactor, root atom and so on
1366 * \param RestrictedKeySet Restricted vertex set to use in context of molecule
1367 * \return number of inserted fragments
1368 * \note ShortestPathList in FragmentSearch structure is probably due to NumberOfAtomsSPLevel and SP not needed anymore
1369 */
1370int molecule::PowerSetGenerator(ofstream *out, int Order, struct UniqueFragments &FragmentSearch, KeySet RestrictedKeySet)
1371{
1372 bond **BondsList = NULL;
1373 int Counter = FragmentSearch.FragmentCounter; // mark current value of counter
1374
1375 *out << endl;
1376 *out << Verbose(0) << "Begin of PowerSetGenerator with order " << Order << " at Root " << *FragmentSearch.Root << "." << endl;
1377
1378 SetSPList(out, Order, FragmentSearch);
1379
1380 // do a BFS search to fill the SP lists and label the found vertices
1381 FillSPListandLabelVertices(out, Order, FragmentSearch, this, RestrictedKeySet);
1382
1383 // outputting all list for debugging
1384 OutputSPList(out, Order, FragmentSearch);
1385
1386 // creating fragments with the found edge sets (may be done in reverse order, faster)
1387 int SP = CountNumbersInBondsList(out, Order, FragmentSearch);
1388 *out << Verbose(0) << "Total number of edges is " << SP << "." << endl;
1389 if (SP >= (Order-1)) {
1390 // start with root (push on fragment stack)
1391 *out << Verbose(0) << "Starting fragment generation with " << *FragmentSearch.Root << ", local nr is " << FragmentSearch.Root->nr << "." << endl;
1392 FragmentSearch.FragmentSet->clear();
1393 *out << Verbose(0) << "Preparing subset for this root and calling generator." << endl;
1394
1395 // prepare the subset and call the generator
1396 BondsList = Malloc<bond*>(FragmentSearch.BondsPerSPCount[0], "molecule::PowerSetGenerator: **BondsList");
1397 BondsList[0] = FragmentSearch.BondsPerSPList[0]->next; // on SP level 0 there's only the root bond
1398
1399 SPFragmentGenerator(out, &FragmentSearch, 0, BondsList, FragmentSearch.BondsPerSPCount[0], Order);
1400
1401 Free(&BondsList);
1402 } else {
1403 *out << Verbose(0) << "Not enough total number of edges to build " << Order << "-body fragments." << endl;
1404 }
1405
1406 // as FragmentSearch structure is used only once, we don't have to clean it anymore
1407 // remove root from stack
1408 *out << Verbose(0) << "Removing root again from stack." << endl;
1409 FragmentSearch.FragmentSet->erase(FragmentSearch.Root->nr);
1410
1411 // free'ing the bonds lists
1412 ResetSPList(out, Order, FragmentSearch);
1413
1414 // return list
1415 *out << Verbose(0) << "End of PowerSetGenerator." << endl;
1416 return (FragmentSearch.FragmentCounter - Counter);
1417};
1418
1419bool KeyCompare::operator() (const KeySet SubgraphA, const KeySet SubgraphB) const
1420{
1421 //cout << "my check is used." << endl;
1422 if (SubgraphA.size() < SubgraphB.size()) {
1423 return true;
1424 } else {
1425 if (SubgraphA.size() > SubgraphB.size()) {
1426 return false;
1427 } else {
1428 KeySet::iterator IteratorA = SubgraphA.begin();
1429 KeySet::iterator IteratorB = SubgraphB.begin();
1430 while ((IteratorA != SubgraphA.end()) && (IteratorB != SubgraphB.end())) {
1431 if ((*IteratorA) < (*IteratorB))
1432 return true;
1433 else if ((*IteratorA) > (*IteratorB)) {
1434 return false;
1435 } // else, go on to next index
1436 IteratorA++;
1437 IteratorB++;
1438 } // end of while loop
1439 }// end of check in case of equal sizes
1440 }
1441 return false; // if we reach this point, they are equal
1442};
1443
1444
1445/** Combines all KeySets from all orders into single ones (with just unique entries).
1446 * \param *out output stream for debugging
1447 * \param *&FragmentList list to fill
1448 * \param ***FragmentLowerOrdersList
1449 * \param &RootStack stack with all root candidates (unequal to each atom in complete molecule if adaptive scheme is applied)
1450 * \param *mol molecule with atoms and bonds
1451 */
1452int CombineAllOrderListIntoOne(ofstream *out, Graph *&FragmentList, Graph ***FragmentLowerOrdersList, KeyStack &RootStack, molecule *mol)
1453{
1454 int RootNr = 0;
1455 int RootKeyNr = 0;
1456 int StartNr = 0;
1457 int counter = 0;
1458 int NumLevels = 0;
1459 atom *Walker = NULL;
1460
1461 *out << Verbose(0) << "Combining the lists of all orders per order and finally into a single one." << endl;
1462 if (FragmentList == NULL) {
1463 FragmentList = new Graph;
1464 counter = 0;
1465 } else {
1466 counter = FragmentList->size();
1467 }
1468
1469 StartNr = RootStack.back();
1470 do {
1471 RootKeyNr = RootStack.front();
1472 RootStack.pop_front();
1473 Walker = mol->FindAtom(RootKeyNr);
1474 NumLevels = 1 << (Walker->AdaptiveOrder - 1);
1475 for(int i=0;i<NumLevels;i++) {
1476 if (FragmentLowerOrdersList[RootNr][i] != NULL) {
1477 InsertGraphIntoGraph(out, *FragmentList, (*FragmentLowerOrdersList[RootNr][i]), &counter);
1478 }
1479 }
1480 RootStack.push_back(Walker->nr);
1481 RootNr++;
1482 } while (RootKeyNr != StartNr);
1483 return counter;
1484};
1485
1486/** Free's memory allocated for all KeySets from all orders.
1487 * \param *out output stream for debugging
1488 * \param ***FragmentLowerOrdersList
1489 * \param &RootStack stack with all root candidates (unequal to each atom in complete molecule if adaptive scheme is applied)
1490 * \param *mol molecule with atoms and bonds
1491 */
1492void FreeAllOrdersList(ofstream *out, Graph ***FragmentLowerOrdersList, KeyStack &RootStack, molecule *mol)
1493{
1494 *out << Verbose(1) << "Free'ing the lists of all orders per order." << endl;
1495 int RootNr = 0;
1496 int RootKeyNr = 0;
1497 int NumLevels = 0;
1498 atom *Walker = NULL;
1499 while (!RootStack.empty()) {
1500 RootKeyNr = RootStack.front();
1501 RootStack.pop_front();
1502 Walker = mol->FindAtom(RootKeyNr);
1503 NumLevels = 1 << (Walker->AdaptiveOrder - 1);
1504 for(int i=0;i<NumLevels;i++) {
1505 if (FragmentLowerOrdersList[RootNr][i] != NULL) {
1506 delete(FragmentLowerOrdersList[RootNr][i]);
1507 }
1508 }
1509 Free(&FragmentLowerOrdersList[RootNr]);
1510 RootNr++;
1511 }
1512 Free(&FragmentLowerOrdersList);
1513};
1514
1515
1516/** Performs BOSSANOVA decomposition at selected sites, increasing the cutoff by one at these sites.
1517 * -# constructs a complete keyset of the molecule
1518 * -# In a loop over all possible roots from the given rootstack
1519 * -# increases order of root site
1520 * -# calls PowerSetGenerator with this order, the complete keyset and the rootkeynr
1521 * -# for all consecutive lower levels PowerSetGenerator is called with the suborder, the higher order keyset
1522as the restricted one and each site in the set as the root)
1523 * -# these are merged into a fragment list of keysets
1524 * -# All fragment lists (for all orders, i.e. from all destination fields) are merged into one list for return
1525 * Important only is that we create all fragments, it is not important if we create them more than once
1526 * as these copies are filtered out via use of the hash table (KeySet).
1527 * \param *out output stream for debugging
1528 * \param Fragment&*List list of already present keystacks (adaptive scheme) or empty list
1529 * \param &RootStack stack with all root candidates (unequal to each atom in complete molecule if adaptive scheme is applied)
1530 * \param *MinimumRingSize minimum ring size for each atom (molecule::Atomcount)
1531 * \return pointer to Graph list
1532 */
1533void molecule::FragmentBOSSANOVA(ofstream *out, Graph *&FragmentList, KeyStack &RootStack, int *MinimumRingSize)
1534{
1535 Graph ***FragmentLowerOrdersList = NULL;
1536 int NumLevels = 0;
1537 int NumMolecules = 0;
1538 int TotalNumMolecules = 0;
1539 int *NumMoleculesOfOrder = NULL;
1540 int Order = 0;
1541 int UpgradeCount = RootStack.size();
1542 KeyStack FragmentRootStack;
1543 int RootKeyNr = 0;
1544 int RootNr = 0;
1545 struct UniqueFragments FragmentSearch;
1546
1547 *out << Verbose(0) << "Begin of FragmentBOSSANOVA." << endl;
1548
1549 // FragmentLowerOrdersList is a 2D-array of pointer to MoleculeListClass objects, one dimension represents the ANOVA expansion of a single order (i.e. 5)
1550 // with all needed lower orders that are subtracted, the other dimension is the BondOrder (i.e. from 1 to 5)
1551 NumMoleculesOfOrder = Malloc<int>(UpgradeCount, "molecule::FragmentBOSSANOVA: *NumMoleculesOfOrder");
1552 FragmentLowerOrdersList = Malloc<Graph**>(UpgradeCount, "molecule::FragmentBOSSANOVA: ***FragmentLowerOrdersList");
1553
1554 // initialise the fragments structure
1555 FragmentSearch.ShortestPathList = Malloc<int>(AtomCount, "molecule::PowerSetGenerator: *ShortestPathList");
1556 FragmentSearch.FragmentCounter = 0;
1557 FragmentSearch.FragmentSet = new KeySet;
1558 FragmentSearch.Root = FindAtom(RootKeyNr);
1559 for (int i=AtomCount;i--;) {
1560 FragmentSearch.ShortestPathList[i] = -1;
1561 }
1562
1563 // Construct the complete KeySet which we need for topmost level only (but for all Roots)
1564 atom *Walker = start;
1565 KeySet CompleteMolecule;
1566 while (Walker->next != end) {
1567 Walker = Walker->next;
1568 CompleteMolecule.insert(Walker->GetTrueFather()->nr);
1569 }
1570
1571 // this can easily be seen: if Order is 5, then the number of levels for each lower order is the total sum of the number of levels above, as
1572 // each has to be split up. E.g. for the second level we have one from 5th, one from 4th, two from 3th (which in turn is one from 5th, one from 4th),
1573 // hence we have overall four 2th order levels for splitting. This also allows for putting all into a single array (FragmentLowerOrdersList[])
1574 // with the order along the cells as this: 5433222211111111 for BondOrder 5 needing 16=pow(2,5-1) cells (only we use bit-shifting which is faster)
1575 RootNr = 0; // counts through the roots in RootStack
1576 while ((RootNr < UpgradeCount) && (!RootStack.empty())) {
1577 RootKeyNr = RootStack.front();
1578 RootStack.pop_front();
1579 Walker = FindAtom(RootKeyNr);
1580 // check cyclic lengths
1581 //if ((MinimumRingSize[Walker->GetTrueFather()->nr] != -1) && (Walker->GetTrueFather()->AdaptiveOrder+1 > MinimumRingSize[Walker->GetTrueFather()->nr])) {
1582 // *out << Verbose(0) << "Bond order " << Walker->GetTrueFather()->AdaptiveOrder << " of Root " << *Walker << " greater than or equal to Minimum Ring size of " << MinimumRingSize << " found is not allowed." << endl;
1583 //} else
1584 {
1585 // increase adaptive order by one
1586 Walker->GetTrueFather()->AdaptiveOrder++;
1587 Order = Walker->AdaptiveOrder = Walker->GetTrueFather()->AdaptiveOrder;
1588
1589 // initialise Order-dependent entries of UniqueFragments structure
1590 InitialiseSPList(out, Order, FragmentSearch);
1591
1592 // allocate memory for all lower level orders in this 1D-array of ptrs
1593 NumLevels = 1 << (Order-1); // (int)pow(2,Order);
1594 FragmentLowerOrdersList[RootNr] = Malloc<Graph*>(NumLevels, "molecule::FragmentBOSSANOVA: **FragmentLowerOrdersList[]");
1595 for (int i=0;i<NumLevels;i++)
1596 FragmentLowerOrdersList[RootNr][i] = NULL;
1597
1598 // create top order where nothing is reduced
1599 *out << Verbose(0) << "==============================================================================================================" << endl;
1600 *out << Verbose(0) << "Creating KeySets of Bond Order " << Order << " for " << *Walker << ", " << (RootStack.size()-RootNr) << " Roots remaining." << endl; // , NumLevels is " << NumLevels << "
1601
1602 // Create list of Graphs of current Bond Order (i.e. F_{ij})
1603 FragmentLowerOrdersList[RootNr][0] = new Graph;
1604 FragmentSearch.TEFactor = 1.;
1605 FragmentSearch.Leaflet = FragmentLowerOrdersList[RootNr][0]; // set to insertion graph
1606 FragmentSearch.Root = Walker;
1607 NumMoleculesOfOrder[RootNr] = PowerSetGenerator(out, Walker->AdaptiveOrder, FragmentSearch, CompleteMolecule);
1608
1609 // output resulting number
1610 *out << Verbose(1) << "Number of resulting KeySets is: " << NumMoleculesOfOrder[RootNr] << "." << endl;
1611 if (NumMoleculesOfOrder[RootNr] != 0) {
1612 NumMolecules = 0;
1613 } else {
1614 Walker->GetTrueFather()->MaxOrder = true;
1615 }
1616 // now, we have completely filled each cell of FragmentLowerOrdersList[] for the current Walker->AdaptiveOrder
1617 //NumMoleculesOfOrder[Walker->AdaptiveOrder-1] = NumMolecules;
1618 TotalNumMolecules += NumMoleculesOfOrder[RootNr];
1619// *out << Verbose(1) << "Number of resulting molecules for Order " << (int)Walker->GetTrueFather()->AdaptiveOrder << " is: " << NumMoleculesOfOrder[RootNr] << "." << endl;
1620 RootStack.push_back(RootKeyNr); // put back on stack
1621 RootNr++;
1622
1623 // free Order-dependent entries of UniqueFragments structure for next loop cycle
1624 FreeSPList(out, Order, FragmentSearch);
1625 }
1626 }
1627 *out << Verbose(0) << "==============================================================================================================" << endl;
1628 *out << Verbose(1) << "Total number of resulting molecules is: " << TotalNumMolecules << "." << endl;
1629 *out << Verbose(0) << "==============================================================================================================" << endl;
1630
1631 // cleanup FragmentSearch structure
1632 Free(&FragmentSearch.ShortestPathList);
1633 delete(FragmentSearch.FragmentSet);
1634
1635 // now, FragmentLowerOrdersList is complete, it looks - for BondOrder 5 - as this (number is the ANOVA Order of the terms therein)
1636 // 5433222211111111
1637 // 43221111
1638 // 3211
1639 // 21
1640 // 1
1641
1642 // Subsequently, we combine all into a single list (FragmentList)
1643 CombineAllOrderListIntoOne(out, FragmentList, FragmentLowerOrdersList, RootStack, this);
1644 FreeAllOrdersList(out, FragmentLowerOrdersList, RootStack, this);
1645 Free(&NumMoleculesOfOrder);
1646
1647 *out << Verbose(0) << "End of FragmentBOSSANOVA." << endl;
1648};
1649
1650/** Corrects the nuclei position if the fragment was created over the cell borders.
1651 * Scans all bonds, checks the distance, if greater than typical, we have a candidate for the correction.
1652 * We remove the bond whereafter the graph probably separates. Then, we translate the one component periodically
1653 * and re-add the bond. Looping on the distance check.
1654 * \param *out ofstream for debugging messages
1655 */
1656void molecule::ScanForPeriodicCorrection(ofstream *out)
1657{
1658 bond *Binder = NULL;
1659 bond *OtherBinder = NULL;
1660 atom *Walker = NULL;
1661 atom *OtherWalker = NULL;
1662 double *matrix = ReturnFullMatrixforSymmetric(cell_size);
1663 enum Shading *ColorList = NULL;
1664 double tmp;
1665 Vector Translationvector;
1666 //class StackClass<atom *> *CompStack = NULL;
1667 class StackClass<atom *> *AtomStack = new StackClass<atom *>(AtomCount);
1668 bool flag = true;
1669
1670 *out << Verbose(2) << "Begin of ScanForPeriodicCorrection." << endl;
1671
1672 ColorList = Malloc<enum Shading>(AtomCount, "molecule::ScanForPeriodicCorrection: *ColorList");
1673 while (flag) {
1674 // remove bonds that are beyond bonddistance
1675 for(int i=NDIM;i--;)
1676 Translationvector.x[i] = 0.;
1677 // scan all bonds
1678 Binder = first;
1679 flag = false;
1680 while ((!flag) && (Binder->next != last)) {
1681 Binder = Binder->next;
1682 for (int i=NDIM;i--;) {
1683 tmp = fabs(Binder->leftatom->x.x[i] - Binder->rightatom->x.x[i]);
1684 //*out << Verbose(3) << "Checking " << i << "th distance of " << *Binder->leftatom << " to " << *Binder->rightatom << ": " << tmp << "." << endl;
1685 if (tmp > BondDistance) {
1686 OtherBinder = Binder->next; // note down binding partner for later re-insertion
1687 unlink(Binder); // unlink bond
1688 *out << Verbose(2) << "Correcting at bond " << *Binder << "." << endl;
1689 flag = true;
1690 break;
1691 }
1692 }
1693 }
1694 if (flag) {
1695 // create translation vector from their periodically modified distance
1696 for (int i=NDIM;i--;) {
1697 tmp = Binder->leftatom->x.x[i] - Binder->rightatom->x.x[i];
1698 if (fabs(tmp) > BondDistance)
1699 Translationvector.x[i] = (tmp < 0) ? +1. : -1.;
1700 }
1701 Translationvector.MatrixMultiplication(matrix);
1702 //*out << Verbose(3) << "Translation vector is ";
1703 Translationvector.Output(out);
1704 *out << endl;
1705 // apply to all atoms of first component via BFS
1706 for (int i=AtomCount;i--;)
1707 ColorList[i] = white;
1708 AtomStack->Push(Binder->leftatom);
1709 while (!AtomStack->IsEmpty()) {
1710 Walker = AtomStack->PopFirst();
1711 //*out << Verbose (3) << "Current Walker is: " << *Walker << "." << endl;
1712 ColorList[Walker->nr] = black; // mark as explored
1713 Walker->x.AddVector(&Translationvector); // translate
1714 for (int i=0;i<NumberOfBondsPerAtom[Walker->nr];i++) { // go through all binding partners
1715 if (ListOfBondsPerAtom[Walker->nr][i] != Binder) {
1716 OtherWalker = ListOfBondsPerAtom[Walker->nr][i]->GetOtherAtom(Walker);
1717 if (ColorList[OtherWalker->nr] == white) {
1718 AtomStack->Push(OtherWalker); // push if yet unexplored
1719 }
1720 }
1721 }
1722 }
1723 // re-add bond
1724 link(Binder, OtherBinder);
1725 } else {
1726 *out << Verbose(3) << "No corrections for this fragment." << endl;
1727 }
1728 //delete(CompStack);
1729 }
1730
1731 // free allocated space from ReturnFullMatrixforSymmetric()
1732 delete(AtomStack);
1733 Free(&ColorList);
1734 Free(&matrix);
1735 *out << Verbose(2) << "End of ScanForPeriodicCorrection." << endl;
1736};
Note: See TracBrowser for help on using the repository browser.