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