| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
 | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [e138de] | 8 | /** \file MoleculeListClass.cpp
 | 
|---|
 | 9 |  *
 | 
|---|
 | 10 |  * Function implementations for the class MoleculeListClass.
 | 
|---|
 | 11 |  *
 | 
|---|
 | 12 |  */
 | 
|---|
 | 13 | 
 | 
|---|
| [bf3817] | 14 | // include config.h
 | 
|---|
| [aafd77] | 15 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 16 | #include <config.h>
 | 
|---|
 | 17 | #endif
 | 
|---|
 | 18 | 
 | 
|---|
| [ad011c] | 19 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [112b09] | 20 | 
 | 
|---|
| [49e1ae] | 21 | #include <cstring>
 | 
|---|
 | 22 | 
 | 
|---|
| [aafd77] | 23 | #include <gsl/gsl_inline.h>
 | 
|---|
 | 24 | #include <gsl/gsl_heapsort.h>
 | 
|---|
 | 25 | 
 | 
|---|
| [e138de] | 26 | #include "atom.hpp"
 | 
|---|
| [129204] | 27 | #include "Bond/bond.hpp"
 | 
|---|
| [e138de] | 28 | #include "boundary.hpp"
 | 
|---|
| [583081] | 29 | #include "Box.hpp"
 | 
|---|
 | 30 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 31 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 32 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| [e138de] | 33 | #include "config.hpp"
 | 
|---|
 | 34 | #include "element.hpp"
 | 
|---|
| [129204] | 35 | #include "Graph/BondGraph.hpp"
 | 
|---|
| [583081] | 36 | #include "Helpers/fast_functions.hpp"
 | 
|---|
| [952f38] | 37 | #include "Helpers/helpers.hpp"
 | 
|---|
| [583081] | 38 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
| [e138de] | 39 | #include "linkedcell.hpp"
 | 
|---|
 | 40 | #include "molecule.hpp"
 | 
|---|
 | 41 | #include "periodentafel.hpp"
 | 
|---|
| [88b400] | 42 | #include "tesselation.hpp"
 | 
|---|
| [583081] | 43 | #include "World.hpp"
 | 
|---|
 | 44 | #include "WorldTime.hpp"
 | 
|---|
| [920c70] | 45 | 
 | 
|---|
| [e138de] | 46 | /*********************************** Functions for class MoleculeListClass *************************/
 | 
|---|
 | 47 | 
 | 
|---|
 | 48 | /** Constructor for MoleculeListClass.
 | 
|---|
 | 49 |  */
 | 
|---|
| [cbc5fb] | 50 | MoleculeListClass::MoleculeListClass(World *_world) :
 | 
|---|
| [cd5047] | 51 |   Observable("MoleculeListClass"),
 | 
|---|
| [81a9bc] | 52 |   MaxIndex(1),
 | 
|---|
 | 53 |   world(_world)
 | 
|---|
| [97b825] | 54 | {};
 | 
|---|
| [e138de] | 55 | 
 | 
|---|
 | 56 | /** Destructor for MoleculeListClass.
 | 
|---|
 | 57 |  */
 | 
|---|
 | 58 | MoleculeListClass::~MoleculeListClass()
 | 
|---|
 | 59 | {
 | 
|---|
| [bd6bfa] | 60 |   DoLog(4) && (Log() << Verbose(4) << "Clearing ListOfMolecules." << endl);
 | 
|---|
 | 61 |   for(MoleculeList::iterator MolRunner = ListOfMolecules.begin(); MolRunner != ListOfMolecules.end(); ++MolRunner)
 | 
|---|
 | 62 |     (*MolRunner)->signOff(this);
 | 
|---|
| [e138de] | 63 |   ListOfMolecules.clear(); // empty list
 | 
|---|
 | 64 | };
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 | /** Insert a new molecule into the list and set its number.
 | 
|---|
 | 67 |  * \param *mol molecule to add to list.
 | 
|---|
 | 68 |  */
 | 
|---|
 | 69 | void MoleculeListClass::insert(molecule *mol)
 | 
|---|
 | 70 | {
 | 
|---|
| [2ba827] | 71 |   OBSERVE;
 | 
|---|
| [e138de] | 72 |   mol->IndexNr = MaxIndex++;
 | 
|---|
 | 73 |   ListOfMolecules.push_back(mol);
 | 
|---|
| [520c8b] | 74 |   mol->signOn(this);
 | 
|---|
| [e138de] | 75 | };
 | 
|---|
 | 76 | 
 | 
|---|
| [bd6bfa] | 77 | /** Erases a molecule from the list.
 | 
|---|
 | 78 |  * \param *mol molecule to add to list.
 | 
|---|
 | 79 |  */
 | 
|---|
 | 80 | void MoleculeListClass::erase(molecule *mol)
 | 
|---|
 | 81 | {
 | 
|---|
 | 82 |   OBSERVE;
 | 
|---|
 | 83 |   mol->signOff(this);
 | 
|---|
 | 84 |   ListOfMolecules.remove(mol);
 | 
|---|
 | 85 | };
 | 
|---|
 | 86 | 
 | 
|---|
| [a0064e] | 87 | /** Comparison function for two values.
 | 
|---|
 | 88 |  * \param *a
 | 
|---|
 | 89 |  * \param *b
 | 
|---|
 | 90 |  * \return <0, \a *a less than \a *b, ==0 if equal, >0 \a *a greater than \a *b
 | 
|---|
 | 91 |  */
 | 
|---|
 | 92 | int CompareDoubles (const void * a, const void * b)
 | 
|---|
 | 93 | {
 | 
|---|
 | 94 |   if (*(double *)a > *(double *)b)
 | 
|---|
 | 95 |     return -1;
 | 
|---|
 | 96 |   else if (*(double *)a < *(double *)b)
 | 
|---|
 | 97 |     return 1;
 | 
|---|
 | 98 |   else
 | 
|---|
 | 99 |     return 0;
 | 
|---|
 | 100 | };
 | 
|---|
 | 101 | 
 | 
|---|
 | 102 | 
 | 
|---|
| [e138de] | 103 | /** Compare whether two molecules are equal.
 | 
|---|
 | 104 |  * \param *a molecule one
 | 
|---|
 | 105 |  * \param *n molecule two
 | 
|---|
 | 106 |  * \return lexical value (-1, 0, +1)
 | 
|---|
 | 107 |  */
 | 
|---|
 | 108 | int MolCompare(const void *a, const void *b)
 | 
