source: src/periodentafel.cpp@ 6d6b54

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 6d6b54 was 112b09, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added #include "Helpers/MemDebug.hpp" to all .cpp files

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