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