| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
 | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [6ac7ee] | 8 | /** \file periodentafel.cpp
 | 
|---|
 | 9 |  *
 | 
|---|
 | 10 |  * Function implementations for the class periodentafel.
 | 
|---|
 | 11 |  *
 | 
|---|
 | 12 |  */
 | 
|---|
 | 13 | 
 | 
|---|
| [bf3817] | 14 | // include config.h
 | 
|---|
 | 15 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 16 | #include <config.h>
 | 
|---|
 | 17 | #endif
 | 
|---|
| [112b09] | 18 | 
 | 
|---|
| [ad011c] | 19 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [6ac7ee] | 20 | 
 | 
|---|
| [cd4ccc] | 21 | #include <iomanip>
 | 
|---|
| [4eb4fe] | 22 | #include <iostream>
 | 
|---|
| [cd4ccc] | 23 | #include <fstream>
 | 
|---|
| [49e1ae] | 24 | #include <cstring>
 | 
|---|
| [cd4ccc] | 25 | 
 | 
|---|
| [ad011c] | 26 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [f66195] | 27 | #include "element.hpp"
 | 
|---|
| [4eb4fe] | 28 | #include "elements_db.hpp"
 | 
|---|
| [952f38] | 29 | #include "Helpers/helpers.hpp"
 | 
|---|
| [ad011c] | 30 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [6ac7ee] | 31 | #include "periodentafel.hpp"
 | 
|---|
| [ad011c] | 32 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| [6ac7ee] | 33 | 
 | 
|---|
| [ead4e6] | 34 | using namespace std;
 | 
|---|
 | 35 | 
 | 
|---|
| [6ac7ee] | 36 | /************************************* Functions for class periodentafel ***************************/
 | 
|---|
 | 37 | 
 | 
|---|
 | 38 | /** constructor for class periodentafel
 | 
|---|
 | 39 |  * Initialises start and end of list and resets periodentafel::checkliste to false.
 | 
|---|
 | 40 |  */
 | 
|---|
| [ead4e6] | 41 | periodentafel::periodentafel()
 | 
|---|
| [4eb4fe] | 42 | {
 | 
|---|
| [f34c23] | 43 |   {
 | 
|---|
 | 44 |     stringstream input(elementsDB,ios_base::in);
 | 
|---|
| [b2ae3b] | 45 | #ifndef NDEBUG
 | 
|---|
 | 46 |     bool status =
 | 
|---|
 | 47 | #endif
 | 
|---|
 | 48 |         LoadElementsDatabase(input);
 | 
|---|
| [f34c23] | 49 |     ASSERT(status,  "General element initialization failed");
 | 
|---|
 | 50 |   }
 | 
|---|
| [67c92b] | 51 |   {
 | 
|---|
 | 52 |     stringstream input(ElectronegativitiesDB,ios_base::in);
 | 
|---|
 | 53 | #ifndef NDEBUG
 | 
|---|
 | 54 |     bool status =
 | 
|---|
 | 55 | #endif
 | 
|---|
 | 56 |         LoadElectronegativityDatabase(input);
 | 
|---|
 | 57 |     ASSERT(status, "Electronegativities entry of element initialization failed");
 | 
|---|
 | 58 |   }
 | 
|---|
| [f34c23] | 59 |   {
 | 
|---|
 | 60 |     stringstream input(valenceDB,ios_base::in);
 | 
|---|
| [b2ae3b] | 61 | #ifndef NDEBUG
 | 
|---|
 | 62 |     bool status =
 | 
|---|
 | 63 | #endif
 | 
|---|
| [67c92b] | 64 |         LoadValenceDatabase(input);
 | 
|---|
| [f34c23] | 65 |     ASSERT(status, "Valence entry of element initialization failed");
 | 
|---|
 | 66 |   }
 | 
|---|
 | 67 |   {
 | 
|---|
 | 68 |     stringstream input(orbitalsDB,ios_base::in);
 | 
|---|
| [b2ae3b] | 69 | #ifndef NDEBUG
 | 
|---|
 | 70 |     bool status =
 | 
|---|
 | 71 | #endif
 | 
|---|
| [67c92b] | 72 |         LoadOrbitalsDatabase(input);
 | 
|---|
| [f34c23] | 73 |     ASSERT(status, "Orbitals entry of element initialization failed");
 | 
|---|
 | 74 |   }
 | 
|---|
 | 75 |   {
 | 
|---|
 | 76 |     stringstream input(HbondangleDB,ios_base::in);
 | 
|---|
| [b2ae3b] | 77 | #ifndef NDEBUG
 | 
|---|
 | 78 |     bool status =
 | 
|---|
 | 79 | #endif
 | 
|---|
| [67c92b] | 80 |         LoadHBondAngleDatabase(input);
 | 
|---|
| [f34c23] | 81 |     ASSERT(status, "HBond angle entry of element initialization failed");
 | 
|---|
 | 82 |   }
 | 
|---|
 | 83 |   {
 | 
|---|
 | 84 |     stringstream input(HbonddistanceDB,ios_base::in);
 | 
|---|
| [b2ae3b] | 85 | #ifndef NDEBUG
 | 
|---|
 | 86 |     bool status =
 | 
|---|
 | 87 | #endif
 | 
|---|
| [67c92b] | 88 |         LoadHBondLengthsDatabase(input);
 | 
|---|
| [f34c23] | 89 |     ASSERT(status, "HBond distance entry of element initialization failed");
 | 
|---|
 | 90 |   }
 | 
|---|
| [4eb4fe] | 91 | };
 | 
