[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"
|
---|
[76096d] | 46 | #include "Helpers/defs.hpp"
|
---|
[6ac7ee] | 47 | #include "periodentafel.hpp"
|
---|
| 48 |
|
---|
[ead4e6] | 49 | using namespace std;
|
---|
| 50 |
|
---|
[6ac7ee] | 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 | */
|
---|
[4ae823] | 56 | periodentafel::periodentafel(const bool DoLoad)
|
---|
[4eb4fe] | 57 | {
|
---|
[4ae823] | 58 | if (DoLoad) {
|
---|
| 59 | ScanPeriodentafel();
|
---|
[064178] | 60 | }
|
---|
[4eb4fe] | 61 | };
|
---|
[6ac7ee] | 62 |
|
---|
| 63 | /** destructor for class periodentafel
|
---|
| 64 | * Removes every element and afterwards deletes start and end of list.
|
---|
[42af9e] | 65 | * TODO: Handle when elements have changed and store databases then
|
---|
[6ac7ee] | 66 | */
|
---|
| 67 | periodentafel::~periodentafel()
|
---|
| 68 | {
|
---|
[042f82] | 69 | CleanupPeriodtable();
|
---|
[6ac7ee] | 70 | };
|
---|
| 71 |
|
---|
| 72 | /** Adds element to period table list
|
---|
| 73 | * \param *pointer element to be added
|
---|
[4eb4fe] | 74 | * \return iterator to added element
|
---|
[6ac7ee] | 75 | */
|
---|
[e5c0a1] | 76 | periodentafel::iterator periodentafel::AddElement(element * pointer)
|
---|
[6ac7ee] | 77 | {
|
---|
[ed26ae] | 78 | atomicNumber_t Z = pointer->getAtomicNumber();
|
---|
[4eb4fe] | 79 | ASSERT(!elements.count(Z), "Element is already present.");
|
---|
[ed26ae] | 80 | if (pointer->getAtomicNumber() < 1 && pointer->getAtomicNumber() >= MAX_ELEMENTS)
|
---|
[47d041] | 81 | ELOG(0, "Invalid Z number!");
|
---|
[ead4e6] | 82 | pair<iterator,bool> res = elements.insert(pair<atomicNumber_t,element*>(Z,pointer));
|
---|
| 83 | return res.first;
|
---|
[6ac7ee] | 84 | };
|
---|
| 85 |
|
---|
| 86 | /** Removes element from list.
|
---|
| 87 | * \param *pointer element to be removed
|
---|
| 88 | */
|
---|
[e5c0a1] | 89 | size_t periodentafel::RemoveElement(const element * pointer)
|
---|
[6ac7ee] | 90 | {
|
---|
[ed26ae] | 91 | return RemoveElement(pointer->getAtomicNumber());
|
---|
[4eb4fe] | 92 | };
|
---|
| 93 |
|
---|
| 94 | /** Removes element from list.
|
---|
| 95 | * \param Z element to be removed
|
---|
| 96 | */
|
---|
[61745cc] | 97 | size_t periodentafel::RemoveElement(atomicNumber_t Z)
|
---|
[4eb4fe] | 98 | {
|
---|
[61745cc] | 99 | return elements.erase(Z);
|
---|
[6ac7ee] | 100 | };
|
---|
| 101 |
|
---|
| 102 | /** Removes every element from the period table.
|
---|
| 103 | */
|
---|
[ead4e6] | 104 | void periodentafel::CleanupPeriodtable()
|
---|
[6ac7ee] | 105 | {
|
---|
[745a85] | 106 | for(iterator iter=elements.begin();iter!=elements.end();++iter){
|
---|
| 107 | delete(*iter).second;
|
---|
| 108 | }
|
---|
[ead4e6] | 109 | elements.clear();
|
---|
[6ac7ee] | 110 | };
|
---|
| 111 |
|
---|
| 112 | /** Finds an element by its atomic number.
|
---|
[fb73b8] | 113 | * If element is not yet in list, returns NULL.
|
---|
[6ac7ee] | 114 | * \param Z atomic number
|
---|
[fb73b8] | 115 | * \return pointer to element or NULL if not found
|
---|
[6ac7ee] | 116 | */
|
---|
[e5c0a1] | 117 | const element * periodentafel::FindElement(atomicNumber_t Z) const
|
---|
[6ac7ee] | 118 | {
|
---|
[ead4e6] | 119 | const_iterator res = elements.find(Z);
|
---|
| 120 | return res!=elements.end()?((*res).second):0;
|
---|
[6ac7ee] | 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 | */
|
---|
[e5c0a1] | 128 | const element * periodentafel::FindElement(const string &shorthand) const
|
---|
[6ac7ee] | 129 | {
|
---|
[ead4e6] | 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 | }
|
---|
[042f82] | 136 | }
|
---|
[ead4e6] | 137 | return res;
|
---|
[6ac7ee] | 138 | };
|
---|
| 139 |
|
---|
| 140 | /** Asks for element number and returns pointer to element
|
---|
[4eb4fe] | 141 | * \return desired element or NULL
|
---|
[6ac7ee] | 142 | */
|
---|
[e5c0a1] | 143 | const element * periodentafel::AskElement() const
|
---|
[6ac7ee] | 144 | {
|
---|
[e5c0a1] | 145 | const element * walker = NULL;
|
---|
[042f82] | 146 | int Z;
|
---|
| 147 | do {
|
---|
[47d041] | 148 | std::cout << "Atomic number Z: ";
|
---|
| 149 | std::cin >> Z;
|
---|
[042f82] | 150 | walker = this->FindElement(Z); // give type
|
---|
| 151 | } while (walker == NULL);
|
---|
| 152 | return walker;
|
---|
[6ac7ee] | 153 | };
|
---|
| 154 |
|
---|
[fb73b8] | 155 | /** Asks for element and if not found, presents mask to enter info.
|
---|
| 156 | * \return pointer to either present or newly created element
|
---|
| 157 | */
|
---|
[e5c0a1] | 158 | const element * periodentafel::EnterElement()
|
---|
[fb73b8] | 159 | {
|
---|
[ead4e6] | 160 | atomicNumber_t Z = 0;
|
---|
[47d041] | 161 | std::cout << "Atomic number: " << Z;
|
---|
[fb73b8] | 162 | cin >> Z;
|
---|
[e5c0a1] | 163 | const element *res = FindElement(Z);
|
---|
[ead4e6] | 164 | if (!res) {
|
---|
| 165 | // TODO: make this using the constructor
|
---|
[47d041] | 166 | std::cout << "Element not found in database, please enter." << std::endl;
|
---|
[4eb4fe] | 167 | element *tmp = new element;
|
---|
[ead4e6] | 168 | tmp->Z = Z;
|
---|
[47d041] | 169 | std::cout << "Mass: ";
|
---|
[ead4e6] | 170 | cin >> tmp->mass;
|
---|
[47d041] | 171 | std::cout << "Name [max 64 chars]: ";
|
---|
[bae8b0] | 172 | cin >> tmp->name;
|
---|
[47d041] | 173 | std::cout << "Short form [max 3 chars]: ";
|
---|
[bae8b0] | 174 | cin >> tmp->symbol;
|
---|
[ead4e6] | 175 | AddElement(tmp);
|
---|
[4eb4fe] | 176 | return tmp;
|
---|
[fb73b8] | 177 | }
|
---|
[ead4e6] | 178 | return res;
|
---|
[fb73b8] | 179 | };
|
---|
| 180 |
|
---|
[ead4e6] | 181 |
|
---|
| 182 | /******************** Access to iterators ****************************/
|
---|
[e5c0a1] | 183 | periodentafel::const_iterator periodentafel::begin() const{
|
---|
[ead4e6] | 184 | return elements.begin();
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[e5c0a1] | 187 | periodentafel::const_iterator periodentafel::end() const{
|
---|
[ead4e6] | 188 | return elements.end();
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[e5c0a1] | 191 | periodentafel::reverse_iterator periodentafel::rbegin() const{
|
---|
[ead4e6] | 192 | return reverse_iterator(elements.end());
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[e5c0a1] | 195 | periodentafel::reverse_iterator periodentafel::rend() const{
|
---|
[ead4e6] | 196 | return reverse_iterator(elements.begin());
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[47ed3d] | 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 |
|
---|
[6ac7ee] | 214 | /** Prints period table to given stream.
|
---|
| 215 | * \param output stream
|
---|
| 216 | */
|
---|
[ead4e6] | 217 | bool periodentafel::Output(ostream * const output) const
|
---|
[6ac7ee] | 218 | {
|
---|
[042f82] | 219 | if (output != NULL) {
|
---|
[47ed3d] | 220 | for(elementSet::const_iterator iter = elements.begin(); iter != elements.end(); ++iter) {
|
---|
| 221 | OutputElement(output, iter->second);
|
---|
[042f82] | 222 | }
|
---|
[47ed3d] | 223 | return true;
|
---|
| 224 | }
|
---|
| 225 | return false;
|
---|
| 226 | }
|
---|
[6ac7ee] | 227 |
|
---|
[4ae823] | 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 |
|
---|
[6ac7ee] | 291 | /** Loads element list from file.
|
---|
| 292 | * \param *path to to standard file names
|
---|
| 293 | */
|
---|
[989bf6] | 294 | bool periodentafel::LoadPeriodentafel(const char *path)
|
---|
[6ac7ee] | 295 | {
|
---|
[4eb4fe] | 296 | ifstream input;
|
---|
[042f82] | 297 | bool status = true;
|
---|
| 298 | bool otherstatus = true;
|
---|
[76096d] | 299 | char filename[MAXSTRINGSIZE];
|
---|
[6ac7ee] | 300 |
|
---|
[042f82] | 301 | // fill elements DB
|
---|
[76096d] | 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);
|
---|
[042f82] | 306 | strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
|
---|
| 307 | strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
|
---|
[4eb4fe] | 308 | input.open(filename);
|
---|
[61745cc] | 309 | if (!input.fail())
|
---|
[47d041] | 310 | LOG(0, "Using " << filename << " as elements database.");
|
---|
[e5c0a1] | 311 | status = status && LoadElementsDatabase(input);
|
---|
[61745cc] | 312 | input.close();
|
---|
| 313 | input.clear();
|
---|
[4eb4fe] | 314 |
|
---|
[67c92b] | 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())
|
---|
[47d041] | 321 | LOG(0, "Using " << filename << " as electronegativity database.");
|
---|
[67c92b] | 322 | otherstatus = otherstatus && LoadElectronegativityDatabase(input);
|
---|
| 323 | input.close();
|
---|
| 324 | input.clear();
|
---|
| 325 |
|
---|
[4eb4fe] | 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);
|
---|
[61745cc] | 331 | if (!input.fail())
|
---|
[47d041] | 332 | LOG(0, "Using " << filename << " as valence database.");
|
---|
[67c92b] | 333 | otherstatus = otherstatus && LoadValenceDatabase(input);
|
---|
[61745cc] | 334 | input.close();
|
---|
| 335 | input.clear();
|
---|
[4eb4fe] | 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);
|
---|
[61745cc] | 342 | if (!input.fail())
|
---|
[47d041] | 343 | LOG(0, "Using " << filename << " as orbitals database.");
|
---|
[67c92b] | 344 | otherstatus = otherstatus && LoadOrbitalsDatabase(input);
|
---|
[61745cc] | 345 | input.close();
|
---|
| 346 | input.clear();
|
---|
[4eb4fe] | 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);
|
---|
[61745cc] | 353 | if (!input.fail())
|
---|
[47d041] | 354 | LOG(0, "Using " << filename << " as H bond angle database.");
|
---|
[67c92b] | 355 | otherstatus = otherstatus && LoadHBondAngleDatabase(input);
|
---|
[61745cc] | 356 | input.close();
|
---|
| 357 | input.clear();
|
---|
[4eb4fe] | 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);
|
---|
[61745cc] | 364 | if (!input.fail())
|
---|
[47d041] | 365 | LOG(0, "Using " << filename << " as H bond length database.");
|
---|
[67c92b] | 366 | otherstatus = otherstatus && LoadHBondLengthsDatabase(input);
|
---|
[61745cc] | 367 | input.close();
|
---|
| 368 | input.clear();
|
---|
[4eb4fe] | 369 |
|
---|
[3613a2] | 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())
|
---|
[47d041] | 376 | LOG(0, "Using " << filename << " as color database.");
|
---|
[3613a2] | 377 | otherstatus = otherstatus && LoadColorDatabase(input);
|
---|
| 378 | input.close();
|
---|
| 379 | input.clear();
|
---|
| 380 |
|
---|
[4eb4fe] | 381 | if (!otherstatus){
|
---|
[47d041] | 382 | ELOG(2, "Something went wrong while parsing the other databases!");
|
---|
[4eb4fe] | 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 | */
|
---|
[e5c0a1] | 392 | bool periodentafel::LoadElementsDatabase(istream &input)
|
---|
[4eb4fe] | 393 | {
|
---|
[ff73a2] | 394 | bool status = true;
|
---|
[e5c0a1] | 395 | string header1tmp,header2tmp;
|
---|
[47d041] | 396 | // std::stringstream parsedelements;
|
---|
[e5c0a1] | 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
|
---|
[4e6d74] | 402 | //cout << "First header: " << header1tmp << endl;
|
---|
| 403 | //cout << "Second header: " << header2tmp << endl;
|
---|
[47d041] | 404 | // parsedelements << "Parsed elements:");
|
---|
[e5c0a1] | 405 | while (!input.eof()) {
|
---|
[042f82] | 406 | element *neues = new element;
|
---|
[83f176] | 407 | input >> neues->name;
|
---|
[67c92b] | 408 | //input >> ws;
|
---|
[83f176] | 409 | input >> neues->symbol;
|
---|
[67c92b] | 410 | //input >> ws;
|
---|
[e5c0a1] | 411 | input >> neues->period;
|
---|
[67c92b] | 412 | //input >> ws;
|
---|
[e5c0a1] | 413 | input >> neues->group;
|
---|
[67c92b] | 414 | //input >> ws;
|
---|
[e5c0a1] | 415 | input >> neues->block;
|
---|
[67c92b] | 416 | //input >> ws;
|
---|
[e5c0a1] | 417 | input >> neues->Z;
|
---|
[67c92b] | 418 | //input >> ws;
|
---|
[e5c0a1] | 419 | input >> neues->mass;
|
---|
[67c92b] | 420 | //input >> ws;
|
---|
[e5c0a1] | 421 | input >> neues->CovalentRadius;
|
---|
[67c92b] | 422 | //input >> ws;
|
---|
[e5c0a1] | 423 | input >> neues->VanDerWaalsRadius;
|
---|
[67c92b] | 424 | //input >> ws;
|
---|
[e5c0a1] | 425 | input >> ws;
|
---|
[042f82] | 426 | //neues->Output((ofstream *)&cout);
|
---|
[ed26ae] | 427 | if ((neues->getAtomicNumber() > 0) && (neues->getAtomicNumber() < MAX_ELEMENTS)) {
|
---|
[e5c0a1] | 428 | parsedElements[neues->Z] = neues;
|
---|
[47d041] | 429 | // parsedelements << " " << *neues);
|
---|
[ff73a2] | 430 | } else {
|
---|
[47d041] | 431 | ELOG(2, "Detected empty line or invalid element in elements db, discarding.");
|
---|
| 432 | // parsedelements << " <?>");
|
---|
[db6bf74] | 433 | delete(neues);
|
---|
[042f82] | 434 | }
|
---|
[e5c0a1] | 435 | // when the input is in failed state, we most likely just read garbage
|
---|
| 436 | if(input.fail()) {
|
---|
[47d041] | 437 | ELOG(2, "Error parsing elements db.");
|
---|
[e5c0a1] | 438 | status = false;
|
---|
| 439 | break;
|
---|
| 440 | }
|
---|
[042f82] | 441 | }
|
---|
[61745cc] | 442 | } else {
|
---|
[47d041] | 443 | ELOG(1, "Could not open the database.");
|
---|
[ff73a2] | 444 | status = false;
|
---|
[61745cc] | 445 | }
|
---|
[47d041] | 446 | //LOG(0, parsedElements.str());
|
---|
[ff73a2] | 447 |
|
---|
[e5c0a1] | 448 | if (!parsedElements.size())
|
---|
[ff73a2] | 449 | status = false;
|
---|
| 450 |
|
---|
[e5c0a1] | 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;
|
---|
[9f99b3] | 459 | delete(iter->second);
|
---|
[e5c0a1] | 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 |
|
---|
[ff73a2] | 473 | return status;
|
---|
[4eb4fe] | 474 | }
|
---|
[6ac7ee] | 475 |
|
---|
[67c92b] | 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;
|
---|
[47d041] | 492 | //LOG(1, "INFO: Element " << Z << " has " << FindElement(Z)->Electronegativity << " valence electrons.");
|
---|
[67c92b] | 493 | }
|
---|
| 494 | return true;
|
---|
| 495 | } else
|
---|
| 496 | return false;
|
---|
| 497 | }
|
---|
| 498 |
|
---|
[4eb4fe] | 499 | /** load the valence info.
|
---|
| 500 | * \param *input stream to parse from
|
---|
| 501 | * \return true - parsing successful, false - something went wrong
|
---|
| 502 | */
|
---|
[67c92b] | 503 | bool periodentafel::LoadValenceDatabase(istream &input)
|
---|
[4eb4fe] | 504 | {
|
---|
| 505 | char dummy[MAXSTRINGSIZE];
|
---|
[67c92b] | 506 | if (!input.fail()) {
|
---|
| 507 | input.getline(dummy, MAXSTRINGSIZE);
|
---|
| 508 | while (!input.eof()) {
|
---|
[ead4e6] | 509 | atomicNumber_t Z;
|
---|
[67c92b] | 510 | input >> Z;
|
---|
[4eb4fe] | 511 | ASSERT(elements.count(Z), "Element not present");
|
---|
[67c92b] | 512 | input >> ws;
|
---|
| 513 | input >> elements[Z]->Valence;
|
---|
| 514 | input >> ws;
|
---|
[47d041] | 515 | //LOG(3, "INFO: Element " << Z << " has " << FindElement(Z)->Valence << " valence electrons.");
|
---|
[042f82] | 516 | }
|
---|
[4eb4fe] | 517 | return true;
|
---|
[042f82] | 518 | } else
|
---|
[4eb4fe] | 519 | return false;
|
---|
| 520 | }
|
---|
[6ac7ee] | 521 |
|
---|
[4eb4fe] | 522 | /** load the orbitals info.
|
---|
| 523 | * \param *input stream to parse from
|
---|
| 524 | * \return true - parsing successful, false - something went wrong
|
---|
| 525 | */
|
---|
[67c92b] | 526 | bool periodentafel::LoadOrbitalsDatabase(istream &input)
|
---|
[4eb4fe] | 527 | {
|
---|
| 528 | char dummy[MAXSTRINGSIZE];
|
---|
[67c92b] | 529 | if (!input.fail()) {
|
---|
| 530 | input.getline(dummy, MAXSTRINGSIZE);
|
---|
| 531 | while (!input.eof()) {
|
---|
[ead4e6] | 532 | atomicNumber_t Z;
|
---|
[67c92b] | 533 | input >> Z;
|
---|
[4eb4fe] | 534 | ASSERT(elements.count(Z), "Element not present");
|
---|
[67c92b] | 535 | input >> ws;
|
---|
| 536 | input >> elements[Z]->NoValenceOrbitals;
|
---|
| 537 | input >> ws;
|
---|
[47d041] | 538 | //LOG(3, "Element " << Z << " has " << FindElement(Z)->NoValenceOrbitals << " number of singly occupied valence orbitals.");
|
---|
[042f82] | 539 | }
|
---|
[4eb4fe] | 540 | return true;
|
---|
[042f82] | 541 | } else
|
---|
[4eb4fe] | 542 | return false;
|
---|
| 543 | }
|
---|
[6ac7ee] | 544 |
|
---|
[4eb4fe] | 545 | /** load the hbond angles info.
|
---|
| 546 | * \param *input stream to parse from
|
---|
| 547 | * \return true - parsing successful, false - something went wrong
|
---|
| 548 | */
|
---|
[67c92b] | 549 | bool periodentafel::LoadHBondAngleDatabase(istream &input)
|
---|
[4eb4fe] | 550 | {
|
---|
| 551 | char dummy[MAXSTRINGSIZE];
|
---|
[67c92b] | 552 | if (!input.fail()) {
|
---|
| 553 | input.getline(dummy, MAXSTRINGSIZE);
|
---|
| 554 | while (!input.eof()) {
|
---|
[ead4e6] | 555 | atomicNumber_t Z;
|
---|
[67c92b] | 556 | input >> Z;
|
---|
[4eb4fe] | 557 | ASSERT(elements.count(Z), "Element not present");
|
---|
[67c92b] | 558 | input >> ws;
|
---|
| 559 | input >> elements[Z]->HBondAngle[0];
|
---|
| 560 | input >> elements[Z]->HBondAngle[1];
|
---|
| 561 | input >> elements[Z]->HBondAngle[2];
|
---|
| 562 | input >> ws;
|
---|
[47d041] | 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.");
|
---|
[042f82] | 564 | }
|
---|
[4eb4fe] | 565 | return true;
|
---|
[042f82] | 566 | } else
|
---|
[4eb4fe] | 567 | return false;
|
---|
| 568 | }
|
---|
[6ac7ee] | 569 |
|
---|
[4eb4fe] | 570 | /** load the hbond lengths info.
|
---|
| 571 | * \param *input stream to parse from
|
---|
| 572 | * \return true - parsing successful, false - something went wrong
|
---|
| 573 | */
|
---|
[67c92b] | 574 | bool periodentafel::LoadHBondLengthsDatabase(istream &input)
|
---|
[4eb4fe] | 575 | {
|
---|
| 576 | char dummy[MAXSTRINGSIZE];
|
---|
[67c92b] | 577 | if (!input.fail()) {
|
---|
| 578 | input.getline(dummy, MAXSTRINGSIZE);
|
---|
| 579 | while (!input.eof()) {
|
---|
[ead4e6] | 580 | atomicNumber_t Z;
|
---|
[67c92b] | 581 | input >> Z;
|
---|
[4eb4fe] | 582 | ASSERT(elements.count(Z), "Element not present");
|
---|
[67c92b] | 583 | input >> ws;
|
---|
| 584 | input >> elements[Z]->HBondDistance[0];
|
---|
| 585 | input >> elements[Z]->HBondDistance[1];
|
---|
| 586 | input >> elements[Z]->HBondDistance[2];
|
---|
| 587 | input >> ws;
|
---|
[47d041] | 588 | //LOG(3, "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen.");
|
---|
[042f82] | 589 | }
|
---|
[4eb4fe] | 590 | return true;
|
---|
[042f82] | 591 | } else
|
---|
[4eb4fe] | 592 | return false;
|
---|
| 593 | }
|
---|
[6ac7ee] | 594 |
|
---|
[064178] | 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;
|
---|
[907636] | 619 | // {
|
---|
| 620 | // const element * tmp = FindElement(Z);
|
---|
| 621 | // LOG(0, "Element " << tmp->getName() << " has ("
|
---|
[064178] | 622 | // << (int)tmp->color[0] << "," << (int)tmp->color[1] << "," << (int)tmp->color[2]
|
---|
[907636] | 623 | // << ") colors.");
|
---|
| 624 | // }
|
---|
[064178] | 625 | }
|
---|
| 626 | return true;
|
---|
| 627 | } else
|
---|
| 628 | return false;
|
---|
| 629 | }
|
---|
| 630 |
|
---|
[6ac7ee] | 631 | /** Stores element list to file.
|
---|
| 632 | */
|
---|
[989bf6] | 633 | bool periodentafel::StorePeriodentafel(const char *path) const
|
---|
[6ac7ee] | 634 | {
|
---|
[042f82] | 635 | bool result = true;
|
---|
| 636 | ofstream f;
|
---|
| 637 | char filename[MAXSTRINGSIZE];
|
---|
[6ac7ee] | 638 |
|
---|
[76096d] | 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);
|
---|
[042f82] | 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;
|
---|
[ead4e6] | 649 | for(const_iterator iter=elements.begin();iter!=elements.end();++iter){
|
---|
[47ed3d] | 650 | OutputElement(&f, iter->second);
|
---|
[042f82] | 651 | }
|
---|
| 652 | f.close();
|
---|
[4eb4fe] | 653 | return true;
|
---|
[042f82] | 654 | } else
|
---|
[4eb4fe] | 655 | return result;
|
---|
[6ac7ee] | 656 | };
|
---|
[b60804] | 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 | }
|
---|