source: src/periodentafel.cpp@ ea430a

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

Removed smaller warning, mostly due to NDEBUG.

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