source: src/moleculelist.cpp@ 266237

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 266237 was 266237, checked in by Frederik Heber <heber@…>, 16 years ago

Huge refactoring: molecule::ListOfBondsPerAtom and molecule::NumberOfBondsPerAtom removed, atom::ListOfBonds introduced. Unit Test for ListOfBonds manipulation introduced.

  • changes to builder.cpp: removed CreateListOfBondsPerAtom() calls, as the creation of the global arrays is not necessary anymore
  • changes to LinkedCell: LinkedCell::CheckBounds(int[NDIM]) does not admonish out of bonds as this is not desired for the local offset which may become out of bounds.
  • changes to lists.hpp templates: BUGFIX: unlink() now sets ->next and ->previous to NULL, cleanup() uses removedwithoutcheck()
  • new templates for molecule.hpp: SumPerAtom() allows for summation of the return value of atom:...() member fiunctions. This is needed e.g. for atom::CorrectBondDegree()

Signed-off-by: Frederik Heber <heber@…>

  • Property mode set to 100755
File size: 39.9 KB
RevLine 
[14de469]1/** \file MoleculeListClass.cpp
[1907a7]2 *
[14de469]3 * Function implementations for the class MoleculeListClass.
[1907a7]4 *
[14de469]5 */
6
[f66195]7#include "atom.hpp"
8#include "bond.hpp"
[f7f7a4]9#include "boundary.hpp"
[a80fbdf]10#include "config.hpp"
[f66195]11#include "element.hpp"
12#include "helpers.hpp"
13#include "linkedcell.hpp"
[cee0b57]14#include "molecule.hpp"
[29812d]15#include "memoryallocator.hpp"
[f66195]16#include "periodentafel.hpp"
[14de469]17
18/*********************************** Functions for class MoleculeListClass *************************/
19
20/** Constructor for MoleculeListClass.
21 */
22MoleculeListClass::MoleculeListClass()
23{
[1907a7]24 // empty lists
25 ListOfMolecules.clear();
26 MaxIndex = 1;
[14de469]27};
28
29/** Destructor for MoleculeListClass.
30 */
31MoleculeListClass::~MoleculeListClass()
32{
[db942e]33 cout << Verbose(3) << this << ": Freeing ListOfMolcules." << endl;
[1907a7]34 for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) {
35 cout << Verbose(4) << "ListOfMolecules: Freeing " << *ListRunner << "." << endl;
36 delete (*ListRunner);
[14de469]37 }
38 cout << Verbose(4) << "Freeing ListOfMolecules." << endl;
[1907a7]39 ListOfMolecules.clear(); // empty list
[14de469]40};
41
[1907a7]42/** Insert a new molecule into the list and set its number.
43 * \param *mol molecule to add to list.
44 * \return true - add successful
[14de469]45 */
[178f92]46void MoleculeListClass::insert(molecule *mol)
[14de469]47{
[1907a7]48 mol->IndexNr = MaxIndex++;
49 ListOfMolecules.push_back(mol);
[14de469]50};
51
[db942e]52/** Compare whether two molecules are equal.
53 * \param *a molecule one
54 * \param *n molecule two
55 * \return lexical value (-1, 0, +1)
56 */
[14de469]57int MolCompare(const void *a, const void *b)
58{
59 int *aList = NULL, *bList = NULL;
60 int Count, Counter, aCounter, bCounter;
61 int flag;
62 atom *aWalker = NULL;
63 atom *bWalker = NULL;
[1907a7]64
[14de469]65 // sort each atom list and put the numbers into a list, then go through
66 //cout << "Comparing fragment no. " << *(molecule **)a << " to " << *(molecule **)b << "." << endl;
[1907a7]67 if ((**(molecule **) a).AtomCount < (**(molecule **) b).AtomCount) {
[14de469]68 return -1;
[1907a7]69 } else {
70 if ((**(molecule **) a).AtomCount > (**(molecule **) b).AtomCount)
71 return +1;
[14de469]72 else {
[1907a7]73 Count = (**(molecule **) a).AtomCount;
[7f3b9d]74 aList = new int[Count];
75 bList = new int[Count];
[1907a7]76
[14de469]77 // fill the lists
[1907a7]78 aWalker = (**(molecule **) a).start;
79 bWalker = (**(molecule **) b).start;
[14de469]80 Counter = 0;
81 aCounter = 0;
82 bCounter = 0;
[1907a7]83 while ((aWalker->next != (**(molecule **) a).end) && (bWalker->next != (**(molecule **) b).end)) {
[14de469]84 aWalker = aWalker->next;
85 bWalker = bWalker->next;
86 if (aWalker->GetTrueFather() == NULL)
87 aList[Counter] = Count + (aCounter++);
88 else
89 aList[Counter] = aWalker->GetTrueFather()->nr;
90 if (bWalker->GetTrueFather() == NULL)
91 bList[Counter] = Count + (bCounter++);
92 else
93 bList[Counter] = bWalker->GetTrueFather()->nr;
94 Counter++;
95 }
96 // check if AtomCount was for real
97 flag = 0;
[1907a7]98 if ((aWalker->next == (**(molecule **) a).end) && (bWalker->next != (**(molecule **) b).end)) {
[14de469]99 flag = -1;
100 } else {
[1907a7]101 if ((aWalker->next != (**(molecule **) a).end) && (bWalker->next == (**(molecule **) b).end))
[14de469]102 flag = 1;
103 }
104 if (flag == 0) {
105 // sort the lists
[1907a7]106 gsl_heapsort(aList, Count, sizeof(int), CompareDoubles);
107 gsl_heapsort(bList, Count, sizeof(int), CompareDoubles);
108 // compare the lists
109
[14de469]110 flag = 0;
[1907a7]111 for (int i = 0; i < Count; i++) {
[14de469]112 if (aList[i] < bList[i]) {
113 flag = -1;
114 } else {
115 if (aList[i] > bList[i])
116 flag = 1;
117 }
118 if (flag != 0)
119 break;
120 }
121 }
[1907a7]122 delete[] (aList);
123 delete[] (bList);
[14de469]124 return flag;
125 }
126 }
[1907a7]127 return -1;
128};
129
130/** Output of a list of all molecules.
131 * \param *out output stream
132 */
133void MoleculeListClass::Enumerate(ofstream *out)
134{
135 element* Elemental = NULL;
136 atom *Walker = NULL;
137 int Counts[MAX_ELEMENTS];
[3af1f0]138 double size=0;
139 Vector Origin;
[1907a7]140
141 // header
[3af1f0]142 *out << "Index\tName\t\tAtoms\tFormula\tCenter\tSize" << endl;
[1907a7]143 cout << Verbose(0) << "-----------------------------------------------" << endl;
144 if (ListOfMolecules.size() == 0)
145 *out << "\tNone" << endl;
146 else {
[3af1f0]147 Origin.Zero();
[1907a7]148 for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) {
149 // reset element counts
150 for (int j = 0; j<MAX_ELEMENTS;j++)
151 Counts[j] = 0;
[3af1f0]152 // count atoms per element and determine size of bounding sphere
153 size=0.;
[1907a7]154 Walker = (*ListRunner)->start;
155 while (Walker->next != (*ListRunner)->end) {
156 Walker = Walker->next;
157 Counts[Walker->type->Z]++;
[3af1f0]158 if (Walker->x.DistanceSquared(&Origin) > size)
159 size = Walker->x.DistanceSquared(&Origin);
[1907a7]160 }
161 // output Index, Name, number of atoms, chemical formula
162 *out << ((*ListRunner)->ActiveFlag ? "*" : " ") << (*ListRunner)->IndexNr << "\t" << (*ListRunner)->name << "\t\t" << (*ListRunner)->AtomCount << "\t";
163 Elemental = (*ListRunner)->elemente->end;
[3af1f0]164 while(Elemental->previous != (*ListRunner)->elemente->start) {
[1907a7]165 Elemental = Elemental->previous;
166 if (Counts[Elemental->Z] != 0)
167 *out << Elemental->symbol << Counts[Elemental->Z];
168 }
[3af1f0]169 // Center and size
170 *out << "\t" << (*ListRunner)->Center << "\t" << sqrt(size) << endl;
[1907a7]171 }
172 }
173};
174
175/** Returns the molecule with the given index \a index.
176 * \param index index of the desired molecule
177 * \return pointer to molecule structure, NULL if not found
178 */
179molecule * MoleculeListClass::ReturnIndex(int index)
180{
[3af1f0]181 for(MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++)
182 if ((*ListRunner)->IndexNr == index)
183 return (*ListRunner);
184 return NULL;
[1907a7]185};
186
187/** Simple merge of two molecules into one.
188 * \param *mol destination molecule
189 * \param *srcmol source molecule
190 * \return true - merge successful, false - merge failed (probably due to non-existant indices
191 */
192bool MoleculeListClass::SimpleMerge(molecule *mol, molecule *srcmol)
193{
194 if (srcmol == NULL)
195 return false;
196
197 // put all molecules of src into mol
198 atom *Walker = srcmol->start;
199 atom *NextAtom = Walker->next;
200 while (NextAtom != srcmol->end) {
201 Walker = NextAtom;
202 NextAtom = Walker->next;
203 srcmol->UnlinkAtom(Walker);
204 mol->AddAtom(Walker);
205 }
206
207 // remove src
208 ListOfMolecules.remove(srcmol);
209 delete(srcmol);
210 return true;
211};
212
213/** Simple add of one molecules into another.
214 * \param *mol destination molecule
215 * \param *srcmol source molecule
216 * \return true - merge successful, false - merge failed (probably due to non-existant indices
217 */
218bool MoleculeListClass::SimpleAdd(molecule *mol, molecule *srcmol)
219{
220 if (srcmol == NULL)
221 return false;
222
223 // put all molecules of src into mol
224 atom *Walker = srcmol->start;
225 atom *NextAtom = Walker->next;
226 while (NextAtom != srcmol->end) {
227 Walker = NextAtom;
228 NextAtom = Walker->next;
229 Walker = mol->AddCopyAtom(Walker);
230 Walker->father = Walker;
231 }
232
233 return true;
234};
235
236/** Simple merge of a given set of molecules into one.
237 * \param *mol destination molecule
238 * \param *src index of set of source molecule
239 * \param N number of source molecules
240 * \return true - merge successful, false - some merges failed (probably due to non-existant indices)
241 */
242bool MoleculeListClass::SimpleMultiMerge(molecule *mol, int *src, int N)
243{
244 bool status = true;
245 // check presence of all source molecules
246 for (int i=0;i<N;i++) {
247 molecule *srcmol = ReturnIndex(src[i]);
248 status = status && SimpleMerge(mol, srcmol);
249 }
250 return status;
251};
252
253/** Simple add of a given set of molecules into one.
254 * \param *mol destination molecule
255 * \param *src index of set of source molecule
256 * \param N number of source molecules
257 * \return true - merge successful, false - some merges failed (probably due to non-existant indices)
258 */
259bool MoleculeListClass::SimpleMultiAdd(molecule *mol, int *src, int N)
260{
261 bool status = true;
262 // check presence of all source molecules
263 for (int i=0;i<N;i++) {
264 molecule *srcmol = ReturnIndex(src[i]);
265 status = status && SimpleAdd(mol, srcmol);
266 }
267 return status;
268};
269
270/** Scatter merge of a given set of molecules into one.
271 * Scatter merge distributes the molecules in such a manner that they don't overlap.
272 * \param *mol destination molecule
273 * \param *src index of set of source molecule
274 * \param N number of source molecules
275 * \return true - merge successful, false - merge failed (probably due to non-existant indices
276 * \TODO find scatter center for each src molecule
277 */
278bool MoleculeListClass::ScatterMerge(molecule *mol, int *src, int N)
279{
280 // check presence of all source molecules
281 for (int i=0;i<N;i++) {
282 // get pointer to src molecule
283 molecule *srcmol = ReturnIndex(src[i]);
284 if (srcmol == NULL)
285 return false;
286 }
287 // adapt each Center
288 for (int i=0;i<N;i++) {
289 // get pointer to src molecule
290 molecule *srcmol = ReturnIndex(src[i]);
291 //srcmol->Center.Zero();
292 srcmol->Translate(&srcmol->Center);
293 }
294 // perform a simple multi merge
295 SimpleMultiMerge(mol, src, N);
296 return true;
297};
298
299/** Embedding merge of a given set of molecules into one.
300 * Embedding merge inserts one molecule into the other.
[f7f7a4]301 * \param *mol destination molecule (fixed one)
302 * \param *srcmol source molecule (variable one, where atoms are taken from)
303 * \return true - merge successful, false - merge failed (probably due to non-existant indices)
304 * \TODO linked cell dimensions for boundary points has to be as big as inner diameter!
[1907a7]305 */
306bool MoleculeListClass::EmbedMerge(molecule *mol, molecule *srcmol)
307{
[f7f7a4]308 if ((srcmol == NULL) || (mol == NULL)) {
309 cout << Verbose(1) << "ERROR: Either fixed or variable molecule is given as NULL." << endl;
[1907a7]310 return false;
[f7f7a4]311 }
[1907a7]312
[f7f7a4]313 // calculate envelope for *mol
314 LinkedCell *LCList = new LinkedCell(mol, 8.);
315 FindNonConvexBorder((ofstream *)&cout, mol, LCList, 4., NULL);
316 if (mol->TesselStruct == NULL) {
317 cout << Verbose(1) << "ERROR: Could not tesselate the fixed molecule." << endl;
318 return false;
319 }
320 delete(LCList);
321 LCList = new LinkedCell(mol->TesselStruct, 8.); // re-create with boundary points only!
[1907a7]322
[f7f7a4]323 // prepare index list for bonds
324 srcmol->CountAtoms((ofstream *)&cout);
325 atom ** CopyAtoms = new atom*[srcmol->AtomCount];
326 for(int i=0;i<srcmol->AtomCount;i++)
327 CopyAtoms[i] = NULL;
328
329 // for each of the source atoms check whether we are in- or outside and add copy atom
330 atom *Walker = srcmol->start;
331 int nr=0;
332 while (Walker->next != srcmol->end) {
333 Walker = Walker->next;
334 cout << Verbose(2) << "INFO: Current Walker is " << *Walker << "." << endl;
335 if (!mol->TesselStruct->IsInnerPoint((ofstream *)&cout, Walker->x, LCList)) {
336 CopyAtoms[Walker->nr] = new atom(Walker);
337 mol->AddAtom(CopyAtoms[Walker->nr]);
338 nr++;
339 } else {
340 // do nothing
341 }
342 }
343 cout << Verbose(1) << nr << " of " << srcmol->AtomCount << " atoms have been merged.";
344
345 // go through all bonds and add as well
346 bond *Binder = srcmol->first;
347 while(Binder->next != srcmol->last) {
348 Binder = Binder->next;
349 cout << Verbose(3) << "Adding Bond between " << *CopyAtoms[Binder->leftatom->nr] << " and " << *CopyAtoms[Binder->rightatom->nr]<< "." << endl;
350 mol->AddBond(CopyAtoms[Binder->leftatom->nr], CopyAtoms[Binder->rightatom->nr], Binder->BondDegree);
351 }
352 delete(LCList);
[1907a7]353 return true;
[14de469]354};
355
356/** Simple output of the pointers in ListOfMolecules.
357 * \param *out output stream
358 */
359void MoleculeListClass::Output(ofstream *out)
360{
[1907a7]361 *out << Verbose(1) << "MoleculeList: ";
362 for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++)
363 *out << *ListRunner << "\t";
[14de469]364 *out << endl;
365};
366
[390248]367/** Calculates necessary hydrogen correction due to unwanted interaction between saturated ones.
368 * If for a pair of two hydrogen atoms a and b, at least is a saturated one, and a and b are not
369 * bonded to the same atom, then we add for this pair a correction term constructed from a Morse
370 * potential function fit to QM calculations with respecting to the interatomic hydrogen distance.
371 * \param *out output stream for debugging
372 * \param *path path to file
373 */
374bool MoleculeListClass::AddHydrogenCorrection(ofstream *out, char *path)
375{
376 atom *Walker = NULL;
377 atom *Runner = NULL;
[266237]378 bond *Binder = NULL;
[390248]379 double ***FitConstant = NULL, **correction = NULL;
[1907a7]380 int a, b;
[390248]381 ofstream output;
382 ifstream input;
383 string line;
384 stringstream zeile;
385 double distance;
386 char ParsedLine[1023];
387 double tmp;
388 char *FragmentNumber = NULL;
389
390 cout << Verbose(1) << "Saving hydrogen saturation correction ... ";
391 // 0. parse in fit constant files that should have the same dimension as the final energy files
392 // 0a. find dimension of matrices with constants
393 line = path;
394 line.append("/");
395 line += FRAGMENTPREFIX;
396 line += "1";
397 line += FITCONSTANTSUFFIX;
398 input.open(line.c_str());
399 if (input == NULL) {
[1907a7]400 cerr << endl << "Unable to open " << line << ", is the directory correct?"
401 << endl;
[390248]402 return false;
403 }
[1907a7]404 a = 0;
405 b = -1; // we overcount by one
[390248]406 while (!input.eof()) {
407 input.getline(ParsedLine, 1023);
408 zeile.str(ParsedLine);
[1907a7]409 int i = 0;
[390248]410 while (!zeile.eof()) {
411 zeile >> distance;
412 i++;
413 }
[1907a7]414 if (i > a)
415 a = i;
[390248]416 b++;
417 }
418 cout << "I recognized " << a << " columns and " << b << " rows, ";
419 input.close();
[1907a7]420
[390248]421 // 0b. allocate memory for constants
[29812d]422 FitConstant = Malloc<double**>(3, "MoleculeListClass::AddHydrogenCorrection: ***FitConstant");
[1907a7]423 for (int k = 0; k < 3; k++) {
[29812d]424 FitConstant[k] = Malloc<double*>(a, "MoleculeListClass::AddHydrogenCorrection: **FitConstant[]");
[1907a7]425 for (int i = a; i--;) {
[29812d]426 FitConstant[k][i] = Malloc<double>(b, "MoleculeListClass::AddHydrogenCorrection: *FitConstant[][]");
[390248]427 }
428 }
429 // 0c. parse in constants
[1907a7]430 for (int i = 0; i < 3; i++) {
[390248]431 line = path;
432 line.append("/");
433 line += FRAGMENTPREFIX;
[1907a7]434 sprintf(ParsedLine, "%d", i + 1);
[390248]435 line += ParsedLine;
436 line += FITCONSTANTSUFFIX;
437 input.open(line.c_str());
438 if (input == NULL) {
439 cerr << endl << "Unable to open " << line << ", is the directory correct?" << endl;
440 return false;
441 }
[1907a7]442 int k = 0, l;
[390248]443 while ((!input.eof()) && (k < b)) {
444 input.getline(ParsedLine, 1023);
445 //cout << "Current Line: " << ParsedLine << endl;
446 zeile.str(ParsedLine);
447 zeile.clear();
448 l = 0;
449 while ((!zeile.eof()) && (l < a)) {
450 zeile >> FitConstant[i][l][k];
451 //cout << FitConstant[i][l][k] << "\t";
452 l++;
453 }
454 //cout << endl;
455 k++;
456 }
457 input.close();
458 }
[1907a7]459 for (int k = 0; k < 3; k++) {
[390248]460 cout << "Constants " << k << ":" << endl;
[1907a7]461 for (int j = 0; j < b; j++) {
462 for (int i = 0; i < a; i++) {
[390248]463 cout << FitConstant[k][i][j] << "\t";
464 }
465 cout << endl;
466 }
467 cout << endl;
468 }
[1907a7]469
[390248]470 // 0d. allocate final correction matrix
[29812d]471 correction = Malloc<double*>(a, "MoleculeListClass::AddHydrogenCorrection: **correction");
[1907a7]472 for (int i = a; i--;)
[29812d]473 correction[i] = Malloc<double>(b, "MoleculeListClass::AddHydrogenCorrection: *correction[]");
[1907a7]474
[390248]475 // 1a. go through every molecule in the list
[1907a7]476 for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) {
[390248]477 // 1b. zero final correction matrix
[1907a7]478 for (int k = a; k--;)
479 for (int j = b; j--;)
[390248]480 correction[k][j] = 0.;
481 // 2. take every hydrogen that is a saturated one
[1907a7]482 Walker = (*ListRunner)->start;
483 while (Walker->next != (*ListRunner)->end) {
[390248]484 Walker = Walker->next;
[266237]485 //cout << Verbose(1) << "Walker: " << *Walker << " with first bond " << *(Walker->ListOfBonds.begin()) << "." << endl;
[1907a7]486 if ((Walker->type->Z == 1) && ((Walker->father == NULL)
487 || (Walker->father->type->Z != 1))) { // if it's a hydrogen
488 Runner = (*ListRunner)->start;
489 while (Runner->next != (*ListRunner)->end) {
[390248]490 Runner = Runner->next;
[266237]491 //cout << Verbose(2) << "Runner: " << *Runner << " with first bond " << *(Walker->ListOfBonds.begin()) << "." << endl;
[390248]492 // 3. take every other hydrogen that is the not the first and not bound to same bonding partner
[266237]493 Binder = *(Runner->ListOfBonds.begin());
494 if ((Runner->type->Z == 1) && (Runner->nr > Walker->nr) && (Binder->GetOtherAtom(Runner) != Binder->GetOtherAtom(Walker))) { // (hydrogens have only one bonding partner!)
[390248]495 // 4. evaluate the morse potential for each matrix component and add up
[1907a7]496 distance = Runner->x.Distance(&Walker->x);
497 //cout << "Fragment " << (*ListRunner)->name << ": " << *Runner << "<= " << distance << "=>" << *Walker << ":" << endl;
498 for (int k = 0; k < a; k++) {
499 for (int j = 0; j < b; j++) {
500 switch (k) {
501 case 1:
502 case 7:
503 case 11:
504 tmp = pow(FitConstant[0][k][j] * (1. - exp(-FitConstant[1][k][j] * (distance - FitConstant[2][k][j]))), 2);
505 break;
506 default:
507 tmp = FitConstant[0][k][j] * pow(distance, FitConstant[1][k][j]) + FitConstant[2][k][j];
[390248]508 };
[1907a7]509 correction[k][j] -= tmp; // ground state is actually lower (disturbed by additional interaction)
[390248]510 //cout << tmp << "\t";
511 }
512 //cout << endl;
513 }
514 //cout << endl;
515 }
516 }
517 }
518 }
519 // 5. write final matrix to file
520 line = path;
521 line.append("/");
522 line += FRAGMENTPREFIX;
[1907a7]523 FragmentNumber = FixedDigitNumber(ListOfMolecules.size(), (*ListRunner)->IndexNr);
[390248]524 line += FragmentNumber;
[1907a7]525 delete (FragmentNumber);
[390248]526 line += HCORRECTIONSUFFIX;
527 output.open(line.c_str());
528 output << "Time\t\tTotal\t\tKinetic\t\tNonLocal\tCorrelation\tExchange\tPseudo\t\tHartree\t\t-Gauss\t\tEwald\t\tIonKin\t\tETotal" << endl;
[1907a7]529 for (int j = 0; j < b; j++) {
530 for (int i = 0; i < a; i++)
[390248]531 output << correction[i][j] << "\t";
532 output << endl;
533 }
534 output.close();
535 }
536 line = path;
537 line.append("/");
538 line += HCORRECTIONSUFFIX;
539 output.open(line.c_str());
540 output << "Time\t\tTotal\t\tKinetic\t\tNonLocal\tCorrelation\tExchange\tPseudo\t\tHartree\t\t-Gauss\t\tEwald\t\tIonKin\t\tETotal" << endl;
[1907a7]541 for (int j = 0; j < b; j++) {
542 for (int i = 0; i < a; i++)
[390248]543 output << 0 << "\t";
544 output << endl;
545 }
546 output.close();
547 // 6. free memory of parsed matrices
[29812d]548 FitConstant = Malloc<double**>(a, "MoleculeListClass::AddHydrogenCorrection: ***FitConstant");
[1907a7]549 for (int k = 0; k < 3; k++) {
[29812d]550 FitConstant[k] = Malloc<double*>(a, "MoleculeListClass::AddHydrogenCorrection: **FitConstant[]");
[1907a7]551 for (int i = a; i--;) {
[29812d]552 FitConstant[k][i] = Malloc<double>(b, "MoleculeListClass::AddHydrogenCorrection: *FitConstant[][]");
[390248]553 }
554 }
555 cout << "done." << endl;
556 return true;
557};
[2459b1]558
559/** Store force indices, i.e. the connection between the nuclear index in the total molecule config and the respective atom in fragment config.
560 * \param *out output stream for debugging
561 * \param *path path to file
562 * \param *SortIndex Index to map from the BFS labeling to the sequence how of Ion_Type in the config
563 * \return true - file written successfully, false - writing failed
564 */
[1907a7]565bool MoleculeListClass::StoreForcesFile(ofstream *out, char *path,
566 int *SortIndex)
[2459b1]567{
568 bool status = true;
569 ofstream ForcesFile;
570 stringstream line;
571 atom *Walker = NULL;
572 element *runner = NULL;
573
574 // open file for the force factors
575 *out << Verbose(1) << "Saving force factors ... ";
576 line << path << "/" << FRAGMENTPREFIX << FORCESFILE;
577 ForcesFile.open(line.str().c_str(), ios::out);
578 if (ForcesFile != NULL) {
579 //cout << Verbose(1) << "Final AtomicForcesList: ";
580 //output << prefix << "Forces" << endl;
[1907a7]581 for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) {
582 runner = (*ListRunner)->elemente->start;
583 while (runner->next != (*ListRunner)->elemente->end) { // go through every element
[2459b1]584 runner = runner->next;
[1907a7]585 if ((*ListRunner)->ElementsInMolecule[runner->Z]) { // if this element got atoms
586 Walker = (*ListRunner)->start;
587 while (Walker->next != (*ListRunner)->end) { // go through every atom of this element
[2459b1]588 Walker = Walker->next;
589 if (Walker->type->Z == runner->Z) {
590 if ((Walker->GetTrueFather() != NULL) && (Walker->GetTrueFather() != Walker)) {// if there is a rea
591 //cout << "Walker is " << *Walker << " with true father " << *( Walker->GetTrueFather()) << ", it
[1907a7]592 ForcesFile << SortIndex[Walker->GetTrueFather()->nr] << "\t";
593 } else
594 // otherwise a -1 to indicate an added saturation hydrogen
595 ForcesFile << "-1\t";
[2459b1]596 }
597 }
[1907a7]598 }
[2459b1]599 }
600 ForcesFile << endl;
601 }
602 ForcesFile.close();
603 *out << Verbose(1) << "done." << endl;
604 } else {
605 status = false;
606 *out << Verbose(1) << "failed to open file " << line.str() << "." << endl;
607 }
608 ForcesFile.close();
[1907a7]609
[2459b1]610 return status;
611};
612
[14de469]613/** Writes a config file for each molecule in the given \a **FragmentList.
[db942e]614 * \param *out output stream for debugging
[14de469]615 * \param *configuration standard configuration to attach atoms in fragment molecule to.
616 * \param *SortIndex Index to map from the BFS labeling to the sequence how of Ion_Type in the config
[85bac0]617 * \param DoPeriodic true - call ScanForPeriodicCorrection, false - don't
618 * \param DoCentering true - call molecule::CenterEdge(), false - don't
[14de469]619 * \return true - success (each file was written), false - something went wrong.
620 */
[d067d45]621bool MoleculeListClass::OutputConfigForListOfFragments(ofstream *out, config *configuration, int *SortIndex)
[14de469]622{
623 ofstream outputFragment;
[c750cc]624 char FragmentName[MAXSTRINGSIZE];
625 char PathBackup[MAXSTRINGSIZE];
[14de469]626 bool result = true;
627 bool intermediateResult = true;
628 atom *Walker = NULL;
[e9b8bb]629 Vector BoxDimension;
[1e9384]630 char *FragmentNumber = NULL;
[5e4058]631 char *path = NULL;
[14de469]632 int FragmentCounter = 0;
[a8aac8d]633 ofstream output;
[1907a7]634
[14de469]635 // store the fragments as config and as xyz
[1907a7]636 for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) {
[db942e]637 // save default path as it is changed for each fragment
[5e4058]638 path = configuration->GetDefaultPath();
639 if (path != NULL)
640 strcpy(PathBackup, path);
641 else
642 cerr << "OutputConfigForListOfFragments: NULL default path obtained from config!" << endl;
[db942e]643
644 // correct periodic
[1907a7]645 (*ListRunner)->ScanForPeriodicCorrection(out);
[db942e]646
647 // output xyz file
[1907a7]648 FragmentNumber = FixedDigitNumber(ListOfMolecules.size(), FragmentCounter++);
649 sprintf(FragmentName, "%s/%s%s.conf.xyz", configuration->configpath, FRAGMENTPREFIX, FragmentNumber);
[db942e]650 outputFragment.open(FragmentName, ios::out);
[1907a7]651 *out << Verbose(2) << "Saving bond fragment No. " << FragmentNumber << "/" << FragmentCounter - 1 << " as XYZ ...";
652 if ((intermediateResult = (*ListRunner)->OutputXYZ(&outputFragment)))
[db942e]653 *out << " done." << endl;
654 else
655 *out << " failed." << endl;
656 result = result && intermediateResult;
657 outputFragment.close();
658 outputFragment.clear();
659
[c1fc22]660 // list atoms in fragment for debugging
[db942e]661 *out << Verbose(2) << "Contained atoms: ";
[1907a7]662 Walker = (*ListRunner)->start;
663 while (Walker->next != (*ListRunner)->end) {
[db942e]664 Walker = Walker->next;
665 *out << Walker->Name << " ";
[14de469]666 }
[db942e]667 *out << endl;
[1907a7]668
[db942e]669 // center on edge
[1907a7]670 (*ListRunner)->CenterEdge(out, &BoxDimension);
671 (*ListRunner)->SetBoxDimension(&BoxDimension); // update Box of atoms by boundary
672 int j = -1;
673 for (int k = 0; k < NDIM; k++) {
674 j += k + 1;
675 BoxDimension.x[k] = 2.5 * (configuration->GetIsAngstroem() ? 1. : 1. / AtomicLengthToAngstroem);
676 (*ListRunner)->cell_size[j] += BoxDimension.x[k] * 2.;
[14de469]677 }
[1907a7]678 (*ListRunner)->Translate(&BoxDimension);
[db942e]679
680 // also calculate necessary orbitals
[1907a7]681 (*ListRunner)->CountElements(); // this is a bugfix, atoms should shoulds actually be added correctly to this fragment
682 (*ListRunner)->CalculateOrbitals(*configuration);
683
[db942e]684 // change path in config
[c1fc22]685 //strcpy(PathBackup, configuration->configpath);
[1907a7]686 sprintf(FragmentName, "%s/%s%s/", PathBackup, FRAGMENTPREFIX, FragmentNumber);
[db942e]687 configuration->SetDefaultPath(FragmentName);
[1907a7]688
[c1fc22]689 // and save as config
[1907a7]690 sprintf(FragmentName, "%s/%s%s.conf", configuration->configpath, FRAGMENTPREFIX, FragmentNumber);
691 *out << Verbose(2) << "Saving bond fragment No. " << FragmentNumber << "/" << FragmentCounter - 1 << " as config ...";
692 if ((intermediateResult = configuration->Save(FragmentName, (*ListRunner)->elemente, (*ListRunner))))
[db942e]693 *out << " done." << endl;
694 else
695 *out << " failed." << endl;
[9a5bcd]696 result = result && intermediateResult;
[c1fc22]697
[db942e]698 // restore old config
699 configuration->SetDefaultPath(PathBackup);
[c1fc22]700
701 // and save as mpqc input file
[1907a7]702 sprintf(FragmentName, "%s/%s%s.conf", configuration->configpath, FRAGMENTPREFIX, FragmentNumber);
703 *out << Verbose(2) << "Saving bond fragment No. " << FragmentNumber << "/" << FragmentCounter - 1 << " as mpqc input ...";
704 if ((intermediateResult = configuration->SaveMPQC(FragmentName, (*ListRunner))))
[c1fc22]705 *out << " done." << endl;
706 else
707 *out << " failed." << endl;
[1907a7]708
[db942e]709 result = result && intermediateResult;
[9a5bcd]710 //outputFragment.close();
711 //outputFragment.clear();
[1907a7]712 delete (FragmentNumber);
[29812d]713 //Free(&FragmentNumber);
[14de469]714 }
[2459b1]715 cout << " done." << endl;
[1907a7]716
[14de469]717 // printing final number
[db942e]718 *out << "Final number of fragments: " << FragmentCounter << "." << endl;
[1907a7]719
[14de469]720 return result;
721};
722
[1907a7]723/** Counts the number of molecules with the molecule::ActiveFlag set.
724 * \return number of molecules with ActiveFlag set to true.
725 */
726int MoleculeListClass::NumberOfActiveMolecules()
727{
728 int count = 0;
729 for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++)
730 count += ((*ListRunner)->ActiveFlag ? 1 : 0);
731 return count;
732};
733
734
[14de469]735/******************************************* Class MoleculeLeafClass ************************************************/
736
737/** Constructor for MoleculeLeafClass root leaf.
738 * \param *Up Leaf on upper level
739 * \param *PreviousLeaf NULL - We are the first leaf on this level, otherwise points to previous in list
740 */
741//MoleculeLeafClass::MoleculeLeafClass(MoleculeLeafClass *Up = NULL, MoleculeLeafClass *Previous = NULL)
742MoleculeLeafClass::MoleculeLeafClass(MoleculeLeafClass *PreviousLeaf = NULL)
743{
[437922]744 // if (Up != NULL)
745 // if (Up->DownLeaf == NULL) // are we the first down leaf for the upper leaf?
746 // Up->DownLeaf = this;
747 // UpLeaf = Up;
748 // DownLeaf = NULL;
[14de469]749 Leaf = NULL;
750 previous = PreviousLeaf;
751 if (previous != NULL) {
752 MoleculeLeafClass *Walker = previous->next;
753 previous->next = this;
754 next = Walker;
755 } else {
756 next = NULL;
757 }
758};
759
760/** Destructor for MoleculeLeafClass.
761 */
762MoleculeLeafClass::~MoleculeLeafClass()
763{
[437922]764 // if (DownLeaf != NULL) {// drop leaves further down
765 // MoleculeLeafClass *Walker = DownLeaf;
766 // MoleculeLeafClass *Next;
767 // do {
768 // Next = Walker->NextLeaf;
769 // delete(Walker);
770 // Walker = Next;
771 // } while (Walker != NULL);
772 // // Last Walker sets DownLeaf automatically to NULL
773 // }
[14de469]774 // remove the leaf itself
775 if (Leaf != NULL) {
[1907a7]776 delete (Leaf);
[14de469]777 Leaf = NULL;
778 }
779 // remove this Leaf from level list
[1907a7]780 if (previous != NULL)
[14de469]781 previous->next = next;
[437922]782 // } else { // we are first in list (connects to UpLeaf->DownLeaf)
783 // if ((NextLeaf != NULL) && (NextLeaf->UpLeaf == NULL))
784 // NextLeaf->UpLeaf = UpLeaf; // either null as we are top level or the upleaf of the first node
785 // if (UpLeaf != NULL)
786 // UpLeaf->DownLeaf = NextLeaf; // either null as we are only leaf or NextLeaf if we are just the first
787 // }
788 // UpLeaf = NULL;
[14de469]789 if (next != NULL) // are we last in list
790 next->previous = previous;
791 next = NULL;
792 previous = NULL;
793};
794
795/** Adds \a molecule leaf to the tree.
796 * \param *ptr ptr to molecule to be added
797 * \param *Previous previous MoleculeLeafClass referencing level and which on the level
798 * \return true - success, false - something went wrong
799 */
800bool MoleculeLeafClass::AddLeaf(molecule *ptr, MoleculeLeafClass *Previous)
801{
802 return false;
803};
[c82f3d]804
805/** Fills the bond structure of this chain list subgraphs that are derived from a complete \a *reference molecule.
806 * Calls this routine in each MoleculeLeafClass::next subgraph if it's not NULL.
807 * \param *out output stream for debugging
808 * \param *reference reference molecule with the bond structure to be copied
809 * \param &FragmentCounter Counter needed to address \a **ListOfLocalAtoms
810 * \param ***ListOfLocalAtoms Lookup table for each subgraph and index of each atom in \a *reference, may be NULL on start, then it is filled
811 * \param FreeList true - ***ListOfLocalAtoms is free'd before return, false - it is not
812 * \return true - success, false - faoilure
813 */
814bool MoleculeLeafClass::FillBondStructureFromReference(ofstream *out, molecule *reference, int &FragmentCounter, atom ***&ListOfLocalAtoms, bool FreeList)
815{
[266237]816 atom *Walker = NULL;
817 atom *OtherWalker = NULL;
818 atom *Father = NULL;
[c82f3d]819 bool status = true;
820 int AtomNo;
821
[617b53]822 *out << Verbose(1) << "Begin of FillBondStructureFromReference." << endl;
[d2a294]823 // fill ListOfLocalAtoms if NULL was given
824 if (!FillListOfLocalAtoms(out, ListOfLocalAtoms, FragmentCounter, reference->AtomCount, FreeList)) {
825 *out << Verbose(1) << "Filling of ListOfLocalAtoms failed." << endl;
826 return false;
[c82f3d]827 }
[1907a7]828
[c82f3d]829 if (status) {
[1907a7]830 *out << Verbose(1) << "Creating adjacency list for subgraph " << this
831 << "." << endl;
[c82f3d]832 Walker = Leaf->start;
833 while (Walker->next != Leaf->end) {
834 Walker = Walker->next;
[266237]835 Father = Walker->GetTrueFather();
836 AtomNo = Father->nr; // global id of the current walker
837 for (BondList::const_iterator Runner = Father->ListOfBonds.begin(); Runner != Father->ListOfBonds.end(); (++Runner)) {
838 OtherWalker = ListOfLocalAtoms[FragmentCounter][(*Runner)->GetOtherAtom(Walker->GetTrueFather())->nr]; // local copy of current bond partner of walker
[c82f3d]839 if (OtherWalker != NULL) {
840 if (OtherWalker->nr > Walker->nr)
[266237]841 Leaf->AddBond(Walker, OtherWalker, (*Runner)->BondDegree);
[c82f3d]842 } else {
[266237]843 *out << Verbose(1) << "OtherWalker = ListOfLocalAtoms[" << FragmentCounter << "][" << (*Runner)->GetOtherAtom(Walker->GetTrueFather())->nr << "] is NULL!" << endl;
[c82f3d]844 status = false;
845 }
846 }
847 }
848 FragmentCounter++;
849 if (next != NULL)
850 status = next->FillBondStructureFromReference(out, reference, FragmentCounter, ListOfLocalAtoms);
[617b53]851 FragmentCounter--;
[c82f3d]852 }
[1907a7]853
[617b53]854 if ((FreeList) && (ListOfLocalAtoms != NULL)) {
[c82f3d]855 // free the index lookup list
[29812d]856 Free(&ListOfLocalAtoms[FragmentCounter]);
[3ccc3e]857 if (FragmentCounter == 0) // first fragments frees the initial pointer to list
[29812d]858 Free(&ListOfLocalAtoms);
[c82f3d]859 }
[386aa2]860 FragmentCounter--;
[617b53]861 *out << Verbose(1) << "End of FillBondStructureFromReference." << endl;
[c82f3d]862 return status;
863};
864
[386aa2]865/** Fills the root stack for sites to be used as root in fragmentation depending on order or adaptivity criteria
866 * Again, as in \sa FillBondStructureFromReference steps recursively through each Leaf in this chain list of molecule's.
867 * \param *out output stream for debugging
868 * \param *&RootStack stack to be filled
[5de3c9]869 * \param *AtomMask defines true/false per global Atom::nr to mask in/out each nuclear site
[386aa2]870 * \param &FragmentCounter counts through the fragments in this MoleculeLeafClass
871 * \return true - stack is non-empty, fragmentation necessary, false - stack is empty, no more sites to update
872 */
[1907a7]873bool MoleculeLeafClass::FillRootStackForSubgraphs(ofstream *out,
874 KeyStack *&RootStack, bool *AtomMask, int &FragmentCounter)
[386aa2]875{
[5de3c9]876 atom *Walker = NULL, *Father = NULL;
[386aa2]877
878 if (RootStack != NULL) {
[1907a7]879 // find first root candidates
[de293ac]880 if (&(RootStack[FragmentCounter]) != NULL) {
[1907a7]881 RootStack[FragmentCounter].clear();
[de293ac]882 Walker = Leaf->start;
883 while (Walker->next != Leaf->end) { // go through all (non-hydrogen) atoms
884 Walker = Walker->next;
885 Father = Walker->GetTrueFather();
886 if (AtomMask[Father->nr]) // apply mask
[1907a7]887#ifdef ADDHYDROGEN
[de293ac]888 if (Walker->type->Z != 1) // skip hydrogen
[1907a7]889#endif
890 RootStack[FragmentCounter].push_front(Walker->nr);
[d2a294]891 }
[de293ac]892 if (next != NULL)
893 next->FillRootStackForSubgraphs(out, RootStack, AtomMask, ++FragmentCounter);
[1907a7]894 } else {
895 *out << Verbose(1) << "Rootstack[" << FragmentCounter << "] is NULL." << endl;
[de293ac]896 return false;
[386aa2]897 }
[d2a294]898 FragmentCounter--;
[386aa2]899 return true;
[d2a294]900 } else {
901 *out << Verbose(1) << "Rootstack is NULL." << endl;
[386aa2]902 return false;
[d2a294]903 }
904};
905
906/** Fills a lookup list of father's Atom::nr -> atom for each subgraph.
907 * \param *out output stream fro debugging
908 * \param ***ListOfLocalAtoms Lookup table for each subgraph and index of each atom in global molecule, may be NULL on start, then it is filled
[3ccc3e]909 * \param FragmentCounter counts the fragments as we move along the list
[d2a294]910 * \param GlobalAtomCount number of atoms in the complete molecule
911 * \param &FreeList true - ***ListOfLocalAtoms is free'd before return, false - it is not
912 * \return true - succes, false - failure
913 */
[3ccc3e]914bool MoleculeLeafClass::FillListOfLocalAtoms(ofstream *out, atom ***&ListOfLocalAtoms, const int FragmentCounter, const int GlobalAtomCount, bool &FreeList)
[d2a294]915{
916 bool status = true;
[1907a7]917
[d2a294]918 int Counter = Count();
919 if (ListOfLocalAtoms == NULL) { // allocated initial pointer
920 // allocate and set each field to NULL
[29812d]921 ListOfLocalAtoms = Malloc<atom**>(Counter, "MoleculeLeafClass::FillBondStructureFromReference - ***ListOfLocalAtoms");
[d2a294]922 if (ListOfLocalAtoms != NULL) {
[1907a7]923 for (int i = Counter; i--;)
[d2a294]924 ListOfLocalAtoms[i] = NULL;
925 FreeList = FreeList && true;
926 } else
927 status = false;
928 }
[1907a7]929
[d2a294]930 if ((ListOfLocalAtoms != NULL) && (ListOfLocalAtoms[FragmentCounter] == NULL)) { // allocate and fill list of this fragment/subgraph
931 status = status && CreateFatherLookupTable(out, Leaf->start, Leaf->end, ListOfLocalAtoms[FragmentCounter], GlobalAtomCount);
932 FreeList = FreeList && true;
933 }
[1907a7]934
[d2a294]935 return status;
936};
937
938/** The indices per keyset are compared to the respective father's Atom::nr in each subgraph and thus put into \a **&FragmentList.
939 * \param *out output stream fro debugging
940 * \param *reference reference molecule with the bond structure to be copied
941 * \param *KeySetList list with all keysets
942 * \param ***ListOfLocalAtoms Lookup table for each subgraph and index of each atom in global molecule, may be NULL on start, then it is filled
943 * \param **&FragmentList list to be allocated and returned
944 * \param &FragmentCounter counts the fragments as we move along the list
945 * \param FreeList true - ***ListOfLocalAtoms is free'd before return, false - it is not
946 * \retuen true - success, false - failure
947 */
[1907a7]948bool MoleculeLeafClass::AssignKeySetsToFragment(ofstream *out,
949 molecule *reference, Graph *KeySetList, atom ***&ListOfLocalAtoms,
950 Graph **&FragmentList, int &FragmentCounter, bool FreeList)
[d2a294]951{
952 bool status = true;
953 int KeySetCounter = 0;
[1907a7]954
[617b53]955 *out << Verbose(1) << "Begin of AssignKeySetsToFragment." << endl;
[d2a294]956 // fill ListOfLocalAtoms if NULL was given
957 if (!FillListOfLocalAtoms(out, ListOfLocalAtoms, FragmentCounter, reference->AtomCount, FreeList)) {
958 *out << Verbose(1) << "Filling of ListOfLocalAtoms failed." << endl;
959 return false;
960 }
961
962 // allocate fragment list
963 if (FragmentList == NULL) {
964 KeySetCounter = Count();
[29812d]965 FragmentList = Malloc<Graph*>(KeySetCounter, "MoleculeLeafClass::AssignKeySetsToFragment - **FragmentList");
[1907a7]966 for (int i = KeySetCounter; i--;)
[d2a294]967 FragmentList[i] = NULL;
968 KeySetCounter = 0;
969 }
[1907a7]970
[d2a294]971 if ((KeySetList != NULL) && (KeySetList->size() != 0)) { // if there are some scanned keysets at all
972 // assign scanned keysets
973 if (FragmentList[FragmentCounter] == NULL)
974 FragmentList[FragmentCounter] = new Graph;
975 KeySet *TempSet = new KeySet;
[1907a7]976 for (Graph::iterator runner = KeySetList->begin(); runner != KeySetList->end(); runner++) { // key sets contain global numbers!
977 if (ListOfLocalAtoms[FragmentCounter][reference->FindAtom(*((*runner).first.begin()))->nr] != NULL) {// as we may assume that that bond structure is unchanged, we only test the first key in each set
[d2a294]978 // translate keyset to local numbers
[1907a7]979 for (KeySet::iterator sprinter = (*runner).first.begin(); sprinter != (*runner).first.end(); sprinter++)
[d2a294]980 TempSet->insert(ListOfLocalAtoms[FragmentCounter][reference->FindAtom(*sprinter)->nr]->nr);
981 // insert into FragmentList
[1907a7]982 FragmentList[FragmentCounter]->insert(GraphPair(*TempSet, pair<int, double> (KeySetCounter++, (*runner).second.second)));
[d2a294]983 }
984 TempSet->clear();
985 }
[1907a7]986 delete (TempSet);
[d2a294]987 if (KeySetCounter == 0) {// if there are no keysets, delete the list
988 *out << Verbose(1) << "KeySetCounter is zero, deleting FragmentList." << endl;
[1907a7]989 delete (FragmentList[FragmentCounter]);
[d2a294]990 } else
991 *out << Verbose(1) << KeySetCounter << " keysets were assigned to subgraph " << FragmentCounter << "." << endl;
992 FragmentCounter++;
993 if (next != NULL)
994 next->AssignKeySetsToFragment(out, reference, KeySetList, ListOfLocalAtoms, FragmentList, FragmentCounter, FreeList);
[617b53]995 FragmentCounter--;
[d2a294]996 } else
997 *out << Verbose(1) << "KeySetList is NULL or empty." << endl;
[1907a7]998
[617b53]999 if ((FreeList) && (ListOfLocalAtoms != NULL)) {
[3ccc3e]1000 // free the index lookup list
[29812d]1001 Free(&ListOfLocalAtoms[FragmentCounter]);
[3ccc3e]1002 if (FragmentCounter == 0) // first fragments frees the initial pointer to list
[29812d]1003 Free(&ListOfLocalAtoms);
[3ccc3e]1004 }
[617b53]1005 *out << Verbose(1) << "End of AssignKeySetsToFragment." << endl;
[d2a294]1006 return status;
[386aa2]1007};
[c82f3d]1008
[87f6c9]1009/** Translate list into global numbers (i.e. ones that are valid in "this" molecule, not in MolecularWalker->Leaf)
1010 * \param *out output stream for debugging
1011 * \param **FragmentList Graph with local numbers per fragment
1012 * \param &FragmentCounter counts the fragments as we move along the list
1013 * \param &TotalNumberOfKeySets global key set counter
1014 * \param &TotalGraph Graph to be filled with global numbers
[1907a7]1015 */
1016void MoleculeLeafClass::TranslateIndicesToGlobalIDs(ofstream *out,
1017 Graph **FragmentList, int &FragmentCounter, int &TotalNumberOfKeySets,
1018 Graph &TotalGraph)
[87f6c9]1019{
[362b0e]1020 *out << Verbose(1) << "Begin of TranslateIndicesToGlobalIDs." << endl;
[87f6c9]1021 KeySet *TempSet = new KeySet;
[de293ac]1022 if (FragmentList[FragmentCounter] != NULL) {
[1907a7]1023 for (Graph::iterator runner = FragmentList[FragmentCounter]->begin(); runner != FragmentList[FragmentCounter]->end(); runner++) {
1024 for (KeySet::iterator sprinter = (*runner).first.begin(); sprinter != (*runner).first.end(); sprinter++)
[de293ac]1025 TempSet->insert((Leaf->FindAtom(*sprinter))->GetTrueFather()->nr);
[1907a7]1026 TotalGraph.insert(GraphPair(*TempSet, pair<int, double> (TotalNumberOfKeySets++, (*runner).second.second)));
[de293ac]1027 TempSet->clear();
1028 }
[1907a7]1029 delete (TempSet);
[de293ac]1030 } else {
1031 *out << Verbose(1) << "FragmentList is NULL." << endl;
[87f6c9]1032 }
1033 if (next != NULL)
1034 next->TranslateIndicesToGlobalIDs(out, FragmentList, ++FragmentCounter, TotalNumberOfKeySets, TotalGraph);
1035 FragmentCounter--;
[362b0e]1036 *out << Verbose(1) << "End of TranslateIndicesToGlobalIDs." << endl;
[87f6c9]1037};
1038
[386aa2]1039/** Simply counts the number of items in the list, from given MoleculeLeafClass.
1040 * \return number of items
1041 */
[d2a294]1042int MoleculeLeafClass::Count() const
[386aa2]1043{
1044 if (next != NULL)
[1907a7]1045 return next->Count() + 1;
[386aa2]1046 else
[1907a7]1047 return 1;
[386aa2]1048};
[1907a7]1049
Note: See TracBrowser for help on using the repository browser.