|---|
 | 109 | {
 | 
|---|
 | 110 |   int *aList = NULL, *bList = NULL;
 | 
|---|
 | 111 |   int Count, Counter, aCounter, bCounter;
 | 
|---|
 | 112 |   int flag;
 | 
|---|
 | 113 | 
 | 
|---|
 | 114 |   // sort each atom list and put the numbers into a list, then go through
 | 
|---|
 | 115 |   //Log() << Verbose(0) << "Comparing fragment no. " << *(molecule **)a << " to " << *(molecule **)b << "." << endl;
 | 
|---|
| [ea7176] | 116 |   // Yes those types are awkward... but check it for yourself it checks out this way
 | 
|---|
 | 117 |   molecule *const *mol1_ptr= static_cast<molecule *const *>(a);
 | 
|---|
 | 118 |   molecule *mol1 = *mol1_ptr;
 | 
|---|
 | 119 |   molecule *const *mol2_ptr= static_cast<molecule *const *>(b);
 | 
|---|
 | 120 |   molecule *mol2 = *mol2_ptr;
 | 
|---|
 | 121 |   if (mol1->getAtomCount() < mol2->getAtomCount()) {
 | 
|---|
| [e138de] | 122 |     return -1;
 | 
|---|
 | 123 |   } else {
 | 
|---|
| [ea7176] | 124 |     if (mol1->getAtomCount() > mol2->getAtomCount())
 | 
|---|
| [e138de] | 125 |       return +1;
 | 
|---|
 | 126 |     else {
 | 
|---|
| [ea7176] | 127 |       Count = mol1->getAtomCount();
 | 
|---|
| [e138de] | 128 |       aList = new int[Count];
 | 
|---|
 | 129 |       bList = new int[Count];
 | 
|---|
 | 130 | 
 | 
|---|
 | 131 |       // fill the lists
 | 
|---|
 | 132 |       Counter = 0;
 | 
|---|
 | 133 |       aCounter = 0;
 | 
|---|
 | 134 |       bCounter = 0;
 | 
|---|
| [ea7176] | 135 |       molecule::const_iterator aiter = mol1->begin();
 | 
|---|
 | 136 |       molecule::const_iterator biter = mol2->begin();
 | 
|---|
 | 137 |       for (;(aiter != mol1->end()) && (biter != mol2->end());
 | 
|---|
| [9879f6] | 138 |           ++aiter, ++biter) {
 | 
|---|
 | 139 |         if ((*aiter)->GetTrueFather() == NULL)
 | 
|---|
| [e138de] | 140 |           aList[Counter] = Count + (aCounter++);
 | 
|---|
 | 141 |         else
 | 
|---|
| [735b1c] | 142 |           aList[Counter] = (*aiter)->GetTrueFather()->getNr();
 | 
|---|
| [9879f6] | 143 |         if ((*biter)->GetTrueFather() == NULL)
 | 
|---|
| [e138de] | 144 |           bList[Counter] = Count + (bCounter++);
 | 
|---|
 | 145 |         else
 | 
|---|
| [735b1c] | 146 |           bList[Counter] = (*biter)->GetTrueFather()->getNr();
 | 
|---|
| [e138de] | 147 |         Counter++;
 | 
|---|
 | 148 |       }
 | 
|---|
 | 149 |       // check if AtomCount was for real
 | 
|---|
 | 150 |       flag = 0;
 | 
|---|
| [ea7176] | 151 |       if ((aiter == mol1->end()) && (biter != mol2->end())) {
 | 
|---|
| [e138de] | 152 |         flag = -1;
 | 
|---|
 | 153 |       } else {
 | 
|---|
| [ea7176] | 154 |         if ((aiter != mol1->end()) && (biter == mol2->end()))
 | 
|---|
| [e138de] | 155 |           flag = 1;
 | 
|---|
 | 156 |       }
 | 
|---|
 | 157 |       if (flag == 0) {
 | 
|---|
 | 158 |         // sort the lists
 | 
|---|
 | 159 |         gsl_heapsort(aList, Count, sizeof(int), CompareDoubles);
 | 
|---|
 | 160 |         gsl_heapsort(bList, Count, sizeof(int), CompareDoubles);
 | 
|---|
 | 161 |         // compare the lists
 | 
|---|
 | 162 | 
 | 
|---|
 | 163 |         flag = 0;
 | 
|---|
 | 164 |         for (int i = 0; i < Count; i++) {
 | 
|---|
 | 165 |           if (aList[i] < bList[i]) {
 | 
|---|
 | 166 |             flag = -1;
 | 
|---|
 | 167 |           } else {
 | 
|---|
 | 168 |             if (aList[i] > bList[i])
 | 
|---|
 | 169 |               flag = 1;
 | 
|---|
 | 170 |           }
 | 
|---|
 | 171 |           if (flag != 0)
 | 
|---|
 | 172 |             break;
 | 
|---|
 | 173 |         }
 | 
|---|
 | 174 |       }
 | 
|---|
 | 175 |       delete[] (aList);
 | 
|---|
 | 176 |       delete[] (bList);
 | 
|---|
 | 177 |       return flag;
 | 
|---|
 | 178 |     }
 | 
|---|
 | 179 |   }
 | 
|---|
 | 180 |   return -1;
 | 
|---|
 | 181 | };
 | 
|---|
 | 182 | 
 | 
|---|
 | 183 | /** Output of a list of all molecules.
 | 
|---|
 | 184 |  * \param *out output stream
 | 
|---|
 | 185 |  */
 | 
|---|
| [24a5e0] | 186 | void MoleculeListClass::Enumerate(ostream *out)
 | 
|---|
| [e138de] | 187 | {
 | 
|---|
| [ead4e6] | 188 |   periodentafel *periode = World::getInstance().getPeriode();
 | 
|---|
 | 189 |   std::map<atomicNumber_t,unsigned int> counts;
 | 
|---|
| [e138de] | 190 |   double size=0;
 | 
|---|
 | 191 |   Vector Origin;
 | 
|---|
 | 192 | 
 | 
|---|
 | 193 |   // header
 | 
|---|
| [835a0f] | 194 |   (*out) << "Index\tName\t\tAtoms\tFormula\tCenter\tSize" << endl;
 | 
|---|
 | 195 |   (*out) << "-----------------------------------------------" << endl;
 | 
|---|
| [e138de] | 196 |   if (ListOfMolecules.size() == 0)
 | 
|---|
| [835a0f] | 197 |     (*out) << "\tNone" << endl;
 | 
|---|
| [e138de] | 198 |   else {
 | 
|---|
 | 199 |     Origin.Zero();
 | 
|---|
 | 200 |     for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) {
 | 
|---|
 | 201 |       // count atoms per element and determine size of bounding sphere
 | 
|---|
 | 202 |       size=0.;
 | 
|---|
| [9879f6] | 203 |       for (molecule::const_iterator iter = (*ListRunner)->begin(); iter != (*ListRunner)->end(); ++iter) {
 | 
|---|
| [d74077] | 204 |         counts[(*iter)->getType()->getNumber()]++;
 | 
|---|
 | 205 |         if ((*iter)->DistanceSquared(Origin) > size)
 | 
|---|
 | 206 |           size = (*iter)->DistanceSquared(Origin);
 | 
|---|
| [e138de] | 207 |       }
 | 
|---|
 | 208 |       // output Index, Name, number of atoms, chemical formula
 | 
|---|
| [ea7176] | 209 |       (*out) << ((*ListRunner)->ActiveFlag ? "*" : " ") << (*ListRunner)->IndexNr << "\t" << (*ListRunner)->name << "\t\t" << (*ListRunner)->getAtomCount() << "\t";
 | 
|---|
| [ead4e6] | 210 | 
 | 
|---|
 | 211 |       std::map<atomicNumber_t,unsigned int>::reverse_iterator iter;
 | 
|---|
 | 212 |       for(iter=counts.rbegin(); iter!=counts.rend();++iter){
 | 
|---|
 | 213 |         atomicNumber_t Z =(*iter).first;
 | 
|---|
 | 214 |         (*out) << periode->FindElement(Z)->getSymbol() << (*iter).second;
 | 
|---|
| [e138de] | 215 |       }
 | 
|---|
 | 216 |       // Center and size
 | 
|---|
| [1883f9] | 217 |       Vector *Center = (*ListRunner)->DetermineCenterOfAll();
 | 
|---|
 | 218 |       (*out) << "\t" << *Center << "\t" << sqrt(size) << endl;
 | 
|---|
 | 219 |       delete(Center);
 | 
|---|
| [e138de] | 220 |     }
 | 
|---|
 | 221 |   }
 | 
|---|
 | 222 | };
 | 
|---|
 | 223 | 
 | 
|---|
 | 224 | /** Returns the molecule with the given index \a index.
 | 
|---|
 | 225 |  * \param index index of the desired molecule
 | 
|---|
| [1907a7] | 226 |  * \return pointer to molecule structure, NULL if not found
 | 
|---|
| [e138de] | 227 |  */
 | 
|---|
 | 228 | molecule * MoleculeListClass::ReturnIndex(int index)
 | 
|---|
 | 229 | {
 | 
|---|
 | 230 |   for(MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++)
 | 
|---|
 | 231 |     if ((*ListRunner)->IndexNr == index)
 | 
|---|
 | 232 |       return (*ListRunner);
 | 
|---|
 | 233 |   return NULL;
 | 
|---|
 | 234 | };
 | 
|---|
 | 235 | 
 | 
|---|
 | 236 | 
 | 
|---|
 | 237 | /** Simple output of the pointers in ListOfMolecules.
 | 
|---|
 | 238 |  * \param *out output stream
 | 
|---|
 | 239 |  */
 | 
|---|
 | 240 | void MoleculeListClass::Output(ofstream *out)
 | 
|---|
 | 241 | {
 | 
|---|
| [a67d19] | 242 |   DoLog(1) && (Log() << Verbose(1) << "MoleculeList: ");
 | 
|---|
| [e138de] | 243 |   for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++)
 | 
|---|
| [a67d19] | 244 |     DoLog(0) && (Log() << Verbose(0) << *ListRunner << "\t");
 | 
|---|
 | 245 |   DoLog(0) && (Log() << Verbose(0) << endl);
 | 
|---|
| [e138de] | 246 | };
 | 
|---|
 | 247 | 
 | 
|---|
 | 248 | /** Calculates necessary hydrogen correction due to unwanted interaction between saturated ones.
 | 
|---|
 | 249 |  * If for a pair of two hydrogen atoms a and b, at least is a saturated one, and a and b are not
 | 
|---|
 | 250 |  * bonded to the same atom, then we add for this pair a correction term constructed from a Morse
 | 
|---|
 | 251 |  * potential function fit to QM calculations with respecting to the interatomic hydrogen distance.
 | 
|---|
| [35b698] | 252 |  * \param &path path to file
 | 
|---|
| [e138de] | 253 |  */
 | 
|---|
| [35b698] | 254 | bool MoleculeListClass::AddHydrogenCorrection(std::string &path)
 | 
