| 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 | 
 | 
|---|
| 12 | #include "element.hpp"
 | 
|---|
| 13 | #include "helpers.hpp"
 | 
|---|
| 14 | #include "lists.hpp"
 | 
|---|
| 15 | #include "log.hpp"
 | 
|---|
| 16 | #include "periodentafel.hpp"
 | 
|---|
| 17 | #include "verbose.hpp"
 | 
|---|
| 18 | 
 | 
|---|
| 19 | /************************************* Functions for class periodentafel ***************************/
 | 
|---|
| 20 | 
 | 
|---|
| 21 | /** constructor for class periodentafel
 | 
|---|
| 22 |  * Initialises start and end of list and resets periodentafel::checkliste to false.
 | 
|---|
| 23 |  */
 | 
|---|
| 24 | periodentafel::periodentafel() : start(new element), end(new element)
 | 
|---|
| 25 | {
 | 
|---|
| 26 |   start->previous = NULL;
 | 
|---|
| 27 |   start->next = end;
 | 
|---|
| 28 |   end->previous = start;
 | 
|---|
| 29 |   end->next = NULL;
 | 
|---|
| 30 | };
 | 
|---|
| 31 | 
 | 
|---|
| 32 | /** destructor for class periodentafel
 | 
|---|
| 33 |  * Removes every element and afterwards deletes start and end of list.
 | 
|---|
| 34 |  */
 | 
|---|
| 35 | periodentafel::~periodentafel()
 | 
|---|
| 36 | {
 | 
|---|
| 37 |   CleanupPeriodtable();
 | 
|---|
| 38 |   delete(end);
 | 
|---|
| 39 |   delete(start);
 | 
|---|
| 40 | };
 | 
|---|
| 41 | 
 | 
|---|
| 42 | /** Adds element to period table list
 | 
|---|
| 43 |  * \param *pointer element to be added
 | 
|---|
| 44 |  * \return true - succeeded, false - does not occur
 | 
|---|
| 45 |  */
 | 
|---|
| 46 | bool periodentafel::AddElement(element * const pointer)
 | 
|---|
| 47 | {
 | 
|---|
| 48 |   pointer->sort = &pointer->Z;
 | 
|---|
| 49 |   if (pointer->Z < 1 && pointer->Z >= MAX_ELEMENTS)
 | 
|---|
| 50 |     Log() << Verbose(0) << "Invalid Z number!\n";
 | 
|---|
| 51 |   return add(pointer, end);
 | 
|---|
| 52 | };
 | 
|---|
| 53 | 
 | 
|---|
| 54 | /** Removes element from list.
 | 
|---|
| 55 |  * \param *pointer element to be removed
 | 
|---|
| 56 |  * \return true - succeeded, false - element not found
 | 
|---|
| 57 |  */
 | 
|---|
| 58 | bool periodentafel::RemoveElement(element * const pointer)
 | 
|---|
| 59 | {
 | 
|---|
| 60 |   return remove(pointer, start, end);
 | 
|---|
| 61 | };
 | 
|---|
| 62 | 
 | 
|---|
| 63 | /** Removes every element from the period table.
 | 
|---|
| 64 |  * \return true - succeeded, false - does not occur
 | 
|---|
| 65 |  */
 | 
|---|
| 66 | bool periodentafel::CleanupPeriodtable()
 | 
|---|
| 67 | {
 | 
|---|
| 68 |   return cleanup(start,end);
 | 
|---|
| 69 | };
 | 
|---|
| 70 | 
 | 
|---|
| 71 | /** Finds an element by its atomic number.
 | 
|---|
| 72 |  * If element is not yet in list, returns NULL.
 | 
|---|
| 73 |  * \param Z atomic number
 | 
|---|
| 74 |  * \return pointer to element or NULL if not found
 | 
|---|
| 75 |  */
 | 
|---|
| 76 | element * const periodentafel::FindElement(const int Z) const
 | 
