| [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 | 
 | 
|---|
| [47d041] | 21 | #include <cstring>
 | 
|---|
 | 22 | #include <fstream>
 | 
|---|
| [cd4ccc] | 23 | #include <iomanip>
 | 
|---|
| [4eb4fe] | 24 | #include <iostream>
 | 
|---|
| [47d041] | 25 | #include <sstream>
 | 
|---|
| [cd4ccc] | 26 | 
 | 
|---|
| [ad011c] | 27 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [47d041] | 28 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [f66195] | 29 | #include "element.hpp"
 | 
|---|
| [4eb4fe] | 30 | #include "elements_db.hpp"
 | 
|---|
| [6ac7ee] | 31 | #include "periodentafel.hpp"
 | 
|---|
 | 32 | 
 | 
|---|
| [ead4e6] | 33 | using namespace std;
 | 
|---|
 | 34 | 
 | 
|---|
| [6ac7ee] | 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 |  */
 | 
|---|
| [4ae823] | 40 | periodentafel::periodentafel(const bool DoLoad)
 | 
|---|
| [4eb4fe] | 41 | {
 | 
|---|
| [4ae823] | 42 |   if (DoLoad) {
 | 
|---|
 | 43 |     ScanPeriodentafel();
 | 
|---|
| [064178] | 44 |   }
 | 
|---|
| [4eb4fe] | 45 | };
 | 
|---|
| [6ac7ee] | 46 | 
 | 
|---|
 | 47 | /** destructor for class periodentafel
 | 
|---|
 | 48 |  * Removes every element and afterwards deletes start and end of list.
 | 
|---|
| [42af9e] | 49 |  * TODO: Handle when elements have changed and store databases then
 | 
|---|
| [6ac7ee] | 50 |  */
 | 
|---|
 | 51 | periodentafel::~periodentafel()
 | 
|---|
 | 52 | {
 | 
|---|
| [042f82] | 53 |   CleanupPeriodtable();
 | 
|---|
| [6ac7ee] | 54 | };
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 | /** Adds element to period table list
 | 
|---|
 | 57 |  * \param *pointer element to be added
 | 
|---|
| [4eb4fe] | 58 |  * \return iterator to added element
 | 
|---|
| [6ac7ee] | 59 |  */
 | 
|---|
| [e5c0a1] | 60 | periodentafel::iterator periodentafel::AddElement(element * pointer)
 | 
|---|
| [6ac7ee] | 61 | {
 | 
|---|
| [ead4e6] | 62 |   atomicNumber_t Z = pointer->getNumber();
 | 
|---|
| [4eb4fe] | 63 |   ASSERT(!elements.count(Z), "Element is already present.");
 | 
|---|
| [ead4e6] | 64 |   if (pointer->getNumber() < 1 && pointer->getNumber() >= MAX_ELEMENTS)
 | 
|---|
| [47d041] | 65 |     ELOG(0, "Invalid Z number!");
 | 
|---|
| [ead4e6] | 66 |   pair<iterator,bool> res = elements.insert(pair<atomicNumber_t,element*>(Z,pointer));
 | 
|---|
 | 67 |   return res.first;
 | 
|---|
| [6ac7ee] | 68 | };
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 | /** Removes element from list.
 | 
|---|
 | 71 |  * \param *pointer element to be removed
 | 
|---|
 | 72 |  */
 | 
|---|
| [e5c0a1] | 73 | size_t periodentafel::RemoveElement(const element * pointer)
 | 
|---|
| [6ac7ee] | 74 | {
 | 
|---|
| [61745cc] | 75 |   return RemoveElement(pointer->getNumber());
 | 
|---|
| [4eb4fe] | 76 | };
 | 
|---|
 | 77 | 
 | 
|---|
 | 78 | /** Removes element from list.
 | 
|---|
 | 79 |  * \param Z element to be removed
 | 
|---|
 | 80 |  */
 | 
|---|
| [61745cc] | 81 | size_t periodentafel::RemoveElement(atomicNumber_t Z)
 | 
|---|
| [4eb4fe] | 82 | {
 | 
|---|
| [61745cc] | 83 |   return elements.erase(Z);
 | 
|---|
| [6ac7ee] | 84 | };
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 | /** Removes every element from the period table.
 | 
|---|
 | 87 |  */
 | 
|---|
| [ead4e6] | 88 | void periodentafel::CleanupPeriodtable()
 | 
|---|
| [6ac7ee] | 89 | {
 | 
|---|
| [745a85] | 90 |   for(iterator iter=elements.begin();iter!=elements.end();++iter){
 | 
|---|
 | 91 |     delete(*iter).second;
 | 
|---|
 | 92 |   }
 | 
|---|
| [ead4e6] | 93 |   elements.clear();
 | 
|---|
| [6ac7ee] | 94 | };
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 | /** Finds an element by its atomic number.
 | 
|---|
| [fb73b8] | 97 |  * If element is not yet in list, returns NULL.
 | 
|---|
| [6ac7ee] | 98 |  * \param Z atomic number
 | 
|---|
| [fb73b8] | 99 |  * \return pointer to element or NULL if not found
 | 
|---|
| [6ac7ee] | 100 |  */
 | 
|---|
| [e5c0a1] | 101 | const element * periodentafel::FindElement(atomicNumber_t Z) const
 | 
|---|
| [6ac7ee] | 102 | {
 | 
|---|
| [ead4e6] | 103 |   const_iterator res = elements.find(Z);
 | 
|---|
 | 104 |   return res!=elements.end()?((*res).second):0;
 | 
|---|
| [6ac7ee] | 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 |  */
 | 
|---|
| [e5c0a1] | 112 | const element * periodentafel::FindElement(const string &shorthand) const
 | 
|---|
| [6ac7ee] | 113 | {
 | 
|---|
| [ead4e6] | 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 |     }
 | 
|---|
| [042f82] | 120 |   }
 | 
|---|
| [ead4e6] | 121 |   return res;
 | 
|---|
| [6ac7ee] | 122 | };
 | 
