DOLFIN
DOLFIN C++ interface
Parameters.h
1 // Copyright (C) 2009-2017 Anders Logg and Garth N. Wells
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 
18 #ifndef __PARAMETERS_H
19 #define __PARAMETERS_H
20 
21 #include <map>
22 #include <set>
23 #include <vector>
24 #include <boost/optional.hpp>
25 #include <boost/variant.hpp>
26 #include "Parameter.h"
27 #include <dolfin/log/log.h>
28 
29 namespace boost
30 {
31  namespace program_options
32  {
33  class variables_map;
34  class options_description;
35  }
36 }
37 
38 namespace dolfin
39 {
40 
93 
94  class Parameters
95  {
96  public:
97 
99  explicit Parameters(std::string key="parameters");
100 
102  virtual ~Parameters();
103 
106 
108  std::string name() const;
109 
111  void rename(std::string key);
112 
114  void clear();
115 
116  // Note: This is not called 'add' because SWIG does not handle
117  // typesafe C++ enums correctly. It may be renamed when switching
118  // to pybind11.
119  //
121  void add_unset(std::string key, Parameter::Type type);
122 
123  // Deprecated. Use add_unset (see add_unset note).
124  //
128  template<typename T> void add(std::string key)
129  {
130  // Check key name
131  if (has_parameter(key))
132  {
133  dolfin_error("Parameters.cpp",
134  "add parameter",
135  "Parameter \"%s.%s\" already defined",
136  this->name().c_str(), key.c_str());
137  }
138 
139  // Add parameter. Check for bool must come before check for
140  // std::is_integral.
141  if (std::is_same<T, bool>::value)
142  _parameters.insert({key, Parameter(key, Parameter::Type::Bool)});
143  else if (std::is_same<T, std::string>::value)
144  _parameters.insert({key, Parameter(key, Parameter::Type::String)});
145  else if (std::is_integral<T>::value)
146  _parameters.insert({key, Parameter(key, Parameter::Type::Int)});
147  else if (std::is_floating_point<T>::value)
148  _parameters.insert({key, Parameter(key, Parameter::Type::Float)});
149  else
150  {
151  dolfin_error("Parameters.cpp",
152  "add parameter",
153  "Parameter type not supported");
154  }
155  }
156 
160  template<typename T> void add(std::string key, T min, T max)
161  {
162  // Check key name
163  if (has_parameter(key))
164  {
165  dolfin_error("Parameters.cpp",
166  "add parameter",
167  "Parameter \"%s.%s\" already defined",
168  this->name().c_str(), key.c_str());
169  }
170 
171  // Add parameter
172  _parameters.insert({key, Parameter(key, min, max)});
173  }
174 
178  void add(std::string key, std::set<std::string> valid_values)
179  {
180  // Check key name
181  if (has_parameter(key))
182  {
183  dolfin_error("Parameters.cpp",
184  "add parameter",
185  "Parameter \"%s.%s\" already defined",
186  this->name().c_str(), key.c_str());
187  }
188 
189  // Add parameter
190  _parameters.insert({key, Parameter(key, valid_values)});
191  }
192 
194  void add(std::string key, int value);
195 
197  void add(std::string key, int value, int min_value, int max_value);
198 
200  void add(std::string key, double value);
201 
203  void add(std::string key, double value, double min_value, double max_value);
204 
206  void add(std::string key, std::string value);
207 
209  void add(std::string key, const char* value);
210 
212  void add(std::string key, std::string value, std::set<std::string> range);
213 
215  void add(std::string key, const char* value, std::set<std::string> range);
216 
218  void add(std::string key, bool value);
219 
221  void add(const Parameters& parameters);
222 
224  void remove(std::string key);
225 
227  virtual void parse(int argc, char* argv[]);
228 
230  void update(const Parameters& parameters);
231 
233  Parameter& operator[] (std::string key);
234 
236  const Parameter& operator[] (std::string key) const;
237 
238  // Note: We would have liked to use [] also for access of nested
239  // parameter sets just like we do in Python but we can't overload
240  // on return type.
241 
243  Parameters& operator() (std::string key);
244 
246  const Parameters& operator() (std::string key) const;
247 
249  const Parameters& operator= (const Parameters& parameters);
250 
252  bool has_key(std::string key) const;
253 
255  bool has_parameter(std::string key) const;
256 
258  bool has_parameter_set(std::string key) const;
259 
261  void get_parameter_keys(std::vector<std::string>& keys) const;
262 
264  void get_parameter_set_keys(std::vector<std::string>& keys) const;
265 
267  std::string str(bool verbose) const;
268 
270  boost::optional<Parameter&> find_parameter(std::string key);
271 
273  boost::optional<Parameters&> find_parameter_set(std::string key);
274 
275  protected:
276 
278  void parse_common(int argc, char* argv[]);
279 
281  void parse_petsc(int argc, char* argv[]);
282 
283  private:
284 
285  // Add all parameters as options to a boost::program_option
286  // instance
287  void
288  add_parameter_set_to_po(boost::program_options::options_description& desc,
289  const Parameters &parameters,
290  std::string base_name="") const;
291 
292  // Read in values from the boost::variable_map
293  void read_vm(boost::program_options::variables_map& vm,
294  Parameters &parameters,
295  std::string base_name="");
296 
297  // Parameter set key
298  std::string _key;
299 
300  // Map from key to parameter(s)
301  std::map<std::string, boost::variant<Parameter, Parameters>> _parameters;
302 
303  public:
304 
306  std::size_t size() const { return _parameters.size(); }
307 
309  std::map<std::string, boost::variant<Parameter, Parameters>>::const_iterator begin() const
310  { return _parameters.cbegin(); }
311  //decltype(_parameters.cbegin()) begin() const { return _parameters.cbegin(); }
312 
314  std::map<std::string, boost::variant<Parameter, Parameters>>::const_iterator end() const
315  { return _parameters.cend(); }
316  //decltype(_parameters.cend()) end() const { return _parameters.cend(); }
317 
318  };
319 
322 
323 }
324 
325 #endif
void add(std::string key, T min, T max)
Definition: Parameters.h:160
Definition: XDMFFile.h:39
void add(std::string key, std::set< std::string > valid_values)
Definition: Parameters.h:178
Type
Enum for the parameter type.
Definition: Parameter.h:54
Definition: adapt.h:29
void add(std::string key)
Definition: Parameters.h:128
std::size_t size() const
Interface for pybind11 iterators.
Definition: Parameters.h:306
std::map< std::string, boost::variant< Parameter, Parameters > >::const_iterator end() const
Interface for pybind11 iterators.
Definition: Parameters.h:314
Parameters empty_parameters("empty")
Default empty parameters.
Definition: Parameters.h:321
Base class for parameters.
Definition: Parameter.h:33
Definition: Parameters.h:94
std::map< std::string, boost::variant< Parameter, Parameters > >::const_iterator begin() const
Interface for pybind11 iterators.
Definition: Parameters.h:309
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition: log.cpp:129
GlobalParameters parameters
The global parameter database.
Definition: GlobalParameters.cpp:32