| 1 | gezelter | 1210 | /********************************************************************** | 
| 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 "config.h" | 
| 33 | cli2 | 1290 | #include <cstdlib> | 
| 34 | gezelter | 1210 | #include <string> | 
| 35 |  |  | #include <fstream> | 
| 36 | gezelter | 1291 | #include <cstdlib> | 
| 37 | gezelter | 1210 | #include "utils/ElementsTable.hpp" | 
| 38 |  |  | #include "utils/simError.h" | 
| 39 | chuckv | 1224 | #include "io/basic_ifstrstream.hpp" | 
| 40 | gezelter | 1210 |  | 
| 41 |  |  | #if !HAVE_STRNCASECMP | 
| 42 |  |  | extern "C" int strncasecmp(const char *s1, const char *s2, size_t n); | 
| 43 |  |  | #endif | 
| 44 |  |  |  | 
| 45 |  |  | #ifdef WIN32 | 
| 46 |  |  | #define FILE_SEP_CHAR "\\" | 
| 47 |  |  | #else | 
| 48 |  |  | #define FILE_SEP_CHAR "/" | 
| 49 |  |  | #endif | 
| 50 |  |  |  | 
| 51 |  |  | #ifndef BUFF_SIZE | 
| 52 |  |  | #define BUFF_SIZE 32768 | 
| 53 |  |  | #endif | 
| 54 |  |  |  | 
| 55 |  |  | namespace oopse { | 
| 56 |  |  |  | 
| 57 |  |  | ElementsTable etab; | 
| 58 |  |  |  | 
| 59 |  |  | ElementsTable::ElementsTable() { | 
| 60 |  |  | init_ = false; | 
| 61 |  |  | STR_DEFINE(dir_, FRC_PATH ); | 
| 62 |  |  | envvar_ = "FORCE_PARAM_PATH"; | 
| 63 |  |  | filename_ = "element.txt"; | 
| 64 |  |  | } | 
| 65 |  |  |  | 
| 66 |  |  | ElementsTable::~ElementsTable() { | 
| 67 |  |  | std::vector<Element*>::iterator i; | 
| 68 |  |  | for (i = elements_.begin(); i != elements_.end(); i++) | 
| 69 |  |  | delete *i; | 
| 70 |  |  | } | 
| 71 |  |  |  | 
| 72 |  |  | void ElementsTable::ParseLine(const char *line) { | 
| 73 |  |  | int num, maxbonds; | 
| 74 |  |  | char symbol[5]; | 
| 75 |  |  | char name[256]; | 
| 76 |  |  | RealType Rcov,Rvdw,mass, elNeg, ionize, elAffin; | 
| 77 |  |  | RealType red, green, blue; | 
| 78 |  |  |  | 
| 79 |  |  | // skip comment line (at the top) | 
| 80 |  |  |  | 
| 81 |  |  | if (line[0] != '#')  { | 
| 82 |  |  | sscanf(line,"%d %5s %lf %*f %lf %d %lf %lf %lf %lf %lf %lf %lf %255s", | 
| 83 |  |  | &num, | 
| 84 |  |  | symbol, | 
| 85 |  |  | &Rcov, | 
| 86 |  |  | &Rvdw, | 
| 87 |  |  | &maxbonds, | 
| 88 |  |  | &mass, | 
| 89 |  |  | &elNeg, | 
| 90 |  |  | &ionize, | 
| 91 |  |  | &elAffin, | 
| 92 |  |  | &red, | 
| 93 |  |  | &green, | 
| 94 |  |  | &blue, | 
| 95 |  |  | name); | 
| 96 |  |  |  | 
| 97 |  |  | Element *ele = new Element(num, symbol, Rcov, Rvdw, maxbonds, mass, | 
| 98 |  |  | elNeg, ionize, elAffin, red, green, blue, | 
| 99 |  |  | name); | 
| 100 |  |  | elements_.push_back(ele); | 
| 101 |  |  | } | 
| 102 |  |  | } | 
| 103 |  |  |  | 
| 104 |  |  | unsigned int ElementsTable::GetNumberOfElements() { | 
| 105 |  |  | if (!init_) | 
| 106 |  |  | Init(); | 
| 107 |  |  |  | 
| 108 |  |  | return elements_.size(); | 
| 109 |  |  | } | 
| 110 |  |  |  | 
| 111 |  |  | char *ElementsTable::GetSymbol(int atomicnum) { | 
| 112 |  |  | if (!init_) | 
| 113 |  |  | Init(); | 
| 114 |  |  |  | 
| 115 |  |  | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 116 |  |  | return("\0"); | 
| 117 |  |  |  | 
| 118 |  |  | return(elements_[atomicnum]->GetSymbol()); | 
| 119 |  |  | } | 
| 120 |  |  |  | 
| 121 |  |  | int ElementsTable::GetMaxBonds(int atomicnum) { | 
| 122 |  |  | if (!init_) | 
| 123 |  |  | Init(); | 
| 124 |  |  |  | 
| 125 |  |  | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 126 |  |  | return(0); | 
| 127 |  |  |  | 
| 128 |  |  | return(elements_[atomicnum]->GetMaxBonds()); | 
| 129 |  |  | } | 
| 130 |  |  |  | 
| 131 |  |  | RealType ElementsTable::GetElectroNeg(int atomicnum) { | 
| 132 |  |  | if (!init_) | 
| 133 |  |  | Init(); | 
| 134 |  |  |  | 
| 135 |  |  | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 136 |  |  | return(0.0); | 
| 137 |  |  |  | 
| 138 |  |  | return(elements_[atomicnum]->GetElectroNeg()); | 
| 139 |  |  | } | 
| 140 |  |  |  | 
| 141 |  |  | RealType ElementsTable::GetIonization(int atomicnum) { | 
| 142 |  |  | if (!init_) | 
| 143 |  |  | Init(); | 
| 144 |  |  |  | 
| 145 |  |  | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 146 |  |  | return(0.0); | 
| 147 |  |  |  | 
| 148 |  |  | return(elements_[atomicnum]->GetIonization()); | 
| 149 |  |  | } | 
| 150 |  |  |  | 
| 151 |  |  |  | 
| 152 |  |  | RealType ElementsTable::GetElectronAffinity(int atomicnum) { | 
| 153 |  |  | if (!init_) | 
| 154 |  |  | Init(); | 
| 155 |  |  |  | 
| 156 |  |  | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 157 |  |  | return(0.0); | 
| 158 |  |  |  | 
| 159 |  |  | return(elements_[atomicnum]->GetElectronAffinity()); | 
| 160 |  |  | } | 
| 161 |  |  |  | 
| 162 |  |  | std::vector<RealType> ElementsTable::GetRGB(int atomicnum) { | 
| 163 |  |  | if (!init_) | 
| 164 |  |  | Init(); | 
| 165 |  |  |  | 
| 166 |  |  | std::vector <RealType> colors; | 
| 167 |  |  | colors.reserve(3); | 
| 168 |  |  |  | 
| 169 |  |  | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) { | 
| 170 |  |  | colors.push_back(0.0); | 
| 171 |  |  | colors.push_back(0.0); | 
| 172 |  |  | colors.push_back(0.0); | 
| 173 |  |  | return(colors); | 
| 174 |  |  | } | 
| 175 |  |  |  | 
| 176 |  |  | colors.push_back(elements_[atomicnum]->GetRed()); | 
| 177 |  |  | colors.push_back(elements_[atomicnum]->GetGreen()); | 
| 178 |  |  | colors.push_back(elements_[atomicnum]->GetBlue()); | 
| 179 |  |  |  | 
| 180 |  |  | return (colors); | 
| 181 |  |  | } | 
| 182 |  |  |  | 
| 183 |  |  | std::string ElementsTable::GetName(int atomicnum) { | 
| 184 |  |  | if (!init_) | 
| 185 |  |  | Init(); | 
| 186 |  |  |  | 
| 187 |  |  | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 188 |  |  | return("Unknown"); | 
| 189 |  |  |  | 
| 190 |  |  | return(elements_[atomicnum]->GetName()); | 
| 191 |  |  | } | 
| 192 |  |  |  | 
| 193 |  |  | RealType ElementsTable::GetVdwRad(int atomicnum) { | 
| 194 |  |  | if (!init_) | 
| 195 |  |  | Init(); | 
| 196 |  |  |  | 
| 197 |  |  | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 198 |  |  | return(0.0); | 
| 199 |  |  |  | 
| 200 |  |  | return(elements_[atomicnum]->GetVdwRad()); | 
| 201 |  |  | } | 
| 202 |  |  |  | 
| 203 |  |  | RealType ElementsTable::CorrectedBondRad(int atomicnum, int hyb) { | 
| 204 |  |  | RealType rad; | 
| 205 |  |  | if (!init_) | 
| 206 |  |  | Init(); | 
| 207 |  |  |  | 
| 208 |  |  | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 209 |  |  | return(1.0); | 
| 210 |  |  |  | 
| 211 |  |  | rad = elements_[atomicnum]->GetCovalentRad(); | 
| 212 |  |  |  | 
| 213 |  |  | if (hyb == 2) | 
| 214 |  |  | rad *= 0.95; | 
| 215 |  |  | else if (hyb == 1) | 
| 216 |  |  | rad *= 0.90; | 
| 217 |  |  |  | 
| 218 |  |  | return(rad); | 
| 219 |  |  | } | 
| 220 |  |  |  | 
| 221 |  |  | RealType ElementsTable::CorrectedVdwRad(int atomicnum, int hyb) { | 
| 222 |  |  | RealType rad; | 
| 223 |  |  | if (!init_) | 
| 224 |  |  | Init(); | 
| 225 |  |  |  | 
| 226 |  |  | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 227 |  |  | return(1.95); | 
| 228 |  |  |  | 
| 229 |  |  | rad = elements_[atomicnum]->GetVdwRad(); | 
| 230 |  |  |  | 
| 231 |  |  | if (hyb == 2) | 
| 232 |  |  | rad *= 0.95; | 
| 233 |  |  | else if (hyb == 1) | 
| 234 |  |  | rad *= 0.90; | 
| 235 |  |  |  | 
| 236 |  |  | return(rad); | 
| 237 |  |  | } | 
| 238 |  |  |  | 
| 239 |  |  | RealType ElementsTable::GetCovalentRad(int atomicnum) { | 
| 240 |  |  | if (!init_) | 
| 241 |  |  | Init(); | 
| 242 |  |  |  | 
| 243 |  |  | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 244 |  |  | return(0.0); | 
| 245 |  |  |  | 
| 246 |  |  | return(elements_[atomicnum]->GetCovalentRad()); | 
| 247 |  |  | } | 
| 248 |  |  |  | 
| 249 |  |  | RealType ElementsTable::GetMass(int atomicnum) { | 
| 250 |  |  | if (!init_) | 
| 251 |  |  | Init(); | 
| 252 |  |  |  | 
| 253 |  |  | if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) | 
| 254 |  |  | return(0.0); | 
| 255 |  |  |  | 
| 256 |  |  | return(elements_[atomicnum]->GetMass()); | 
| 257 |  |  | } | 
| 258 |  |  |  | 
| 259 |  |  | int ElementsTable::GetAtomicNum(const char *sym) { | 
| 260 |  |  | int temp; | 
| 261 |  |  | return GetAtomicNum(sym, temp); | 
| 262 |  |  | } | 
| 263 |  |  |  | 
| 264 |  |  | int ElementsTable::GetAtomicNum(const char *sym, int &iso) { | 
| 265 |  |  | if (!init_) | 
| 266 |  |  | Init(); | 
| 267 |  |  |  | 
| 268 |  |  | std::vector<Element*>::iterator i; | 
| 269 |  |  | for (i = elements_.begin();i != elements_.end();i++) | 
| 270 |  |  | if (!strncasecmp(sym,(*i)->GetSymbol(),2)) | 
| 271 |  |  | return((*i)->GetAtomicNum()); | 
| 272 |  |  |  | 
| 273 |  |  | if (strcasecmp(sym, "D") == 0) { | 
| 274 |  |  | iso = 2; | 
| 275 |  |  | return(1); | 
| 276 |  |  | } else if (strcasecmp(sym, "T") == 0) { | 
| 277 |  |  | iso = 3; | 
| 278 |  |  | return(1); | 
| 279 |  |  | } else | 
| 280 |  |  | iso = 0; | 
| 281 |  |  | return(0); | 
| 282 |  |  | } | 
| 283 |  |  |  | 
| 284 |  |  | void ElementsTable::Init() { | 
| 285 |  |  | if (init_) | 
| 286 |  |  | return; | 
| 287 |  |  | init_ = true; | 
| 288 |  |  |  | 
| 289 |  |  | std::string buffer, subbuffer; | 
| 290 | chuckv | 1224 | ifstrstream ifs1, ifs2, ifs3, ifs4, *ifsP; | 
| 291 | gezelter | 1210 | // First, look for an environment variable | 
| 292 |  |  | if (getenv(envvar_.c_str()) != NULL) { | 
| 293 |  |  | buffer = getenv(envvar_.c_str()); | 
| 294 |  |  | buffer += FILE_SEP_CHAR; | 
| 295 |  |  |  | 
| 296 |  |  | if (!subdir_.empty()) { | 
| 297 |  |  | subbuffer = buffer; | 
| 298 |  |  | subbuffer += subdir_; | 
| 299 |  |  | subbuffer += FILE_SEP_CHAR; | 
| 300 |  |  | } | 
| 301 |  |  |  | 
| 302 |  |  | buffer += filename_; | 
| 303 |  |  | subbuffer += filename_; | 
| 304 |  |  |  | 
| 305 |  |  | ifs1.open(subbuffer.c_str()); | 
| 306 |  |  | ifsP= &ifs1; | 
| 307 |  |  | if (!(*ifsP)) { | 
| 308 |  |  | ifs2.open(buffer.c_str()); | 
| 309 |  |  | ifsP = &ifs2; | 
| 310 |  |  | } | 
| 311 |  |  | } else { | 
| 312 |  |  | sprintf( painCave.errMsg, | 
| 313 |  |  | "ElementsTable error.\n" | 
| 314 |  |  | "\tunable to open datafile %s \n", filename_.c_str()); | 
| 315 |  |  | painCave.isFatal = 0; | 
| 316 |  |  | simError(); | 
| 317 |  |  | } | 
| 318 |  |  |  | 
| 319 |  |  | char charBuffer[BUFF_SIZE]; | 
| 320 |  |  | if ((*ifsP)) { | 
| 321 |  |  | while(ifsP->getline(charBuffer,BUFF_SIZE)) | 
| 322 |  |  | ParseLine(charBuffer); | 
| 323 |  |  |  | 
| 324 |  |  | if (ifs1) | 
| 325 |  |  | ifs1.close(); | 
| 326 |  |  | if (ifs2) | 
| 327 |  |  | ifs2.close(); | 
| 328 |  |  | if (ifs3) | 
| 329 |  |  | ifs3.close(); | 
| 330 |  |  | if (ifs4) | 
| 331 |  |  | ifs4.close(); | 
| 332 |  |  |  | 
| 333 |  |  | if (GetSize() == 0) { | 
| 334 |  |  | sprintf( painCave.errMsg, | 
| 335 |  |  | "ElementsTable error.\n" | 
| 336 |  |  | "\tCannot initialize database %s \n", filename_.c_str()); | 
| 337 |  |  | painCave.isFatal = 0; | 
| 338 |  |  | simError(); | 
| 339 |  |  | } | 
| 340 |  |  |  | 
| 341 |  |  | } | 
| 342 |  |  |  | 
| 343 |  |  | } | 
| 344 |  |  | } |