source: src/periodentafel.cpp@ 1ac51b

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

Made the construction of the names of database files simpler

  • Property mode set to 100755
File size: 10.5 KB
Line 
1/** \file periodentafel.cpp
2 *
3 * Function implementations for the class periodentafel.
4 *
5 */
6
7using namespace std;
8
9#include <iomanip>
10#include <fstream>
11#include <cstring>
12#include <cassert>
13
14#include "element.hpp"
15#include "helpers.hpp"
16#include "lists.hpp"
17#include "log.hpp"
18#include "periodentafel.hpp"
19#include "verbose.hpp"
20
21using namespace std;
22
23/************************************* Functions for class periodentafel ***************************/
24
25/** constructor for class periodentafel
26 * Initialises start and end of list and resets periodentafel::checkliste to false.
27 */
28periodentafel::periodentafel()
29{};
30
31/** destructor for class periodentafel
32 * Removes every element and afterwards deletes start and end of list.
33 */
34periodentafel::~periodentafel()
35{
36 CleanupPeriodtable();
37};
38
39/** Adds element to period table list
40 * \param *pointer element to be added
41 * \return true - succeeded, false - does not occur
42 */
43periodentafel::iterator periodentafel::AddElement(element * const pointer)
44{
45 atomicNumber_t Z = pointer->getNumber();
46 assert(!elements.count(Z));
47 pointer->sort = &pointer->Z;
48 if (pointer->getNumber() < 1 && pointer->getNumber() >= MAX_ELEMENTS)
49 DoeLog(0) && (eLog() << Verbose(0) << "Invalid Z number!\n");
50 pair<iterator,bool> res = elements.insert(pair<atomicNumber_t,element*>(Z,pointer));
51 return res.first;
52};
53
54/** Removes element from list.
55 * \param *pointer element to be removed
56 * \return true - succeeded, false - element not found
57 */
58void periodentafel::RemoveElement(element * const pointer)
59{
60 atomicNumber_t Z = pointer->getNumber();
61 elements.erase(Z);
62};
63
64/** Removes every element from the period table.
65 * \return true - succeeded, false - does not occur
66 */
67void periodentafel::CleanupPeriodtable()
68{
69 for(iterator iter=elements.begin();iter!=elements.end();++iter){
70 delete(*iter).second;
71 }
72 elements.clear();
73};
74
75/** Finds an element by its atomic number.
76 * If element is not yet in list, returns NULL.
77 * \param Z atomic number
78 * \return pointer to element or NULL if not found
79 */
80const element * periodentafel::FindElement(atomicNumber_t Z) const
81{
82 const_iterator res = elements.find(Z);
83 return res!=elements.end()?((*res).second):0;
84};
85
86/** Finds an element by its atomic number.
87 * If element is not yet in list, datas are asked and stored in database.
88 * \param shorthand chemical symbol of the element, e.g. H for hydrogene
89 * \return pointer to element
90 */
91const element * periodentafel::FindElement(const char * const shorthand) const
92{
93 element *res = 0;
94 for(const_iterator iter=elements.begin();iter!=elements.end();++iter) {
95 if((*iter).second->getSymbol() == shorthand){
96 res = (*iter).second;
97 break;
98 }
99 }
100 return res;
101};
102
103/** Asks for element number and returns pointer to element
104 */
105const element * periodentafel::AskElement() const
106{
107 const element *walker = NULL;
108 int Z;
109 do {
110 DoLog(0) && (Log() << Verbose(0) << "Atomic number Z: ");
111 cin >> Z;
112 walker = this->FindElement(Z); // give type
113 } while (walker == NULL);
114 return walker;
115};
116
117/** Asks for element and if not found, presents mask to enter info.
118 * \return pointer to either present or newly created element
119 */
120const element * periodentafel::EnterElement()
121{
122 const element *res = NULL;
123 atomicNumber_t Z = 0;
124 DoLog(0) && (Log() << Verbose(0) << "Atomic number: " << Z << endl);
125 cin >> Z;
126 res = FindElement(Z);
127 if (!res) {
128 // TODO: make this using the constructor
129 element *tmp;
130 DoLog(0) && (Log() << Verbose(0) << "Element not found in database, please enter." << endl);
131 tmp = new element;
132 tmp->Z = Z;
133 DoLog(0) && (Log() << Verbose(0) << "Mass: " << endl);
134 cin >> tmp->mass;
135 DoLog(0) && (Log() << Verbose(0) << "Name [max 64 chars]: " << endl);
136 cin >> tmp->name;
137 DoLog(0) && (Log() << Verbose(0) << "Short form [max 3 chars]: " << endl);
138 cin >> tmp->symbol;
139 AddElement(tmp);
140 res = tmp;
141 }
142 return res;
143};
144
145
146/******************** Access to iterators ****************************/
147periodentafel::const_iterator periodentafel::begin(){
148 return elements.begin();
149}
150
151periodentafel::const_iterator periodentafel::end(){
152 return elements.end();
153}
154
155periodentafel::reverse_iterator periodentafel::rbegin(){
156 return reverse_iterator(elements.end());
157}
158
159periodentafel::reverse_iterator periodentafel::rend(){
160 return reverse_iterator(elements.begin());
161}
162
163/** Prints period table to given stream.
164 * \param output stream
165 */
166bool periodentafel::Output(ostream * const output) const
167{
168 bool result = true;
169 if (output != NULL) {
170 for(const_iterator iter=elements.begin(); iter !=elements.end();++iter){
171 result = result && (*iter).second->Output(output);
172 }
173 return result;
174 } else
175 return false;
176};
177
178/** Prints period table to given stream.
179 * \param *output output stream
180 * \param *checkliste elements table for this molecule
181 */
182bool periodentafel::Checkout(ostream * const output, const int * const checkliste) const
183{
184 bool result = true;
185 int No = 1;
186
187 if (output != NULL) {
188 *output << "# Ion type data (PP = PseudoPotential, Z = atomic number)" << endl;
189 *output << "#Ion_TypeNr.\tAmount\tZ\tRGauss\tL_Max(PP)L_Loc(PP)IonMass\t# chemical name, symbol" << endl;
190 for(const_iterator iter=elements.begin(); iter!=elements.end();++iter){
191 if (((*iter).first < MAX_ELEMENTS) && (checkliste[(*iter).first])) {
192 (*iter).second->No = No;
193 result = result && (*iter).second->Checkout(output, No++, checkliste[(*iter).first]);
194 }
195 }
196 return result;
197 } else
198 return false;
199};
200
201/** Loads element list from file.
202 * \param *path to to standard file names
203 */
204bool periodentafel::LoadPeriodentafel(const char *path)
205{
206 ifstream infile;
207 element *ptr;
208 map<atomicNumber_t,element*> parsedElems;
209 bool status = true;
210 bool otherstatus = true;
211 char filename[255];
212
213 // fill elements DB
214 snprintf(filename,MAXSTRINGSIZE,"%s/%s",path,STANDARDELEMENTSDB);
215 infile.open(filename);
216 if (infile != NULL) {
217 infile.getline(header1, MAXSTRINGSIZE);
218 infile.getline(header2, MAXSTRINGSIZE); // skip first two header lines
219 DoLog(0) && (Log() << Verbose(0) << "Parsed elements:");
220 while (!infile.eof()) {
221 element *neues = new element;
222 infile >> neues->name;
223 //infile >> ws;
224 infile >> neues->symbol;
225 //infile >> ws;
226 infile >> neues->period;
227 //infile >> ws;
228 infile >> neues->group;
229 //infile >> ws;
230 infile >> neues->block;
231 //infile >> ws;
232 infile >> neues->Z;
233 //infile >> ws;
234 infile >> neues->mass;
235 //infile >> ws;
236 infile >> neues->CovalentRadius;
237 //infile >> ws;
238 infile >> neues->VanDerWaalsRadius;
239 //infile >> ws;
240 infile >> ws;
241 DoLog(0) && (Log() << Verbose(0) << " " << neues->symbol);
242 //neues->Output((ofstream *)&cout);
243 if ((neues->Z > 0) && (neues->Z < MAX_ELEMENTS))
244 parsedElems[neues->getNumber()] = neues;
245 else {
246 DoLog(0) && (Log() << Verbose(0) << "Could not parse element: ");
247 neues->Output((ofstream *)&cout);
248 delete(neues);
249 }
250 }
251 DoLog(0) && (Log() << Verbose(0) << endl);
252 infile.close();
253 infile.clear();
254 } else
255 status = false;
256
257 // fill valence DB per element
258 snprintf(filename,MAXSTRINGSIZE,"%s/%s",path,STANDARDVALENCEDB);
259 infile.open(filename);
260 if (infile != NULL) {
261 while (!infile.eof()) {
262 atomicNumber_t Z;
263 infile >> Z;
264 infile >> ws;
265 infile >> parsedElems[Z]->Valence;
266 infile >> ws;
267 //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->Valence << " valence electrons." << endl;
268 }
269 infile.close();
270 infile.clear();
271 } else
272 otherstatus = false;
273
274 // fill valence DB per element
275 snprintf(filename,MAXSTRINGSIZE,"%s/%s",path,STANDARDORBITALDB);
276 infile.open(filename);
277 if (infile != NULL) {
278 while (!infile.eof()) {
279 atomicNumber_t Z;
280 infile >> Z;
281 infile >> ws;
282 infile >> parsedElems[Z]->NoValenceOrbitals;
283 infile >> ws;
284 //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
285 }
286 infile.close();
287 infile.clear();
288 } else
289 otherstatus = false;
290
291 // fill H-BondDistance DB per element
292 snprintf(filename,MAXSTRINGSIZE,"%s/%s",path,STANDARDHBONDDISTANCEDB);
293 infile.open(filename);
294 if (infile != NULL) {
295 while (!infile.eof()) {
296 atomicNumber_t Z;
297 infile >> Z;
298 ptr = parsedElems[Z];
299 infile >> ws;
300 infile >> ptr->HBondDistance[0];
301 infile >> ptr->HBondDistance[1];
302 infile >> ptr->HBondDistance[2];
303 infile >> ws;
304 //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen." << endl;
305 }
306 infile.close();
307 infile.clear();
308 } else
309 otherstatus = false;
310
311 // fill H-BondAngle DB per element
312 snprintf(filename,MAXSTRINGSIZE,"%s/%s",path,STANDARDHBONDANGLEDB);
313 infile.open(filename);
314 if (infile != NULL) {
315 while (!infile.eof()) {
316 atomicNumber_t Z;
317 infile >> Z;
318 ptr = parsedElems[Z];
319 infile >> ws;
320 infile >> ptr->HBondAngle[0];
321 infile >> ptr->HBondAngle[1];
322 infile >> ptr->HBondAngle[2];
323 infile >> ws;
324 //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;
325 }
326 infile.close();
327 } else
328 otherstatus = false;
329
330 if (otherstatus){
331 map<atomicNumber_t,element*>::iterator iter;
332 for(iter=parsedElems.begin();iter!=parsedElems.end();++iter){
333 AddElement((*iter).second);
334 }
335 }
336 else{
337 DoeLog(2) && (eLog()<< Verbose(2) << "Something went wrong while parsing the other databases!" << endl);
338 map<atomicNumber_t,element*>::iterator iter;
339 for(iter=parsedElems.begin();iter!=parsedElems.end();++iter){
340 AddElement((*iter).second);
341 }
342 }
343
344 return status;
345};
346
347/** Stores element list to file.
348 */
349bool periodentafel::StorePeriodentafel(const char *path) const
350{
351 bool result = true;
352 ofstream f;
353 char filename[MAXSTRINGSIZE];
354
355 snprintf(filename,MAXSTRINGSIZE,"%s/%s",path,STANDARDELEMENTSDB);
356 f.open(filename);
357 if (f != NULL) {
358 f << header1 << endl;
359 f << header2 << endl;
360 for(const_iterator iter=elements.begin();iter!=elements.end();++iter){
361 result = result && (*iter).second->Output(&f);
362 }
363 f.close();
364 } else
365 result = false;
366 return result;
367};
Note: See TracBrowser for help on using the repository browser.