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