|---|
| [e138de] | 255 | {
 | 
|---|
 | 256 |   bond *Binder = NULL;
 | 
|---|
 | 257 |   double ***FitConstant = NULL, **correction = NULL;
 | 
|---|
 | 258 |   int a, b;
 | 
|---|
 | 259 |   ofstream output;
 | 
|---|
 | 260 |   ifstream input;
 | 
|---|
 | 261 |   string line;
 | 
|---|
 | 262 |   stringstream zeile;
 | 
|---|
 | 263 |   double distance;
 | 
|---|
 | 264 |   char ParsedLine[1023];
 | 
|---|
 | 265 |   double tmp;
 | 
|---|
 | 266 |   char *FragmentNumber = NULL;
 | 
|---|
 | 267 | 
 | 
|---|
| [a67d19] | 268 |   DoLog(1) && (Log() << Verbose(1) << "Saving hydrogen saturation correction ... ");
 | 
|---|
| [e138de] | 269 |   // 0. parse in fit constant files that should have the same dimension as the final energy files
 | 
|---|
 | 270 |   // 0a. find dimension of matrices with constants
 | 
|---|
 | 271 |   line = path;
 | 
|---|
 | 272 |   line += "1";
 | 
|---|
 | 273 |   line += FITCONSTANTSUFFIX;
 | 
|---|
 | 274 |   input.open(line.c_str());
 | 
|---|
| [35b698] | 275 |   if (input.fail()) {
 | 
|---|
| [a67d19] | 276 |     DoLog(1) && (Log() << Verbose(1) << endl << "Unable to open " << line << ", is the directory correct?" << endl);
 | 
|---|
| [e138de] | 277 |     return false;
 | 
|---|
 | 278 |   }
 | 
|---|
 | 279 |   a = 0;
 | 
|---|
 | 280 |   b = -1; // we overcount by one
 | 
|---|
 | 281 |   while (!input.eof()) {
 | 
|---|
 | 282 |     input.getline(ParsedLine, 1023);
 | 
|---|
 | 283 |     zeile.str(ParsedLine);
 | 
|---|
 | 284 |     int i = 0;
 | 
|---|
 | 285 |     while (!zeile.eof()) {
 | 
|---|
 | 286 |       zeile >> distance;
 | 
|---|
 | 287 |       i++;
 | 
|---|
 | 288 |     }
 | 
|---|
 | 289 |     if (i > a)
 | 
|---|
 | 290 |       a = i;
 | 
|---|
 | 291 |     b++;
 | 
|---|
 | 292 |   }
 | 
|---|
| [a67d19] | 293 |   DoLog(0) && (Log() << Verbose(0) << "I recognized " << a << " columns and " << b << " rows, ");
 | 
|---|
| [e138de] | 294 |   input.close();
 | 
|---|
 | 295 | 
 | 
|---|
 | 296 |   // 0b. allocate memory for constants
 | 
|---|
| [920c70] | 297 |   FitConstant = new double**[3];
 | 
|---|
| [e138de] | 298 |   for (int k = 0; k < 3; k++) {
 | 
|---|
| [920c70] | 299 |     FitConstant[k] = new double*[a];
 | 
|---|
| [e138de] | 300 |     for (int i = a; i--;) {
 | 
|---|
| [920c70] | 301 |       FitConstant[k][i] = new double[b];
 | 
|---|
 | 302 |       for (int j = b; j--;) {
 | 
|---|
 | 303 |         FitConstant[k][i][j] = 0.;
 | 
|---|
 | 304 |       }
 | 
|---|
| [e138de] | 305 |     }
 | 
|---|
 | 306 |   }
 | 
|---|
 | 307 |   // 0c. parse in constants
 | 
|---|
 | 308 |   for (int i = 0; i < 3; i++) {
 | 
|---|
 | 309 |     line = path;
 | 
|---|
 | 310 |     line.append("/");
 | 
|---|
 | 311 |     line += FRAGMENTPREFIX;
 | 
|---|
 | 312 |     sprintf(ParsedLine, "%d", i + 1);
 | 
|---|
 | 313 |     line += ParsedLine;
 | 
|---|
 | 314 |     line += FITCONSTANTSUFFIX;
 | 
|---|
 | 315 |     input.open(line.c_str());
 | 
|---|
 | 316 |     if (input == NULL) {
 | 
|---|
| [58ed4a] | 317 |       DoeLog(0) && (eLog()<< Verbose(0) << endl << "Unable to open " << line << ", is the directory correct?" << endl);
 | 
|---|
| [e359a8] | 318 |       performCriticalExit();
 | 
|---|
| [e138de] | 319 |       return false;
 | 
|---|
 | 320 |     }
 | 
|---|
 | 321 |     int k = 0, l;
 | 
|---|
 | 322 |     while ((!input.eof()) && (k < b)) {
 | 
|---|
 | 323 |       input.getline(ParsedLine, 1023);
 | 
|---|
 | 324 |       //Log() << Verbose(0) << "Current Line: " << ParsedLine << endl;
 | 
|---|
 | 325 |       zeile.str(ParsedLine);
 | 
|---|
 | 326 |       zeile.clear();
 | 
|---|
 | 327 |       l = 0;
 | 
|---|
 | 328 |       while ((!zeile.eof()) && (l < a)) {
 | 
|---|
 | 329 |         zeile >> FitConstant[i][l][k];
 | 
|---|
 | 330 |         //Log() << Verbose(0) << FitConstant[i][l][k] << "\t";
 | 
|---|
 | 331 |         l++;
 | 
|---|
 | 332 |       }
 | 
|---|
 | 333 |       //Log() << Verbose(0) << endl;
 | 
|---|
 | 334 |       k++;
 | 
|---|
 | 335 |     }
 | 
|---|
 | 336 |     input.close();
 | 
|---|
 | 337 |   }
 | 
|---|
 | 338 |   for (int k = 0; k < 3; k++) {
 | 
|---|
| [a67d19] | 339 |     DoLog(0) && (Log() << Verbose(0) << "Constants " << k << ":" << endl);
 | 
|---|
| [e138de] | 340 |     for (int j = 0; j < b; j++) {
 | 
|---|
 | 341 |       for (int i = 0; i < a; i++) {
 | 
|---|
| [a67d19] | 342 |         DoLog(0) && (Log() << Verbose(0) << FitConstant[k][i][j] << "\t");
 | 
|---|
| [e138de] | 343 |       }
 | 
|---|
| [a67d19] | 344 |       DoLog(0) && (Log() << Verbose(0) << endl);
 | 
|---|
| [e138de] | 345 |     }
 | 
|---|
| [a67d19] | 346 |     DoLog(0) && (Log() << Verbose(0) << endl);
 | 
|---|
| [e138de] | 347 |   }
 | 
|---|
 | 348 | 
 | 
|---|
 | 349 |   // 0d. allocate final correction matrix
 | 
|---|
| [920c70] | 350 |   correction = new double*[a];
 | 
|---|
| [e138de] | 351 |   for (int i = a; i--;)
 | 
|---|
| [920c70] | 352 |     correction[i] = new double[b];
 | 
|---|
| [e138de] | 353 | 
 | 
|---|
 | 354 |   // 1a. go through every molecule in the list
 | 
|---|
 | 355 |   for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) {
 | 
|---|
 | 356 |     // 1b. zero final correction matrix
 | 
|---|
 | 357 |     for (int k = a; k--;)
 | 
|---|
 | 358 |       for (int j = b; j--;)
 | 
|---|
 | 359 |         correction[k][j] = 0.;
 | 
|---|
 | 360 |     // 2. take every hydrogen that is a saturated one
 | 
|---|
| [9879f6] | 361 |     for (molecule::const_iterator iter = (*ListRunner)->begin(); iter != (*ListRunner)->end(); ++iter) {
 | 
|---|
| [9d83b6] | 362 |       //Log() << Verbose(1) << "(*iter): " << *(*iter) << " with first bond " << *((*iter)->getListOfBonds().begin()) << "." << endl;
 | 
|---|
| [83f176] | 363 |       if (((*iter)->getType()->getAtomicNumber() == 1) && (((*iter)->father == NULL)
 | 
|---|
 | 364 |           || ((*iter)->father->getType()->getAtomicNumber() != 1))) { // if it's a hydrogen
 | 
|---|
| [9879f6] | 365 |         for (molecule::const_iterator runner = (*ListRunner)->begin(); runner != (*ListRunner)->end(); ++runner) {
 | 
|---|
| [9d83b6] | 366 |           //Log() << Verbose(2) << "Runner: " << *(*runner) << " with first bond " << *((*iter)->getListOfBonds().begin()) << "." << endl;
 | 
|---|
| [e138de] | 367 |           // 3. take every other hydrogen that is the not the first and not bound to same bonding partner
 | 
|---|
| [9d83b6] | 368 |           Binder = *((*runner)->getListOfBonds().begin());
 | 
|---|
| [735b1c] | 369 |           if (((*runner)->getType()->getAtomicNumber() == 1) && ((*runner)->getNr() > (*iter)->getNr()) && (Binder->GetOtherAtom((*runner)) != Binder->GetOtherAtom((*iter)))) { // (hydrogens have only one bonding partner!)
 | 
|---|
| [e138de] | 370 |             // 4. evaluate the morse potential for each matrix component and add up
 | 
|---|
| [d74077] | 371 |             distance = (*runner)->distance(*(*iter));
 | 
|---|
| [9879f6] | 372 |             //Log() << Verbose(0) << "Fragment " << (*ListRunner)->name << ": " << *(*runner) << "<= " << distance << "=>" << *(*iter) << ":" << endl;
 | 
|---|
| [e138de] | 373 |             for (int k = 0; k < a; k++) {
 | 
|---|
 | 374 |               for (int j = 0; j < b; j++) {
 | 
|---|
 | 375 |                 switch (k) {
 | 
|---|
 | 376 |                   case 1:
 | 
|---|
 | 377 |                   case 7:
 | 
|---|
 | 378 |                   case 11:
 | 
|---|
 | 379 |                     tmp = pow(FitConstant[0][k][j] * (1. - exp(-FitConstant[1][k][j] * (distance - FitConstant[2][k][j]))), 2);
 | 
|---|
 | 380 |                     break;
 | 
|---|
 | 381 |                   default:
 | 
|---|
 | 382 |                     tmp = FitConstant[0][k][j] * pow(distance, FitConstant[1][k][j]) + FitConstant[2][k][j];
 | 
|---|
 | 383 |                 };
 | 
|---|
 | 384 |                 correction[k][j] -= tmp; // ground state is actually lower (disturbed by additional interaction)
 | 
|---|
 | 385 |                 //Log() << Verbose(0) << tmp << "\t";
 | 
|---|
 | 386 |               }
 | 
|---|
 | 387 |               //Log() << Verbose(0) << endl;
 | 
|---|
 | 388 |             }
 | 
|---|
 | 389 |             //Log() << Verbose(0) << endl;
 | 
|---|
 | 390 |           }
 | 
|---|
 | 391 |         }
 | 
|---|
 | 392 |       }
 | 
|---|
 | 393 |     }
 | 
|---|
 | 394 |     // 5. write final matrix to file
 | 
|---|
 | 395 |     line = path;
 | 
|---|
 | 396 |     line.append("/");
 | 
|---|
 | 397 |     line += FRAGMENTPREFIX;
 | 
|---|
 | 398 |     FragmentNumber = FixedDigitNumber(ListOfMolecules.size(), (*ListRunner)->IndexNr);
 | 
|---|
 | 399 |     line += FragmentNumber;
 | 
|---|
| [920c70] | 400 |     delete[] (FragmentNumber);
 | 
|---|
| [e138de] | 401 |     line += HCORRECTIONSUFFIX;
 | 
|---|
 | 402 |     output.open(line.c_str());
 | 
|---|
 | 403 |     output << "Time\t\tTotal\t\tKinetic\t\tNonLocal\tCorrelation\tExchange\tPseudo\t\tHartree\t\t-Gauss\t\tEwald\t\tIonKin\t\tETotal" << endl;
 | 
|---|
 | 404 |     for (int j = 0; j < b; j++) {
 | 
|---|
 | 405 |       for (int i = 0; i < a; i++)
 | 
|---|
 | 406 |         output << correction[i][j] << "\t";
 | 
|---|
 | 407 |       output << endl;
 | 
|---|
 | 408 |     }
 | 
|---|
 | 409 |     output.close();
 | 
|---|
 | 410 |   }
 | 
