source: src/periodentafel.cpp@ 4e6d74

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 4e6d74 was 4e6d74, checked in by Frederik Heber <heber@…>, 14 years ago

Removed output of headers in periodentafel::LoadElementsDatabase().

  • Property mode set to 100755
File size: 15.0 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/helpers.hpp"
20#include "lists.hpp"
21#include "Helpers/Log.hpp"
22#include "periodentafel.hpp"
23#include "Helpers/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 * pointer)
75{
76 atomicNumber_t Z = pointer->getNumber();
77 ASSERT(!elements.count(Z), "Element is already present.");
78 if (pointer->getNumber() < 1 && pointer->getNumber() >= MAX_ELEMENTS)
79 DoeLog(0) && (eLog() << Verbose(0) << "Invalid Z number!\n");
80 pair<iterator,bool> res = elements.insert(pair<atomicNumber_t,element*>(Z,pointer));
81 return res.first;
82};
83
84/** Removes element from list.
85 * \param *pointer element to be removed
86 */
87size_t periodentafel::RemoveElement(const element * pointer)
88{
89 return RemoveElement(pointer->getNumber());
90};
91
92/** Removes element from list.
93 * \param Z element to be removed
94 */
95size_t periodentafel::RemoveElement(atomicNumber_t Z)
96{
97 return elements.erase(Z);
98};
99
100/** Removes every element from the period table.
101 */
102void periodentafel::CleanupPeriodtable()
103{
104 for(iterator iter=elements.begin();iter!=elements.end();++iter){
105 delete(*iter).second;
106 }
107 elements.clear();
108};
109
110/** Finds an element by its atomic number.
111 * If element is not yet in list, returns NULL.
112 * \param Z atomic number
113 * \return pointer to element or NULL if not found
114 */
115const element * periodentafel::FindElement(atomicNumber_t Z) const
116{
117 const_iterator res = elements.find(Z);
118 return res!=elements.end()?((*res).second):0;
119};
120
121/** Finds an element by its atomic number.
122 * If element is not yet in list, datas are asked and stored in database.
123 * \param shorthand chemical symbol of the element, e.g. H for hydrogene
124 * \return pointer to element
125 */
126const element * periodentafel::FindElement(const string &shorthand) const
127{
128 element *res = 0;
129 for(const_iterator iter=elements.begin();iter!=elements.end();++iter) {
130 if((*iter).second->getSymbol() == shorthand){
131 res = (*iter).second;
132 break;
133 }
134 }
135 return res;
136};
137
138/** Asks for element number and returns pointer to element
139 * \return desired element or NULL
140 */
141const element * periodentafel::AskElement() const
142{
143 const element * walker = NULL;
144 int Z;
145 do {
146 DoLog(0) && (Log() << Verbose(0) << "Atomic number Z: ");
147 cin >> Z;
148 walker = this->FindElement(Z); // give type
149 } while (walker == NULL);
150 return walker;
151};
152
153/** Asks for element and if not found, presents mask to enter info.
154 * \return pointer to either present or newly created element
155 */
156const element * periodentafel::EnterElement()
157{
158 atomicNumber_t Z = 0;
159 DoLog(0) && (Log() << Verbose(0) << "Atomic number: " << Z << endl);
160 cin >> Z;
161 const element *res = FindElement(Z);
162 if (!res) {
163 // TODO: make this using the constructor
164 DoLog(0) && (Log() << Verbose(0) << "Element not found in database, please enter." << endl);
165 element *tmp = new element;
166 tmp->Z = Z;
167 DoLog(0) && (Log() << Verbose(0) << "Mass: " << endl);
168 cin >> tmp->mass;
169 DoLog(0) && (Log() << Verbose(0) << "Name [max 64 chars]: " << endl);
170 cin >> tmp->getName();
171 DoLog(0) && (Log() << Verbose(0) << "Short form [max 3 chars]: " << endl);
172 cin >> tmp->getSymbol();
173 AddElement(tmp);
174 return tmp;
175 }
176 return res;
177};
178
179
180/******************** Access to iterators ****************************/
181periodentafel::const_iterator periodentafel::begin() const{
182 return elements.begin();
183}
184
185periodentafel::const_iterator periodentafel::end() const{
186 return elements.end();
187}
188
189periodentafel::reverse_iterator periodentafel::rbegin() const{
190 return reverse_iterator(elements.end());
191}
192
193periodentafel::reverse_iterator periodentafel::rend() const{
194 return reverse_iterator(elements.begin());
195}
196
197/** Prints period table to given stream.
198 * \param output stream
199 */
200bool periodentafel::Output(ostream * const output) const
201{
202 bool result = true;
203 if (output != NULL) {
204 for(const_iterator iter=elements.begin(); iter !=elements.end();++iter){
205 result = result && (*iter).second->Output(output);
206 }
207 return result;
208 } else
209 return false;
210};
211
212/** Loads element list from file.
213 * \param *path to to standard file names
214 */
215bool periodentafel::LoadPeriodentafel(const char *path)
216{
217 ifstream input;
218 bool status = true;
219 bool otherstatus = true;
220 char filename[255];
221
222 // fill elements DB
223 strncpy(filename, path, MAXSTRINGSIZE);
224 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
225 strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
226 input.open(filename);
227 if (!input.fail())
228 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as elements database." << endl);
229 status = status && LoadElementsDatabase(input);
230 input.close();
231 input.clear();
232
233 // fill valence DB per element
234 strncpy(filename, path, MAXSTRINGSIZE);
235 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
236 strncat(filename, STANDARDVALENCEDB, MAXSTRINGSIZE-strlen(filename));
237 input.open(filename);
238 if (!input.fail())
239 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as valence database." << endl);
240 otherstatus = otherstatus && LoadValenceDatabase(&input);
241 input.close();
242 input.clear();
243
244 // fill orbitals DB per element
245 strncpy(filename, path, MAXSTRINGSIZE);
246 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
247 strncat(filename, STANDARDORBITALDB, MAXSTRINGSIZE-strlen(filename));
248 input.open(filename);
249 if (!input.fail())
250 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as orbitals database." << endl);
251 otherstatus = otherstatus && LoadOrbitalsDatabase(&input);
252 input.close();
253 input.clear();
254
255 // fill H-BondAngle DB per element
256 strncpy(filename, path, MAXSTRINGSIZE);
257 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
258 strncat(filename, STANDARDHBONDANGLEDB, MAXSTRINGSIZE-strlen(filename));
259 input.open(filename);
260 if (!input.fail())
261 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as H bond angle database." << endl);
262 otherstatus = otherstatus && LoadHBondAngleDatabase(&input);
263 input.close();
264 input.clear();
265
266 // fill H-BondDistance DB per element
267 strncpy(filename, path, MAXSTRINGSIZE);
268 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
269 strncat(filename, STANDARDHBONDDISTANCEDB, MAXSTRINGSIZE-strlen(filename));
270 input.open(filename);
271 if (!input.fail())
272 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as H bond length database." << endl);
273 otherstatus = otherstatus && LoadHBondLengthsDatabase(&input);
274 input.close();
275 input.clear();
276
277 if (!otherstatus){
278 DoeLog(2) && (eLog()<< Verbose(2) << "Something went wrong while parsing the other databases!" << endl);
279 }
280
281 return status;
282};
283
284/** load the element info.
285 * \param *input stream to parse from
286 * \return true - parsing successful, false - something went wrong
287 */
288bool periodentafel::LoadElementsDatabase(istream &input)
289{
290 bool status = true;
291 string header1tmp,header2tmp;
292 // first parse into a map, so we can revert to old status in case something goes wront
293 map<atomicNumber_t,element*> parsedElements;
294 if (!input.fail()) {
295 getline(input,header1tmp);
296 getline(input,header2tmp); // skip first two header lines
297 //cout << "First header: " << header1tmp << endl;
298 //cout << "Second header: " << header2tmp << endl;
299 DoLog(0) && (Log() << Verbose(0) << "Parsed elements:");
300 while (!input.eof()) {
301 element *neues = new element;
302 input >> neues->getName();;
303 //(*input) >> ws;
304 input >> neues->getSymbol();
305 //(*input) >> ws;
306 input >> neues->period;
307 //(*input) >> ws;
308 input >> neues->group;
309 //(*input) >> ws;
310 input >> neues->block;
311 //(*input) >> ws;
312 input >> neues->Z;
313 //(*input) >> ws;
314 input >> neues->mass;
315 //(*input) >> ws;
316 input >> neues->CovalentRadius;
317 //(*input) >> ws;
318 input >> neues->VanDerWaalsRadius;
319 //(*input) >> ws;
320 input >> ws;
321 //neues->Output((ofstream *)&cout);
322 if ((neues->getNumber() > 0) && (neues->getNumber() < MAX_ELEMENTS)) {
323 parsedElements[neues->Z] = neues;
324 DoLog(0) && (Log() << Verbose(0) << " " << *neues);
325 } else {
326 DoeLog(2) && (eLog() << Verbose(2) << "Detected empty line or invalid element in elements db, discarding." << endl);
327 DoLog(0) && (Log() << Verbose(0) << " <?>");
328 delete(neues);
329 }
330 // when the input is in failed state, we most likely just read garbage
331 if(input.fail()) {
332 DoeLog(2) && (eLog() << Verbose(2) << "Error parsing elements db." << endl);
333 status = false;
334 break;
335 }
336 }
337 DoLog(0) && (Log() << Verbose(0) << endl);
338 } else {
339 DoeLog(1) && (eLog() << Verbose(1) << "Could not open the database." << endl);
340 status = false;
341 }
342
343 if (!parsedElements.size())
344 status = false;
345
346 if(status){
347 for(map<atomicNumber_t,element*>::iterator iter=parsedElements.begin();
348 iter!=parsedElements.end();
349 ++iter){
350 if (elements.count(iter->first)) {
351 // if element already present, replace the old one
352 // pointer to old element might still be in use, so we have to replace into the old element
353 *(elements[iter->first])=*iter->second;
354 }
355 else {
356 // no such element in periodentafel... we can just insert
357 elements[iter->first] = iter->second;
358 }
359 }
360 // all went well.. we now copy the header
361 strncpy(header1,header1tmp.c_str(),MAXSTRINGSIZE);
362 header1[MAXSTRINGSIZE-1]=0;
363 strncpy(header2,header2tmp.c_str(),MAXSTRINGSIZE);
364 header2[MAXSTRINGSIZE-1]=0;
365 }
366
367 return status;
368}
369
370/** load the valence info.
371 * \param *input stream to parse from
372 * \return true - parsing successful, false - something went wrong
373 */
374bool periodentafel::LoadValenceDatabase(istream *input)
375{
376 char dummy[MAXSTRINGSIZE];
377 if (!(*input).fail()) {
378 (*input).getline(dummy, MAXSTRINGSIZE);
379 while (!(*input).eof()) {
380 atomicNumber_t Z;
381 (*input) >> Z;
382 ASSERT(elements.count(Z), "Element not present");
383 (*input) >> ws;
384 (*input) >> elements[Z]->Valence;
385 (*input) >> ws;
386 //Log() << Verbose(3) << "Element " << Z << " has " << FindElement(Z)->Valence << " valence electrons." << endl;
387 }
388 return true;
389 } else
390 return false;
391}
392
393/** load the orbitals info.
394 * \param *input stream to parse from
395 * \return true - parsing successful, false - something went wrong
396 */
397bool periodentafel::LoadOrbitalsDatabase(istream *input)
398{
399 char dummy[MAXSTRINGSIZE];
400 if (!(*input).fail()) {
401 (*input).getline(dummy, MAXSTRINGSIZE);
402 while (!(*input).eof()) {
403 atomicNumber_t Z;
404 (*input) >> Z;
405 ASSERT(elements.count(Z), "Element not present");
406 (*input) >> ws;
407 (*input) >> elements[Z]->NoValenceOrbitals;
408 (*input) >> ws;
409 //Log() << Verbose(3) << "Element " << Z << " has " << FindElement(Z)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
410 }
411 return true;
412 } else
413 return false;
414}
415
416/** load the hbond angles info.
417 * \param *input stream to parse from
418 * \return true - parsing successful, false - something went wrong
419 */
420bool periodentafel::LoadHBondAngleDatabase(istream *input)
421{
422 char dummy[MAXSTRINGSIZE];
423 if (!(*input).fail()) {
424 (*input).getline(dummy, MAXSTRINGSIZE);
425 while (!(*input).eof()) {
426 atomicNumber_t Z;
427 (*input) >> Z;
428 ASSERT(elements.count(Z), "Element not present");
429 (*input) >> ws;
430 (*input) >> elements[Z]->HBondAngle[0];
431 (*input) >> elements[Z]->HBondAngle[1];
432 (*input) >> elements[Z]->HBondAngle[2];
433 (*input) >> ws;
434 //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;
435 }
436 return true;
437 } else
438 return false;
439}
440
441/** load the hbond lengths info.
442 * \param *input stream to parse from
443 * \return true - parsing successful, false - something went wrong
444 */
445bool periodentafel::LoadHBondLengthsDatabase(istream *input)
446{
447 char dummy[MAXSTRINGSIZE];
448 if (!(*input).fail()) {
449 (*input).getline(dummy, MAXSTRINGSIZE);
450 while (!(*input).eof()) {
451 atomicNumber_t Z;
452 (*input) >> Z;
453 ASSERT(elements.count(Z), "Element not present");
454 (*input) >> ws;
455 (*input) >> elements[Z]->HBondDistance[0];
456 (*input) >> elements[Z]->HBondDistance[1];
457 (*input) >> elements[Z]->HBondDistance[2];
458 (*input) >> ws;
459 //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen." << endl;
460 }
461 return true;
462 } else
463 return false;
464}
465
466/** Stores element list to file.
467 */
468bool periodentafel::StorePeriodentafel(const char *path) const
469{
470 bool result = true;
471 ofstream f;
472 char filename[MAXSTRINGSIZE];
473
474 strncpy(filename, path, MAXSTRINGSIZE);
475 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
476 strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
477 f.open(filename);
478 if (f != NULL) {
479 f << header1 << endl;
480 f << header2 << endl;
481 for(const_iterator iter=elements.begin();iter!=elements.end();++iter){
482 result = result && (*iter).second->Output(&f);
483 }
484 f.close();
485 return true;
486 } else
487 return result;
488};
Note: See TracBrowser for help on using the repository browser.