ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/utils/ElementsTable.cpp
Revision: 3319
Committed: Wed Jan 23 03:45:33 2008 UTC (16 years, 5 months ago) by gezelter
File size: 8317 byte(s)
Log Message:
Removed older version of openbabel from our code.  We now have a
configure check to see if openbabel is installed and then we link to
the stuff we need.  Conversion to OOPSE's md format is handled by only
one application (atom2md), so most of the work went on there.
ElementsTable still needs some work to function in parallel.

File Contents

# User Rev Content
1 gezelter 3319 /**********************************************************************
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 OOPSE 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     #include <string>
34     #include <fstream>
35     #include "utils/ElementsTable.hpp"
36     #include "utils/simError.h"
37    
38     #if !HAVE_STRNCASECMP
39     extern "C" int strncasecmp(const char *s1, const char *s2, size_t n);
40     #endif
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 oopse {
53    
54     ElementsTable etab;
55    
56     ElementsTable::ElementsTable() {
57     init_ = false;
58     STR_DEFINE(dir_, 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[5];
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    
78     if (line[0] != '#') {
79     sscanf(line,"%d %5s %lf %*f %lf %d %lf %lf %lf %lf %lf %lf %lf %255s",
80     &num,
81     symbol,
82     &Rcov,
83     &Rvdw,
84     &maxbonds,
85     &mass,
86     &elNeg,
87     &ionize,
88     &elAffin,
89     &red,
90     &green,
91     &blue,
92     name);
93    
94     Element *ele = new Element(num, symbol, Rcov, Rvdw, maxbonds, mass,
95     elNeg, ionize, elAffin, red, green, blue,
96     name);
97     elements_.push_back(ele);
98     }
99     }
100    
101     unsigned int ElementsTable::GetNumberOfElements() {
102     if (!init_)
103     Init();
104    
105     return elements_.size();
106     }
107    
108     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     std::ifstream 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     if (!subdir_.empty()) {
294     subbuffer = buffer;
295     subbuffer += subdir_;
296     subbuffer += FILE_SEP_CHAR;
297     }
298    
299     buffer += filename_;
300     subbuffer += filename_;
301    
302     ifs1.open(subbuffer.c_str());
303     ifsP= &ifs1;
304     if (!(*ifsP)) {
305     ifs2.open(buffer.c_str());
306     ifsP = &ifs2;
307     }
308     } else {
309     sprintf( painCave.errMsg,
310     "ElementsTable error.\n"
311     "\tunable to open datafile %s \n", filename_.c_str());
312     painCave.isFatal = 0;
313     simError();
314     }
315    
316     char charBuffer[BUFF_SIZE];
317     if ((*ifsP)) {
318     while(ifsP->getline(charBuffer,BUFF_SIZE))
319     ParseLine(charBuffer);
320    
321     if (ifs1)
322     ifs1.close();
323     if (ifs2)
324     ifs2.close();
325     if (ifs3)
326     ifs3.close();
327     if (ifs4)
328     ifs4.close();
329    
330     if (GetSize() == 0) {
331     sprintf( painCave.errMsg,
332     "ElementsTable error.\n"
333     "\tCannot initialize database %s \n", filename_.c_str());
334     painCave.isFatal = 0;
335     simError();
336     }
337    
338     }
339    
340     }
341     }