source: molecuilder/src/periodentafel.cpp@ 5738177

Last change on this file since 5738177 was d639c7, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Fixed memory leak in periodentafel

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