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