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

# User Rev Content
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 gezelter 1879 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
39 gezelter 1782 * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010).
40 chuckv 748 *
41     */
42    
43     #ifndef UTILS_PARAMETERMANAGER_HPP
44     #define UTILS_PARAMETERMANAGER_HPP
45    
46     #include <iostream>
47 jmarr 1401 #include <cstdio>
48 chuckv 748
49     #include <stdlib.h>
50     #include <vector>
51     #include <string>
52     #include <map>
53 tim 963 #include "config.h"
54 chuckv 748
55    
56 gezelter 1291 #include "utils/simError.h"
57     #include "utils/StringTokenizer.hpp"
58 chuckv 748 #include "utils/CaseConversion.hpp"
59    
60 gezelter 1313
61 chuckv 748 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 gezelter 1390 OpenMD::toLower(v);
82 chuckv 748 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 gezelter 1313 //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 tim 963 //RealType
118 chuckv 748 template<>
119 tim 963 struct ParameterTraits<RealType>{
120     typedef RealType RepType;
121 chuckv 748 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 tim 963 static bool convert(int v, RepType& r) {r = static_cast<RealType>(v); return true;}
125 gezelter 1313 static bool convert(unsigned long int v, RepType& r) {r = static_cast<RealType>(v); return true;}
126 tim 963 static std::string getParamType() { return "RealType";}
127 chuckv 748 };
128    
129 gezelter 1291 //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 gezelter 1390 OpenMD::StringTokenizer tokenizer(v," ;,\t\n\r");
138 gezelter 1291 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 gezelter 1782 "Incorrect number of tokens to make a pair!\n");
147 gezelter 1390 painCave.severity = OPENMD_ERROR;
148 gezelter 1291 painCave.isFatal = 1;
149     simError();
150     }
151     return false;
152     }
153     static std::string getParamType() { return "std::pair<int, int>";}
154     };
155 chuckv 748
156 gezelter 2026
157 gezelter 1782 template<>
158 gezelter 2026 struct ParameterTraits<std::vector<RealType> >{
159     typedef std::vector<RealType> RepType;
160 gezelter 1782 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 gezelter 2026 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 gezelter 1782 }
172 gezelter 2026 return true;
173 gezelter 1782 }
174 gezelter 2026 static std::string getParamType() {return "std::vector<RealType>"; }
175 gezelter 1782 };
176 gezelter 1291
177 gezelter 1782
178 chuckv 748 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 gezelter 1313 virtual bool setData(unsigned long int) = 0;
192 tim 963 virtual bool setData(RealType) = 0;
193 gezelter 1291 virtual bool setData(std::pair<int, int>) = 0;
194 gezelter 2026 virtual bool setData(std::vector<RealType>) = 0;
195 chuckv 748 virtual std::string getParamType() = 0;
196     protected:
197 gezelter 1313 std::string keyword_;
198 chuckv 748 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 gezelter 1782 void setDefaultValue(const ParamType& value) {data_ = value; defaultValue_ = true; empty_ = false;}
208 chuckv 748 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 gezelter 1313 virtual bool setData(unsigned long int lival) {
217     return internalSetData<unsigned long int>(lival);
218     }
219 tim 963 virtual bool setData(RealType dval) {
220     return internalSetData<RealType>(dval);
221 chuckv 748 }
222 gezelter 1291 virtual bool setData(std::pair<int, int> pval) {
223     return internalSetData<std::pair<int, int> >(pval);
224     }
225 gezelter 2026 virtual bool setData(std::vector<RealType> pval) {
226     return internalSetData<std::vector<RealType> >(pval);
227 gezelter 1782 }
228 chuckv 748
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 gezelter 1782 #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 chuckv 748
261    
262 gezelter 1782
263 chuckv 748 #define DefineParameter(NAME,KEYWORD) \
264     NAME.setKeyword(KEYWORD); \
265 tim 817 parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME)));
266 chuckv 748
267     #define DefineOptionalParameter(NAME,KEYWORD) \
268     NAME.setKeyword(KEYWORD); NAME.setOptional(true); \
269 tim 817 parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME)));
270 chuckv 748
271     #define DefineOptionalParameterWithDefaultValue(NAME,KEYWORD, DEFAULTVALUE) \
272     NAME.setKeyword(KEYWORD); NAME.setOptional(true); NAME.setDefaultValue(DEFAULTVALUE); \
273 tim 817 parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME)));
274 chuckv 748
275     #define CheckParameter(NAME, CONSTRAINT) \
276 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();} }
277 chuckv 748
278    
279    
280     #endif

Properties

Name Value
svn:keywords Author Id Revision Date