OpenMD
3.2
Molecular Dynamics in the Open
Toggle main menu visibility
Loading...
Searching...
No Matches
ElementsTable.cpp
Go to the documentation of this file.
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 "
utils/ElementsTable.hpp
"
32
33
#include <config.h>
34
35
#include <algorithm>
36
#include <cstdlib>
37
#include <fstream>
38
#include <iostream>
39
#include <string>
40
41
#include "
io/ifstrstream.hpp
"
42
#include "utils/simError.h"
43
44
#ifdef WIN32
45
#define FILE_SEP_CHAR "\\"
46
#else
47
#define FILE_SEP_CHAR "/"
48
#endif
49
50
#ifndef BUFF_SIZE
51
#define BUFF_SIZE 32768
52
#endif
53
54
namespace
OpenMD
{
55
56
ElementsTable
etab;
57
58
ElementsTable::ElementsTable
() {
init_
=
false
; }
59
60
ElementsTable::~ElementsTable
() {
61
std::vector<Element*>::iterator i;
62
for
(i = elements_.begin(); i != elements_.end(); ++i)
63
delete
*i;
64
}
65
66
void
ElementsTable::ParseLine
(
const
char
* buffer) {
67
int
num, maxbonds;
68
char
symbol[4];
69
char
name[256];
70
RealType Rcov, Rvdw, mass, elNeg, ARENeg, ionize, elAffin;
71
RealType red, green, blue;
72
73
// skip comment line (at the top)
74
if
(buffer[0] !=
'#'
) {
75
sscanf(buffer,
76
"%d %3s %lf %lf %*f %lf %d %lf %lf %lf %lf %lf %lf %lf %255s"
,
77
&num, symbol, &ARENeg, &Rcov, &Rvdw, &maxbonds, &mass, &elNeg,
78
&ionize, &elAffin, &red, &green, &blue, name);
79
80
Element
* ele =
81
new
Element
(num, symbol, ARENeg, Rcov, Rvdw, maxbonds, mass, elNeg,
82
ionize, elAffin, red, green, blue, name);
83
elements_.push_back(ele);
84
}
85
}
86
87
unsigned
int
ElementsTable::GetNumberOfElements
() {
88
if
(!
init_
)
Init
();
89
90
return
elements_.size();
91
}
92
93
const
char
*
ElementsTable::GetSymbol
(
int
atomicnum) {
94
if
(!
init_
)
Init
();
95
96
if
(atomicnum < 0 || atomicnum >=
static_cast<
int
>
(elements_.size()))
97
return
(
"\0"
);
98
99
return
(elements_[atomicnum]->
GetSymbol
());
100
}
101
102
int
ElementsTable::GetMaxBonds
(
int
atomicnum) {
103
if
(!
init_
)
Init
();
104
105
if
(atomicnum < 0 || atomicnum >=
static_cast<
int
>
(elements_.size()))
106
return
(0);
107
108
return
(elements_[atomicnum]->
GetMaxBonds
());
109
}
110
111
RealType
ElementsTable::GetElectroNeg
(
int
atomicnum) {
112
if
(!
init_
)
Init
();
113
114
if
(atomicnum < 0 || atomicnum >=
static_cast<
int
>
(elements_.size()))
115
return
(0.0);
116
117
return
(elements_[atomicnum]->
GetElectroNeg
());
118
}
119
120
RealType
ElementsTable::GetAllredRochowElectroNeg
(
int
atomicnum) {
121
if
(!
init_
)
Init
();
122
123
if
(atomicnum < 0 || atomicnum >=
static_cast<
int
>
(elements_.size()))
124
return
(0.0);
125
126
return
(elements_[atomicnum]->
GetAllredRochowElectroNeg
());
127
}
128
129
RealType
ElementsTable::GetIonization
(
int
atomicnum) {
130
if
(!
init_
)
Init
();
131
132
if
(atomicnum < 0 || atomicnum >=
static_cast<
int
>
(elements_.size()))
133
return
(0.0);
134
135
return
(elements_[atomicnum]->
GetIonization
());
136
}
137
138
RealType
ElementsTable::GetElectronAffinity
(
int
atomicnum) {
139
if
(!
init_
)
Init
();
140
141
if
(atomicnum < 0 || atomicnum >=
static_cast<
int
>
(elements_.size()))
142
return
(0.0);
143
144
return
(elements_[atomicnum]->
GetElectronAffinity
());
145
}
146
147
std::vector<RealType>
ElementsTable::GetRGB
(
int
atomicnum) {
148
if
(!
init_
)
Init
();
149
150
std::vector<RealType> colors;
151
colors.reserve(3);
152
153
if
(atomicnum < 0 || atomicnum >=
static_cast<
int
>
(elements_.size())) {
154
colors.push_back(0.0);
155
colors.push_back(0.0);
156
colors.push_back(0.0);
157
return
(colors);
158
}
159
160
colors.push_back(elements_[atomicnum]->GetRed());
161
colors.push_back(elements_[atomicnum]->GetGreen());
162
colors.push_back(elements_[atomicnum]->GetBlue());
163
164
return
(colors);
165
}
166
167
std::string
ElementsTable::GetName
(
int
atomicnum) {
168
if
(!
init_
)
Init
();
169
170
if
(atomicnum < 0 || atomicnum >=
static_cast<
int
>
(elements_.size()))
171
return
(
"Unknown"
);
172
173
return
(elements_[atomicnum]->
GetName
());
174
}
175
176
RealType
ElementsTable::GetVdwRad
(
int
atomicnum) {
177
if
(!
init_
)
Init
();
178
179
if
(atomicnum < 0 || atomicnum >=
static_cast<
int
>
(elements_.size()))
180
return
(0.0);
181
182
return
(elements_[atomicnum]->
GetVdwRad
());
183
}
184
185
RealType
ElementsTable::CorrectedBondRad
(
int
atomicnum,
int
hyb) {
186
RealType rad;
187
if
(!
init_
)
Init
();
188
189
if
(atomicnum < 0 || atomicnum >=
static_cast<
int
>
(elements_.size()))
190
return
(1.0);
191
192
rad = elements_[atomicnum]->GetCovalentRad();
193
194
if
(hyb == 2)
195
rad *= 0.95;
196
else
if
(hyb == 1)
197
rad *= 0.90;
198
199
return
(rad);
200
}
201
202
RealType
ElementsTable::CorrectedVdwRad
(
int
atomicnum,
int
hyb) {
203
RealType rad;
204
if
(!
init_
)
Init
();
205
206
if
(atomicnum < 0 || atomicnum >=
static_cast<
int
>
(elements_.size()))
207
return
(1.95);
208
209
rad = elements_[atomicnum]->GetVdwRad();
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::GetCovalentRad
(
int
atomicnum) {
220
if
(!
init_
)
Init
();
221
222
if
(atomicnum < 0 || atomicnum >=
static_cast<
int
>
(elements_.size()))
223
return
(0.0);
224
225
return
(elements_[atomicnum]->
GetCovalentRad
());
226
}
227
228
RealType
ElementsTable::GetMass
(
int
atomicnum) {
229
if
(!
init_
)
Init
();
230
231
if
(atomicnum < 0 || atomicnum >=
static_cast<
int
>
(elements_.size()))
232
return
(0.0);
233
234
return
(elements_[atomicnum]->
GetMass
());
235
}
236
237
int
ElementsTable::GetAtomicNum
(
const
char
* sym) {
238
int
temp;
239
return
GetAtomicNum
(sym, temp);
240
}
241
242
int
ElementsTable::GetAtomicNum
(
const
char
* identifier,
int
& iso) {
243
if
(!
init_
)
Init
();
244
245
// Compare to symbol
246
std::vector<Element*>::iterator i;
247
for
(i = elements_.begin(); i != elements_.end(); ++i)
248
if
(!strncasecmp(identifier, (*i)->GetSymbol(), 3))
249
return
((*i)->GetAtomicNum());
250
251
// Compare to IUPAC name (an abbreviated name will also work if 5
252
// letters or more)
253
int
numCharsToTest = std::max<int>(strlen(identifier), 5);
254
for
(i = elements_.begin(); i != elements_.end(); ++i)
255
if
(strncasecmp(identifier, (*i)->GetName().c_str(), numCharsToTest) == 0)
256
return
((*i)->GetAtomicNum());
257
258
if
(strcasecmp(identifier,
"D"
) == 0 ||
259
(strcasecmp(identifier,
"Deuterium"
) == 0)) {
260
iso = 2;
261
return
(1);
262
}
else
if
(strcasecmp(identifier,
"T"
) == 0 ||
263
(strcasecmp(identifier,
"Tritium"
) == 0)) {
264
iso = 3;
265
return
(1);
266
}
else
if
(strcasecmp(identifier,
"Hl"
) == 0) {
267
// ligand hydrogen -- found in some CIF PR#3048959.
268
snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
269
"ElementsTable warning.\n"
270
"\tCannot understand the element label %s\n"
271
"\tGuessing it's hydrogen\n"
,
272
identifier);
273
painCave.isFatal = 0;
274
painCave.severity = OPENMD_WARNING;
275
simError();
276
return
(1);
277
}
else
278
iso = 0;
279
280
if
(identifier[0] !=
'*'
) {
281
// Quiet down the error messages for now:
282
// snprintf( painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
283
// "ElementsTable warning.\n"
284
// "\tCannot understand the element label %s\n", identifier);
285
// painCave.isFatal = 0;
286
// painCave.severity = OPENMD_WARNING;
287
// simError();
288
}
289
return
(0);
290
}
291
292
int
ElementsTable::GetAtomicNum
(std::string name,
int
& iso) {
293
return
GetAtomicNum
(name.c_str(), iso);
294
}
295
296
void
ElementsTable::Init
() {
297
if
(
init_
)
return
;
298
init_
=
true
;
299
300
char
* tempPath;
301
char
charBuffer[BUFF_SIZE];
302
tempPath = getenv(
"FORCE_PARAM_PATH"
);
303
304
if
(tempPath == NULL) {
305
// convert a macro from compiler to a string in c++
306
STR_DEFINE(
dir_
, FRC_PATH);
307
}
else
{
308
dir_
= tempPath;
309
}
310
311
std::string
filename_
(
"element.txt"
);
312
313
ifstrstream
* efStream =
new
ifstrstream
();
314
315
// Try to open the elements file in current directory first
316
efStream->
open
(
filename_
.c_str());
317
318
if
(!efStream->
is_open
()) {
319
// If current directory does not contain the elements file,
320
// try to open it in ffPath_:
321
filename_
=
dir_
+ FILE_SEP_CHAR +
filename_
;
322
efStream->
open
(
filename_
.c_str());
323
324
if
(!efStream->
is_open
()) {
325
snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
326
"Error opening the ElementsTable file:\n"
327
"\t%s\n"
328
"\tHave you tried setting the FORCE_PARAM_PATH environment "
329
"variable?\n"
,
330
filename_
.c_str());
331
painCave.severity = OPENMD_ERROR;
332
painCave.isFatal = 1;
333
delete
efStream;
334
simError();
335
}
336
}
337
338
if
(efStream->
is_open
()) {
339
while
(efStream->getline(charBuffer, BUFF_SIZE))
340
ParseLine
(charBuffer);
341
}
342
343
if
(efStream->
is_open
()) efStream->
close
();
344
delete
efStream;
345
346
if
(GetSize() == 0) {
347
snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
348
"ElementsTable error.\n"
349
"\tCannot initialize database %s \n"
,
350
filename_
.c_str());
351
painCave.isFatal = 0;
352
simError();
353
}
354
}
355
}
// namespace OpenMD
ElementsTable.hpp
This basic Periodic Table class was originally taken from the data.h file in OpenBabel.
OpenMD::Element
Definition
Element.hpp:40
OpenMD::ElementsTable
Periodic Table of the Elements Using element data is a place holder when we lack information about a ...
Definition
ElementsTable.hpp:72
OpenMD::ElementsTable::dir_
std::string dir_
data directory for file if _envvar fails
Definition
ElementsTable.hpp:196
OpenMD::ElementsTable::init_
bool init_
whether the data been read already
Definition
ElementsTable.hpp:194
OpenMD::ElementsTable::CorrectedVdwRad
RealType CorrectedVdwRad(int atomicnum, int hyb=3)
Definition
ElementsTable.cpp:202
OpenMD::ElementsTable::CorrectedBondRad
RealType CorrectedBondRad(int atomicnum, int hyb=3)
Definition
ElementsTable.cpp:185
OpenMD::ElementsTable::GetCovalentRad
RealType GetCovalentRad(int atomicnum)
Definition
ElementsTable.cpp:219
OpenMD::ElementsTable::~ElementsTable
~ElementsTable()
Destructor.
Definition
ElementsTable.cpp:60
OpenMD::ElementsTable::GetSymbol
const char * GetSymbol(int atomicnum)
Definition
ElementsTable.cpp:93
OpenMD::ElementsTable::GetNumberOfElements
unsigned int GetNumberOfElements()
Definition
ElementsTable.cpp:87
OpenMD::ElementsTable::GetAllredRochowElectroNeg
RealType GetAllredRochowElectroNeg(int atomicnum)
Definition
ElementsTable.cpp:120
OpenMD::ElementsTable::ElementsTable
ElementsTable()
Constructor.
Definition
ElementsTable.cpp:58
OpenMD::ElementsTable::GetRGB
std::vector< RealType > GetRGB(int atomicnum)
Definition
ElementsTable.cpp:147
OpenMD::ElementsTable::filename_
std::string filename_
file to search for
Definition
ElementsTable.hpp:195
OpenMD::ElementsTable::GetMaxBonds
int GetMaxBonds(int atomicnum)
Definition
ElementsTable.cpp:102
OpenMD::ElementsTable::GetElectronAffinity
RealType GetElectronAffinity(int atomicnum)
Definition
ElementsTable.cpp:138
OpenMD::ElementsTable::GetIonization
RealType GetIonization(int atomicnum)
Definition
ElementsTable.cpp:129
OpenMD::ElementsTable::GetMass
RealType GetMass(int atomicnum)
Definition
ElementsTable.cpp:228
OpenMD::ElementsTable::GetName
std::string GetName(int atomicnum)
Definition
ElementsTable.cpp:167
OpenMD::ElementsTable::Init
void Init()
Read in the data file.
Definition
ElementsTable.cpp:296
OpenMD::ElementsTable::ParseLine
void ParseLine(const char *line)
Specified by particular table classes (parses an individual data line).
Definition
ElementsTable.cpp:66
OpenMD::ElementsTable::GetVdwRad
RealType GetVdwRad(int atomicnum)
Definition
ElementsTable.cpp:176
OpenMD::ElementsTable::GetElectroNeg
RealType GetElectroNeg(int atomicnum)
Definition
ElementsTable.cpp:111
OpenMD::ElementsTable::GetAtomicNum
int GetAtomicNum(const char *str)
Definition
ElementsTable.cpp:237
OpenMD::ifstrstream
ifstrstream class provides a stream interface to read data from files.
Definition
ifstrstream.hpp:86
OpenMD::ifstrstream::open
void open(const char *filename, std::ios_base::openmode mode=std::ios_base::in, bool checkFilename=false)
Opens a file and associates a buffer with the specified file to perform the i/o operations (single mo...
Definition
ifstrstream.cpp:130
OpenMD::ifstrstream::is_open
bool is_open()
Tests if the stream is currently associated with a valid buffer.
Definition
ifstrstream.cpp:148
OpenMD::ifstrstream::close
void close()
In single mode, closes a file.
Definition
ifstrstream.cpp:162
ifstrstream.hpp
OpenMD
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
Definition
ActionCorrFunc.cpp:63
utils
ElementsTable.cpp
Generated on
for OpenMD by
1.17.0