|---|
 | 123 | 
 | 
|---|
 | 124 | /** Asks for element number and returns pointer to element
 | 
|---|
| [4eb4fe] | 125 |  * \return desired element or NULL
 | 
|---|
| [6ac7ee] | 126 |  */
 | 
|---|
| [e5c0a1] | 127 | const element * periodentafel::AskElement() const
 | 
|---|
| [6ac7ee] | 128 | {
 | 
|---|
| [e5c0a1] | 129 |   const element * walker = NULL;
 | 
|---|
| [042f82] | 130 |   int Z;
 | 
|---|
 | 131 |   do {
 | 
|---|
| [47d041] | 132 |     std::cout << "Atomic number Z: ";
 | 
|---|
 | 133 |     std::cin >> Z;
 | 
|---|
| [042f82] | 134 |     walker = this->FindElement(Z);  // give type
 | 
|---|
 | 135 |   } while (walker == NULL);
 | 
|---|
 | 136 |   return walker;
 | 
|---|
| [6ac7ee] | 137 | };
 | 
|---|
 | 138 | 
 | 
|---|
| [fb73b8] | 139 | /** Asks for element and if not found, presents mask to enter info.
 | 
|---|
 | 140 |  * \return pointer to either present or newly created element
 | 
|---|
 | 141 |  */
 | 
|---|
| [e5c0a1] | 142 | const element * periodentafel::EnterElement()
 | 
|---|
| [fb73b8] | 143 | {
 | 
|---|
| [ead4e6] | 144 |   atomicNumber_t Z = 0;
 | 
|---|
| [47d041] | 145 |   std::cout << "Atomic number: " << Z;
 | 
|---|
| [fb73b8] | 146 |   cin >> Z;
 | 
|---|
| [e5c0a1] | 147 |   const element *res = FindElement(Z);
 | 
|---|
| [ead4e6] | 148 |   if (!res) {
 | 
|---|
 | 149 |     // TODO: make this using the constructor
 | 
|---|
| [47d041] | 150 |     std::cout << "Element not found in database, please enter." << std::endl;
 | 
|---|
| [4eb4fe] | 151 |     element *tmp = new element;
 | 
|---|
| [ead4e6] | 152 |     tmp->Z = Z;
 | 
|---|
| [47d041] | 153 |     std::cout << "Mass: ";
 | 
|---|
| [ead4e6] | 154 |     cin >> tmp->mass;
 | 
|---|
| [47d041] | 155 |     std::cout << "Name [max 64 chars]: ";
 | 
|---|
| [bae8b0] | 156 |     cin >> tmp->name;
 | 
|---|
| [47d041] | 157 |     std::cout << "Short form [max 3 chars]: ";
 | 
|---|
| [bae8b0] | 158 |     cin >> tmp->symbol;
 | 
|---|
| [ead4e6] | 159 |     AddElement(tmp);
 | 
|---|
| [4eb4fe] | 160 |     return tmp;
 | 
|---|
| [fb73b8] | 161 |   }
 | 
|---|
| [ead4e6] | 162 |   return res;
 | 
|---|
| [fb73b8] | 163 | };
 | 
