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