source: src/periodentafel.cpp@ 88104f

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 88104f was 61745cc, checked in by Frederik Heber <heber@…>, 15 years ago

BUGFIX: LoadPeriodentafel() did not close files and elements have to be replaced in-place.

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