|---|
| [6ac7ee] | 92 | 
 | 
|---|
 | 93 | /** destructor for class periodentafel
 | 
|---|
 | 94 |  * Removes every element and afterwards deletes start and end of list.
 | 
|---|
| [42af9e] | 95 |  * TODO: Handle when elements have changed and store databases then
 | 
|---|
| [6ac7ee] | 96 |  */
 | 
|---|
 | 97 | periodentafel::~periodentafel()
 | 
|---|
 | 98 | {
 | 
|---|
| [042f82] | 99 |   CleanupPeriodtable();
 | 
|---|
| [6ac7ee] | 100 | };
 | 
|---|
 | 101 | 
 | 
|---|
 | 102 | /** Adds element to period table list
 | 
|---|
 | 103 |  * \param *pointer element to be added
 | 
|---|
| [4eb4fe] | 104 |  * \return iterator to added element
 | 
|---|
| [6ac7ee] | 105 |  */
 | 
|---|
| [e5c0a1] | 106 | periodentafel::iterator periodentafel::AddElement(element * pointer)
 | 
|---|
| [6ac7ee] | 107 | {
 | 
|---|
| [ead4e6] | 108 |   atomicNumber_t Z = pointer->getNumber();
 | 
|---|
| [4eb4fe] | 109 |   ASSERT(!elements.count(Z), "Element is already present.");
 | 
|---|
| [ead4e6] | 110 |   if (pointer->getNumber() < 1 && pointer->getNumber() >= MAX_ELEMENTS)
 | 
|---|
| [5f612ee] | 111 |     DoeLog(0) && (eLog() << Verbose(0) << "Invalid Z number!\n");
 | 
|---|
| [ead4e6] | 112 |   pair<iterator,bool> res = elements.insert(pair<atomicNumber_t,element*>(Z,pointer));
 | 
|---|
 | 113 |   return res.first;
 | 
|---|
| [6ac7ee] | 114 | };
 | 
|---|
 | 115 | 
 | 
|---|
 | 116 | /** Removes element from list.
 | 
|---|
 | 117 |  * \param *pointer element to be removed
 | 
|---|
 | 118 |  */
 | 
|---|
| [e5c0a1] | 119 | size_t periodentafel::RemoveElement(const element * pointer)
 | 
|---|
| [6ac7ee] | 120 | {
 | 
|---|
| [61745cc] | 121 |   return RemoveElement(pointer->getNumber());
 | 
|---|
| [4eb4fe] | 122 | };
 | 
|---|
 | 123 | 
 | 
|---|
 | 124 | /** Removes element from list.
 | 
|---|
 | 125 |  * \param Z element to be removed
 | 
|---|
 | 126 |  */
 | 
|---|
| [61745cc] | 127 | size_t periodentafel::RemoveElement(atomicNumber_t Z)
 | 
|---|
| [4eb4fe] | 128 | {
 | 
|---|
| [61745cc] | 129 |   return elements.erase(Z);
 | 
|---|
| [6ac7ee] | 130 | };
 | 
|---|
 | 131 | 
 | 
|---|
 | 132 | /** Removes every element from the period table.
 | 
|---|
 | 133 |  */
 | 
|---|
| [ead4e6] | 134 | void periodentafel::CleanupPeriodtable()
 | 
|---|
| [6ac7ee] | 135 | {
 | 
|---|
| [745a85] | 136 |   for(iterator iter=elements.begin();iter!=elements.end();++iter){
 | 
|---|
 | 137 |     delete(*iter).second;
 | 
|---|
 | 138 |   }
 | 
|---|
| [ead4e6] | 139 |   elements.clear();
 | 
|---|
| [6ac7ee] | 140 | };
 | 
|---|
 | 141 | 
 | 
|---|
 | 142 | /** Finds an element by its atomic number.
 | 
|---|
| [fb73b8] | 143 |  * If element is not yet in list, returns NULL.
 | 
|---|
| [6ac7ee] | 144 |  * \param Z atomic number
 | 
|---|
| [fb73b8] | 145 |  * \return pointer to element or NULL if not found
 | 
|---|
| [6ac7ee] | 146 |  */
 | 