|---|
 | 164 | 
 | 
|---|
| [ead4e6] | 165 | 
 | 
|---|
 | 166 | /******************** Access to iterators ****************************/
 | 
|---|
| [e5c0a1] | 167 | periodentafel::const_iterator periodentafel::begin() const{
 | 
|---|
| [ead4e6] | 168 |   return elements.begin();
 | 
|---|
 | 169 | }
 | 
|---|
 | 170 | 
 | 
|---|
| [e5c0a1] | 171 | periodentafel::const_iterator periodentafel::end() const{
 | 
|---|
| [ead4e6] | 172 |   return elements.end();
 | 
|---|
 | 173 | }
 | 
|---|
 | 174 | 
 | 
|---|
| [e5c0a1] | 175 | periodentafel::reverse_iterator periodentafel::rbegin() const{
 | 
|---|
| [ead4e6] | 176 |   return reverse_iterator(elements.end());
 | 
|---|
 | 177 | }
 | 
|---|
 | 178 | 
 | 
|---|
| [e5c0a1] | 179 | periodentafel::reverse_iterator periodentafel::rend() const{
 | 
|---|
| [ead4e6] | 180 |   return reverse_iterator(elements.begin());
 | 
|---|
 | 181 | }
 | 
|---|
 | 182 | 
 | 
|---|
| [6ac7ee] | 183 | /** Prints period table to given stream.
 | 
|---|
 | 184 |  * \param output stream
 | 
|---|
 | 185 |  */
 | 
|---|
| [ead4e6] | 186 | bool periodentafel::Output(ostream * const output) const
 | 
|---|
| [6ac7ee] | 187 | {
 | 
|---|
| [042f82] | 188 |   bool result = true;
 | 
|---|
 | 189 |   if (output != NULL) {
 | 
|---|
| [ead4e6] | 190 |     for(const_iterator iter=elements.begin(); iter !=elements.end();++iter){
 | 
|---|
 | 191 |       result = result && (*iter).second->Output(output);
 | 
|---|
| [042f82] | 192 |     }
 | 
|---|
 | 193 |     return result;
 | 
|---|
 | 194 |   } else
 | 
|---|
 | 195 |     return false;
 | 
|---|
| [6ac7ee] | 196 | };
 | 
|---|
 | 197 | 
 | 
|---|
| [4ae823] | 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 | 
 | 
|---|
| [6ac7ee] | 261 | /** Loads element list from file.
 | 
|---|
 | 262 |  * \param *path to to standard file names
 | 
|---|
 | 263 |  */
 | 
|---|
| [989bf6] | 264 | bool periodentafel::LoadPeriodentafel(const char *path)
 | 
|---|
| [6ac7ee] | 265 | {
 | 
|---|
| [4eb4fe] | 266 |   ifstream input;
 | 
|---|
| [042f82] | 267 |   bool status = true;
 | 
|---|
 | 268 |   bool otherstatus = true;
 | 
|---|
 | 269 |   char filename[255];
 | 
|---|
| [6ac7ee] | 270 | 
 | 
|---|
| [042f82] | 271 |   // fill elements DB
 | 
|---|
 | 272 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
 | 273 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 274 |   strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
| [4eb4fe] | 275 |   input.open(filename);
 | 
|---|
| [61745cc] | 276 |   if (!input.fail())
 | 
|---|
| [47d041] | 277 |     LOG(0, "Using " << filename << " as elements database.");
 | 
|---|
| [e5c0a1] | 278 |   status = status && LoadElementsDatabase(input);
 | 
|---|
| [61745cc] | 279 |   input.close();
 | 
|---|
 | 280 |   input.clear();
 | 
|---|
| [4eb4fe] | 281 | 
 | 
|---|
| [67c92b] | 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())
 | 
