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