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