Path Integral Quantum Monte Carlo
Public Member Functions
Parameters Class Reference

Simulation Parameters. More...

#include <setup.h>

Public Member Functions

template<typename Ttype >
void add (string, string, string)
 Add a parameter to the map without a default value. More...
 
template<typename Ttype >
void add (string, string, string, const Ttype)
 Add a parameter to the map with a default value. More...
 
template<typename Ttype >
void set (const string &, const Ttype)
 Set a parameter from a value. More...
 
template<typename Ttype >
void set (const string &, const pt::ptree &)
 Set a parameter from an xml node. More...
 
void setupCommandLine (boost::ptr_map< string, po::options_description > &)
 Insert options into the options_description data structure to be read from the command line. More...
 
void update (int, char *[], po::options_description &)
 Update parameters from command line. More...
 
void update (const pt::ptree &)
 Update parameters from an xml file.
 
void print ()
 print out the parameter map More...
 
const po::variables_map & operator() () const
 Parameters() returns the full map.
 
po::variables_map & operator() ()
 
const bool operator() (const string &key) const
 Parmaters(key) returns the existence of a parameter.
 
const po::variable_value & operator[] (const string &key) const
 Parmaters[key] returns the variable value at key.
 
po::variable_value & operator[] (const string &key)
 

Detailed Description

Simulation Parameters.

See also
http://stackoverflow.com/questions/7174781/boost-program-options-notifier-for-options-with-no-value in the future to change default value of no value options to be bool

Definition at line 44 of file setup.h.

Member Function Documentation

◆ add() [1/2]

template<typename Ttype >
void Parameters::add ( string  _label,
string  _helpMessage,
string  _pClass 
)

Add a parameter to the map without a default value.

Initialize a parameter in the map.

Parameters
_labellongName,shortName where shortName is optional
_helpMessagea command line help message
_pClassthe class of parameter

Definition at line 125 of file setup.h.

125  {
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 }

◆ add() [2/2]

template<typename Ttype >
void Parameters::add ( string  _label,
string  _helpMessage,
string  _pClass,
const Ttype  _defaultValue 
)

Add a parameter to the map with a default value.

Initialize a parameter in the map with a default value.

Parameters
_labellongName,shortName where shortName is optional
_helpMessagea command line help message
_pClassthe class of parameter
_defaultValuethe default value of the parameter

Definition at line 168 of file setup.h.

169  {
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 }

◆ print()

void Parameters::print ( )

print out the parameter map

Print all values in a parameter map.

See also
http://stackoverflow.com/questions/21008893/boost-program-options-iterate-over-variables-map

Definition at line 75 of file setup.cpp.

75  {
76 
77  for(auto const& par : params ) {
78  if (!par.second.empty()) {
79  string key = par.first;
80  cout << key << ":\t";
81  if (type.at(key) == typeid(bool))
82  cout << params.count(key);
83  else
84  extract[par.first](par.second);
85  cout << endl;
86  }
87  }
88 }

◆ set() [1/2]

template<typename Ttype >
void Parameters::set ( const string &  key,
const pt::ptree &  xml 
)

Set a parameter from an xml node.

Set the value of a parameter in the map from an xml node.

Parameters
keyname of the parameter
xmlnode

Definition at line 216 of file setup.h.

216  {
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 }

◆ set() [2/2]

template<typename Ttype >
void Parameters::set ( const string &  key,
const Ttype  val 
)

Set a parameter from a value.

Set the value of a parameter in the map from a value.

Parameters
keyname of the parameter
valvalue of the parameter

Definition at line 192 of file setup.h.

192  {
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 }
+ Here is the caller graph for this function:

◆ setupCommandLine()

void Parameters::setupCommandLine ( boost::ptr_map< string, po::options_description > &  _options)

Insert options into the options_description data structure to be read from the command line.

Insert all parameters into a pointer map of options.

At present we can accept bool, double, int, uint32, string and vector<string> options. This needs to be modified for each new option data type.

Definition at line 97 of file setup.cpp.

