| 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. | 
| 5 |  | 
| 6 | We have retained the OpenBabel copyright and GPL license on this class: | 
| 7 |  | 
| 8 | Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc. | 
| 9 | Some portions Copyright (C) 2001-2005 by Geoffrey R. Hutchison | 
| 10 |  | 
| 11 | This file is part of the Open Babel project. | 
| 12 | For more information, see <http://openbabel.sourceforge.net/> | 
| 13 |  | 
| 14 | This program is free software; you can redistribute it and/or modify | 
| 15 | it under the terms of the GNU General Public License as published by | 
| 16 | the Free Software Foundation version 2 of the License. | 
| 17 |  | 
| 18 | This program is distributed in the hope that it will be useful, | 
| 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 21 | GNU General Public License for more details. | 
| 22 | ***********************************************************************/ | 
| 23 |  | 
| 24 | /** | 
| 25 | * @file ElementsTable.cpp | 
| 26 | * @author gezelter | 
| 27 | * @date 12/21/2007 | 
| 28 | * @time 11:30am | 
| 29 | * @version 1.0 | 
| 30 | */ | 
| 31 |  | 
| 32 | #include <iostream> | 
| 33 | #include "config.h" | 
| 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" | 
| 41 |  | 
| 42 | #if !HAVE_STRNCASECMP | 
| 43 | extern "C" int strncasecmp(const char *s1, const char *s2, size_t n); | 
| 44 | #endif | 
| 45 |  | 
| 46 | #ifdef WIN32 | 
| 47 | #define FILE_SEP_CHAR "\\" | 
| 48 | #else | 
| 49 | #define FILE_SEP_CHAR "/" | 
| 50 | #endif | 
| 51 |  | 
| 52 | #ifndef BUFF_SIZE | 
| 53 | #define BUFF_SIZE 32768 | 
| 54 | #endif | 
| 55 |  | 
| 56 | namespace oopse { | 
| 57 |  | 
| 58 | ElementsTable etab; | 
| 59 |  | 
| 60 | ElementsTable::ElementsTable() { | 
| 61 | init_ = false; | 
| 62 | STR_DEFINE(dir_, FRC_PATH ); | 
| 63 | envvar_ = "FORCE_PARAM_PATH"; | 
| 64 | filename_ = "element.txt"; | 
| 65 | } | 
| 66 |  | 
| 67 | ElementsTable::~ElementsTable() { | 
| 68 | std::vector<Element*>::iterator i; | 
| 69 | for (i = elements_.begin(); i != elements_.end(); i++) | 
| 70 | delete *i; | 
| 71 | } | 
| 72 |  | 
| 73 | void ElementsTable::ParseLine(const char *line) { | 
| 74 | int num, maxbonds; | 
| 75 | char symbol[5]; | 
| 76 | char name[256]; | 
| 77 | RealType Rcov,Rvdw,mass, elNeg, ionize, elAffin; | 
| 78 | RealType red, green, blue; | 
| 79 |  | 
| 80 | // skip comment line (at the top) | 
| 81 |  | 
| 82 | if (line[0] != '#')  { | 
| 83 | sscanf(line,"%d %5s %lf %*f %lf %d %lf %lf %lf %lf %lf %lf %lf %255s", | 
| 84 | &num, | 
| 85 | symbol, | 
| 86 | &Rcov, | 
| 87 | &Rvdw, | 
| 88 | &maxbonds, | 
| 89 | &mass, | 
| 90 | &elNeg, | 
| 91 | &ionize, | 
| 92 | &elAffin, | 
| 93 | &red, | 
| 94 | &green, | 
| 95 | &blue, | 
| 96 | name); | 
| 97 |  | 
| 98 | Element *ele = new Element(num, symbol, Rcov, Rvdw, maxbonds, mass, | 
| 99 | elNeg, ionize, elAffin, red, green, blue, | 
| 100 | name); | 
| 101 | elements_.push_back(ele); | 
| 102 |  | 
| 103 | } | 
| 104 | } | 
| 105 |  | 
| 106 | unsigned int ElementsTable::GetNumberOfElements() { | 
| 107 | if (!init_) | 
| 108 | Init(); | 
| 109 |  | 
| 110 | return elements_.size(); | 
| 111 | } | 
| 112 |  | 
| 113 | char *ElementsTable::GetSymbol(int atomicnum) { | 
| 114 | if (!init_) | 
| 115 | Init(); | 
| 116 |  | 
| 117 | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 118 | return("\0"); | 
| 119 |  | 
| 120 | return(elements_[atomicnum]->GetSymbol()); | 
| 121 | } | 
| 122 |  | 
| 123 | int ElementsTable::GetMaxBonds(int atomicnum) { | 
| 124 | if (!init_) | 
| 125 | Init(); | 
| 126 |  | 
| 127 | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 128 | return(0); | 
| 129 |  | 
| 130 | return(elements_[atomicnum]->GetMaxBonds()); | 
| 131 | } | 
| 132 |  | 
| 133 | RealType ElementsTable::GetElectroNeg(int atomicnum) { | 
| 134 | if (!init_) | 
| 135 | Init(); | 
| 136 |  | 
| 137 | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 138 | return(0.0); | 
| 139 |  | 
| 140 | return(elements_[atomicnum]->GetElectroNeg()); | 
| 141 | } | 
| 142 |  | 
| 143 | RealType ElementsTable::GetIonization(int atomicnum) { | 
| 144 | if (!init_) | 
| 145 | Init(); | 
| 146 |  | 
| 147 | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 148 | return(0.0); | 
| 149 |  | 
| 150 | return(elements_[atomicnum]->GetIonization()); | 
| 151 | } | 
| 152 |  | 
| 153 |  | 
| 154 | RealType ElementsTable::GetElectronAffinity(int atomicnum) { | 
| 155 | if (!init_) | 
| 156 | Init(); | 
| 157 |  | 
| 158 | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 159 | return(0.0); | 
| 160 |  | 
| 161 | return(elements_[atomicnum]->GetElectronAffinity()); | 
| 162 | } | 
| 163 |  | 
| 164 | std::vector<RealType> ElementsTable::GetRGB(int atomicnum) { | 
| 165 | if (!init_) | 
| 166 | Init(); | 
| 167 |  | 
| 168 | std::vector <RealType> colors; | 
| 169 | colors.reserve(3); | 
| 170 |  | 
| 171 | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) { | 
| 172 | colors.push_back(0.0); | 
| 173 | colors.push_back(0.0); | 
| 174 | colors.push_back(0.0); | 
| 175 | return(colors); | 
| 176 | } | 
| 177 |  | 
| 178 | colors.push_back(elements_[atomicnum]->GetRed()); | 
| 179 | colors.push_back(elements_[atomicnum]->GetGreen()); | 
| 180 | colors.push_back(elements_[atomicnum]->GetBlue()); | 
| 181 |  | 
| 182 | return (colors); | 
| 183 | } | 
| 184 |  | 
| 185 | std::string ElementsTable::GetName(int atomicnum) { | 
| 186 | if (!init_) | 
| 187 | Init(); | 
| 188 |  | 
| 189 | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 190 | return("Unknown"); | 
| 191 |  | 
| 192 | return(elements_[atomicnum]->GetName()); | 
| 193 | } | 
| 194 |  | 
| 195 | RealType ElementsTable::GetVdwRad(int atomicnum) { | 
| 196 | if (!init_) | 
| 197 | Init(); | 
| 198 |  | 
| 199 | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 200 | return(0.0); | 
| 201 |  | 
| 202 | return(elements_[atomicnum]->GetVdwRad()); | 
| 203 | } | 
| 204 |  | 
| 205 | RealType ElementsTable::CorrectedBondRad(int atomicnum, int hyb) { | 
| 206 | RealType rad; | 
| 207 | if (!init_) | 
| 208 | Init(); | 
| 209 |  | 
| 210 | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 211 | return(1.0); | 
| 212 |  | 
| 213 | rad = elements_[atomicnum]->GetCovalentRad(); | 
| 214 |  | 
| 215 | if (hyb == 2) | 
| 216 | rad *= 0.95; | 
| 217 | else if (hyb == 1) | 
| 218 | rad *= 0.90; | 
| 219 |  | 
| 220 | return(rad); | 
| 221 | } | 
| 222 |  | 
| 223 | RealType ElementsTable::CorrectedVdwRad(int atomicnum, int hyb) { | 
| 224 | RealType rad; | 
| 225 | if (!init_) | 
| 226 | Init(); | 
| 227 |  | 
| 228 | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 229 | return(1.95); | 
| 230 |  | 
| 231 | rad = elements_[atomicnum]->GetVdwRad(); | 
| 232 |  | 
| 233 | if (hyb == 2) | 
| 234 | rad *= 0.95; | 
| 235 | else if (hyb == 1) | 
| 236 | rad *= 0.90; | 
| 237 |  | 
| 238 | return(rad); | 
| 239 | } | 
| 240 |  | 
| 241 | RealType ElementsTable::GetCovalentRad(int atomicnum) { | 
| 242 | if (!init_) | 
| 243 | Init(); | 
| 244 |  | 
| 245 | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 246 | return(0.0); | 
| 247 |  | 
| 248 | return(elements_[atomicnum]->GetCovalentRad()); | 
| 249 | } | 
| 250 |  | 
| 251 | RealType ElementsTable::GetMass(int atomicnum) { | 
| 252 | if (!init_) | 
| 253 | Init(); | 
| 254 |  | 
| 255 | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 256 | return(0.0); | 
| 257 |  | 
| 258 | return(elements_[atomicnum]->GetMass()); | 
| 259 | } | 
| 260 |  | 
| 261 | int ElementsTable::GetAtomicNum(const char *sym) { | 
| 262 | int temp; | 
| 263 | return GetAtomicNum(sym, temp); | 
| 264 | } | 
| 265 |  | 
| 266 | int ElementsTable::GetAtomicNum(const char *sym, int &iso) { | 
| 267 | if (!init_) | 
| 268 | Init(); | 
| 269 |  | 
| 270 | std::vector<Element*>::iterator i; | 
| 271 | for (i = elements_.begin();i != elements_.end();i++) | 
| 272 | if (!strncasecmp(sym,(*i)->GetSymbol(),2)) | 
| 273 | return((*i)->GetAtomicNum()); | 
| 274 |  | 
| 275 | if (strcasecmp(sym, "D") == 0) { | 
| 276 | iso = 2; | 
| 277 | return(1); | 
| 278 | } else if (strcasecmp(sym, "T") == 0) { | 
| 279 | iso = 3; | 
| 280 | return(1); | 
| 281 | } else | 
| 282 | iso = 0; | 
| 283 | return(0); | 
| 284 | } | 
| 285 |  | 
| 286 | void ElementsTable::Init() { | 
| 287 | if (init_) | 
| 288 | return; | 
| 289 | init_ = true; | 
| 290 |  | 
| 291 | std::string buffer, subbuffer; | 
| 292 | ifstrstream ifs1, ifs2, ifs3, ifs4, *ifsP; | 
| 293 | // First, look for an environment variable | 
| 294 | if (getenv(envvar_.c_str()) != NULL) { | 
| 295 | buffer = getenv(envvar_.c_str()); | 
| 296 | buffer += FILE_SEP_CHAR; | 
| 297 |  | 
| 298 | if (!subdir_.empty()) { | 
| 299 | subbuffer = buffer; | 
| 300 | subbuffer += subdir_; | 
| 301 | subbuffer += FILE_SEP_CHAR; | 
| 302 | } | 
| 303 |  | 
| 304 | buffer += filename_; | 
| 305 | subbuffer += filename_; | 
| 306 |  | 
| 307 | ifs1.open(subbuffer.c_str()); | 
| 308 | ifsP= &ifs1; | 
| 309 | if (!(*ifsP)) { | 
| 310 | ifs2.open(buffer.c_str()); | 
| 311 | ifsP = &ifs2; | 
| 312 | } | 
| 313 | } else { | 
| 314 | sprintf( painCave.errMsg, | 
| 315 | "ElementsTable error.\n" | 
| 316 | "\tunable to open datafile %s \n", filename_.c_str()); | 
| 317 | painCave.isFatal = 0; | 
| 318 | simError(); | 
| 319 | } | 
| 320 |  | 
| 321 | char charBuffer[BUFF_SIZE]; | 
| 322 | if ((*ifsP)) { | 
| 323 | while(ifsP->getline(charBuffer,BUFF_SIZE)) | 
| 324 | ParseLine(charBuffer); | 
| 325 |  | 
| 326 | if (ifs1) | 
| 327 | ifs1.close(); | 
| 328 | if (ifs2) | 
| 329 | ifs2.close(); | 
| 330 | if (ifs3) | 
| 331 | ifs3.close(); | 
| 332 | if (ifs4) | 
| 333 | ifs4.close(); | 
| 334 |  | 
| 335 | if (GetSize() == 0) { | 
| 336 | sprintf( painCave.errMsg, | 
| 337 | "ElementsTable error.\n" | 
| 338 | "\tCannot initialize database %s \n", filename_.c_str()); | 
| 339 | painCave.isFatal = 0; | 
| 340 | simError(); | 
| 341 | } | 
| 342 |  | 
| 343 | } | 
| 344 |  | 
| 345 | } | 
| 346 | } |