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