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