| 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 | gezelter | 1665 | * [4] Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 |  |  | * [4] , Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). * | 
| 41 | chuckv | 748 | *  ParameterManager.hpp | 
| 42 |  |  | * | 
| 43 |  |  | *  Created by Charles F. Vardeman II on 11/16/05. | 
| 44 |  |  | *  @author  Charles F. Vardeman II | 
| 45 | gezelter | 1442 | *  @version $Id$ | 
| 46 | chuckv | 748 | * | 
| 47 |  |  | */ | 
| 48 |  |  |  | 
| 49 |  |  | #ifndef UTILS_PARAMETERMANAGER_HPP | 
| 50 |  |  | #define UTILS_PARAMETERMANAGER_HPP | 
| 51 |  |  |  | 
| 52 |  |  | #include <iostream> | 
| 53 | jmarr | 1401 | #include <cstdio> | 
| 54 | chuckv | 748 |  | 
| 55 |  |  | #include <stdlib.h> | 
| 56 |  |  | #include <vector> | 
| 57 |  |  | #include <string> | 
| 58 |  |  | #include <map> | 
| 59 | tim | 963 | #include "config.h" | 
| 60 | chuckv | 748 |  | 
| 61 |  |  |  | 
| 62 | gezelter | 1291 | #include "utils/simError.h" | 
| 63 |  |  | #include "utils/StringTokenizer.hpp" | 
| 64 | chuckv | 748 | #include "utils/CaseConversion.hpp" | 
| 65 |  |  |  | 
| 66 | gezelter | 1313 |  | 
| 67 | chuckv | 748 | template<typename T> | 
| 68 |  |  | struct ParameterTraits; | 
| 69 |  |  |  | 
| 70 |  |  | //string | 
| 71 |  |  | template<> | 
| 72 |  |  | struct ParameterTraits<std::string>{ | 
| 73 |  |  | typedef std::string RepType;       // Representation type of the value | 
| 74 |  |  |  | 
| 75 |  |  | template<typename T> static bool    convert(T v, RepType& r){return false;} // !NB everything is ok | 
| 76 |  |  | template<typename T> static RepType convert(T v)            {RepType tmp; convert(v,tmp);return tmp;} | 
| 77 |  |  | static bool convert(RepType v, RepType& r) { r = v; return true;} | 
| 78 |  |  | static std::string getParamType() { return "string";} | 
| 79 |  |  | }; | 
| 80 |  |  | //bool | 
| 81 |  |  | template<> | 
| 82 |  |  | struct ParameterTraits<bool>{ | 
| 83 |  |  | typedef bool RepType; | 
| 84 |  |  | template<typename T> static bool    convert(T, RepType&){return false;} | 
| 85 |  |  | template<typename T> static RepType convert(T v)        {RepType tmp; convert(v,tmp);return tmp;} | 
| 86 |  |  | static bool convert(std::string v, RepType& r) { | 
| 87 | gezelter | 1390 | OpenMD::toLower(v); | 
| 88 | chuckv | 748 | bool result = false; | 
| 89 |  |  | if (v == "true") { | 
| 90 |  |  | r = true; | 
| 91 |  |  | result = true; | 
| 92 |  |  | } else if (v == "false") { | 
| 93 |  |  | r = false; | 
| 94 |  |  | result = true; | 
| 95 |  |  | } | 
| 96 |  |  |  | 
| 97 |  |  | return result; | 
| 98 |  |  | } | 
| 99 |  |  | static std::string getParamType() { return "bool";} | 
| 100 |  |  | }; | 
| 101 |  |  |  | 
| 102 |  |  | //int | 
| 103 |  |  | template<> | 
| 104 |  |  | struct ParameterTraits<int>{ | 
| 105 |  |  | typedef int RepType; | 
| 106 |  |  | template<typename T> static bool    convert(T, RepType&){return false;} | 
| 107 |  |  | template<typename T> static RepType convert(T v)        {RepType tmp; convert(v,tmp);return tmp;} | 
| 108 |  |  | static bool convert(RepType v, RepType& r)            { r=v; return true;} | 
| 109 |  |  | static std::string getParamType() { return "int";} | 
| 110 |  |  | }; | 
| 111 |  |  |  | 
| 112 | gezelter | 1313 | //int | 
| 113 |  |  | template<> | 
| 114 |  |  | struct ParameterTraits<unsigned long int>{ | 
| 115 |  |  | typedef unsigned long int RepType; | 
| 116 |  |  | template<typename T> static bool    convert(T, RepType&){return false;} | 
| 117 |  |  | template<typename T> static RepType convert(T v)        {RepType tmp; convert(v,tmp);return tmp;} | 
| 118 |  |  | static bool convert(RepType v, RepType& r)            { r=v; return true;} | 
| 119 |  |  | static bool convert(int v, RepType& r)                {r = static_cast<unsigned long int>(v); return true;} | 
| 120 |  |  | static std::string getParamType() { return "unsigned long int";} | 
| 121 |  |  | }; | 
| 122 |  |  |  | 
| 123 | tim | 963 | //RealType | 
| 124 | chuckv | 748 | template<> | 
| 125 | tim | 963 | struct ParameterTraits<RealType>{ | 
| 126 |  |  | typedef RealType RepType; | 
| 127 | chuckv | 748 | template<typename T> static bool    convert(T, RepType&){return false;} | 
| 128 |  |  | template<typename T> static RepType convert(T v)        {RepType tmp; convert(v,tmp);return tmp;} | 
| 129 |  |  | static bool convert(RepType v, RepType& r)            {r=v; return true;} | 
| 130 | tim | 963 | static bool convert(int v, RepType& r)                {r = static_cast<RealType>(v); return true;} | 
| 131 | gezelter | 1313 | static bool convert(unsigned long int v, RepType& r)  {r = static_cast<RealType>(v); return true;} | 
| 132 | tim | 963 | static std::string getParamType() { return "RealType";} | 
| 133 | chuckv | 748 | }; | 
| 134 |  |  |  | 
| 135 | gezelter | 1291 | //Pair of ints | 
| 136 |  |  | template<> | 
| 137 |  |  | struct ParameterTraits<std::pair<int, int> >{ | 
| 138 |  |  | typedef std::pair<int, int>  RepType; | 
| 139 |  |  | template<typename T> static bool    convert(T, RepType&){return false;} | 
| 140 |  |  | template<typename T> static RepType convert(T v)        {RepType tmp; convert(v,tmp);return tmp;} | 
| 141 |  |  | static bool convert(RepType v, RepType& r)            {r=v; return true;} | 
| 142 |  |  | static bool convert(std::string v, RepType& r) { | 
| 143 | gezelter | 1390 | OpenMD::StringTokenizer tokenizer(v," ;,\t\n\r"); | 
| 144 | gezelter | 1291 | if (tokenizer.countTokens() == 2) { | 
| 145 |  |  | int atom1 = tokenizer.nextTokenAsInt(); | 
| 146 |  |  | int atom2 = tokenizer.nextTokenAsInt(); | 
| 147 |  |  | r = std::make_pair(atom1, atom2); | 
| 148 |  |  | return true; | 
| 149 |  |  | } else { | 
| 150 |  |  | sprintf(painCave.errMsg, | 
| 151 |  |  | "ParameterManager Error: " | 
| 152 |  |  | "Not enough tokens to make pair!\n"); | 
| 153 | gezelter | 1390 | painCave.severity = OPENMD_ERROR; | 
| 154 | gezelter | 1291 | painCave.isFatal = 1; | 
| 155 |  |  | simError(); | 
| 156 |  |  | } | 
| 157 |  |  | return false; | 
| 158 |  |  | } | 
| 159 |  |  | static std::string getParamType() { return "std::pair<int, int>";} | 
| 160 |  |  | }; | 
| 161 | chuckv | 748 |  | 
| 162 | gezelter | 1700 | //OpenMD's internal Vector3d | 
| 163 |  |  | template<> | 
| 164 |  |  | struct ParameterTraits<OpenMD::Vector3d >{ | 
| 165 |  |  | typedef OpenMD::Vector3d  RepType; | 
| 166 |  |  | template<typename T> static bool    convert(T, RepType&){return false;} | 
| 167 |  |  | template<typename T> static RepType convert(T v)        {RepType tmp; convert(v,tmp);return tmp;} | 
| 168 |  |  | static bool convert(RepType v, RepType& r)            {r=v; return true;} | 
| 169 |  |  | static bool convert(std::string v, RepType& r) { | 
| 170 |  |  | OpenMD::StringTokenizer tokenizer(v," ();,\t\n\r"); | 
| 171 |  |  | if (tokenizer.countTokens() == 3) { | 
| 172 |  |  | RealType v1 = tokenizer.nextTokenAsDouble(); | 
| 173 |  |  | RealType v2 = tokenizer.nextTokenAsDouble(); | 
| 174 |  |  | RealType v3 = tokenizer.nextTokenAsDouble(); | 
| 175 |  |  | r = OpenMD::Vector3d(v1, v2, v3); | 
| 176 |  |  | return true; | 
| 177 |  |  | } else { | 
| 178 |  |  | sprintf(painCave.errMsg, | 
| 179 |  |  | "ParameterManager Error: " | 
| 180 |  |  | "Incorrect number of tokens to make a Vector3!\n"); | 
| 181 |  |  | painCave.severity = OPENMD_ERROR; | 
| 182 |  |  | painCave.isFatal = 1; | 
| 183 |  |  | simError(); | 
| 184 |  |  | } | 
| 185 |  |  | return false; | 
| 186 |  |  | } | 
| 187 |  |  | static std::string getParamType() { return "OpenMD::Vector3d";} | 
| 188 |  |  | }; | 
| 189 | gezelter | 1291 |  | 
| 190 | gezelter | 1700 |  | 
| 191 | chuckv | 748 | class ParameterBase { | 
| 192 |  |  | public: | 
| 193 |  |  | ParameterBase() : keyword_(), optional_(false), defaultValue_(false), empty_(true) {} | 
| 194 |  |  | virtual ~ParameterBase() {} | 
| 195 |  |  | bool isOptional() {return optional_;} | 
| 196 |  |  | void setOptional(bool optional) {optional_ = optional;} | 
| 197 |  |  | bool hasDefaultValue() {return defaultValue_;} | 
| 198 |  |  | virtual bool isValid() { return true;} | 
| 199 |  |  | const std::string& getKeyword() {return keyword_;} | 
| 200 |  |  | void setKeyword(const std::string& keyword) { keyword_ = keyword;} | 
| 201 |  |  | bool empty() {return empty_;} | 
| 202 |  |  | virtual bool setData(std::string) = 0; | 
| 203 |  |  | virtual bool setData(int) = 0; | 
| 204 | gezelter | 1313 | virtual bool setData(unsigned long int) = 0; | 
| 205 | tim | 963 | virtual bool setData(RealType) = 0; | 
| 206 | gezelter | 1291 | virtual bool setData(std::pair<int, int>) = 0; | 
| 207 | gezelter | 1700 | virtual bool setData(OpenMD::Vector3d) = 0; | 
| 208 | chuckv | 748 | virtual std::string getParamType() = 0; | 
| 209 |  |  | protected: | 
| 210 | gezelter | 1313 | std::string keyword_; | 
| 211 | chuckv | 748 | bool optional_; | 
| 212 |  |  | bool defaultValue_; | 
| 213 |  |  | bool empty_; | 
| 214 |  |  | }; | 
| 215 |  |  |  | 
| 216 |  |  | template<class ParamType> | 
| 217 |  |  | class Parameter : public ParameterBase{ | 
| 218 |  |  | public: | 
| 219 |  |  | typedef ParameterTraits<ParamType> ValueType; | 
| 220 |  |  | void setDefaultValue(const ParamType& value) {data_ = value; defaultValue_ = true;} | 
| 221 |  |  | ParamType getData() { return data_;} | 
| 222 |  |  |  | 
| 223 |  |  | virtual bool setData(std::string sval) { | 
| 224 |  |  | return internalSetData<std::string>(sval); | 
| 225 |  |  | } | 
| 226 | gezelter | 1313 |  | 
| 227 | chuckv | 748 | virtual bool setData(int ival) { | 
| 228 |  |  | return internalSetData<int>(ival); | 
| 229 |  |  | } | 
| 230 | gezelter | 1313 |  | 
| 231 |  |  | virtual bool setData(unsigned long int lival) { | 
| 232 |  |  | return internalSetData<unsigned long int>(lival); | 
| 233 |  |  | } | 
| 234 | chuckv | 748 |  | 
| 235 | tim | 963 | virtual bool setData(RealType dval) { | 
| 236 |  |  | return internalSetData<RealType>(dval); | 
| 237 | chuckv | 748 | } | 
| 238 | gezelter | 1291 |  | 
| 239 |  |  | virtual bool setData(std::pair<int, int> pval) { | 
| 240 |  |  | return internalSetData<std::pair<int, int> >(pval); | 
| 241 |  |  | } | 
| 242 | gezelter | 1700 | virtual bool setData(OpenMD::Vector3d pval) { | 
| 243 |  |  | return internalSetData<OpenMD::Vector3d >(pval); | 
| 244 |  |  | } | 
| 245 | chuckv | 748 |  | 
| 246 |  |  | virtual std::string getParamType() { return ParameterTraits<ParamType>::getParamType();} | 
| 247 |  |  | private: | 
| 248 |  |  | template<class T> bool internalSetData(T data) { | 
| 249 |  |  | ParamType tmp; | 
| 250 |  |  | bool result = ValueType::convert(data, tmp); | 
| 251 |  |  | if (result) { | 
| 252 |  |  | empty_ = false; | 
| 253 |  |  | data_ = tmp; | 
| 254 |  |  | } | 
| 255 |  |  | return result; | 
| 256 |  |  | } | 
| 257 |  |  |  | 
| 258 |  |  | private: | 
| 259 |  |  | ParamType data_; | 
| 260 |  |  |  | 
| 261 |  |  | }; | 
| 262 |  |  |  | 
| 263 |  |  | #define DeclareParameter(NAME, TYPE)         \ | 
| 264 |  |  | private:                                                   \ | 
| 265 |  |  | Parameter<TYPE> NAME;                                     \ | 
| 266 |  |  | public:                                                      \ | 
| 267 |  |  | bool have##NAME() { return !NAME.empty();}  \ | 
| 268 |  |  | TYPE get##NAME() { return NAME.getData();} | 
| 269 |  |  |  | 
| 270 | gezelter | 1613 | #define DeclareAlterableParameter(NAME, TYPE)         \ | 
| 271 |  |  | private:                                                   \ | 
| 272 |  |  | Parameter<TYPE> NAME;                                     \ | 
| 273 |  |  | public:                                                      \ | 
| 274 |  |  | bool have##NAME() { return !NAME.empty();}  \ | 
| 275 |  |  | TYPE get##NAME() { return NAME.getData();}  \ | 
| 276 |  |  | bool set##NAME(TYPE s) { return NAME.setData(s);}  \ | 
| 277 | chuckv | 748 |  | 
| 278 |  |  |  | 
| 279 | gezelter | 1613 |  | 
| 280 | chuckv | 748 | #define DefineParameter(NAME,KEYWORD)                              \ | 
| 281 |  |  | NAME.setKeyword(KEYWORD);                  \ | 
| 282 | tim | 817 | parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME))); | 
| 283 | chuckv | 748 |  | 
| 284 |  |  | #define DefineOptionalParameter(NAME,KEYWORD)                              \ | 
| 285 |  |  | NAME.setKeyword(KEYWORD); NAME.setOptional(true);                    \ | 
| 286 | tim | 817 | parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME))); | 
| 287 | chuckv | 748 |  | 
| 288 |  |  | #define DefineOptionalParameterWithDefaultValue(NAME,KEYWORD, DEFAULTVALUE)                              \ | 
| 289 |  |  | NAME.setKeyword(KEYWORD); NAME.setOptional(true); NAME.setDefaultValue(DEFAULTVALUE);                      \ | 
| 290 | tim | 817 | parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME))); | 
| 291 | chuckv | 748 |  | 
| 292 |  |  | #define CheckParameter(NAME, CONSTRAINT)                              \ | 
| 293 | 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();} } | 
| 294 | chuckv | 748 |  | 
| 295 |  |  |  | 
| 296 |  |  |  | 
| 297 |  |  | #endif |