|---|
| [47d041] | 288 |     LOG(0, "Using " << filename << " as electronegativity database.");
 | 
|---|
| [67c92b] | 289 |   otherstatus = otherstatus && LoadElectronegativityDatabase(input);
 | 
|---|
 | 290 |   input.close();
 | 
|---|
 | 291 |   input.clear();
 | 
|---|
 | 292 | 
 | 
|---|
| [4eb4fe] | 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);
 | 
|---|
| [61745cc] | 298 |   if (!input.fail())
 | 
|---|
| [47d041] | 299 |     LOG(0, "Using " << filename << " as valence database.");
 | 
|---|
| [67c92b] | 300 |   otherstatus = otherstatus && LoadValenceDatabase(input);
 | 
|---|
| [61745cc] | 301 |   input.close();
 | 
|---|
 | 302 |   input.clear();
 | 
|---|
| [4eb4fe] | 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);
 | 
|---|
| [61745cc] | 309 |   if (!input.fail())
 | 
|---|
| [47d041] | 310 |     LOG(0, "Using " << filename << " as orbitals database.");
 | 
|---|
| [67c92b] | 311 |   otherstatus = otherstatus && LoadOrbitalsDatabase(input);
 | 
|---|
| [61745cc] | 312 |   input.close();
 | 
|---|
 | 313 |   input.clear();
 | 
|---|
| [4eb4fe] | 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);
 | 
|---|
| [61745cc] | 320 |   if (!input.fail())
 | 
|---|
| [47d041] | 321 |     LOG(0, "Using " << filename << " as H bond angle database.");
 | 
|---|
| [67c92b] | 322 |   otherstatus = otherstatus && LoadHBondAngleDatabase(input);
 | 
|---|
| [61745cc] | 323 |   input.close();
 | 
|---|
 | 324 |   input.clear();
 | 
|---|
| [4eb4fe] | 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);
 | 
|---|
| [61745cc] | 331 |   if (!input.fail())
 | 
|---|
| [47d041] | 332 |     LOG(0, "Using " << filename << " as H bond length database.");
 | 
|---|
| [67c92b] | 333 |   otherstatus = otherstatus && LoadHBondLengthsDatabase(input);
 | 
|---|
| [61745cc] | 334 |   input.close();
 | 
|---|
 | 335 |   input.clear();
 | 
|---|
| [4eb4fe] | 336 | 
 | 
|---|
| [3613a2] | 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())
 | 
|---|
| [47d041] | 343 |     LOG(0, "Using " << filename << " as color database.");
 | 
|---|
| [3613a2] | 344 |   otherstatus = otherstatus && LoadColorDatabase(input);
 | 
|---|
 | 345 |   input.close();
 | 
|---|
 | 346 |   input.clear();
 | 
|---|
 | 347 | 
 | 
