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