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