|---|
| [920c70] | 411 |   for (int i = a; i--;)
 | 
|---|
 | 412 |     delete[](correction[i]);
 | 
|---|
 | 413 |   delete[](correction);
 | 
|---|
 | 414 | 
 | 
|---|
| [e138de] | 415 |   line = path;
 | 
|---|
 | 416 |   line.append("/");
 | 
|---|
 | 417 |   line += HCORRECTIONSUFFIX;
 | 
|---|
 | 418 |   output.open(line.c_str());
 | 
|---|
 | 419 |   output << "Time\t\tTotal\t\tKinetic\t\tNonLocal\tCorrelation\tExchange\tPseudo\t\tHartree\t\t-Gauss\t\tEwald\t\tIonKin\t\tETotal" << endl;
 | 
|---|
 | 420 |   for (int j = 0; j < b; j++) {
 | 
|---|
 | 421 |     for (int i = 0; i < a; i++)
 | 
|---|
 | 422 |       output << 0 << "\t";
 | 
|---|
 | 423 |     output << endl;
 | 
|---|
 | 424 |   }
 | 
|---|
 | 425 |   output.close();
 | 
|---|
 | 426 |   // 6. free memory of parsed matrices
 | 
|---|
 | 427 |   for (int k = 0; k < 3; k++) {
 | 
|---|
 | 428 |     for (int i = a; i--;) {
 | 
|---|
| [920c70] | 429 |       delete[](FitConstant[k][i]);
 | 
|---|
| [e138de] | 430 |     }
 | 
|---|
| [920c70] | 431 |     delete[](FitConstant[k]);
 | 
|---|
| [e138de] | 432 |   }
 | 
|---|
| [920c70] | 433 |   delete[](FitConstant);
 | 
|---|
| [a67d19] | 434 |   DoLog(0) && (Log() << Verbose(0) << "done." << endl);
 | 
|---|
| [e138de] | 435 |   return true;
 | 
|---|
 | 436 | };
 | 
|---|
 | 437 | 
 | 
|---|
 | 438 | /** Store force indices, i.e. the connection between the nuclear index in the total molecule config and the respective atom in fragment config.
 | 
|---|
| [35b698] | 439 |  * \param &path path to file
 | 
|---|
| [e138de] | 440 |  * \param *SortIndex Index to map from the BFS labeling to the sequence how of Ion_Type in the config
 | 
|---|
 | 441 |  * \return true - file written successfully, false - writing failed
 | 
|---|
 | 442 |  */
 | 
|---|
| [35b698] | 443 | bool MoleculeListClass::StoreForcesFile(std::string &path, int *SortIndex)
 | 
|---|
| [e138de] | 444 | {
 | 
|---|
 | 445 |   bool status = true;
 | 
|---|
| [35b698] | 446 |   string filename(path);
 | 
|---|
 | 447 |   filename += FORCESFILE;
 | 
|---|
 | 448 |   ofstream ForcesFile(filename.c_str());
 | 
|---|
| [ead4e6] | 449 |   periodentafel *periode=World::getInstance().getPeriode();
 | 
|---|
| [e138de] | 450 | 
 | 
|---|
 | 451 |   // open file for the force factors
 | 
|---|
| [a67d19] | 452 |   DoLog(1) && (Log() << Verbose(1) << "Saving  force factors ... ");
 | 
|---|
| [35b698] | 453 |   if (!ForcesFile.fail()) {
 | 
|---|
| [e138de] | 454 |     //Log() << Verbose(1) << "Final AtomicForcesList: ";
 | 
|---|
 | 455 |     //output << prefix << "Forces" << endl;
 | 
|---|
 | 456 |     for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) {
 | 
|---|
| [ead4e6] | 457 |       periodentafel::const_iterator elemIter;
 | 
|---|
 | 458 |       for(elemIter=periode->begin();elemIter!=periode->end();++elemIter){
 | 
|---|
| [389cc8] | 459 |         if ((*ListRunner)->hasElement((*elemIter).first)) { // if this element got atoms
 | 
|---|
| [a7b761b] | 460 |           for(molecule::iterator atomIter = (*ListRunner)->begin(); atomIter !=(*ListRunner)->end();++atomIter){
 | 
|---|
| [d74077] | 461 |             if ((*atomIter)->getType()->getNumber() == (*elemIter).first) {
 | 
|---|
| [a7b761b] | 462 |               if (((*atomIter)->GetTrueFather() != NULL) && ((*atomIter)->GetTrueFather() != (*atomIter))) {// if there is a rea
 | 
|---|
| [e138de] | 463 |                 //Log() << Verbose(0) << "Walker is " << *Walker << " with true father " << *( Walker->GetTrueFather()) << ", it
 | 
|---|
| [735b1c] | 464 |                 ForcesFile << SortIndex[(*atomIter)->GetTrueFather()->getNr()] << "\t";
 | 
|---|
| [e138de] | 465 |               } else
 | 
|---|
 | 466 |                 // otherwise a -1 to indicate an added saturation hydrogen
 | 
|---|
 | 467 |                 ForcesFile << "-1\t";
 | 
|---|
 | 468 |             }
 | 
|---|
 | 469 |           }
 | 
|---|
 | 470 |         }
 | 
|---|
 | 471 |       }
 | 
|---|
 | 472 |       ForcesFile << endl;
 | 
|---|
 | 473 |     }
 | 
|---|
 | 474 |     ForcesFile.close();
 | 
|---|
| [a67d19] | 475 |     DoLog(1) && (Log() << Verbose(1) << "done." << endl);
 | 
|---|
| [e138de] | 476 |   } else {
 | 
|---|
 | 477 |     status = false;
 | 
|---|
| [35b698] | 478 |     DoLog(1) && (Log() << Verbose(1) << "failed to open file " << filename << "." << endl);
 | 
|---|
| [e138de] | 479 |   }
 | 
|---|
 | 480 |   ForcesFile.close();
 | 
|---|
 | 481 | 
 | 
|---|
 | 482 |   return status;
 | 
|---|
 | 483 | };
 | 
|---|
 | 484 | 
 | 
