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