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