|---|
 | 485 | /** Writes a config file for each molecule in the given \a **FragmentList.
 | 
|---|
 | 486 |  * \param *out output stream for debugging
 | 
|---|
| [35b698] | 487 |  * \param &prefix path and prefix to the fragment config files
 | 
|---|
| [e138de] | 488 |  * \param *SortIndex Index to map from the BFS labeling to the sequence how of Ion_Type in the config
 | 
|---|
 | 489 |  * \return true - success (each file was written), false - something went wrong.
 | 
|---|
 | 490 |  */
 | 
|---|
| [35b698] | 491 | bool MoleculeListClass::OutputConfigForListOfFragments(std::string &prefix, int *SortIndex)
 | 
|---|
| [e138de] | 492 | {
 | 
|---|
 | 493 |   ofstream outputFragment;
 | 
|---|
| [35b698] | 494 |   std::string FragmentName;
 | 
|---|
| [e138de] | 495 |   char PathBackup[MAXSTRINGSIZE];
 | 
|---|
 | 496 |   bool result = true;
 | 
|---|
 | 497 |   bool intermediateResult = true;
 | 
|---|
 | 498 |   Vector BoxDimension;
 | 
|---|
 | 499 |   char *FragmentNumber = NULL;
 | 
|---|
 | 500 |   char *path = NULL;
 | 
|---|
 | 501 |   int FragmentCounter = 0;
 | 
|---|
 | 502 |   ofstream output;
 | 
|---|
| [cca9ef] | 503 |   RealSpaceMatrix cell_size = World::getInstance().getDomain().getM();
 | 
|---|
 | 504 |   RealSpaceMatrix cell_size_backup = cell_size;
 | 
|---|
| [3c58f8] | 505 |   int count=0;
 | 
|---|
| [e138de] | 506 | 
 | 
|---|
 | 507 |   // store the fragments as config and as xyz
 | 
|---|
 | 508 |   for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) {
 | 
|---|
 | 509 |     // save default path as it is changed for each fragment
 | 
|---|
| [35b698] | 510 |     path = World::getInstance().getConfig()->GetDefaultPath();
 | 
|---|
| [e138de] | 511 |     if (path != NULL)
 | 
|---|
 | 512 |       strcpy(PathBackup, path);
 | 
|---|
| [e359a8] | 513 |     else {
 | 
|---|
| [efe516] | 514 |       ELOG(0, "OutputConfigForListOfFragments: NULL default path obtained from config!");
 | 
|---|
| [e359a8] | 515 |       performCriticalExit();
 | 
|---|
 | 516 |     }
 | 
|---|
| [e138de] | 517 | 
 | 
|---|
 | 518 |     // correct periodic
 | 
|---|
| [3c58f8] | 519 |     if ((*ListRunner)->ScanForPeriodicCorrection()) {
 | 
|---|
 | 520 |       count++;
 | 
|---|
 | 521 |     }
 | 
|---|
| [e138de] | 522 | 
 | 
|---|
| [efe516] | 523 |     {
 | 
|---|
 | 524 |       // list atoms in fragment for debugging
 | 
|---|
 | 525 |       std::stringstream output;
 | 
|---|
 | 526 |       output << "Contained atoms: ";
 | 
|---|
 | 527 |       for (molecule::const_iterator iter = (*ListRunner)->begin(); iter != (*ListRunner)->end(); ++iter) {
 | 
|---|
 | 528 |         output << (*iter)->getName() << " ";
 | 
|---|
 | 529 |       }
 | 
|---|
 | 530 |       LOG(2, output.str());
 | 
|---|
 | 531 |     }
 | 
|---|
| [e138de] | 532 | 
 | 
|---|
| [efe516] | 533 |     {
 | 
|---|
 | 534 |       // output xyz file
 | 
|---|
 | 535 |       FragmentNumber = FixedDigitNumber(ListOfMolecules.size(), FragmentCounter++);
 | 
|---|
 | 536 |       FragmentName = prefix + FragmentNumber + ".conf.xyz";
 | 
|---|
 | 537 |       outputFragment.open(FragmentName.c_str(), ios::out);
 | 
|---|
 | 538 |       std::stringstream output;
 | 
|---|
 | 539 |       output << "Saving bond fragment No. " << FragmentNumber << "/" << FragmentCounter - 1 << " as XYZ ... ";
 | 
|---|
 | 540 |       if ((intermediateResult = (*ListRunner)->OutputXYZ(&outputFragment)))
 | 
|---|
 | 541 |         output << " done.";
 | 
|---|
 | 542 |       else
 | 
|---|
 | 543 |         output << " failed.";
 | 
|---|
 | 544 |       LOG(3, output.str());
 | 
|---|
 | 545 |       result = result && intermediateResult;
 | 
|---|
 | 546 |       outputFragment.close();
 | 
|---|
 | 547 |       outputFragment.clear();
 | 
|---|
| [e138de] | 548 |     }
 | 
|---|
 | 549 | 
 | 
|---|
 | 550 |     // center on edge
 | 
|---|
 | 551 |     (*ListRunner)->CenterEdge(&BoxDimension);
 | 
|---|
| [4c2643] | 552 |     for (int k = 0; k < NDIM; k++)  // if one edge is to small, set at least to 1 angstroem
 | 
|---|
 | 553 |       if (BoxDimension[k] < 1.)
 | 
|---|
 | 554 |         BoxDimension[k] += 1.;
 | 
|---|
| [e138de] | 555 |     (*ListRunner)->SetBoxDimension(&BoxDimension); // update Box of atoms by boundary
 | 
|---|
 | 556 |     for (int k = 0; k < NDIM; k++) {
 | 
|---|
| [35b698] | 557 |       BoxDimension[k] = 2.5 * (World::getInstance().getConfig()->GetIsAngstroem() ? 1. : 1. / AtomicLengthToAngstroem);
 | 
|---|
| [84c494] | 558 |       cell_size.at(k,k) = BoxDimension[k] * 2.;
 | 
|---|
| [e138de] | 559 |     }
 | 
|---|
| [84c494] | 560 |     World::getInstance().setDomain(cell_size);
 | 
|---|
| [e138de] | 561 |     (*ListRunner)->Translate(&BoxDimension);
 | 
|---|
 | 562 | 
 | 
|---|
 | 563 |     // change path in config
 | 
|---|
| [35b698] | 564 |     FragmentName = PathBackup;
 | 
|---|
 | 565 |     FragmentName += "/";
 | 
|---|
 | 566 |     FragmentName += FRAGMENTPREFIX;
 | 
|---|
 | 567 |     FragmentName += FragmentNumber;
 | 
|---|
 | 568 |     FragmentName += "/";
 | 
|---|
 | 569 |     World::getInstance().getConfig()->SetDefaultPath(FragmentName.c_str());
 | 
|---|
| [e138de] | 570 | 
 | 
|---|
| [efe516] | 571 |     {
 | 
|---|
 | 572 |       // and save as config
 | 
|---|
 | 573 |       FragmentName = prefix + FragmentNumber + ".conf";
 | 
|---|
 | 574 |       std::stringstream output;
 | 
|---|
 | 575 |       output << "Saving bond fragment No. " << FragmentNumber << "/" << FragmentCounter - 1 << " as config ... ";
 | 
|---|
 | 576 |       if ((intermediateResult = World::getInstance().getConfig()->Save(FragmentName.c_str(), (*ListRunner)->elemente, (*ListRunner))))
 | 
|---|
 | 577 |         output << " done.";
 | 
|---|
 | 578 |       else
 | 
|---|
 | 579 |         output << " failed.";
 | 
|---|
 | 580 |       LOG(3, output.str());
 | 
|---|
 | 581 |       result = result && intermediateResult;
 | 
|---|
 | 582 |     }
 | 
|---|
| [e138de] | 583 | 
 | 
|---|
 | 584 |     // restore old config
 | 
|---|
| [35b698] | 585 |     World::getInstance().getConfig()->SetDefaultPath(PathBackup);
 | 
|---|
| [e138de] | 586 | 
 | 
|---|
| [efe516] | 587 |     {
 | 
|---|
 | 588 |       // and save as mpqc input file
 | 
|---|
 | 589 |       stringstream output;
 | 
|---|
 | 590 |       FragmentName = prefix + FragmentNumber + ".conf";
 | 
|---|
 | 591 |       output << "Saving bond fragment No. " << FragmentNumber << "/" << FragmentCounter - 1 << " as mpqc input ... ";
 | 
|---|
 | 592 |       if ((intermediateResult = World::getInstance().getConfig()->SaveMPQC(FragmentName.c_str(), (*ListRunner))))
 | 
|---|
 | 593 |         output << " done.";
 | 
|---|
 | 594 |       else
 | 
|---|
 | 595 |         output << " failed.";
 | 
|---|
 | 596 |       LOG(3, output.str());
 | 
|---|
 | 597 |     }
 | 
|---|
| [e138de] | 598 | 
 | 
|---|
 | 599 |     result = result && intermediateResult;
 | 
|---|
 | 600 |     //outputFragment.close();
 | 
|---|
 | 601 |     //outputFragment.clear();
 | 
|---|
| [920c70] | 602 |     delete[](FragmentNumber);
 | 
|---|
| [e138de] | 603 |   }
 | 
