ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/utils/ParameterManager.hpp
Revision: 2026
Committed: Wed Oct 22 12:23:59 2014 UTC (10 years, 6 months ago) by gezelter
File size: 11015 byte(s)
Log Message:
Starting to add support for UniformGradient. 
Changed Vector3d input type to a more general std::vector<RealType> input.  This change alters RNEMD and UniformField inputs.

File Contents

# Content
1 /*
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 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
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.
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 * 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 *
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>
51 #include <string>
52 #include <map>
53 #include "config.h"
54
55
56 #include "utils/simError.h"
57 #include "utils/StringTokenizer.hpp"
58 #include "utils/CaseConversion.hpp"
59
60
61 template<typename T>
62 struct ParameterTraits;
63
64 //string
65 template<>
66 struct ParameterTraits<std::string>{
67 typedef std::string RepType; // Representation type of the value
68
69 template<typename T> static bool convert(T v, RepType& r){return false;} // !NB everything is ok
70 template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;}
71 static bool convert(RepType v, RepType& r) { r = v; return true;}
72 static std::string getParamType() { return "string";}
73 };
74 //bool
75 template<>
76 struct ParameterTraits<bool>{
77 typedef bool RepType;
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 OpenMD::toLower(v);
82 bool result = false;
83 if (v == "true") {
84 r = true;
85 result = true;
86 } else if (v == "false") {
87 r = false;
88 result = true;
89 }
90
91 return result;
92 }
93 static std::string getParamType() { return "bool";}
94 };
95
96 //int
97 template<>
98 struct ParameterTraits<int>{
99 typedef int RepType;
100 template<typename T> static bool convert(T, RepType&){return false;}
101 template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;}
102 static bool convert(RepType v, RepType& r) { r=v; return 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>{
120 typedef RealType RepType;
121 template<typename T> static bool convert(T, RepType&){return false;}
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) {}
181 virtual ~ParameterBase() {}
182 bool isOptional() {return optional_;}
183 void setOptional(bool optional) {optional_ = optional;}
184 bool hasDefaultValue() {return defaultValue_;}
185 virtual bool isValid() { return true;}
186 const std::string& getKeyword() {return keyword_;}
187 void setKeyword(const std::string& keyword) { keyword_ = keyword;}
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_;
198 bool optional_;
199 bool defaultValue_;
200 bool empty_;
201 };
202
203 template<class ParamType>
204 class Parameter : public ParameterBase{
205 public:
206 typedef ParameterTraits<ParamType> ValueType;
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) {
211 return internalSetData<std::string>(sval);
212 }
213 virtual bool setData(int ival) {
214 return internalSetData<int>(ival);
215 }
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:
231 template<class T> bool internalSetData(T data) {
232 ParamType tmp;
233 bool result = ValueType::convert(data, tmp);
234 if (result) {
235 empty_ = false;
236 data_ = tmp;
237 }
238 return result;
239 }
240
241 private:
242 ParamType data_;
243
244 };
245
246 #define DeclareParameter(NAME, TYPE) \
247 private: \
248 Parameter<TYPE> NAME; \
249 public: \
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)));
266
267 #define DefineOptionalParameter(NAME,KEYWORD) \
268 NAME.setKeyword(KEYWORD); NAME.setOptional(true); \
269 parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME)));
270
271 #define DefineOptionalParameterWithDefaultValue(NAME,KEYWORD, DEFAULTVALUE) \
272 NAME.setKeyword(KEYWORD); NAME.setOptional(true); NAME.setDefaultValue(DEFAULTVALUE); \
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 = OPENMD_ERROR; simError();} }
277
278
279
280 #endif

Properties

Name Value
svn:keywords Author Id Revision Date