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