| [e138de] | 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. | 
|---|
| [1907a7] | 46 | * \return true - add successful | 
|---|
| [e138de] | 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 | Log() << Verbose(0) << "Index\tName\t\tAtoms\tFormula\tCenter\tSize" << endl; | 
|---|
|  | 145 | Log() << Verbose(0) << "-----------------------------------------------" << endl; | 
|---|
|  | 146 | if (ListOfMolecules.size() == 0) | 
|---|
|  | 147 | Log() << Verbose(0) << "\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 | Log() << Verbose(0) << ((*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 | Log() << Verbose(0) << Elemental->symbol << Counts[Elemental->Z]; | 
|---|
|  | 170 | } | 
|---|
|  | 171 | // Center and size | 
|---|
|  | 172 | Log() << Verbose(0) << "\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 | 
|---|
| [1907a7] | 179 | * \return pointer to molecule structure, NULL if not found | 
|---|
| [e138de] | 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 | 
|---|
| [1907a7] | 192 | * \return true - merge successful, false - merge failed (probably due to non-existant indices | 
|---|
| [e138de] | 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 | Log() << Verbose(1) << "ERROR: 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 | Log() << Verbose(1) << "ERROR: 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?" | 
|---|
|  | 405 | << endl; | 
|---|
|  | 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 | return false; | 
|---|
|  | 445 | } | 
|---|
|  | 446 | int k = 0, l; | 
|---|
|  | 447 | while ((!input.eof()) && (k < b)) { | 
|---|
|  | 448 | input.getline(ParsedLine, 1023); | 
|---|
|  | 449 | //Log() << Verbose(0) << "Current Line: " << ParsedLine << endl; | 
|---|
|  | 450 | zeile.str(ParsedLine); | 
|---|
|  | 451 | zeile.clear(); | 
|---|
|  | 452 | l = 0; | 
|---|
|  | 453 | while ((!zeile.eof()) && (l < a)) { | 
|---|
|  | 454 | zeile >> FitConstant[i][l][k]; | 
|---|
|  | 455 | //Log() << Verbose(0) << FitConstant[i][l][k] << "\t"; | 
|---|
|  | 456 | l++; | 
|---|
|  | 457 | } | 
|---|
|  | 458 | //Log() << Verbose(0) << endl; | 
|---|
|  | 459 | k++; | 
|---|
|  | 460 | } | 
|---|
|  | 461 | input.close(); | 
|---|
|  | 462 | } | 
|---|
|  | 463 | for (int k = 0; k < 3; k++) { | 
|---|
|  | 464 | Log() << Verbose(0) << "Constants " << k << ":" << endl; | 
|---|
|  | 465 | for (int j = 0; j < b; j++) { | 
|---|
|  | 466 | for (int i = 0; i < a; i++) { | 
|---|
|  | 467 | Log() << Verbose(0) << FitConstant[k][i][j] << "\t"; | 
|---|
|  | 468 | } | 
|---|
|  | 469 | Log() << Verbose(0) << endl; | 
|---|
|  | 470 | } | 
|---|
|  | 471 | Log() << Verbose(0) << endl; | 
|---|
|  | 472 | } | 
|---|
|  | 473 |  | 
|---|
|  | 474 | // 0d. allocate final correction matrix | 
|---|
|  | 475 | correction = Calloc<double*>(a, "MoleculeListClass::AddHydrogenCorrection: **correction"); | 
|---|
|  | 476 | for (int i = a; i--;) | 
|---|
|  | 477 | correction[i] = Calloc<double>(b, "MoleculeListClass::AddHydrogenCorrection: *correction[]"); | 
|---|
|  | 478 |  | 
|---|
|  | 479 | // 1a. go through every molecule in the list | 
|---|
|  | 480 | for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) { | 
|---|
|  | 481 | // 1b. zero final correction matrix | 
|---|
|  | 482 | for (int k = a; k--;) | 
|---|
|  | 483 | for (int j = b; j--;) | 
|---|
|  | 484 | correction[k][j] = 0.; | 
|---|
|  | 485 | // 2. take every hydrogen that is a saturated one | 
|---|
|  | 486 | Walker = (*ListRunner)->start; | 
|---|
|  | 487 | while (Walker->next != (*ListRunner)->end) { | 
|---|
|  | 488 | Walker = Walker->next; | 
|---|
|  | 489 | //Log() << Verbose(1) << "Walker: " << *Walker << " with first bond " << *(Walker->ListOfBonds.begin()) << "." << endl; | 
|---|
|  | 490 | if ((Walker->type->Z == 1) && ((Walker->father == NULL) | 
|---|
|  | 491 | || (Walker->father->type->Z != 1))) { // if it's a hydrogen | 
|---|
|  | 492 | Runner = (*ListRunner)->start; | 
|---|
|  | 493 | while (Runner->next != (*ListRunner)->end) { | 
|---|
|  | 494 | Runner = Runner->next; | 
|---|
|  | 495 | //Log() << Verbose(2) << "Runner: " << *Runner << " with first bond " << *(Walker->ListOfBonds.begin()) << "." << endl; | 
|---|
|  | 496 | // 3. take every other hydrogen that is the not the first and not bound to same bonding partner | 
|---|
|  | 497 | Binder = *(Runner->ListOfBonds.begin()); | 
|---|
|  | 498 | if ((Runner->type->Z == 1) && (Runner->nr > Walker->nr) && (Binder->GetOtherAtom(Runner) != Binder->GetOtherAtom(Walker))) { // (hydrogens have only one bonding partner!) | 
|---|
|  | 499 | // 4. evaluate the morse potential for each matrix component and add up | 
|---|
|  | 500 | distance = Runner->x.Distance(&Walker->x); | 
|---|
|  | 501 | //Log() << Verbose(0) << "Fragment " << (*ListRunner)->name << ": " << *Runner << "<= " << distance << "=>" << *Walker << ":" << endl; | 
|---|
|  | 502 | for (int k = 0; k < a; k++) { | 
|---|
|  | 503 | for (int j = 0; j < b; j++) { | 
|---|
|  | 504 | switch (k) { | 
|---|
|  | 505 | case 1: | 
|---|
|  | 506 | case 7: | 
|---|
|  | 507 | case 11: | 
|---|
|  | 508 | tmp = pow(FitConstant[0][k][j] * (1. - exp(-FitConstant[1][k][j] * (distance - FitConstant[2][k][j]))), 2); | 
|---|
|  | 509 | break; | 
|---|
|  | 510 | default: | 
|---|
|  | 511 | tmp = FitConstant[0][k][j] * pow(distance, FitConstant[1][k][j]) + FitConstant[2][k][j]; | 
|---|
|  | 512 | }; | 
|---|
|  | 513 | correction[k][j] -= tmp; // ground state is actually lower (disturbed by additional interaction) | 
|---|
|  | 514 | //Log() << Verbose(0) << tmp << "\t"; | 
|---|
|  | 515 | } | 
|---|
|  | 516 | //Log() << Verbose(0) << endl; | 
|---|
|  | 517 | } | 
|---|
|  | 518 | //Log() << Verbose(0) << endl; | 
|---|
|  | 519 | } | 
|---|
|  | 520 | } | 
|---|
|  | 521 | } | 
|---|
|  | 522 | } | 
|---|
|  | 523 | // 5. write final matrix to file | 
|---|
|  | 524 | line = path; | 
|---|
|  | 525 | line.append("/"); | 
|---|
|  | 526 | line += FRAGMENTPREFIX; | 
|---|
|  | 527 | FragmentNumber = FixedDigitNumber(ListOfMolecules.size(), (*ListRunner)->IndexNr); | 
|---|
|  | 528 | line += FragmentNumber; | 
|---|
|  | 529 | delete (FragmentNumber); | 
|---|
|  | 530 | line += HCORRECTIONSUFFIX; | 
|---|
|  | 531 | output.open(line.c_str()); | 
|---|
|  | 532 | output << "Time\t\tTotal\t\tKinetic\t\tNonLocal\tCorrelation\tExchange\tPseudo\t\tHartree\t\t-Gauss\t\tEwald\t\tIonKin\t\tETotal" << endl; | 
|---|
|  | 533 | for (int j = 0; j < b; j++) { | 
|---|
|  | 534 | for (int i = 0; i < a; i++) | 
|---|
|  | 535 | output << correction[i][j] << "\t"; | 
|---|
|  | 536 | output << endl; | 
|---|
|  | 537 | } | 
|---|
|  | 538 | output.close(); | 
|---|
|  | 539 | } | 
|---|
|  | 540 | line = path; | 
|---|
|  | 541 | line.append("/"); | 
|---|
|  | 542 | line += HCORRECTIONSUFFIX; | 
|---|
|  | 543 | output.open(line.c_str()); | 
|---|
|  | 544 | output << "Time\t\tTotal\t\tKinetic\t\tNonLocal\tCorrelation\tExchange\tPseudo\t\tHartree\t\t-Gauss\t\tEwald\t\tIonKin\t\tETotal" << endl; | 
|---|
|  | 545 | for (int j = 0; j < b; j++) { | 
|---|
|  | 546 | for (int i = 0; i < a; i++) | 
|---|
|  | 547 | output << 0 << "\t"; | 
|---|
|  | 548 | output << endl; | 
|---|
|  | 549 | } | 
|---|
|  | 550 | output.close(); | 
|---|
|  | 551 | // 6. free memory of parsed matrices | 
|---|
|  | 552 | for (int k = 0; k < 3; k++) { | 
|---|
|  | 553 | for (int i = a; i--;) { | 
|---|
|  | 554 | Free(&FitConstant[k][i]); | 
|---|
|  | 555 | } | 
|---|
|  | 556 | Free(&FitConstant[k]); | 
|---|
|  | 557 | } | 
|---|
|  | 558 | Free(&FitConstant); | 
|---|
|  | 559 | Log() << Verbose(0) << "done." << endl; | 
|---|
|  | 560 | return true; | 
|---|
|  | 561 | }; | 
|---|
|  | 562 |  | 
|---|
|  | 563 | /** Store force indices, i.e. the connection between the nuclear index in the total molecule config and the respective atom in fragment config. | 
|---|
|  | 564 | * \param *out output stream for debugging | 
|---|
|  | 565 | * \param *path path to file | 
|---|
|  | 566 | * \param *SortIndex Index to map from the BFS labeling to the sequence how of Ion_Type in the config | 
|---|
|  | 567 | * \return true - file written successfully, false - writing failed | 
|---|
|  | 568 | */ | 
|---|
|  | 569 | bool MoleculeListClass::StoreForcesFile(char *path, | 
|---|
|  | 570 | int *SortIndex) | 
|---|
|  | 571 | { | 
|---|
|  | 572 | bool status = true; | 
|---|
|  | 573 | ofstream ForcesFile; | 
|---|
|  | 574 | stringstream line; | 
|---|
|  | 575 | atom *Walker = NULL; | 
|---|
|  | 576 | element *runner = NULL; | 
|---|
|  | 577 |  | 
|---|
|  | 578 | // open file for the force factors | 
|---|
|  | 579 | Log() << Verbose(1) << "Saving  force factors ... "; | 
|---|
|  | 580 | line << path << "/" << FRAGMENTPREFIX << FORCESFILE; | 
|---|
|  | 581 | ForcesFile.open(line.str().c_str(), ios::out); | 
|---|
|  | 582 | if (ForcesFile != NULL) { | 
|---|
|  | 583 | //Log() << Verbose(1) << "Final AtomicForcesList: "; | 
|---|
|  | 584 | //output << prefix << "Forces" << endl; | 
|---|
|  | 585 | for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) { | 
|---|
|  | 586 | runner = (*ListRunner)->elemente->start; | 
|---|
|  | 587 | while (runner->next != (*ListRunner)->elemente->end) { // go through every element | 
|---|
|  | 588 | runner = runner->next; | 
|---|
|  | 589 | if ((*ListRunner)->ElementsInMolecule[runner->Z]) { // if this element got atoms | 
|---|
|  | 590 | Walker = (*ListRunner)->start; | 
|---|
|  | 591 | while (Walker->next != (*ListRunner)->end) { // go through every atom of this element | 
|---|
|  | 592 | Walker = Walker->next; | 
|---|
|  | 593 | if (Walker->type->Z == runner->Z) { | 
|---|
|  | 594 | if ((Walker->GetTrueFather() != NULL) && (Walker->GetTrueFather() != Walker)) {// if there is a rea | 
|---|
|  | 595 | //Log() << Verbose(0) << "Walker is " << *Walker << " with true father " << *( Walker->GetTrueFather()) << ", it | 
|---|
|  | 596 | ForcesFile << SortIndex[Walker->GetTrueFather()->nr] << "\t"; | 
|---|
|  | 597 | } else | 
|---|
|  | 598 | // otherwise a -1 to indicate an added saturation hydrogen | 
|---|
|  | 599 | ForcesFile << "-1\t"; | 
|---|
|  | 600 | } | 
|---|
|  | 601 | } | 
|---|
|  | 602 | } | 
|---|
|  | 603 | } | 
|---|
|  | 604 | ForcesFile << endl; | 
|---|
|  | 605 | } | 
|---|
|  | 606 | ForcesFile.close(); | 
|---|
|  | 607 | Log() << Verbose(1) << "done." << endl; | 
|---|
|  | 608 | } else { | 
|---|
|  | 609 | status = false; | 
|---|
|  | 610 | Log() << Verbose(1) << "failed to open file " << line.str() << "." << endl; | 
|---|
|  | 611 | } | 
|---|
|  | 612 | ForcesFile.close(); | 
|---|
|  | 613 |  | 
|---|
|  | 614 | return status; | 
|---|
|  | 615 | }; | 
|---|
|  | 616 |  | 
|---|
|  | 617 | /** Writes a config file for each molecule in the given \a **FragmentList. | 
|---|
|  | 618 | * \param *out output stream for debugging | 
|---|
|  | 619 | * \param *configuration standard configuration to attach atoms in fragment molecule to. | 
|---|
|  | 620 | * \param *SortIndex Index to map from the BFS labeling to the sequence how of Ion_Type in the config | 
|---|
|  | 621 | * \param DoPeriodic true - call ScanForPeriodicCorrection, false - don't | 
|---|
|  | 622 | * \param DoCentering true - call molecule::CenterEdge(), false - don't | 
|---|
|  | 623 | * \return true - success (each file was written), false - something went wrong. | 
|---|
|  | 624 | */ | 
|---|
|  | 625 | bool MoleculeListClass::OutputConfigForListOfFragments(config *configuration, int *SortIndex) | 
|---|
|  | 626 | { | 
|---|
|  | 627 | ofstream outputFragment; | 
|---|
|  | 628 | char FragmentName[MAXSTRINGSIZE]; | 
|---|
|  | 629 | char PathBackup[MAXSTRINGSIZE]; | 
|---|
|  | 630 | bool result = true; | 
|---|
|  | 631 | bool intermediateResult = true; | 
|---|
|  | 632 | atom *Walker = NULL; | 
|---|
|  | 633 | Vector BoxDimension; | 
|---|
|  | 634 | char *FragmentNumber = NULL; | 
|---|
|  | 635 | char *path = NULL; | 
|---|
|  | 636 | int FragmentCounter = 0; | 
|---|
|  | 637 | ofstream output; | 
|---|
|  | 638 |  | 
|---|
|  | 639 | // store the fragments as config and as xyz | 
|---|
|  | 640 | for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) { | 
|---|
|  | 641 | // save default path as it is changed for each fragment | 
|---|
|  | 642 | path = configuration->GetDefaultPath(); | 
|---|
|  | 643 | if (path != NULL) | 
|---|
|  | 644 | strcpy(PathBackup, path); | 
|---|
|  | 645 | else | 
|---|
|  | 646 | eLog() << Verbose(0) << "OutputConfigForListOfFragments: NULL default path obtained from config!" << endl; | 
|---|
|  | 647 |  | 
|---|
|  | 648 | // correct periodic | 
|---|
|  | 649 | (*ListRunner)->ScanForPeriodicCorrection(); | 
|---|
|  | 650 |  | 
|---|
|  | 651 | // output xyz file | 
|---|
|  | 652 | FragmentNumber = FixedDigitNumber(ListOfMolecules.size(), FragmentCounter++); | 
|---|
|  | 653 | sprintf(FragmentName, "%s/%s%s.conf.xyz", configuration->configpath, FRAGMENTPREFIX, FragmentNumber); | 
|---|
|  | 654 | outputFragment.open(FragmentName, ios::out); | 
|---|
|  | 655 | Log() << Verbose(2) << "Saving bond fragment No. " << FragmentNumber << "/" << FragmentCounter - 1 << " as XYZ ..."; | 
|---|
|  | 656 | if ((intermediateResult = (*ListRunner)->OutputXYZ(&outputFragment))) | 
|---|
|  | 657 | Log() << Verbose(0) << " done." << endl; | 
|---|
|  | 658 | else | 
|---|
|  | 659 | Log() << Verbose(0) << " failed." << endl; | 
|---|
|  | 660 | result = result && intermediateResult; | 
|---|
|  | 661 | outputFragment.close(); | 
|---|
|  | 662 | outputFragment.clear(); | 
|---|
|  | 663 |  | 
|---|
|  | 664 | // list atoms in fragment for debugging | 
|---|
|  | 665 | Log() << Verbose(2) << "Contained atoms: "; | 
|---|
|  | 666 | Walker = (*ListRunner)->start; | 
|---|
|  | 667 | while (Walker->next != (*ListRunner)->end) { | 
|---|
|  | 668 | Walker = Walker->next; | 
|---|
|  | 669 | Log() << Verbose(0) << Walker->Name << " "; | 
|---|
|  | 670 | } | 
|---|
|  | 671 | Log() << Verbose(0) << endl; | 
|---|
|  | 672 |  | 
|---|
|  | 673 | // center on edge | 
|---|
|  | 674 | (*ListRunner)->CenterEdge(&BoxDimension); | 
|---|
|  | 675 | (*ListRunner)->SetBoxDimension(&BoxDimension); // update Box of atoms by boundary | 
|---|
|  | 676 | int j = -1; | 
|---|
|  | 677 | for (int k = 0; k < NDIM; k++) { | 
|---|
|  | 678 | j += k + 1; | 
|---|
|  | 679 | BoxDimension.x[k] = 2.5 * (configuration->GetIsAngstroem() ? 1. : 1. / AtomicLengthToAngstroem); | 
|---|
|  | 680 | (*ListRunner)->cell_size[j] += BoxDimension.x[k] * 2.; | 
|---|
|  | 681 | } | 
|---|
|  | 682 | (*ListRunner)->Translate(&BoxDimension); | 
|---|
|  | 683 |  | 
|---|
|  | 684 | // also calculate necessary orbitals | 
|---|
|  | 685 | (*ListRunner)->CountElements(); // this is a bugfix, atoms should shoulds actually be added correctly to this fragment | 
|---|
|  | 686 | (*ListRunner)->CalculateOrbitals(*configuration); | 
|---|
|  | 687 |  | 
|---|
|  | 688 | // change path in config | 
|---|
|  | 689 | //strcpy(PathBackup, configuration->configpath); | 
|---|
|  | 690 | sprintf(FragmentName, "%s/%s%s/", PathBackup, FRAGMENTPREFIX, FragmentNumber); | 
|---|
|  | 691 | configuration->SetDefaultPath(FragmentName); | 
|---|
|  | 692 |  | 
|---|
|  | 693 | // and save as config | 
|---|
|  | 694 | sprintf(FragmentName, "%s/%s%s.conf", configuration->configpath, FRAGMENTPREFIX, FragmentNumber); | 
|---|
|  | 695 | Log() << Verbose(2) << "Saving bond fragment No. " << FragmentNumber << "/" << FragmentCounter - 1 << " as config ..."; | 
|---|
|  | 696 | if ((intermediateResult = configuration->Save(FragmentName, (*ListRunner)->elemente, (*ListRunner)))) | 
|---|
|  | 697 | Log() << Verbose(0) << " done." << endl; | 
|---|
|  | 698 | else | 
|---|
|  | 699 | Log() << Verbose(0) << " failed." << endl; | 
|---|
|  | 700 | result = result && intermediateResult; | 
|---|
|  | 701 |  | 
|---|
|  | 702 | // restore old config | 
|---|
|  | 703 | configuration->SetDefaultPath(PathBackup); | 
|---|
|  | 704 |  | 
|---|
|  | 705 | // and save as mpqc input file | 
|---|
|  | 706 | sprintf(FragmentName, "%s/%s%s.conf", configuration->configpath, FRAGMENTPREFIX, FragmentNumber); | 
|---|
|  | 707 | Log() << Verbose(2) << "Saving bond fragment No. " << FragmentNumber << "/" << FragmentCounter - 1 << " as mpqc input ..."; | 
|---|
|  | 708 | if ((intermediateResult = configuration->SaveMPQC(FragmentName, (*ListRunner)))) | 
|---|
|  | 709 | Log() << Verbose(2) << " done." << endl; | 
|---|
|  | 710 | else | 
|---|
|  | 711 | Log() << Verbose(0) << " failed." << endl; | 
|---|
|  | 712 |  | 
|---|
|  | 713 | result = result && intermediateResult; | 
|---|
|  | 714 | //outputFragment.close(); | 
|---|
|  | 715 | //outputFragment.clear(); | 
|---|
|  | 716 | Free(&FragmentNumber); | 
|---|
|  | 717 | } | 
|---|
|  | 718 | Log() << Verbose(0) << " done." << endl; | 
|---|
|  | 719 |  | 
|---|
|  | 720 | // printing final number | 
|---|
|  | 721 | Log() << Verbose(2) << "Final number of fragments: " << FragmentCounter << "." << endl; | 
|---|
|  | 722 |  | 
|---|
|  | 723 | return result; | 
|---|
|  | 724 | }; | 
|---|
|  | 725 |  | 
|---|
|  | 726 | /** Counts the number of molecules with the molecule::ActiveFlag set. | 
|---|
| [1907a7] | 727 | * \return number of molecules with ActiveFlag set to true. | 
|---|
| [e138de] | 728 | */ | 
|---|
|  | 729 | int MoleculeListClass::NumberOfActiveMolecules() | 
|---|
|  | 730 | { | 
|---|
|  | 731 | int count = 0; | 
|---|
|  | 732 | for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) | 
|---|
|  | 733 | count += ((*ListRunner)->ActiveFlag ? 1 : 0); | 
|---|
|  | 734 | return count; | 
|---|
|  | 735 | }; | 
|---|
|  | 736 |  | 
|---|
|  | 737 | /** Dissects given \a *mol into connected subgraphs and inserts them as new molecules but with old atoms into \a this. | 
|---|
|  | 738 | * \param *out output stream for debugging | 
|---|
|  | 739 | * \param *mol molecule with atoms to dissect | 
|---|
|  | 740 | * \param *configuration config with BondGraph | 
|---|
|  | 741 | */ | 
|---|
|  | 742 | void MoleculeListClass::DissectMoleculeIntoConnectedSubgraphs(molecule * const mol, config * const configuration) | 
|---|
|  | 743 | { | 
|---|
|  | 744 | // 1. dissect the molecule into connected subgraphs | 
|---|
|  | 745 | configuration ->BG->ConstructBondGraph(mol); | 
|---|
|  | 746 |  | 
|---|
|  | 747 | // 2. scan for connected subgraphs | 
|---|
|  | 748 | MoleculeLeafClass *Subgraphs = NULL;      // list of subgraphs from DFS analysis | 
|---|
|  | 749 | class StackClass<bond *> *BackEdgeStack = NULL; | 
|---|
|  | 750 | Subgraphs = mol->DepthFirstSearchAnalysis(BackEdgeStack); | 
|---|
|  | 751 | delete(BackEdgeStack); | 
|---|
|  | 752 |  | 
|---|
|  | 753 | // 3. dissect (the following construct is needed to have the atoms not in the order of the DFS, but in | 
|---|
|  | 754 | // the original one as parsed in) | 
|---|
|  | 755 | // TODO: Optimize this, when molecules just contain pointer list of global atoms! | 
|---|
|  | 756 |  | 
|---|
|  | 757 | // 4a. create array of molecules to fill | 
|---|
|  | 758 | const int MolCount = Subgraphs->next->Count(); | 
|---|
|  | 759 | molecule **molecules = Malloc<molecule *>(MolCount, "config::Load() - **molecules"); | 
|---|
|  | 760 | for (int i=0;i<MolCount;i++) { | 
|---|
|  | 761 | molecules[i] = (molecule*) new molecule(mol->elemente); | 
|---|
|  | 762 | molecules[i]->ActiveFlag = true; | 
|---|
|  | 763 | insert(molecules[i]); | 
|---|
|  | 764 | } | 
|---|
|  | 765 |  | 
|---|
|  | 766 | // 4b. create and fill map of which atom is associated to which connected molecule (note, counting starts at 1) | 
|---|
|  | 767 | int FragmentCounter = 0; | 
|---|
|  | 768 | int *MolMap = Calloc<int>(mol->AtomCount, "config::Load() - *MolMap"); | 
|---|
|  | 769 | MoleculeLeafClass *MolecularWalker = Subgraphs; | 
|---|
|  | 770 | atom *Walker = NULL; | 
|---|
|  | 771 | while (MolecularWalker->next != NULL) { | 
|---|
|  | 772 | MolecularWalker = MolecularWalker->next; | 
|---|
|  | 773 | Walker = MolecularWalker->Leaf->start; | 
|---|
|  | 774 | while (Walker->next != MolecularWalker->Leaf->end) { | 
|---|
|  | 775 | Walker = Walker->next; | 
|---|
|  | 776 | MolMap[Walker->GetTrueFather()->nr] = FragmentCounter+1; | 
|---|
|  | 777 | } | 
|---|
|  | 778 | FragmentCounter++; | 
|---|
|  | 779 | } | 
|---|
|  | 780 |  | 
|---|
|  | 781 | // 4c. relocate atoms to new molecules and remove from Leafs | 
|---|
|  | 782 | Walker = mol->start; | 
|---|
|  | 783 | while (mol->start->next != mol->end) { | 
|---|
|  | 784 | Walker = mol->start->next; | 
|---|
|  | 785 | if ((Walker->nr <0) || (Walker->nr >= mol->AtomCount)) { | 
|---|
|  | 786 | eLog() << Verbose(0) << "Index of atom " << *Walker << " is invalid!" << endl; | 
|---|
|  | 787 | performCriticalExit(); | 
|---|
|  | 788 | } | 
|---|
|  | 789 | FragmentCounter = MolMap[Walker->nr]; | 
|---|
|  | 790 | if (FragmentCounter != 0) { | 
|---|
|  | 791 | Log() << Verbose(3) << "Re-linking " << *Walker << "..." << endl; | 
|---|
|  | 792 | unlink(Walker); | 
|---|
|  | 793 | molecules[FragmentCounter-1]->AddAtom(Walker);    // counting starts at 1 | 
|---|
|  | 794 | } else { | 
|---|
|  | 795 | eLog() << Verbose(0) << "Atom " << *Walker << " not associated to molecule!" << endl; | 
|---|
|  | 796 | performCriticalExit(); | 
|---|
|  | 797 | } | 
|---|
|  | 798 | } | 
|---|
|  | 799 | // 4d. we don't need to redo bonds, as they are connected subgraphs and still maintained their ListOfBonds | 
|---|
|  | 800 | // 4e. free Leafs | 
|---|
|  | 801 | MolecularWalker = Subgraphs; | 
|---|
|  | 802 | while (MolecularWalker->next != NULL) { | 
|---|
|  | 803 | MolecularWalker = MolecularWalker->next; | 
|---|
|  | 804 | delete(MolecularWalker->previous); | 
|---|
|  | 805 | } | 
|---|
|  | 806 | delete(MolecularWalker); | 
|---|
|  | 807 | Free(&MolMap); | 
|---|
|  | 808 | Free(&molecules); | 
|---|
|  | 809 | Log() << Verbose(1) << "I scanned " << FragmentCounter << " molecules." << endl; | 
|---|
|  | 810 | }; | 
|---|
|  | 811 |  | 
|---|
|  | 812 | /******************************************* Class MoleculeLeafClass ************************************************/ | 
|---|
|  | 813 |  | 
|---|
|  | 814 | /** Constructor for MoleculeLeafClass root leaf. | 
|---|
|  | 815 | * \param *Up Leaf on upper level | 
|---|
|  | 816 | * \param *PreviousLeaf NULL - We are the first leaf on this level, otherwise points to previous in list | 
|---|
|  | 817 | */ | 
|---|
|  | 818 | //MoleculeLeafClass::MoleculeLeafClass(MoleculeLeafClass *Up = NULL, MoleculeLeafClass *Previous = NULL) | 
|---|
|  | 819 | MoleculeLeafClass::MoleculeLeafClass(MoleculeLeafClass *PreviousLeaf = NULL) | 
|---|
|  | 820 | { | 
|---|
|  | 821 | //  if (Up != NULL) | 
|---|
|  | 822 | //    if (Up->DownLeaf == NULL) // are we the first down leaf for the upper leaf? | 
|---|
|  | 823 | //      Up->DownLeaf = this; | 
|---|
|  | 824 | //  UpLeaf = Up; | 
|---|
|  | 825 | //  DownLeaf = NULL; | 
|---|
|  | 826 | Leaf = NULL; | 
|---|
|  | 827 | previous = PreviousLeaf; | 
|---|
|  | 828 | if (previous != NULL) { | 
|---|
|  | 829 | MoleculeLeafClass *Walker = previous->next; | 
|---|
|  | 830 | previous->next = this; | 
|---|
|  | 831 | next = Walker; | 
|---|
|  | 832 | } else { | 
|---|
|  | 833 | next = NULL; | 
|---|
|  | 834 | } | 
|---|
|  | 835 | }; | 
|---|
|  | 836 |  | 
|---|
|  | 837 | /** Destructor for MoleculeLeafClass. | 
|---|
|  | 838 | */ | 
|---|
|  | 839 | MoleculeLeafClass::~MoleculeLeafClass() | 
|---|
|  | 840 | { | 
|---|
|  | 841 | //  if (DownLeaf != NULL) {// drop leaves further down | 
|---|
|  | 842 | //    MoleculeLeafClass *Walker = DownLeaf; | 
|---|
|  | 843 | //    MoleculeLeafClass *Next; | 
|---|
|  | 844 | //    do { | 
|---|
|  | 845 | //      Next = Walker->NextLeaf; | 
|---|
|  | 846 | //      delete(Walker); | 
|---|
|  | 847 | //      Walker = Next; | 
|---|
|  | 848 | //    } while (Walker != NULL); | 
|---|
|  | 849 | //    // Last Walker sets DownLeaf automatically to NULL | 
|---|
|  | 850 | //  } | 
|---|
|  | 851 | // remove the leaf itself | 
|---|
|  | 852 | if (Leaf != NULL) { | 
|---|
|  | 853 | delete (Leaf); | 
|---|
|  | 854 | Leaf = NULL; | 
|---|
|  | 855 | } | 
|---|
|  | 856 | // remove this Leaf from level list | 
|---|
|  | 857 | if (previous != NULL) | 
|---|
|  | 858 | previous->next = next; | 
|---|
|  | 859 | //  } else { // we are first in list (connects to UpLeaf->DownLeaf) | 
|---|
|  | 860 | //    if ((NextLeaf != NULL) && (NextLeaf->UpLeaf == NULL)) | 
|---|
|  | 861 | //      NextLeaf->UpLeaf = UpLeaf;  // either null as we are top level or the upleaf of the first node | 
|---|
|  | 862 | //    if (UpLeaf != NULL) | 
|---|
|  | 863 | //      UpLeaf->DownLeaf = NextLeaf;  // either null as we are only leaf or NextLeaf if we are just the first | 
|---|
|  | 864 | //  } | 
|---|
|  | 865 | //  UpLeaf = NULL; | 
|---|
|  | 866 | if (next != NULL) // are we last in list | 
|---|
|  | 867 | next->previous = previous; | 
|---|
|  | 868 | next = NULL; | 
|---|
|  | 869 | previous = NULL; | 
|---|
|  | 870 | }; | 
|---|
|  | 871 |  | 
|---|
|  | 872 | /** Adds \a molecule leaf to the tree. | 
|---|
|  | 873 | * \param *ptr ptr to molecule to be added | 
|---|
|  | 874 | * \param *Previous previous MoleculeLeafClass referencing level and which on the level | 
|---|
|  | 875 | * \return true - success, false - something went wrong | 
|---|
|  | 876 | */ | 
|---|
|  | 877 | bool MoleculeLeafClass::AddLeaf(molecule *ptr, MoleculeLeafClass *Previous) | 
|---|
|  | 878 | { | 
|---|
|  | 879 | return false; | 
|---|
|  | 880 | }; | 
|---|
|  | 881 |  | 
|---|
|  | 882 | /** Fills the bond structure of this chain list subgraphs that are derived from a complete \a *reference molecule. | 
|---|
|  | 883 | * Calls this routine in each MoleculeLeafClass::next subgraph if it's not NULL. | 
|---|
|  | 884 | * \param *out output stream for debugging | 
|---|
|  | 885 | * \param *reference reference molecule with the bond structure to be copied | 
|---|
|  | 886 | * \param &FragmentCounter Counter needed to address \a **ListOfLocalAtoms | 
|---|
|  | 887 | * \param ***ListOfLocalAtoms Lookup table for each subgraph and index of each atom in \a *reference, may be NULL on start, then it is filled | 
|---|
|  | 888 | * \param FreeList true - ***ListOfLocalAtoms is free'd before return, false - it is not | 
|---|
|  | 889 | * \return true - success, false - faoilure | 
|---|
|  | 890 | */ | 
|---|
|  | 891 | bool MoleculeLeafClass::FillBondStructureFromReference(const molecule * const reference, int &FragmentCounter, atom ***&ListOfLocalAtoms, bool FreeList) | 
|---|
|  | 892 | { | 
|---|
|  | 893 | atom *Walker = NULL; | 
|---|
|  | 894 | atom *OtherWalker = NULL; | 
|---|
|  | 895 | atom *Father = NULL; | 
|---|
|  | 896 | bool status = true; | 
|---|
|  | 897 | int AtomNo; | 
|---|
|  | 898 |  | 
|---|
|  | 899 | Log() << Verbose(1) << "Begin of FillBondStructureFromReference." << endl; | 
|---|
|  | 900 | // fill ListOfLocalAtoms if NULL was given | 
|---|
|  | 901 | if (!FillListOfLocalAtoms(ListOfLocalAtoms, FragmentCounter, reference->AtomCount, FreeList)) { | 
|---|
|  | 902 | Log() << Verbose(1) << "Filling of ListOfLocalAtoms failed." << endl; | 
|---|
|  | 903 | return false; | 
|---|
|  | 904 | } | 
|---|
|  | 905 |  | 
|---|
|  | 906 | if (status) { | 
|---|
|  | 907 | Log() << Verbose(1) << "Creating adjacency list for subgraph " << Leaf << "." << endl; | 
|---|
|  | 908 | // remove every bond from the list | 
|---|
|  | 909 | bond *Binder = NULL; | 
|---|
|  | 910 | while (Leaf->last->previous != Leaf->first) { | 
|---|
|  | 911 | Binder = Leaf->last->previous; | 
|---|
|  | 912 | Binder->leftatom->UnregisterBond(Binder); | 
|---|
|  | 913 | Binder->rightatom->UnregisterBond(Binder); | 
|---|
|  | 914 | removewithoutcheck(Binder); | 
|---|
|  | 915 | } | 
|---|
|  | 916 |  | 
|---|
|  | 917 | Walker = Leaf->start; | 
|---|
|  | 918 | while (Walker->next != Leaf->end) { | 
|---|
|  | 919 | Walker = Walker->next; | 
|---|
|  | 920 | Father = Walker->GetTrueFather(); | 
|---|
|  | 921 | AtomNo = Father->nr; // global id of the current walker | 
|---|
|  | 922 | for (BondList::const_iterator Runner = Father->ListOfBonds.begin(); Runner != Father->ListOfBonds.end(); (++Runner)) { | 
|---|
|  | 923 | OtherWalker = ListOfLocalAtoms[FragmentCounter][(*Runner)->GetOtherAtom(Walker->GetTrueFather())->nr]; // local copy of current bond partner of walker | 
|---|
|  | 924 | if (OtherWalker != NULL) { | 
|---|
|  | 925 | if (OtherWalker->nr > Walker->nr) | 
|---|
|  | 926 | Leaf->AddBond(Walker, OtherWalker, (*Runner)->BondDegree); | 
|---|
|  | 927 | } else { | 
|---|
|  | 928 | Log() << Verbose(1) << "OtherWalker = ListOfLocalAtoms[" << FragmentCounter << "][" << (*Runner)->GetOtherAtom(Walker->GetTrueFather())->nr << "] is NULL!" << endl; | 
|---|
|  | 929 | status = false; | 
|---|
|  | 930 | } | 
|---|
|  | 931 | } | 
|---|
|  | 932 | } | 
|---|
|  | 933 | } | 
|---|
|  | 934 |  | 
|---|
|  | 935 | if ((FreeList) && (ListOfLocalAtoms != NULL)) { | 
|---|
|  | 936 | // free the index lookup list | 
|---|
|  | 937 | Free(&ListOfLocalAtoms[FragmentCounter]); | 
|---|
|  | 938 | if (FragmentCounter == 0) // first fragments frees the initial pointer to list | 
|---|
|  | 939 | Free(&ListOfLocalAtoms); | 
|---|
|  | 940 | } | 
|---|
|  | 941 | Log() << Verbose(1) << "End of FillBondStructureFromReference." << endl; | 
|---|
|  | 942 | return status; | 
|---|
|  | 943 | }; | 
|---|
|  | 944 |  | 
|---|
|  | 945 | /** Fills the root stack for sites to be used as root in fragmentation depending on order or adaptivity criteria | 
|---|
|  | 946 | * Again, as in \sa FillBondStructureFromReference steps recursively through each Leaf in this chain list of molecule's. | 
|---|
|  | 947 | * \param *out output stream for debugging | 
|---|
|  | 948 | * \param *&RootStack stack to be filled | 
|---|
|  | 949 | * \param *AtomMask defines true/false per global Atom::nr to mask in/out each nuclear site | 
|---|
|  | 950 | * \param &FragmentCounter counts through the fragments in this MoleculeLeafClass | 
|---|
|  | 951 | * \return true - stack is non-empty, fragmentation necessary, false - stack is empty, no more sites to update | 
|---|
|  | 952 | */ | 
|---|
|  | 953 | bool MoleculeLeafClass::FillRootStackForSubgraphs(KeyStack *&RootStack, bool *AtomMask, int &FragmentCounter) | 
|---|
|  | 954 | { | 
|---|
|  | 955 | atom *Walker = NULL, *Father = NULL; | 
|---|
|  | 956 |  | 
|---|
|  | 957 | if (RootStack != NULL) { | 
|---|
|  | 958 | // find first root candidates | 
|---|
|  | 959 | if (&(RootStack[FragmentCounter]) != NULL) { | 
|---|
|  | 960 | RootStack[FragmentCounter].clear(); | 
|---|
|  | 961 | Walker = Leaf->start; | 
|---|
|  | 962 | while (Walker->next != Leaf->end) { // go through all (non-hydrogen) atoms | 
|---|
|  | 963 | Walker = Walker->next; | 
|---|
|  | 964 | Father = Walker->GetTrueFather(); | 
|---|
|  | 965 | if (AtomMask[Father->nr]) // apply mask | 
|---|
|  | 966 | #ifdef ADDHYDROGEN | 
|---|
|  | 967 | if (Walker->type->Z != 1) // skip hydrogen | 
|---|
|  | 968 | #endif | 
|---|
|  | 969 | RootStack[FragmentCounter].push_front(Walker->nr); | 
|---|
|  | 970 | } | 
|---|
|  | 971 | if (next != NULL) | 
|---|
|  | 972 | next->FillRootStackForSubgraphs(RootStack, AtomMask, ++FragmentCounter); | 
|---|
|  | 973 | } else { | 
|---|
|  | 974 | Log() << Verbose(1) << "Rootstack[" << FragmentCounter << "] is NULL." << endl; | 
|---|
|  | 975 | return false; | 
|---|
|  | 976 | } | 
|---|
|  | 977 | FragmentCounter--; | 
|---|
|  | 978 | return true; | 
|---|
|  | 979 | } else { | 
|---|
|  | 980 | Log() << Verbose(1) << "Rootstack is NULL." << endl; | 
|---|
|  | 981 | return false; | 
|---|
|  | 982 | } | 
|---|
|  | 983 | }; | 
|---|
|  | 984 |  | 
|---|
|  | 985 | /** Fills a lookup list of father's Atom::nr -> atom for each subgraph. | 
|---|
|  | 986 | * \param *out output stream from debugging | 
|---|
|  | 987 | * \param ***ListOfLocalAtoms Lookup table for each subgraph and index of each atom in global molecule, may be NULL on start, then it is filled | 
|---|
|  | 988 | * \param FragmentCounter counts the fragments as we move along the list | 
|---|
|  | 989 | * \param GlobalAtomCount number of atoms in the complete molecule | 
|---|
|  | 990 | * \param &FreeList true - ***ListOfLocalAtoms is free'd before return, false - it is not | 
|---|
|  | 991 | * \return true - success, false - failure | 
|---|
|  | 992 | */ | 
|---|
|  | 993 | bool MoleculeLeafClass::FillListOfLocalAtoms(atom ***&ListOfLocalAtoms, const int FragmentCounter, const int GlobalAtomCount, bool &FreeList) | 
|---|
|  | 994 | { | 
|---|
|  | 995 | bool status = true; | 
|---|
|  | 996 |  | 
|---|
|  | 997 | if (ListOfLocalAtoms == NULL) { // allocated initial pointer | 
|---|
|  | 998 | // allocate and set each field to NULL | 
|---|
|  | 999 | const int Counter = Count(); | 
|---|
|  | 1000 | ListOfLocalAtoms = Calloc<atom**>(Counter, "MoleculeLeafClass::FillListOfLocalAtoms - ***ListOfLocalAtoms"); | 
|---|
|  | 1001 | if (ListOfLocalAtoms == NULL) { | 
|---|
|  | 1002 | FreeList = FreeList && false; | 
|---|
|  | 1003 | status = false; | 
|---|
|  | 1004 | } | 
|---|
|  | 1005 | } | 
|---|
|  | 1006 |  | 
|---|
|  | 1007 | if ((ListOfLocalAtoms != NULL) && (ListOfLocalAtoms[FragmentCounter] == NULL)) { // allocate and fill list of this fragment/subgraph | 
|---|
|  | 1008 | status = status && CreateFatherLookupTable(Leaf->start, Leaf->end, ListOfLocalAtoms[FragmentCounter], GlobalAtomCount); | 
|---|
|  | 1009 | FreeList = FreeList && true; | 
|---|
|  | 1010 | } | 
|---|
|  | 1011 |  | 
|---|
|  | 1012 | return status; | 
|---|
|  | 1013 | }; | 
|---|
|  | 1014 |  | 
|---|
|  | 1015 | /** The indices per keyset are compared to the respective father's Atom::nr in each subgraph and thus put into \a **&FragmentList. | 
|---|
|  | 1016 | * \param *out output stream fro debugging | 
|---|
|  | 1017 | * \param *reference reference molecule with the bond structure to be copied | 
|---|
|  | 1018 | * \param *KeySetList list with all keysets | 
|---|
|  | 1019 | * \param ***ListOfLocalAtoms Lookup table for each subgraph and index of each atom in global molecule, may be NULL on start, then it is filled | 
|---|
|  | 1020 | * \param **&FragmentList list to be allocated and returned | 
|---|
|  | 1021 | * \param &FragmentCounter counts the fragments as we move along the list | 
|---|
|  | 1022 | * \param FreeList true - ***ListOfLocalAtoms is free'd before return, false - it is not | 
|---|
|  | 1023 | * \retuen true - success, false - failure | 
|---|
|  | 1024 | */ | 
|---|
|  | 1025 | bool MoleculeLeafClass::AssignKeySetsToFragment(molecule *reference, Graph *KeySetList, atom ***&ListOfLocalAtoms, Graph **&FragmentList, int &FragmentCounter, bool FreeList) | 
|---|
|  | 1026 | { | 
|---|
|  | 1027 | bool status = true; | 
|---|
|  | 1028 | int KeySetCounter = 0; | 
|---|
|  | 1029 |  | 
|---|
|  | 1030 | Log() << Verbose(1) << "Begin of AssignKeySetsToFragment." << endl; | 
|---|
|  | 1031 | // fill ListOfLocalAtoms if NULL was given | 
|---|
|  | 1032 | if (!FillListOfLocalAtoms(ListOfLocalAtoms, FragmentCounter, reference->AtomCount, FreeList)) { | 
|---|
|  | 1033 | Log() << Verbose(1) << "Filling of ListOfLocalAtoms failed." << endl; | 
|---|
|  | 1034 | return false; | 
|---|
|  | 1035 | } | 
|---|
|  | 1036 |  | 
|---|
|  | 1037 | // allocate fragment list | 
|---|
|  | 1038 | if (FragmentList == NULL) { | 
|---|
|  | 1039 | KeySetCounter = Count(); | 
|---|
|  | 1040 | FragmentList = Calloc<Graph*>(KeySetCounter, "MoleculeLeafClass::AssignKeySetsToFragment - **FragmentList"); | 
|---|
|  | 1041 | KeySetCounter = 0; | 
|---|
|  | 1042 | } | 
|---|
|  | 1043 |  | 
|---|
|  | 1044 | if ((KeySetList != NULL) && (KeySetList->size() != 0)) { // if there are some scanned keysets at all | 
|---|
|  | 1045 | // assign scanned keysets | 
|---|
|  | 1046 | if (FragmentList[FragmentCounter] == NULL) | 
|---|
|  | 1047 | FragmentList[FragmentCounter] = new Graph; | 
|---|
|  | 1048 | KeySet *TempSet = new KeySet; | 
|---|
|  | 1049 | for (Graph::iterator runner = KeySetList->begin(); runner != KeySetList->end(); runner++) { // key sets contain global numbers! | 
|---|
|  | 1050 | 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 | 
|---|
|  | 1051 | // translate keyset to local numbers | 
|---|
|  | 1052 | for (KeySet::iterator sprinter = (*runner).first.begin(); sprinter != (*runner).first.end(); sprinter++) | 
|---|
|  | 1053 | TempSet->insert(ListOfLocalAtoms[FragmentCounter][reference->FindAtom(*sprinter)->nr]->nr); | 
|---|
|  | 1054 | // insert into FragmentList | 
|---|
|  | 1055 | FragmentList[FragmentCounter]->insert(GraphPair(*TempSet, pair<int, double> (KeySetCounter++, (*runner).second.second))); | 
|---|
|  | 1056 | } | 
|---|
|  | 1057 | TempSet->clear(); | 
|---|
|  | 1058 | } | 
|---|
|  | 1059 | delete (TempSet); | 
|---|
|  | 1060 | if (KeySetCounter == 0) {// if there are no keysets, delete the list | 
|---|
|  | 1061 | Log() << Verbose(1) << "KeySetCounter is zero, deleting FragmentList." << endl; | 
|---|
|  | 1062 | delete (FragmentList[FragmentCounter]); | 
|---|
|  | 1063 | } else | 
|---|
|  | 1064 | Log() << Verbose(1) << KeySetCounter << " keysets were assigned to subgraph " << FragmentCounter << "." << endl; | 
|---|
|  | 1065 | FragmentCounter++; | 
|---|
|  | 1066 | if (next != NULL) | 
|---|
|  | 1067 | next->AssignKeySetsToFragment(reference, KeySetList, ListOfLocalAtoms, FragmentList, FragmentCounter, FreeList); | 
|---|
|  | 1068 | FragmentCounter--; | 
|---|
|  | 1069 | } else | 
|---|
|  | 1070 | Log() << Verbose(1) << "KeySetList is NULL or empty." << endl; | 
|---|
|  | 1071 |  | 
|---|
|  | 1072 | if ((FreeList) && (ListOfLocalAtoms != NULL)) { | 
|---|
|  | 1073 | // free the index lookup list | 
|---|
|  | 1074 | Free(&ListOfLocalAtoms[FragmentCounter]); | 
|---|
|  | 1075 | if (FragmentCounter == 0) // first fragments frees the initial pointer to list | 
|---|
|  | 1076 | Free(&ListOfLocalAtoms); | 
|---|
|  | 1077 | } | 
|---|
|  | 1078 | Log() << Verbose(1) << "End of AssignKeySetsToFragment." << endl; | 
|---|
|  | 1079 | return status; | 
|---|
|  | 1080 | }; | 
|---|
|  | 1081 |  | 
|---|
|  | 1082 | /** Translate list into global numbers (i.e. ones that are valid in "this" molecule, not in MolecularWalker->Leaf) | 
|---|
|  | 1083 | * \param *out output stream for debugging | 
|---|
|  | 1084 | * \param **FragmentList Graph with local numbers per fragment | 
|---|
|  | 1085 | * \param &FragmentCounter counts the fragments as we move along the list | 
|---|
|  | 1086 | * \param &TotalNumberOfKeySets global key set counter | 
|---|
|  | 1087 | * \param &TotalGraph Graph to be filled with global numbers | 
|---|
|  | 1088 | */ | 
|---|
|  | 1089 | void MoleculeLeafClass::TranslateIndicesToGlobalIDs(Graph **FragmentList, int &FragmentCounter, int &TotalNumberOfKeySets, Graph &TotalGraph) | 
|---|
|  | 1090 | { | 
|---|
|  | 1091 | Log() << Verbose(1) << "Begin of TranslateIndicesToGlobalIDs." << endl; | 
|---|
|  | 1092 | KeySet *TempSet = new KeySet; | 
|---|
|  | 1093 | if (FragmentList[FragmentCounter] != NULL) { | 
|---|
|  | 1094 | for (Graph::iterator runner = FragmentList[FragmentCounter]->begin(); runner != FragmentList[FragmentCounter]->end(); runner++) { | 
|---|
|  | 1095 | for (KeySet::iterator sprinter = (*runner).first.begin(); sprinter != (*runner).first.end(); sprinter++) | 
|---|
|  | 1096 | TempSet->insert((Leaf->FindAtom(*sprinter))->GetTrueFather()->nr); | 
|---|
|  | 1097 | TotalGraph.insert(GraphPair(*TempSet, pair<int, double> (TotalNumberOfKeySets++, (*runner).second.second))); | 
|---|
|  | 1098 | TempSet->clear(); | 
|---|
|  | 1099 | } | 
|---|
|  | 1100 | delete (TempSet); | 
|---|
|  | 1101 | } else { | 
|---|
|  | 1102 | Log() << Verbose(1) << "FragmentList is NULL." << endl; | 
|---|
|  | 1103 | } | 
|---|
|  | 1104 | if (next != NULL) | 
|---|
|  | 1105 | next->TranslateIndicesToGlobalIDs(FragmentList, ++FragmentCounter, TotalNumberOfKeySets, TotalGraph); | 
|---|
|  | 1106 | FragmentCounter--; | 
|---|
|  | 1107 | Log() << Verbose(1) << "End of TranslateIndicesToGlobalIDs." << endl; | 
|---|
|  | 1108 | }; | 
|---|
|  | 1109 |  | 
|---|
|  | 1110 | /** Simply counts the number of items in the list, from given MoleculeLeafClass. | 
|---|
|  | 1111 | * \return number of items | 
|---|
|  | 1112 | */ | 
|---|
|  | 1113 | int MoleculeLeafClass::Count() const | 
|---|
|  | 1114 | { | 
|---|
|  | 1115 | if (next != NULL) | 
|---|
|  | 1116 | return next->Count() + 1; | 
|---|
|  | 1117 | else | 
|---|
|  | 1118 | return 1; | 
|---|
|  | 1119 | }; | 
|---|
|  | 1120 |  | 
|---|