[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
|
---|
| 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
|
---|
[a67d19] | 221 | DoLog(0) && (Log() << Verbose(0) << "Parsed elements:");
|
---|
[042f82] | 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;
|
---|
[a67d19] | 243 | DoLog(0) && (Log() << Verbose(0) << " " << neues->symbol);
|
---|
[042f82] | 244 | //neues->Output((ofstream *)&cout);
|
---|
| 245 | if ((neues->Z > 0) && (neues->Z < MAX_ELEMENTS))
|
---|
[ead4e6] | 246 | parsedElems[neues->getNumber()] = neues;
|
---|
[042f82] | 247 | else {
|
---|
[a67d19] | 248 | DoLog(0) && (Log() << Verbose(0) << "Could not parse element: ");
|
---|
[042f82] | 249 | neues->Output((ofstream *)&cout);
|
---|
[db6bf74] | 250 | delete(neues);
|
---|
[042f82] | 251 | }
|
---|
| 252 | }
|
---|
[a67d19] | 253 | DoLog(0) && (Log() << Verbose(0) << endl);
|
---|
[042f82] | 254 | infile.close();
|
---|
| 255 | infile.clear();
|
---|
| 256 | } else
|
---|
| 257 | status = false;
|
---|
[6ac7ee] | 258 |
|
---|
[042f82] | 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()) {
|
---|
[ead4e6] | 266 | atomicNumber_t Z;
|
---|
| 267 | infile >> Z;
|
---|
[042f82] | 268 | infile >> ws;
|
---|
[ead4e6] | 269 | infile >> parsedElems[Z]->Valence;
|
---|
[042f82] | 270 | infile >> ws;
|
---|
[e138de] | 271 | //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->Valence << " valence electrons." << endl;
|
---|
[042f82] | 272 | }
|
---|
| 273 | infile.close();
|
---|
| 274 | infile.clear();
|
---|
| 275 | } else
|
---|
| 276 | otherstatus = false;
|
---|
[6ac7ee] | 277 |
|
---|
[042f82] | 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()) {
|
---|
[ead4e6] | 285 | atomicNumber_t Z;
|
---|
| 286 | infile >> Z;
|
---|
[042f82] | 287 | infile >> ws;
|
---|
[ead4e6] | 288 | infile >> parsedElems[Z]->NoValenceOrbitals;
|
---|
[042f82] | 289 | infile >> ws;
|
---|
[e138de] | 290 | //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
|
---|
[042f82] | 291 | }
|
---|
| 292 | infile.close();
|
---|
| 293 | infile.clear();
|
---|
| 294 | } else
|
---|
| 295 | otherstatus = false;
|
---|
[6ac7ee] | 296 |
|
---|
[042f82] | 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()) {
|
---|
[ead4e6] | 304 | atomicNumber_t Z;
|
---|
| 305 | infile >> Z;
|
---|
| 306 | ptr = parsedElems[Z];
|
---|
[042f82] | 307 | infile >> ws;
|
---|
| 308 | infile >> ptr->HBondDistance[0];
|
---|
| 309 | infile >> ptr->HBondDistance[1];
|
---|
| 310 | infile >> ptr->HBondDistance[2];
|
---|
| 311 | infile >> ws;
|
---|
[e138de] | 312 | //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen." << endl;
|
---|
[042f82] | 313 | }
|
---|
| 314 | infile.close();
|
---|
| 315 | infile.clear();
|
---|
| 316 | } else
|
---|
| 317 | otherstatus = false;
|
---|
[6ac7ee] | 318 |
|
---|
[042f82] | 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()) {
|
---|
[ead4e6] | 326 | atomicNumber_t Z;
|
---|
| 327 | infile >> Z;
|
---|
| 328 | ptr = parsedElems[Z];
|
---|
[042f82] | 329 | infile >> ws;
|
---|
| 330 | infile >> ptr->HBondAngle[0];
|
---|
| 331 | infile >> ptr->HBondAngle[1];
|
---|
| 332 | infile >> ptr->HBondAngle[2];
|
---|
| 333 | infile >> ws;
|
---|
[e138de] | 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;
|
---|
[042f82] | 335 | }
|
---|
| 336 | infile.close();
|
---|
| 337 | } else
|
---|
| 338 | otherstatus = false;
|
---|
[6ac7ee] | 339 |
|
---|
[ead4e6] | 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{
|
---|
[58ed4a] | 347 | DoeLog(2) && (eLog()<< Verbose(2) << "Something went wrong while parsing the other databases!" << endl);
|
---|
[745a85] | 348 | map<atomicNumber_t,element*>::iterator iter;
|
---|
| 349 | for(iter=parsedElems.begin();iter!=parsedElems.end();++iter){
|
---|
| 350 | AddElement((*iter).second);
|
---|
| 351 | }
|
---|
[ead4e6] | 352 | }
|
---|
[6ac7ee] | 353 |
|
---|
[042f82] | 354 | return status;
|
---|
[6ac7ee] | 355 | };
|
---|
| 356 |
|
---|
| 357 | /** Stores element list to file.
|
---|
| 358 | */
|
---|
[989bf6] | 359 | bool periodentafel::StorePeriodentafel(const char *path) const
|
---|
[6ac7ee] | 360 | {
|
---|
[042f82] | 361 | bool result = true;
|
---|
| 362 | ofstream f;
|
---|
| 363 | char filename[MAXSTRINGSIZE];
|
---|
[6ac7ee] | 364 |
|
---|
[042f82] | 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;
|
---|
[ead4e6] | 372 | for(const_iterator iter=elements.begin();iter!=elements.end();++iter){
|
---|
| 373 | result = result && (*iter).second->Output(&f);
|
---|
[042f82] | 374 | }
|
---|
| 375 | f.close();
|
---|
| 376 | } else
|
---|
| 377 | result = false;
|
---|
| 378 | return result;
|
---|
[6ac7ee] | 379 | };
|
---|