source: src/periodentafel.cpp@ 2fe971

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 Candidate_v1.7.0 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 2fe971 was 2fe971, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Made fields name and symbol of element class private

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