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