| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [bcf653] | 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 | {
 | 
|---|
| [ed26ae] | 62 |   atomicNumber_t Z = pointer->getAtomicNumber();
 | 
|---|
| [4eb4fe] | 63 |   ASSERT(!elements.count(Z), "Element is already present.");
 | 
|---|
| [ed26ae] | 64 |   if (pointer->getAtomicNumber() < 1 && pointer->getAtomicNumber() >= 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 | {
 | 
|---|
| [ed26ae] | 75 |   return RemoveElement(pointer->getAtomicNumber());
 | 
|---|
| [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 | 
 | 
|---|
| [47ed3d] | 183 | /** Prints element data to \a *out.
 | 
|---|
 | 184 |  * \param *out outstream
 | 
|---|
 | 185 |  */
 | 
|---|
 | 186 | void periodentafel::OutputElement(ostream * const out, const element *elem) const
 | 
|---|
 | 187 | {
 | 
|---|
 | 188 |   *out << elem->getName() << "\t";
 | 
|---|
 | 189 |   *out << elem->getSymbol() << "\t";
 | 
|---|
 | 190 |   *out << elem->getAtomicNumber() << "\t";
 | 
|---|
 | 191 |   *out << elem->getMass() << "\t";
 | 
|---|
 | 192 |   *out << elem->getCovalentRadius() << "\t";
 | 
|---|
 | 193 |   *out << elem->getVanDerWaalsRadius() << std::endl;
 | 
|---|
 | 194 |   //*out << elem->getSymbol() << "\t"  << fixed << setprecision(11) << showpoint << elem->getMass() << "g/mol\t" << elem->getName() << "\t" << elem->getSymbol() << "\t" << endl;
 | 
|---|
 | 195 | };
 | 
|---|
 | 196 | 
 | 
|---|
 | 197 | 
 | 
|---|
| [6ac7ee] | 198 | /** Prints period table to given stream.
 | 
|---|
 | 199 |  * \param output stream
 | 
|---|
 | 200 |  */
 | 
|---|
| [ead4e6] | 201 | bool periodentafel::Output(ostream * const output) const
 | 
|---|
| [6ac7ee] | 202 | {
 | 
|---|
| [042f82] | 203 |   if (output != NULL) {
 | 
|---|
| [47ed3d] | 204 |     for(elementSet::const_iterator iter = elements.begin(); iter != elements.end(); ++iter) {
 | 
|---|
 | 205 |       OutputElement(output, iter->second);
 | 
|---|
| [042f82] | 206 |     }
 | 
|---|
| [47ed3d] | 207 |     return true;
 | 
|---|
 | 208 |   }
 | 
|---|
 | 209 |   return false;
 | 
|---|
 | 210 | }
 | 
|---|
| [6ac7ee] | 211 | 
 | 
|---|
| [4ae823] | 212 | /** Scan periodentafel contents from internal databases.
 | 
|---|
 | 213 |  *
 | 
|---|
 | 214 |  */
 | 
|---|
 | 215 | void periodentafel::ScanPeriodentafel()
 | 
|---|
 | 216 | {
 | 
|---|
 | 217 |   {
 | 
|---|
 | 218 |     stringstream input(elementsDB,ios_base::in);
 | 
|---|
 | 219 | #ifndef NDEBUG
 | 
|---|
 | 220 |     bool status =
 | 
|---|
 | 221 | #endif
 | 
|---|
 | 222 |         LoadElementsDatabase(input);
 | 
|---|
 | 223 |     ASSERT(status,  "General element initialization failed");
 | 
|---|
 | 224 |   }
 | 
|---|
 | 225 |   {
 | 
|---|
 | 226 |     stringstream input(ElectronegativitiesDB,ios_base::in);
 | 
|---|
 | 227 | #ifndef NDEBUG
 | 
|---|
 | 228 |     bool status =
 | 
|---|
 | 229 | #endif
 | 
|---|
 | 230 |         LoadElectronegativityDatabase(input);
 | 
|---|
 | 231 |     ASSERT(status, "Electronegativities entry of element initialization failed");
 | 
|---|
 | 232 |   }
 | 
|---|
 | 233 |   {
 | 
|---|
 | 234 |     stringstream input(valenceDB,ios_base::in);
 | 
|---|
 | 235 | #ifndef NDEBUG
 | 
|---|
 | 236 |     bool status =
 | 
|---|
 | 237 | #endif
 | 
|---|
 | 238 |         LoadValenceDatabase(input);
 | 
|---|
 | 239 |     ASSERT(status, "Valence entry of element initialization failed");
 | 
|---|
 | 240 |   }
 | 
|---|
 | 241 |   {
 | 
|---|
 | 242 |     stringstream input(orbitalsDB,ios_base::in);
 | 
|---|
 | 243 | #ifndef NDEBUG
 | 
|---|
 | 244 |     bool status =
 | 
|---|
 | 245 | #endif
 | 
|---|
 | 246 |         LoadOrbitalsDatabase(input);
 | 
|---|
 | 247 |     ASSERT(status, "Orbitals entry of element initialization failed");
 | 
|---|
 | 248 |   }
 | 
|---|
 | 249 |   {
 | 
|---|
 | 250 |     stringstream input(HbondangleDB,ios_base::in);
 | 
|---|
 | 251 | #ifndef NDEBUG
 | 
|---|
 | 252 |     bool status =
 | 
|---|
 | 253 | #endif
 | 
|---|
 | 254 |         LoadHBondAngleDatabase(input);
 | 
|---|
 | 255 |     ASSERT(status, "HBond angle entry of element initialization failed");
 | 
|---|
 | 256 |   }
 | 
|---|
 | 257 |   {
 | 
|---|
 | 258 |     stringstream input(HbonddistanceDB,ios_base::in);
 | 
|---|
 | 259 | #ifndef NDEBUG
 | 
|---|
 | 260 |     bool status =
 | 
|---|
 | 261 | #endif
 | 
|---|
 | 262 |         LoadHBondLengthsDatabase(input);
 | 
|---|
 | 263 |     ASSERT(status, "HBond distance entry of element initialization failed");
 | 
|---|
 | 264 |   }
 | 
|---|
 | 265 |   {
 | 
|---|
 | 266 |     stringstream input(ColorDB,ios_base::in);
 | 
|---|
 | 267 | #ifndef NDEBUG
 | 
|---|
 | 268 |     bool status =
 | 
|---|
 | 269 | #endif
 | 
|---|
 | 270 |         LoadColorDatabase(input);
 | 
|---|
 | 271 |     ASSERT(status, "color entry of element initialization failed");
 | 
|---|
 | 272 |   }
 | 
|---|
 | 273 | }
 | 
|---|
 | 274 | 
 | 
|---|
| [6ac7ee] | 275 | /** Loads element list from file.
 | 
|---|
 | 276 |  * \param *path to to standard file names
 | 
|---|
 | 277 |  */
 | 
|---|
| [989bf6] | 278 | bool periodentafel::LoadPeriodentafel(const char *path)
 | 
|---|
| [6ac7ee] | 279 | {
 | 
|---|
| [4eb4fe] | 280 |   ifstream input;
 | 
|---|
| [042f82] | 281 |   bool status = true;
 | 
|---|
 | 282 |   bool otherstatus = true;
 | 
|---|
 | 283 |   char filename[255];
 | 
|---|
| [6ac7ee] | 284 | 
 | 
|---|
| [042f82] | 285 |   // fill elements DB
 | 
|---|
 | 286 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
 | 287 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 288 |   strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
| [4eb4fe] | 289 |   input.open(filename);
 | 
|---|
| [61745cc] | 290 |   if (!input.fail())
 | 
|---|
| [47d041] | 291 |     LOG(0, "Using " << filename << " as elements database.");
 | 
|---|
| [e5c0a1] | 292 |   status = status && LoadElementsDatabase(input);
 | 
|---|
| [61745cc] | 293 |   input.close();
 | 
|---|
 | 294 |   input.clear();
 | 
|---|
| [4eb4fe] | 295 | 
 | 
|---|
| [67c92b] | 296 |   // fill valence DB per element
 | 
|---|
 | 297 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
 | 298 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 299 |   strncat(filename, STANDARDELECTRONEGATIVITYDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 300 |   input.open(filename);
 | 
|---|
 | 301 |   if (!input.fail())
 | 
|---|
| [47d041] | 302 |     LOG(0, "Using " << filename << " as electronegativity database.");
 | 
|---|
| [67c92b] | 303 |   otherstatus = otherstatus && LoadElectronegativityDatabase(input);
 | 
|---|
 | 304 |   input.close();
 | 
|---|
 | 305 |   input.clear();
 | 
|---|
 | 306 | 
 | 
|---|
| [4eb4fe] | 307 |   // fill valence DB per element
 | 
|---|
 | 308 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
 | 309 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 310 |   strncat(filename, STANDARDVALENCEDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 311 |   input.open(filename);
 | 
|---|
| [61745cc] | 312 |   if (!input.fail())
 | 
|---|
| [47d041] | 313 |     LOG(0, "Using " << filename << " as valence database.");
 | 
|---|
| [67c92b] | 314 |   otherstatus = otherstatus && LoadValenceDatabase(input);
 | 
|---|
| [61745cc] | 315 |   input.close();
 | 
|---|
 | 316 |   input.clear();
 | 
|---|
| [4eb4fe] | 317 | 
 | 
|---|
 | 318 |   // fill orbitals DB per element
 | 
|---|
 | 319 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
 | 320 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 321 |   strncat(filename, STANDARDORBITALDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 322 |   input.open(filename);
 | 
|---|
| [61745cc] | 323 |   if (!input.fail())
 | 
|---|
| [47d041] | 324 |     LOG(0, "Using " << filename << " as orbitals database.");
 | 
|---|
| [67c92b] | 325 |   otherstatus = otherstatus && LoadOrbitalsDatabase(input);
 | 
|---|
| [61745cc] | 326 |   input.close();
 | 
|---|
 | 327 |   input.clear();
 | 
|---|
| [4eb4fe] | 328 | 
 | 
|---|
 | 329 |   // fill H-BondAngle DB per element
 | 
|---|
 | 330 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
 | 331 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 332 |   strncat(filename, STANDARDHBONDANGLEDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 333 |   input.open(filename);
 | 
|---|
| [61745cc] | 334 |   if (!input.fail())
 | 
|---|
| [47d041] | 335 |     LOG(0, "Using " << filename << " as H bond angle database.");
 | 
|---|
| [67c92b] | 336 |   otherstatus = otherstatus && LoadHBondAngleDatabase(input);
 | 
|---|
| [61745cc] | 337 |   input.close();
 | 
|---|
 | 338 |   input.clear();
 | 
|---|
| [4eb4fe] | 339 | 
 | 
|---|
 | 340 |   // fill H-BondDistance DB per element
 | 
|---|
 | 341 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
 | 342 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 343 |   strncat(filename, STANDARDHBONDDISTANCEDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 344 |   input.open(filename);
 | 
|---|
| [61745cc] | 345 |   if (!input.fail())
 | 
|---|
| [47d041] | 346 |     LOG(0, "Using " << filename << " as H bond length database.");
 | 
|---|
| [67c92b] | 347 |   otherstatus = otherstatus && LoadHBondLengthsDatabase(input);
 | 
|---|
| [61745cc] | 348 |   input.close();
 | 
|---|
 | 349 |   input.clear();
 | 
|---|
| [4eb4fe] | 350 | 
 | 
|---|
| [3613a2] | 351 |   // fill color DB per element
 | 
|---|
 | 352 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
 | 353 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 354 |   strncat(filename, STANDARDCOLORDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 355 |   input.open(filename);
 | 
|---|
 | 356 |   if (!input.fail())
 | 
|---|
| [47d041] | 357 |     LOG(0, "Using " << filename << " as color database.");
 | 
|---|
| [3613a2] | 358 |   otherstatus = otherstatus && LoadColorDatabase(input);
 | 
|---|
 | 359 |   input.close();
 | 
|---|
 | 360 |   input.clear();
 | 
|---|
 | 361 | 
 | 
|---|
| [4eb4fe] | 362 |   if (!otherstatus){
 | 
|---|
| [47d041] | 363 |     ELOG(2, "Something went wrong while parsing the other databases!");
 | 
|---|
| [4eb4fe] | 364 |   }
 | 
|---|
 | 365 | 
 | 
|---|
 | 366 |   return status;
 | 
|---|
 | 367 | };
 | 
|---|
 | 368 | 
 | 
|---|
 | 369 | /** load the element info.
 | 
|---|
 | 370 |  * \param *input stream to parse from
 | 
|---|
 | 371 |  * \return true - parsing successful, false - something went wrong
 | 
|---|
 | 372 |  */
 | 
|---|
| [e5c0a1] | 373 | bool periodentafel::LoadElementsDatabase(istream &input)
 | 
|---|
| [4eb4fe] | 374 | {
 | 
|---|
| [ff73a2] | 375 |   bool status = true;
 | 
|---|
| [e5c0a1] | 376 |   string header1tmp,header2tmp;
 | 
|---|
| [47d041] | 377 | //  std::stringstream parsedelements;
 | 
|---|
| [e5c0a1] | 378 |   // first parse into a map, so we can revert to old status in case something goes wront
 | 
|---|
 | 379 |   map<atomicNumber_t,element*> parsedElements;
 | 
|---|
 | 380 |   if (!input.fail()) {
 | 
|---|
 | 381 |     getline(input,header1tmp);
 | 
|---|
 | 382 |     getline(input,header2tmp); // skip first two header lines
 | 
|---|
| [4e6d74] | 383 |     //cout << "First header: " << header1tmp << endl;
 | 
|---|
 | 384 |     //cout << "Second header: " << header2tmp << endl;
 | 
|---|
| [47d041] | 385 | //    parsedelements <<  "Parsed elements:");
 | 
|---|
| [e5c0a1] | 386 |     while (!input.eof()) {
 | 
|---|
| [042f82] | 387 |       element *neues = new element;
 | 
|---|
| [83f176] | 388 |       input >> neues->name;
 | 
|---|
| [67c92b] | 389 |       //input >> ws;
 | 
|---|
| [83f176] | 390 |       input >> neues->symbol;
 | 
|---|
| [67c92b] | 391 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 392 |       input >> neues->period;
 | 
|---|
| [67c92b] | 393 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 394 |       input >> neues->group;
 | 
|---|
| [67c92b] | 395 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 396 |       input >> neues->block;
 | 
|---|
| [67c92b] | 397 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 398 |       input >> neues->Z;
 | 
|---|
| [67c92b] | 399 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 400 |       input >> neues->mass;
 | 
|---|
| [67c92b] | 401 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 402 |       input >> neues->CovalentRadius;
 | 
|---|
| [67c92b] | 403 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 404 |       input >> neues->VanDerWaalsRadius;
 | 
|---|
| [67c92b] | 405 |       //input >> ws;
 | 
|---|
| [e5c0a1] | 406 |       input >> ws;
 | 
|---|
| [042f82] | 407 |       //neues->Output((ofstream *)&cout);
 | 
|---|
| [ed26ae] | 408 |       if ((neues->getAtomicNumber() > 0) && (neues->getAtomicNumber() < MAX_ELEMENTS)) {
 | 
|---|
| [e5c0a1] | 409 |         parsedElements[neues->Z] = neues;
 | 
|---|
| [47d041] | 410 | //        parsedelements << " " << *neues);
 | 
|---|
| [ff73a2] | 411 |       } else {
 | 
|---|
| [47d041] | 412 |         ELOG(2, "Detected empty line or invalid element in elements db, discarding.");
 | 
|---|
 | 413 | //        parsedelements << " <?>");
 | 
|---|
| [db6bf74] | 414 |         delete(neues);
 | 
|---|
| [042f82] | 415 |       }
 | 
|---|
| [e5c0a1] | 416 |       // when the input is in failed state, we most likely just read garbage
 | 
|---|
 | 417 |       if(input.fail()) {
 | 
|---|
| [47d041] | 418 |         ELOG(2, "Error parsing elements db.");
 | 
|---|
| [e5c0a1] | 419 |         status = false;
 | 
|---|
 | 420 |         break;
 | 
|---|
 | 421 |       }
 | 
|---|
| [042f82] | 422 |     }
 | 
|---|
| [61745cc] | 423 |   } else {
 | 
|---|
| [47d041] | 424 |     ELOG(1, "Could not open the database.");
 | 
|---|
| [ff73a2] | 425 |     status = false;
 | 
|---|
| [61745cc] | 426 |   }
 | 
|---|
| [47d041] | 427 |   //LOG(0, parsedElements.str());
 | 
|---|
| [ff73a2] | 428 | 
 | 
|---|
| [e5c0a1] | 429 |   if (!parsedElements.size())
 | 
|---|
| [ff73a2] | 430 |     status = false;
 | 
|---|
 | 431 | 
 | 
|---|
| [e5c0a1] | 432 |   if(status){
 | 
|---|
 | 433 |     for(map<atomicNumber_t,element*>::iterator iter=parsedElements.begin();
 | 
|---|
 | 434 |                                                iter!=parsedElements.end();
 | 
|---|
 | 435 |                                                ++iter){
 | 
|---|
 | 436 |       if (elements.count(iter->first)) {
 | 
|---|
 | 437 |         // if element already present, replace the old one
 | 
|---|
 | 438 |         // pointer to old element might still be in use, so we have to replace into the old element
 | 
|---|
 | 439 |         *(elements[iter->first])=*iter->second;
 | 
|---|
| [9f99b3] | 440 |         delete(iter->second);
 | 
|---|
| [e5c0a1] | 441 |       }
 | 
|---|
 | 442 |       else {
 | 
|---|
 | 443 |         // no such element in periodentafel... we can just insert
 | 
|---|
 | 444 |         elements[iter->first] = iter->second;
 | 
|---|
 | 445 |       }
 | 
|---|
 | 446 |     }
 | 
|---|
 | 447 |     // all went well.. we now copy the header
 | 
|---|
 | 448 |     strncpy(header1,header1tmp.c_str(),MAXSTRINGSIZE);
 | 
|---|
 | 449 |     header1[MAXSTRINGSIZE-1]=0;
 | 
|---|
 | 450 |     strncpy(header2,header2tmp.c_str(),MAXSTRINGSIZE);
 | 
|---|
 | 451 |     header2[MAXSTRINGSIZE-1]=0;
 | 
|---|
 | 452 |   }
 | 
|---|
 | 453 | 
 | 
|---|
| [ff73a2] | 454 |   return status;
 | 
|---|
| [4eb4fe] | 455 | }
 | 
|---|
| [6ac7ee] | 456 | 
 | 
|---|
| [67c92b] | 457 | /** load the electronegativity info.
 | 
|---|
 | 458 |  * \param *input stream to parse from
 | 
|---|
 | 459 |  * \return true - parsing successful, false - something went wrong
 | 
|---|
 | 460 |  */
 | 
|---|
 | 461 | bool periodentafel::LoadElectronegativityDatabase(std::istream &input)
 | 
|---|
 | 462 | {
 | 
|---|
 | 463 |   char dummy[MAXSTRINGSIZE];
 | 
|---|
 | 464 |   if (!input.fail()) {
 | 
|---|
 | 465 |     input.getline(dummy, MAXSTRINGSIZE);
 | 
|---|
 | 466 |     while (!input.eof()) {
 | 
|---|
 | 467 |       atomicNumber_t Z;
 | 
|---|
 | 468 |       input >> Z;
 | 
|---|
 | 469 |       ASSERT(elements.count(Z), "Element not present");
 | 
|---|
 | 470 |       input >> ws;
 | 
|---|
 | 471 |       input >> elements[Z]->Electronegativity;
 | 
|---|
 | 472 |       input >> ws;
 | 
|---|
| [47d041] | 473 |       //LOG(1, "INFO: Element " << Z << " has " << FindElement(Z)->Electronegativity << " valence electrons.");
 | 
|---|
| [67c92b] | 474 |     }
 | 
|---|
 | 475 |     return true;
 | 
|---|
 | 476 |   } else
 | 
|---|
 | 477 |     return false;
 | 
|---|
 | 478 | }
 | 
|---|
 | 479 | 
 | 
|---|
| [4eb4fe] | 480 | /** load the valence info.
 | 
|---|
 | 481 |  * \param *input stream to parse from
 | 
|---|
 | 482 |  * \return true - parsing successful, false - something went wrong
 | 
|---|
 | 483 |  */
 | 
|---|
| [67c92b] | 484 | bool periodentafel::LoadValenceDatabase(istream &input)
 | 
|---|
| [4eb4fe] | 485 | {
 | 
|---|
 | 486 |   char dummy[MAXSTRINGSIZE];
 | 
|---|
| [67c92b] | 487 |   if (!input.fail()) {
 | 
|---|
 | 488 |     input.getline(dummy, MAXSTRINGSIZE);
 | 
|---|
 | 489 |     while (!input.eof()) {
 | 
|---|
| [ead4e6] | 490 |       atomicNumber_t Z;
 | 
|---|
| [67c92b] | 491 |       input >> Z;
 | 
|---|
| [4eb4fe] | 492 |       ASSERT(elements.count(Z), "Element not present");
 | 
|---|
| [67c92b] | 493 |       input >> ws;
 | 
|---|
 | 494 |       input >> elements[Z]->Valence;
 | 
|---|
 | 495 |       input >> ws;
 | 
|---|
| [47d041] | 496 |       //LOG(3, "INFO: Element " << Z << " has " << FindElement(Z)->Valence << " valence electrons.");
 | 
|---|
| [042f82] | 497 |     }
 | 
|---|
| [4eb4fe] | 498 |     return true;
 | 
|---|
| [042f82] | 499 |   } else
 | 
|---|
| [4eb4fe] | 500 |                 return false;
 | 
|---|
 | 501 | }
 | 
|---|
| [6ac7ee] | 502 | 
 | 
|---|
| [4eb4fe] | 503 | /** load the orbitals info.
 | 
|---|
 | 504 |  * \param *input stream to parse from
 | 
|---|
 | 505 |  * \return true - parsing successful, false - something went wrong
 | 
|---|
 | 506 |  */
 | 
|---|
| [67c92b] | 507 | bool periodentafel::LoadOrbitalsDatabase(istream &input)
 | 
|---|
| [4eb4fe] | 508 | {
 | 
|---|
 | 509 |   char dummy[MAXSTRINGSIZE];
 | 
|---|
| [67c92b] | 510 |   if (!input.fail()) {
 | 
|---|
 | 511 |     input.getline(dummy, MAXSTRINGSIZE);
 | 
|---|
 | 512 |     while (!input.eof()) {
 | 
|---|
| [ead4e6] | 513 |       atomicNumber_t Z;
 | 
|---|
| [67c92b] | 514 |       input >> Z;
 | 
|---|
| [4eb4fe] | 515 |       ASSERT(elements.count(Z), "Element not present");
 | 
|---|
| [67c92b] | 516 |       input >> ws;
 | 
|---|
 | 517 |       input >> elements[Z]->NoValenceOrbitals;
 | 
|---|
 | 518 |       input >> ws;
 | 
|---|
| [47d041] | 519 |       //LOG(3, "Element " << Z << " has " << FindElement(Z)->NoValenceOrbitals << " number of singly occupied valence orbitals.");
 | 
|---|
| [042f82] | 520 |     }
 | 
|---|
| [4eb4fe] | 521 |     return true;
 | 
|---|
| [042f82] | 522 |   } else
 | 
|---|
| [4eb4fe] | 523 |     return false;
 | 
|---|
 | 524 | }
 | 
|---|
| [6ac7ee] | 525 | 
 | 
|---|
| [4eb4fe] | 526 | /** load the hbond angles info.
 | 
|---|
 | 527 |  * \param *input stream to parse from
 | 
|---|
 | 528 |  * \return true - parsing successful, false - something went wrong
 | 
|---|
 | 529 |  */
 | 
|---|
| [67c92b] | 530 | bool periodentafel::LoadHBondAngleDatabase(istream &input)
 | 
|---|
| [4eb4fe] | 531 | {
 | 
|---|
 | 532 |   char dummy[MAXSTRINGSIZE];
 | 
|---|
| [67c92b] | 533 |   if (!input.fail()) {
 | 
|---|
 | 534 |     input.getline(dummy, MAXSTRINGSIZE);
 | 
|---|
 | 535 |     while (!input.eof()) {
 | 
|---|
| [ead4e6] | 536 |       atomicNumber_t Z;
 | 
|---|
| [67c92b] | 537 |       input >> Z;
 | 
|---|
| [4eb4fe] | 538 |       ASSERT(elements.count(Z), "Element not present");
 | 
|---|
| [67c92b] | 539 |       input >> ws;
 | 
|---|
 | 540 |       input >> elements[Z]->HBondAngle[0];
 | 
|---|
 | 541 |       input >> elements[Z]->HBondAngle[1];
 | 
|---|
 | 542 |       input >> elements[Z]->HBondAngle[2];
 | 
|---|
 | 543 |       input >> ws;
 | 
|---|
| [47d041] | 544 |       //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] | 545 |     }
 | 
|---|
| [4eb4fe] | 546 |     return true;
 | 
|---|
| [042f82] | 547 |   } else
 | 
|---|
| [4eb4fe] | 548 |                 return false;
 | 
|---|
 | 549 | }
 | 
|---|
| [6ac7ee] | 550 | 
 | 
|---|
| [4eb4fe] | 551 | /** load the hbond lengths info.
 | 
|---|
 | 552 |  * \param *input stream to parse from
 | 
|---|
 | 553 |  * \return true - parsing successful, false - something went wrong
 | 
|---|
 | 554 |  */
 | 
|---|
| [67c92b] | 555 | bool periodentafel::LoadHBondLengthsDatabase(istream &input)
 | 
|---|
| [4eb4fe] | 556 | {
 | 
|---|
 | 557 |   char dummy[MAXSTRINGSIZE];
 | 
|---|
| [67c92b] | 558 |   if (!input.fail()) {
 | 
|---|
 | 559 |     input.getline(dummy, MAXSTRINGSIZE);
 | 
|---|
 | 560 |     while (!input.eof()) {
 | 
|---|
| [ead4e6] | 561 |       atomicNumber_t Z;
 | 
|---|
| [67c92b] | 562 |       input >> Z;
 | 
|---|
| [4eb4fe] | 563 |       ASSERT(elements.count(Z), "Element not present");
 | 
|---|
| [67c92b] | 564 |       input >> ws;
 | 
|---|
 | 565 |       input >> elements[Z]->HBondDistance[0];
 | 
|---|
 | 566 |       input >> elements[Z]->HBondDistance[1];
 | 
|---|
 | 567 |       input >> elements[Z]->HBondDistance[2];
 | 
|---|
 | 568 |       input >> ws;
 | 
|---|
| [47d041] | 569 |       //LOG(3, "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen.");
 | 
|---|
| [042f82] | 570 |     }
 | 
|---|
| [4eb4fe] | 571 |     return true;
 | 
|---|
| [042f82] | 572 |   } else
 | 
|---|
| [4eb4fe] | 573 |                 return false;
 | 
|---|
 | 574 | }
 | 
|---|
| [6ac7ee] | 575 | 
 | 
|---|
| [064178] | 576 | /** load the color info.
 | 
|---|
 | 577 |  * \param *input stream to parse from
 | 
|---|
 | 578 |  * \return true - parsing successful, false - something went wrong
 | 
|---|
 | 579 |  */
 | 
|---|
 | 580 | bool periodentafel::LoadColorDatabase(istream &input)
 | 
|---|
 | 581 | {
 | 
|---|
 | 582 |   char dummy[MAXSTRINGSIZE];
 | 
|---|
 | 583 |   if (!input.fail()) {
 | 
|---|
 | 584 |     input.getline(dummy, MAXSTRINGSIZE);
 | 
|---|
 | 585 |     while (!input.eof()) {
 | 
|---|
 | 586 |       atomicNumber_t Z;
 | 
|---|
 | 587 |       input >> Z;
 | 
|---|
 | 588 |       ASSERT(elements.count(Z), "Element not present");
 | 
|---|
 | 589 |       input >> ws;
 | 
|---|
 | 590 |       input >> dummy;
 | 
|---|
 | 591 |       {
 | 
|---|
 | 592 |         int tmpcolor;   // char here will only parse a single char (i.e. only "2" out of "255")
 | 
|---|
 | 593 |         for (int i=0;i<3;++i) {
 | 
|---|
 | 594 |           input >> ws;
 | 
|---|
 | 595 |           input >> tmpcolor;
 | 
|---|
 | 596 |           elements[Z]->color[i] = (unsigned char)tmpcolor;
 | 
|---|
 | 597 |         }
 | 
|---|
 | 598 |       }
 | 
|---|
 | 599 |       input >> ws;
 | 
|---|
| [907636] | 600 | //      {
 | 
|---|
 | 601 | //        const element * tmp = FindElement(Z);
 | 
|---|
 | 602 | //        LOG(0, "Element " << tmp->getName() << " has ("
 | 
|---|
| [064178] | 603 | //            << (int)tmp->color[0] << "," << (int)tmp->color[1] << "," << (int)tmp->color[2]
 | 
|---|
| [907636] | 604 | //            << ") colors.");
 | 
|---|
 | 605 | //      }
 | 
|---|
| [064178] | 606 |     }
 | 
|---|
 | 607 |     return true;
 | 
|---|
 | 608 |   } else
 | 
|---|
 | 609 |     return false;
 | 
|---|
 | 610 | }
 | 
|---|
 | 611 | 
 | 
|---|
| [6ac7ee] | 612 | /** Stores element list to file.
 | 
|---|
 | 613 |  */
 | 
|---|
| [989bf6] | 614 | bool periodentafel::StorePeriodentafel(const char *path) const
 | 
|---|
| [6ac7ee] | 615 | {
 | 
|---|
| [042f82] | 616 |   bool result = true;
 | 
|---|
 | 617 |   ofstream f;
 | 
|---|
 | 618 |   char filename[MAXSTRINGSIZE];
 | 
|---|
| [6ac7ee] | 619 | 
 | 
|---|
| [042f82] | 620 |   strncpy(filename, path, MAXSTRINGSIZE);
 | 
|---|
 | 621 |   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 622 |   strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
 | 
|---|
 | 623 |   f.open(filename);
 | 
|---|
 | 624 |   if (f != NULL) {
 | 
|---|
 | 625 |     f << header1 << endl;
 | 
|---|
 | 626 |     f << header2 << endl;
 | 
|---|
| [ead4e6] | 627 |     for(const_iterator iter=elements.begin();iter!=elements.end();++iter){
 | 
|---|
| [47ed3d] | 628 |       OutputElement(&f, iter->second);
 | 
|---|
| [042f82] | 629 |     }
 | 
|---|
 | 630 |     f.close();
 | 
|---|
| [4eb4fe] | 631 |     return true;
 | 
|---|
| [042f82] | 632 |   } else
 | 
|---|
| [4eb4fe] | 633 |     return result;
 | 
|---|
| [6ac7ee] | 634 | };
 | 
|---|
| [b60804] | 635 | 
 | 
|---|
 | 636 | /** Comparison operator for periodentafel.
 | 
|---|
 | 637 |  *
 | 
|---|
 | 638 |  * @param other other instance to compare to
 | 
|---|
 | 639 |  * @return true when both contain same elements
 | 
|---|
 | 640 |  */
 | 
|---|
 | 641 | bool periodentafel::operator==(const periodentafel &other) const
 | 
|---|
 | 642 | {
 | 
|---|
 | 643 |   // there are only pointers in the elementSet, hence we have to compare ourselves
 | 
|---|
 | 644 |   if (elements.size() != other.elements.size()) return false;
 | 
|---|
 | 645 |   const_iterator iter = elements.begin();
 | 
|---|
 | 646 |   const_iterator otheriter = other.elements.begin();
 | 
|---|
 | 647 |   for (;(iter != elements.end()) && (otheriter != other.elements.end());
 | 
|---|
 | 648 |       ++iter, ++otheriter) {
 | 
|---|
 | 649 |     bool status = true;
 | 
|---|
 | 650 |     status = status && (iter->first == otheriter->first);
 | 
|---|
 | 651 |     status = status && (*(iter->second) == *(otheriter->second));
 | 
|---|
 | 652 |     if (!status) {
 | 
|---|
 | 653 |       std::cout << *(iter->second) << " not equal to " << *(otheriter->second) << "." << std::endl;
 | 
|---|
 | 654 |       return false;
 | 
|---|
 | 655 |     }
 | 
|---|
 | 656 | //    else
 | 
|---|
 | 657 | //      std::cout << (iter->second)->getName() << " are equal to " << (otheriter->second)->getName() << "." << std::endl;
 | 
|---|
 | 658 |   }
 | 
|---|
 | 659 |   if (strncmp(header1, other.header1, MAXSTRINGSIZE) != 0) return false;
 | 
|---|
 | 660 |   if (strncmp(header2, other.header2, MAXSTRINGSIZE) != 0) return false;
 | 
|---|
 | 661 |   return true;
 | 
|---|
 | 662 | }
 | 
|---|