97  {
98 
99  /* We iterate through the parameters map */
100  for (auto & par : params) {
101 
102  /* get the key */
103  string key = par.first;
104 
105  /* Get the correct option class */
106  po::options_description &option = _options[pClass[key]];
107 
108  /* construct a command line label out of the long and short name */
109  string label = key;
110  if (shortName[key] != "")
111  label += "," + shortName[key];
112 
113  /* insert the option for each possible data type */
114  if (type.at(key) == typeid(bool))
115  option.add_options()(label.c_str(),helpMessage[key].c_str());
116  /* option.add_options()(label.c_str(),po::bool_switch()->default_value(false),helpMessage[key].c_str()); */
117  else if (type.at(key) == typeid(double))
118  option.add_options()(label.c_str(),initValue<double>(key),helpMessage[key].c_str());
119  else if (type.at(key) == typeid(int))
120  option.add_options()(label.c_str(),initValue<int>(key),helpMessage[key].c_str());
121  else if (type.at(key) == typeid(uint32))
122  option.add_options()(label.c_str(),initValue<uint32>(key),helpMessage[key].c_str());
123  else if (type.at(key) == typeid(string))
124  option.add_options()(label.c_str(),initValue<string>(key),helpMessage[key].c_str());
125  else if (type.at(key) == typeid(vector<string>)) {
126 
127  if (state[key] == DEFAULTED) {
128  /* a temporary copy of the vector of strings */
129  vector<string> ops = par.second.as<vector<string>>();
130  option.add_options()(label.c_str(), po::value<vector<string>>()->
131  default_value(ops, getList(ops))->composing(), helpMessage[key].c_str());
132  }
133  else
134  option.add_options()(label.c_str(),
135  po::value<vector<string>>()->composing(), helpMessage[key].c_str());
136  }
137  else
138  cerr << "insertOption Failed to find a valid type.";
139  }
140 }
unsigned long uint32
Unsigned integer type, at least 32 bits.
Definition: common.h:105
string getList(const vector< string > &options)
Create a comma separated list from a vector of strings.
Definition: setup.cpp:25
+ Here is the caller graph for this function:

◆ update()

void Parameters::update ( int  argc,
char *  argv[],
po::options_description &  cmdLineOptions 
)

Update parameters from command line.

Update the state of all parameters.

We check both the commmand line options as well as any parameters specified in an XML file. The command line takes precedence.

!!NB!! XML options needed to be correctly sorted in the file or they will be ignored. This should be fixed to exit gracefully with an error message.

Definition at line 151 of file setup.cpp.

151  {
152 
153  /* create a temporary parameter map */
154  po::variables_map cmdparams;
155 
156  /* Get the values from the command line */
157  po::store(po::parse_command_line(argc, argv, cmdLineOptions), cmdparams);
158  po::notify(cmdparams);
159 
160  /* Now we go through our local parameter map and adjust values, keeping in
161  * mind that we store boolean values */
162  for (auto & par : params) {
163 
164  /* get the key and value */
165  string key = par.first;
166  auto &val = par.second;
167 
168  /* If the parameter exists, copy it over */
169  if (cmdparams.count(key)) {
170 
171  val = cmdparams[key];
172 
173  /* update the state */
174  if (cmdparams[key].empty())
175  state[key] = UNSET;
176  else if (cmdparams[key].defaulted())
177  state[key] = DEFAULTED;
178  else
179  state[key] = SET;
180  }
181  }
182 
183  /* Now we load a potential xml file from disk. It is optional so we use a
184  * try/catch. */
185  if (!params["param_file"].empty()) {
186 
187  try {
188  pt::ptree xmlParams;
189  pt::read_xml(params["param_file"].as<string>(),xmlParams);
190  xmlParams = xmlParams.get_child("pimc");
191 
192  /* Go through our local parameter map and determine what needs to be
193  * set from the xml file */
194  for (auto & par : params) {
195 
196  /* get the key */
197  string key = par.first;
198 
199  /* the parameter class string */
200  string paramClass = pClass[key] + "_parameters";
201 
202  /* Get a clean xml node */
203  pt::ptree xml;
204  if (getNode(xmlParams,xml,paramClass)) {
205 
206  if (type.at(key) == typeid(bool)){
207  set<bool>(key,xml);
208  }
209  else if (type.at(key) == typeid(double)) {
210  set<double>(key,xml);
211  }
212  else if (type.at(key) == typeid(int)) {
213  set<int>(key,xml);
214  }
215  else if (type.at(key) == typeid(uint32)) {
216  set<uint32>(key,xml);
217  }
218  else if (type.at(key) == typeid(string)) {
219  set<string>(key,xml);
220  }
221  else if (type.at(key) == typeid(vector<string>)) {
222 
223  string pluralKey = key + "s";
224  if (xml.count(pluralKey) != 0) {
225 
226  /* a temporary copy of the vector of strings */
227  vector<string> ops;
228 
229  /* get the options */
230  for (auto const &opName : xml.get_child(pluralKey))
231  ops.push_back(opName.second.data());
232 
233  /* update the parameter */
234  set<vector<string>>(key,ops);
235  state[key] = SET;
236  }
237  }
238  else
239  cerr << "xmlInsertOption Failed to find a valid type.";
240  } //if getNode
241  } //for par
242 
243  }
244  catch(...) {
245  cerr << "Cannot read parameter file: " << params["param_file"].as<string>() << endl;
246  }
247  }
248 }
+ Here is the caller graph for this function:

The documentation for this class was generated from the following files: