| [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;
 | 
|---|
 | 150 |       if (checkliste[walker->Z]) {
 | 
|---|
 | 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.
 | 
|---|
 | 162 |  */
 | 
|---|
 | 163 | bool periodentafel::LoadPeriodentafel()
 | 
|---|
 | 164 | {
 | 
|---|
 | 165 |   ifstream infile;
 | 
|---|
 | 166 |   double tmp;
 | 
|---|
 | 167 |   element *ptr;
 | 
|---|
 | 168 |   
 | 
|---|
 | 169 |   // fill elements DB
 | 
|---|
 | 170 |   infile.open("elements.db");
 | 
|---|
 | 171 |   infile.getline(header1, 255);
 | 
|---|
 | 172 |   infile.getline(header2, 255); // skip first two header lines
 | 
|---|
 | 173 |   while (!infile.eof()) {
 | 
|---|
 | 174 |     element *neues = new element;
 | 
|---|
 | 175 |     infile >> neues->name;
 | 
|---|
 | 176 |     //infile >> ws;
 | 
|---|
 | 177 |     infile >> neues->symbol;
 | 
|---|
 | 178 |     //infile >> ws;
 | 
|---|
 | 179 |     infile >> neues->period;
 | 
|---|
 | 180 |     //infile >> ws;
 | 
|---|
 | 181 |     infile >> neues->group;
 | 
|---|
 | 182 |     //infile >> ws;
 | 
|---|
 | 183 |     infile >> neues->block;
 | 
|---|
 | 184 |     //infile >> ws;
 | 
|---|
 | 185 |     infile >> neues->Z;
 | 
|---|
 | 186 |     //infile >> ws;
 | 
|---|
 | 187 |     infile >> neues->mass;
 | 
|---|
 | 188 |     //infile >> ws;
 | 
|---|
 | 189 |     infile >> neues->CovalentRadius;
 | 
|---|
 | 190 |     //infile >> ws;
 | 
|---|
 | 191 |     infile >> neues->VanDerWaalsRadius;
 | 
|---|
 | 192 |     //infile >> ws;
 | 
|---|
 | 193 |     periodentafel::AddElement(neues);
 | 
|---|
 | 194 |     infile >> ws;
 | 
|---|
 | 195 |     neues->Output((ofstream *)&cout);
 | 
|---|
 | 196 |   }
 | 
|---|
 | 197 |   infile.close();
 | 
|---|
 | 198 |   infile.clear();
 | 
|---|
 | 199 | 
 | 
|---|
 | 200 |   // fill valence DB per element
 | 
|---|
 | 201 |   infile.open("valence.db");
 | 
|---|
 | 202 |   while (!infile.eof()) {
 | 
|---|
 | 203 |         infile >> tmp;
 | 
|---|
 | 204 |         infile >> ws;
 | 
|---|
 | 205 |         infile >> FindElement((int)tmp)->Valence;
 | 
|---|
 | 206 |         infile >> ws;
 | 
|---|
 | 207 |         //cout << Verbose(3) << "Element " << (int)tmp << " has " << find_element((int)tmp)->Valence << " valence electrons." << endl;
 | 
|---|
 | 208 |   }
 | 
|---|
 | 209 |   infile.close();
 | 
|---|
 | 210 |   infile.clear();
 | 
|---|
 | 211 | 
 | 
|---|
 | 212 |   // fill valence DB per element
 | 
|---|
 | 213 |   infile.open("orbitals.db");
 | 
|---|
 | 214 |   while (!infile.eof()) {
 | 
|---|
 | 215 |     infile >> tmp;
 | 
|---|
 | 216 |     infile >> ws;
 | 
|---|
 | 217 |     infile >> FindElement((int)tmp)->NoValenceOrbitals;
 | 
|---|
 | 218 |     infile >> ws;
 | 
|---|
 | 219 |     //cout << Verbose(3) << "Element " << (int)tmp << " has " << find_element((int)tmp)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
 | 
|---|
 | 220 |   }
 | 
|---|
 | 221 |   infile.close();
 | 
|---|
 | 222 |   infile.clear();
 | 
|---|
 | 223 |   
 | 
|---|
 | 224 |   // fill H-BondDistance DB per element
 | 
|---|
 | 225 |   infile.open("Hbonddistance.db");
 | 
|---|
 | 226 |   while (!infile.eof()) {
 | 
|---|
 | 227 |         infile >> tmp;
 | 
|---|
 | 228 |     ptr = FindElement((int)tmp);
 | 
|---|
 | 229 |         infile >> ws;
 | 
|---|
 | 230 |     infile >> ptr->HBondDistance[0];
 | 
|---|
 | 231 |     infile >> ptr->HBondDistance[1];
 | 
|---|
 | 232 |     infile >> ptr->HBondDistance[2];
 | 
|---|
 | 233 |         infile >> ws;
 | 
|---|
 | 234 |     //cout << Verbose(3) << "Element " << (int)tmp << " has " << find_element((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen." << endl;
 | 
|---|
 | 235 |   }
 | 
|---|
 | 236 |   infile.close();
 | 
|---|
 | 237 |   infile.clear();
 | 
|---|
 | 238 |   
 | 
|---|
 | 239 |   // fill H-BondAngle DB per element
 | 
|---|
 | 240 |   infile.open("Hbondangle.db");
 | 
|---|
 | 241 |   while (!infile.eof()) {
 | 
|---|
 | 242 |     infile >> tmp;
 | 
|---|
 | 243 |     ptr = FindElement((int)tmp);
 | 
|---|
 | 244 |     infile >> ws;
 | 
|---|
 | 245 |     infile >> ptr->HBondAngle[0];
 | 
|---|
 | 246 |     infile >> ptr->HBondAngle[1];
 | 
|---|
 | 247 |     infile >> ptr->HBondAngle[2];
 | 
|---|
 | 248 |     infile >> ws;
 | 
|---|
 | 249 |     //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;
 | 
|---|
 | 250 |   }
 | 
|---|
 | 251 |   infile.close();
 | 
|---|
 | 252 |   
 | 
|---|
 | 253 |   return true;
 | 
|---|
 | 254 | };
 | 
|---|
 | 255 | 
 | 
|---|
 | 256 | /** Stores element list to file.
 | 
|---|
 | 257 |  */
 | 
|---|
 | 258 | bool periodentafel::StorePeriodentafel() const
 | 
|---|
 | 259 | {
 | 
|---|
 | 260 |   bool result = true;
 | 
|---|
 | 261 |   ofstream f("elements.db");
 | 
|---|
 | 262 |   f << header1 << endl;
 | 
|---|
 | 263 |   f << header2 << endl;
 | 
|---|
 | 264 |   element *walker = periodentafel::start;
 | 
|---|
 | 265 |   while (walker->next != periodentafel::end) {
 | 
|---|
 | 266 |     walker = walker->next;
 | 
|---|
 | 267 |     result = result && walker->Output(&f);
 | 
|---|
 | 268 |   }
 | 
|---|
 | 269 |   f.close();
 | 
|---|
 | 270 |   return result;
 | 
|---|
 | 271 | };
 | 
|---|