|---|
| 77 | {
 | 
|---|
| 78 |   element *walker = find(&Z, start,end);
 | 
|---|
| 79 |   return(walker);
 | 
|---|
| 80 | };
 | 
|---|
| 81 | 
 | 
|---|
| 82 | /** Finds an element by its atomic number.
 | 
|---|
| 83 |  * If element is not yet in list, datas are asked and stored in database.
 | 
|---|
| 84 |  * \param shorthand chemical symbol of the element, e.g. H for hydrogene
 | 
|---|
| 85 |  * \return pointer to element
 | 
|---|
| 86 |  */
 | 
|---|
| 87 | element * const periodentafel::FindElement(const char * const shorthand) const
 | 
|---|
| 88 | {
 | 
|---|
| 89 |   element *walker =  periodentafel::start;
 | 
|---|
| 90 |   while (walker->next != periodentafel::end) {
 | 
|---|
| 91 |     walker = walker->next;
 | 
|---|
| 92 |     if (strncmp(walker->symbol, shorthand, 3) == 0)
 | 
|---|
| 93 |       return(walker);
 | 
|---|
| 94 |   }
 | 
|---|
| 95 |   return (NULL);
 | 
|---|
| 96 | };
 | 
|---|
| 97 | 
 | 
|---|
| 98 | /** Asks for element number and returns pointer to element
 | 
|---|
| 99 |  */
 | 
|---|
| 100 | element * const periodentafel::AskElement() const
 | 
|---|
| 101 | {
 | 
|---|
| 102 |   element *walker = NULL;
 | 
|---|
| 103 |   int Z;
 | 
|---|
| 104 |   do {
 | 
|---|
| 105 |     Log() << Verbose(0) << "Atomic number Z: ";
 | 
|---|
| 106 |     cin >> Z;
 | 
|---|
| 107 |     walker = this->FindElement(Z);  // give type
 | 
|---|
| 108 |   } while (walker == NULL);
 | 
|---|
| 109 |   return walker;
 | 
|---|
| 110 | };
 | 
|---|
| 111 | 
 | 
|---|
| 112 | /** Asks for element and if not found, presents mask to enter info.
 | 
|---|
| 113 |  * \return pointer to either present or newly created element
 | 
|---|
| 114 |  */
 | 
|---|
| 115 | element * const periodentafel::EnterElement()
 | 
|---|
| 116 | {
 | 
|---|
| 117 |   element *walker = NULL;
 | 
|---|
| 118 |   int Z = -1;
 | 
|---|
| 119 |   Log() << Verbose(0) << "Atomic number: " << Z << endl;
 | 
|---|
| 120 |   cin >> Z;
 | 
|---|
| 121 |   walker = FindElement(Z);
 | 
|---|
| 122 |   if (walker == NULL) {
 | 
|---|
| 123 |     Log() << Verbose(0) << "Element not found in database, please enter." << endl;
 | 
|---|
| 124 |     walker = new element;
 | 
|---|
| 125 |     walker->Z = Z;
 | 
|---|
| 126 |     Log() << Verbose(0) << "Mass: " << endl;
 | 
|---|
| 127 |     cin >> walker->mass;
 | 
|---|
| 128 |     Log() << Verbose(0) << "Name [max 64 chars]: " << endl;
 | 
|---|
| 129 |     cin >> walker->name;
 | 
|---|
| 130 |     Log() << Verbose(0) << "Short form [max 3 chars]: " << endl;
 | 
|---|
| 131 |     cin >> walker->symbol;
 | 
|---|
| 132 |     periodentafel::AddElement(walker);
 | 
|---|
| 133 |   }
 | 
|---|
| 134 |   return(walker);
 | 
|---|
| 135 | };
 | 
|---|
| 136 | 
 | 
|---|
| 137 | /** Prints period table to given stream.
 | 
|---|
| 138 |  * \param output stream
 | 
|---|
| 139 |  */
 | 
