source: src/periodentafel.cpp@ 2a76b0

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

Made the element class use strings to store name and symbol

  • Property mode set to 100755
File size: 14.4 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);
[7e3fc94]170 cin >> tmp->getName();
[a67d19]171 DoLog(0) && (Log() << Verbose(0) << "Short form [max 3 chars]: " << endl);
[7e3fc94]172 cin >> tmp->getSymbol();
[ead4e6]173 AddElement(tmp);
[4eb4fe]174 return tmp;
[fb73b8]175 }
[ead4e6]176 return res;
[fb73b8]177};
178
[ead4e6]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
[6ac7ee]197/** Prints period table to given stream.
198 * \param output stream
199 */
[ead4e6]200bool periodentafel::Output(ostream * const output) const
[6ac7ee]201{
[042f82]202 bool result = true;
203 if (output != NULL) {
[ead4e6]204 for(const_iterator iter=elements.begin(); iter !=elements.end();++iter){
205 result = result && (*iter).second->Output(output);
[042f82]206 }
207 return result;
208 } else
209 return false;
[6ac7ee]210};
211
212/** Loads element list from file.
213 * \param *path to to standard file names
214 */
[989bf6]215bool periodentafel::LoadPeriodentafel(const char *path)
[6ac7ee]216{
[4eb4fe]217 ifstream input;
[042f82]218 bool status = true;
219 bool otherstatus = true;
220 char filename[255];
[6ac7ee]221
[042f82]222 // fill elements DB
223 strncpy(filename, path, MAXSTRINGSIZE);
224 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
225 strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
[4eb4fe]226 input.open(filename);
[61745cc]227 if (!input.fail())
228 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as elements database." << endl);
[4eb4fe]229 status = status && LoadElementsDatabase(&input);
[61745cc]230 input.close();
231 input.clear();
[4eb4fe]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);
[61745cc]238 if (!input.fail())
239 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as valence database." << endl);
[4eb4fe]240 otherstatus = otherstatus && LoadValenceDatabase(&input);
[61745cc]241 input.close();
242 input.clear();
[4eb4fe]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);
[61745cc]249 if (!input.fail())
250 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as orbitals database." << endl);
[4eb4fe]251 otherstatus = otherstatus && LoadOrbitalsDatabase(&input);
[61745cc]252 input.close();
253 input.clear();
[4eb4fe]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);
[61745cc]260 if (!input.fail())
261 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as H bond angle database." << endl);
[4eb4fe]262 otherstatus = otherstatus && LoadHBondAngleDatabase(&input);
[61745cc]263 input.close();
264 input.clear();
[4eb4fe]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);
[61745cc]271 if (!input.fail())
272 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as H bond length database." << endl);
[4eb4fe]273 otherstatus = otherstatus && LoadHBondLengthsDatabase(&input);
[61745cc]274 input.close();
275 input.clear();
[4eb4fe]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{
[ff73a2]290 bool status = true;
291 int counter = 0;
[61745cc]292 pair< std::map<atomicNumber_t,element*>::iterator, bool > InserterTest;
[ff73a2]293 if (!(*input).fail()) {
[4eb4fe]294 (*input).getline(header1, MAXSTRINGSIZE);
295 (*input).getline(header2, MAXSTRINGSIZE); // skip first two header lines
[a67d19]296 DoLog(0) && (Log() << Verbose(0) << "Parsed elements:");
[4eb4fe]297 while (!(*input).eof()) {
[042f82]298 element *neues = new element;
[7e3fc94]299 (*input) >> neues->getName();;
[4eb4fe]300 //(*input) >> ws;
[7e3fc94]301 (*input) >> neues->getSymbol();
[4eb4fe]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;
[042f82]318 //neues->Output((ofstream *)&cout);
[61745cc]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;
[61b5f0]325 delete(neues);
326 neues = Elemental;
[61745cc]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 }
[2fe971]331 DoLog(0) && (Log() << Verbose(0) << " " << *neues);
[ff73a2]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) << " <?>");
[db6bf74]336 delete(neues);
[042f82]337 }
338 }
[a67d19]339 DoLog(0) && (Log() << Verbose(0) << endl);
[61745cc]340 } else {
341 DoeLog(1) && (eLog() << Verbose(1) << "Could not open the database." << endl);
[ff73a2]342 status = false;
[61745cc]343 }
[ff73a2]344
345 if (counter == 0)
346 status = false;
347
348 return status;
[4eb4fe]349}
[6ac7ee]350
[4eb4fe]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];
[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]->Valence;
366 (*input) >> ws;
[274d45]367 //Log() << Verbose(3) << "Element " << Z << " has " << FindElement(Z)->Valence << " valence electrons." << endl;
[042f82]368 }
[4eb4fe]369 return true;
[042f82]370 } else
[4eb4fe]371 return false;
372}
[6ac7ee]373
[4eb4fe]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];
[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]->NoValenceOrbitals;
389 (*input) >> ws;
[274d45]390 //Log() << Verbose(3) << "Element " << Z << " has " << FindElement(Z)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
[042f82]391 }
[4eb4fe]392 return true;
[042f82]393 } else
[4eb4fe]394 return false;
395}
[6ac7ee]396
[4eb4fe]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];
[ff73a2]404 if (!(*input).fail()) {
[4eb4fe]405 (*input).getline(dummy, MAXSTRINGSIZE);
406 while (!(*input).eof()) {
[ead4e6]407 atomicNumber_t Z;
[4eb4fe]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;
[042f82]416 }
[4eb4fe]417 return true;
[042f82]418 } else
[4eb4fe]419 return false;
420}
[6ac7ee]421
[4eb4fe]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];
[ff73a2]429 if (!(*input).fail()) {
[4eb4fe]430 (*input).getline(dummy, MAXSTRINGSIZE);
431 while (!(*input).eof()) {
[ead4e6]432 atomicNumber_t Z;
[4eb4fe]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;
[042f82]441 }
[4eb4fe]442 return true;
[042f82]443 } else
[4eb4fe]444 return false;
445}
[6ac7ee]446
447/** Stores element list to file.
448 */
[989bf6]449bool periodentafel::StorePeriodentafel(const char *path) const
[6ac7ee]450{
[042f82]451 bool result = true;
452 ofstream f;
453 char filename[MAXSTRINGSIZE];
[6ac7ee]454
[042f82]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;
[ead4e6]462 for(const_iterator iter=elements.begin();iter!=elements.end();++iter){
463 result = result && (*iter).second->Output(&f);
[042f82]464 }
465 f.close();
[4eb4fe]466 return true;
[042f82]467 } else
[4eb4fe]468 return result;
[6ac7ee]469};
Note: See TracBrowser for help on using the repository browser.