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