|---|
| 140 | bool periodentafel::Output(ofstream * const output) const
 | 
|---|
| 141 | {
 | 
|---|
| 142 |   bool result = true;
 | 
|---|
| 143 |   element *walker = start;
 | 
|---|
| 144 |   if (output != NULL) {
 | 
|---|
| 145 |     while (walker->next != end) {
 | 
|---|
| 146 |       walker = walker->next;
 | 
|---|
| 147 |       result = result && walker->Output(output);
 | 
|---|
| 148 |     }
 | 
|---|
| 149 |     return result;
 | 
|---|
| 150 |   } else
 | 
|---|
| 151 |     return false;
 | 
|---|
| 152 | };
 | 
|---|
| 153 | 
 | 
|---|
| 154 | /** Prints period table to given stream.
 | 
|---|
| 155 |  * \param *output output stream
 | 
|---|
| 156 |  * \param *checkliste elements table for this molecule
 | 
|---|
| 157 |  */
 | 
|---|
| 158 | bool periodentafel::Checkout(ofstream * const output, const int * const checkliste) const
 | 
|---|
| 159 | {
 | 
|---|
| 160 |   element *walker = start;
 | 
|---|
| 161 |   bool result = true;
 | 
|---|
| 162 |   int No = 1;
 | 
|---|
| 163 | 
 | 
|---|
| 164 |   if (output != NULL) {
 | 
|---|
| 165 |     *output << "# Ion type data (PP = PseudoPotential, Z = atomic number)" << endl;
 | 
|---|
| 166 |     *output << "#Ion_TypeNr.\tAmount\tZ\tRGauss\tL_Max(PP)L_Loc(PP)IonMass\t# chemical name, symbol" << endl;
 | 
|---|
| 167 |     while (walker->next != end) {
 | 
|---|
| 168 |       walker = walker->next;
 | 
|---|
| 169 |       if ((walker != NULL) && (walker->Z > 0) && (walker->Z < MAX_ELEMENTS) && (checkliste[walker->Z])) {
 | 
|---|
| 170 |         walker->No = No;
 | 
|---|
| 171 |         result = result && walker->Checkout(output, No++, checkliste[walker->Z]);
 | 
|---|
| 172 |       }
 | 
|---|
| 173 |     }
 | 
|---|
| 174 |     return result;
 | 
|---|
| 175 |   } else
 | 
|---|
| 176 |     return false;
 | 
|---|
| 177 | };
 | 
|---|
| 178 | 
 | 
|---|
| 179 | /** Loads element list from file.
 | 
|---|
| 180 |  * \param *path to to standard file names
 | 
|---|
| 181 |  */
 | 
|---|
| 182 | bool periodentafel::LoadPeriodentafel(const char *path)
 | 