|---|
| [4eb4fe] | 348 |   if (!otherstatus){
 | 
|---|
| [47d041] | 349 |     ELOG(2, "Something went wrong while parsing the other databases!");
 | 
|---|
| [4eb4fe] | 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 |  */
 | 
|---|
| [e5c0a1] | 359 | bool periodentafel::LoadElementsDatabase(istream &input)
 | 
|---|
| [4eb4fe] | 360 | {
 | 
|---|
| [ff73a2] | 361 |   bool status = true;
 | 
|---|
| [e5c0a1] | 362 |   string header1tmp,header2tmp;
 | 
|---|
| [47d041] | 363 | //  std::stringstream parsedelements;
 | 
|---|
| [e5c0a1] | 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
 | 
|---|
| [4e6d74] | 369 |     //cout << "First header: " << header1tmp << endl;
 | 
|---|
 | 370 |     //cout << "Second header: " << header2tmp << endl;
 | 
|---|
| [47d041] | 371 | //    parsedelements <<  "Parsed elements:");
 | 
|---|
| [e5c0a1] | 372 |     while (!input.eof()) {
 | 
|---|
| [042f82] | 373 |       element *neues = new element;
 | 
|---|
| [83f176] | 374 |       input >> neues->name;
 | 
|---|
| [67c92b] | 375 |       //input >> ws;
 | 
|---|
| [83f176] | 376 |       input >> neues->symbol;
 | 
|---|
| [67c92b] | 377 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 378 |       input >> neues->period;
 | 
|---|
| [67c92b] | 379 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 380 |       input >> neues->group;
 | 
|---|
| [67c92b] | 381 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 382 |       input >> neues->block;
 | 
|---|
| [67c92b] | 383 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 384 |       input >> neues->Z;
 | 
|---|
| [67c92b] | 385 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 386 |       input >> neues->mass;
 | 
|---|
| [67c92b] | 387 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 388 |       input >> neues->CovalentRadius;
 | 
|---|
| [67c92b] | 389 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 390 |       input >> neues->VanDerWaalsRadius;
 | 
|---|
| [67c92b] | 391 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 392 |       input >> ws;
 | 
|---|
| [042f82] | 393 |       //neues->Output((ofstream *)&cout);
 | 
|---|
| [61745cc] | 394 |       if ((neues->getNumber() > 0) && (neues->getNumber() < MAX_ELEMENTS)) {
 | 
|---|
| [e5c0a1] | 395 |         parsedElements[neues->Z] = neues;
 | 
|---|
| [47d041] | 396 | //        parsedelements << " " << *neues);
 | 
|---|
| [ff73a2] | 397 |       } else {
 | 
|---|
| [47d041] | 398 |         ELOG(2, "Detected empty line or invalid element in elements db, discarding.");
 | 
|---|
 | 399 | //        parsedelements << " <?>");
 | 
|---|
| [db6bf74] | 400 |         delete(neues);
 | 
|---|
| [042f82] | 401 |       }
 | 
|---|
| [e5c0a1] | 402 |       // when the input is in failed state, we most likely just read garbage
 | 
|---|
 | 403 |       if(input.fail()) {
 | 
|---|
| [47d041] | 404 |         ELOG(2, "Error parsing elements db.");
 | 
|---|
| [e5c0a1] | 405 |         status = false;
 | 
|---|
 | 406 |         break;
 | 
|---|
 | 407 |       }
 | 
|---|
| [042f82] | 408 |     }
 | 
|---|
| [61745cc] | 409 |   } else {
 | 
|---|
| [47d041] | 410 |     ELOG(1, "Could not open the database.");
 | 
|---|
| [ff73a2] | 411 |     status = false;
 | 
|---|
| [61745cc] | 412 |   }
 | 
|---|
| [47d041] | 413 |   //LOG(0, parsedElements.str());
 | 
|---|
| [ff73a2] | 414 | 
 | 
|---|
| [e5c0a1] | 415 |   if (!parsedElements.size())
 | 
|---|
| [ff73a2] | 416 |     status = false;
 | 
|---|
 | 417 | 
 | 
|---|
| [e5c0a1] | 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;
 | 
|---|
| [9f99b3] | 426 |         delete(iter->second);
 | 
|---|
| [e5c0a1] | 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 | 
 | 
|---|
| [ff73a2] | 440 |   return status;
 | 
|---|
| [4eb4fe] | 441 | }
 | 
|---|
| [6ac7ee] | 442 | 
 | 
|---|
| [67c92b] | 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;
 | 
|---|
| [47d041] | 459 |       //LOG(1, "INFO: Element " << Z << " has " << FindElement(Z)->Electronegativity << " valence electrons.");
 | 
|---|
| [67c92b] | 460 |     }
 | 
|---|
 | 461 |     return true;
 | 
|---|
 | 462 |   } else
 | 
|---|
 | 463 |     return false;
 | 
|---|
 | 464 | }
 | 
|---|
 | 465 | 
 | 
|---|
| [4eb4fe] | 466 | /** load the valence info.
 | 
|---|
 | 467 |  * \param *input stream to parse from
 | 
|---|
 | 468 |  * \return true - parsing successful, false - something went wrong
 | 
|---|
 | 469 |  */
 | 
|---|
| [67c92b] | 470 | bool periodentafel::LoadValenceDatabase(istream &input)
 | 
