| [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>
 | 
|---|
 | 11 | 
 | 
|---|
| [f66195] | 12 | #include "element.hpp"
 | 
|---|
| [cd4ccc] | 13 | #include "helpers.hpp"
 | 
|---|
| [f66195] | 14 | #include "lists.hpp"
 | 
|---|
| [e138de] | 15 | #include "log.hpp"
 | 
|---|
| [6ac7ee] | 16 | #include "periodentafel.hpp"
 | 
|---|
| [cd4ccc] | 17 | #include "verbose.hpp"
 | 
|---|
| [6ac7ee] | 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 |  */
 | 
|---|
| [fb73b8] | 24 | periodentafel::periodentafel() : start(new element), end(new element)
 | 
|---|
| [6ac7ee] | 25 | {
 | 
|---|
| [042f82] | 26 |   start->previous = NULL;
 | 
|---|
 | 27 |   start->next = end;
 | 
|---|
 | 28 |   end->previous = start;
 | 
|---|
 | 29 |   end->next = NULL;
 | 
|---|
| [6ac7ee] | 30 | };
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | /** destructor for class periodentafel
 | 
|---|
 | 33 |  * Removes every element and afterwards deletes start and end of list.
 | 
|---|
 | 34 |  */
 | 
|---|
 | 35 | periodentafel::~periodentafel()
 | 
|---|
 | 36 | {
 | 
|---|
| [042f82] | 37 |   CleanupPeriodtable();
 | 
|---|
 | 38 |   delete(end);
 | 
|---|
 | 39 |   delete(start);
 | 
|---|
| [6ac7ee] | 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 |  */
 | 
|---|
| [fb73b8] | 46 | bool periodentafel::AddElement(element * const pointer)
 | 
|---|
| [6ac7ee] | 47 | {
 | 
|---|
| [042f82] | 48 |   pointer->sort = &pointer->Z;
 | 
|---|
 | 49 |   if (pointer->Z < 1 && pointer->Z >= MAX_ELEMENTS)
 | 
|---|
| [e138de] | 50 |     Log() << Verbose(0) << "Invalid Z number!\n";
 | 
|---|
| [042f82] | 51 |   return add(pointer, end);
 | 
|---|
| [6ac7ee] | 52 | };
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 | /** Removes element from list.
 | 
|---|
 | 55 |  * \param *pointer element to be removed
 | 
|---|
 | 56 |  * \return true - succeeded, false - element not found
 | 
|---|
 | 57 |  */
 | 
|---|
| [fb73b8] | 58 | bool periodentafel::RemoveElement(element * const pointer)
 | 
|---|
| [6ac7ee] | 59 | {
 | 
|---|
| [042f82] | 60 |   return remove(pointer, start, end);
 | 
|---|
| [6ac7ee] | 61 | };
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 | /** Removes every element from the period table.
 | 
|---|
 | 64 |  * \return true - succeeded, false - does not occur
 | 
|---|
 | 65 |  */
 | 
|---|
 | 66 | bool periodentafel::CleanupPeriodtable()
 | 
|---|
 | 67 | {
 | 
|---|
| [042f82] | 68 |   return cleanup(start,end);
 | 
|---|
| [6ac7ee] | 69 | };
 | 
|---|
 | 70 | 
 | 
|---|
 | 71 | /** Finds an element by its atomic number.
 | 
|---|
| [fb73b8] | 72 |  * If element is not yet in list, returns NULL.
 | 
|---|
| [6ac7ee] | 73 |  * \param Z atomic number
 | 
|---|
| [fb73b8] | 74 |  * \return pointer to element or NULL if not found
 | 
|---|
| [6ac7ee] | 75 |  */
 | 
|---|
| [fb73b8] | 76 | element * const periodentafel::FindElement(const int Z) const
 | 
|---|
| [6ac7ee] | 77 | {
 | 
|---|
| [042f82] | 78 |   element *walker = find(&Z, start,end);
 | 
|---|
 | 79 |   return(walker);
 | 
|---|
| [6ac7ee] | 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 |  */
 | 
|---|
| [fb73b8] | 87 | element * const periodentafel::FindElement(const char * const shorthand) const
 | 
|---|
| [6ac7ee] | 88 | {
 | 
|---|
| [042f82] | 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);
 | 
|---|
| [6ac7ee] | 96 | };
 | 
|---|
 | 97 | 
 | 
|---|
 | 98 | /** Asks for element number and returns pointer to element
 | 
|---|
 | 99 |  */
 | 
|---|
| [fb73b8] | 100 | element * const periodentafel::AskElement() const
 | 
|---|
| [6ac7ee] | 101 | {
 | 
|---|
| [042f82] | 102 |   element *walker = NULL;
 | 
|---|
 | 103 |   int Z;
 | 
|---|
 | 104 |   do {
 | 
|---|
| [e138de] | 105 |     Log() << Verbose(0) << "Atomic number Z: ";
 | 
|---|
| [042f82] | 106 |     cin >> Z;
 | 
|---|
 | 107 |     walker = this->FindElement(Z);  // give type
 | 
|---|
 | 108 |   } while (walker == NULL);
 | 
|---|
 | 109 |   return walker;
 | 
|---|
| [6ac7ee] | 110 | };
 | 
|---|
 | 111 | 
 | 
|---|
| [fb73b8] | 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;
 | 
|---|
| [e138de] | 119 |   Log() << Verbose(0) << "Atomic number: " << Z << endl;
 | 
|---|
| [fb73b8] | 120 |   cin >> Z;
 | 
|---|
 | 121 |   walker = FindElement(Z);
 | 
|---|
 | 122 |   if (walker == NULL) {
 | 
|---|
| [e138de] | 123 |     Log() << Verbose(0) << "Element not found in database, please enter." << endl;
 | 
|---|
| [fb73b8] | 124 |     walker = new element;
 | 
|---|
 | 125 |     walker->Z = Z;
 | 
|---|
| [e138de] | 126 |     Log() << Verbose(0) << "Mass: " << endl;
 | 
|---|
| [fb73b8] | 127 |     cin >> walker->mass;
 | 
|---|
| [e138de] | 128 |     Log() << Verbose(0) << "Name [max 64 chars]: " << endl;
 | 
|---|
| [fb73b8] | 129 |     cin >> walker->name;
 | 
|---|
| [e138de] | 130 |     Log() << Verbose(0) << "Short form [max 3 chars]: " << endl;
 | 
|---|
| [fb73b8] | 131 |     cin >> walker->symbol;
 | 
|---|
 | 132 |     periodentafel::AddElement(walker);
 | 
|---|
 | 133 |   }
 | 
|---|
 | 134 |   return(walker);
 | 
|---|
 | 135 | };
 | 
|---|
 | 136 | 
 | 
|---|
| [6ac7ee] | 137 | /** Prints period table to given stream.
 | 
|---|
 | 138 |  * \param output stream
 | 
|---|
 | 139 |  */
 | 
|---|
| [fb73b8] | 140 | bool periodentafel::Output(ofstream * const output) const
 | 
|---|
| [6ac7ee] | 141 | {
 | 
|---|
| [042f82] | 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;
 | 
|---|
| [6ac7ee] | 152 | };
 | 
|---|
 | 153 | 
 | 
|---|
 | 154 | /** Prints period table to given stream.
 | 
|---|
 | 155 |  * \param *output output stream
 | 
|---|
 | 156 |  * \param *checkliste elements table for this molecule
 | 
|---|
 | 157 |  */
 | 
|---|
| [fb73b8] | 158 | bool periodentafel::Checkout(ofstream * const output, const int * const checkliste) const
 | 
|---|
| [6ac7ee] | 159 | {
 | 
|---|
| [042f82] | 160 |   element *walker = start;
 | 
|---|
 | 161 |   bool result = true;
 | 
|---|
 | 162 |   int No = 1;
 | 
|---|
| [6ac7ee] | 163 | 
 | 
|---|
| [042f82] | 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;
 | 
|---|
| [6ac7ee] | 177 | };
 | 
|---|
 | 178 | 
 | 
|---|
 | 179 | /** Loads element list from file.
 | 
|---|
 | 180 |  * \param *path to to standard file names
 | 
|---|
 | 181 |  */
 | 
|---|
| [989bf6] | 182 | bool periodentafel::LoadPeriodentafel(const char *path)
 | 
|---|
| [6ac7ee] | 183 | {
 | 
|---|
| [042f82] | 184 |   ifstream infile;
 | 
|---|
 | 185 |   double tmp;
 | 
|---|
 | 186 |   element *ptr;
 | 
|---|
 | 187 |   bool status = true;
 | 
|---|
 | 188 |   bool otherstatus = true;
 | 
|---|
 | 189 |   char filename[255];
 | 
|---|
| [6ac7ee] | 190 | 
 | 
|---|
| [042f82] | 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
 | 
|---|
| [e138de] | 199 |     Log() << Verbose(0) <<  "Parsed elements:";
 | 
|---|
| [042f82] | 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;
 | 
|---|
| [e138de] | 221 |       Log() << Verbose(0) << " " << neues->symbol;
 | 
|---|
| [042f82] | 222 |       //neues->Output((ofstream *)&cout);
 | 
|---|
 | 223 |       if ((neues->Z > 0) && (neues->Z < MAX_ELEMENTS))
 | 
|---|
 | 224 |         periodentafel::AddElement(neues);
 | 
|---|
 | 225 |       else {
 | 
|---|
| [e138de] | 226 |         Log() << Verbose(0) << "Could not parse element: ";
 | 
|---|
| [042f82] | 227 |         neues->Output((ofstream *)&cout);
 | 
|---|
| [db6bf74] | 228 |         delete(neues);
 | 
|---|
| [042f82] | 229 |       }
 | 
|---|
 | 230 |     }
 | 
|---|
| [e138de] | 231 |     Log() << Verbose(0) << endl;
 | 
|---|
| [042f82] | 232 |     infile.close();
 | 
|---|
 | 233 |     infile.clear();
 | 
|---|
 | 234 |   } else
 | 
