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