source: src/periodentafel.cpp@ 266875

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 266875 was febef3, checked in by Frederik Heber <heber@…>, 14 years ago

Removed verbosity of LoadElectronegativityDb.

  • Property mode set to 100755
File size: 16.8 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/** \file periodentafel.cpp
9 *
10 * Function implementations for the class periodentafel.
11 *
12 */
13
14// include config.h
15#ifdef HAVE_CONFIG_H
16#include <config.h>
17#endif
18
19#include "CodePatterns/MemDebug.hpp"
20
21#include <iomanip>
22#include <iostream>
23#include <fstream>
24#include <cstring>
25
26#include "CodePatterns/Assert.hpp"
27#include "element.hpp"
28#include "elements_db.hpp"
29#include "Helpers/helpers.hpp"
30#include "CodePatterns/Log.hpp"
31//#include "lists.hpp"
32#include "periodentafel.hpp"
33#include "CodePatterns/Verbose.hpp"
34
35using namespace std;
36
37/************************************* Functions for class periodentafel ***************************/
38
39/** constructor for class periodentafel
40 * Initialises start and end of list and resets periodentafel::checkliste to false.
41 */
42periodentafel::periodentafel()
43{
44 {
45 stringstream input(elementsDB,ios_base::in);
46#ifndef NDEBUG
47 bool status =
48#endif
49 LoadElementsDatabase(input);
50 ASSERT(status, "General element initialization failed");
51 }
52 {
53 stringstream input(ElectronegativitiesDB,ios_base::in);
54#ifndef NDEBUG
55 bool status =
56#endif
57 LoadElectronegativityDatabase(input);
58 ASSERT(status, "Electronegativities entry of element initialization failed");
59 }
60 {
61 stringstream input(valenceDB,ios_base::in);
62#ifndef NDEBUG
63 bool status =
64#endif
65 LoadValenceDatabase(input);
66 ASSERT(status, "Valence entry of element initialization failed");
67 }
68 {
69 stringstream input(orbitalsDB,ios_base::in);
70#ifndef NDEBUG
71 bool status =
72#endif
73 LoadOrbitalsDatabase(input);
74 ASSERT(status, "Orbitals entry of element initialization failed");
75 }
76 {
77 stringstream input(HbondangleDB,ios_base::in);
78#ifndef NDEBUG
79 bool status =
80#endif
81 LoadHBondAngleDatabase(input);
82 ASSERT(status, "HBond angle entry of element initialization failed");
83 }
84 {
85 stringstream input(HbonddistanceDB,ios_base::in);
86#ifndef NDEBUG
87 bool status =
88#endif
89 LoadHBondLengthsDatabase(input);
90 ASSERT(status, "HBond distance entry of element initialization failed");
91 }
92};
93
94/** destructor for class periodentafel
95 * Removes every element and afterwards deletes start and end of list.
96 * TODO: Handle when elements have changed and store databases then
97 */
98periodentafel::~periodentafel()
99{
100 CleanupPeriodtable();
101};
102
103/** Adds element to period table list
104 * \param *pointer element to be added
105 * \return iterator to added element
106 */
107periodentafel::iterator periodentafel::AddElement(element * pointer)
108{
109 atomicNumber_t Z = pointer->getNumber();
110 ASSERT(!elements.count(Z), "Element is already present.");
111 if (pointer->getNumber() < 1 && pointer->getNumber() >= MAX_ELEMENTS)
112 DoeLog(0) && (eLog() << Verbose(0) << "Invalid Z number!\n");
113 pair<iterator,bool> res = elements.insert(pair<atomicNumber_t,element*>(Z,pointer));
114 return res.first;
115};
116
117/** Removes element from list.
118 * \param *pointer element to be removed
119 */
120size_t periodentafel::RemoveElement(const element * pointer)
121{
122 return RemoveElement(pointer->getNumber());
123};
124
125/** Removes element from list.
126 * \param Z element to be removed
127 */
128size_t periodentafel::RemoveElement(atomicNumber_t Z)
129{
130 return elements.erase(Z);
131};
132
133/** Removes every element from the period table.
134 */
135void periodentafel::CleanupPeriodtable()
136{
137 for(iterator iter=elements.begin();iter!=elements.end();++iter){
138 delete(*iter).second;
139 }
140 elements.clear();
141};
142
143/** Finds an element by its atomic number.
144 * If element is not yet in list, returns NULL.
145 * \param Z atomic number
146 * \return pointer to element or NULL if not found
147 */
148const element * periodentafel::FindElement(atomicNumber_t Z) const
149{
150 const_iterator res = elements.find(Z);
151 return res!=elements.end()?((*res).second):0;
152};
153
154/** Finds an element by its atomic number.
155 * If element is not yet in list, datas are asked and stored in database.
156 * \param shorthand chemical symbol of the element, e.g. H for hydrogene
157 * \return pointer to element
158 */
159const element * periodentafel::FindElement(const string &shorthand) const
160{
161 element *res = 0;
162 for(const_iterator iter=elements.begin();iter!=elements.end();++iter) {
163 if((*iter).second->getSymbol() == shorthand){
164 res = (*iter).second;
165 break;
166 }
167 }
168 return res;
169};
170
171/** Asks for element number and returns pointer to element
172 * \return desired element or NULL
173 */
174const element * periodentafel::AskElement() const
175{
176 const element * walker = NULL;
177 int Z;
178 do {
179 DoLog(0) && (Log() << Verbose(0) << "Atomic number Z: ");
180 cin >> Z;
181 walker = this->FindElement(Z); // give type
182 } while (walker == NULL);
183 return walker;
184};
185
186/** Asks for element and if not found, presents mask to enter info.
187 * \return pointer to either present or newly created element
188 */
189const element * periodentafel::EnterElement()
190{
191 atomicNumber_t Z = 0;
192 DoLog(0) && (Log() << Verbose(0) << "Atomic number: " << Z << endl);
193 cin >> Z;
194 const element *res = FindElement(Z);
195 if (!res) {
196 // TODO: make this using the constructor
197 DoLog(0) && (Log() << Verbose(0) << "Element not found in database, please enter." << endl);
198 element *tmp = new element;
199 tmp->Z = Z;
200 DoLog(0) && (Log() << Verbose(0) << "Mass: " << endl);
201 cin >> tmp->mass;
202 DoLog(0) && (Log() << Verbose(0) << "Name [max 64 chars]: " << endl);
203 cin >> tmp->getName();
204 DoLog(0) && (Log() << Verbose(0) << "Short form [max 3 chars]: " << endl);
205 cin >> tmp->getSymbol();
206 AddElement(tmp);
207 return tmp;
208 }
209 return res;
210};
211
212
213/******************** Access to iterators ****************************/
214periodentafel::const_iterator periodentafel::begin() const{
215 return elements.begin();
216}
217
218periodentafel::const_iterator periodentafel::end() const{
219 return elements.end();
220}
221
222periodentafel::reverse_iterator periodentafel::rbegin() const{
223 return reverse_iterator(elements.end());
224}
225
226periodentafel::reverse_iterator periodentafel::rend() const{
227 return reverse_iterator(elements.begin());
228}
229
230/** Prints period table to given stream.
231 * \param output stream
232 */
233bool periodentafel::Output(ostream * const output) const
234{
235 bool result = true;
236 if (output != NULL) {
237 for(const_iterator iter=elements.begin(); iter !=elements.end();++iter){
238 result = result && (*iter).second->Output(output);
239 }
240 return result;
241 } else
242 return false;
243};
244
245/** Loads element list from file.
246 * \param *path to to standard file names
247 */
248bool periodentafel::LoadPeriodentafel(const char *path)
249{
250 ifstream input;
251 bool status = true;
252 bool otherstatus = true;
253 char filename[255];
254
255 // fill elements DB
256 strncpy(filename, path, MAXSTRINGSIZE);
257 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
258 strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
259 input.open(filename);
260 if (!input.fail())
261 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as elements database." << endl);
262 status = status && LoadElementsDatabase(input);
263 input.close();
264 input.clear();
265
266 // fill valence DB per element
267 strncpy(filename, path, MAXSTRINGSIZE);
268 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
269 strncat(filename, STANDARDELECTRONEGATIVITYDB, MAXSTRINGSIZE-strlen(filename));
270 input.open(filename);
271 if (!input.fail())
272 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as electronegativity database." << endl);
273 otherstatus = otherstatus && LoadElectronegativityDatabase(input);
274 input.close();
275 input.clear();
276
277 // fill valence DB per element
278 strncpy(filename, path, MAXSTRINGSIZE);
279 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
280 strncat(filename, STANDARDVALENCEDB, MAXSTRINGSIZE-strlen(filename));
281 input.open(filename);
282 if (!input.fail())
283 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as valence database." << endl);
284 otherstatus = otherstatus && LoadValenceDatabase(input);
285 input.close();
286 input.clear();
287
288 // fill orbitals DB per element
289 strncpy(filename, path, MAXSTRINGSIZE);
290 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
291 strncat(filename, STANDARDORBITALDB, MAXSTRINGSIZE-strlen(filename));
292 input.open(filename);
293 if (!input.fail())
294 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as orbitals database." << endl);
295 otherstatus = otherstatus && LoadOrbitalsDatabase(input);
296 input.close();
297 input.clear();
298
299 // fill H-BondAngle DB per element
300 strncpy(filename, path, MAXSTRINGSIZE);
301 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
302 strncat(filename, STANDARDHBONDANGLEDB, MAXSTRINGSIZE-strlen(filename));
303 input.open(filename);
304 if (!input.fail())
305 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as H bond angle database." << endl);
306 otherstatus = otherstatus && LoadHBondAngleDatabase(input);
307 input.close();
308 input.clear();
309
310 // fill H-BondDistance DB per element
311 strncpy(filename, path, MAXSTRINGSIZE);
312 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
313 strncat(filename, STANDARDHBONDDISTANCEDB, MAXSTRINGSIZE-strlen(filename));
314 input.open(filename);
315 if (!input.fail())
316 DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as H bond length database." << endl);
317 otherstatus = otherstatus && LoadHBondLengthsDatabase(input);
318 input.close();
319 input.clear();
320
321 if (!otherstatus){
322 DoeLog(2) && (eLog()<< Verbose(2) << "Something went wrong while parsing the other databases!" << endl);
323 }
324
325 return status;
326};
327
328/** load the element info.
329 * \param *input stream to parse from
330 * \return true - parsing successful, false - something went wrong
331 */
332bool periodentafel::LoadElementsDatabase(istream &input)
333{
334 bool status = true;
335 string header1tmp,header2tmp;
336 // first parse into a map, so we can revert to old status in case something goes wront
337 map<atomicNumber_t,element*> parsedElements;
338 if (!input.fail()) {
339 getline(input,header1tmp);
340 getline(input,header2tmp); // skip first two header lines
341 //cout << "First header: " << header1tmp << endl;
342 //cout << "Second header: " << header2tmp << endl;
343// DoLog(0) && (Log() << Verbose(0) << "Parsed elements:");
344 while (!input.eof()) {
345 element *neues = new element;
346 input >> neues->name;
347 //input >> ws;
348 input >> neues->symbol;
349 //input >> ws;
350 input >> neues->period;
351 //input >> ws;
352 input >> neues->group;
353 //input >> ws;
354 input >> neues->block;
355 //input >> ws;
356 input >> neues->Z;
357 //input >> ws;
358 input >> neues->mass;
359 //input >> ws;
360 input >> neues->CovalentRadius;
361 //input >> ws;
362 input >> neues->VanDerWaalsRadius;
363 //input >> ws;
364 input >> ws;
365 //neues->Output((ofstream *)&cout);
366 if ((neues->getNumber() > 0) && (neues->getNumber() < MAX_ELEMENTS)) {
367 parsedElements[neues->Z] = neues;
368// DoLog(0) && (Log() << Verbose(0) << " " << *neues);
369 } else {
370 DoeLog(2) && (eLog() << Verbose(2) << "Detected empty line or invalid element in elements db, discarding." << endl);
371 DoLog(0) && (Log() << Verbose(0) << " <?>");
372 delete(neues);
373 }
374 // when the input is in failed state, we most likely just read garbage
375 if(input.fail()) {
376 DoeLog(2) && (eLog() << Verbose(2) << "Error parsing elements db." << endl);
377 status = false;
378 break;
379 }
380 }
381// DoLog(0) && (Log() << Verbose(0) << endl);
382 } else {
383 DoeLog(1) && (eLog() << Verbose(1) << "Could not open the database." << endl);
384 status = false;
385 }
386
387 if (!parsedElements.size())
388 status = false;
389
390 if(status){
391 for(map<atomicNumber_t,element*>::iterator iter=parsedElements.begin();
392 iter!=parsedElements.end();
393 ++iter){
394 if (elements.count(iter->first)) {
395 // if element already present, replace the old one
396 // pointer to old element might still be in use, so we have to replace into the old element
397 *(elements[iter->first])=*iter->second;
398 delete(iter->second);
399 }
400 else {
401 // no such element in periodentafel... we can just insert
402 elements[iter->first] = iter->second;
403 }
404 }
405 // all went well.. we now copy the header
406 strncpy(header1,header1tmp.c_str(),MAXSTRINGSIZE);
407 header1[MAXSTRINGSIZE-1]=0;
408 strncpy(header2,header2tmp.c_str(),MAXSTRINGSIZE);
409 header2[MAXSTRINGSIZE-1]=0;
410 }
411
412 return status;
413}
414
415/** load the electronegativity info.
416 * \param *input stream to parse from
417 * \return true - parsing successful, false - something went wrong
418 */
419bool periodentafel::LoadElectronegativityDatabase(std::istream &input)
420{
421 char dummy[MAXSTRINGSIZE];
422 if (!input.fail()) {
423 input.getline(dummy, MAXSTRINGSIZE);
424 while (!input.eof()) {
425 atomicNumber_t Z;
426 input >> Z;
427 ASSERT(elements.count(Z), "Element not present");
428 input >> ws;
429 input >> elements[Z]->Electronegativity;
430 input >> ws;
431 //DoLog(1) && (Log() << Verbose(1)
432 // << "Element " << Z << " has " << FindElement(Z)->Electronegativity << " valence electrons." << endl);
433 }
434 return true;
435 } else
436 return false;
437}
438
439/** load the valence info.
440 * \param *input stream to parse from
441 * \return true - parsing successful, false - something went wrong
442 */
443bool periodentafel::LoadValenceDatabase(istream &input)
444{
445 char dummy[MAXSTRINGSIZE];
446 if (!input.fail()) {
447 input.getline(dummy, MAXSTRINGSIZE);
448 while (!input.eof()) {
449 atomicNumber_t Z;
450 input >> Z;
451 ASSERT(elements.count(Z), "Element not present");
452 input >> ws;
453 input >> elements[Z]->Valence;
454 input >> ws;
455 //Log() << Verbose(3) << "Element " << Z << " has " << FindElement(Z)->Valence << " valence electrons." << endl;
456 }
457 return true;
458 } else
459 return false;
460}
461
462/** load the orbitals info.
463 * \param *input stream to parse from
464 * \return true - parsing successful, false - something went wrong
465 */
466bool periodentafel::LoadOrbitalsDatabase(istream &input)
467{
468 char dummy[MAXSTRINGSIZE];
469 if (!input.fail()) {
470 input.getline(dummy, MAXSTRINGSIZE);
471 while (!input.eof()) {
472 atomicNumber_t Z;
473 input >> Z;
474 ASSERT(elements.count(Z), "Element not present");
475 input >> ws;
476 input >> elements[Z]->NoValenceOrbitals;
477 input >> ws;
478 //Log() << Verbose(3) << "Element " << Z << " has " << FindElement(Z)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
479 }
480 return true;
481 } else
482 return false;
483}
484
485/** load the hbond angles info.
486 * \param *input stream to parse from
487 * \return true - parsing successful, false - something went wrong
488 */
489bool periodentafel::LoadHBondAngleDatabase(istream &input)
490{
491 char dummy[MAXSTRINGSIZE];
492 if (!input.fail()) {
493 input.getline(dummy, MAXSTRINGSIZE);
494 while (!input.eof()) {
495 atomicNumber_t Z;
496 input >> Z;
497 ASSERT(elements.count(Z), "Element not present");
498 input >> ws;
499 input >> elements[Z]->HBondAngle[0];
500 input >> elements[Z]->HBondAngle[1];
501 input >> elements[Z]->HBondAngle[2];
502 input >> ws;
503 //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;
504 }
505 return true;
506 } else
507 return false;
508}
509
510/** load the hbond lengths info.
511 * \param *input stream to parse from
512 * \return true - parsing successful, false - something went wrong
513 */
514bool periodentafel::LoadHBondLengthsDatabase(istream &input)
515{
516 char dummy[MAXSTRINGSIZE];
517 if (!input.fail()) {
518 input.getline(dummy, MAXSTRINGSIZE);
519 while (!input.eof()) {
520 atomicNumber_t Z;
521 input >> Z;
522 ASSERT(elements.count(Z), "Element not present");
523 input >> ws;
524 input >> elements[Z]->HBondDistance[0];
525 input >> elements[Z]->HBondDistance[1];
526 input >> elements[Z]->HBondDistance[2];
527 input >> ws;
528 //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen." << endl;
529 }
530 return true;
531 } else
532 return false;
533}
534
535/** Stores element list to file.
536 */
537bool periodentafel::StorePeriodentafel(const char *path) const
538{
539 bool result = true;
540 ofstream f;
541 char filename[MAXSTRINGSIZE];
542
543 strncpy(filename, path, MAXSTRINGSIZE);
544 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
545 strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
546 f.open(filename);
547 if (f != NULL) {
548 f << header1 << endl;
549 f << header2 << endl;
550 for(const_iterator iter=elements.begin();iter!=elements.end();++iter){
551 result = result && (*iter).second->Output(&f);
552 }
553 f.close();
554 return true;
555 } else
556 return result;
557};
Note: See TracBrowser for help on using the repository browser.