| 1 | /** \file periodentafel.cpp
|
|---|
| 2 | *
|
|---|
| 3 | * Function implementations for the class periodentafel.
|
|---|
| 4 | *
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | using 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 |
|
|---|
| 21 | using 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 | */
|
|---|
| 28 | periodentafel::periodentafel()
|
|---|
| 29 | {};
|
|---|
| 30 |
|
|---|
| 31 | /** destructor for class periodentafel
|
|---|
| 32 | * Removes every element and afterwards deletes start and end of list.
|
|---|
| 33 | */
|
|---|
| 34 | periodentafel::~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 | */
|
|---|
| 43 | periodentafel::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 | */
|
|---|
| 58 | void 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 | */
|
|---|
| 67 | void 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 | */
|
|---|
| 80 | const 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 | */
|
|---|
| 91 | const 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 | */
|
|---|
| 105 | const 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 | */
|
|---|
| 120 | const 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 ****************************/
|
|---|
| 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 |
|
|---|
| 163 | /** Prints period table to given stream.
|
|---|
| 164 | * \param output stream
|
|---|
| 165 | */
|
|---|
| 166 | bool 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 | */
|
|---|
| 182 | bool 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 | */
|
|---|
| 204 | bool 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 | 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
|
|---|
| 221 | DoLog(0) && (Log() << Verbose(0) << "Parsed elements:");
|
|---|
| 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;
|
|---|
| 243 | DoLog(0) && (Log() << Verbose(0) << " " << neues->symbol);
|
|---|
| 244 | //neues->Output((ofstream *)&cout);
|
|---|
| 245 | if ((neues->Z > 0) && (neues->Z < MAX_ELEMENTS))
|
|---|
| 246 | parsedElems[neues->getNumber()] = neues;
|
|---|
| 247 | else {
|
|---|
| 248 | DoLog(0) && (Log() << Verbose(0) << "Could not parse element: ");
|
|---|
| 249 | neues->Output((ofstream *)&cout);
|
|---|
| 250 | delete(neues);
|
|---|
| 251 | }
|
|---|
| 252 | }
|
|---|
| 253 | DoLog(0) && (Log() << Verbose(0) << endl);
|
|---|
| 254 | infile.close();
|
|---|
| 255 | infile.clear();
|
|---|
| 256 | } else
|
|---|
| 257 | status = false;
|
|---|
| 258 |
|
|---|
| 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()) {
|
|---|
| 266 | atomicNumber_t Z;
|
|---|
| 267 | infile >> Z;
|
|---|
| 268 | infile >> ws;
|
|---|
| 269 | infile >> parsedElems[Z]->Valence;
|
|---|
| 270 | infile >> ws;
|
|---|
| 271 | //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->Valence << " valence electrons." << endl;
|
|---|
| 272 | }
|
|---|
| 273 | infile.close();
|
|---|
| 274 | infile.clear();
|
|---|
| 275 | } else
|
|---|
| 276 | otherstatus = false;
|
|---|
| 277 |
|
|---|
| 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()) {
|
|---|
| 285 | atomicNumber_t Z;
|
|---|
| 286 | infile >> Z;
|
|---|
| 287 | infile >> ws;
|
|---|
| 288 | infile >> parsedElems[Z]->NoValenceOrbitals;
|
|---|
| 289 | infile >> ws;
|
|---|
| 290 | //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
|
|---|
| 291 | }
|
|---|
| 292 | infile.close();
|
|---|
| 293 | infile.clear();
|
|---|
| 294 | } else
|
|---|
| 295 | otherstatus = false;
|
|---|
| 296 |
|
|---|
| 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()) {
|
|---|
| 304 | atomicNumber_t Z;
|
|---|
| 305 | infile >> Z;
|
|---|
| 306 | ptr = parsedElems[Z];
|
|---|
| 307 | infile >> ws;
|
|---|
| 308 | infile >> ptr->HBondDistance[0];
|
|---|
| 309 | infile >> ptr->HBondDistance[1];
|
|---|
| 310 | infile >> ptr->HBondDistance[2];
|
|---|
| 311 | infile >> ws;
|
|---|
| 312 | //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen." << endl;
|
|---|
| 313 | }
|
|---|
| 314 | infile.close();
|
|---|
| 315 | infile.clear();
|
|---|
| 316 | } else
|
|---|
| 317 | otherstatus = false;
|
|---|
| 318 |
|
|---|
| 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()) {
|
|---|
| 326 | atomicNumber_t Z;
|
|---|
| 327 | infile >> Z;
|
|---|
| 328 | ptr = parsedElems[Z];
|
|---|
| 329 | infile >> ws;
|
|---|
| 330 | infile >> ptr->HBondAngle[0];
|
|---|
| 331 | infile >> ptr->HBondAngle[1];
|
|---|
| 332 | infile >> ptr->HBondAngle[2];
|
|---|
| 333 | infile >> ws;
|
|---|
| 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;
|
|---|
| 335 | }
|
|---|
| 336 | infile.close();
|
|---|
| 337 | } else
|
|---|
| 338 | otherstatus = false;
|
|---|
| 339 |
|
|---|
| 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{
|
|---|
| 347 | DoeLog(2) && (eLog()<< Verbose(2) << "Something went wrong while parsing the other databases!" << endl);
|
|---|
| 348 | map<atomicNumber_t,element*>::iterator iter;
|
|---|
| 349 | for(iter=parsedElems.begin();iter!=parsedElems.end();++iter){
|
|---|
| 350 | AddElement((*iter).second);
|
|---|
| 351 | }
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | return status;
|
|---|
| 355 | };
|
|---|
| 356 |
|
|---|
| 357 | /** Stores element list to file.
|
|---|
| 358 | */
|
|---|
| 359 | bool periodentafel::StorePeriodentafel(const char *path) const
|
|---|
| 360 | {
|
|---|
| 361 | bool result = true;
|
|---|
| 362 | ofstream f;
|
|---|
| 363 | char filename[MAXSTRINGSIZE];
|
|---|
| 364 |
|
|---|
| 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;
|
|---|
| 372 | for(const_iterator iter=elements.begin();iter!=elements.end();++iter){
|
|---|
| 373 | result = result && (*iter).second->Output(&f);
|
|---|
| 374 | }
|
|---|
| 375 | f.close();
|
|---|
| 376 | } else
|
|---|
| 377 | result = false;
|
|---|
| 378 | return result;
|
|---|
| 379 | };
|
|---|