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