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