1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2011 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /** \file MoleculeListClass.cpp
|
---|
9 | *
|
---|
10 | * Function implementations for the class MoleculeListClass.
|
---|
11 | *
|
---|
12 | */
|
---|
13 |
|
---|
14 | // include config.h
|
---|
15 | #ifdef HAVE_CONFIG_H
|
---|
16 | #include <config.h>
|
---|
17 | #endif
|
---|
18 |
|
---|
19 | #include "CodePatterns/MemDebug.hpp"
|
---|
20 |
|
---|
21 | #include <iostream>
|
---|
22 |
|
---|
23 | //#include <gsl/gsl_inline.h>
|
---|
24 | #include <gsl/gsl_heapsort.h>
|
---|
25 |
|
---|
26 | #include "MoleculeListClass.hpp"
|
---|
27 |
|
---|
28 | #include "CodePatterns/Log.hpp"
|
---|
29 |
|
---|
30 | #include "atom.hpp"
|
---|
31 | #include "Bond/bond.hpp"
|
---|
32 | #include "Box.hpp"
|
---|
33 | #include "config.hpp"
|
---|
34 | #include "Element/element.hpp"
|
---|
35 | #include "Element/periodentafel.hpp"
|
---|
36 | #include "Fragmentation/Graph.hpp"
|
---|
37 | #include "Fragmentation/KeySet.hpp"
|
---|
38 | #include "Graph/BondGraph.hpp"
|
---|
39 | #include "Helpers/helpers.hpp"
|
---|
40 | #include "molecule.hpp"
|
---|
41 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
42 | #include "Parser/FormatParserStorage.hpp"
|
---|
43 | #include "World.hpp"
|
---|
44 |
|
---|
45 |
|
---|
46 | /** Constructor for MoleculeListClass.
|
---|
47 | */
|
---|
48 | MoleculeListClass::MoleculeListClass(World *_world) :
|
---|
49 | Observable("MoleculeListClass"),
|
---|
50 | MaxIndex(1),
|
---|
51 | world(_world)
|
---|
52 | {};
|
---|
53 |
|
---|
54 | /** Destructor for MoleculeListClass.
|
---|
55 | */
|
---|
56 | MoleculeListClass::~MoleculeListClass()
|
---|
57 | {
|
---|
58 | LOG(4, "Clearing ListOfMolecules.");
|
---|
59 | for(MoleculeList::iterator MolRunner = ListOfMolecules.begin(); MolRunner != ListOfMolecules.end(); ++MolRunner)
|
---|
60 | (*MolRunner)->signOff(this);
|
---|
61 | ListOfMolecules.clear(); // empty list
|
---|
62 | };
|
---|
63 |
|
---|
64 | /** Insert a new molecule into the list and set its number.
|
---|
65 | * \param *mol molecule to add to list.
|
---|
66 | */
|
---|
67 | void MoleculeListClass::insert(molecule *mol)
|
---|
68 | {
|
---|
69 | OBSERVE;
|
---|
70 | mol->IndexNr = MaxIndex++;
|
---|
71 | ListOfMolecules.push_back(mol);
|
---|
72 | mol->signOn(this);
|
---|
73 | };
|
---|
74 |
|
---|
75 | /** Erases a molecule from the list.
|
---|
76 | * \param *mol molecule to add to list.
|
---|
77 | */
|
---|
78 | void MoleculeListClass::erase(molecule *mol)
|
---|
79 | {
|
---|
80 | OBSERVE;
|
---|
81 | mol->signOff(this);
|
---|
82 | ListOfMolecules.remove(mol);
|
---|
83 | };
|
---|
84 |
|
---|
85 | /** Comparison function for two values.
|
---|
86 | * \param *a
|
---|
87 | * \param *b
|
---|
88 | * \return <0, \a *a less than \a *b, ==0 if equal, >0 \a *a greater than \a *b
|
---|
89 | */
|
---|
90 | int CompareDoubles (const void * a, const void * b)
|
---|
91 | {
|
---|
92 | if (*(double *)a > *(double *)b)
|
---|
93 | return -1;
|
---|
94 | else if (*(double *)a < *(double *)b)
|
---|
95 | return 1;
|
---|
96 | else
|
---|
97 | return 0;
|
---|
98 | };
|
---|
99 |
|
---|
100 |
|
---|
101 | /** Compare whether two molecules are equal.
|
---|
102 | * \param *a molecule one
|
---|
103 | * \param *n molecule two
|
---|
104 | * \return lexical value (-1, 0, +1)
|
---|
105 | */
|
---|
106 | int MolCompare(const void *a, const void *b)
|
---|
107 | {
|
---|
108 | int *aList = NULL, *bList = NULL;
|
---|
109 | int Count, Counter, aCounter, bCounter;
|
---|
110 | int flag;
|
---|
111 |
|
---|
112 | // sort each atom list and put the numbers into a list, then go through
|
---|
113 | //LOG(0, "Comparing fragment no. " << *(molecule **)a << " to " << *(molecule **)b << ".");
|
---|
114 | // Yes those types are awkward... but check it for yourself it checks out this way
|
---|
115 | molecule *const *mol1_ptr= static_cast<molecule *const *>(a);
|
---|
116 | molecule *mol1 = *mol1_ptr;
|
---|
117 | molecule *const *mol2_ptr= static_cast<molecule *const *>(b);
|
---|
118 | molecule *mol2 = *mol2_ptr;
|
---|
119 | if (mol1->getAtomCount() < mol2->getAtomCount()) {
|
---|
120 | return -1;
|
---|
121 | } else {
|
---|
122 | if (mol1->getAtomCount() > mol2->getAtomCount())
|
---|
123 | return +1;
|
---|
124 | else {
|
---|
125 | Count = mol1->getAtomCount();
|
---|
126 | aList = new int[Count];
|
---|
127 | bList = new int[Count];
|
---|
128 |
|
---|
129 | // fill the lists
|
---|
130 | Counter = 0;
|
---|
131 | aCounter = 0;
|
---|
132 | bCounter = 0;
|
---|
133 | molecule::const_iterator aiter = mol1->begin();
|
---|
134 | molecule::const_iterator biter = mol2->begin();
|
---|
135 | for (;(aiter != mol1->end()) && (biter != mol2->end());
|
---|
136 | ++aiter, ++biter) {
|
---|
137 | if ((*aiter)->GetTrueFather() == NULL)
|
---|
138 | aList[Counter] = Count + (aCounter++);
|
---|
139 | else
|
---|
140 | aList[Counter] = (*aiter)->GetTrueFather()->getNr();
|
---|
141 | if ((*biter)->GetTrueFather() == NULL)
|
---|
142 | bList[Counter] = Count + (bCounter++);
|
---|
143 | else
|
---|
144 | bList[Counter] = (*biter)->GetTrueFather()->getNr();
|
---|
145 | Counter++;
|
---|
146 | }
|
---|
147 | // check if AtomCount was for real
|
---|
148 | flag = 0;
|
---|
149 | if ((aiter == mol1->end()) && (biter != mol2->end())) {
|
---|
150 | flag = -1;
|
---|
151 | } else {
|
---|
152 | if ((aiter != mol1->end()) && (biter == mol2->end()))
|
---|
153 | flag = 1;
|
---|
154 | }
|
---|
155 | if (flag == 0) {
|
---|
156 | // sort the lists
|
---|
157 | gsl_heapsort(aList, Count, sizeof(int), CompareDoubles);
|
---|
158 | gsl_heapsort(bList, Count, sizeof(int), CompareDoubles);
|
---|
159 | // compare the lists
|
---|
160 |
|
---|
161 | flag = 0;
|
---|
162 | for (int i = 0; i < Count; i++) {
|
---|
163 | if (aList[i] < bList[i]) {
|
---|
164 | flag = -1;
|
---|
165 | } else {
|
---|
166 | if (aList[i] > bList[i])
|
---|
167 | flag = 1;
|
---|
168 | }
|
---|
169 | if (flag != 0)
|
---|
170 | break;
|
---|
171 | }
|
---|
172 | }
|
---|
173 | delete[] (aList);
|
---|
174 | delete[] (bList);
|
---|
175 | return flag;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | return -1;
|
---|
179 | };
|
---|
180 |
|
---|
181 | /** Output of a list of all molecules.
|
---|
182 | * \param *out output stream
|
---|
183 | */
|
---|
184 | void MoleculeListClass::Enumerate(std::ostream *out)
|
---|
185 | {
|
---|
186 | periodentafel *periode = World::getInstance().getPeriode();
|
---|
187 | std::map<atomicNumber_t,unsigned int> counts;
|
---|
188 | double size=0;
|
---|
189 | Vector Origin;
|
---|
190 |
|
---|
191 | // header
|
---|
192 | (*out) << "Index\tName\t\tAtoms\tFormula\tCenter\tSize" << endl;
|
---|
193 | (*out) << "-----------------------------------------------" << endl;
|
---|
194 | if (ListOfMolecules.size() == 0)
|
---|
195 | (*out) << "\tNone" << endl;
|
---|
196 | else {
|
---|
197 | Origin.Zero();
|
---|
198 | for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) {
|
---|
199 | // count atoms per element and determine size of bounding sphere
|
---|
200 | size=0.;
|
---|
201 | for (molecule::const_iterator iter = (*ListRunner)->begin(); iter != (*ListRunner)->end(); ++iter) {
|
---|
202 | counts[(*iter)->getType()->getNumber()]++;
|
---|
203 | if ((*iter)->DistanceSquared(Origin) > size)
|
---|
204 | size = (*iter)->DistanceSquared(Origin);
|
---|
205 | }
|
---|
206 | // output Index, Name, number of atoms, chemical formula
|
---|
207 | (*out) << ((*ListRunner)->ActiveFlag ? "*" : " ") << (*ListRunner)->IndexNr << "\t" << (*ListRunner)->name << "\t\t" << (*ListRunner)->getAtomCount() << "\t";
|
---|
208 |
|
---|
209 | std::map<atomicNumber_t,unsigned int>::reverse_iterator iter;
|
---|
210 | for(iter=counts.rbegin(); iter!=counts.rend();++iter){
|
---|
211 | atomicNumber_t Z =(*iter).first;
|
---|
212 | (*out) << periode->FindElement(Z)->getSymbol() << (*iter).second;
|
---|
213 | }
|
---|
214 | // Center and size
|
---|
215 | Vector *Center = (*ListRunner)->DetermineCenterOfAll();
|
---|
216 | (*out) << "\t" << *Center << "\t" << sqrt(size) << endl;
|
---|
217 | delete(Center);
|
---|
218 | }
|
---|
219 | }
|
---|
220 | };
|
---|
221 |
|
---|
222 | /** Returns the molecule with the given index \a index.
|
---|
223 | * \param index index of the desired molecule
|
---|
224 | * \return pointer to molecule structure, NULL if not found
|
---|
225 | */
|
---|
226 | molecule * MoleculeListClass::ReturnIndex(int index)
|
---|
227 | {
|
---|
228 | for(MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++)
|
---|
229 | if ((*ListRunner)->IndexNr == index)
|
---|
230 | return (*ListRunner);
|
---|
231 | return NULL;
|
---|
232 | };
|
---|
233 |
|
---|
234 |
|
---|
235 | /** Simple output of the pointers in ListOfMolecules.
|
---|
236 | * \param *out output stream
|
---|
237 | */
|
---|
238 | void MoleculeListClass::Output(std::ostream *out)
|
---|
239 | {
|
---|
240 | if (DoLog(1)) {
|
---|
241 | std::stringstream output;
|
---|
242 | output << "MoleculeList: ";
|
---|
243 | for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++)
|
---|
244 | output << *ListRunner << "\t";
|
---|
245 | LOG(1, output.str());
|
---|
246 | }
|
---|
247 | };
|
---|
248 |
|
---|
249 | /** Returns a string with \a i prefixed with 0s to match order of total number of molecules in digits.
|
---|
250 | * \param FragmentNumber total number of fragments to determine necessary number of digits
|
---|
251 | * \param digits number to create with 0 prefixed
|
---|
252 | * \return allocated(!) char array with number in digits, ten base.
|
---|
253 | */
|
---|
254 | inline char *FixedDigitNumber(const int FragmentNumber, const int digits)
|
---|
255 | {
|
---|
256 | char *returnstring;
|
---|
257 | int number = FragmentNumber;
|
---|
258 | int order = 0;
|
---|
259 | while (number != 0) { // determine number of digits needed
|
---|
260 | number = (int)floor(((double)number / 10.));
|
---|
261 | order++;
|
---|
262 | //LOG(0, "Number is " << number << ", order is " << order << ".");
|
---|
263 | }
|
---|
264 | // allocate string
|
---|
265 | returnstring = new char[order + 2];
|
---|
266 | // terminate and fill string array from end backward
|
---|
267 | returnstring[order] = '\0';
|
---|
268 | number = digits;
|
---|
269 | for (int i=order;i--;) {
|
---|
270 | returnstring[i] = '0' + (char)(number % 10);
|
---|
271 | number = (int)floor(((double)number / 10.));
|
---|
272 | }
|
---|
273 | //LOG(0, returnstring);
|
---|
274 | return returnstring;
|
---|
275 | };
|
---|
276 |
|
---|
277 | /** Calculates necessary hydrogen correction due to unwanted interaction between saturated ones.
|
---|
278 | * If for a pair of two hydrogen atoms a and b, at least is a saturated one, and a and b are not
|
---|
279 | * bonded to the same atom, then we add for this pair a correction term constructed from a Morse
|
---|
280 | * potential function fit to QM calculations with respecting to the interatomic hydrogen distance.
|
---|
281 | * \param &path path to file
|
---|
282 | */
|
---|
283 | bool MoleculeListClass::AddHydrogenCorrection(std::string &path)
|
---|
284 | {
|
---|
285 | const bond *Binder = NULL;
|
---|
286 | double ***FitConstant = NULL, **correction = NULL;
|
---|
287 | int a, b;
|
---|
288 | ofstream output;
|
---|
289 | ifstream input;
|
---|
290 | string line;
|
---|
291 | stringstream zeile;
|
---|
292 | double distance;
|
---|
293 | char ParsedLine[1023];
|
---|
294 | double tmp;
|
---|
295 | char *FragmentNumber = NULL;
|
---|
296 |
|
---|
297 | LOG(1, "Saving hydrogen saturation correction ... ");
|
---|
298 | // 0. parse in fit constant files that should have the same dimension as the final energy files
|
---|
299 | // 0a. find dimension of matrices with constants
|
---|
300 | line = path;
|
---|
301 | line += "1";
|
---|
302 | line += FITCONSTANTSUFFIX;
|
---|
303 | input.open(line.c_str());
|
---|
304 | if (input.fail()) {
|
---|
305 | LOG(1, endl << "Unable to open " << line << ", is the directory correct?");
|
---|
306 | return false;
|
---|
307 | }
|
---|
308 | a = 0;
|
---|
309 | b = -1; // we overcount by one
|
---|
310 | while (!input.eof()) {
|
---|
311 | input.getline(ParsedLine, 1023);
|
---|
312 | zeile.str(ParsedLine);
|
---|
313 | int i = 0;
|
---|
314 | while (!zeile.eof()) {
|
---|
315 | zeile >> distance;
|
---|
316 | i++;
|
---|
317 | }
|
---|
318 | if (i > a)
|
---|
319 | a = i;
|
---|
320 | b++;
|
---|
321 | }
|
---|
322 | LOG(0, "I recognized " << a << " columns and " << b << " rows, ");
|
---|
323 | input.close();
|
---|
324 |
|
---|
325 | // 0b. allocate memory for constants
|
---|
326 | FitConstant = new double**[3];
|
---|
327 | for (int k = 0; k < 3; k++) {
|
---|
328 | FitConstant[k] = new double*[a];
|
---|
329 | for (int i = a; i--;) {
|
---|
330 | FitConstant[k][i] = new double[b];
|
---|
331 | for (int j = b; j--;) {
|
---|
332 | FitConstant[k][i][j] = 0.;
|
---|
333 | }
|
---|
334 | }
|
---|
335 | }
|
---|
336 | // 0c. parse in constants
|
---|
337 | for (int i = 0; i < 3; i++) {
|
---|
338 | line = path;
|
---|
339 | line.append("/");
|
---|
340 | line += FRAGMENTPREFIX;
|
---|
341 | sprintf(ParsedLine, "%d", i + 1);
|
---|
342 | line += ParsedLine;
|
---|
343 | line += FITCONSTANTSUFFIX;
|
---|
344 | input.open(line.c_str());
|
---|
345 | if (input.fail()) {
|
---|
346 | ELOG(0, endl << "Unable to open " << line << ", is the directory correct?");
|
---|
347 | performCriticalExit();
|
---|
348 | return false;
|
---|
349 | }
|
---|
350 | int k = 0, l;
|
---|
351 | while ((!input.eof()) && (k < b)) {
|
---|
352 | input.getline(ParsedLine, 1023);
|
---|
353 | //LOG(1, "INFO: Current Line: " << ParsedLine);
|
---|
354 | zeile.str(ParsedLine);
|
---|
355 | zeile.clear();
|
---|
356 | l = 0;
|
---|
357 | //std::stringstream output;
|
---|
358 | while ((!zeile.eof()) && (l < a)) {
|
---|
359 | zeile >> FitConstant[i][l][k];
|
---|
360 | //output << FitConstant[i][l][k] << "\t";
|
---|
361 | l++;
|
---|
362 | }
|
---|
363 | //LOG(1, "INFO: fit constant are " << output.str());
|
---|
364 | k++;
|
---|
365 | }
|
---|
366 | input.close();
|
---|
367 | }
|
---|
368 | if (DoLog(1)) {
|
---|
369 | for (int k = 0; k < 3; k++) {
|
---|
370 | std::stringstream output;
|
---|
371 | output << "Constants " << k << ": ";
|
---|
372 | for (int j = 0; j < b; j++) {
|
---|
373 | for (int i = 0; i < a; i++) {
|
---|
374 | output << FitConstant[k][i][j] << "\t";
|
---|
375 | }
|
---|
376 | output << std::endl;
|
---|
377 | }
|
---|
378 | LOG(0, output.str());
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 | // 0d. allocate final correction matrix
|
---|
383 | correction = new double*[a];
|
---|
384 | for (int i = a; i--;)
|
---|
385 | correction[i] = new double[b];
|
---|
386 |
|
---|
387 | // 1a. go through every molecule in the list
|
---|
388 | for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) {
|
---|
389 | // 1b. zero final correction matrix
|
---|
390 | for (int k = a; k--;)
|
---|
391 | for (int j = b; j--;)
|
---|
392 | correction[k][j] = 0.;
|
---|
393 | // 2. take every hydrogen that is a saturated one
|
---|
394 | for (molecule::const_iterator iter = (*ListRunner)->begin(); iter != (*ListRunner)->end(); ++iter) {
|
---|
395 | //LOG(1, "(*iter): " << *(*iter) << " with first bond " << *((*iter)->getListOfBonds().begin()) << ".");
|
---|
396 | if (((*iter)->getType()->getAtomicNumber() == 1) && (((*iter)->father == NULL)
|
---|
397 | || ((*iter)->father->getType()->getAtomicNumber() != 1))) { // if it's a hydrogen
|
---|
398 | for (molecule::const_iterator runner = (*ListRunner)->begin(); runner != (*ListRunner)->end(); ++runner) {
|
---|
399 | //LOG(2, "Runner: " << *(*runner) << " with first bond " << *((*iter)->getListOfBonds().begin()) << ".");
|
---|
400 | // 3. take every other hydrogen that is the not the first and not bound to same bonding partner
|
---|
401 | const BondList &bondlist = (*runner)->getListOfBonds();
|
---|
402 | Binder = *(bondlist.begin());
|
---|
403 | if (((*runner)->getType()->getAtomicNumber() == 1) && ((*runner)->getNr() > (*iter)->getNr()) && (Binder->GetOtherAtom((*runner)) != Binder->GetOtherAtom((*iter)))) { // (hydrogens have only one bonding partner!)
|
---|
404 | // 4. evaluate the morse potential for each matrix component and add up
|
---|
405 | distance = (*runner)->distance(*(*iter));
|
---|
406 | //std::stringstream output;
|
---|
407 | //output << "Fragment " << (*ListRunner)->name << ": " << *(*runner) << "<= " << distance << "=>" << *(*iter) << ":";
|
---|
408 | for (int k = 0; k < a; k++) {
|
---|
409 | for (int j = 0; j < b; j++) {
|
---|
410 | switch (k) {
|
---|
411 | case 1:
|
---|
412 | case 7:
|
---|
413 | case 11:
|
---|
414 | tmp = pow(FitConstant[0][k][j] * (1. - exp(-FitConstant[1][k][j] * (distance - FitConstant[2][k][j]))), 2);
|
---|
415 | break;
|
---|
416 | default:
|
---|
417 | tmp = FitConstant[0][k][j] * pow(distance, FitConstant[1][k][j]) + FitConstant[2][k][j];
|
---|
418 | break;
|
---|
419 | };
|
---|
420 | correction[k][j] -= tmp; // ground state is actually lower (disturbed by additional interaction)
|
---|
421 | //output << tmp << "\t";
|
---|
422 | }
|
---|
423 | //output << endl;
|
---|
424 | }
|
---|
425 | //LOG(0, output.str());
|
---|
426 | }
|
---|
427 | }
|
---|
428 | }
|
---|
429 | }
|
---|
430 | // 5. write final matrix to file
|
---|
431 | line = path;
|
---|
432 | line.append("/");
|
---|
433 | line += FRAGMENTPREFIX;
|
---|
434 | FragmentNumber = FixedDigitNumber(ListOfMolecules.size(), (*ListRunner)->IndexNr);
|
---|
435 | line += FragmentNumber;
|
---|
436 | delete[] (FragmentNumber);
|
---|
437 | line += HCORRECTIONSUFFIX;
|
---|
438 | output.open(line.c_str());
|
---|
439 | output << "Time\t\tTotal\t\tKinetic\t\tNonLocal\tCorrelation\tExchange\tPseudo\t\tHartree\t\t-Gauss\t\tEwald\t\tIonKin\t\tETotal" << endl;
|
---|
440 | for (int j = 0; j < b; j++) {
|
---|
441 | for (int i = 0; i < a; i++)
|
---|
442 | output << correction[i][j] << "\t";
|
---|
443 | output << endl;
|
---|
444 | }
|
---|
445 | output.close();
|
---|
446 | }
|
---|
447 | for (int i = a; i--;)
|
---|
448 | delete[](correction[i]);
|
---|
449 | delete[](correction);
|
---|
450 |
|
---|
451 | line = path;
|
---|
452 | line.append("/");
|
---|
453 | line += HCORRECTIONSUFFIX;
|
---|
454 | output.open(line.c_str());
|
---|
455 | output << "Time\t\tTotal\t\tKinetic\t\tNonLocal\tCorrelation\tExchange\tPseudo\t\tHartree\t\t-Gauss\t\tEwald\t\tIonKin\t\tETotal" << endl;
|
---|
456 | for (int j = 0; j < b; j++) {
|
---|
457 | for (int i = 0; i < a; i++)
|
---|
458 | output << 0 << "\t";
|
---|
459 | output << endl;
|
---|
460 | }
|
---|
461 | output.close();
|
---|
462 | // 6. free memory of parsed matrices
|
---|
463 | for (int k = 0; k < 3; k++) {
|
---|
464 | for (int i = a; i--;) {
|
---|
465 | delete[](FitConstant[k][i]);
|
---|
466 | }
|
---|
467 | delete[](FitConstant[k]);
|
---|
468 | }
|
---|
469 | delete[](FitConstant);
|
---|
470 | LOG(0, "done.");
|
---|
471 | return true;
|
---|
472 | };
|
---|
473 |
|
---|
474 | /** Store force indices, i.e. the connection between the nuclear index in the total molecule config and the respective atom in fragment config.
|
---|
475 | * \param &path path to file
|
---|
476 | * \param *SortIndex Index to map from the BFS labeling to the sequence how of Ion_Type in the config
|
---|
477 | * \return true - file written successfully, false - writing failed
|
---|
478 | */
|
---|
479 | bool MoleculeListClass::StoreForcesFile(std::string &path, int *SortIndex)
|
---|
480 | {
|
---|
481 | bool status = true;
|
---|
482 | string filename(path);
|
---|
483 | filename += FORCESFILE;
|
---|
484 | ofstream ForcesFile(filename.c_str());
|
---|
485 | periodentafel *periode=World::getInstance().getPeriode();
|
---|
486 |
|
---|
487 | // open file for the force factors
|
---|
488 | LOG(1, "Saving force factors ... ");
|
---|
489 | if (!ForcesFile.fail()) {
|
---|
490 | //output << prefix << "Forces" << endl;
|
---|
491 | for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) {
|
---|
492 | periodentafel::const_iterator elemIter;
|
---|
493 | for(elemIter=periode->begin();elemIter!=periode->end();++elemIter){
|
---|
494 | if ((*ListRunner)->hasElement((*elemIter).first)) { // if this element got atoms
|
---|
495 | for(molecule::iterator atomIter = (*ListRunner)->begin(); atomIter !=(*ListRunner)->end();++atomIter){
|
---|
496 | if ((*atomIter)->getType()->getNumber() == (*elemIter).first) {
|
---|
497 | if (((*atomIter)->GetTrueFather() != NULL) && ((*atomIter)->GetTrueFather() != (*atomIter))) {// if there is a rea
|
---|
498 | ForcesFile << SortIndex[(*atomIter)->GetTrueFather()->getNr()] << "\t";
|
---|
499 | } else
|
---|
500 | // otherwise a -1 to indicate an added saturation hydrogen
|
---|
501 | ForcesFile << "-1\t";
|
---|
502 | }
|
---|
503 | }
|
---|
504 | }
|
---|
505 | }
|
---|
506 | ForcesFile << endl;
|
---|
507 | }
|
---|
508 | ForcesFile.close();
|
---|
509 | LOG(1, "done.");
|
---|
510 | } else {
|
---|
511 | status = false;
|
---|
512 | LOG(1, "failed to open file " << filename << ".");
|
---|
513 | }
|
---|
514 | ForcesFile.close();
|
---|
515 |
|
---|
516 | return status;
|
---|
517 | };
|
---|
518 |
|
---|
519 | /** Writes a config file for each molecule in the given \a **FragmentList.
|
---|
520 | * \param *out output stream for debugging
|
---|
521 | * \param &prefix path and prefix to the fragment config files
|
---|
522 | * \param *SortIndex Index to map from the BFS labeling to the sequence how of Ion_Type in the config
|
---|
523 | * \param type desired type to store
|
---|
524 | * \return true - success (each file was written), false - something went wrong.
|
---|
525 | */
|
---|
526 | bool MoleculeListClass::OutputConfigForListOfFragments(std::string &prefix, int *SortIndex, ParserTypes type)
|
---|
527 | {
|
---|
528 | ofstream outputFragment;
|
---|
529 | std::string FragmentName;
|
---|
530 | bool result = true;
|
---|
531 | bool intermediateResult = true;
|
---|
532 | Vector BoxDimension;
|
---|
533 | char *FragmentNumber = NULL;
|
---|
534 | int FragmentCounter = 0;
|
---|
535 | ofstream output;
|
---|
536 | RealSpaceMatrix cell_size = World::getInstance().getDomain().getM();
|
---|
537 | RealSpaceMatrix cell_size_backup = cell_size;
|
---|
538 | int count=0;
|
---|
539 |
|
---|
540 | // store the fragments as config and as xyz
|
---|
541 | for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) {
|
---|
542 | // correct periodic
|
---|
543 | if ((*ListRunner)->ScanForPeriodicCorrection()) {
|
---|
544 | count++;
|
---|
545 | }
|
---|
546 |
|
---|
547 | {
|
---|
548 | // list atoms in fragment for debugging
|
---|
549 | std::stringstream output;
|
---|
550 | output << "Contained atoms: ";
|
---|
551 | for (molecule::const_iterator iter = (*ListRunner)->begin(); iter != (*ListRunner)->end(); ++iter) {
|
---|
552 | output << (*iter)->getName() << " ";
|
---|
553 | }
|
---|
554 | LOG(2, output.str());
|
---|
555 | }
|
---|
556 |
|
---|
557 | {
|
---|
558 | // center on edge
|
---|
559 | (*ListRunner)->CenterEdge(&BoxDimension);
|
---|
560 | for (int k = 0; k < NDIM; k++) // if one edge is to small, set at least to 1 angstroem
|
---|
561 | if (BoxDimension[k] < 1.)
|
---|
562 | BoxDimension[k] += 1.;
|
---|
563 | (*ListRunner)->SetBoxDimension(&BoxDimension); // update Box of atoms by boundary
|
---|
564 | for (int k = 0; k < NDIM; k++) {
|
---|
565 | BoxDimension[k] = 2.5 * (World::getInstance().getConfig()->GetIsAngstroem() ? 1. : 1. / AtomicLengthToAngstroem);
|
---|
566 | cell_size.at(k,k) = BoxDimension[k] * 2.;
|
---|
567 | }
|
---|
568 | World::getInstance().setDomain(cell_size);
|
---|
569 | (*ListRunner)->Translate(&BoxDimension);
|
---|
570 |
|
---|
571 | // output file
|
---|
572 | std::vector<atom *> atoms;
|
---|
573 | for (molecule::const_iterator iter = (*ListRunner)->begin(); iter != (*ListRunner)->end(); ++iter) {
|
---|
574 | atoms.push_back(*iter);
|
---|
575 | }
|
---|
576 | FragmentNumber = FixedDigitNumber(ListOfMolecules.size(), FragmentCounter++);
|
---|
577 | FragmentName = prefix + FragmentNumber + "." + FormatParserStorage::getInstance().getSuffixFromType(type);
|
---|
578 | outputFragment.open(FragmentName.c_str(), ios::out);
|
---|
579 | std::stringstream output;
|
---|
580 | output << "Saving bond fragment No. " << FragmentNumber << "/" << FragmentCounter - 1 << " as XYZ ... ";
|
---|
581 | if ((intermediateResult = FormatParserStorage::getInstance().save(
|
---|
582 | outputFragment,
|
---|
583 | FormatParserStorage::getInstance().getSuffixFromType(type),
|
---|
584 | atoms)))
|
---|
585 | output << " done.";
|
---|
586 | else
|
---|
587 | output << " failed.";
|
---|
588 | LOG(3, output.str());
|
---|
589 | delete[](FragmentNumber);
|
---|
590 |
|
---|
591 | result = result && intermediateResult;
|
---|
592 | outputFragment.close();
|
---|
593 | outputFragment.clear();
|
---|
594 | }
|
---|
595 | }
|
---|
596 | LOG(0, "STATUS: done.");
|
---|
597 |
|
---|
598 | // printing final number
|
---|
599 | LOG(2, "INFO: Final number of fragments: " << FragmentCounter << ".");
|
---|
600 |
|
---|
601 | // printing final number
|
---|
602 | LOG(2, "INFO: For " << count << " fragments periodic correction would have been necessary.");
|
---|
603 |
|
---|
604 | // restore cell_size
|
---|
605 | World::getInstance().setDomain(cell_size_backup);
|
---|
606 |
|
---|
607 | return result;
|
---|
608 | };
|
---|
609 |
|
---|
610 | /** Counts the number of molecules with the molecule::ActiveFlag set.
|
---|
611 | * \return number of molecules with ActiveFlag set to true.
|
---|
612 | */
|
---|
613 | int MoleculeListClass::NumberOfActiveMolecules()
|
---|
614 | {
|
---|
615 | int count = 0;
|
---|
616 | for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++)
|
---|
617 | count += ((*ListRunner)->ActiveFlag ? 1 : 0);
|
---|
618 | return count;
|
---|
619 | };
|
---|
620 |
|
---|
621 | /** Count all atoms in each molecule.
|
---|
622 | * \return number of atoms in the MoleculeListClass.
|
---|
623 | * TODO: the inner loop should be done by some (double molecule::CountAtom()) function
|
---|
624 | */
|
---|
625 | int MoleculeListClass::CountAllAtoms() const
|
---|
626 | {
|
---|
627 | int AtomNo = 0;
|
---|
628 | for (MoleculeList::const_iterator MolWalker = ListOfMolecules.begin(); MolWalker != ListOfMolecules.end(); MolWalker++) {
|
---|
629 | AtomNo += (*MolWalker)->size();
|
---|
630 | }
|
---|
631 | return AtomNo;
|
---|
632 | }
|
---|
633 |
|
---|
634 | /***********
|
---|
635 | * Methods Moved here from the menus
|
---|
636 | */
|
---|
637 |
|
---|
638 | void MoleculeListClass::createNewMolecule(periodentafel *periode) {
|
---|
639 | OBSERVE;
|
---|
640 | molecule *mol = NULL;
|
---|
641 | mol = World::getInstance().createMolecule();
|
---|
642 | insert(mol);
|
---|
643 | };
|
---|
644 |
|
---|
645 | void MoleculeListClass::loadFromXYZ(periodentafel *periode){
|
---|
646 | molecule *mol = NULL;
|
---|
647 | Vector center;
|
---|
648 | char filename[MAXSTRINGSIZE];
|
---|
649 | std::cout << "Format should be XYZ with: ShorthandOfElement\tX\tY\tZ" << endl;
|
---|
650 | mol = World::getInstance().createMolecule();
|
---|
651 | do {
|
---|
652 | std::cout << "Enter file name: ";
|
---|
653 | cin >> filename;
|
---|
654 | } while (!mol->AddXYZFile(filename));
|
---|
655 | mol->SetNameFromFilename(filename);
|
---|
656 | // center at set box dimensions
|
---|
657 | mol->CenterEdge(¢er);
|
---|
658 | RealSpaceMatrix domain;
|
---|
659 | for(int i =0;i<NDIM;++i)
|
---|
660 | domain.at(i,i) = center[i];
|
---|
661 | World::getInstance().setDomain(domain);
|
---|
662 | insert(mol);
|
---|
663 | }
|
---|
664 |
|
---|
665 | void MoleculeListClass::setMoleculeFilename() {
|
---|
666 | char filename[MAXSTRINGSIZE];
|
---|
667 | int nr;
|
---|
668 | molecule *mol = NULL;
|
---|
669 | do {
|
---|
670 | std::cout << "Enter index of molecule: ";
|
---|
671 | cin >> nr;
|
---|
672 | mol = ReturnIndex(nr);
|
---|
673 | } while (mol == NULL);
|
---|
674 | std::cout << "Enter name: ";
|
---|
675 | cin >> filename;
|
---|
676 | mol->SetNameFromFilename(filename);
|
---|
677 | }
|
---|
678 |
|
---|
679 | void MoleculeListClass::parseXYZIntoMolecule(){
|
---|
680 | char filename[MAXSTRINGSIZE];
|
---|
681 | int nr;
|
---|
682 | molecule *mol = NULL;
|
---|
683 | mol = NULL;
|
---|
684 | do {
|
---|
685 | std::cout << "Enter index of molecule: ";
|
---|
686 | cin >> nr;
|
---|
687 | mol = ReturnIndex(nr);
|
---|
688 | } while (mol == NULL);
|
---|
689 | std::cout << "Format should be XYZ with: ShorthandOfElement\tX\tY\tZ" << endl;
|
---|
690 | do {
|
---|
691 | std::cout << "Enter file name: ";
|
---|
692 | cin >> filename;
|
---|
693 | } while (!mol->AddXYZFile(filename));
|
---|
694 | mol->SetNameFromFilename(filename);
|
---|
695 | };
|
---|
696 |
|
---|
697 | void MoleculeListClass::eraseMolecule(){
|
---|
698 | int nr;
|
---|
699 | molecule *mol = NULL;
|
---|
700 | std::cout << "Enter index of molecule: ";
|
---|
701 | cin >> nr;
|
---|
702 | for(MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++)
|
---|
703 | if (nr == (*ListRunner)->IndexNr) {
|
---|
704 | mol = *ListRunner;
|
---|
705 | ListOfMolecules.erase(ListRunner);
|
---|
706 | World::getInstance().destroyMolecule(mol);
|
---|
707 | break;
|
---|
708 | }
|
---|
709 | };
|
---|