source: src/periodentafel.cpp@ 15b670

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

MEMFIXES: no more default saving/loading of element's db, config::SaveTREMOLO(), molecule::CreateMappingLabelsToConfigSequence()

Signed-off-by: Frederik Heber <heber@…>

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