| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 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 <cstring> | 
|---|
| 22 | #include <fstream> | 
|---|
| 23 | #include <iomanip> | 
|---|
| 24 | #include <iostream> | 
|---|
| 25 | #include <sstream> | 
|---|
| 26 |  | 
|---|
| 27 | #include "CodePatterns/Assert.hpp" | 
|---|
| 28 | #include "CodePatterns/Log.hpp" | 
|---|
| 29 | #include "element.hpp" | 
|---|
| 30 | #include "elements_db.hpp" | 
|---|
| 31 | #include "periodentafel.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->getAtomicNumber(); | 
|---|
| 63 | ASSERT(!elements.count(Z), "Element is already present."); | 
|---|
| 64 | if (pointer->getAtomicNumber() < 1 && pointer->getAtomicNumber() >= MAX_ELEMENTS) | 
|---|
| 65 | ELOG(0, "Invalid Z number!"); | 
|---|
| 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->getAtomicNumber()); | 
|---|
| 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 | std::cout << "Atomic number Z: "; | 
|---|
| 133 | std::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 | std::cout << "Atomic number: " << Z; | 
|---|
| 146 | cin >> Z; | 
|---|
| 147 | const element *res = FindElement(Z); | 
|---|
| 148 | if (!res) { | 
|---|
| 149 | // TODO: make this using the constructor | 
|---|
| 150 | std::cout << "Element not found in database, please enter." << std::endl; | 
|---|
| 151 | element *tmp = new element; | 
|---|
| 152 | tmp->Z = Z; | 
|---|
| 153 | std::cout << "Mass: "; | 
|---|
| 154 | cin >> tmp->mass; | 
|---|
| 155 | std::cout << "Name [max 64 chars]: "; | 
|---|
| 156 | cin >> tmp->name; | 
|---|
| 157 | std::cout << "Short form [max 3 chars]: "; | 
|---|
| 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 element data to \a *out. | 
|---|
| 184 | * \param *out outstream | 
|---|
| 185 | */ | 
|---|
| 186 | void periodentafel::OutputElement(ostream * const out, const element *elem) const | 
|---|
| 187 | { | 
|---|
| 188 | *out << elem->getName() << "\t"; | 
|---|
| 189 | *out << elem->getSymbol() << "\t"; | 
|---|
| 190 | *out << elem->getAtomicNumber() << "\t"; | 
|---|
| 191 | *out << elem->getMass() << "\t"; | 
|---|
| 192 | *out << elem->getCovalentRadius() << "\t"; | 
|---|
| 193 | *out << elem->getVanDerWaalsRadius() << std::endl; | 
|---|
| 194 | //*out << elem->getSymbol() << "\t"  << fixed << setprecision(11) << showpoint << elem->getMass() << "g/mol\t" << elem->getName() << "\t" << elem->getSymbol() << "\t" << endl; | 
|---|
| 195 | }; | 
|---|
| 196 |  | 
|---|
| 197 |  | 
|---|
| 198 | /** Prints period table to given stream. | 
|---|
| 199 | * \param output stream | 
|---|
| 200 | */ | 
|---|
| 201 | bool periodentafel::Output(ostream * const output) const | 
|---|
| 202 | { | 
|---|
| 203 | if (output != NULL) { | 
|---|
| 204 | for(elementSet::const_iterator iter = elements.begin(); iter != elements.end(); ++iter) { | 
|---|
| 205 | OutputElement(output, iter->second); | 
|---|
| 206 | } | 
|---|
| 207 | return true; | 
|---|
| 208 | } | 
|---|
| 209 | return false; | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | /** Scan periodentafel contents from internal databases. | 
|---|
| 213 | * | 
|---|
| 214 | */ | 
|---|
| 215 | void periodentafel::ScanPeriodentafel() | 
|---|
| 216 | { | 
|---|
| 217 | { | 
|---|
| 218 | stringstream input(elementsDB,ios_base::in); | 
|---|
| 219 | #ifndef NDEBUG | 
|---|
| 220 | bool status = | 
|---|
| 221 | #endif | 
|---|
| 222 | LoadElementsDatabase(input); | 
|---|
| 223 | ASSERT(status,  "General element initialization failed"); | 
|---|
| 224 | } | 
|---|
| 225 | { | 
|---|
| 226 | stringstream input(ElectronegativitiesDB,ios_base::in); | 
|---|
| 227 | #ifndef NDEBUG | 
|---|
| 228 | bool status = | 
|---|
| 229 | #endif | 
|---|
| 230 | LoadElectronegativityDatabase(input); | 
|---|
| 231 | ASSERT(status, "Electronegativities entry of element initialization failed"); | 
|---|
| 232 | } | 
|---|
| 233 | { | 
|---|
| 234 | stringstream input(valenceDB,ios_base::in); | 
|---|
| 235 | #ifndef NDEBUG | 
|---|
| 236 | bool status = | 
|---|
| 237 | #endif | 
|---|
| 238 | LoadValenceDatabase(input); | 
|---|
| 239 | ASSERT(status, "Valence entry of element initialization failed"); | 
|---|
| 240 | } | 
|---|
| 241 | { | 
|---|
| 242 | stringstream input(orbitalsDB,ios_base::in); | 
|---|
| 243 | #ifndef NDEBUG | 
|---|
| 244 | bool status = | 
|---|
| 245 | #endif | 
|---|
| 246 | LoadOrbitalsDatabase(input); | 
|---|
| 247 | ASSERT(status, "Orbitals entry of element initialization failed"); | 
|---|
| 248 | } | 
|---|
| 249 | { | 
|---|
| 250 | stringstream input(HbondangleDB,ios_base::in); | 
|---|
| 251 | #ifndef NDEBUG | 
|---|
| 252 | bool status = | 
|---|
| 253 | #endif | 
|---|
| 254 | LoadHBondAngleDatabase(input); | 
|---|
| 255 | ASSERT(status, "HBond angle entry of element initialization failed"); | 
|---|
| 256 | } | 
|---|
| 257 | { | 
|---|
| 258 | stringstream input(HbonddistanceDB,ios_base::in); | 
|---|
| 259 | #ifndef NDEBUG | 
|---|
| 260 | bool status = | 
|---|
| 261 | #endif | 
|---|
| 262 | LoadHBondLengthsDatabase(input); | 
|---|
| 263 | ASSERT(status, "HBond distance entry of element initialization failed"); | 
|---|
| 264 | } | 
|---|
| 265 | { | 
|---|
| 266 | stringstream input(ColorDB,ios_base::in); | 
|---|
| 267 | #ifndef NDEBUG | 
|---|
| 268 | bool status = | 
|---|
| 269 | #endif | 
|---|
| 270 | LoadColorDatabase(input); | 
|---|
| 271 | ASSERT(status, "color entry of element initialization failed"); | 
|---|
| 272 | } | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | /** Loads element list from file. | 
|---|
| 276 | * \param *path to to standard file names | 
|---|
| 277 | */ | 
|---|
| 278 | bool periodentafel::LoadPeriodentafel(const char *path) | 
|---|
| 279 | { | 
|---|
| 280 | ifstream input; | 
|---|
| 281 | bool status = true; | 
|---|
| 282 | bool otherstatus = true; | 
|---|
| 283 | char filename[255]; | 
|---|
| 284 |  | 
|---|
| 285 | // fill elements DB | 
|---|
| 286 | strncpy(filename, path, MAXSTRINGSIZE); | 
|---|
| 287 | strncat(filename, "/", MAXSTRINGSIZE-strlen(filename)); | 
|---|
| 288 | strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename)); | 
|---|
| 289 | input.open(filename); | 
|---|
| 290 | if (!input.fail()) | 
|---|
| 291 | LOG(0, "Using " << filename << " as elements database."); | 
|---|
| 292 | status = status && LoadElementsDatabase(input); | 
|---|
| 293 | input.close(); | 
|---|
| 294 | input.clear(); | 
|---|
| 295 |  | 
|---|
| 296 | // fill valence DB per element | 
|---|
| 297 | strncpy(filename, path, MAXSTRINGSIZE); | 
|---|
| 298 | strncat(filename, "/", MAXSTRINGSIZE-strlen(filename)); | 
|---|
| 299 | strncat(filename, STANDARDELECTRONEGATIVITYDB, MAXSTRINGSIZE-strlen(filename)); | 
|---|
| 300 | input.open(filename); | 
|---|
| 301 | if (!input.fail()) | 
|---|
| 302 | LOG(0, "Using " << filename << " as electronegativity database."); | 
|---|
| 303 | otherstatus = otherstatus && LoadElectronegativityDatabase(input); | 
|---|
| 304 | input.close(); | 
|---|
| 305 | input.clear(); | 
|---|
| 306 |  | 
|---|
| 307 | // fill valence DB per element | 
|---|
| 308 | strncpy(filename, path, MAXSTRINGSIZE); | 
|---|
| 309 | strncat(filename, "/", MAXSTRINGSIZE-strlen(filename)); | 
|---|
| 310 | strncat(filename, STANDARDVALENCEDB, MAXSTRINGSIZE-strlen(filename)); | 
|---|
| 311 | input.open(filename); | 
|---|
| 312 | if (!input.fail()) | 
|---|
| 313 | LOG(0, "Using " << filename << " as valence database."); | 
|---|
| 314 | otherstatus = otherstatus && LoadValenceDatabase(input); | 
|---|
| 315 | input.close(); | 
|---|
| 316 | input.clear(); | 
|---|
| 317 |  | 
|---|
| 318 | // fill orbitals DB per element | 
|---|
| 319 | strncpy(filename, path, MAXSTRINGSIZE); | 
|---|
| 320 | strncat(filename, "/", MAXSTRINGSIZE-strlen(filename)); | 
|---|
| 321 | strncat(filename, STANDARDORBITALDB, MAXSTRINGSIZE-strlen(filename)); | 
|---|
| 322 | input.open(filename); | 
|---|
| 323 | if (!input.fail()) | 
|---|
| 324 | LOG(0, "Using " << filename << " as orbitals database."); | 
|---|
| 325 | otherstatus = otherstatus && LoadOrbitalsDatabase(input); | 
|---|
| 326 | input.close(); | 
|---|
| 327 | input.clear(); | 
|---|
| 328 |  | 
|---|
| 329 | // fill H-BondAngle DB per element | 
|---|
| 330 | strncpy(filename, path, MAXSTRINGSIZE); | 
|---|
| 331 | strncat(filename, "/", MAXSTRINGSIZE-strlen(filename)); | 
|---|
| 332 | strncat(filename, STANDARDHBONDANGLEDB, MAXSTRINGSIZE-strlen(filename)); | 
|---|
| 333 | input.open(filename); | 
|---|
| 334 | if (!input.fail()) | 
|---|
| 335 | LOG(0, "Using " << filename << " as H bond angle database."); | 
|---|
| 336 | otherstatus = otherstatus && LoadHBondAngleDatabase(input); | 
|---|
| 337 | input.close(); | 
|---|
| 338 | input.clear(); | 
|---|
| 339 |  | 
|---|
| 340 | // fill H-BondDistance DB per element | 
|---|
| 341 | strncpy(filename, path, MAXSTRINGSIZE); | 
|---|
| 342 | strncat(filename, "/", MAXSTRINGSIZE-strlen(filename)); | 
|---|
| 343 | strncat(filename, STANDARDHBONDDISTANCEDB, MAXSTRINGSIZE-strlen(filename)); | 
|---|
| 344 | input.open(filename); | 
|---|
| 345 | if (!input.fail()) | 
|---|
| 346 | LOG(0, "Using " << filename << " as H bond length database."); | 
|---|
| 347 | otherstatus = otherstatus && LoadHBondLengthsDatabase(input); | 
|---|
| 348 | input.close(); | 
|---|
| 349 | input.clear(); | 
|---|
| 350 |  | 
|---|
| 351 | // fill color DB per element | 
|---|
| 352 | strncpy(filename, path, MAXSTRINGSIZE); | 
|---|
| 353 | strncat(filename, "/", MAXSTRINGSIZE-strlen(filename)); | 
|---|
| 354 | strncat(filename, STANDARDCOLORDB, MAXSTRINGSIZE-strlen(filename)); | 
|---|
| 355 | input.open(filename); | 
|---|
| 356 | if (!input.fail()) | 
|---|
| 357 | LOG(0, "Using " << filename << " as color database."); | 
|---|
| 358 | otherstatus = otherstatus && LoadColorDatabase(input); | 
|---|
| 359 | input.close(); | 
|---|
| 360 | input.clear(); | 
|---|
| 361 |  | 
|---|
| 362 | if (!otherstatus){ | 
|---|
| 363 | ELOG(2, "Something went wrong while parsing the other databases!"); | 
|---|
| 364 | } | 
|---|
| 365 |  | 
|---|
| 366 | return status; | 
|---|
| 367 | }; | 
|---|
| 368 |  | 
|---|
| 369 | /** load the element info. | 
|---|
| 370 | * \param *input stream to parse from | 
|---|
| 371 | * \return true - parsing successful, false - something went wrong | 
|---|
| 372 | */ | 
|---|
| 373 | bool periodentafel::LoadElementsDatabase(istream &input) | 
|---|
| 374 | { | 
|---|
| 375 | bool status = true; | 
|---|
| 376 | string header1tmp,header2tmp; | 
|---|
| 377 | //  std::stringstream parsedelements; | 
|---|
| 378 | // first parse into a map, so we can revert to old status in case something goes wront | 
|---|
| 379 | map<atomicNumber_t,element*> parsedElements; | 
|---|
| 380 | if (!input.fail()) { | 
|---|
| 381 | getline(input,header1tmp); | 
|---|
| 382 | getline(input,header2tmp); // skip first two header lines | 
|---|
| 383 | //cout << "First header: " << header1tmp << endl; | 
|---|
| 384 | //cout << "Second header: " << header2tmp << endl; | 
|---|
| 385 | //    parsedelements <<  "Parsed elements:"); | 
|---|
| 386 | while (!input.eof()) { | 
|---|
| 387 | element *neues = new element; | 
|---|
| 388 | input >> neues->name; | 
|---|
| 389 | //input >> ws; | 
|---|
| 390 | input >> neues->symbol; | 
|---|
| 391 | //input >> ws; | 
|---|
| 392 | input >> neues->period; | 
|---|
| 393 | //input >> ws; | 
|---|
| 394 | input >> neues->group; | 
|---|
| 395 | //input >> ws; | 
|---|
| 396 | input >> neues->block; | 
|---|
| 397 | //input >> ws; | 
|---|
| 398 | input >> neues->Z; | 
|---|
| 399 | //input >> ws; | 
|---|
| 400 | input >> neues->mass; | 
|---|
| 401 | //input >> ws; | 
|---|
| 402 | input >> neues->CovalentRadius; | 
|---|
| 403 | //input >> ws; | 
|---|
| 404 | input >> neues->VanDerWaalsRadius; | 
|---|
| 405 | //input >> ws; | 
|---|
| 406 | input >> ws; | 
|---|
| 407 | //neues->Output((ofstream *)&cout); | 
|---|
| 408 | if ((neues->getAtomicNumber() > 0) && (neues->getAtomicNumber() < MAX_ELEMENTS)) { | 
|---|
| 409 | parsedElements[neues->Z] = neues; | 
|---|
| 410 | //        parsedelements << " " << *neues); | 
|---|
| 411 | } else { | 
|---|
| 412 | ELOG(2, "Detected empty line or invalid element in elements db, discarding."); | 
|---|
| 413 | //        parsedelements << " <?>"); | 
|---|
| 414 | delete(neues); | 
|---|
| 415 | } | 
|---|
| 416 | // when the input is in failed state, we most likely just read garbage | 
|---|
| 417 | if(input.fail()) { | 
|---|
| 418 | ELOG(2, "Error parsing elements db."); | 
|---|
| 419 | status = false; | 
|---|
| 420 | break; | 
|---|
| 421 | } | 
|---|
| 422 | } | 
|---|
| 423 | } else { | 
|---|
| 424 | ELOG(1, "Could not open the database."); | 
|---|
| 425 | status = false; | 
|---|
| 426 | } | 
|---|
| 427 | //LOG(0, parsedElements.str()); | 
|---|
| 428 |  | 
|---|
| 429 | if (!parsedElements.size()) | 
|---|
| 430 | status = false; | 
|---|
| 431 |  | 
|---|
| 432 | if(status){ | 
|---|
| 433 | for(map<atomicNumber_t,element*>::iterator iter=parsedElements.begin(); | 
|---|
| 434 | iter!=parsedElements.end(); | 
|---|
| 435 | ++iter){ | 
|---|
| 436 | if (elements.count(iter->first)) { | 
|---|
| 437 | // if element already present, replace the old one | 
|---|
| 438 | // pointer to old element might still be in use, so we have to replace into the old element | 
|---|
| 439 | *(elements[iter->first])=*iter->second; | 
|---|
| 440 | delete(iter->second); | 
|---|
| 441 | } | 
|---|
| 442 | else { | 
|---|
| 443 | // no such element in periodentafel... we can just insert | 
|---|
| 444 | elements[iter->first] = iter->second; | 
|---|
| 445 | } | 
|---|
| 446 | } | 
|---|
| 447 | // all went well.. we now copy the header | 
|---|
| 448 | strncpy(header1,header1tmp.c_str(),MAXSTRINGSIZE); | 
|---|
| 449 | header1[MAXSTRINGSIZE-1]=0; | 
|---|
| 450 | strncpy(header2,header2tmp.c_str(),MAXSTRINGSIZE); | 
|---|
| 451 | header2[MAXSTRINGSIZE-1]=0; | 
|---|
| 452 | } | 
|---|
| 453 |  | 
|---|
| 454 | return status; | 
|---|
| 455 | } | 
|---|
| 456 |  | 
|---|
| 457 | /** load the electronegativity info. | 
|---|
| 458 | * \param *input stream to parse from | 
|---|
| 459 | * \return true - parsing successful, false - something went wrong | 
|---|
| 460 | */ | 
|---|
| 461 | bool periodentafel::LoadElectronegativityDatabase(std::istream &input) | 
|---|
| 462 | { | 
|---|
| 463 | char dummy[MAXSTRINGSIZE]; | 
|---|
| 464 | if (!input.fail()) { | 
|---|
| 465 | input.getline(dummy, MAXSTRINGSIZE); | 
|---|
| 466 | while (!input.eof()) { | 
|---|
| 467 | atomicNumber_t Z; | 
|---|
| 468 | input >> Z; | 
|---|
| 469 | ASSERT(elements.count(Z), "Element not present"); | 
|---|
| 470 | input >> ws; | 
|---|
| 471 | input >> elements[Z]->Electronegativity; | 
|---|
| 472 | input >> ws; | 
|---|
| 473 | //LOG(1, "INFO: Element " << Z << " has " << FindElement(Z)->Electronegativity << " valence electrons."); | 
|---|
| 474 | } | 
|---|
| 475 | return true; | 
|---|
| 476 | } else | 
|---|
| 477 | return false; | 
|---|
| 478 | } | 
|---|
| 479 |  | 
|---|
| 480 | /** load the valence info. | 
|---|
| 481 | * \param *input stream to parse from | 
|---|
| 482 | * \return true - parsing successful, false - something went wrong | 
|---|
| 483 | */ | 
|---|
| 484 | bool periodentafel::LoadValenceDatabase(istream &input) | 
|---|
| 485 | { | 
|---|
| 486 | char dummy[MAXSTRINGSIZE]; | 
|---|
| 487 | if (!input.fail()) { | 
|---|
| 488 | input.getline(dummy, MAXSTRINGSIZE); | 
|---|
| 489 | while (!input.eof()) { | 
|---|
| 490 | atomicNumber_t Z; | 
|---|
| 491 | input >> Z; | 
|---|
| 492 | ASSERT(elements.count(Z), "Element not present"); | 
|---|
| 493 | input >> ws; | 
|---|
| 494 | input >> elements[Z]->Valence; | 
|---|
| 495 | input >> ws; | 
|---|
| 496 | //LOG(3, "INFO: Element " << Z << " has " << FindElement(Z)->Valence << " valence electrons."); | 
|---|
| 497 | } | 
|---|
| 498 | return true; | 
|---|
| 499 | } else | 
|---|
| 500 | return false; | 
|---|
| 501 | } | 
|---|
| 502 |  | 
|---|
| 503 | /** load the orbitals info. | 
|---|
| 504 | * \param *input stream to parse from | 
|---|
| 505 | * \return true - parsing successful, false - something went wrong | 
|---|
| 506 | */ | 
|---|
| 507 | bool periodentafel::LoadOrbitalsDatabase(istream &input) | 
|---|
| 508 | { | 
|---|
| 509 | char dummy[MAXSTRINGSIZE]; | 
|---|
| 510 | if (!input.fail()) { | 
|---|
| 511 | input.getline(dummy, MAXSTRINGSIZE); | 
|---|
| 512 | while (!input.eof()) { | 
|---|
| 513 | atomicNumber_t Z; | 
|---|
| 514 | input >> Z; | 
|---|
| 515 | ASSERT(elements.count(Z), "Element not present"); | 
|---|
| 516 | input >> ws; | 
|---|
| 517 | input >> elements[Z]->NoValenceOrbitals; | 
|---|
| 518 | input >> ws; | 
|---|
| 519 | //LOG(3, "Element " << Z << " has " << FindElement(Z)->NoValenceOrbitals << " number of singly occupied valence orbitals."); | 
|---|
| 520 | } | 
|---|
| 521 | return true; | 
|---|
| 522 | } else | 
|---|
| 523 | return false; | 
|---|
| 524 | } | 
|---|
| 525 |  | 
|---|
| 526 | /** load the hbond angles info. | 
|---|
| 527 | * \param *input stream to parse from | 
|---|
| 528 | * \return true - parsing successful, false - something went wrong | 
|---|
| 529 | */ | 
|---|
| 530 | bool periodentafel::LoadHBondAngleDatabase(istream &input) | 
|---|
| 531 | { | 
|---|
| 532 | char dummy[MAXSTRINGSIZE]; | 
|---|
| 533 | if (!input.fail()) { | 
|---|
| 534 | input.getline(dummy, MAXSTRINGSIZE); | 
|---|
| 535 | while (!input.eof()) { | 
|---|
| 536 | atomicNumber_t Z; | 
|---|
| 537 | input >> Z; | 
|---|
| 538 | ASSERT(elements.count(Z), "Element not present"); | 
|---|
| 539 | input >> ws; | 
|---|
| 540 | input >> elements[Z]->HBondAngle[0]; | 
|---|
| 541 | input >> elements[Z]->HBondAngle[1]; | 
|---|
| 542 | input >> elements[Z]->HBondAngle[2]; | 
|---|
| 543 | input >> ws; | 
|---|
| 544 | //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."); | 
|---|
| 545 | } | 
|---|
| 546 | return true; | 
|---|
| 547 | } else | 
|---|
| 548 | return false; | 
|---|
| 549 | } | 
|---|
| 550 |  | 
|---|
| 551 | /** load the hbond lengths info. | 
|---|
| 552 | * \param *input stream to parse from | 
|---|
| 553 | * \return true - parsing successful, false - something went wrong | 
|---|
| 554 | */ | 
|---|
| 555 | bool periodentafel::LoadHBondLengthsDatabase(istream &input) | 
|---|
| 556 | { | 
|---|
| 557 | char dummy[MAXSTRINGSIZE]; | 
|---|
| 558 | if (!input.fail()) { | 
|---|
| 559 | input.getline(dummy, MAXSTRINGSIZE); | 
|---|
| 560 | while (!input.eof()) { | 
|---|
| 561 | atomicNumber_t Z; | 
|---|
| 562 | input >> Z; | 
|---|
| 563 | ASSERT(elements.count(Z), "Element not present"); | 
|---|
| 564 | input >> ws; | 
|---|
| 565 | input >> elements[Z]->HBondDistance[0]; | 
|---|
| 566 | input >> elements[Z]->HBondDistance[1]; | 
|---|
| 567 | input >> elements[Z]->HBondDistance[2]; | 
|---|
| 568 | input >> ws; | 
|---|
| 569 | //LOG(3, "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen."); | 
|---|
| 570 | } | 
|---|
| 571 | return true; | 
|---|
| 572 | } else | 
|---|
| 573 | return false; | 
|---|
| 574 | } | 
|---|
| 575 |  | 
|---|
| 576 | /** load the color info. | 
|---|
| 577 | * \param *input stream to parse from | 
|---|
| 578 | * \return true - parsing successful, false - something went wrong | 
|---|
| 579 | */ | 
|---|
| 580 | bool periodentafel::LoadColorDatabase(istream &input) | 
|---|
| 581 | { | 
|---|
| 582 | char dummy[MAXSTRINGSIZE]; | 
|---|
| 583 | if (!input.fail()) { | 
|---|
| 584 | input.getline(dummy, MAXSTRINGSIZE); | 
|---|
| 585 | while (!input.eof()) { | 
|---|
| 586 | atomicNumber_t Z; | 
|---|
| 587 | input >> Z; | 
|---|
| 588 | ASSERT(elements.count(Z), "Element not present"); | 
|---|
| 589 | input >> ws; | 
|---|
| 590 | input >> dummy; | 
|---|
| 591 | { | 
|---|
| 592 | int tmpcolor;   // char here will only parse a single char (i.e. only "2" out of "255") | 
|---|
| 593 | for (int i=0;i<3;++i) { | 
|---|
| 594 | input >> ws; | 
|---|
| 595 | input >> tmpcolor; | 
|---|
| 596 | elements[Z]->color[i] = (unsigned char)tmpcolor; | 
|---|
| 597 | } | 
|---|
| 598 | } | 
|---|
| 599 | input >> ws; | 
|---|
| 600 | //      { | 
|---|
| 601 | //        const element * tmp = FindElement(Z); | 
|---|
| 602 | //        LOG(0, "Element " << tmp->getName() << " has (" | 
|---|
| 603 | //            << (int)tmp->color[0] << "," << (int)tmp->color[1] << "," << (int)tmp->color[2] | 
|---|
| 604 | //            << ") colors."); | 
|---|
| 605 | //      } | 
|---|
| 606 | } | 
|---|
| 607 | return true; | 
|---|
| 608 | } else | 
|---|
| 609 | return false; | 
|---|
| 610 | } | 
|---|
| 611 |  | 
|---|
| 612 | /** Stores element list to file. | 
|---|
| 613 | */ | 
|---|
| 614 | bool periodentafel::StorePeriodentafel(const char *path) const | 
|---|
| 615 | { | 
|---|
| 616 | bool result = true; | 
|---|
| 617 | ofstream f; | 
|---|
| 618 | char filename[MAXSTRINGSIZE]; | 
|---|
| 619 |  | 
|---|
| 620 | strncpy(filename, path, MAXSTRINGSIZE); | 
|---|
| 621 | strncat(filename, "/", MAXSTRINGSIZE-strlen(filename)); | 
|---|
| 622 | strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename)); | 
|---|
| 623 | f.open(filename); | 
|---|
| 624 | if (f != NULL) { | 
|---|
| 625 | f << header1 << endl; | 
|---|
| 626 | f << header2 << endl; | 
|---|
| 627 | for(const_iterator iter=elements.begin();iter!=elements.end();++iter){ | 
|---|
| 628 | OutputElement(&f, iter->second); | 
|---|
| 629 | } | 
|---|
| 630 | f.close(); | 
|---|
| 631 | return true; | 
|---|
| 632 | } else | 
|---|
| 633 | return result; | 
|---|
| 634 | }; | 
|---|
| 635 |  | 
|---|
| 636 | /** Comparison operator for periodentafel. | 
|---|
| 637 | * | 
|---|
| 638 | * @param other other instance to compare to | 
|---|
| 639 | * @return true when both contain same elements | 
|---|
| 640 | */ | 
|---|
| 641 | bool periodentafel::operator==(const periodentafel &other) const | 
|---|
| 642 | { | 
|---|
| 643 | // there are only pointers in the elementSet, hence we have to compare ourselves | 
|---|
| 644 | if (elements.size() != other.elements.size()) return false; | 
|---|
| 645 | const_iterator iter = elements.begin(); | 
|---|
| 646 | const_iterator otheriter = other.elements.begin(); | 
|---|
| 647 | for (;(iter != elements.end()) && (otheriter != other.elements.end()); | 
|---|
| 648 | ++iter, ++otheriter) { | 
|---|
| 649 | bool status = true; | 
|---|
| 650 | status = status && (iter->first == otheriter->first); | 
|---|
| 651 | status = status && (*(iter->second) == *(otheriter->second)); | 
|---|
| 652 | if (!status) { | 
|---|
| 653 | std::cout << *(iter->second) << " not equal to " << *(otheriter->second) << "." << std::endl; | 
|---|
| 654 | return false; | 
|---|
| 655 | } | 
|---|
| 656 | //    else | 
|---|
| 657 | //      std::cout << (iter->second)->getName() << " are equal to " << (otheriter->second)->getName() << "." << std::endl; | 
|---|
| 658 | } | 
|---|
| 659 | if (strncmp(header1, other.header1, MAXSTRINGSIZE) != 0) return false; | 
|---|
| 660 | if (strncmp(header2, other.header2, MAXSTRINGSIZE) != 0) return false; | 
|---|
| 661 | return true; | 
|---|
| 662 | } | 
|---|