|---|
| [e5c0a1] | 147 | const element * periodentafel::FindElement(atomicNumber_t Z) const
 | 
|---|
| [6ac7ee] | 148 | {
 | 
|---|
| [ead4e6] | 149 |   const_iterator res = elements.find(Z);
 | 
|---|
 | 150 |   return res!=elements.end()?((*res).second):0;
 | 
|---|
| [6ac7ee] | 151 | };
 | 
|---|
 | 152 | 
 | 
|---|
 | 153 | /** Finds an element by its atomic number.
 | 
|---|
 | 154 |  * If element is not yet in list, datas are asked and stored in database.
 | 
|---|
 | 155 |  * \param shorthand chemical symbol of the element, e.g. H for hydrogene
 | 
|---|
 | 156 |  * \return pointer to element
 | 
|---|
 | 157 |  */
 | 
|---|
| [e5c0a1] | 158 | const element * periodentafel::FindElement(const string &shorthand) const
 | 
|---|
| [6ac7ee] | 159 | {
 | 
|---|
| [ead4e6] | 160 |   element *res = 0;
 | 
|---|
 | 161 |   for(const_iterator iter=elements.begin();iter!=elements.end();++iter) {
 | 
|---|
 | 162 |     if((*iter).second->getSymbol() == shorthand){
 | 
|---|
 | 163 |       res = (*iter).second;
 | 
|---|
 | 164 |       break;
 | 
|---|
 | 165 |     }
 | 
|---|
| [042f82] | 166 |   }
 | 
|---|
| [ead4e6] | 167 |   return res;
 | 
|---|
| [6ac7ee] | 168 | };
 | 
|---|
 | 169 | 
 | 
|---|
 | 170 | /** Asks for element number and returns pointer to element
 | 
|---|
| [4eb4fe] | 171 |  * \return desired element or NULL
 | 
|---|
| [6ac7ee] | 172 |  */
 | 
|---|
| [e5c0a1] | 173 | const element * periodentafel::AskElement() const
 | 
|---|
| [6ac7ee] | 174 | {
 | 
|---|
| [e5c0a1] | 175 |   const element * walker = NULL;
 | 
|---|
| [042f82] | 176 |   int Z;
 | 
|---|
 | 177 |   do {
 | 
|---|
| [a67d19] | 178 |     DoLog(0) && (Log() << Verbose(0) << "Atomic number Z: ");
 | 
|---|
| [042f82] | 179 |     cin >> Z;
 | 
|---|
 | 180 |     walker = this->FindElement(Z);  // give type
 | 
|---|
 | 181 |   } while (walker == NULL);
 | 
|---|
 | 182 |   return walker;
 | 
|---|
| [6ac7ee] | 183 | };
 | 
|---|
 | 184 | 
 | 
|---|
| [fb73b8] | 185 | /** Asks for element and if not found, presents mask to enter info.
 | 
|---|
 | 186 |  * \return pointer to either present or newly created element
 | 
|---|
 | 187 |  */
 | 
|---|
| [e5c0a1] | 188 | const element * periodentafel::EnterElement()
 | 
|---|
| [fb73b8] | 189 | {
 | 
|---|
| [ead4e6] | 190 |   atomicNumber_t Z = 0;
 | 
|---|
| [a67d19] | 191 |   DoLog(0) && (Log() << Verbose(0) << "Atomic number: " << Z << endl);
 | 
|---|
| [fb73b8] | 192 |   cin >> Z;
 | 
|---|
| [e5c0a1] | 193 |   const element *res = FindElement(Z);
 | 
|---|
| [ead4e6] | 194 |   if (!res) {
 | 
|---|
 | 195 |     // TODO: make this using the constructor
 | 
|---|
| [a67d19] | 196 |     DoLog(0) && (Log() << Verbose(0) << "Element not found in database, please enter." << endl);
 | 
|---|
| [4eb4fe] | 197 |     element *tmp = new element;
 | 
|---|
| [ead4e6] | 198 |     tmp->Z = Z;
 | 
|---|
| [a67d19] | 199 |     DoLog(0) && (Log() << Verbose(0) << "Mass: " << endl);
 | 
|---|
| [ead4e6] | 200 |     cin >> tmp->mass;
 | 
|---|
| [a67d19] | 201 |     DoLog(0) && (Log() << Verbose(0) << "Name [max 64 chars]: " << endl);
 | 
|---|
| [7e3fc94] | 202 |     cin >> tmp->getName();
 | 
|---|
| [a67d19] | 203 |     DoLog(0) && (Log() << Verbose(0) << "Short form [max 3 chars]: " << endl);
 | 
|---|
| [7e3fc94] | 204 |     cin >> tmp->getSymbol();
 | 
|---|
| [ead4e6] | 205 |     AddElement(tmp);
 | 
|---|
| [4eb4fe] | 206 |     return tmp;
 | 
|---|
| [fb73b8] | 207 |   }
 | 
|---|
| [ead4e6] | 208 |   return res;
 | 
|---|
| [fb73b8] | 209 | };
 | 
