source: src/periodentafel.cpp@ 06f8ee

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 06f8ee was ff73a2, checked in by Frederik Heber <heber@…>, 15 years ago

Refined check for case '-e' of testsuite and some found bugfixes of periodentafel loading.

  • LoadPeriodentafel() gives always a properly allocated istream, hence checking for NULL in all Load..Database() is nonsense, checking for fail() is needed.
  • LoadElementsDatabase()
    • counts the number of parsed elements (returns false when zero)
    • discards empty lines (elements that dont' make sense)
  • Property mode set to 100755
File size: 13.7 KB
RevLine 
[6ac7ee]1/** \file periodentafel.cpp
2 *
3 * Function implementations for the class periodentafel.
4 *
5 */
6
7using namespace std;
8
[cd4ccc]9#include <iomanip>
[4eb4fe]10#include <iostream>
[cd4ccc]11#include <fstream>
[49e1ae]12#include <cstring>
[cd4ccc]13
[4eb4fe]14#include "Helpers/Assert.hpp"
[f66195]15#include "element.hpp"
[4eb4fe]16#include "elements_db.hpp"
[cd4ccc]17#include "helpers.hpp"
[f66195]18#include "lists.hpp"
[e138de]19#include "log.hpp"
[6ac7ee]20#include "periodentafel.hpp"
[cd4ccc]21#include "verbose.hpp"
[6ac7ee]22
[ead4e6]23using namespace std;
24
[6ac7ee]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 */
[ead4e6]30periodentafel::periodentafel()
[4eb4fe]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};
[6ac7ee]38
39/** destructor for class periodentafel
40 * Removes every element and afterwards deletes start and end of list.
41 */
42periodentafel::~periodentafel()
43{
[042f82]44 CleanupPeriodtable();
[6ac7ee]45};
46
47/** Adds element to period table list
48 * \param *pointer element to be added
[4eb4fe]49 * \return iterator to added element
[6ac7ee]50 */
[ead4e6]51periodentafel::iterator periodentafel::AddElement(element * const pointer)
[6ac7ee]52{
[ead4e6]53 atomicNumber_t Z = pointer->getNumber();
[4eb4fe]54 ASSERT(!elements.count(Z), "Element is already present.");
[042f82]55 pointer->sort = &pointer->Z;
[ead4e6]56 if (pointer->getNumber() < 1 && pointer->getNumber() >= MAX_ELEMENTS)
[5f612ee]57 DoeLog(0) && (eLog() << Verbose(0) << "Invalid Z number!\n");
[ead4e6]58 pair<iterator,bool> res = elements.insert(pair<atomicNumber_t,element*>(Z,pointer));
59 return res.first;
[6ac7ee]60};
61
62/** Removes element from list.
63 * \param *pointer element to be removed
64 */
[ead4e6]65void periodentafel::RemoveElement(element * const pointer)
[6ac7ee]66{
[4eb4fe]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{
[ead4e6]75 elements.erase(Z);
[6ac7ee]76};
77
78/** Removes every element from the period table.
79 */
[ead4e6]80void periodentafel::CleanupPeriodtable()
[6ac7ee]81{
[745a85]82 for(iterator iter=elements.begin();iter!=elements.end();++iter){
83 delete(*iter).second;
84 }
[ead4e6]85 elements.clear();
[6ac7ee]86};
87
88/** Finds an element by its atomic number.
[fb73b8]89 * If element is not yet in list, returns NULL.
[6ac7ee]90 * \param Z atomic number
[fb73b8]91 * \return pointer to element or NULL if not found
[6ac7ee]92 */
[4eb4fe]93element * const periodentafel::FindElement(atomicNumber_t Z) const
[6ac7ee]94{
[ead4e6]95 const_iterator res = elements.find(Z);
96 return res!=elements.end()?((*res).second):0;
[6ac7ee]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 */
[4eb4fe]104element * const periodentafel::FindElement(const char * const shorthand) const
[6ac7ee]105{
[ead4e6]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 }
[042f82]112 }
[ead4e6]113 return res;
[6ac7ee]114};
115
116/** Asks for element number and returns pointer to element
[4eb4fe]117 * \return desired element or NULL
[6ac7ee]118 */
[4eb4fe]119element * const periodentafel::AskElement() const
[6ac7ee]120{
[4eb4fe]121 element * walker = NULL;
[042f82]122 int Z;
123 do {
[a67d19]124 DoLog(0) && (Log() << Verbose(0) << "Atomic number Z: ");
[042f82]125 cin >> Z;
126 walker = this->FindElement(Z); // give type
127 } while (walker == NULL);
128 return walker;
[6ac7ee]129};
130
[fb73b8]131/** Asks for element and if not found, presents mask to enter info.
132 * \return pointer to either present or newly created element
133 */
[4eb4fe]134element * const periodentafel::EnterElement()
[fb73b8]135{
[ead4e6]136 atomicNumber_t Z = 0;
[a67d19]137 DoLog(0) && (Log() << Verbose(0) << "Atomic number: " << Z << endl);
[fb73b8]138 cin >> Z;
[4eb4fe]139 element * const res = FindElement(Z);
[ead4e6]140 if (!res) {
141 // TODO: make this using the constructor
[a67d19]142 DoLog(0) && (Log() << Verbose(0) << "Element not found in database, please enter." << endl);
[4eb4fe]143 element *tmp = new element;
[ead4e6]144 tmp->Z = Z;
[a67d19]145 DoLog(0) && (Log() << Verbose(0) << "Mass: " << endl);
[ead4e6]146 cin >> tmp->mass;
[a67d19]147 DoLog(0) && (Log() << Verbose(0) << "Name [max 64 chars]: " << endl);
[ead4e6]148 cin >> tmp->name;
[a67d19]149 DoLog(0) && (Log() << Verbose(0) << "Short form [max 3 chars]: " << endl);
[ead4e6]150 cin >> tmp->symbol;
151 AddElement(tmp);
[4eb4fe]152 return tmp;
[fb73b8]153 }
[ead4e6]154 return res;
[fb73b8]155};
156
[ead4e6]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
[6ac7ee]175/** Prints period table to given stream.
176 * \param output stream
177 */
[ead4e6]178bool periodentafel::Output(ostream * const output) const
[6ac7ee]179{
[042f82]180 bool result = true;
181 if (output != NULL) {
[ead4e6]182 for(const_iterator iter=elements.begin(); iter !=elements.end();++iter){
183 result = result && (*iter).second->Output(output);
[042f82]184 }
185 return result;
186 } else
187 return false;
[6ac7ee]188};
189
190/** Prints period table to given stream.
191 * \param *output output stream
192 * \param *checkliste elements table for this molecule
193 */
[ead4e6]194bool periodentafel::Checkout(ostream * const output, const int * const checkliste) const
[6ac7ee]195{
[042f82]196 bool result = true;
197 int No = 1;
[6ac7ee]198
[042f82]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;
[ead4e6]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]);
[042f82]206 }
207 }
208 return result;
209 } else
210 return false;
[6ac7ee]211};
212
213/** Loads element list from file.
214 * \param *path to to standard file names
215 */
[989bf6]216bool periodentafel::LoadPeriodentafel(const char *path)
[6ac7ee]217{
[4eb4fe]218 ifstream input;
[042f82]219 bool status = true;
220 bool otherstatus = true;
221 char filename[255];
[6ac7ee]222
[042f82]223 // fill elements DB
224 strncpy(filename, path, MAXSTRINGSIZE);
225 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
226 strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
[4eb4fe]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{
[ff73a2]271 bool status = true;
272 int counter = 0;
273 if (!(*input).fail()) {
[4eb4fe]274 (*input).getline(header1, MAXSTRINGSIZE);
275 (*input).getline(header2, MAXSTRINGSIZE); // skip first two header lines
[a67d19]276 DoLog(0) && (Log() << Verbose(0) << "Parsed elements:");
[4eb4fe]277 while (!(*input).eof()) {
[042f82]278 element *neues = new element;
[4eb4fe]279 (*input) >> neues->name;
280 //(*input) >> ws;
281 (*input) >> neues->symbol;
282 //(*input) >> ws;
283 (*input) >> neues->period;
284 //(*input) >> ws;
285 (*input) >> neues->group;
286 //(*input) >> ws;
287 (*input) >> neues->block;
288 //(*input) >> ws;
289 (*input) >> neues->Z;
290 //(*input) >> ws;
291 (*input) >> neues->mass;
292 //(*input) >> ws;
293 (*input) >> neues->CovalentRadius;
294 //(*input) >> ws;
295 (*input) >> neues->VanDerWaalsRadius;
296 //(*input) >> ws;
297 (*input) >> ws;
298 if (elements.count(neues->Z)) {// if element already present, remove and delete it
299 element * const Elemental = FindElement(neues->Z);
300 ASSERT(Elemental != NULL, "element should be present but is not??");
301 RemoveElement(Elemental);
302 delete(Elemental);
303 }
[042f82]304 //neues->Output((ofstream *)&cout);
[ff73a2]305 if ((neues->Z > 0) && (neues->Z < MAX_ELEMENTS)) {
306 DoLog(0) && (Log() << Verbose(0) << " " << neues->symbol);
[4eb4fe]307 elements[neues->getNumber()] = neues;
[ff73a2]308 counter++;
309 } else {
310 DoeLog(2) && (eLog() << Verbose(2) << "Detected empty line or invalid element in elements db, discarding." << endl);
311 DoLog(0) && (Log() << Verbose(0) << " <?>");
[db6bf74]312 delete(neues);
[042f82]313 }
314 }
[a67d19]315 DoLog(0) && (Log() << Verbose(0) << endl);
[042f82]316 } else
[ff73a2]317 status = false;
318
319 if (counter == 0)
320 status = false;
321
322 return status;
[4eb4fe]323}
[6ac7ee]324
[4eb4fe]325/** load the valence info.
326 * \param *input stream to parse from
327 * \return true - parsing successful, false - something went wrong
328 */
329bool periodentafel::LoadValenceDatabase(istream *input)
330{
331 char dummy[MAXSTRINGSIZE];
[ff73a2]332 if (!(*input).fail()) {
[4eb4fe]333 (*input).getline(dummy, MAXSTRINGSIZE);
334 while (!(*input).eof()) {
[ead4e6]335 atomicNumber_t Z;
[4eb4fe]336 (*input) >> Z;
337 ASSERT(elements.count(Z), "Element not present");
338 (*input) >> ws;
339 (*input) >> elements[Z]->Valence;
340 (*input) >> ws;
[e138de]341 //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->Valence << " valence electrons." << endl;
[042f82]342 }
[4eb4fe]343 return true;
[042f82]344 } else
[4eb4fe]345 return false;
346}
[6ac7ee]347
[4eb4fe]348/** load the orbitals info.
349 * \param *input stream to parse from
350 * \return true - parsing successful, false - something went wrong
351 */
352bool periodentafel::LoadOrbitalsDatabase(istream *input)
353{
354 char dummy[MAXSTRINGSIZE];
[ff73a2]355 if (!(*input).fail()) {
[4eb4fe]356 (*input).getline(dummy, MAXSTRINGSIZE);
357 while (!(*input).eof()) {
[ead4e6]358 atomicNumber_t Z;
[4eb4fe]359 (*input) >> Z;
360 ASSERT(elements.count(Z), "Element not present");
361 (*input) >> ws;
362 (*input) >> elements[Z]->NoValenceOrbitals;
363 (*input) >> ws;
[e138de]364 //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
[042f82]365 }
[4eb4fe]366 return true;
[042f82]367 } else
[4eb4fe]368 return false;
369}
[6ac7ee]370
[4eb4fe]371/** load the hbond angles info.
372 * \param *input stream to parse from
373 * \return true - parsing successful, false - something went wrong
374 */
375bool periodentafel::LoadHBondAngleDatabase(istream *input)
376{
377 char dummy[MAXSTRINGSIZE];
[ff73a2]378 if (!(*input).fail()) {
[4eb4fe]379 (*input).getline(dummy, MAXSTRINGSIZE);
380 while (!(*input).eof()) {
[ead4e6]381 atomicNumber_t Z;
[4eb4fe]382 (*input) >> Z;
383 ASSERT(elements.count(Z), "Element not present");
384 (*input) >> ws;
385 (*input) >> elements[Z]->HBondAngle[0];
386 (*input) >> elements[Z]->HBondAngle[1];
387 (*input) >> elements[Z]->HBondAngle[2];
388 (*input) >> ws;
389 //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;
[042f82]390 }
[4eb4fe]391 return true;
[042f82]392 } else
[4eb4fe]393 return false;
394}
[6ac7ee]395
[4eb4fe]396/** load the hbond lengths info.
397 * \param *input stream to parse from
398 * \return true - parsing successful, false - something went wrong
399 */
400bool periodentafel::LoadHBondLengthsDatabase(istream *input)
401{
402 char dummy[MAXSTRINGSIZE];
[ff73a2]403 if (!(*input).fail()) {
[4eb4fe]404 (*input).getline(dummy, MAXSTRINGSIZE);
405 while (!(*input).eof()) {
[ead4e6]406 atomicNumber_t Z;
[4eb4fe]407 (*input) >> Z;
408 ASSERT(elements.count(Z), "Element not present");
409 (*input) >> ws;
410 (*input) >> elements[Z]->HBondDistance[0];
411 (*input) >> elements[Z]->HBondDistance[1];
412 (*input) >> elements[Z]->HBondDistance[2];
413 (*input) >> ws;
414 //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen." << endl;
[042f82]415 }
[4eb4fe]416 return true;
[042f82]417 } else
[4eb4fe]418 return false;
419}
[6ac7ee]420
421/** Stores element list to file.
422 */
[989bf6]423bool periodentafel::StorePeriodentafel(const char *path) const
[6ac7ee]424{
[042f82]425 bool result = true;
426 ofstream f;
427 char filename[MAXSTRINGSIZE];
[6ac7ee]428
[042f82]429 strncpy(filename, path, MAXSTRINGSIZE);
430 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
431 strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
432 f.open(filename);
433 if (f != NULL) {
434 f << header1 << endl;
435 f << header2 << endl;
[ead4e6]436 for(const_iterator iter=elements.begin();iter!=elements.end();++iter){
437 result = result && (*iter).second->Output(&f);
[042f82]438 }
439 f.close();
[4eb4fe]440 return true;
[042f82]441 } else
[4eb4fe]442 return result;
[6ac7ee]443};
Note: See TracBrowser for help on using the repository browser.