|---|
| [4eb4fe] | 471 | {
 | 
|---|
 | 472 |   char dummy[MAXSTRINGSIZE];
 | 
|---|
| [67c92b] | 473 |   if (!input.fail()) {
 | 
|---|
 | 474 |     input.getline(dummy, MAXSTRINGSIZE);
 | 
|---|
 | 475 |     while (!input.eof()) {
 | 
|---|
| [ead4e6] | 476 |       atomicNumber_t Z;
 | 
|---|
| [67c92b] | 477 |       input >> Z;
 | 
|---|
| [4eb4fe] | 478 |       ASSERT(elements.count(Z), "Element not present");
 | 
|---|
| [67c92b] | 479 |       input >> ws;
 | 
|---|
 | 480 |       input >> elements[Z]->Valence;
 | 
|---|
 | 481 |       input >> ws;
 | 
|---|
| [47d041] | 482 |       //LOG(3, "INFO: Element " << Z << " has " << FindElement(Z)->Valence << " valence electrons.");
 | 
|---|
| [042f82] | 483 |     }
 | 
|---|
| [4eb4fe] | 484 |     return true;
 | 
|---|
| [042f82] | 485 |   } else
 | 
|---|
| [4eb4fe] | 486 |                 return false;
 | 
|---|
 | 487 | }
 | 
|---|
| [6ac7ee] | 488 | 
 | 
|---|
| [4eb4fe] | 489 | /** load the orbitals info.
 | 
|---|
 | 490 |  * \param *input stream to parse from
 | 
|---|
 | 491 |  * \return true - parsing successful, false - something went wrong
 | 
|---|
 | 492 |  */
 | 
|---|
| [67c92b] | 493 | bool periodentafel::LoadOrbitalsDatabase(istream &input)
 | 
|---|
| [4eb4fe] | 494 | {
 | 
|---|
 | 495 |   char dummy[MAXSTRINGSIZE];
 | 
|---|
| [67c92b] | 496 |   if (!input.fail()) {
 | 
|---|
 | 497 |     input.getline(dummy, MAXSTRINGSIZE);
 | 
|---|
 | 498 |     while (!input.eof()) {
 | 
|---|
| [ead4e6] | 499 |       atomicNumber_t Z;
 | 
|---|
| [67c92b] | 500 |       input >> Z;
 | 
|---|
| [4eb4fe] | 501 |       ASSERT(elements.count(Z), "Element not present");
 | 
|---|
| [67c92b] | 502 |       input >> ws;
 | 
|---|
 | 503 |       input >> elements[Z]->NoValenceOrbitals;
 | 
|---|
 | 504 |       input >> ws;
 | 
|---|
| [47d041] | 505 |       //LOG(3, "Element " << Z << " has " << FindElement(Z)->NoValenceOrbitals << " number of singly occupied valence orbitals.");
 | 
|---|
| [042f82] | 506 |     }
 | 
|---|
| [4eb4fe] | 507 |     return true;
 | 
|---|
| [042f82] | 508 |   } else
 | 
|---|
| [4eb4fe] | 509 |     return false;
 | 
|---|
 | 510 | }
 | 
|---|
| [6ac7ee] | 511 | 
 | 
|---|
| [4eb4fe] | 512 | /** load the hbond angles info.
 | 
|---|
 | 513 |  * \param *input stream to parse from
 | 
|---|
 | 514 |  * \return true - parsing successful, false - something went wrong
 | 
|---|
 | 515 |  */
 | 
|---|
| [67c92b] | 516 | bool periodentafel::LoadHBondAngleDatabase(istream &input)
 | 
|---|
| [4eb4fe] | 517 | {
 | 
|---|
 | 518 |   char dummy[MAXSTRINGSIZE];
 | 
|---|
| [67c92b] | 519 |   if (!input.fail()) {
 | 
|---|
 | 520 |     input.getline(dummy, MAXSTRINGSIZE);
 | 
|---|
 | 521 |     while (!input.eof()) {
 | 
|---|
| [ead4e6] | 522 |       atomicNumber_t Z;
 | 
|---|
| [67c92b] | 523 |       input >> Z;
 | 
|---|
| [4eb4fe] | 524 |       ASSERT(elements.count(Z), "Element not present");
 | 
|---|
| [67c92b] | 525 |       input >> ws;
 | 
|---|
 | 526 |       input >> elements[Z]->HBondAngle[0];
 | 
|---|
 | 527 |       input >> elements[Z]->HBondAngle[1];
 | 
|---|
 | 528 |       input >> elements[Z]->HBondAngle[2];
 | 
|---|
 | 529 |       input >> ws;
 | 
|---|
| [47d041] | 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.");
 | 
|---|
| [042f82] | 531 |     }
 | 
|---|
| [4eb4fe] | 532 |     return true;
 | 
|---|
| [042f82] | 533 |   } else
 | 
|---|
| [4eb4fe] | 534 |                 return false;
 | 
|---|
 | 535 | }
 | 
