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