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