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