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