source: src/periodentafel.cpp@ ad7270

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

Reduced initial verbosity of code.

FIX:

  • initUI() - no more admonishing of missing bond length table.

The following messages have been placed in comments:

  • Registering of Action/Options.
  • Looking for KnownTypes.
  • Stating MenuNames, Separators and submenu creations.
  • No more listing of parsed elements.
  • No more listing of thermostats.

The following messages have been added:

  • prepended scanning arguments in CommandLineParser::scan...().
  • prepended running arguments in CommandLineWindow::display().
  • additional new line after ProgamHeader().
  • correct CommandLine UI setting to occur before arguments are parsed.
  • changed verbosity levels of CommandLineParser::scanforSequenceOfArguments(), also all use DoLog(), i.e. global verbosity makes them be listed again.

NOTE:

  • MemDebug: as delete NULL is safe, we do not warn when this occurs. Also, in Qt this happens a lot.

TESTFIX:

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