|---|
| 183 | {
 | 
|---|
| 184 |   ifstream infile;
 | 
|---|
| 185 |   double tmp;
 | 
|---|
| 186 |   element *ptr;
 | 
|---|
| 187 |   bool status = true;
 | 
|---|
| 188 |   bool otherstatus = true;
 | 
|---|
| 189 |   char filename[255];
 | 
|---|
| 190 | 
 | 
|---|
| 191 |   // fill elements DB
 | 
|---|
| 192 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
| 193 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
| 194 |   strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
| 195 |   infile.open(filename);
 | 
|---|
| 196 |   if (infile != NULL) {
 | 
|---|
| 197 |     infile.getline(header1, MAXSTRINGSIZE);
 | 
|---|
| 198 |     infile.getline(header2, MAXSTRINGSIZE); // skip first two header lines
 | 
|---|
| 199 |     Log() << Verbose(0) <<  "Parsed elements:";
 | 
|---|
| 200 |     while (!infile.eof()) {
 | 
|---|
| 201 |       element *neues = new element;
 | 
|---|
| 202 |       infile >> neues->name;
 | 
|---|
| 203 |       //infile >> ws;
 | 
|---|
| 204 |       infile >> neues->symbol;
 | 
|---|
| 205 |       //infile >> ws;
 | 
|---|
| 206 |       infile >> neues->period;
 | 
|---|
| 207 |       //infile >> ws;
 | 
|---|
| 208 |       infile >> neues->group;
 | 
|---|
| 209 |       //infile >> ws;
 | 
|---|
| 210 |       infile >> neues->block;
 | 
|---|
| 211 |       //infile >> ws;
 | 
|---|
| 212 |       infile >> neues->Z;
 | 
|---|
| 213 |       //infile >> ws;
 | 
|---|
| 214 |       infile >> neues->mass;
 | 
|---|
| 215 |       //infile >> ws;
 | 
|---|
| 216 |       infile >> neues->CovalentRadius;
 | 
|---|
| 217 |       //infile >> ws;
 | 
|---|
| 218 |       infile >> neues->VanDerWaalsRadius;
 | 
|---|
| 219 |       //infile >> ws;
 | 
|---|
| 220 |       infile >> ws;
 | 
|---|
| 221 |       Log() << Verbose(0) << " " << neues->symbol;
 | 
|---|
| 222 |       //neues->Output((ofstream *)&cout);
 | 
|---|
| 223 |       if ((neues->Z > 0) && (neues->Z < MAX_ELEMENTS))
 | 
|---|
| 224 |         periodentafel::AddElement(neues);
 | 
|---|
| 225 |       else {
 | 
|---|
| 226 |         Log() << Verbose(0) << "Could not parse element: ";
 | 
|---|
| 227 |         neues->Output((ofstream *)&cout);
 | 
|---|
| 228 |         delete(neues);
 | 
|---|
| 229 |       }
 | 
|---|
| 230 |     }
 | 
|---|
| 231 |     Log() << Verbose(0) << endl;
 | 
|---|
| 232 |     infile.close();
 | 
|---|
| 233 |     infile.clear();
 | 
|---|
| 234 |   } else
 | 
|---|
| 235 |     status = false;
 | 
|---|
| 236 | 
 | 
|---|
| 237 |   // fill valence DB per element
 | 
|---|
| 238 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
| 239 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
| 240 |   strncat(filename, STANDARDVALENCEDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
| 241 |   infile.open(filename);
 | 
|---|
| 242 |   if (infile != NULL) {
 | 
|---|
| 243 |     while (!infile.eof()) {
 | 
|---|
| 244 |       infile >> tmp;
 | 
|---|
| 245 |       infile >> ws;
 | 
|---|
| 246 |       infile >> FindElement((int)tmp)->Valence;
 | 
|---|
| 247 |       infile >> ws;
 | 
|---|
| 248 |       //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->Valence << " valence electrons." << endl;
 | 
|---|
| 249 |     }
 | 
|---|
| 250 |     infile.close();
 | 
|---|
| 251 |     infile.clear();
 | 
|---|
| 252 |   } else
 | 
|---|
| 253 |     otherstatus = false;
 | 
|---|
| 254 | 
 | 
|---|
| 255 |   // fill valence DB per element
 | 
|---|
| 256 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
| 257 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
| 258 |   strncat(filename, STANDARDORBITALDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
| 259 |   infile.open(filename);
 | 
|---|
| 260 |   if (infile != NULL) {
 | 
|---|
| 261 |     while (!infile.eof()) {
 | 
|---|
| 262 |       infile >> tmp;
 | 
|---|
| 263 |       infile >> ws;
 | 
|---|
| 264 |       infile >> FindElement((int)tmp)->NoValenceOrbitals;
 | 
|---|
| 265 |       infile >> ws;
 | 
|---|
| 266 |       //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
 | 
|---|
| 267 |     }
 | 
|---|
| 268 |     infile.close();
 | 
|---|
| 269 |     infile.clear();
 | 
|---|
| 270 |   } else
 | 
|---|
| 271 |     otherstatus = false;
 | 
|---|
| 272 | 
 | 
|---|
| 273 |   // fill H-BondDistance DB per element
 | 
|---|
| 274 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
| 275 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
| 276 |   strncat(filename, STANDARDHBONDDISTANCEDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
| 277 |   infile.open(filename);
 | 
|---|
| 278 |   if (infile != NULL) {
 | 
|---|
| 279 |     while (!infile.eof()) {
 | 
|---|
| 280 |       infile >> tmp;
 | 
|---|
| 281 |       ptr = FindElement((int)tmp);
 | 
|---|
| 282 |       infile >> ws;
 | 
|---|
| 283 |       infile >> ptr->HBondDistance[0];
 | 
|---|
| 284 |       infile >> ptr->HBondDistance[1];
 | 
|---|
| 285 |       infile >> ptr->HBondDistance[2];
 | 
|---|
| 286 |       infile >> ws;
 | 
|---|
| 287 |       //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen." << endl;
 | 
|---|
| 288 |     }
 | 
|---|
| 289 |     infile.close();
 | 
|---|
| 290 |     infile.clear();
 | 
|---|
| 291 |   } else
 | 
|---|
| 292 |     otherstatus = false;
 | 
|---|
| 293 | 
 | 
|---|
| 294 |   // fill H-BondAngle DB per element
 | 
|---|
| 295 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
| 296 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
| 297 |   strncat(filename, STANDARDHBONDANGLEDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
| 298 |   infile.open(filename);
 | 
|---|
| 299 |   if (infile != NULL) {
 | 
|---|
| 300 |     while (!infile.eof()) {
 | 
|---|
| 301 |       infile >> tmp;
 | 
|---|
| 302 |       ptr = FindElement((int)tmp);
 | 
|---|
| 303 |       infile >> ws;
 | 
|---|
| 304 |       infile >> ptr->HBondAngle[0];
 | 
|---|
| 305 |       infile >> ptr->HBondAngle[1];
 | 
|---|
| 306 |       infile >> ptr->HBondAngle[2];
 | 
|---|
| 307 |       infile >> ws;
 | 
|---|
| 308 |       //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;
 | 
|---|
| 309 |     }
 | 
|---|
| 310 |     infile.close();
 | 
|---|
| 311 |   } else
 | 
|---|
| 312 |     otherstatus = false;
 | 
|---|
| 313 | 
 | 
|---|
| 314 |   if (!otherstatus)
 | 
|---|
| 315 |     eLog() << Verbose(2) << "Something went wrong while parsing the other databases!" << endl;
 | 
|---|
| 316 | 
 | 
|---|
| 317 |   return status;
 | 
|---|
| 318 | };
 | 
|---|
| 319 | 
 | 
|---|
| 320 | /** Stores element list to file.
 | 
|---|
| 321 |  */
 | 
|---|
| 322 | bool periodentafel::StorePeriodentafel(const char *path) const
 | 
|---|
| 323 | {
 | 
|---|
| 324 |   bool result = true;
 | 
|---|
| 325 |   ofstream f;
 | 
|---|
| 326 |   char filename[MAXSTRINGSIZE];
 | 
|---|
| 327 | 
 | 
|---|
| 328 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
| 329 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
| 330 |   strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
| 331 |   f.open(filename);
 | 
|---|
| 332 |   if (f != NULL) {
 | 
|---|
| 333 |     f << header1 << endl;
 | 
|---|
| 334 |     f << header2 << endl;
 | 
|---|
| 335 |     element *walker = periodentafel::start;
 | 
|---|
| 336 |     while (walker->next != periodentafel::end) {
 | 
|---|
| 337 |       walker = walker->next;
 | 
|---|
| 338 |       result = result && walker->Output(&f);
 | 
|---|
| 339 |     }
 | 
|---|
| 340 |     f.close();
 | 
|---|
| 341 |   } else
 | 
|---|
| 342 |     result = false;
 | 
|---|
| 343 |   return result;
 | 
|---|
| 344 | };
 | 
|---|