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