|---|
| [efe516] | 604 |   LOG(0, "STATUS: done.");
 | 
|---|
| [e138de] | 605 | 
 | 
|---|
 | 606 |   // printing final number
 | 
|---|
| [efe516] | 607 |   LOG(2, "INFO: Final number of fragments: " << FragmentCounter << ".");
 | 
|---|
| [e138de] | 608 | 
 | 
|---|
| [3c58f8] | 609 |   // printing final number
 | 
|---|
| [efe516] | 610 |   LOG(2, "INFO: For " << count << " fragments periodic correction would have been necessary.");
 | 
|---|
| [3c58f8] | 611 | 
 | 
|---|
| [b34306] | 612 |   // restore cell_size
 | 
|---|
| [84c494] | 613 |   World::getInstance().setDomain(cell_size_backup);
 | 
|---|
| [e138de] | 614 | 
 | 
|---|
 | 615 |   return result;
 | 
|---|
 | 616 | };
 | 
|---|
 | 617 | 
 | 
|---|
 | 618 | /** Counts the number of molecules with the molecule::ActiveFlag set.
 | 
|---|
| [1907a7] | 619 |  * \return number of molecules with ActiveFlag set to true.
 | 
|---|
| [e138de] | 620 |  */
 | 
|---|
 | 621 | int MoleculeListClass::NumberOfActiveMolecules()
 | 
|---|
 | 622 | {
 | 
|---|
 | 623 |   int count = 0;
 | 
|---|
 | 624 |   for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++)
 | 
|---|
 | 625 |     count += ((*ListRunner)->ActiveFlag ? 1 : 0);
 | 
|---|
 | 626 |   return count;
 | 
|---|
 | 627 | };
 | 
|---|
 | 628 | 
 | 
|---|
| [568be7] | 629 | /** Count all atoms in each molecule.
 | 
|---|
 | 630 |  * \return number of atoms in the MoleculeListClass.
 | 
|---|
 | 631 |  * TODO: the inner loop should be done by some (double molecule::CountAtom()) function
 | 
|---|
 | 632 |  */
 | 
|---|
 | 633 | int MoleculeListClass::CountAllAtoms() const
 | 
|---|
 | 634 | {
 | 
|---|
 | 635 |   int AtomNo = 0;
 | 
|---|
 | 636 |   for (MoleculeList::const_iterator MolWalker = ListOfMolecules.begin(); MolWalker != ListOfMolecules.end(); MolWalker++) {
 | 
|---|
| [9879f6] | 637 |     AtomNo += (*MolWalker)->size();
 | 
|---|
| [568be7] | 638 |   }
 | 
|---|
 | 639 |   return AtomNo;
 | 
|---|
 | 640 | }
 | 
|---|
 | 641 | 
 | 
|---|
| [477bb2] | 642 | /***********
 | 
|---|
 | 643 |  * Methods Moved here from the menus
 | 
|---|
 | 644 |  */
 | 
|---|
| [568be7] | 645 | 
 | 
|---|
| [477bb2] | 646 | void MoleculeListClass::createNewMolecule(periodentafel *periode) {
 | 
|---|
| [2ba827] | 647 |   OBSERVE;
 | 
|---|
| [477bb2] | 648 |   molecule *mol = NULL;
 | 
|---|
| [23b547] | 649 |   mol = World::getInstance().createMolecule();
 | 
|---|
| [477bb2] | 650 |   insert(mol);
 | 
|---|
 | 651 | };
 | 
|---|
 | 652 | 
 | 
|---|
 | 653 | void MoleculeListClass::loadFromXYZ(periodentafel *periode){
 | 
|---|
 | 654 |   molecule *mol = NULL;
 | 
|---|
 | 655 |   Vector center;
 | 
|---|
 | 656 |   char filename[MAXSTRINGSIZE];
 | 
|---|
 | 657 |   Log() << Verbose(0) << "Format should be XYZ with: ShorthandOfElement\tX\tY\tZ" << endl;
 | 
|---|
| [23b547] | 658 |   mol = World::getInstance().createMolecule();
 | 
|---|
| [477bb2] | 659 |   do {
 | 
|---|
 | 660 |     Log() << Verbose(0) << "Enter file name: ";
 | 
|---|
 | 661 |     cin >> filename;
 | 
|---|
 | 662 |   } while (!mol->AddXYZFile(filename));
 | 
|---|
 | 663 |   mol->SetNameFromFilename(filename);
 | 
|---|
 | 664 |   // center at set box dimensions
 | 
|---|
 | 665 |   mol->CenterEdge(¢er);
 | 
|---|
| [cca9ef] | 666 |   RealSpaceMatrix domain;
 | 
|---|
| [84c494] | 667 |   for(int i =0;i<NDIM;++i)
 | 
|---|
 | 668 |     domain.at(i,i) = center[i];
 | 
|---|
 | 669 |   World::getInstance().setDomain(domain);
 | 
|---|
| [477bb2] | 670 |   insert(mol);
 | 
|---|
 | 671 | }
 | 
|---|
 | 672 | 
 | 
|---|
 | 673 | void MoleculeListClass::setMoleculeFilename() {
 | 
|---|
 | 674 |   char filename[MAXSTRINGSIZE];
 | 
|---|
 | 675 |   int nr;
 | 
|---|
 | 676 |   molecule *mol = NULL;
 | 
|---|
 | 677 |   do {
 | 
|---|
 | 678 |     Log() << Verbose(0) << "Enter index of molecule: ";
 | 
|---|
 | 679 |     cin >> nr;
 | 
|---|
 | 680 |     mol = ReturnIndex(nr);
 | 
|---|
 | 681 |   } while (mol == NULL);
 | 
|---|
 | 682 |   Log() << Verbose(0) << "Enter name: ";
 | 
|---|
 | 683 |   cin >> filename;
 | 
|---|
 | 684 |   mol->SetNameFromFilename(filename);
 | 
|---|
 | 685 | }
 | 
|---|
 | 686 | 
 | 
|---|
 | 687 | void MoleculeListClass::parseXYZIntoMolecule(){
 | 
|---|
 | 688 |   char filename[MAXSTRINGSIZE];
 | 
|---|
 | 689 |   int nr;
 | 
|---|
 | 690 |   molecule *mol = NULL;
 | 
|---|
 | 691 |   mol = NULL;
 | 
|---|
 | 692 |   do {
 | 
|---|
 | 693 |    Log() << Verbose(0) << "Enter index of molecule: ";
 | 
|---|
 | 694 |    cin >> nr;
 | 
|---|
 | 695 |    mol = ReturnIndex(nr);
 | 
|---|
 | 696 |   } while (mol == NULL);
 | 
|---|
 | 697 |   Log() << Verbose(0) << "Format should be XYZ with: ShorthandOfElement\tX\tY\tZ" << endl;
 | 
|---|
 | 698 |   do {
 | 
|---|
 | 699 |    Log() << Verbose(0) << "Enter file name: ";
 | 
|---|
 | 700 |    cin >> filename;
 | 
|---|
 | 701 |   } while (!mol->AddXYZFile(filename));
 | 
|---|
 | 702 |   mol->SetNameFromFilename(filename);
 | 
|---|
 | 703 | };
 | 
|---|
 | 704 | 
 | 
|---|
 | 705 | void MoleculeListClass::eraseMolecule(){
 | 
|---|
 | 706 |   int nr;
 | 
|---|
 | 707 |   molecule *mol = NULL;
 | 
|---|
 | 708 |   Log() << Verbose(0) << "Enter index of molecule: ";
 | 
|---|
 | 709 |   cin >> nr;
 | 
|---|
 | 710 |   for(MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++)
 | 
|---|
 | 711 |     if (nr == (*ListRunner)->IndexNr) {
 | 
|---|
 | 712 |       mol = *ListRunner;
 | 
|---|
 | 713 |       ListOfMolecules.erase(ListRunner);
 | 
|---|
| [23b547] | 714 |       World::getInstance().destroyMolecule(mol);
 | 
|---|
| [477bb2] | 715 |       break;
 | 
|---|
 | 716 |     }
 | 
|---|
 | 717 | };
 | 
|---|
 | 718 | 
 | 
|---|
| [77675f] | 719 | 
 | 
|---|
| [e138de] | 720 | /******************************************* Class MoleculeLeafClass ************************************************/
 | 
|---|
 | 721 | 
 | 
|---|
 | 722 | /** Constructor for MoleculeLeafClass root leaf.
 | 
|---|
 | 723 |  * \param *Up Leaf on upper level
 | 
|---|
 | 724 |  * \param *PreviousLeaf NULL - We are the first leaf on this level, otherwise points to previous in list
 | 
|---|
 | 725 |  */
 | 
|---|
 | 726 | //MoleculeLeafClass::MoleculeLeafClass(MoleculeLeafClass *Up = NULL, MoleculeLeafClass *Previous = NULL)
 | 