|---|
 | 210 | 
 | 
|---|
| [ead4e6] | 211 | 
 | 
|---|
 | 212 | /******************** Access to iterators ****************************/
 | 
|---|
| [e5c0a1] | 213 | periodentafel::const_iterator periodentafel::begin() const{
 | 
|---|
| [ead4e6] | 214 |   return elements.begin();
 | 
|---|
 | 215 | }
 | 
|---|
 | 216 | 
 | 
|---|
| [e5c0a1] | 217 | periodentafel::const_iterator periodentafel::end() const{
 | 
|---|
| [ead4e6] | 218 |   return elements.end();
 | 
|---|
 | 219 | }
 | 
|---|
 | 220 | 
 | 
|---|
| [e5c0a1] | 221 | periodentafel::reverse_iterator periodentafel::rbegin() const{
 | 
|---|
| [ead4e6] | 222 |   return reverse_iterator(elements.end());
 | 
|---|
 | 223 | }
 | 
|---|
 | 224 | 
 | 
|---|
| [e5c0a1] | 225 | periodentafel::reverse_iterator periodentafel::rend() const{
 | 
|---|
| [ead4e6] | 226 |   return reverse_iterator(elements.begin());
 | 
|---|
 | 227 | }
 | 
|---|
 | 228 | 
 | 
|---|
| [6ac7ee] | 229 | /** Prints period table to given stream.
 | 
|---|
 | 230 |  * \param output stream
 | 
|---|
 | 231 |  */
 | 
|---|
| [ead4e6] | 232 | bool periodentafel::Output(ostream * const output) const
 | 
|---|
| [6ac7ee] | 233 | {
 | 
|---|
| [042f82] | 234 |   bool result = true;
 | 
|---|
 | 235 |   if (output != NULL) {
 | 
|---|
| [ead4e6] | 236 |     for(const_iterator iter=elements.begin(); iter !=elements.end();++iter){
 | 
|---|
 | 237 |       result = result && (*iter).second->Output(output);
 | 
|---|
| [042f82] | 238 |     }
 | 
|---|
 | 239 |     return result;
 | 
|---|
 | 240 |   } else
 | 
|---|
 | 241 |     return false;
 | 
|---|
| [6ac7ee] | 242 | };
 | 
|---|
 | 243 | 
 | 
|---|
 | 244 | /** Loads element list from file.
 | 
|---|
 | 245 |  * \param *path to to standard file names
 | 
|---|
 | 246 |  */
 | 
|---|
| [989bf6] | 247 | bool periodentafel::LoadPeriodentafel(const char *path)
 | 
