| 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 | Log() << 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 | elements.clear();
|
|---|
| 70 | };
|
|---|
| 71 |
|
|---|
| 72 | /** Finds an element by its atomic number.
|
|---|
| 73 | * If element is not yet in list, returns NULL.
|
|---|
| 74 | * \param Z atomic number
|
|---|
| 75 | * \return pointer to element or NULL if not found
|
|---|
| 76 | */
|
|---|
| 77 | const element * periodentafel::FindElement(atomicNumber_t Z) const
|
|---|
| 78 | {
|
|---|
| 79 | const_iterator res = elements.find(Z);
|
|---|
| 80 | return res!=elements.end()?((*res).second):0;
|
|---|
| 81 | };
|
|---|
| 82 |
|
|---|
| 83 | /** Finds an element by its atomic number.
|
|---|
| 84 | * If element is not yet in list, datas are asked and stored in database.
|
|---|
| 85 | * \param shorthand chemical symbol of the element, e.g. H for hydrogene
|
|---|
| 86 | * \return pointer to element
|
|---|
| 87 | */
|
|---|
| 88 | const element * periodentafel::FindElement(const char * const shorthand) const
|
|---|
| 89 | {
|
|---|
| 90 | element *res = 0;
|
|---|
| 91 | for(const_iterator iter=elements.begin();iter!=elements.end();++iter) {
|
|---|
| 92 | if((*iter).second->getSymbol() == shorthand){
|
|---|
| 93 | res = (*iter).second;
|
|---|
| 94 | break;
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | return res;
|
|---|
| 98 | };
|
|---|
| 99 |
|
|---|
| 100 | /** Asks for element number and returns pointer to element
|
|---|
| 101 | */
|
|---|
| 102 | const element * periodentafel::AskElement() const
|
|---|
| 103 | {
|
|---|
| 104 | const element *walker = NULL;
|
|---|
| 105 | int Z;
|
|---|
| 106 | do {
|
|---|
| 107 | Log() << Verbose(0) << "Atomic number Z: ";
|
|---|
| 108 | cin >> Z;
|
|---|
| 109 | walker = this->FindElement(Z); // give type
|
|---|
| 110 | } while (walker == NULL);
|
|---|
| 111 | return walker;
|
|---|
| 112 | };
|
|---|
| 113 |
|
|---|
| 114 | /** Asks for element and if not found, presents mask to enter info.
|
|---|
| 115 | * \return pointer to either present or newly created element
|
|---|
| 116 | */
|
|---|
| 117 | const element * periodentafel::EnterElement()
|
|---|
| 118 | {
|
|---|
| 119 | const element *res = NULL;
|
|---|
| 120 | atomicNumber_t Z = 0;
|
|---|
| 121 | Log() << Verbose(0) << "Atomic number: " << Z << endl;
|
|---|
| 122 | cin >> Z;
|
|---|
| 123 | res = FindElement(Z);
|
|---|
| 124 | if (!res) {
|
|---|
| 125 | // TODO: make this using the constructor
|
|---|
| 126 | element *tmp;
|
|---|
| 127 | Log() << Verbose(0) << "Element not found in database, please enter." << endl;
|
|---|
| 128 | tmp = new element;
|
|---|
| 129 | tmp->Z = Z;
|
|---|
| 130 | Log() << Verbose(0) << "Mass: " << endl;
|
|---|
| 131 | cin >> tmp->mass;
|
|---|
| 132 | Log() << Verbose(0) << "Name [max 64 chars]: " << endl;
|
|---|
| 133 | cin >> tmp->name;
|
|---|
| 134 | Log() << Verbose(0) << "Short form [max 3 chars]: " << endl;
|
|---|
| 135 | cin >> tmp->symbol;
|
|---|
| 136 | AddElement(tmp);
|
|---|
| 137 | res = tmp;
|
|---|
| 138 | }
|
|---|
| 139 | return res;
|
|---|
| 140 | };
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 | /******************** Access to iterators ****************************/
|
|---|
| 144 | periodentafel::const_iterator periodentafel::begin(){
|
|---|
| 145 | return elements.begin();
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | periodentafel::const_iterator periodentafel::end(){
|
|---|
| 149 | return elements.end();
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | periodentafel::reverse_iterator periodentafel::rbegin(){
|
|---|
| 153 | return reverse_iterator(elements.end());
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | periodentafel::reverse_iterator periodentafel::rend(){
|
|---|
| 157 | return reverse_iterator(elements.begin());
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | /** Prints period table to given stream.
|
|---|
| 161 | * \param output stream
|
|---|
| 162 | */
|
|---|
| 163 | bool periodentafel::Output(ostream * const output) const
|
|---|
| 164 | {
|
|---|
| 165 | bool result = true;
|
|---|
| 166 | if (output != NULL) {
|
|---|
| 167 | for(const_iterator iter=elements.begin(); iter !=elements.end();++iter){
|
|---|
| 168 | result = result && (*iter).second->Output(output);
|
|---|
| 169 | }
|
|---|
| 170 | return result;
|
|---|
| 171 | } else
|
|---|
| 172 | return false;
|
|---|
| 173 | };
|
|---|
| 174 |
|
|---|
| 175 | /** Prints period table to given stream.
|
|---|
| 176 | * \param *output output stream
|
|---|
| 177 | * \param *checkliste elements table for this molecule
|
|---|
| 178 | */
|
|---|
| 179 | bool periodentafel::Checkout(ostream * const output, const int * const checkliste) const
|
|---|
| 180 | {
|
|---|
| 181 | bool result = true;
|
|---|
| 182 | int No = 1;
|
|---|
| 183 |
|
|---|
| 184 | if (output != NULL) {
|
|---|
| 185 | *output << "# Ion type data (PP = PseudoPotential, Z = atomic number)" << endl;
|
|---|
| 186 | *output << "#Ion_TypeNr.\tAmount\tZ\tRGauss\tL_Max(PP)L_Loc(PP)IonMass\t# chemical name, symbol" << endl;
|
|---|
| 187 | for(const_iterator iter=elements.begin(); iter!=elements.end();++iter){
|
|---|
| 188 | if (((*iter).first < MAX_ELEMENTS) && (checkliste[(*iter).first])) {
|
|---|
| 189 | (*iter).second->No = No;
|
|---|
| 190 | result = result && (*iter).second->Checkout(output, No++, checkliste[(*iter).first]);
|
|---|
| 191 | }
|
|---|
| 192 | }
|
|---|
| 193 | return result;
|
|---|
| 194 | } else
|
|---|
| 195 | return false;
|
|---|
| 196 | };
|
|---|
| 197 |
|
|---|
| 198 | /** Loads element list from file.
|
|---|
| 199 | * \param *path to to standard file names
|
|---|
| 200 | */
|
|---|
| 201 | bool periodentafel::LoadPeriodentafel(const char *path)
|
|---|
| 202 | {
|
|---|
| 203 | ifstream infile;
|
|---|
| 204 | element *ptr;
|
|---|
| 205 | map<atomicNumber_t,element*> parsedElems;
|
|---|
| 206 | bool status = true;
|
|---|
| 207 | bool otherstatus = true;
|
|---|
| 208 | char filename[255];
|
|---|
| 209 |
|
|---|
| 210 | // fill elements DB
|
|---|
| 211 | strncpy(filename, path, MAXSTRINGSIZE);
|
|---|
| 212 | strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
|
|---|
| 213 | strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
|
|---|
| 214 | infile.open(filename);
|
|---|
| 215 | if (infile != NULL) {
|
|---|
| 216 | infile.getline(header1, MAXSTRINGSIZE);
|
|---|
| 217 | infile.getline(header2, MAXSTRINGSIZE); // skip first two header lines
|
|---|
| 218 | Log() << Verbose(0) << "Parsed elements:";
|
|---|
| 219 | while (!infile.eof()) {
|
|---|
| 220 | element *neues = new element;
|
|---|
| 221 | infile >> neues->name;
|
|---|
| 222 | //infile >> ws;
|
|---|
| 223 | infile >> neues->symbol;
|
|---|
| 224 | //infile >> ws;
|
|---|
| 225 | infile >> neues->period;
|
|---|
| 226 | //infile >> ws;
|
|---|
| 227 | infile >> neues->group;
|
|---|
| 228 | //infile >> ws;
|
|---|
| 229 | infile >> neues->block;
|
|---|
| 230 | //infile >> ws;
|
|---|
| 231 | infile >> neues->Z;
|
|---|
| 232 | //infile >> ws;
|
|---|
| 233 | infile >> neues->mass;
|
|---|
| 234 | //infile >> ws;
|
|---|
| 235 | infile >> neues->CovalentRadius;
|
|---|
| 236 | //infile >> ws;
|
|---|
| 237 | infile >> neues->VanDerWaalsRadius;
|
|---|
| 238 | //infile >> ws;
|
|---|
| 239 | infile >> ws;
|
|---|
| 240 | Log() << Verbose(0) << " " << neues->symbol;
|
|---|
| 241 | //neues->Output((ofstream *)&cout);
|
|---|
| 242 | if ((neues->Z > 0) && (neues->Z < MAX_ELEMENTS))
|
|---|
| 243 | parsedElems[neues->getNumber()] = neues;
|
|---|
| 244 | else {
|
|---|
| 245 | Log() << Verbose(0) << "Could not parse element: ";
|
|---|
| 246 | neues->Output((ofstream *)&cout);
|
|---|
| 247 | delete(neues);
|
|---|
| 248 | }
|
|---|
| 249 | }
|
|---|
| 250 | Log() << Verbose(0) << endl;
|
|---|
| 251 | infile.close();
|
|---|
| 252 | infile.clear();
|
|---|
| 253 | } else
|
|---|
| 254 | status = false;
|
|---|
| 255 |
|
|---|
| 256 | // fill valence DB per element
|
|---|
| 257 | strncpy(filename, path, MAXSTRINGSIZE);
|
|---|
| 258 | strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
|
|---|
| 259 | strncat(filename, STANDARDVALENCEDB, MAXSTRINGSIZE-strlen(filename));
|
|---|
| 260 | infile.open(filename);
|
|---|
| 261 | if (infile != NULL) {
|
|---|
| 262 | while (!infile.eof()) {
|
|---|
| 263 | atomicNumber_t Z;
|
|---|
| 264 | infile >> Z;
|
|---|
| 265 | infile >> ws;
|
|---|
| 266 | infile >> parsedElems[Z]->Valence;
|
|---|
| 267 | infile >> ws;
|
|---|
| 268 | //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->Valence << " valence electrons." << endl;
|
|---|
| 269 | }
|
|---|
| 270 | infile.close();
|
|---|
| 271 | infile.clear();
|
|---|
| 272 | } else
|
|---|
| 273 | otherstatus = false;
|
|---|
| 274 |
|
|---|
| 275 | // fill valence DB per element
|
|---|
| 276 | strncpy(filename, path, MAXSTRINGSIZE);
|
|---|
| 277 | strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
|
|---|
| 278 | strncat(filename, STANDARDORBITALDB, MAXSTRINGSIZE-strlen(filename));
|
|---|
| 279 | infile.open(filename);
|
|---|
| 280 | if (infile != NULL) {
|
|---|
| 281 | while (!infile.eof()) {
|
|---|
| 282 | atomicNumber_t Z;
|
|---|
| 283 | infile >> Z;
|
|---|
| 284 | infile >> ws;
|
|---|
| 285 | infile >> parsedElems[Z]->NoValenceOrbitals;
|
|---|
| 286 | infile >> ws;
|
|---|
| 287 | //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
|
|---|
| 288 | }
|
|---|
| 289 | infile.close();
|
|---|
| 290 | infile.clear();
|
|---|
| 291 | } else
|
|---|
| 292 | otherstatus = false;
|
|---|
| 293 |
|
|---|
| 294 | // fill H-BondDistance DB per element
|
|---|
| 295 | strncpy(filename, path, MAXSTRINGSIZE);
|
|---|
| 296 | strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
|
|---|
| 297 | strncat(filename, STANDARDHBONDDISTANCEDB, MAXSTRINGSIZE-strlen(filename));
|
|---|
| 298 | infile.open(filename);
|
|---|
| 299 | if (infile != NULL) {
|
|---|
| 300 | while (!infile.eof()) {
|
|---|
| 301 | atomicNumber_t Z;
|
|---|
| 302 | infile >> Z;
|
|---|
| 303 | ptr = parsedElems[Z];
|
|---|
| 304 | infile >> ws;
|
|---|
| 305 | infile >> ptr->HBondDistance[0];
|
|---|
| 306 | infile >> ptr->HBondDistance[1];
|
|---|
| 307 | infile >> ptr->HBondDistance[2];
|
|---|
| 308 | infile >> ws;
|
|---|
| 309 | //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen." << endl;
|
|---|
| 310 | }
|
|---|
| 311 | infile.close();
|
|---|
| 312 | infile.clear();
|
|---|
| 313 | } else
|
|---|
| 314 | otherstatus = false;
|
|---|
| 315 |
|
|---|
| 316 | // fill H-BondAngle DB per element
|
|---|
| 317 | strncpy(filename, path, MAXSTRINGSIZE);
|
|---|
| 318 | strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
|
|---|
| 319 | strncat(filename, STANDARDHBONDANGLEDB, MAXSTRINGSIZE-strlen(filename));
|
|---|
| 320 | infile.open(filename);
|
|---|
| 321 | if (infile != NULL) {
|
|---|
| 322 | while (!infile.eof()) {
|
|---|
| 323 | atomicNumber_t Z;
|
|---|
| 324 | infile >> Z;
|
|---|
| 325 | ptr = parsedElems[Z];
|
|---|
| 326 | infile >> ws;
|
|---|
| 327 | infile >> ptr->HBondAngle[0];
|
|---|
| 328 | infile >> ptr->HBondAngle[1];
|
|---|
| 329 | infile >> ptr->HBondAngle[2];
|
|---|
| 330 | infile >> ws;
|
|---|
| 331 | //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;
|
|---|
| 332 | }
|
|---|
| 333 | infile.close();
|
|---|
| 334 | } else
|
|---|
| 335 | otherstatus = false;
|
|---|
| 336 |
|
|---|
| 337 | if (otherstatus){
|
|---|
| 338 | map<atomicNumber_t,element*>::iterator iter;
|
|---|
| 339 | for(iter=parsedElems.begin();iter!=parsedElems.end();++iter){
|
|---|
| 340 | AddElement((*iter).second);
|
|---|
| 341 | }
|
|---|
| 342 | }
|
|---|
| 343 | else{
|
|---|
| 344 | eLog() << Verbose(2) << "Something went wrong while parsing the other databases!" << endl;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | return status;
|
|---|
| 348 | };
|
|---|
| 349 |
|
|---|
| 350 | /** Stores element list to file.
|
|---|
| 351 | */
|
|---|
| 352 | bool periodentafel::StorePeriodentafel(const char *path) const
|
|---|
| 353 | {
|
|---|
| 354 | bool result = true;
|
|---|
| 355 | ofstream f;
|
|---|
| 356 | char filename[MAXSTRINGSIZE];
|
|---|
| 357 |
|
|---|
| 358 | strncpy(filename, path, MAXSTRINGSIZE);
|
|---|
| 359 | strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
|
|---|
| 360 | strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
|
|---|
| 361 | f.open(filename);
|
|---|
| 362 | if (f != NULL) {
|
|---|
| 363 | f << header1 << endl;
|
|---|
| 364 | f << header2 << endl;
|
|---|
| 365 | for(const_iterator iter=elements.begin();iter!=elements.end();++iter){
|
|---|
| 366 | result = result && (*iter).second->Output(&f);
|
|---|
| 367 | }
|
|---|
| 368 | f.close();
|
|---|
| 369 | } else
|
|---|
| 370 | result = false;
|
|---|
| 371 | return result;
|
|---|
| 372 | };
|
|---|