6 |
|
* redistribute this software in source and binary code form, provided |
7 |
|
* that the following conditions are met: |
8 |
|
* |
9 |
< |
* 1. Acknowledgement of the program authors must be made in any |
10 |
< |
* publication of scientific results based in part on use of the |
11 |
< |
* program. An acceptable form of acknowledgement is citation of |
12 |
< |
* the article in which the program was described (Matthew |
13 |
< |
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
14 |
< |
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
15 |
< |
* Parallel Simulation Engine for Molecular Dynamics," |
16 |
< |
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
17 |
< |
* |
18 |
< |
* 2. Redistributions of source code must retain the above copyright |
9 |
> |
* 1. Redistributions of source code must retain the above copyright |
10 |
|
* notice, this list of conditions and the following disclaimer. |
11 |
|
* |
12 |
< |
* 3. Redistributions in binary form must reproduce the above copyright |
12 |
> |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
|
* notice, this list of conditions and the following disclaimer in the |
14 |
|
* documentation and/or other materials provided with the |
15 |
|
* distribution. |
29 |
|
* University of Notre Dame has been advised of the possibility of |
30 |
|
* such damages. |
31 |
|
* |
32 |
+ |
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
+ |
* research, please cite the appropriate papers when you publish your |
34 |
+ |
* work. Good starting points are: |
35 |
+ |
* |
36 |
+ |
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
+ |
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
+ |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). |
39 |
+ |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
|
* |
42 |
– |
* ParameterManager.hpp |
43 |
– |
* OOPSE-2.0 |
44 |
– |
* |
45 |
– |
* Created by Charles F. Vardeman II on 11/16/05. |
46 |
– |
* @author Charles F. Vardeman II |
47 |
– |
* @version $Id: ParameterManager.hpp,v 1.3 2006-05-17 21:51:42 tim Exp $ |
48 |
– |
* |
41 |
|
*/ |
42 |
|
|
43 |
|
#ifndef UTILS_PARAMETERMANAGER_HPP |
44 |
|
#define UTILS_PARAMETERMANAGER_HPP |
45 |
|
|
46 |
|
#include <iostream> |
47 |
+ |
#include <cstdio> |
48 |
|
|
49 |
|
#include <stdlib.h> |
50 |
|
#include <vector> |
53 |
|
#include "config.h" |
54 |
|
|
55 |
|
|
56 |
+ |
#include "utils/simError.h" |
57 |
+ |
#include "utils/StringTokenizer.hpp" |
58 |
|
#include "utils/CaseConversion.hpp" |
59 |
|
|
60 |
|
|
66 |
– |
|
61 |
|
template<typename T> |
62 |
|
struct ParameterTraits; |
63 |
|
|
78 |
|
template<typename T> static bool convert(T, RepType&){return false;} |
79 |
|
template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;} |
80 |
|
static bool convert(std::string v, RepType& r) { |
81 |
< |
oopse::toLower(v); |
81 |
> |
OpenMD::toLower(v); |
82 |
|
bool result = false; |
83 |
|
if (v == "true") { |
84 |
|
r = true; |
103 |
|
static std::string getParamType() { return "int";} |
104 |
|
}; |
105 |
|
|
106 |
+ |
//int |
107 |
+ |
template<> |
108 |
+ |
struct ParameterTraits<unsigned long int>{ |
109 |
+ |
typedef unsigned long int RepType; |
110 |
+ |
template<typename T> static bool convert(T, RepType&){return false;} |
111 |
+ |
template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;} |
112 |
+ |
static bool convert(RepType v, RepType& r) { r=v; return true;} |
113 |
+ |
static bool convert(int v, RepType& r) {r = static_cast<unsigned long int>(v); return true;} |
114 |
+ |
static std::string getParamType() { return "unsigned long int";} |
115 |
+ |
}; |
116 |
+ |
|
117 |
|
//RealType |
118 |
|
template<> |
119 |
|
struct ParameterTraits<RealType>{ |
122 |
|
template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;} |
123 |
|
static bool convert(RepType v, RepType& r) {r=v; return true;} |
124 |
|
static bool convert(int v, RepType& r) {r = static_cast<RealType>(v); return true;} |
125 |
+ |
static bool convert(unsigned long int v, RepType& r) {r = static_cast<RealType>(v); return true;} |
126 |
|
static std::string getParamType() { return "RealType";} |
127 |
|
}; |
128 |
|
|
129 |
+ |
//Pair of ints |
130 |
+ |
template<> |
131 |
+ |
struct ParameterTraits<std::pair<int, int> >{ |
132 |
+ |
typedef std::pair<int, int> RepType; |
133 |
+ |
template<typename T> static bool convert(T, RepType&){return false;} |
134 |
+ |
template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;} |
135 |
+ |
static bool convert(RepType v, RepType& r) {r=v; return true;} |
136 |
+ |
static bool convert(std::string v, RepType& r) { |
137 |
+ |
OpenMD::StringTokenizer tokenizer(v," ;,\t\n\r"); |
138 |
+ |
if (tokenizer.countTokens() == 2) { |
139 |
+ |
int atom1 = tokenizer.nextTokenAsInt(); |
140 |
+ |
int atom2 = tokenizer.nextTokenAsInt(); |
141 |
+ |
r = std::make_pair(atom1, atom2); |
142 |
+ |
return true; |
143 |
+ |
} else { |
144 |
+ |
sprintf(painCave.errMsg, |
145 |
+ |
"ParameterManager Error: " |
146 |
+ |
"Incorrect number of tokens to make a pair!\n"); |
147 |
+ |
painCave.severity = OPENMD_ERROR; |
148 |
+ |
painCave.isFatal = 1; |
149 |
+ |
simError(); |
150 |
+ |
} |
151 |
+ |
return false; |
152 |
+ |
} |
153 |
+ |
static std::string getParamType() { return "std::pair<int, int>";} |
154 |
+ |
}; |
155 |
|
|
156 |
+ |
|
157 |
+ |
template<> |
158 |
+ |
struct ParameterTraits<std::vector<RealType> >{ |
159 |
+ |
typedef std::vector<RealType> RepType; |
160 |
+ |
template<typename T> static bool convert(T, RepType&){return false;} |
161 |
+ |
template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;} |
162 |
+ |
static bool convert(RepType v, RepType& r) {r=v; return true;} |
163 |
+ |
static bool convert(std::string v, RepType& r) { |
164 |
+ |
std::cerr << "calling tokenizer\n"; |
165 |
+ |
OpenMD::StringTokenizer tokenizer(v," ();,\t\n\r"); |
166 |
+ |
unsigned int size = tokenizer.countTokens(); |
167 |
+ |
r = std::vector<RealType>( size, 0.0 ); |
168 |
+ |
for (unsigned int i = 0; i < size; i++) { |
169 |
+ |
RealType v = tokenizer.nextTokenAsDouble(); |
170 |
+ |
r[i] = v; |
171 |
+ |
} |
172 |
+ |
return true; |
173 |
+ |
} |
174 |
+ |
static std::string getParamType() {return "std::vector<RealType>"; } |
175 |
+ |
}; |
176 |
+ |
|
177 |
+ |
|
178 |
|
class ParameterBase { |
179 |
|
public: |
180 |
|
ParameterBase() : keyword_(), optional_(false), defaultValue_(false), empty_(true) {} |
188 |
|
bool empty() {return empty_;} |
189 |
|
virtual bool setData(std::string) = 0; |
190 |
|
virtual bool setData(int) = 0; |
191 |
+ |
virtual bool setData(unsigned long int) = 0; |
192 |
|
virtual bool setData(RealType) = 0; |
193 |
+ |
virtual bool setData(std::pair<int, int>) = 0; |
194 |
+ |
virtual bool setData(std::vector<RealType>) = 0; |
195 |
|
virtual std::string getParamType() = 0; |
196 |
|
protected: |
197 |
< |
std::string keyword_; |
197 |
> |
std::string keyword_; |
198 |
|
bool optional_; |
199 |
|
bool defaultValue_; |
200 |
|
bool empty_; |
204 |
|
class Parameter : public ParameterBase{ |
205 |
|
public: |
206 |
|
typedef ParameterTraits<ParamType> ValueType; |
207 |
< |
void setDefaultValue(const ParamType& value) {data_ = value; defaultValue_ = true;} |
207 |
> |
void setDefaultValue(const ParamType& value) {data_ = value; defaultValue_ = true; empty_ = false;} |
208 |
|
ParamType getData() { return data_;} |
209 |
|
|
210 |
|
virtual bool setData(std::string sval) { |
213 |
|
virtual bool setData(int ival) { |
214 |
|
return internalSetData<int>(ival); |
215 |
|
} |
216 |
< |
|
216 |
> |
virtual bool setData(unsigned long int lival) { |
217 |
> |
return internalSetData<unsigned long int>(lival); |
218 |
> |
} |
219 |
|
virtual bool setData(RealType dval) { |
220 |
|
return internalSetData<RealType>(dval); |
221 |
|
} |
222 |
+ |
virtual bool setData(std::pair<int, int> pval) { |
223 |
+ |
return internalSetData<std::pair<int, int> >(pval); |
224 |
+ |
} |
225 |
+ |
virtual bool setData(std::vector<RealType> pval) { |
226 |
+ |
return internalSetData<std::vector<RealType> >(pval); |
227 |
+ |
} |
228 |
|
|
229 |
|
virtual std::string getParamType() { return ParameterTraits<ParamType>::getParamType();} |
230 |
|
private: |
250 |
|
bool have##NAME() { return !NAME.empty();} \ |
251 |
|
TYPE get##NAME() { return NAME.getData();} |
252 |
|
|
253 |
+ |
#define DeclareAlterableParameter(NAME, TYPE) \ |
254 |
+ |
private: \ |
255 |
+ |
Parameter<TYPE> NAME; \ |
256 |
+ |
public: \ |
257 |
+ |
bool have##NAME() { return !NAME.empty();} \ |
258 |
+ |
TYPE get##NAME() { return NAME.getData();} \ |
259 |
+ |
bool set##NAME(TYPE s) { return NAME.setData(s);} \ |
260 |
|
|
261 |
|
|
262 |
+ |
|
263 |
|
#define DefineParameter(NAME,KEYWORD) \ |
264 |
|
NAME.setKeyword(KEYWORD); \ |
265 |
|
parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME))); |
273 |
|
parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME))); |
274 |
|
|
275 |
|
#define CheckParameter(NAME, CONSTRAINT) \ |
276 |
< |
if (!NAME.empty()) { if (!(CONSTRAINT)(NAME.getData())) { sprintf(painCave.errMsg,"Error in checking %s : should be %s\n",NAME.getKeyword().c_str(),(CONSTRAINT).getConstraintDescription().c_str()); painCave.isFatal = 1; painCave.severity = OOPSE_ERROR; simError();} } |
276 |
> |
if (!NAME.empty()) { if (!(CONSTRAINT)(NAME.getData())) { sprintf(painCave.errMsg,"Error in checking %s : should be %s\n",NAME.getKeyword().c_str(),(CONSTRAINT).getConstraintDescription().c_str()); painCave.isFatal = 1; painCave.severity = OPENMD_ERROR; simError();} } |
277 |
|
|
278 |
|
|
279 |
|
|