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