| 1 |  | /********************************************************************** | 
| 2 |  |  | 
| 3 |  | This basic Periodic Table class was originally taken from the data.cpp | 
| 4 | < | file in OpenBabel. The code has been modified to match the OOPSE coding style. | 
| 4 | > | file in OpenBabel. The code has been modified to match the OpenMD coding style. | 
| 5 |  |  | 
| 6 |  | We have retained the OpenBabel copyright and GPL license on this class: | 
| 7 |  |  | 
| 25 |  | * @file ElementsTable.cpp | 
| 26 |  | * @author gezelter | 
| 27 |  | * @date 12/21/2007 | 
| 28 | – | * @time 11:30am | 
| 28 |  | * @version 1.0 | 
| 29 |  | */ | 
| 30 |  |  | 
| 31 |  | #include "config.h" | 
| 32 | + |  | 
| 33 | + | #include <iostream> | 
| 34 |  | #include <cstdlib> | 
| 35 |  | #include <string> | 
| 36 |  | #include <fstream> | 
| 37 | + | #include <cstdlib> | 
| 38 |  | #include "utils/ElementsTable.hpp" | 
| 39 |  | #include "utils/simError.h" | 
| 40 | < | #include "io/basic_ifstrstream.hpp" | 
| 40 | > | #include "io/ifstrstream.hpp" | 
| 41 |  |  | 
| 42 | < | #if !HAVE_STRNCASECMP | 
| 43 | < | extern "C" int strncasecmp(const char *s1, const char *s2, size_t n); | 
| 42 | > | #ifdef _MSC_VER | 
| 43 | > | #define strncasecmp _strnicmp | 
| 44 | > | #define strcasecmp _stricmp | 
| 45 |  | #endif | 
| 46 |  |  | 
| 47 | + |  | 
| 48 |  | #ifdef WIN32 | 
| 49 |  | #define FILE_SEP_CHAR "\\" | 
| 50 |  | #else | 
| 55 |  | #define BUFF_SIZE 32768 | 
| 56 |  | #endif | 
| 57 |  |  | 
| 58 | < | namespace oopse { | 
| 58 | > | namespace OpenMD { | 
| 59 |  |  | 
| 60 |  | ElementsTable etab; | 
| 61 |  |  | 
| 62 |  | ElementsTable::ElementsTable() { | 
| 63 |  | init_ = false; | 
| 64 | < | STR_DEFINE(dir_, FRC_PATH ); | 
| 64 | > | dir_ = std::string("TO_STRING(FRC_PATH)"); | 
| 65 |  | envvar_ = "FORCE_PARAM_PATH"; | 
| 66 |  | filename_ = "element.txt"; | 
| 67 |  | } | 
| 68 |  |  | 
| 69 |  | ElementsTable::~ElementsTable() { | 
| 70 |  | std::vector<Element*>::iterator i; | 
| 71 | < | for (i = elements_.begin(); i != elements_.end(); i++) | 
| 71 | > | for (i = elements_.begin(); i != elements_.end(); ++i) | 
| 72 |  | delete *i; | 
| 73 |  | } | 
| 74 |  |  | 
| 75 |  | void ElementsTable::ParseLine(const char *line) { | 
| 76 |  | int num, maxbonds; | 
| 77 | < | char symbol[5]; | 
| 77 | > | char symbol[6]; | 
| 78 |  | char name[256]; | 
| 79 |  | RealType Rcov,Rvdw,mass, elNeg, ionize, elAffin; | 
| 80 |  | RealType red, green, blue; | 
| 81 |  |  | 
| 82 |  | // skip comment line (at the top) | 
| 79 | – |  | 
| 83 |  | if (line[0] != '#')  { | 
| 84 |  | sscanf(line,"%d %5s %lf %*f %lf %d %lf %lf %lf %lf %lf %lf %lf %255s", | 
| 85 |  | &num, | 
| 100 |  | elNeg, ionize, elAffin, red, green, blue, | 
| 101 |  | name); | 
| 102 |  | elements_.push_back(ele); | 
| 103 | + |  | 
| 104 |  | } | 
| 105 |  | } | 
| 106 |  |  | 
| 111 |  | return elements_.size(); | 
| 112 |  | } | 
| 113 |  |  | 
| 114 | < | char *ElementsTable::GetSymbol(int atomicnum) { | 
| 114 | > | const char *ElementsTable::GetSymbol(int atomicnum) { | 
| 115 |  | if (!init_) | 
| 116 |  | Init(); | 
| 117 |  |  | 
| 269 |  | Init(); | 
| 270 |  |  | 
| 271 |  | std::vector<Element*>::iterator i; | 
| 272 | < | for (i = elements_.begin();i != elements_.end();i++) | 
| 272 | > | for (i = elements_.begin();i != elements_.end(); ++i) | 
| 273 |  | if (!strncasecmp(sym,(*i)->GetSymbol(),2)) | 
| 274 |  | return((*i)->GetAtomicNum()); | 
| 275 |  |  | 
| 296 |  | buffer = getenv(envvar_.c_str()); | 
| 297 |  | buffer += FILE_SEP_CHAR; | 
| 298 |  |  | 
| 299 | + |  | 
| 300 | + |  | 
| 301 | + |  | 
| 302 |  | if (!subdir_.empty()) { | 
| 303 |  | subbuffer = buffer; | 
| 304 |  | subbuffer += subdir_; | 
| 305 |  | subbuffer += FILE_SEP_CHAR; | 
| 306 |  | } | 
| 307 |  |  | 
| 308 | + |  | 
| 309 | + |  | 
| 310 |  | buffer += filename_; | 
| 311 |  | subbuffer += filename_; | 
| 312 | + |  | 
| 313 |  |  | 
| 314 |  | ifs1.open(subbuffer.c_str()); | 
| 315 |  | ifsP= &ifs1; | 
| 316 | < | if (!(*ifsP)) { | 
| 316 | > | if (!(ifsP->is_open())) { | 
| 317 |  | ifs2.open(buffer.c_str()); | 
| 318 |  | ifsP = &ifs2; | 
| 319 |  | } | 
| 320 | + |  | 
| 321 |  | } else { | 
| 322 |  | sprintf( painCave.errMsg, | 
| 323 |  | "ElementsTable error.\n" | 
| 326 |  | simError(); | 
| 327 |  | } | 
| 328 |  |  | 
| 329 | < | char charBuffer[BUFF_SIZE]; | 
| 329 | > |  | 
| 330 |  | if ((*ifsP)) { | 
| 331 | + | char charBuffer[BUFF_SIZE]; | 
| 332 |  | while(ifsP->getline(charBuffer,BUFF_SIZE)) | 
| 333 |  | ParseLine(charBuffer); | 
| 334 | < |  | 
| 335 | < | if (ifs1) | 
| 336 | < | ifs1.close(); | 
| 337 | < | if (ifs2) | 
| 338 | < | ifs2.close(); | 
| 339 | < | if (ifs3) | 
| 340 | < | ifs3.close(); | 
| 341 | < | if (ifs4) | 
| 342 | < | ifs4.close(); | 
| 334 | > |  | 
| 335 | > | if (ifs1) | 
| 336 | > | ifs1.close(); | 
| 337 | > | if (ifs2) | 
| 338 | > | ifs2.close(); | 
| 339 | > | if (ifs3) | 
| 340 | > | ifs3.close(); | 
| 341 | > | if (ifs4) | 
| 342 | > | ifs4.close(); | 
| 343 | > |  | 
| 344 | > | if (GetSize() == 0) { | 
| 345 | > | sprintf( painCave.errMsg, | 
| 346 | > | "ElementsTable error.\n" | 
| 347 | > | "\tCannot initialize database %s \n", filename_.c_str()); | 
| 348 | > | painCave.isFatal = 0; | 
| 349 | > | simError(); | 
| 350 | > | } | 
| 351 |  |  | 
| 332 | – | if (GetSize() == 0) { | 
| 333 | – | sprintf( painCave.errMsg, | 
| 334 | – | "ElementsTable error.\n" | 
| 335 | – | "\tCannot initialize database %s \n", filename_.c_str()); | 
| 336 | – | painCave.isFatal = 0; | 
| 337 | – | simError(); | 
| 352 |  | } | 
| 339 | – |  | 
| 340 | – | } | 
| 353 |  |  | 
| 354 |  | } | 
| 355 |  | } |