Loading...
Searching...
No Matches
Parameters.h
// Copyright (C) 2009-2017 Anders Logg and Garth N. Wells
//
// This file is part of DOLFIN.
//
// DOLFIN is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DOLFIN is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
#ifndef __PARAMETERS_H
#define __PARAMETERS_H
#include <map>
#include <set>
#include <vector>
#include <boost/optional.hpp>
#include <boost/variant.hpp>
#include "Parameter.h"
#include <dolfin/log/log.h>
namespace boost
{
namespace program_options
{
class variables_map;
class options_description;
}
}
namespace dolfin
{
class Parameters
{
public:
explicit Parameters(std::string key="parameters");
virtual ~Parameters();
Parameters(const Parameters& parameters);
std::string name() const;
void rename(std::string key);
void clear();
// Note: This is not called 'add' because SWIG does not handle
// typesafe C++ enums correctly. It may be renamed when switching
// to pybind11.
//
void add_unset(std::string key, Parameter::Type type);
// Deprecated. Use add_unset (see add_unset note).
//
template<typename T> void add(std::string key)
{
// Check key name
if (has_parameter(key))
{
dolfin_error("Parameters.cpp",
"add parameter",
"Parameter \"%s.%s\" already defined",
this->name().c_str(), key.c_str());
}
// Add parameter. Check for bool must come before check for
// std::is_integral.
if (std::is_same<T, bool>::value)
_parameters.insert({key, Parameter(key, Parameter::Type::Bool)});
else if (std::is_same<T, std::string>::value)
_parameters.insert({key, Parameter(key, Parameter::Type::String)});
else if (std::is_integral<T>::value)
_parameters.insert({key, Parameter(key, Parameter::Type::Int)});
else if (std::is_floating_point<T>::value)
_parameters.insert({key, Parameter(key, Parameter::Type::Float)});
else
{
dolfin_error("Parameters.cpp",
"add parameter",
"Parameter type not supported");
}
}
template<typename T> void add(std::string key, T min, T max)
{
// Check key name
if (has_parameter(key))
{
dolfin_error("Parameters.cpp",
"add parameter",
"Parameter \"%s.%s\" already defined",
this->name().c_str(), key.c_str());
}
// Add parameter
_parameters.insert({key, Parameter(key, min, max)});
}
void add(std::string key, std::set<std::string> valid_values)
{
// Check key name
if (has_parameter(key))
{
dolfin_error("Parameters.cpp",
"add parameter",
"Parameter \"%s.%s\" already defined",
this->name().c_str(), key.c_str());
}
// Add parameter
_parameters.insert({key, Parameter(key, valid_values)});
}
void add(std::string key, int value);
void add(std::string key, int value, int min_value, int max_value);
void add(std::string key, double value);
void add(std::string key, double value, double min_value, double max_value);
void add(std::string key, std::string value);
void add(std::string key, const char* value);
void add(std::string key, std::string value, std::set<std::string> range);
void add(std::string key, const char* value, std::set<std::string> range);
void add(std::string key, bool value);
void add(const Parameters& parameters);
void remove(std::string key);
virtual void parse(int argc, char* argv[]);
void update(const Parameters& parameters);
Parameter& operator[] (std::string key);
const Parameter& operator[] (std::string key) const;
// Note: We would have liked to use [] also for access of nested
// parameter sets just like we do in Python but we can't overload
// on return type.
Parameters& operator() (std::string key);
const Parameters& operator() (std::string key) const;
const Parameters& operator= (const Parameters& parameters);
bool has_key(std::string key) const;
bool has_parameter(std::string key) const;
bool has_parameter_set(std::string key) const;
void get_parameter_keys(std::vector<std::string>& keys) const;
void get_parameter_set_keys(std::vector<std::string>& keys) const;
std::string str(bool verbose) const;
boost::optional<Parameter&> find_parameter(std::string key);
boost::optional<Parameters&> find_parameter_set(std::string key);
protected:
void parse_common(int argc, char* argv[]);
void parse_petsc(int argc, char* argv[]);
private:
// Add all parameters as options to a boost::program_option
// instance
void
add_parameter_set_to_po(boost::program_options::options_description& desc,
const Parameters &parameters,
std::string base_name="") const;
// Read in values from the boost::variable_map
void read_vm(boost::program_options::variables_map& vm,
Parameters &parameters,
std::string base_name="");
// Parameter set key
std::string _key;
// Map from key to parameter(s)
std::map<std::string, boost::variant<Parameter, Parameters>> _parameters;
public:
std::size_t size() const { return _parameters.size(); }
std::map<std::string, boost::variant<Parameter, Parameters>>::const_iterator begin() const
{ return _parameters.cbegin(); }
//decltype(_parameters.cbegin()) begin() const { return _parameters.cbegin(); }
std::map<std::string, boost::variant<Parameter, Parameters>>::const_iterator end() const
{ return _parameters.cend(); }
//decltype(_parameters.cend()) end() const { return _parameters.cend(); }
};
extern Parameters empty_parameters;
}
#endif
dolfin::Parameter
Base class for parameters.
Definition Parameter.h:34
dolfin::Parameter::Type
Type
Enum for the parameter type.
Definition Parameter.h:54
dolfin::Parameters
Definition Parameters.h:95
dolfin::Parameters::update
void update(const Parameters &parameters)
Update parameters with another set of parameters.
Definition Parameters.cpp:245
dolfin::Parameters::parse_common
void parse_common(int argc, char *argv[])
Parse filtered options (everything except PETSc options).
Definition Parameters.cpp:458
dolfin::Parameters::has_key
bool has_key(std::string key) const
Check if parameter set has key (parameter or nested parameter set).
Definition Parameters.cpp:381
dolfin::Parameters::add
void add(std::string key, std::set< std::string > valid_values)
Definition Parameters.h:178
dolfin::Parameters::add
void add(std::string key)
Definition Parameters.h:128
dolfin::Parameters::parse
virtual void parse(int argc, char *argv[])
Parse parameters from command-line.
Definition Parameters.cpp:239
dolfin::Parameters::find_parameter
boost::optional< Parameter & > find_parameter(std::string key)
Return parameter, if present.
Definition Parameters.cpp:611
dolfin::Parameters::rename
void rename(std::string key)
Rename parameter set.
Definition Parameters.cpp:55
dolfin::Parameters::~Parameters
virtual ~Parameters()
Destructor.
Definition Parameters.cpp:40
dolfin::Parameters::has_parameter_set
bool has_parameter_set(std::string key) const
Check if parameter set has given nested parameter set.
Definition Parameters.cpp:392
dolfin::Parameters::Parameters
Parameters(std::string key="parameters")
Create empty parameter set.
Definition Parameters.cpp:34
dolfin::Parameters::get_parameter_set_keys
void get_parameter_set_keys(std::vector< std::string > &keys) const
Return a vector of parameter set keys.
Definition Parameters.cpp:407
dolfin::Parameters::begin
std::map< std::string, boost::variant< Parameter, Parameters > >::const_iterator begin() const
Interface for pybind11 iterators.
Definition Parameters.h:309
dolfin::Parameters::clear
void clear()
Clear parameter set.
Definition Parameters.cpp:61
dolfin::Parameters::add_unset
void add_unset(std::string key, Parameter::Type type)
Add unset parameter of specified type.
Definition Parameters.cpp:70
dolfin::Parameters::operator()
Parameters & operator()(std::string key)
Return nested parameter set for given key.
Definition Parameters.cpp:330
dolfin::Parameters::str
std::string str(bool verbose) const
Return informal string representation (pretty-print).
Definition Parameters.cpp:416
dolfin::Parameters::remove
void remove(std::string key)
Remove parameter or parameter set with given key.
Definition Parameters.cpp:223
dolfin::Parameters::operator=
const Parameters & operator=(const Parameters &parameters)
Assignment operator.
Definition Parameters.cpp:370
dolfin::Parameters::has_parameter
bool has_parameter(std::string key) const
Check if parameter set has given parameter.
Definition Parameters.cpp:386
dolfin::Parameters::parse_petsc
void parse_petsc(int argc, char *argv[])
Parse filtered options (only PETSc options).
Definition Parameters.cpp:502
dolfin::Parameters::find_parameter_set
boost::optional< Parameters & > find_parameter_set(std::string key)
Return parameter set, if present.
Definition Parameters.cpp:622
dolfin::Parameters::add
void add(std::string key, T min, T max)
Definition Parameters.h:160
dolfin::Parameters::end
std::map< std::string, boost::variant< Parameter, Parameters > >::const_iterator end() const
Interface for pybind11 iterators.
Definition Parameters.h:314
dolfin::Parameters::get_parameter_keys
void get_parameter_keys(std::vector< std::string > &keys) const
Return a vector of parameter keys.
Definition Parameters.cpp:398
dolfin::Parameters::name
std::string name() const
Return name for parameter set.
Definition Parameters.cpp:50
dolfin::Parameters::size
std::size_t size() const
Interface for pybind11 iterators.
Definition Parameters.h:306
dolfin::Parameters::operator[]
Parameter & operator[](std::string key)
Return parameter for given key.
Definition Parameters.cpp:290
dolfin
Definition adapt.h:30
dolfin::empty_parameters
Parameters empty_parameters("empty")
Default empty parameters.
Definition Parameters.h:321
dolfin::dolfin_error
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition log.cpp:129
dolfin::parameters
GlobalParameters parameters
The global parameter database.
Definition GlobalParameters.cpp:32