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