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