source: src/periodentafel.cpp@ 673c7f

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 673c7f was 4eb4fe, checked in by Frederik Heber <heber@…>, 15 years ago

"-e <db path>" not necessary anymore.

Removed necessity of specifying path to databases (this was one check of molecuilder/test/testsuite.at which cannot be fulfilled anymore with boost::program_options)
For this to work a great number of small changes have been necessary:

class periodentafel:

  • all .db files merged into const char * arrays in elements_db.cpp
  • periodentafel rewritten:
  • FindElement(), AskElement() and EnterElement return element * const instead of const element * (i.e. the contents of the pointer is const (the element) not the pointer itself which is very vexatious (i.e. FindElement() yields const element * which can subsequently not be used for RemoveElement(), ...)
  • parsedElems is not needed anymore. Instead we operate on map elements directly
  • new unittest periodentafelTest which is made friend of periodentafel to be able to access private loading functions directly

A number of unit tests had to be changed (all that create elements during setUp() which is now unnecessary)

Some of the analysis_bonds function's signatures were changed in the process:

Finally, the respective tests are removed from molecuilder/tests/testsuite.at.

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