|---|
| [97b825] | 727 | MoleculeLeafClass::MoleculeLeafClass(MoleculeLeafClass *PreviousLeaf = NULL) :
 | 
|---|
 | 728 |   Leaf(NULL),
 | 
|---|
 | 729 |   previous(PreviousLeaf)
 | 
|---|
| [e138de] | 730 | {
 | 
|---|
 | 731 |   //  if (Up != NULL)
 | 
|---|
 | 732 |   //    if (Up->DownLeaf == NULL) // are we the first down leaf for the upper leaf?
 | 
|---|
 | 733 |   //      Up->DownLeaf = this;
 | 
|---|
 | 734 |   //  UpLeaf = Up;
 | 
|---|
 | 735 |   //  DownLeaf = NULL;
 | 
|---|
 | 736 |   if (previous != NULL) {
 | 
|---|
 | 737 |     MoleculeLeafClass *Walker = previous->next;
 | 
|---|
 | 738 |     previous->next = this;
 | 
|---|
 | 739 |     next = Walker;
 | 
|---|
 | 740 |   } else {
 | 
|---|
 | 741 |     next = NULL;
 | 
|---|
 | 742 |   }
 | 
|---|
 | 743 | };
 | 
|---|
 | 744 | 
 | 
|---|
 | 745 | /** Destructor for MoleculeLeafClass.
 | 
|---|
 | 746 |  */
 | 
|---|
 | 747 | MoleculeLeafClass::~MoleculeLeafClass()
 | 
|---|
 | 748 | {
 | 
|---|
 | 749 |   //  if (DownLeaf != NULL) {// drop leaves further down
 | 
|---|
 | 750 |   //    MoleculeLeafClass *Walker = DownLeaf;
 | 
|---|
 | 751 |   //    MoleculeLeafClass *Next;
 | 
|---|
 | 752 |   //    do {
 | 
|---|
 | 753 |   //      Next = Walker->NextLeaf;
 | 
|---|
 | 754 |   //      delete(Walker);
 | 
|---|
 | 755 |   //      Walker = Next;
 | 
|---|
 | 756 |   //    } while (Walker != NULL);
 | 
|---|
 | 757 |   //    // Last Walker sets DownLeaf automatically to NULL
 | 
|---|
 | 758 |   //  }
 | 
|---|
 | 759 |   // remove the leaf itself
 | 
|---|
 | 760 |   if (Leaf != NULL) {
 | 
|---|
| [00b59d5] | 761 |     Leaf->removeAtomsinMolecule();
 | 
|---|
| [23b547] | 762 |     World::getInstance().destroyMolecule(Leaf);
 | 
|---|
| [e138de] | 763 |     Leaf = NULL;
 | 
|---|
 | 764 |   }
 | 
|---|
 | 765 |   // remove this Leaf from level list
 | 
|---|
 | 766 |   if (previous != NULL)
 | 
|---|
 | 767 |     previous->next = next;
 | 
|---|
 | 768 |   //  } else { // we are first in list (connects to UpLeaf->DownLeaf)
 | 
|---|
 | 769 |   //    if ((NextLeaf != NULL) && (NextLeaf->UpLeaf == NULL))
 | 
|---|
 | 770 |   //      NextLeaf->UpLeaf = UpLeaf;  // either null as we are top level or the upleaf of the first node
 | 
|---|
 | 771 |   //    if (UpLeaf != NULL)
 | 
|---|
 | 772 |   //      UpLeaf->DownLeaf = NextLeaf;  // either null as we are only leaf or NextLeaf if we are just the first
 | 
|---|
 | 773 |   //  }
 | 
|---|
 | 774 |   //  UpLeaf = NULL;
 | 
|---|
 | 775 |   if (next != NULL) // are we last in list
 | 
|---|
 | 776 |     next->previous = previous;
 | 
|---|
 | 777 |   next = NULL;
 | 
|---|
 | 778 |   previous = NULL;
 | 
|---|
 | 779 | };
 | 
|---|
 | 780 | 
 | 
|---|
 | 781 | /** Adds \a molecule leaf to the tree.
 | 
|---|
 | 782 |  * \param *ptr ptr to molecule to be added
 | 
|---|
 | 783 |  * \param *Previous previous MoleculeLeafClass referencing level and which on the level
 | 
|---|
 | 784 |  * \return true - success, false - something went wrong
 | 
|---|
 | 785 |  */
 | 
|---|
 | 786 | bool MoleculeLeafClass::AddLeaf(molecule *ptr, MoleculeLeafClass *Previous)
 | 
|---|
 | 787 | {
 | 
|---|
 | 788 |   return false;
 | 
|---|
 | 789 | };
 | 
|---|
 | 790 | 
 | 
|---|
 | 791 | /** Fills the root stack for sites to be used as root in fragmentation depending on order or adaptivity criteria
 | 
|---|
 | 792 |  * Again, as in \sa FillBondStructureFromReference steps recursively through each Leaf in this chain list of molecule's.
 | 
|---|
 | 793 |  * \param *out output stream for debugging
 | 
|---|
 | 794 |  * \param *&RootStack stack to be filled
 | 
|---|
| [5309ba] | 795 |  * \param *AtomMask defines true/false per global Atom::Nr to mask in/out each nuclear site
 | 
|---|
| [e138de] | 796 |  * \param &FragmentCounter counts through the fragments in this MoleculeLeafClass
 | 
|---|
 | 797 |  * \return true - stack is non-empty, fragmentation necessary, false - stack is empty, no more sites to update
 | 
|---|
 | 798 |  */
 | 
|---|
 | 799 | bool MoleculeLeafClass::FillRootStackForSubgraphs(KeyStack *&RootStack, bool *AtomMask, int &FragmentCounter)
 | 
|---|
 | 800 | {
 | 
|---|
| [9879f6] | 801 |   atom *Father = NULL;
 | 
|---|
| [e138de] | 802 | 
 | 
|---|
 | 803 |   if (RootStack != NULL) {
 | 
|---|
 | 804 |     // find first root candidates
 | 
|---|
 | 805 |     if (&(RootStack[FragmentCounter]) != NULL) {
 | 
|---|
 | 806 |       RootStack[FragmentCounter].clear();
 | 
|---|
| [9879f6] | 807 |       for(molecule::const_iterator iter = Leaf->begin(); iter != Leaf->end(); ++iter) {
 | 
|---|
 | 808 |         Father = (*iter)->GetTrueFather();
 | 
|---|
| [735b1c] | 809 |         if (AtomMask[Father->getNr()]) // apply mask
 | 
|---|
| [e138de] | 810 | #ifdef ADDHYDROGEN
 | 
|---|
| [83f176] | 811 |           if ((*iter)->getType()->getAtomicNumber() != 1) // skip hydrogen
 | 
|---|
| [e138de] | 812 | #endif
 | 
|---|
| [735b1c] | 813 |           RootStack[FragmentCounter].push_front((*iter)->getNr());
 | 
|---|
| [e138de] | 814 |       }
 | 
|---|
 | 815 |       if (next != NULL)
 | 
|---|
 | 816 |         next->FillRootStackForSubgraphs(RootStack, AtomMask, ++FragmentCounter);
 | 
|---|
 | 817 |     } else {
 | 
|---|
| [a67d19] | 818 |       DoLog(1) && (Log() << Verbose(1) << "Rootstack[" << FragmentCounter << "] is NULL." << endl);
 | 
|---|
| [e138de] | 819 |       return false;
 | 
|---|
 | 820 |     }
 | 
|---|
 | 821 |     FragmentCounter--;
 | 
|---|
 | 822 |     return true;
 | 
|---|
 | 823 |   } else {
 | 
|---|
| [a67d19] | 824 |     DoLog(1) && (Log() << Verbose(1) << "Rootstack is NULL." << endl);
 | 
|---|
| [e138de] | 825 |     return false;
 | 
|---|
 | 826 |   }
 | 
|---|
 | 827 | };
 | 
|---|
 | 828 | 
 | 
|---|
| [5309ba] | 829 | /** The indices per keyset are compared to the respective father's Atom::Nr in each subgraph and thus put into \a **&FragmentList.
 | 
|---|
| [e138de] | 830 |  * \param *out output stream fro debugging
 | 
|---|
 | 831 |  * \param *reference reference molecule with the bond structure to be copied
 | 
|---|
 | 832 |  * \param *KeySetList list with all keysets
 | 
|---|
 | 833 |  * \param ***ListOfLocalAtoms Lookup table for each subgraph and index of each atom in global molecule, may be NULL on start, then it is filled
 | 
|---|
 | 834 |  * \param **&FragmentList list to be allocated and returned
 | 
|---|
 | 835 |  * \param &FragmentCounter counts the fragments as we move along the list
 | 
|---|
 | 836 |  * \param FreeList true - ***ListOfLocalAtoms is free'd before return, false - it is not
 | 
|---|
 | 837 |  * \retuen true - success, false - failure
 | 
|---|
 | 838 |  */
 | 
