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