|---|
| [6ac7ee] | 248 | {
 | 
|---|
| [4eb4fe] | 249 |   ifstream input;
 | 
|---|
| [042f82] | 250 |   bool status = true;
 | 
|---|
 | 251 |   bool otherstatus = true;
 | 
|---|
 | 252 |   char filename[255];
 | 
|---|
| [6ac7ee] | 253 | 
 | 
|---|
| [042f82] | 254 |   // fill elements DB
 | 
|---|
 | 255 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
 | 256 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 257 |   strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
| [4eb4fe] | 258 |   input.open(filename);
 | 
|---|
| [61745cc] | 259 |   if (!input.fail())
 | 
|---|
 | 260 |     DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as elements database." << endl);
 | 
|---|
| [e5c0a1] | 261 |   status = status && LoadElementsDatabase(input);
 | 
|---|
| [61745cc] | 262 |   input.close();
 | 
|---|
 | 263 |   input.clear();
 | 
|---|
| [4eb4fe] | 264 | 
 | 
|---|
| [67c92b] | 265 |   // fill valence DB per element
 | 
|---|
 | 266 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
 | 267 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 268 |   strncat(filename, STANDARDELECTRONEGATIVITYDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 269 |   input.open(filename);
 | 
|---|
 | 270 |   if (!input.fail())
 | 
|---|
 | 271 |     DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as electronegativity database." << endl);
 | 
|---|
 | 272 |   otherstatus = otherstatus && LoadElectronegativityDatabase(input);
 | 
|---|
 | 273 |   input.close();
 | 
|---|
 | 274 |   input.clear();
 | 
|---|
 | 275 | 
 | 
|---|
| [4eb4fe] | 276 |   // fill valence DB per element
 | 
|---|
 | 277 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
 | 278 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 279 |   strncat(filename, STANDARDVALENCEDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 280 |   input.open(filename);
 | 
|---|
| [61745cc] | 281 |   if (!input.fail())
 | 
|---|
 | 282 |     DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as valence database." << endl);
 | 
|---|
| [67c92b] | 283 |   otherstatus = otherstatus && LoadValenceDatabase(input);
 | 
|---|
| [61745cc] | 284 |   input.close();
 | 
|---|
 | 285 |   input.clear();
 | 
|---|
| [4eb4fe] | 286 | 
 | 
|---|
 | 287 |   // fill orbitals DB per element
 | 
|---|
 | 288 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
 | 289 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 290 |   strncat(filename, STANDARDORBITALDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 291 |   input.open(filename);
 | 
|---|
| [61745cc] | 292 |   if (!input.fail())
 | 
|---|
 | 293 |     DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as orbitals database." << endl);
 | 
|---|
| [67c92b] | 294 |   otherstatus = otherstatus && LoadOrbitalsDatabase(input);
 | 
|---|
| [61745cc] | 295 |   input.close();
 | 
|---|
 | 296 |   input.clear();
 | 
|---|
| [4eb4fe] | 297 | 
 | 
|---|
 | 298 |   // fill H-BondAngle DB per element
 | 
|---|
 | 299 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
 | 300 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 301 |   strncat(filename, STANDARDHBONDANGLEDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 302 |   input.open(filename);
 | 
|---|
| [61745cc] | 303 |   if (!input.fail())
 | 
|---|
 | 304 |     DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as H bond angle database." << endl);
 | 
|---|
| [67c92b] | 305 |   otherstatus = otherstatus && LoadHBondAngleDatabase(input);
 | 
|---|
| [61745cc] | 306 |   input.close();
 | 
|---|
 | 307 |   input.clear();
 | 
|---|
| [4eb4fe] | 308 | 
 | 
|---|
 | 309 |   // fill H-BondDistance DB per element
 | 
|---|
 | 310 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
 | 311 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 312 |   strncat(filename, STANDARDHBONDDISTANCEDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 313 |   input.open(filename);
 | 
|---|
| [61745cc] | 314 |   if (!input.fail())
 | 
|---|
 | 315 |     DoLog(0) && (Log() << Verbose(0) << "Using " << filename << " as H bond length database." << endl);
 | 
|---|
| [67c92b] | 316 |   otherstatus = otherstatus && LoadHBondLengthsDatabase(input);
 | 
|---|
| [61745cc] | 317 |   input.close();
 | 
|---|
 | 318 |   input.clear();
 | 
|---|
| [4eb4fe] | 319 | 
 | 
|---|
 | 320 |   if (!otherstatus){
 | 
|---|
 | 321 |     DoeLog(2) && (eLog()<< Verbose(2) << "Something went wrong while parsing the other databases!" << endl);
 | 
|---|
 | 322 |   }
 | 
|---|
 | 323 | 
 | 
|---|
 | 324 |   return status;
 | 
|---|
 | 325 | };
 | 
|---|
 | 326 | 
 | 
|---|
 | 327 | /** load the element info.
 | 
|---|
 | 328 |  * \param *input stream to parse from
 | 
|---|
 | 329 |  * \return true - parsing successful, false - something went wrong
 | 
|---|
 | 330 |  */
 | 
|---|
| [e5c0a1] | 331 | bool periodentafel::LoadElementsDatabase(istream &input)
 | 
|---|
| [4eb4fe] | 332 | {
 | 
|---|
| [ff73a2] | 333 |   bool status = true;
 | 
|---|
| [e5c0a1] | 334 |   string header1tmp,header2tmp;
 | 
|---|
 | 335 |   // first parse into a map, so we can revert to old status in case something goes wront
 | 
|---|
 | 336 |   map<atomicNumber_t,element*> parsedElements;
 | 
|---|
 | 337 |   if (!input.fail()) {
 | 
|---|
 | 338 |     getline(input,header1tmp);
 | 
|---|
 | 339 |     getline(input,header2tmp); // skip first two header lines
 | 
|---|
| [4e6d74] | 340 |     //cout << "First header: " << header1tmp << endl;
 | 
|---|
 | 341 |     //cout << "Second header: " << header2tmp << endl;
 | 
|---|
| [ad7270] | 342 | //    DoLog(0) && (Log() << Verbose(0) <<  "Parsed elements:");
 | 
|---|
| [e5c0a1] | 343 |     while (!input.eof()) {
 | 
|---|
| [042f82] | 344 |       element *neues = new element;
 | 
|---|
| [83f176] | 345 |       input >> neues->name;
 | 
|---|
| [67c92b] | 346 |       //input >> ws;
 | 
|---|
| [83f176] | 347 |       input >> neues->symbol;
 | 
|---|
| [67c92b] | 348 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 349 |       input >> neues->period;
 | 
|---|
| [67c92b] | 350 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 351 |       input >> neues->group;
 | 
|---|
| [67c92b] | 352 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 353 |       input >> neues->block;
 | 
|---|
| [67c92b] | 354 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 355 |       input >> neues->Z;
 | 
|---|
| [67c92b] | 356 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 357 |       input >> neues->mass;
 | 
|---|
| [67c92b] | 358 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 359 |       input >> neues->CovalentRadius;
 | 
|---|
| [67c92b] | 360 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 361 |       input >> neues->VanDerWaalsRadius;
 | 
|---|
| [67c92b] | 362 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 363 |       input >> ws;
 | 
|---|
| [042f82] | 364 |       //neues->Output((ofstream *)&cout);
 | 
|---|
| [61745cc] | 365 |       if ((neues->getNumber() > 0) && (neues->getNumber() < MAX_ELEMENTS)) {
 | 
|---|
| [e5c0a1] | 366 |         parsedElements[neues->Z] = neues;
 | 
|---|
| [ad7270] | 367 | //        DoLog(0) && (Log() << Verbose(0) << " " << *neues);
 | 
|---|
| [ff73a2] | 368 |       } else {
 | 
|---|
 | 369 |         DoeLog(2) && (eLog() << Verbose(2) << "Detected empty line or invalid element in elements db, discarding." << endl);
 | 
|---|
 | 370 |         DoLog(0) && (Log() << Verbose(0) << " <?>");
 | 
|---|
| [db6bf74] | 371 |         delete(neues);
 | 
|---|
| [042f82] | 372 |       }
 | 
|---|
| [e5c0a1] | 373 |       // when the input is in failed state, we most likely just read garbage
 | 
|---|
 | 374 |       if(input.fail()) {
 | 
|---|
 | 375 |         DoeLog(2) && (eLog() << Verbose(2) << "Error parsing elements db." << endl);
 | 
|---|
 | 376 |         status = false;
 | 
|---|
 | 377 |         break;
 | 
|---|
 | 378 |       }
 | 
|---|
| [042f82] | 379 |     }
 | 
|---|
| [ad7270] | 380 | //    DoLog(0) && (Log() << Verbose(0) << endl);
 | 
|---|
| [61745cc] | 381 |   } else {
 | 
|---|
 | 382 |     DoeLog(1) && (eLog() << Verbose(1) << "Could not open the database." << endl);
 | 
|---|
| [ff73a2] | 383 |     status = false;
 | 
|---|
| [61745cc] | 384 |   }
 | 
|---|
| [ff73a2] | 385 | 
 | 
|---|
| [e5c0a1] | 386 |   if (!parsedElements.size())
 | 
|---|
| [ff73a2] | 387 |     status = false;
 | 
|---|
 | 388 | 
 | 
|---|
| [e5c0a1] | 389 |   if(status){
 | 
|---|
 | 390 |     for(map<atomicNumber_t,element*>::iterator iter=parsedElements.begin();
 | 
|---|
 | 391 |                                                iter!=parsedElements.end();
 | 
|---|
 | 392 |                                                ++iter){
 | 
|---|
 | 393 |       if (elements.count(iter->first)) {
 | 
|---|
 | 394 |         // if element already present, replace the old one
 | 
|---|
 | 395 |         // pointer to old element might still be in use, so we have to replace into the old element
 | 
|---|
 | 396 |         *(elements[iter->first])=*iter->second;
 | 
|---|
| [9f99b3] | 397 |         delete(iter->second);
 | 
|---|
| [e5c0a1] | 398 |       }
 | 
|---|
 | 399 |       else {
 | 
|---|
 | 400 |         // no such element in periodentafel... we can just insert
 | 
|---|
 | 401 |         elements[iter->first] = iter->second;
 | 
|---|
 | 402 |       }
 | 
|---|
 | 403 |     }
 | 
|---|
 | 404 |     // all went well.. we now copy the header
 | 
|---|
 | 405 |     strncpy(header1,header1tmp.c_str(),MAXSTRINGSIZE);
 | 
|---|
 | 406 |     header1[MAXSTRINGSIZE-1]=0;
 | 
|---|
 | 407 |     strncpy(header2,header2tmp.c_str(),MAXSTRINGSIZE);
 | 
|---|
 | 408 |     header2[MAXSTRINGSIZE-1]=0;
 | 
|---|
 | 409 |   }
 | 
|---|
 | 410 | 
 | 
|---|
| [ff73a2] | 411 |   return status;
 | 
|---|
| [4eb4fe] | 412 | }
 | 
|---|
| [6ac7ee] | 413 | 
 | 
|---|
| [67c92b] | 414 | /** load the electronegativity info.
 | 
|---|
 | 415 |  * \param *input stream to parse from
 | 
|---|
 | 416 |  * \return true - parsing successful, false - something went wrong
 | 
|---|
 | 417 |  */
 | 
|---|
 | 418 | bool periodentafel::LoadElectronegativityDatabase(std::istream &input)
 | 
|---|
 | 419 | {
 | 
|---|
 | 420 |   char dummy[MAXSTRINGSIZE];
 | 
|---|
 | 421 |   if (!input.fail()) {
 | 
|---|
 | 422 |     input.getline(dummy, MAXSTRINGSIZE);
 | 
|---|
 | 423 |     while (!input.eof()) {
 | 
|---|
 | 424 |       atomicNumber_t Z;
 | 
|---|
 | 425 |       input >> Z;
 | 
|---|
 | 426 |       ASSERT(elements.count(Z), "Element not present");
 | 
|---|
 | 427 |       input >> ws;
 | 
|---|
 | 428 |       input >> elements[Z]->Electronegativity;
 | 
|---|
 | 429 |       input >> ws;
 | 
|---|
| [febef3] | 430 |       //DoLog(1) && (Log() << Verbose(1)
 | 
|---|
 | 431 |         //  << "Element " << Z << " has " << FindElement(Z)->Electronegativity << " valence electrons." << endl);
 | 
|---|
| [67c92b] | 432 |     }
 | 
|---|
 | 433 |     return true;
 | 
|---|
 | 434 |   } else
 | 
|---|
 | 435 |     return false;
 | 
|---|
 | 436 | }
 | 
|---|
 | 437 | 
 | 
|---|
| [4eb4fe] | 438 | /** load the valence info.
 | 
|---|
 | 439 |  * \param *input stream to parse from
 | 
|---|
 | 440 |  * \return true - parsing successful, false - something went wrong
 | 
|---|
 | 441 |  */
 | 
|---|
| [67c92b] | 442 | bool periodentafel::LoadValenceDatabase(istream &input)
 | 
|---|
| [4eb4fe] | 443 | {
 | 
|---|
 | 444 |   char dummy[MAXSTRINGSIZE];
 | 
|---|
| [67c92b] | 445 |   if (!input.fail()) {
 | 
|---|
 | 446 |     input.getline(dummy, MAXSTRINGSIZE);
 | 
|---|
 | 447 |     while (!input.eof()) {
 | 
|---|
| [ead4e6] | 448 |       atomicNumber_t Z;
 | 
|---|
| [67c92b] | 449 |       input >> Z;
 | 
|---|
| [4eb4fe] | 450 |       ASSERT(elements.count(Z), "Element not present");
 | 
|---|
| [67c92b] | 451 |       input >> ws;
 | 
|---|
 | 452 |       input >> elements[Z]->Valence;
 | 
|---|
 | 453 |       input >> ws;
 | 
|---|
| [274d45] | 454 |       //Log() << Verbose(3) << "Element " << Z << " has " << FindElement(Z)->Valence << " valence electrons." << endl;
 | 
|---|
| [042f82] | 455 |     }
 | 
|---|
| [4eb4fe] | 456 |     return true;
 | 
|---|
| [042f82] | 457 |   } else
 | 
|---|
| [4eb4fe] | 458 |                 return false;
 | 
|---|
 | 459 | }
 | 
|---|
| [6ac7ee] | 460 | 
 | 
|---|
| [4eb4fe] | 461 | /** load the orbitals info.
 | 
|---|
 | 462 |  * \param *input stream to parse from
 | 
|---|
 | 463 |  * \return true - parsing successful, false - something went wrong
 | 
|---|
 | 464 |  */
 | 
|---|
| [67c92b] | 465 | bool periodentafel::LoadOrbitalsDatabase(istream &input)
 | 
|---|
| [4eb4fe] | 466 | {
 | 
|---|
 | 467 |   char dummy[MAXSTRINGSIZE];
 | 
|---|
| [67c92b] | 468 |   if (!input.fail()) {
 | 
|---|
 | 469 |     input.getline(dummy, MAXSTRINGSIZE);
 | 
|---|
 | 470 |     while (!input.eof()) {
 | 
|---|
| [ead4e6] | 471 |       atomicNumber_t Z;
 | 
|---|
| [67c92b] | 472 |       input >> Z;
 | 
|---|
| [4eb4fe] | 473 |       ASSERT(elements.count(Z), "Element not present");
 | 
|---|
| [67c92b] | 474 |       input >> ws;
 | 
|---|
 | 475 |       input >> elements[Z]->NoValenceOrbitals;
 | 
|---|
 | 476 |       input >> ws;
 | 
|---|
| [274d45] | 477 |       //Log() << Verbose(3) << "Element " << Z << " has " << FindElement(Z)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
 | 
|---|
| [042f82] | 478 |     }
 | 
|---|
| [4eb4fe] | 479 |     return true;
 | 
|---|
| [042f82] | 480 |   } else
 | 
|---|
| [4eb4fe] | 481 |     return false;
 | 
|---|
 | 482 | }
 | 
|---|
| [6ac7ee] | 483 | 
 | 
|---|
| [4eb4fe] | 484 | /** load the hbond angles info.
 | 
|---|
 | 485 |  * \param *input stream to parse from
 | 
|---|
 | 486 |  * \return true - parsing successful, false - something went wrong
 | 
|---|
 | 487 |  */
 | 
|---|
| [67c92b] | 488 | bool periodentafel::LoadHBondAngleDatabase(istream &input)
 | 
|---|
| [4eb4fe] | 489 | {
 | 
|---|
 | 490 |   char dummy[MAXSTRINGSIZE];
 | 
|---|
| [67c92b] | 491 |   if (!input.fail()) {
 | 
|---|
 | 492 |     input.getline(dummy, MAXSTRINGSIZE);
 | 
|---|
 | 493 |     while (!input.eof()) {
 | 
|---|
| [ead4e6] | 494 |       atomicNumber_t Z;
 | 
|---|
| [67c92b] | 495 |       input >> Z;
 | 
|---|
| [4eb4fe] | 496 |       ASSERT(elements.count(Z), "Element not present");
 | 
|---|
| [67c92b] | 497 |       input >> ws;
 | 
|---|
 | 498 |       input >> elements[Z]->HBondAngle[0];
 | 
|---|
 | 499 |       input >> elements[Z]->HBondAngle[1];
 | 
|---|
 | 500 |       input >> elements[Z]->HBondAngle[2];
 | 
|---|
 | 501 |       input >> ws;
 | 
|---|
| [4eb4fe] | 502 |       //Log() << 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;
 | 
|---|
| [042f82] | 503 |     }
 | 
|---|
| [4eb4fe] | 504 |     return true;
 | 
|---|
| [042f82] | 505 |   } else
 | 
|---|
| [4eb4fe] | 506 |                 return false;
 | 
|---|
 | 507 | }
 | 
|---|
| [6ac7ee] | 508 | 
 | 
|---|
| [4eb4fe] | 509 | /** load the hbond lengths info.
 | 
|---|
 | 510 |  * \param *input stream to parse from
 | 
|---|
 | 511 |  * \return true - parsing successful, false - something went wrong
 | 
|---|
 | 512 |  */
 | 
|---|
| [67c92b] | 513 | bool periodentafel::LoadHBondLengthsDatabase(istream &input)
 | 
|---|
| [4eb4fe] | 514 | {
 | 
|---|
 | 515 |   char dummy[MAXSTRINGSIZE];
 | 
|---|
| [67c92b] | 516 |   if (!input.fail()) {
 | 
|---|
 | 517 |     input.getline(dummy, MAXSTRINGSIZE);
 | 
|---|
 | 518 |     while (!input.eof()) {
 | 
|---|
| [ead4e6] | 519 |       atomicNumber_t Z;
 | 
|---|
| [67c92b] | 520 |       input >> Z;
 | 
|---|
| [4eb4fe] | 521 |       ASSERT(elements.count(Z), "Element not present");
 | 
|---|
| [67c92b] | 522 |       input >> ws;
 | 
|---|
 | 523 |       input >> elements[Z]->HBondDistance[0];
 | 
|---|
 | 524 |       input >> elements[Z]->HBondDistance[1];
 | 
|---|
 | 525 |       input >> elements[Z]->HBondDistance[2];
 | 
|---|
 | 526 |       input >> ws;
 | 
|---|
| [4eb4fe] | 527 |       //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen." << endl;
 | 
|---|
| [042f82] | 528 |     }
 | 
|---|
| [4eb4fe] | 529 |     return true;
 | 
|---|
| [042f82] | 530 |   } else
 | 
|---|
| [4eb4fe] | 531 |                 return false;
 | 
|---|
 | 532 | }
 | 
|---|
| [6ac7ee] | 533 | 
 | 
|---|
 | 534 | /** Stores element list to file.
 | 
|---|
 | 535 |  */
 | 
|---|
| [989bf6] | 536 | bool periodentafel::StorePeriodentafel(const char *path) const
 | 
|---|
| [6ac7ee] | 537 | {
 | 
|---|
| [042f82] | 538 |   bool result = true;
 | 
|---|
 | 539 |   ofstream f;
 | 
|---|
 | 540 |   char filename[MAXSTRINGSIZE];
 | 
|---|
| [6ac7ee] | 541 | 
 | 
|---|
| [042f82] | 542 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
 | 543 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 544 |   strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 545 |   f.open(filename);
 | 
|---|
 | 546 |   if (f != NULL) {
 | 
|---|
 | 547 |     f << header1 << endl;
 | 
|---|
 | 548 |     f << header2 << endl;
 | 
|---|
| [ead4e6] | 549 |     for(const_iterator iter=elements.begin();iter!=elements.end();++iter){
 | 
|---|
 | 550 |          result = result && (*iter).second->Output(&f);
 | 
|---|
| [042f82] | 551 |     }
 | 
|---|
 | 552 |     f.close();
 | 
|---|
| [4eb4fe] | 553 |     return true;
 | 
|---|
| [042f82] | 554 |   } else
 | 
|---|
| [4eb4fe] | 555 |     return result;
 | 
|---|
| [6ac7ee] | 556 | };
 | 
|---|