|---|
| [6ac7ee] | 536 | 
 | 
|---|
| [4eb4fe] | 537 | /** load the hbond lengths info.
 | 
|---|
 | 538 |  * \param *input stream to parse from
 | 
|---|
 | 539 |  * \return true - parsing successful, false - something went wrong
 | 
|---|
 | 540 |  */
 | 
|---|
| [67c92b] | 541 | bool periodentafel::LoadHBondLengthsDatabase(istream &input)
 | 
|---|
| [4eb4fe] | 542 | {
 | 
|---|
 | 543 |   char dummy[MAXSTRINGSIZE];
 | 
|---|
| [67c92b] | 544 |   if (!input.fail()) {
 | 
|---|
 | 545 |     input.getline(dummy, MAXSTRINGSIZE);
 | 
|---|
 | 546 |     while (!input.eof()) {
 | 
|---|
| [ead4e6] | 547 |       atomicNumber_t Z;
 | 
|---|
| [67c92b] | 548 |       input >> Z;
 | 
|---|
| [4eb4fe] | 549 |       ASSERT(elements.count(Z), "Element not present");
 | 
|---|
| [67c92b] | 550 |       input >> ws;
 | 
|---|
 | 551 |       input >> elements[Z]->HBondDistance[0];
 | 
|---|
 | 552 |       input >> elements[Z]->HBondDistance[1];
 | 
|---|
 | 553 |       input >> elements[Z]->HBondDistance[2];
 | 
|---|
 | 554 |       input >> ws;
 | 
|---|
| [47d041] | 555 |       //LOG(3, "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen.");
 | 
|---|
| [042f82] | 556 |     }
 | 
|---|
| [4eb4fe] | 557 |     return true;
 | 
|---|
| [042f82] | 558 |   } else
 | 
|---|
| [4eb4fe] | 559 |                 return false;
 | 
|---|
 | 560 | }
 | 
|---|
| [6ac7ee] | 561 | 
 | 
|---|
| [064178] | 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;
 | 
|---|
| [907636] | 586 | //      {
 | 
|---|
 | 587 | //        const element * tmp = FindElement(Z);
 | 
|---|
 | 588 | //        LOG(0, "Element " << tmp->getName() << " has ("
 | 
|---|
| [064178] | 589 | //            << (int)tmp->color[0] << "," << (int)tmp->color[1] << "," << (int)tmp->color[2]
 | 
|---|
| [907636] | 590 | //            << ") colors.");
 | 
|---|
 | 591 | //      }
 | 
|---|
| [064178] | 592 |     }
 | 
|---|
 | 593 |     return true;
 | 
|---|
 | 594 |   } else
 | 
|---|
 | 595 |     return false;
 | 
|---|
 | 596 | }
 | 
|---|
 | 597 | 
 | 
|---|
| [6ac7ee] | 598 | /** Stores element list to file.
 | 
|---|
 | 599 |  */
 | 
|---|
| [989bf6] | 600 | bool periodentafel::StorePeriodentafel(const char *path) const
 | 
|---|
| [6ac7ee] | 601 | {
 | 
|---|
| [042f82] | 602 |   bool result = true;
 | 
|---|
 | 603 |   ofstream f;
 | 
|---|
 | 604 |   char filename[MAXSTRINGSIZE];
 | 
|---|
| [6ac7ee] | 605 | 
 | 
|---|
| [042f82] | 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;
 | 
|---|
| [ead4e6] | 613 |     for(const_iterator iter=elements.begin();iter!=elements.end();++iter){
 | 
|---|
 | 614 |          result = result && (*iter).second->Output(&f);
 | 
|---|
| [042f82] | 615 |     }
 | 
|---|
 | 616 |     f.close();
 | 
|---|
| [4eb4fe] | 617 |     return true;
 | 
|---|
| [042f82] | 618 |   } else
 | 
|---|
| [4eb4fe] | 619 |     return result;
 | 
|---|
| [6ac7ee] | 620 | };
 | 
|---|
| [b60804] | 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 | }
 | 
|---|