source: src/periodentafel.cpp@ 7e3fc94

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 7e3fc94 was 7e3fc94, checked in by Tillmann Crueger <crueger@…>, 14 years ago

Made the element class use strings to store name and symbol

  • Property mode set to 100755
File size: 14.4 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 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(element * const 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 */
115element * const 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 */
126element * const 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 */
141element * const periodentafel::AskElement() const
142{
143 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 */
156element * const periodentafel::EnterElement()
157{
158 atomicNumber_t Z = 0;
159 DoLog(0) && (Log() << Verbose(0) << "Atomic number: " << Z << endl);
160 cin >> Z;
161 element * const 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(){
182 return elements.begin();
183}
184
185periodentafel::const_iterator periodentafel::end(){
186 return elements.end();
187}
188
189periodentafel::reverse_iterator periodentafel::rbegin(){
190 return reverse_iterator(elements.end());
191}
192
193periodentafel::reverse_iterator periodentafel::rend(){
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 int counter = 0;
292 pair< std::map<atomicNumber_t,element*>::iterator, bool > InserterTest;
293 if (!(*input).fail()) {
294 (*input).getline(header1, MAXSTRINGSIZE);
295 (*input).getline(header2, MAXSTRINGSIZE); // skip first two header lines
296 DoLog(0) && (Log() << Verbose(0) << "Parsed elements:");
297 while (!(*input).eof()) {
298 element *neues = new element;
299 (*input) >> neues->getName();;
300 //(*input) >> ws;
301 (*input) >> neues->getSymbol();
302 //(*input) >> ws;
303 (*input) >> neues->period;
304 //(*input) >> ws;
305 (*input) >> neues->group;
306 //(*input) >> ws;
307 (*input) >> neues->block;
308 //(*input) >> ws;
309 (*input) >> neues->Z;
310 //(*input) >> ws;
311 (*input) >> neues->mass;
312 //(*input) >> ws;
313 (*input) >> neues->CovalentRadius;
314 //(*input) >> ws;
315 (*input) >> neues->VanDerWaalsRadius;
316 //(*input) >> ws;
317 (*input) >> ws;
318 //neues->Output((ofstream *)&cout);
319 if ((neues->getNumber() > 0) && (neues->getNumber() < MAX_ELEMENTS)) {
320 if (elements.count(neues->getNumber())) {// if element already present, remove and delete old one (i.e. replace it)
321 //cout << neues->symbol << " is present already." << endl;
322 element * const Elemental = FindElement(neues->getNumber());
323 ASSERT(Elemental != NULL, "element should be present but is not??");
324 *Elemental = *neues;
325 delete(neues);
326 neues = Elemental;
327 } else {
328 InserterTest = elements.insert(pair <atomicNumber_t,element*> (neues->getNumber(), neues));
329 ASSERT(InserterTest.second, "Could not insert new element into periodentafel on LoadElementsDatabase().");
330 }
331 DoLog(0) && (Log() << Verbose(0) << " " << *neues);
332 counter++;
333 } else {
334 DoeLog(2) && (eLog() << Verbose(2) << "Detected empty line or invalid element in elements db, discarding." << endl);
335 DoLog(0) && (Log() << Verbose(0) << " <?>");
336 delete(neues);
337 }
338 }
339 DoLog(0) && (Log() << Verbose(0) << endl);
340 } else {
341 DoeLog(1) && (eLog() << Verbose(1) << "Could not open the database." << endl);
342 status = false;
343 }
344
345 if (counter == 0)
346 status = false;
347
348 return status;
349}
350
351/** load the valence info.
352 * \param *input stream to parse from
353 * \return true - parsing successful, false - something went wrong
354 */
355bool periodentafel::LoadValenceDatabase(istream *input)
356{
357 char dummy[MAXSTRINGSIZE];
358 if (!(*input).fail()) {
359 (*input).getline(dummy, MAXSTRINGSIZE);
360 while (!(*input).eof()) {
361 atomicNumber_t Z;
362 (*input) >> Z;
363 ASSERT(elements.count(Z), "Element not present");
364 (*input) >> ws;
365 (*input) >> elements[Z]->Valence;
366 (*input) >> ws;
367 //Log() << Verbose(3) << "Element " << Z << " has " << FindElement(Z)->Valence << " valence electrons." << endl;
368 }
369 return true;
370 } else
371 return false;
372}
373
374/** load the orbitals info.
375 * \param *input stream to parse from
376 * \return true - parsing successful, false - something went wrong
377 */
378bool periodentafel::LoadOrbitalsDatabase(istream *input)
379{
380 char dummy[MAXSTRINGSIZE];
381 if (!(*input).fail()) {
382 (*input).getline(dummy, MAXSTRINGSIZE);
383 while (!(*input).eof()) {
384 atomicNumber_t Z;
385 (*input) >> Z;
386 ASSERT(elements.count(Z), "Element not present");
387 (*input) >> ws;
388 (*input) >> elements[Z]->NoValenceOrbitals;
389 (*input) >> ws;
390 //Log() << Verbose(3) << "Element " << Z << " has " << FindElement(Z)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
391 }
392 return true;
393 } else
394 return false;
395}
396
397/** load the hbond angles info.
398 * \param *input stream to parse from
399 * \return true - parsing successful, false - something went wrong
400 */
401bool periodentafel::LoadHBondAngleDatabase(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]->HBondAngle[0];
412 (*input) >> elements[Z]->HBondAngle[1];
413 (*input) >> elements[Z]->HBondAngle[2];
414 (*input) >> ws;
415 //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;
416 }
417 return true;
418 } else
419 return false;
420}
421
422/** load the hbond lengths info.
423 * \param *input stream to parse from
424 * \return true - parsing successful, false - something went wrong
425 */
426bool periodentafel::LoadHBondLengthsDatabase(istream *input)
427{
428 char dummy[MAXSTRINGSIZE];
429 if (!(*input).fail()) {
430 (*input).getline(dummy, MAXSTRINGSIZE);
431 while (!(*input).eof()) {
432 atomicNumber_t Z;
433 (*input) >> Z;
434 ASSERT(elements.count(Z), "Element not present");
435 (*input) >> ws;
436 (*input) >> elements[Z]->HBondDistance[0];
437 (*input) >> elements[Z]->HBondDistance[1];
438 (*input) >> elements[Z]->HBondDistance[2];
439 (*input) >> ws;
440 //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen." << endl;
441 }
442 return true;
443 } else
444 return false;
445}
446
447/** Stores element list to file.
448 */
449bool periodentafel::StorePeriodentafel(const char *path) const
450{
451 bool result = true;
452 ofstream f;
453 char filename[MAXSTRINGSIZE];
454
455 strncpy(filename, path, MAXSTRINGSIZE);
456 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
457 strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
458 f.open(filename);
459 if (f != NULL) {
460 f << header1 << endl;
461 f << header2 << endl;
462 for(const_iterator iter=elements.begin();iter!=elements.end();++iter){
463 result = result && (*iter).second->Output(&f);
464 }
465 f.close();
466 return true;
467 } else
468 return result;
469};
Note: See TracBrowser for help on using the repository browser.