source: src/moleculelist.cpp@ 50e4e5

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 50e4e5 was 07a47e, checked in by Frederik Heber <heber@…>, 13 years ago

Replaced enable/disable-hydrogen by internal switch.

  • Property mode set to 100755
File size: 23.6 KB
Line 
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
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 */
48MoleculeListClass::MoleculeListClass(World *_world) :
49 Observable("MoleculeListClass"),
50 MaxIndex(1),
51 world(_world)
52{};
53
54/** Destructor for MoleculeListClass.
55 */
56MoleculeListClass::~MoleculeListClass()
57{
58 DoLog(4) && (Log() << Verbose(4) << "Clearing ListOfMolecules." << endl);
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 */
67void 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 */
78void 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 */
90int 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 */
106int 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() << Verbose(0) << "Comparing fragment no. " << *(molecule **)a << " to " << *(molecule **)b << "." << endl;
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 */
184void 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 */
226molecule * 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 */
238void MoleculeListClass::Output(std::ostream *out)
239{
240 DoLog(1) && (Log() << Verbose(1) << "MoleculeList: ");
241 for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++)
242 DoLog(0) && (Log() << Verbose(0) << *ListRunner << "\t");
243 DoLog(0) && (Log() << Verbose(0) << endl);
244};
245
246/** Returns a string with \a i prefixed with 0s to match order of total number of molecules in digits.
247 * \param FragmentNumber total number of fragments to determine necessary number of digits
248 * \param digits number to create with 0 prefixed
249 * \return allocated(!) char array with number in digits, ten base.
250 */
251inline char *FixedDigitNumber(const int FragmentNumber, const int digits)
252{
253 char *returnstring;
254 int number = FragmentNumber;
255 int order = 0;
256 while (number != 0) { // determine number of digits needed
257 number = (int)floor(((double)number / 10.));
258 order++;
259 //Log() << Verbose(0) << "Number is " << number << ", order is " << order << "." << endl;
260 }
261 // allocate string
262 returnstring = new char[order + 2];
263 // terminate and fill string array from end backward
264 returnstring[order] = '\0';
265 number = digits;
266 for (int i=order;i--;) {
267 returnstring[i] = '0' + (char)(number % 10);
268 number = (int)floor(((double)number / 10.));
269 }
270 //Log() << Verbose(0) << returnstring << endl;
271 return returnstring;
272};
273
274/** Calculates necessary hydrogen correction due to unwanted interaction between saturated ones.
275 * If for a pair of two hydrogen atoms a and b, at least is a saturated one, and a and b are not
276 * bonded to the same atom, then we add for this pair a correction term constructed from a Morse
277 * potential function fit to QM calculations with respecting to the interatomic hydrogen distance.
278 * \param &path path to file
279 */
280bool MoleculeListClass::AddHydrogenCorrection(std::string &path)
281{
282 const bond *Binder = NULL;
283 double ***FitConstant = NULL, **correction = NULL;
284 int a, b;
285 ofstream output;
286 ifstream input;
287 string line;
288 stringstream zeile;
289 double distance;
290 char ParsedLine[1023];
291 double tmp;
292 char *FragmentNumber = NULL;
293
294 DoLog(1) && (Log() << Verbose(1) << "Saving hydrogen saturation correction ... ");
295 // 0. parse in fit constant files that should have the same dimension as the final energy files
296 // 0a. find dimension of matrices with constants
297 line = path;
298 line += "1";
299 line += FITCONSTANTSUFFIX;
300 input.open(line.c_str());
301 if (input.fail()) {
302 DoLog(1) && (Log() << Verbose(1) << endl << "Unable to open " << line << ", is the directory correct?" << endl);
303 return false;
304 }
305 a = 0;
306 b = -1; // we overcount by one
307 while (!input.eof()) {
308 input.getline(ParsedLine, 1023);
309 zeile.str(ParsedLine);
310 int i = 0;
311 while (!zeile.eof()) {
312 zeile >> distance;
313 i++;
314 }
315 if (i > a)
316 a = i;
317 b++;
318 }
319 DoLog(0) && (Log() << Verbose(0) << "I recognized " << a << " columns and " << b << " rows, ");
320 input.close();
321
322 // 0b. allocate memory for constants
323 FitConstant = new double**[3];
324 for (int k = 0; k < 3; k++) {
325 FitConstant[k] = new double*[a];
326 for (int i = a; i--;) {
327 FitConstant[k][i] = new double[b];
328 for (int j = b; j--;) {
329 FitConstant[k][i][j] = 0.;
330 }
331 }
332 }
333 // 0c. parse in constants
334 for (int i = 0; i < 3; i++) {
335 line = path;
336 line.append("/");
337 line += FRAGMENTPREFIX;
338 sprintf(ParsedLine, "%d", i + 1);
339 line += ParsedLine;
340 line += FITCONSTANTSUFFIX;
341 input.open(line.c_str());
342 if (input.fail()) {
343 DoeLog(0) && (eLog()<< Verbose(0) << endl << "Unable to open " << line << ", is the directory correct?" << endl);
344 performCriticalExit();
345 return false;
346 }
347 int k = 0, l;
348 while ((!input.eof()) && (k < b)) {
349 input.getline(ParsedLine, 1023);
350 //Log() << Verbose(0) << "Current Line: " << ParsedLine << endl;
351 zeile.str(ParsedLine);
352 zeile.clear();
353 l = 0;
354 while ((!zeile.eof()) && (l < a)) {
355 zeile >> FitConstant[i][l][k];
356 //Log() << Verbose(0) << FitConstant[i][l][k] << "\t";
357 l++;
358 }
359 //Log() << Verbose(0) << endl;
360 k++;
361 }
362 input.close();
363 }
364 for (int k = 0; k < 3; k++) {
365 DoLog(0) && (Log() << Verbose(0) << "Constants " << k << ":" << endl);
366 for (int j = 0; j < b; j++) {
367 for (int i = 0; i < a; i++) {
368 DoLog(0) && (Log() << Verbose(0) << FitConstant[k][i][j] << "\t");
369 }
370 DoLog(0) && (Log() << Verbose(0) << endl);
371 }
372 DoLog(0) && (Log() << Verbose(0) << endl);
373 }
374
375 // 0d. allocate final correction matrix
376 correction = new double*[a];
377 for (int i = a; i--;)
378 correction[i] = new double[b];
379
380 // 1a. go through every molecule in the list
381 for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) {
382 // 1b. zero final correction matrix
383 for (int k = a; k--;)
384 for (int j = b; j--;)
385 correction[k][j] = 0.;
386 // 2. take every hydrogen that is a saturated one
387 for (molecule::const_iterator iter = (*ListRunner)->begin(); iter != (*ListRunner)->end(); ++iter) {
388 //Log() << Verbose(1) << "(*iter): " << *(*iter) << " with first bond " << *((*iter)->getListOfBonds().begin()) << "." << endl;
389 if (((*iter)->getType()->getAtomicNumber() == 1) && (((*iter)->father == NULL)
390 || ((*iter)->father->getType()->getAtomicNumber() != 1))) { // if it's a hydrogen
391 for (molecule::const_iterator runner = (*ListRunner)->begin(); runner != (*ListRunner)->end(); ++runner) {
392 //Log() << Verbose(2) << "Runner: " << *(*runner) << " with first bond " << *((*iter)->getListOfBonds().begin()) << "." << endl;
393 // 3. take every other hydrogen that is the not the first and not bound to same bonding partner
394 const BondList &bondlist = (*runner)->getListOfBonds();
395 Binder = *(bondlist.begin());
396 if (((*runner)->getType()->getAtomicNumber() == 1) && ((*runner)->getNr() > (*iter)->getNr()) && (Binder->GetOtherAtom((*runner)) != Binder->GetOtherAtom((*iter)))) { // (hydrogens have only one bonding partner!)
397 // 4. evaluate the morse potential for each matrix component and add up
398 distance = (*runner)->distance(*(*iter));
399 //Log() << Verbose(0) << "Fragment " << (*ListRunner)->name << ": " << *(*runner) << "<= " << distance << "=>" << *(*iter) << ":" << endl;
400 for (int k = 0; k < a; k++) {
401 for (int j = 0; j < b; j++) {
402 switch (k) {
403 case 1:
404 case 7:
405 case 11:
406 tmp = pow(FitConstant[0][k][j] * (1. - exp(-FitConstant[1][k][j] * (distance - FitConstant[2][k][j]))), 2);
407 break;
408 default:
409 tmp = FitConstant[0][k][j] * pow(distance, FitConstant[1][k][j]) + FitConstant[2][k][j];
410 };
411 correction[k][j] -= tmp; // ground state is actually lower (disturbed by additional interaction)
412 //Log() << Verbose(0) << tmp << "\t";
413 }
414 //Log() << Verbose(0) << endl;
415 }
416 //Log() << Verbose(0) << endl;
417 }
418 }
419 }
420 }
421 // 5. write final matrix to file
422 line = path;
423 line.append("/");
424 line += FRAGMENTPREFIX;
425 FragmentNumber = FixedDigitNumber(ListOfMolecules.size(), (*ListRunner)->IndexNr);
426 line += FragmentNumber;
427 delete[] (FragmentNumber);
428 line += HCORRECTIONSUFFIX;
429 output.open(line.c_str());
430 output << "Time\t\tTotal\t\tKinetic\t\tNonLocal\tCorrelation\tExchange\tPseudo\t\tHartree\t\t-Gauss\t\tEwald\t\tIonKin\t\tETotal" << endl;
431 for (int j = 0; j < b; j++) {
432 for (int i = 0; i < a; i++)
433 output << correction[i][j] << "\t";
434 output << endl;
435 }
436 output.close();
437 }
438 for (int i = a; i--;)
439 delete[](correction[i]);
440 delete[](correction);
441
442 line = path;
443 line.append("/");
444 line += HCORRECTIONSUFFIX;
445 output.open(line.c_str());
446 output << "Time\t\tTotal\t\tKinetic\t\tNonLocal\tCorrelation\tExchange\tPseudo\t\tHartree\t\t-Gauss\t\tEwald\t\tIonKin\t\tETotal" << endl;
447 for (int j = 0; j < b; j++) {
448 for (int i = 0; i < a; i++)
449 output << 0 << "\t";
450 output << endl;
451 }
452 output.close();
453 // 6. free memory of parsed matrices
454 for (int k = 0; k < 3; k++) {
455 for (int i = a; i--;) {
456 delete[](FitConstant[k][i]);
457 }
458 delete[](FitConstant[k]);
459 }
460 delete[](FitConstant);
461 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
462 return true;
463};
464
465/** Store force indices, i.e. the connection between the nuclear index in the total molecule config and the respective atom in fragment config.
466 * \param &path path to file
467 * \param *SortIndex Index to map from the BFS labeling to the sequence how of Ion_Type in the config
468 * \return true - file written successfully, false - writing failed
469 */
470bool MoleculeListClass::StoreForcesFile(std::string &path, int *SortIndex)
471{
472 bool status = true;
473 string filename(path);
474 filename += FORCESFILE;
475 ofstream ForcesFile(filename.c_str());
476 periodentafel *periode=World::getInstance().getPeriode();
477
478 // open file for the force factors
479 DoLog(1) && (Log() << Verbose(1) << "Saving force factors ... ");
480 if (!ForcesFile.fail()) {
481 //Log() << Verbose(1) << "Final AtomicForcesList: ";
482 //output << prefix << "Forces" << endl;
483 for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) {
484 periodentafel::const_iterator elemIter;
485 for(elemIter=periode->begin();elemIter!=periode->end();++elemIter){
486 if ((*ListRunner)->hasElement((*elemIter).first)) { // if this element got atoms
487 for(molecule::iterator atomIter = (*ListRunner)->begin(); atomIter !=(*ListRunner)->end();++atomIter){
488 if ((*atomIter)->getType()->getNumber() == (*elemIter).first) {
489 if (((*atomIter)->GetTrueFather() != NULL) && ((*atomIter)->GetTrueFather() != (*atomIter))) {// if there is a rea
490 //Log() << Verbose(0) << "Walker is " << *Walker << " with true father " << *( Walker->GetTrueFather()) << ", it
491 ForcesFile << SortIndex[(*atomIter)->GetTrueFather()->getNr()] << "\t";
492 } else
493 // otherwise a -1 to indicate an added saturation hydrogen
494 ForcesFile << "-1\t";
495 }
496 }
497 }
498 }
499 ForcesFile << endl;
500 }
501 ForcesFile.close();
502 DoLog(1) && (Log() << Verbose(1) << "done." << endl);
503 } else {
504 status = false;
505 DoLog(1) && (Log() << Verbose(1) << "failed to open file " << filename << "." << endl);
506 }
507 ForcesFile.close();
508
509 return status;
510};
511
512/** Writes a config file for each molecule in the given \a **FragmentList.
513 * \param *out output stream for debugging
514 * \param &prefix path and prefix to the fragment config files
515 * \param *SortIndex Index to map from the BFS labeling to the sequence how of Ion_Type in the config
516 * \param type desired type to store
517 * \return true - success (each file was written), false - something went wrong.
518 */
519bool MoleculeListClass::OutputConfigForListOfFragments(std::string &prefix, int *SortIndex, ParserTypes type)
520{
521 ofstream outputFragment;
522 std::string FragmentName;
523 bool result = true;
524 bool intermediateResult = true;
525 Vector BoxDimension;
526 char *FragmentNumber = NULL;
527 int FragmentCounter = 0;
528 ofstream output;
529 RealSpaceMatrix cell_size = World::getInstance().getDomain().getM();
530 RealSpaceMatrix cell_size_backup = cell_size;
531 int count=0;
532
533 // store the fragments as config and as xyz
534 for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) {
535 // correct periodic
536 if ((*ListRunner)->ScanForPeriodicCorrection()) {
537 count++;
538 }
539
540 {
541 // list atoms in fragment for debugging
542 std::stringstream output;
543 output << "Contained atoms: ";
544 for (molecule::const_iterator iter = (*ListRunner)->begin(); iter != (*ListRunner)->end(); ++iter) {
545 output << (*iter)->getName() << " ";
546 }
547 LOG(2, output.str());
548 }
549
550 {
551 // center on edge
552 (*ListRunner)->CenterEdge(&BoxDimension);
553 for (int k = 0; k < NDIM; k++) // if one edge is to small, set at least to 1 angstroem
554 if (BoxDimension[k] < 1.)
555 BoxDimension[k] += 1.;
556 (*ListRunner)->SetBoxDimension(&BoxDimension); // update Box of atoms by boundary
557 for (int k = 0; k < NDIM; k++) {
558 BoxDimension[k] = 2.5 * (World::getInstance().getConfig()->GetIsAngstroem() ? 1. : 1. / AtomicLengthToAngstroem);
559 cell_size.at(k,k) = BoxDimension[k] * 2.;
560 }
561 World::getInstance().setDomain(cell_size);
562 (*ListRunner)->Translate(&BoxDimension);
563
564 // output file
565 std::vector<atom *> atoms;
566 for (molecule::const_iterator iter = (*ListRunner)->begin(); iter != (*ListRunner)->end(); ++iter) {
567 atoms.push_back(*iter);
568 }
569 FragmentNumber = FixedDigitNumber(ListOfMolecules.size(), FragmentCounter++);
570 FragmentName = prefix + FragmentNumber + "." + FormatParserStorage::getInstance().getSuffixFromType(type);
571 outputFragment.open(FragmentName.c_str(), ios::out);
572 std::stringstream output;
573 output << "Saving bond fragment No. " << FragmentNumber << "/" << FragmentCounter - 1 << " as XYZ ... ";
574 if ((intermediateResult = FormatParserStorage::getInstance().save(
575 outputFragment,
576 FormatParserStorage::getInstance().getSuffixFromType(type),
577 atoms)))
578 output << " done.";
579 else
580 output << " failed.";
581 LOG(3, output.str());
582 delete[](FragmentNumber);
583
584 result = result && intermediateResult;
585 outputFragment.close();
586 outputFragment.clear();
587 }
588 }
589 LOG(0, "STATUS: done.");
590
591 // printing final number
592 LOG(2, "INFO: Final number of fragments: " << FragmentCounter << ".");
593
594 // printing final number
595 LOG(2, "INFO: For " << count << " fragments periodic correction would have been necessary.");
596
597 // restore cell_size
598 World::getInstance().setDomain(cell_size_backup);
599
600 return result;
601};
602
603/** Counts the number of molecules with the molecule::ActiveFlag set.
604 * \return number of molecules with ActiveFlag set to true.
605 */
606int MoleculeListClass::NumberOfActiveMolecules()
607{
608 int count = 0;
609 for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++)
610 count += ((*ListRunner)->ActiveFlag ? 1 : 0);
611 return count;
612};
613
614/** Count all atoms in each molecule.
615 * \return number of atoms in the MoleculeListClass.
616 * TODO: the inner loop should be done by some (double molecule::CountAtom()) function
617 */
618int MoleculeListClass::CountAllAtoms() const
619{
620 int AtomNo = 0;
621 for (MoleculeList::const_iterator MolWalker = ListOfMolecules.begin(); MolWalker != ListOfMolecules.end(); MolWalker++) {
622 AtomNo += (*MolWalker)->size();
623 }
624 return AtomNo;
625}
626
627/***********
628 * Methods Moved here from the menus
629 */
630
631void MoleculeListClass::createNewMolecule(periodentafel *periode) {
632 OBSERVE;
633 molecule *mol = NULL;
634 mol = World::getInstance().createMolecule();
635 insert(mol);
636};
637
638void MoleculeListClass::loadFromXYZ(periodentafel *periode){
639 molecule *mol = NULL;
640 Vector center;
641 char filename[MAXSTRINGSIZE];
642 Log() << Verbose(0) << "Format should be XYZ with: ShorthandOfElement\tX\tY\tZ" << endl;
643 mol = World::getInstance().createMolecule();
644 do {
645 Log() << Verbose(0) << "Enter file name: ";
646 cin >> filename;
647 } while (!mol->AddXYZFile(filename));
648 mol->SetNameFromFilename(filename);
649 // center at set box dimensions
650 mol->CenterEdge(&center);
651 RealSpaceMatrix domain;
652 for(int i =0;i<NDIM;++i)
653 domain.at(i,i) = center[i];
654 World::getInstance().setDomain(domain);
655 insert(mol);
656}
657
658void MoleculeListClass::setMoleculeFilename() {
659 char filename[MAXSTRINGSIZE];
660 int nr;
661 molecule *mol = NULL;
662 do {
663 Log() << Verbose(0) << "Enter index of molecule: ";
664 cin >> nr;
665 mol = ReturnIndex(nr);
666 } while (mol == NULL);
667 Log() << Verbose(0) << "Enter name: ";
668 cin >> filename;
669 mol->SetNameFromFilename(filename);
670}
671
672void MoleculeListClass::parseXYZIntoMolecule(){
673 char filename[MAXSTRINGSIZE];
674 int nr;
675 molecule *mol = NULL;
676 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) << "Format should be XYZ with: ShorthandOfElement\tX\tY\tZ" << endl;
683 do {
684 Log() << Verbose(0) << "Enter file name: ";
685 cin >> filename;
686 } while (!mol->AddXYZFile(filename));
687 mol->SetNameFromFilename(filename);
688};
689
690void MoleculeListClass::eraseMolecule(){
691 int nr;
692 molecule *mol = NULL;
693 Log() << Verbose(0) << "Enter index of molecule: ";
694 cin >> nr;
695 for(MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++)
696 if (nr == (*ListRunner)->IndexNr) {
697 mol = *ListRunner;
698 ListOfMolecules.erase(ListRunner);
699 World::getInstance().destroyMolecule(mol);
700 break;
701 }
702};
Note: See TracBrowser for help on using the repository browser.