|---|
 | 235 |     status = false;
 | 
|---|
| [6ac7ee] | 236 | 
 | 
|---|
| [042f82] | 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;
 | 
|---|
| [e138de] | 248 |       //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->Valence << " valence electrons." << endl;
 | 
|---|
| [042f82] | 249 |     }
 | 
|---|
 | 250 |     infile.close();
 | 
|---|
 | 251 |     infile.clear();
 | 
|---|
 | 252 |   } else
 | 
|---|
 | 253 |     otherstatus = false;
 | 
|---|
| [6ac7ee] | 254 | 
 | 
|---|
| [042f82] | 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;
 | 
|---|
| [e138de] | 266 |       //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
 | 
|---|
| [042f82] | 267 |     }
 | 
|---|
 | 268 |     infile.close();
 | 
|---|
 | 269 |     infile.clear();
 | 
|---|
 | 270 |   } else
 | 
|---|
 | 271 |     otherstatus = false;
 | 
|---|
| [6ac7ee] | 272 | 
 | 
|---|
| [042f82] | 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;
 | 
|---|
| [e138de] | 287 |       //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen." << endl;
 | 
|---|
| [042f82] | 288 |     }
 | 
|---|
 | 289 |     infile.close();
 | 
|---|
 | 290 |     infile.clear();
 | 
