1 |
chuckv |
748 |
/* |
2 |
|
|
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
|
|
* |
4 |
|
|
* The University of Notre Dame grants you ("Licensee") a |
5 |
|
|
* non-exclusive, royalty free, license to use, modify and |
6 |
|
|
* redistribute this software in source and binary code form, provided |
7 |
|
|
* that the following conditions are met: |
8 |
|
|
* |
9 |
gezelter |
1390 |
* 1. Redistributions of source code must retain the above copyright |
10 |
chuckv |
748 |
* notice, this list of conditions and the following disclaimer. |
11 |
|
|
* |
12 |
gezelter |
1390 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
chuckv |
748 |
* notice, this list of conditions and the following disclaimer in the |
14 |
|
|
* documentation and/or other materials provided with the |
15 |
|
|
* distribution. |
16 |
|
|
* |
17 |
|
|
* This software is provided "AS IS," without a warranty of any |
18 |
|
|
* kind. All express or implied conditions, representations and |
19 |
|
|
* warranties, including any implied warranty of merchantability, |
20 |
|
|
* fitness for a particular purpose or non-infringement, are hereby |
21 |
|
|
* excluded. The University of Notre Dame and its licensors shall not |
22 |
|
|
* be liable for any damages suffered by licensee as a result of |
23 |
|
|
* using, modifying or distributing the software or its |
24 |
|
|
* derivatives. In no event will the University of Notre Dame or its |
25 |
|
|
* licensors be liable for any lost revenue, profit or data, or for |
26 |
|
|
* direct, indirect, special, consequential, incidental or punitive |
27 |
|
|
* damages, however caused and regardless of the theory of liability, |
28 |
|
|
* arising out of the use of or inability to use software, even if the |
29 |
|
|
* University of Notre Dame has been advised of the possibility of |
30 |
|
|
* such damages. |
31 |
|
|
* |
32 |
gezelter |
1390 |
* 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, 24107 (2008). |
39 |
|
|
* [4] Vardeman & Gezelter, in progress (2009). |
40 |
chuckv |
748 |
* |
41 |
|
|
* ParameterManager.hpp |
42 |
|
|
* |
43 |
|
|
* Created by Charles F. Vardeman II on 11/16/05. |
44 |
|
|
* @author Charles F. Vardeman II |
45 |
gezelter |
1390 |
* @version $Id: ParameterManager.hpp,v 1.6 2009-11-25 20:02:04 gezelter Exp $ |
46 |
chuckv |
748 |
* |
47 |
|
|
*/ |
48 |
|
|
|
49 |
|
|
#ifndef UTILS_PARAMETERMANAGER_HPP |
50 |
|
|
#define UTILS_PARAMETERMANAGER_HPP |
51 |
|
|
|
52 |
|
|
#include <iostream> |
53 |
|
|
|
54 |
|
|
#include <stdlib.h> |
55 |
|
|
#include <vector> |
56 |
|
|
#include <string> |
57 |
|
|
#include <map> |
58 |
tim |
963 |
#include "config.h" |
59 |
chuckv |
748 |
|
60 |
|
|
|
61 |
gezelter |
1291 |
#include "utils/simError.h" |
62 |
|
|
#include "utils/StringTokenizer.hpp" |
63 |
chuckv |
748 |
#include "utils/CaseConversion.hpp" |
64 |
|
|
|
65 |
gezelter |
1313 |
|
66 |
chuckv |
748 |
template<typename T> |
67 |
|
|
struct ParameterTraits; |
68 |
|
|
|
69 |
|
|
//string |
70 |
|
|
template<> |
71 |
|
|
struct ParameterTraits<std::string>{ |
72 |
|
|
typedef std::string RepType; // Representation type of the value |
73 |
|
|
|
74 |
|
|
template<typename T> static bool convert(T v, RepType& r){return false;} // !NB everything is ok |
75 |
|
|
template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;} |
76 |
|
|
static bool convert(RepType v, RepType& r) { r = v; return true;} |
77 |
|
|
static std::string getParamType() { return "string";} |
78 |
|
|
}; |
79 |
|
|
//bool |
80 |
|
|
template<> |
81 |
|
|
struct ParameterTraits<bool>{ |
82 |
|
|
typedef bool RepType; |
83 |
|
|
template<typename T> static bool convert(T, RepType&){return false;} |
84 |
|
|
template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;} |
85 |
|
|
static bool convert(std::string v, RepType& r) { |
86 |
gezelter |
1390 |
OpenMD::toLower(v); |
87 |
chuckv |
748 |
bool result = false; |
88 |
|
|
if (v == "true") { |
89 |
|
|
r = true; |
90 |
|
|
result = true; |
91 |
|
|
} else if (v == "false") { |
92 |
|
|
r = false; |
93 |
|
|
result = true; |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
return result; |
97 |
|
|
} |
98 |
|
|
static std::string getParamType() { return "bool";} |
99 |
|
|
}; |
100 |
|
|
|
101 |
|
|
//int |
102 |
|
|
template<> |
103 |
|
|
struct ParameterTraits<int>{ |
104 |
|
|
typedef int RepType; |
105 |
|
|
template<typename T> static bool convert(T, RepType&){return false;} |
106 |
|
|
template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;} |
107 |
|
|
static bool convert(RepType v, RepType& r) { r=v; return true;} |
108 |
|
|
static std::string getParamType() { return "int";} |
109 |
|
|
}; |
110 |
|
|
|
111 |
gezelter |
1313 |
//int |
112 |
|
|
template<> |
113 |
|
|
struct ParameterTraits<unsigned long int>{ |
114 |
|
|
typedef unsigned long int RepType; |
115 |
|
|
template<typename T> static bool convert(T, RepType&){return false;} |
116 |
|
|
template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;} |
117 |
|
|
static bool convert(RepType v, RepType& r) { r=v; return true;} |
118 |
|
|
static bool convert(int v, RepType& r) {r = static_cast<unsigned long int>(v); return true;} |
119 |
|
|
static std::string getParamType() { return "unsigned long int";} |
120 |
|
|
}; |
121 |
|
|
|
122 |
tim |
963 |
//RealType |
123 |
chuckv |
748 |
template<> |
124 |
tim |
963 |
struct ParameterTraits<RealType>{ |
125 |
|
|
typedef RealType RepType; |
126 |
chuckv |
748 |
template<typename T> static bool convert(T, RepType&){return false;} |
127 |
|
|
template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;} |
128 |
|
|
static bool convert(RepType v, RepType& r) {r=v; return true;} |
129 |
tim |
963 |
static bool convert(int v, RepType& r) {r = static_cast<RealType>(v); return true;} |
130 |
gezelter |
1313 |
static bool convert(unsigned long int v, RepType& r) {r = static_cast<RealType>(v); return true;} |
131 |
tim |
963 |
static std::string getParamType() { return "RealType";} |
132 |
chuckv |
748 |
}; |
133 |
|
|
|
134 |
gezelter |
1291 |
//Pair of ints |
135 |
|
|
template<> |
136 |
|
|
struct ParameterTraits<std::pair<int, int> >{ |
137 |
|
|
typedef std::pair<int, int> RepType; |
138 |
|
|
template<typename T> static bool convert(T, RepType&){return false;} |
139 |
|
|
template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;} |
140 |
|
|
static bool convert(RepType v, RepType& r) {r=v; return true;} |
141 |
|
|
static bool convert(std::string v, RepType& r) { |
142 |
gezelter |
1390 |
OpenMD::StringTokenizer tokenizer(v," ;,\t\n\r"); |
143 |
gezelter |
1291 |
if (tokenizer.countTokens() == 2) { |
144 |
|
|
int atom1 = tokenizer.nextTokenAsInt(); |
145 |
|
|
int atom2 = tokenizer.nextTokenAsInt(); |
146 |
|
|
r = std::make_pair(atom1, atom2); |
147 |
|
|
return true; |
148 |
|
|
} else { |
149 |
|
|
sprintf(painCave.errMsg, |
150 |
|
|
"ParameterManager Error: " |
151 |
|
|
"Not enough tokens to make pair!\n"); |
152 |
gezelter |
1390 |
painCave.severity = OPENMD_ERROR; |
153 |
gezelter |
1291 |
painCave.isFatal = 1; |
154 |
|
|
simError(); |
155 |
|
|
} |
156 |
|
|
return false; |
157 |
|
|
} |
158 |
|
|
static std::string getParamType() { return "std::pair<int, int>";} |
159 |
|
|
}; |
160 |
chuckv |
748 |
|
161 |
gezelter |
1291 |
|
162 |
chuckv |
748 |
class ParameterBase { |
163 |
|
|
public: |
164 |
|
|
ParameterBase() : keyword_(), optional_(false), defaultValue_(false), empty_(true) {} |
165 |
|
|
virtual ~ParameterBase() {} |
166 |
|
|
bool isOptional() {return optional_;} |
167 |
|
|
void setOptional(bool optional) {optional_ = optional;} |
168 |
|
|
bool hasDefaultValue() {return defaultValue_;} |
169 |
|
|
virtual bool isValid() { return true;} |
170 |
|
|
const std::string& getKeyword() {return keyword_;} |
171 |
|
|
void setKeyword(const std::string& keyword) { keyword_ = keyword;} |
172 |
|
|
bool empty() {return empty_;} |
173 |
|
|
virtual bool setData(std::string) = 0; |
174 |
|
|
virtual bool setData(int) = 0; |
175 |
gezelter |
1313 |
virtual bool setData(unsigned long int) = 0; |
176 |
tim |
963 |
virtual bool setData(RealType) = 0; |
177 |
gezelter |
1291 |
virtual bool setData(std::pair<int, int>) = 0; |
178 |
chuckv |
748 |
virtual std::string getParamType() = 0; |
179 |
|
|
protected: |
180 |
gezelter |
1313 |
std::string keyword_; |
181 |
chuckv |
748 |
bool optional_; |
182 |
|
|
bool defaultValue_; |
183 |
|
|
bool empty_; |
184 |
|
|
}; |
185 |
|
|
|
186 |
|
|
template<class ParamType> |
187 |
|
|
class Parameter : public ParameterBase{ |
188 |
|
|
public: |
189 |
|
|
typedef ParameterTraits<ParamType> ValueType; |
190 |
|
|
void setDefaultValue(const ParamType& value) {data_ = value; defaultValue_ = true;} |
191 |
|
|
ParamType getData() { return data_;} |
192 |
|
|
|
193 |
|
|
virtual bool setData(std::string sval) { |
194 |
|
|
return internalSetData<std::string>(sval); |
195 |
|
|
} |
196 |
gezelter |
1313 |
|
197 |
chuckv |
748 |
virtual bool setData(int ival) { |
198 |
|
|
return internalSetData<int>(ival); |
199 |
|
|
} |
200 |
gezelter |
1313 |
|
201 |
|
|
virtual bool setData(unsigned long int lival) { |
202 |
|
|
return internalSetData<unsigned long int>(lival); |
203 |
|
|
} |
204 |
chuckv |
748 |
|
205 |
tim |
963 |
virtual bool setData(RealType dval) { |
206 |
|
|
return internalSetData<RealType>(dval); |
207 |
chuckv |
748 |
} |
208 |
gezelter |
1291 |
|
209 |
|
|
virtual bool setData(std::pair<int, int> pval) { |
210 |
|
|
return internalSetData<std::pair<int, int> >(pval); |
211 |
|
|
} |
212 |
chuckv |
748 |
|
213 |
|
|
virtual std::string getParamType() { return ParameterTraits<ParamType>::getParamType();} |
214 |
|
|
private: |
215 |
|
|
template<class T> bool internalSetData(T data) { |
216 |
|
|
ParamType tmp; |
217 |
|
|
bool result = ValueType::convert(data, tmp); |
218 |
|
|
if (result) { |
219 |
|
|
empty_ = false; |
220 |
|
|
data_ = tmp; |
221 |
|
|
} |
222 |
|
|
return result; |
223 |
|
|
} |
224 |
|
|
|
225 |
|
|
private: |
226 |
|
|
ParamType data_; |
227 |
|
|
|
228 |
|
|
}; |
229 |
|
|
|
230 |
|
|
#define DeclareParameter(NAME, TYPE) \ |
231 |
|
|
private: \ |
232 |
|
|
Parameter<TYPE> NAME; \ |
233 |
|
|
public: \ |
234 |
|
|
bool have##NAME() { return !NAME.empty();} \ |
235 |
|
|
TYPE get##NAME() { return NAME.getData();} |
236 |
|
|
|
237 |
|
|
|
238 |
|
|
|
239 |
|
|
#define DefineParameter(NAME,KEYWORD) \ |
240 |
|
|
NAME.setKeyword(KEYWORD); \ |
241 |
tim |
817 |
parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME))); |
242 |
chuckv |
748 |
|
243 |
|
|
#define DefineOptionalParameter(NAME,KEYWORD) \ |
244 |
|
|
NAME.setKeyword(KEYWORD); NAME.setOptional(true); \ |
245 |
tim |
817 |
parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME))); |
246 |
chuckv |
748 |
|
247 |
|
|
#define DefineOptionalParameterWithDefaultValue(NAME,KEYWORD, DEFAULTVALUE) \ |
248 |
|
|
NAME.setKeyword(KEYWORD); NAME.setOptional(true); NAME.setDefaultValue(DEFAULTVALUE); \ |
249 |
tim |
817 |
parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME))); |
250 |
chuckv |
748 |
|
251 |
|
|
#define CheckParameter(NAME, CONSTRAINT) \ |
252 |
gezelter |
1390 |
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();} } |
253 |
chuckv |
748 |
|
254 |
|
|
|
255 |
|
|
|
256 |
|
|
#endif |