|---|
 | 839 | bool MoleculeLeafClass::AssignKeySetsToFragment(molecule *reference, Graph *KeySetList, atom ***&ListOfLocalAtoms, Graph **&FragmentList, int &FragmentCounter, bool FreeList)
 | 
|---|
 | 840 | {
 | 
|---|
 | 841 |   bool status = true;
 | 
|---|
 | 842 |   int KeySetCounter = 0;
 | 
|---|
 | 843 | 
 | 
|---|
| [a67d19] | 844 |   DoLog(1) && (Log() << Verbose(1) << "Begin of AssignKeySetsToFragment." << endl);
 | 
|---|
| [e138de] | 845 |   // fill ListOfLocalAtoms if NULL was given
 | 
|---|
| [c6123b] | 846 |   if (!Leaf->FillListOfLocalAtoms(ListOfLocalAtoms[FragmentCounter], reference->getAtomCount())) {
 | 
|---|
| [a67d19] | 847 |     DoLog(1) && (Log() << Verbose(1) << "Filling of ListOfLocalAtoms failed." << endl);
 | 
|---|
| [e138de] | 848 |     return false;
 | 
|---|
 | 849 |   }
 | 
|---|
 | 850 | 
 | 
|---|
 | 851 |   // allocate fragment list
 | 
|---|
 | 852 |   if (FragmentList == NULL) {
 | 
|---|
 | 853 |     KeySetCounter = Count();
 | 
|---|
| [920c70] | 854 |     FragmentList = new Graph*[KeySetCounter];
 | 
|---|
 | 855 |     for (int i=0;i<KeySetCounter;i++)
 | 
|---|
 | 856 |       FragmentList[i] = NULL;
 | 
|---|
| [e138de] | 857 |     KeySetCounter = 0;
 | 
|---|
 | 858 |   }
 | 
|---|
 | 859 | 
 | 
|---|
 | 860 |   if ((KeySetList != NULL) && (KeySetList->size() != 0)) { // if there are some scanned keysets at all
 | 
|---|
 | 861 |     // assign scanned keysets
 | 
|---|
 | 862 |     if (FragmentList[FragmentCounter] == NULL)
 | 
|---|
 | 863 |       FragmentList[FragmentCounter] = new Graph;
 | 
|---|
 | 864 |     KeySet *TempSet = new KeySet;
 | 
|---|
 | 865 |     for (Graph::iterator runner = KeySetList->begin(); runner != KeySetList->end(); runner++) { // key sets contain global numbers!
 | 
|---|
| [735b1c] | 866 |       if (ListOfLocalAtoms[FragmentCounter][reference->FindAtom(*((*runner).first.begin()))->getNr()] != NULL) {// as we may assume that that bond structure is unchanged, we only test the first key in each set
 | 
|---|
| [e138de] | 867 |         // translate keyset to local numbers
 | 
|---|
 | 868 |         for (KeySet::iterator sprinter = (*runner).first.begin(); sprinter != (*runner).first.end(); sprinter++)
 | 
|---|
| [735b1c] | 869 |           TempSet->insert(ListOfLocalAtoms[FragmentCounter][reference->FindAtom(*sprinter)->getNr()]->getNr());
 | 
|---|
| [e138de] | 870 |         // insert into FragmentList
 | 
|---|
 | 871 |         FragmentList[FragmentCounter]->insert(GraphPair(*TempSet, pair<int, double> (KeySetCounter++, (*runner).second.second)));
 | 
|---|
 | 872 |       }
 | 
|---|
 | 873 |       TempSet->clear();
 | 
|---|
 | 874 |     }
 | 
|---|
 | 875 |     delete (TempSet);
 | 
|---|
 | 876 |     if (KeySetCounter == 0) {// if there are no keysets, delete the list
 | 
|---|
| [a67d19] | 877 |       DoLog(1) && (Log() << Verbose(1) << "KeySetCounter is zero, deleting FragmentList." << endl);
 | 
|---|
| [e138de] | 878 |       delete (FragmentList[FragmentCounter]);
 | 
|---|
 | 879 |     } else
 | 
|---|
| [a67d19] | 880 |       DoLog(1) && (Log() << Verbose(1) << KeySetCounter << " keysets were assigned to subgraph " << FragmentCounter << "." << endl);
 | 
|---|
| [e138de] | 881 |     FragmentCounter++;
 | 
|---|
 | 882 |     if (next != NULL)
 | 
|---|
 | 883 |       next->AssignKeySetsToFragment(reference, KeySetList, ListOfLocalAtoms, FragmentList, FragmentCounter, FreeList);
 | 
|---|
 | 884 |     FragmentCounter--;
 | 
|---|
 | 885 |   } else
 | 
|---|
| [a67d19] | 886 |     DoLog(1) && (Log() << Verbose(1) << "KeySetList is NULL or empty." << endl);
 | 
|---|
| [e138de] | 887 | 
 | 
|---|
 | 888 |   if ((FreeList) && (ListOfLocalAtoms != NULL)) {
 | 
|---|
 | 889 |     // free the index lookup list
 | 
|---|
| [920c70] | 890 |     delete[](ListOfLocalAtoms[FragmentCounter]);
 | 
|---|
| [e138de] | 891 |   }
 | 
|---|
| [a67d19] | 892 |   DoLog(1) && (Log() << Verbose(1) << "End of AssignKeySetsToFragment." << endl);
 | 
|---|
| [e138de] | 893 |   return status;
 | 
|---|
 | 894 | };
 | 
|---|
 | 895 | 
 | 
|---|
 | 896 | /** Translate list into global numbers (i.e. ones that are valid in "this" molecule, not in MolecularWalker->Leaf)
 | 
|---|
 | 897 |  * \param *out output stream for debugging
 | 
|---|
 | 898 |  * \param **FragmentList Graph with local numbers per fragment
 | 
|---|
 | 899 |  * \param &FragmentCounter counts the fragments as we move along the list
 | 
|---|
 | 900 |  * \param &TotalNumberOfKeySets global key set counter
 | 
|---|
 | 901 |  * \param &TotalGraph Graph to be filled with global numbers
 | 
|---|
 | 902 |  */
 | 
|---|
 | 903 | void MoleculeLeafClass::TranslateIndicesToGlobalIDs(Graph **FragmentList, int &FragmentCounter, int &TotalNumberOfKeySets, Graph &TotalGraph)
 | 
|---|
 | 904 | {
 | 
|---|
| [a67d19] | 905 |   DoLog(1) && (Log() << Verbose(1) << "Begin of TranslateIndicesToGlobalIDs." << endl);
 | 
|---|
| [e138de] | 906 |   KeySet *TempSet = new KeySet;
 | 
|---|
 | 907 |   if (FragmentList[FragmentCounter] != NULL) {
 | 
|---|
 | 908 |     for (Graph::iterator runner = FragmentList[FragmentCounter]->begin(); runner != FragmentList[FragmentCounter]->end(); runner++) {
 | 
|---|
 | 909 |       for (KeySet::iterator sprinter = (*runner).first.begin(); sprinter != (*runner).first.end(); sprinter++)
 | 
|---|
| [735b1c] | 910 |         TempSet->insert((Leaf->FindAtom(*sprinter))->GetTrueFather()->getNr());
 | 
|---|
| [e138de] | 911 |       TotalGraph.insert(GraphPair(*TempSet, pair<int, double> (TotalNumberOfKeySets++, (*runner).second.second)));
 | 
|---|
 | 912 |       TempSet->clear();
 | 
|---|
 | 913 |     }
 | 
|---|
 | 914 |     delete (TempSet);
 | 
|---|
 | 915 |   } else {
 | 
|---|
| [a67d19] | 916 |     DoLog(1) && (Log() << Verbose(1) << "FragmentList is NULL." << endl);
 | 
|---|
| [e138de] | 917 |   }
 | 
|---|
 | 918 |   if (next != NULL)
 | 
|---|
 | 919 |     next->TranslateIndicesToGlobalIDs(FragmentList, ++FragmentCounter, TotalNumberOfKeySets, TotalGraph);
 | 
|---|
 | 920 |   FragmentCounter--;
 | 
|---|
| [a67d19] | 921 |   DoLog(1) && (Log() << Verbose(1) << "End of TranslateIndicesToGlobalIDs." << endl);
 | 
|---|
| [e138de] | 922 | };
 | 
|---|
 | 923 | 
 | 
|---|
 | 924 | /** Simply counts the number of items in the list, from given MoleculeLeafClass.
 | 
|---|
 | 925 |  * \return number of items
 | 
|---|
 | 926 |  */
 | 
|---|
 | 927 | int MoleculeLeafClass::Count() const
 | 
|---|
 | 928 | {
 | 
|---|
 | 929 |   if (next != NULL)
 | 
|---|
 | 930 |     return next->Count() + 1;
 | 
|---|
 | 931 |   else
 | 
|---|
 | 932 |     return 1;
 | 
|---|
 | 933 | };
 | 
|---|
 | 934 | 
 | 
|---|