Path Integral Quantum Monte Carlo
setup.h
Go to the documentation of this file.
1 
9 #ifndef SETUP_H
10 #define SETUP_H
11 
12 #include "common.h"
13 #include "factory.h"
14 #include <boost/program_options.hpp>
15 #include <boost/any.hpp>
16 
18 enum ParamState {UNSET,DEFAULTED,SET};
19 
20 namespace po = boost::program_options;
21 namespace pt = boost::property_tree;
22 
23 class Container;
24 class PotentialBase;
25 class WaveFunctionBase;
26 class ActionBase;
27 class Path;
28 class LookupTable;
29 class MoveBase;
30 class EstimatorBase;
31 
32 ostream& operator<<(ostream&, const vector<string>&);
33 
34 // ========================================================================
35 // Parameters Class
36 // ========================================================================
44 class Parameters {
45  public:
46  Parameters() {};
47 
49  template <typename Ttype>
50  void add(string,string,string);
51 
53  template <typename Ttype>
54  void add(string,string,string, const Ttype);
55 
57  template<typename Ttype>
58  void set(const string&, const Ttype);
59 
61  template<typename Ttype>
62  void set(const string&, const pt::ptree &);
63 
66  void setupCommandLine(boost::ptr_map<string,po::options_description>&);
67 
69  void update(int,char*[],po::options_description&);
70 
72  void update(const pt::ptree &);
73 
75  void print();
76 
78  const po::variables_map& operator()() const {return params;}
79  po::variables_map& operator()() { return params;}
80 
82  const bool operator()(const string & key) const { return !(state.at(key) == UNSET); }
83 
85  const po::variable_value & operator[](const string & key) const {return params[key];}
86  po::variable_value & operator[](const string & key) {return params.at(key);}
87 
88  private:
89 
90  using Extractor = map<string, void(*)(const po::variable_value &)>;
91 
92  po::variables_map params;
93  map<string, const type_info&> type;
94  map<string,string> pClass;
95  map<string,ParamState> state;
96  map<string,string> shortName;
97  map<string,string> helpMessage;
98  Extractor extract;
99 
101  vector<string> split(const string &, char);
102 
103  template <typename Ttype>
104  po::typed_value<Ttype, char>* initValue(const string &);
105 
107  bool getNode(const pt::ptree &xmlParams, pt::ptree &node, const string key) {
108  boost::optional<const pt::ptree&> child = xmlParams.get_child_optional(key);
109  if (!child)
110  return false;
111  else
112  node = *child;
113  return true;
114  }
115 };
116 
117 /**************************************************************************/
124 template <typename Ttype>
125 void Parameters::add(string _label,string _helpMessage,string _pClass) {
126 
127  /* We break the label up at a possible comma and store the short-name in a
128  * map*/
129  vector<string> label = split(_label,',');
130  string key = label[0];
131  if (label.size() > 1)
132  shortName.emplace(key,label[1]);
133  else
134  shortName.emplace(key,"");
135 
136  /* Add the help message to a map */
137  helpMessage.emplace(key,_helpMessage);
138 
139  /* Add the parameter class to a map */
140  pClass.emplace(key,_pClass);
141 
142  /* Add the type to the appropriate map */
143  type.emplace(pair<string,const type_info&>(key,typeid(Ttype)));
144 
145  /* Stores the function signature to print an element */
146  extract.emplace(pair<string,void(*)(const po::variable_value &)>
147  (key,[](const po::variable_value& v) {cout << v.as<Ttype>();}));
148 
149  /* Initialize the parameter state */
150  state.emplace(key,UNSET);
151 
152  /* Insert an empty parameter */
153  params.insert(make_pair(key, po::variable_value()));
154 
155  /* update the parameters map */
156  po::notify(params);
157 }
158 
159 /**************************************************************************/
167 template <typename Ttype>
168 void Parameters::add(string _label,string _helpMessage,string _pClass,
169  const Ttype _defaultValue) {
170 
171  /* We perform a standard initialization */
172  add<Ttype>(_label,_helpMessage,_pClass);
173 
174  /* Get the long name */
175  vector<string> label = split(_label,',');
176  string key = label[0];
177 
178  /* Insert the default value into the parameter map */
179  set<Ttype>(key,_defaultValue);
180 
181  /* Set the parameter state */
182  state.at(key) = DEFAULTED;
183 }
184 
185 /**************************************************************************/
191 template<typename Ttype>
192 void Parameters::set(const string& key, const Ttype val) {
193  if (params.count(key)) {
194  po::variables_map::iterator it(params.find(key));
195  po::variable_value & v(it->second);
196  v.value() = val;
197  }
198  else {
199  params.insert(std::make_pair(key, po::variable_value(val, false)));
200  po::notify(params);
201 
202  /* insert the type */
203  type.emplace(pair<string,const type_info&>(key,typeid(Ttype)));
204  }
205 
206  state[key] = SET;
207 }
208 
209 /**************************************************************************/
215 template<typename Ttype>
216 void Parameters::set(const string& key, const pt::ptree &xml) {
217  if (!xml.count(key))
218  return;
219 
220  /* command line overrides xml file */
221  if (state[key] == UNSET || state[key] == DEFAULTED) {
222  set<Ttype>(key,xml.get<Ttype>(key));
223  state[key] = SET;
224  }
225 }
226 
227 /**************************************************************************/
231 template <typename Ttype>
232 po::typed_value<Ttype, char>* Parameters::initValue(const string& key) {
233  if (state[key] == DEFAULTED)
234  return po::value<Ttype>()->default_value(params[key].as<Ttype>());
235  else
236  return po::value<Ttype>();
237 }
238 
239 // ========================================================================
240 // Setup Class
241 // ========================================================================
255 class Setup {
256  public:
257  Setup();
258 
259  /* Get the options from the command line */
260  void getOptions(int, char*[]);
261  /* Parse the options and check for errors (needs to be more complete) */
262  bool parseOptions();
263 
264  /* Setup the worldlines */
265  bool worldlines();
266 
267  /* Setup the physical simulation cell */
268  Container *cell();
269 
270  /* Setup the simulation constants */
271  void setConstants();
272 
273  /* Setup the communicator */
274  void communicator();
275 
276  /* Define the random seed */
277  uint32 seed(const uint32);
278 
279  /* Output all options to disk */
280  void outputOptions(int, char*[], const uint32, const Container*, const iVec&);
281 
282  /* Setup the interaction potential */
284 
285  /* Setup the external potential */
287 
288  /* Setup the trial wave function */
290 
291  /* Setup the action */
293 
294  /* Setup the move array */
295  boost::ptr_vector<MoveBase> * moves(Path &, ActionBase *, MTRand &);
296 
297  /* Setup the estimator array */
298  boost::ptr_vector<EstimatorBase> * estimators(Path &, ActionBase *, MTRand &);
299  boost::ptr_vector<EstimatorBase> * estimators(boost::ptr_vector<Path> &,
300  boost::ptr_vector<ActionBase> &, MTRand&);
301 
303 
304  private:
305  vector<string> interactionPotentialName;
306  vector<string> externalPotentialName;
307  vector<string> waveFunctionName;
308  vector<string> randomGeneratorName;
309  vector<string> actionName;
310  vector<string> estimatorName;
311  vector<string> moveName;
312  vector<string> optionClassNames;
313 
314  string interactionNames;
315  string externalNames;
316  string waveFunctionNames;
317  string randomGeneratorNames;
318  string actionNames;
319  string estimatorNames;
320  string moveNames;
321 
322  /* The factories needed to instantiate objects */
323  MoveFactory moveFactory;
324  EstimatorFactory estimatorFactory;
325  MultiEstimatorFactory multiEstimatorFactory;
326 
327  bool definedCell;
328 
329  boost::ptr_map<string,po::options_description> optionClasses;
330  po::options_description cmdLineOptions;
331 
333  void cleanCommandLineOptions(int, char*[], vector<string> &, vector<string> &,
334  vector<string>&);
335  void update(int,char*[],po::options_description&);
336 
338  void initParameters();
339 
340  /* Get a formatted list of xml options */
341  string getXMLOptionList(const vector<string> &, const string);
342 };
343 
344 #endif
Holds a base class that all action classes will be derived from.
Definition: action.h:29
The base class which holds details on the generalized box that our system will be simulated inside of...
Definition: container.h:24
The base class that all estimator classes will be derived from.
Definition: estimator.h:28
The particle (bead) lookup table.
Definition: lookuptable.h:29
The base class that all moves will be derived from.
Definition: move.h:30
Simulation Parameters.
Definition: setup.h:44
void print()
print out the parameter map
Definition: setup.cpp:75
void set(const string &, const Ttype)
Set a parameter from a value.
Definition: setup.h:192
const po::variable_value & operator[](const string &key) const
Parmaters[key] returns the variable value at key.
Definition: setup.h:85
const bool operator()(const string &key) const
Parmaters(key) returns the existence of a parameter.
Definition: setup.h:82
const po::variables_map & operator()() const
Parameters() returns the full map.
Definition: setup.h:78
void update(int, char *[], po::options_description &)
Update parameters from command line.
Definition: setup.cpp:151
void add(string, string, string)
Add a parameter to the map without a default value.
Definition: setup.h:125
void update(const pt::ptree &)
Update parameters from an xml file.
void setupCommandLine(boost::ptr_map< string, po::options_description > &)
Insert options into the options_description data structure to be read from the command line.
Definition: setup.cpp:97
The space-time trajectories.
Definition: path.h:29
The base class from which all specific potentials are derived from.
Definition: potential.h:32
Setup the simulation.
Definition: setup.h:255
ActionBase * action(const Path &, LookupTable &, PotentialBase *, PotentialBase *, WaveFunctionBase *)
Setup the action.
Definition: setup.cpp:1168
Setup()
Setup the program_options variables.
Definition: setup.cpp:262
uint32 seed(const uint32)
Return the random seed.
Definition: setup.cpp:761
bool parseOptions()
Parse the command line options for obvious errors and return values.
Definition: setup.cpp:488
PotentialBase * interactionPotential(const Container *)
Setup the interaction potential.
Definition: setup.cpp:1026
WaveFunctionBase * waveFunction(const Path &, LookupTable &)
Setup the trial wave function.
Definition: setup.cpp:1143
void setConstants()
Setup the simulation constants.
Definition: setup.cpp:976
void outputOptions(int, char *[], const uint32, const Container *, const iVec &)
Output the simulation parameters to a log file.
Definition: setup.cpp:1451
void communicator()
Setup the communicator.
Definition: setup.cpp:1013
boost::ptr_vector< EstimatorBase > * estimators(Path &, ActionBase *, MTRand &)
Create a list of estimators to be measured.
Definition: setup.cpp:1296
void getOptions(int, char *[])
Define all command line options and get them from the command line.
Definition: setup.cpp:464
boost::ptr_vector< MoveBase > * moves(Path &, ActionBase *, MTRand &)
Define the Monte Carlo updates that will be performed.
Definition: setup.cpp:1227
Parameters params
All simulation parameters.
Definition: setup.h:302
Container * cell()
Setup the simulation cell.
Definition: setup.cpp:771
bool worldlines()
Setup the worldlines.
Definition: setup.cpp:833
PotentialBase * externalPotential(const Container *)
Setup the external potential.
Definition: setup.cpp:1063
Holds a base class that all trial wave function classes will be derived from.
Definition: wavefunction.h:26
Global common header with shared dependencies and methods.
unsigned long uint32
Unsigned integer type, at least 32 bits.
Definition: common.h:105
TinyVector< int, NDIM > iVec
A NDIM-vector of type integer.
Definition: common.h:114
Factory class definitions.
ParamState
The three possible states of a parameter.
Definition: setup.h:18
ostream & operator<<(ostream &, const vector< string > &)
Overload << to print a vector of strings to the terminal.
Definition: setup.cpp:37