source: src/periodentafel.cpp@ 6cfa36

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 6cfa36 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
RevLine 
[6ac7ee]1/** \file periodentafel.cpp
2 *
3 * Function implementations for the class periodentafel.
4 *
5 */
6
[112b09]7#include "Helpers/MemDebug.hpp"
8
[6ac7ee]9using namespace std;
10
[cd4ccc]11#include <iomanip>
[4eb4fe]12#include <iostream>
[cd4ccc]13#include <fstream>
[49e1ae]14#include <cstring>
[cd4ccc]15
[4eb4fe]16#include "Helpers/Assert.hpp"
[f66195]17#include "element.hpp"
[4eb4fe]18#include "elements_db.hpp"
[cd4ccc]19#include "helpers.hpp"
[f66195]20#include "lists.hpp"
[e138de]21#include "log.hpp"
[6ac7ee]22#include "periodentafel.hpp"
[cd4ccc]23#include "verbose.hpp"
[6ac7ee]24
[ead4e6]25using namespace std;
26
[6ac7ee]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 */
[ead4e6]32periodentafel::periodentafel()
[4eb4fe]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};
[6ac7ee]40
41/** destructor for class periodentafel
42 * Removes every element and afterwards deletes start and end of list.
[42af9e]43 * TODO: Handle when elements have changed and store databases then
[6ac7ee]44 */
45periodentafel::~periodentafel()
46{
[042f82]47 CleanupPeriodtable();
[6ac7ee]48};
49
50/** Adds element to period table list
51 * \param *pointer element to be added
[4eb4fe]52 * \return iterator to added element
[6ac7ee]53 */
[ead4e6]54periodentafel::iterator periodentafel::AddElement(element * const pointer)
[6ac7ee]55{
[ead4e6]56 atomicNumber_t Z = pointer->getNumber();
[4eb4fe]57 ASSERT(!elements.count(Z), "Element is already present.");
[042f82]58 pointer->sort = &pointer->Z;
[ead4e6]59 if (pointer->getNumber() < 1 && pointer->getNumber() >= MAX_ELEMENTS)
[5f612ee]60 DoeLog(0) && (eLog() << Verbose(0) << "Invalid Z number!\n");
[ead4e6]61 pair<iterator,bool> res = elements.insert(pair<atomicNumber_t,element*>(Z,pointer));
62 return res.first;
[6ac7ee]63};
64
65/** Removes element from list.
66 * \param *pointer element to be removed
67 */
[ead4e6]68void periodentafel::RemoveElement(element * const pointer)
[6ac7ee]69{
[4eb4fe]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{
[ead4e6]78 elements.erase(Z);
[6ac7ee]79};
80
81/** Removes every element from the period table.
82 */
[ead4e6]83void periodentafel::CleanupPeriodtable()
[6ac7ee]84{
[745a85]85 for(iterator iter=elements.begin();iter!=elements.end();++iter){
86 delete(*iter).second;
87 }
[ead4e6]88 elements.clear();
[6ac7ee]89};
90
91/** Finds an element by its atomic number.
[fb73b8]92 * If element is not yet in list, returns NULL.
[6ac7ee]93 * \param Z atomic number
[fb73b8]94 * \return pointer to element or NULL if not found
[6ac7ee]95 */
[4eb4fe]96element * const periodentafel::FindElement(atomicNumber_t Z) const
[6ac7ee]97{
[ead4e6]98 const_iterator res = elements.find(Z);
99 return res!=elements.end()?((*res).second):0;
[6ac7ee]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 */
[4eb4fe]107element * const periodentafel::FindElement(const char * const shorthand) const
[6ac7ee]108{
[ead4e6]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 }
[042f82]115 }
[ead4e6]116 return res;
[6ac7ee]117};
118
119/** Asks for element number and returns pointer to element
[4eb4fe]120 * \return desired element or NULL
[6ac7ee]121 */
[4eb4fe]122element * const periodentafel::AskElement() const
[6ac7ee]123{
[4eb4fe]124 element * walker = NULL;
[042f82]125 int Z;
126 do {
[a67d19]127 DoLog(0) && (Log() << Verbose(0) << "Atomic number Z: ");
[042f82]128 cin >> Z;
129 walker = this->FindElement(Z); // give type
130 } while (walker == NULL);
131 return walker;
[6ac7ee]132};
133
[fb73b8]134/** Asks for element and if not found, presents mask to enter info.
135 * \return pointer to either present or newly created element
136 */
[4eb4fe]137element * const periodentafel::EnterElement()
[fb73b8]138{
[ead4e6]139 atomicNumber_t Z = 0;
[a67d19]140 DoLog(0) && (Log() << Verbose(0) << "Atomic number: " << Z << endl);
[fb73b8]141 cin >> Z;
[4eb4fe]142 element * const res = FindElement(Z);
[ead4e6]143 if (!res) {
144 // TODO: make this using the constructor
[a67d19]145 DoLog(0) && (Log() << Verbose(0) << "Element not found in database, please enter." << endl);
[4eb4fe]146 element *tmp = new element;
[ead4e6]147 tmp->Z = Z;
[a67d19]148 DoLog(0) && (Log() << Verbose(0) << "Mass: " << endl);
[ead4e6]149 cin >> tmp->mass;
[a67d19]150 DoLog(0) && (Log() << Verbose(0) << "Name [max 64 chars]: " << endl);
[ead4e6]151 cin >> tmp->name;
[a67d19]152 DoLog(0) && (Log() << Verbose(0) << "Short form [max 3 chars]: " << endl);
[ead4e6]153 cin >> tmp->symbol;
154 AddElement(tmp);
[4eb4fe]155 return tmp;
[fb73b8]156 }
[ead4e6]157 return res;
[fb73b8]158};
159
[ead4e6]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
[6ac7ee]178/** Prints period table to given stream.
179 * \param output stream
180 */
[ead4e6]181bool periodentafel::Output(ostream * const output) const
[6ac7ee]182{
[042f82]183 bool result = true;
184 if (output != NULL) {
[ead4e6]185 for(const_iterator iter=elements.begin(); iter !=elements.end();++iter){
186 result = result && (*iter).second->Output(output);
[042f82]187 }
188 return result;
189 } else
190 return false;
[6ac7ee]191};
192
193/** Prints period table to given stream.
194 * \param *output output stream
195 * \param *checkliste elements table for this molecule
196 */
[ead4e6]197bool periodentafel::Checkout(ostream * const output, const int * const checkliste) const
[6ac7ee]198{
[042f82]199 bool result = true;
200 int No = 1;
[6ac7ee]201
[042f82]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;
[ead4e6]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]);
[042f82]209 }
210 }
211 return result;
212 } else
213 return false;
[6ac7ee]214};
215
216/** Loads element list from file.
217 * \param *path to to standard file names
218 */
[989bf6]219bool periodentafel::LoadPeriodentafel(const char *path)
[6ac7ee]220{
[4eb4fe]221 ifstream input;
[042f82]222 bool status = true;
223 bool otherstatus = true;
224 char filename[255];
[6ac7ee]225
[042f82]226 // fill elements DB
227 strncpy(filename, path, MAXSTRINGSIZE);
228 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
229 strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
[4eb4fe]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{
[ff73a2]274 bool status = true;
275 int counter = 0;
276 if (!(*input).fail()) {
[4eb4fe]277 (*input).getline(header1, MAXSTRINGSIZE);
278 (*input).getline(header2, MAXSTRINGSIZE); // skip first two header lines
[a67d19]279 DoLog(0) && (Log() << Verbose(0) << "Parsed elements:");
[4eb4fe]280 while (!(*input).eof()) {
[042f82]281 element *neues = new element;
[4eb4fe]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 }
[042f82]307 //neues->Output((ofstream *)&cout);
[ff73a2]308 if ((neues->Z > 0) && (neues->Z < MAX_ELEMENTS)) {
309 DoLog(0) && (Log() << Verbose(0) << " " << neues->symbol);
[4eb4fe]310 elements[neues->getNumber()] = neues;
[ff73a2]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) << " <?>");
[db6bf74]315 delete(neues);
[042f82]316 }
317 }
[a67d19]318 DoLog(0) && (Log() << Verbose(0) << endl);
[042f82]319 } else
[ff73a2]320 status = false;
321
322 if (counter == 0)
323 status = false;
324
325 return status;
[4eb4fe]326}
[6ac7ee]327
[4eb4fe]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];
[ff73a2]335 if (!(*input).fail()) {
[4eb4fe]336 (*input).getline(dummy, MAXSTRINGSIZE);
337 while (!(*input).eof()) {
[ead4e6]338 atomicNumber_t Z;
[4eb4fe]339 (*input) >> Z;
340 ASSERT(elements.count(Z), "Element not present");
341 (*input) >> ws;
342 (*input) >> elements[Z]->Valence;
343 (*input) >> ws;
[e138de]344 //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->Valence << " valence electrons." << endl;
[042f82]345 }
[4eb4fe]346 return true;
[042f82]347 } else
[4eb4fe]348 return false;
349}
[6ac7ee]350
[4eb4fe]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];
[ff73a2]358 if (!(*input).fail()) {
[4eb4fe]359 (*input).getline(dummy, MAXSTRINGSIZE);
360 while (!(*input).eof()) {
[ead4e6]361 atomicNumber_t Z;
[4eb4fe]362 (*input) >> Z;
363 ASSERT(elements.count(Z), "Element not present");
364 (*input) >> ws;
365 (*input) >> elements[Z]->NoValenceOrbitals;
366 (*input) >> ws;
[e138de]367 //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
[042f82]368 }
[4eb4fe]369 return true;
[042f82]370 } else
[4eb4fe]371 return false;
372}
[6ac7ee]373
[4eb4fe]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];
[ff73a2]381 if (!(*input).fail()) {
[4eb4fe]382 (*input).getline(dummy, MAXSTRINGSIZE);
383 while (!(*input).eof()) {
[ead4e6]384 atomicNumber_t Z;
[4eb4fe]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;
[042f82]393 }
[4eb4fe]394 return true;
[042f82]395 } else
[4eb4fe]396 return false;
397}
[6ac7ee]398
[4eb4fe]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];
[ff73a2]406 if (!(*input).fail()) {
[4eb4fe]407 (*input).getline(dummy, MAXSTRINGSIZE);
408 while (!(*input).eof()) {
[ead4e6]409 atomicNumber_t Z;
[4eb4fe]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;
[042f82]418 }
[4eb4fe]419 return true;
[042f82]420 } else
[4eb4fe]421 return false;
422}
[6ac7ee]423
424/** Stores element list to file.
425 */
[989bf6]426bool periodentafel::StorePeriodentafel(const char *path) const
[6ac7ee]427{
[042f82]428 bool result = true;
429 ofstream f;
430 char filename[MAXSTRINGSIZE];
[6ac7ee]431
[042f82]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;
[ead4e6]439 for(const_iterator iter=elements.begin();iter!=elements.end();++iter){
440 result = result && (*iter).second->Output(&f);
[042f82]441 }
442 f.close();
[4eb4fe]443 return true;
[042f82]444 } else
[4eb4fe]445 return result;
[6ac7ee]446};
Note: See TracBrowser for help on using the repository browser.