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