source: src/periodentafel.cpp@ ba9f5b

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 ba9f5b was f34c23, checked in by Frederik Heber <heber@…>, 15 years ago

MEMFIX: Periodentafel constructor would instantiate stringstreams in database load calls but not remove them.

  • Property mode set to 100755
File size: 15.3 KB
Line 
1/** \file periodentafel.cpp
2 *
3 * Function implementations for the class periodentafel.
4 *
5 */
6
7#include "Helpers/MemDebug.hpp"
8
9using namespace std;
10
11#include <iomanip>
12#include <iostream>
13#include <fstream>
14#include <cstring>
15
16#include "Helpers/Assert.hpp"
17#include "element.hpp"
18#include "elements_db.hpp"
19#include "helpers.hpp"
20#include "lists.hpp"
21#include "log.hpp"
22#include "periodentafel.hpp"
23#include "verbose.hpp"
24
25using namespace std;
26
27/************************************* Functions for class periodentafel ***************************/
28
29/** constructor for class periodentafel
30 * Initialises start and end of list and resets periodentafel::checkliste to false.
31 */
32periodentafel::periodentafel()
33{
34 {
35 stringstream input(elementsDB,ios_base::in);
36 bool status = LoadElementsDatabase(&input);
37 ASSERT(status, "General element initialization failed");
38 }
39 {
40 stringstream input(valenceDB,ios_base::in);
41 bool status = LoadValenceDatabase(&input);
42 ASSERT(status, "Valence entry of element initialization failed");
43 }
44 {
45 stringstream input(orbitalsDB,ios_base::in);
46 bool status = LoadOrbitalsDatabase(&input);
47 ASSERT(status, "Orbitals entry of element initialization failed");
48 }
49 {
50 stringstream input(HbondangleDB,ios_base::in);
51 bool status = LoadHBondAngleDatabase(&input);
52 ASSERT(status, "HBond angle entry of element initialization failed");
53 }
54 {
55 stringstream input(HbonddistanceDB,ios_base::in);
56 bool status = LoadHBondLengthsDatabase(&input);
57 ASSERT(status, "HBond distance entry of element initialization failed");
58 }
59};
60
61/** destructor for class periodentafel
62 * Removes every element and afterwards deletes start and end of list.
63 * TODO: Handle when elements have changed and store databases then
64 */
65periodentafel::~periodentafel()
66{
67 CleanupPeriodtable();
68};
69
70/** Adds element to period table list
71 * \param *pointer element to be added
72 * \return iterator to added element
73 */
74periodentafel::iterator periodentafel::AddElement(element * const pointer)
75{
76 atomicNumber_t Z = pointer->getNumber();
77 ASSERT(!elements.count(Z), "Element is already present.");
78 pointer->sort = &pointer->Z;
79 if (pointer->getNumber() < 1 && pointer->getNumber() >= MAX_ELEMENTS)
80 DoeLog(0) && (eLog() << Verbose(0) << "Invalid Z number!\n");
81 pair<iterator,bool> res = elements.insert(pair<atomicNumber_t,element*>(Z,pointer));
82 return res.first;
83};
84
85/** Removes element from list.
86 * \param *pointer element to be removed
87 */
88size_t periodentafel::RemoveElement(element * const pointer)
89{
90 return RemoveElement(pointer->getNumber());
91};
92
93/** Removes element from list.
94 * \param Z element to be removed
95 */
96size_t periodentafel::RemoveElement(atomicNumber_t Z)
97{
98 return elements.erase(Z);
99};
100
101/** Removes every element from the period table.
102 */
103void periodentafel::CleanupPeriodtable()
104{
105 for(iterator iter=elements.begin();iter!=elements.end();++iter){
106 delete(*iter).second;
107 }
108 elements.clear();
109};
110
111/** Finds an element by its atomic number.
112 * If element is not yet in list, returns NULL.
113 * \param Z atomic number
114 * \return pointer to element or NULL if not found
115 */
116element * const periodentafel::FindElement(atomicNumber_t Z) const
117{
118 const_iterator res = elements.find(Z);
119 return res!=elements.end()?((*res).second):0;
120};
121
122/** Finds an element by its atomic number.
123 * If element is not yet in list, datas are asked and stored in database.
124 * \param shorthand chemical symbol of the element, e.g. H for hydrogene
125 * \return pointer to element
126 */
127element * const periodentafel::FindElement(const char * const shorthand) const
128{
129 element *res = 0;
130 for(const_iterator iter=elements.begin();iter!=elements.end();++iter) {
131 if((*iter).second->getSymbol() == shorthand){
132 res = (*iter).second;
133 break;
134 }
135 }
136 return res;
137};
138
139/** Asks for element number and returns pointer to element
140 * \return desired element or NULL
141 */
142element * const periodentafel::AskElement() const
143{
144 element * walker = NULL;
145 int Z;
146 do {
147 DoLog(0) && (Log() << Verbose(0) << "Atomic number Z: ");
148 cin >> Z;
149 walker = this->FindElement(Z); // give type
150 } while (walker == NULL);
151 return walker;
152};
153
154/** Asks for element and if not found, presents mask to enter info.
155 * \return pointer to either present or newly created element
156 */
157element * const periodentafel::EnterElement()
158{
159 atomicNumber_t Z = 0;
160 DoLog(0) && (Log() << Verbose(0) << "Atomic number: " << Z << endl);
161 cin >> Z;
162 element * const res = FindElement(Z);
163 if (!res) {
164 // TODO: make this using the constructor
165 DoLog(0) && (Log() << Verbose(0) << "Element not found in database, please enter." << endl);
166 element *tmp = new element;
167 tmp->Z = Z;
168 DoLog(0) && (Log() << Verbose(0) << "Mass: " << endl);
169 cin >> tmp->mass;
170 DoLog(0) && (Log() << Verbose(0) << "Name [max 64 chars]: " << endl);
171 cin >> tmp->name;
172 DoLog(0) && (Log() << Verbose(0) << "Short form [max 3 chars]: " << endl);
173 cin >> tmp->symbol;
174 AddElement(tmp);
175 return tmp;
176 }
177 return res;
178};
179
180
181/******************** Access to iterators ****************************/
182periodentafel::const_iterator periodentafel::begin(){
183 return elements.begin();
184}
185
186periodentafel::const_iterator periodentafel::end(){
187 return elements.end();
188}
189
190periodentafel::reverse_iterator periodentafel::rbegin(){
191 return reverse_iterator(elements.end());
192}
193
194periodentafel::reverse_iterator periodentafel::rend(){
195 return reverse_iterator(elements.begin());
196}
197
198/** Prints period table to given stream.
199 * \param output stream
200 */
201bool periodentafel::Output(ostream * const output) const
202{
203 bool result = true;
204 if (output != NULL) {
205 for(const_iterator iter=elements.begin(); iter !=elements.end();++iter){
206 result = result && (*iter).second->Output(output);
207 }
208 return result;
209 } else
210 return false;
211};
212
213/** Prints period table to given stream.
214 * \param *output output stream
215 * \param *checkliste elements table for this molecule
216 */
217bool periodentafel::Checkout(ostream * const output, const int * const checkliste) const
218{
219 bool result = true;
220 int No = 1;
221
222 if (output != NULL) {
223 *output << "# Ion type data (PP = PseudoPotential, Z = atomic number)" << endl;
224 *output << "#Ion_TypeNr.\tAmount\tZ\tRGauss\tL_Max(PP)L_Loc(PP)IonMass\t# chemical name, symbol" << endl;
225 for(const_iterator iter=elements.begin(); iter!=elements.end();++iter){
226 if (((*iter).first < MAX_ELEMENTS) && (checkliste[(*iter).first])) {
227 (*iter).second->No = No;
228 result = result && (*iter).second->Checkout(output, No++, checkliste[(*iter).first]);
229 }
230 }
231 return result;
232 } else
233 return false;
234};
235
236/** Loads element list from file.
237 * \param *path to to standard file names
238 */
239bool periodentafel::LoadPeriodentafel(const char *path)
240{
241 ifstream input;
242 bool status = true;
243 bool otherstatus = true;
244 char filename[255];
245
246 // fill elements DB
247 strncpy(filename, path, MAXSTRINGSIZE);
248 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
249 strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
250 input.open(filename);
251 if (!input.fail())
252 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as elements database." << endl);
253 status = status && LoadElementsDatabase(&input);
254 input.close();
255 input.clear();
256
257 // fill valence DB per element
258 strncpy(filename, path, MAXSTRINGSIZE);
259 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
260 strncat(filename, STANDARDVALENCEDB, MAXSTRINGSIZE-strlen(filename));
261 input.open(filename);
262 if (!input.fail())
263 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as valence database." << endl);
264 otherstatus = otherstatus && LoadValenceDatabase(&input);
265 input.close();
266 input.clear();
267
268 // fill orbitals DB per element
269 strncpy(filename, path, MAXSTRINGSIZE);
270 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
271 strncat(filename, STANDARDORBITALDB, MAXSTRINGSIZE-strlen(filename));
272 input.open(filename);
273 if (!input.fail())
274 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as orbitals database." << endl);
275 otherstatus = otherstatus && LoadOrbitalsDatabase(&input);
276 input.close();
277 input.clear();
278
279 // fill H-BondAngle DB per element
280 strncpy(filename, path, MAXSTRINGSIZE);
281 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
282 strncat(filename, STANDARDHBONDANGLEDB, MAXSTRINGSIZE-strlen(filename));
283 input.open(filename);
284 if (!input.fail())
285 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as H bond angle database." << endl);
286 otherstatus = otherstatus && LoadHBondAngleDatabase(&input);
287 input.close();
288 input.clear();
289
290 // fill H-BondDistance DB per element
291 strncpy(filename, path, MAXSTRINGSIZE);
292 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
293 strncat(filename, STANDARDHBONDDISTANCEDB, MAXSTRINGSIZE-strlen(filename));
294 input.open(filename);
295 if (!input.fail())
296 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as H bond length database." << endl);
297 otherstatus = otherstatus && LoadHBondLengthsDatabase(&input);
298 input.close();
299 input.clear();
300
301 if (!otherstatus){
302 DoeLog(2) && (eLog()<< Verbose(2) << "Something went wrong while parsing the other databases!" << endl);
303 }
304
305 return status;
306};
307
308/** load the element info.
309 * \param *input stream to parse from
310 * \return true - parsing successful, false - something went wrong
311 */
312bool periodentafel::LoadElementsDatabase(istream *input)
313{
314 bool status = true;
315 int counter = 0;
316 pair< std::map<atomicNumber_t,element*>::iterator, bool > InserterTest;
317 if (!(*input).fail()) {
318 (*input).getline(header1, MAXSTRINGSIZE);
319 (*input).getline(header2, MAXSTRINGSIZE); // skip first two header lines
320 DoLog(0) && (Log() << Verbose(0) << "Parsed elements:");
321 while (!(*input).eof()) {
322 element *neues = new element;
323 (*input) >> neues->name;
324 //(*input) >> ws;
325 (*input) >> neues->symbol;
326 //(*input) >> ws;
327 (*input) >> neues->period;
328 //(*input) >> ws;
329 (*input) >> neues->group;
330 //(*input) >> ws;
331 (*input) >> neues->block;
332 //(*input) >> ws;
333 (*input) >> neues->Z;
334 //(*input) >> ws;
335 (*input) >> neues->mass;
336 //(*input) >> ws;
337 (*input) >> neues->CovalentRadius;
338 //(*input) >> ws;
339 (*input) >> neues->VanDerWaalsRadius;
340 //(*input) >> ws;
341 (*input) >> ws;
342 //neues->Output((ofstream *)&cout);
343 if ((neues->getNumber() > 0) && (neues->getNumber() < MAX_ELEMENTS)) {
344 if (elements.count(neues->getNumber())) {// if element already present, remove and delete old one (i.e. replace it)
345 //cout << neues->symbol << " is present already." << endl;
346 element * const Elemental = FindElement(neues->getNumber());
347 ASSERT(Elemental != NULL, "element should be present but is not??");
348 *Elemental = *neues;
349 delete(neues);
350 neues = Elemental;
351 } else {
352 InserterTest = elements.insert(pair <atomicNumber_t,element*> (neues->getNumber(), neues));
353 ASSERT(InserterTest.second, "Could not insert new element into periodentafel on LoadElementsDatabase().");
354 }
355 DoLog(0) && (Log() << Verbose(0) << " " << elements[neues->getNumber()]->symbol);
356 counter++;
357 } else {
358 DoeLog(2) && (eLog() << Verbose(2) << "Detected empty line or invalid element in elements db, discarding." << endl);
359 DoLog(0) && (Log() << Verbose(0) << " <?>");
360 delete(neues);
361 }
362 }
363 DoLog(0) && (Log() << Verbose(0) << endl);
364 } else {
365 DoeLog(1) && (eLog() << Verbose(1) << "Could not open the database." << endl);
366 status = false;
367 }
368
369 if (counter == 0)
370 status = false;
371
372 return status;
373}
374
375/** load the valence info.
376 * \param *input stream to parse from
377 * \return true - parsing successful, false - something went wrong
378 */
379bool periodentafel::LoadValenceDatabase(istream *input)
380{
381 char dummy[MAXSTRINGSIZE];
382 if (!(*input).fail()) {
383 (*input).getline(dummy, MAXSTRINGSIZE);
384 while (!(*input).eof()) {
385 atomicNumber_t Z;
386 (*input) >> Z;
387 ASSERT(elements.count(Z), "Element not present");
388 (*input) >> ws;
389 (*input) >> elements[Z]->Valence;
390 (*input) >> ws;
391 //Log() << Verbose(3) << "Element " << Z << " has " << FindElement(Z)->Valence << " valence electrons." << endl;
392 }
393 return true;
394 } else
395 return false;
396}
397
398/** load the orbitals info.
399 * \param *input stream to parse from
400 * \return true - parsing successful, false - something went wrong
401 */
402bool periodentafel::LoadOrbitalsDatabase(istream *input)
403{
404 char dummy[MAXSTRINGSIZE];
405 if (!(*input).fail()) {
406 (*input).getline(dummy, MAXSTRINGSIZE);
407 while (!(*input).eof()) {
408 atomicNumber_t Z;
409 (*input) >> Z;
410 ASSERT(elements.count(Z), "Element not present");
411 (*input) >> ws;
412 (*input) >> elements[Z]->NoValenceOrbitals;
413 (*input) >> ws;
414 //Log() << Verbose(3) << "Element " << Z << " has " << FindElement(Z)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
415 }
416 return true;
417 } else
418 return false;
419}
420
421/** load the hbond angles info.
422 * \param *input stream to parse from
423 * \return true - parsing successful, false - something went wrong
424 */
425bool periodentafel::LoadHBondAngleDatabase(istream *input)
426{
427 char dummy[MAXSTRINGSIZE];
428 if (!(*input).fail()) {
429 (*input).getline(dummy, MAXSTRINGSIZE);
430 while (!(*input).eof()) {
431 atomicNumber_t Z;
432 (*input) >> Z;
433 ASSERT(elements.count(Z), "Element not present");
434 (*input) >> ws;
435 (*input) >> elements[Z]->HBondAngle[0];
436 (*input) >> elements[Z]->HBondAngle[1];
437 (*input) >> elements[Z]->HBondAngle[2];
438 (*input) >> ws;
439 //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondAngle[0] << ", " << FindElement((int)tmp)->HBondAngle[1] << ", " << FindElement((int)tmp)->HBondAngle[2] << " degrees bond angle for one, two, three connected hydrogens." << endl;
440 }
441 return true;
442 } else
443 return false;
444}
445
446/** load the hbond lengths info.
447 * \param *input stream to parse from
448 * \return true - parsing successful, false - something went wrong
449 */
450bool periodentafel::LoadHBondLengthsDatabase(istream *input)
451{
452 char dummy[MAXSTRINGSIZE];
453 if (!(*input).fail()) {
454 (*input).getline(dummy, MAXSTRINGSIZE);
455 while (!(*input).eof()) {
456 atomicNumber_t Z;
457 (*input) >> Z;
458 ASSERT(elements.count(Z), "Element not present");
459 (*input) >> ws;
460 (*input) >> elements[Z]->HBondDistance[0];
461 (*input) >> elements[Z]->HBondDistance[1];
462 (*input) >> elements[Z]->HBondDistance[2];
463 (*input) >> ws;
464 //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen." << endl;
465 }
466 return true;
467 } else
468 return false;
469}
470
471/** Stores element list to file.
472 */
473bool periodentafel::StorePeriodentafel(const char *path) const
474{
475 bool result = true;
476 ofstream f;
477 char filename[MAXSTRINGSIZE];
478
479 strncpy(filename, path, MAXSTRINGSIZE);
480 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
481 strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
482 f.open(filename);
483 if (f != NULL) {
484 f << header1 << endl;
485 f << header2 << endl;
486 for(const_iterator iter=elements.begin();iter!=elements.end();++iter){
487 result = result && (*iter).second->Output(&f);
488 }
489 f.close();
490 return true;
491 } else
492 return result;
493};
Note: See TracBrowser for help on using the repository browser.