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 OpenMD 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 OpenMD { |
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 |
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 |
|
105 |
unsigned int ElementsTable::GetNumberOfElements() { |
106 |
if (!init_) |
107 |
Init(); |
108 |
|
109 |
return elements_.size(); |
110 |
} |
111 |
|
112 |
const char *ElementsTable::GetSymbol(int atomicnum) { |
113 |
if (!init_) |
114 |
Init(); |
115 |
|
116 |
if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) |
117 |
return("\0"); |
118 |
|
119 |
return(elements_[atomicnum]->GetSymbol()); |
120 |
} |
121 |
|
122 |
int ElementsTable::GetMaxBonds(int atomicnum) { |
123 |
if (!init_) |
124 |
Init(); |
125 |
|
126 |
if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) |
127 |
return(0); |
128 |
|
129 |
return(elements_[atomicnum]->GetMaxBonds()); |
130 |
} |
131 |
|
132 |
RealType ElementsTable::GetElectroNeg(int atomicnum) { |
133 |
if (!init_) |
134 |
Init(); |
135 |
|
136 |
if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) |
137 |
return(0.0); |
138 |
|
139 |
return(elements_[atomicnum]->GetElectroNeg()); |
140 |
} |
141 |
|
142 |
RealType ElementsTable::GetIonization(int atomicnum) { |
143 |
if (!init_) |
144 |
Init(); |
145 |
|
146 |
if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) |
147 |
return(0.0); |
148 |
|
149 |
return(elements_[atomicnum]->GetIonization()); |
150 |
} |
151 |
|
152 |
|
153 |
RealType ElementsTable::GetElectronAffinity(int atomicnum) { |
154 |
if (!init_) |
155 |
Init(); |
156 |
|
157 |
if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) |
158 |
return(0.0); |
159 |
|
160 |
return(elements_[atomicnum]->GetElectronAffinity()); |
161 |
} |
162 |
|
163 |
std::vector<RealType> ElementsTable::GetRGB(int atomicnum) { |
164 |
if (!init_) |
165 |
Init(); |
166 |
|
167 |
std::vector <RealType> colors; |
168 |
colors.reserve(3); |
169 |
|
170 |
if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) { |
171 |
colors.push_back(0.0); |
172 |
colors.push_back(0.0); |
173 |
colors.push_back(0.0); |
174 |
return(colors); |
175 |
} |
176 |
|
177 |
colors.push_back(elements_[atomicnum]->GetRed()); |
178 |
colors.push_back(elements_[atomicnum]->GetGreen()); |
179 |
colors.push_back(elements_[atomicnum]->GetBlue()); |
180 |
|
181 |
return (colors); |
182 |
} |
183 |
|
184 |
std::string ElementsTable::GetName(int atomicnum) { |
185 |
if (!init_) |
186 |
Init(); |
187 |
|
188 |
if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) |
189 |
return("Unknown"); |
190 |
|
191 |
return(elements_[atomicnum]->GetName()); |
192 |
} |
193 |
|
194 |
RealType ElementsTable::GetVdwRad(int atomicnum) { |
195 |
if (!init_) |
196 |
Init(); |
197 |
|
198 |
if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) |
199 |
return(0.0); |
200 |
|
201 |
return(elements_[atomicnum]->GetVdwRad()); |
202 |
} |
203 |
|
204 |
RealType ElementsTable::CorrectedBondRad(int atomicnum, int hyb) { |
205 |
RealType rad; |
206 |
if (!init_) |
207 |
Init(); |
208 |
|
209 |
if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) |
210 |
return(1.0); |
211 |
|
212 |
rad = elements_[atomicnum]->GetCovalentRad(); |
213 |
|
214 |
if (hyb == 2) |
215 |
rad *= 0.95; |
216 |
else if (hyb == 1) |
217 |
rad *= 0.90; |
218 |
|
219 |
return(rad); |
220 |
} |
221 |
|
222 |
RealType ElementsTable::CorrectedVdwRad(int atomicnum, int hyb) { |
223 |
RealType rad; |
224 |
if (!init_) |
225 |
Init(); |
226 |
|
227 |
if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) |
228 |
return(1.95); |
229 |
|
230 |
rad = elements_[atomicnum]->GetVdwRad(); |
231 |
|
232 |
if (hyb == 2) |
233 |
rad *= 0.95; |
234 |
else if (hyb == 1) |
235 |
rad *= 0.90; |
236 |
|
237 |
return(rad); |
238 |
} |
239 |
|
240 |
RealType ElementsTable::GetCovalentRad(int atomicnum) { |
241 |
if (!init_) |
242 |
Init(); |
243 |
|
244 |
if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) |
245 |
return(0.0); |
246 |
|
247 |
return(elements_[atomicnum]->GetCovalentRad()); |
248 |
} |
249 |
|
250 |
RealType ElementsTable::GetMass(int atomicnum) { |
251 |
if (!init_) |
252 |
Init(); |
253 |
|
254 |
if (atomicnum < 0 || atomicnum > static_cast<int>(elements_.size())) |
255 |
return(0.0); |
256 |
|
257 |
return(elements_[atomicnum]->GetMass()); |
258 |
} |
259 |
|
260 |
int ElementsTable::GetAtomicNum(const char *sym) { |
261 |
int temp; |
262 |
return GetAtomicNum(sym, temp); |
263 |
} |
264 |
|
265 |
int ElementsTable::GetAtomicNum(const char *sym, int &iso) { |
266 |
if (!init_) |
267 |
Init(); |
268 |
|
269 |
std::vector<Element*>::iterator i; |
270 |
for (i = elements_.begin();i != elements_.end();i++) |
271 |
if (!strncasecmp(sym,(*i)->GetSymbol(),2)) |
272 |
return((*i)->GetAtomicNum()); |
273 |
|
274 |
if (strcasecmp(sym, "D") == 0) { |
275 |
iso = 2; |
276 |
return(1); |
277 |
} else if (strcasecmp(sym, "T") == 0) { |
278 |
iso = 3; |
279 |
return(1); |
280 |
} else |
281 |
iso = 0; |
282 |
return(0); |
283 |
} |
284 |
|
285 |
void ElementsTable::Init() { |
286 |
if (init_) |
287 |
return; |
288 |
init_ = true; |
289 |
|
290 |
std::string buffer, subbuffer; |
291 |
ifstrstream ifs1, ifs2, ifs3, ifs4, *ifsP; |
292 |
// First, look for an environment variable |
293 |
if (getenv(envvar_.c_str()) != NULL) { |
294 |
buffer = getenv(envvar_.c_str()); |
295 |
buffer += FILE_SEP_CHAR; |
296 |
|
297 |
|
298 |
|
299 |
|
300 |
if (!subdir_.empty()) { |
301 |
subbuffer = buffer; |
302 |
subbuffer += subdir_; |
303 |
subbuffer += FILE_SEP_CHAR; |
304 |
} |
305 |
|
306 |
|
307 |
|
308 |
buffer += filename_; |
309 |
subbuffer += filename_; |
310 |
|
311 |
|
312 |
ifs1.open(subbuffer.c_str()); |
313 |
ifsP= &ifs1; |
314 |
if (!(ifsP->is_open())) { |
315 |
ifs2.open(buffer.c_str()); |
316 |
ifsP = &ifs2; |
317 |
} |
318 |
|
319 |
} else { |
320 |
sprintf( painCave.errMsg, |
321 |
"ElementsTable error.\n" |
322 |
"\tunable to open datafile %s \n", filename_.c_str()); |
323 |
painCave.isFatal = 0; |
324 |
simError(); |
325 |
} |
326 |
|
327 |
char charBuffer[BUFF_SIZE]; |
328 |
if ((*ifsP)) { |
329 |
while(ifsP->getline(charBuffer,BUFF_SIZE)) |
330 |
ParseLine(charBuffer); |
331 |
|
332 |
if (ifs1) |
333 |
ifs1.close(); |
334 |
if (ifs2) |
335 |
ifs2.close(); |
336 |
if (ifs3) |
337 |
ifs3.close(); |
338 |
if (ifs4) |
339 |
ifs4.close(); |
340 |
|
341 |
if (GetSize() == 0) { |
342 |
sprintf( painCave.errMsg, |
343 |
"ElementsTable error.\n" |
344 |
"\tCannot initialize database %s \n", filename_.c_str()); |
345 |
painCave.isFatal = 0; |
346 |
simError(); |
347 |
} |
348 |
|
349 |
} |
350 |
|
351 |
} |
352 |
} |