source: src/periodentafel.cpp@ 435065

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

bonds::previous and ::next removed, lists.hpp deleted.

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