|---|
 | 291 |   } else
 | 
|---|
 | 292 |     otherstatus = false;
 | 
|---|
| [6ac7ee] | 293 | 
 | 
|---|
| [042f82] | 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;
 | 
|---|
| [e138de] | 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;
 | 
|---|
| [042f82] | 309 |     }
 | 
|---|
 | 310 |     infile.close();
 | 
|---|
 | 311 |   } else
 | 
|---|
 | 312 |     otherstatus = false;
 | 
|---|
| [6ac7ee] | 313 | 
 | 
|---|
| [042f82] | 314 |   if (!otherstatus)
 | 
|---|
| [717e0c] | 315 |     eLog() << Verbose(2) << "Something went wrong while parsing the other databases!" << endl;
 | 
|---|
| [6ac7ee] | 316 | 
 | 
|---|
| [042f82] | 317 |   return status;
 | 
|---|
| [6ac7ee] | 318 | };
 | 
|---|
 | 319 | 
 | 
|---|
 | 320 | /** Stores element list to file.
 | 
|---|
 | 321 |  */
 | 
|---|
| [989bf6] | 322 | bool periodentafel::StorePeriodentafel(const char *path) const
 | 
|---|
| [6ac7ee] | 323 | {
 | 
|---|
| [042f82] | 324 |   bool result = true;
 | 
|---|
 | 325 |   ofstream f;
 | 
|---|
 | 326 |   char filename[MAXSTRINGSIZE];
 | 
|---|
| [6ac7ee] | 327 | 
 | 
|---|
| [042f82] | 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;
 | 
|---|
| [6ac7ee] | 344 | };
 | 
|---|