Reference documentation for deal.II version 9.0.0
parameter_handler.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2018 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_parameter_handler_h
17 #define dealii_parameter_handler_h
18 
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/exceptions.h>
22 #include <deal.II/base/subscriptor.h>
23 #include <deal.II/base/patterns.h>
24 
25 #include <boost/property_tree/ptree_fwd.hpp>
26 #include <boost/serialization/split_member.hpp>
27 #include <boost/archive/basic_archive.hpp>
28 #include <boost/property_tree/ptree_serialization.hpp>
29 
30 #include <map>
31 #include <vector>
32 #include <string>
33 #include <memory>
34 
35 DEAL_II_NAMESPACE_OPEN
36 
37 // forward declarations for interfaces and friendship
38 class LogStream;
40 
800 {
801 private:
805  ParameterHandler (const ParameterHandler &) = delete;
806 
810  ParameterHandler &operator= (const ParameterHandler &) = delete;
811 
812 public:
817  {
822  Text = 1,
823 
827  LaTeX = 2,
832 
839  XML = 4,
840 
845  JSON = 5,
846 
851  ShortText = 193
852  };
853 
854 
855 
859  ParameterHandler ();
860 
866  virtual ~ParameterHandler () = default;
867 
896  virtual void parse_input (std::istream &input,
897  const std::string &filename = "input file",
898  const std::string &last_line = "");
899 
943  virtual void parse_input (const std::string &filename,
944  const std::string &last_line = "");
945 
954  virtual void parse_input_from_string (const char *s,
955  const std::string &last_line = "");
956 
964  virtual void parse_input_from_xml (std::istream &input);
965 
973  virtual void parse_input_from_json (std::istream &input);
974 
978  void clear ();
979 
980 
1000  void declare_entry (const std::string &entry,
1001  const std::string &default_value,
1002  const Patterns::PatternBase &pattern = Patterns::Anything(),
1003  const std::string &documentation = std::string());
1004 
1047  void add_action (const std::string &entry,
1048  const std::function<void (const std::string &value)> &action);
1049 
1059  template <class ParameterType>
1060  void add_parameter(const std::string &entry,
1061  ParameterType &parameter,
1062  const std::string &documentation = std::string(),
1063  const Patterns::PatternBase &pattern =
1065 
1109  void declare_alias (const std::string &existing_entry_name,
1110  const std::string &alias_name,
1111  const bool alias_is_deprecated = false);
1112 
1116  void enter_subsection (const std::string &subsection);
1117 
1121  void leave_subsection ();
1122 
1128  std::string get (const std::string &entry_string) const;
1129 
1135  long int get_integer (const std::string &entry_string) const;
1136 
1140  double get_double (const std::string &entry_name) const;
1141 
1147  bool get_bool (const std::string &entry_name) const;
1148 
1158  void set (const std::string &entry_name,
1159  const std::string &new_value);
1160 
1171  void set (const std::string &entry_name,
1172  const char *new_value);
1173 
1183  void set (const std::string &entry_name,
1184  const long int &new_value);
1185 
1199  void set (const std::string &entry_name,
1200  const double &new_value);
1201 
1211  void set (const std::string &entry_name,
1212  const bool &new_value);
1213 
1214 
1273  std::ostream &
1274  print_parameters (std::ostream &out,
1275  const OutputStyle style) const;
1276 
1294  DEAL_II_DEPRECATED
1295  void print_parameters_section (std::ostream &out,
1296  const OutputStyle style,
1297  const unsigned int indent_level,
1298  const bool include_top_level_elements = false);
1299 
1305  void log_parameters (LogStream &out);
1306 
1316  void log_parameters_section (LogStream &out);
1317 
1322  std::size_t memory_consumption () const;
1323 
1328  template <class Archive>
1329  void save (Archive &ar, const unsigned int version) const;
1330 
1335  template <class Archive>
1336  void load (Archive &ar, const unsigned int version);
1337 
1338  BOOST_SERIALIZATION_SPLIT_MEMBER()
1339 
1340 
1343  bool operator == (const ParameterHandler &prm2) const;
1344 
1354  std::string,
1355  << "The following entry already exists: " << arg1 << ".");
1360  std::string, std::string,
1361  << "The string <" << arg1
1362  << "> does not match the given pattern <" << arg2 << ">.");
1367  "You can't leave a subsection if you are already at the top level "
1368  "of the subsection hierarchy.");
1373  std::string,
1374  << "You can't ask for entry <" << arg1 << "> you have not yet declared.");
1375 
1383  std::string, std::string,
1384  << "There are unequal numbers of 'subsection' and 'end' "
1385  "statements in the parameter file <" << arg1 << ">."
1386  << (arg2.size() > 0 ? "\n" + arg2 : ""));
1387 
1393  int, std::string, std::string,
1394  << "Line <" << arg1 << "> of file <" << arg2 << ": There is "
1395  "no such subsection to be entered: " << arg3);
1396 
1403  int, std::string, std::string, << "Line <" << arg1 <<
1404  "> of file <" << arg2 << ">: " << arg3);
1405 
1412  int, std::string, std::string, std::string, std::string,
1413  << "Line <" << arg1 << "> of file <" << arg2 << ">:\n"
1414  " The entry value \n" << " " << arg3 << '\n' <<
1415  " for the entry named\n" << " " << arg4 << '\n' <<
1416  " does not match the given pattern:\n" << " " <<
1417  arg5);
1418 
1425  "The provided file could not be parsed as a "
1426  "ParameterHandler description.");
1427 
1434  std::string, std::string, std::string,
1435  << " The entry value \n" << " " << arg1 << '\n' <<
1436  " for the entry named\n" << " " << arg2 << '\n' <<
1437  " does not match the given pattern:\n" << " " <<
1438  arg3);
1439 
1447  int, std::string, std::string,
1448  << "Line <" << arg1 << "> of file <" << arg2 << ">: This line "
1449  "contains an 'include' or 'INCLUDE' statement, but the given "
1450  "file to include <" << arg3 << "> cannot be opened.");
1451 
1453 private:
1458  static const char path_separator = '.';
1459 
1463  std::vector<std::string> subsection_path;
1464 
1473  std::unique_ptr<boost::property_tree::ptree> entries;
1474 
1480  std::vector<std::unique_ptr<const Patterns::PatternBase> > patterns;
1481 
1488  std::vector<std::function<void (const std::string &)> > actions;
1489 
1496  static
1497  std::string
1498  collate_path_string (const std::vector<std::string> &subsection_path);
1499 
1508  std::string get_current_path () const;
1509 
1514  std::string get_current_full_path (const std::string &name) const;
1515 
1528  void scan_line (std::string line,
1529  const std::string &input_filename,
1530  const unsigned int current_line_n);
1531 
1543  void recursively_print_parameters (const std::vector<std::string> &target_subsection_path,
1544  const OutputStyle style,
1545  const unsigned int indent_level,
1546  std::ostream &out) const;
1547 
1548  friend class MultipleParameterLoop;
1549 };
1550 
1551 
1552 
1770 {
1771 public:
1777  {
1778  public:
1783  virtual ~UserClass () = default;
1784 
1789  virtual void create_new (const unsigned int run_no) = 0;
1790 
1794  virtual void run (ParameterHandler &prm) = 0;
1795  };
1796 
1801 
1806  virtual ~MultipleParameterLoop () override = default;
1807 
1823  virtual void parse_input (std::istream &input,
1824  const std::string &filename = "input file",
1825  const std::string &last_line = "") override;
1826 
1835 
1839  void loop (UserClass &uc);
1840 
1845  std::size_t memory_consumption () const;
1846 
1847 private:
1848 
1852  class Entry
1853  {
1854  public:
1861  {
1869  array
1870  };
1871 
1875  Entry () : type (array) {}
1876 
1882  Entry (const std::vector<std::string> &Path,
1883  const std::string &Name,
1884  const std::string &Value);
1885 
1889  void split_different_values ();
1890 
1894  std::vector<std::string> subsection_path;
1895 
1899  std::string entry_name;
1900 
1904  std::string entry_value;
1905 
1910  std::vector<std::string> different_values;
1911 
1916 
1921  std::size_t memory_consumption () const;
1922  };
1923 
1927  std::vector<Entry> multiple_choices;
1928 
1933  unsigned int n_branches;
1934 
1938  void init_branches ();
1939 
1946  void init_branches_current_section ();
1947 
1951  void fill_entry_values (const unsigned int run_no);
1952 };
1953 
1954 
1955 // ---------------------- inline and template functions --------------------
1956 template <class Archive>
1957 inline
1958 void
1959 ParameterHandler::save (Archive &ar, const unsigned int) const
1960 {
1961  // Forward to serialization
1962  // function in the base class.
1963  ar &static_cast<const Subscriptor &>(*this);
1964 
1965  ar & *entries.get();
1966 
1967  std::vector<std::string> descriptions;
1968 
1969  for (unsigned int j=0; j<patterns.size(); ++j)
1970  descriptions.push_back (patterns[j]->description());
1971 
1972  ar &descriptions;
1973 }
1974 
1975 
1976 template <class Archive>
1977 inline
1978 void
1979 ParameterHandler::load (Archive &ar, const unsigned int)
1980 {
1981  // Forward to serialization
1982  // function in the base class.
1983  ar &static_cast<Subscriptor &>(*this);
1984 
1985  ar & *entries.get();
1986 
1987  std::vector<std::string> descriptions;
1988  ar &descriptions;
1989 
1990  patterns.clear ();
1991  for (unsigned int j=0; j<descriptions.size(); ++j)
1992  patterns.push_back (Patterns::pattern_factory(descriptions[j]));
1993 }
1994 
1995 
1996 template <class ParameterType>
1997 void ParameterHandler::add_parameter(const std::string &entry,
1998  ParameterType &parameter,
1999  const std::string &documentation,
2000  const Patterns::PatternBase &pattern)
2001 {
2002 
2003  static_assert(std::is_const<ParameterType>::value == false,
2004  "You tried to add a parameter using a type "
2005  "that is const. Use a non-const type.");
2006 
2007  declare_entry(entry,
2009  parameter, pattern.clone()),
2010  pattern,
2011  documentation);
2012 
2013  std::string path = get_current_full_path(entry);
2014  const unsigned int pattern_index
2015  = entries->get<unsigned int> (path + path_separator + "pattern");
2016 
2017  auto action = [ &, pattern_index](const std::string &val)
2018  {
2019  parameter =
2020  Patterns::Tools::Convert<ParameterType>::to_value(val, patterns[pattern_index]->clone());
2021  };
2022  add_action(entry, action);
2023 }
2024 
2025 DEAL_II_NAMESPACE_CLOSE
2026 
2027 #endif
std::vector< Entry > multiple_choices
long int get_integer(const std::string &entry_string) const
void loop(ITERATOR begin, typename identity< ITERATOR >::type end, DOFINFO &dinfo, INFOBOX &info, const std::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &cell_worker, const std::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &boundary_worker, const std::function< void(DOFINFO &, DOFINFO &, typename INFOBOX::CellInfo &, typename INFOBOX::CellInfo &)> &face_worker, ASSEMBLER &assembler, const LoopControl &lctrl=LoopControl())
Definition: loop.h:386
static const char path_separator
#define DeclException2(Exception2, type1, type2, outsequence)
Definition: exceptions.h:358
static ::ExceptionBase & ExcEntryAlreadyExists(std::string arg1)
static std::string collate_path_string(const std::vector< std::string > &subsection_path)
static ::ExceptionBase & ExcInvalidEntryForPatternXML(std::string arg1, std::string arg2, std::string arg3)
static ::ExceptionBase & ExcCannotOpenIncludeStatementFile(int arg1, std::string arg2, std::string arg3)
std::string get_current_path() const
static ::ExceptionBase & ExcEntryUndeclared(std::string arg1)
static T to_value(const std::string &s, const std::unique_ptr< Patterns::PatternBase > &p=Convert< T >::to_pattern())=delete
virtual ~ParameterHandler()=default
static ::ExceptionBase & ExcUnbalancedSubsections(std::string arg1, std::string arg2)
std::ostream & print_parameters(std::ostream &out, const OutputStyle style) const
void log_parameters(LogStream &out)
virtual std::unique_ptr< PatternBase > clone() const =0
virtual void parse_input_from_json(std::istream &input)
void log_parameters_section(LogStream &out)
STL namespace.
std::vector< std::string > subsection_path
static ::ExceptionBase & ExcValueDoesNotMatchPattern(std::string arg1, std::string arg2)
void load(Archive &ar, const unsigned int version)
std::vector< std::string > subsection_path
void enter_subsection(const std::string &subsection)
virtual void parse_input_from_xml(std::istream &input)
static ::ExceptionBase & ExcInvalidXMLParameterFile()
static ::ExceptionBase & ExcCannotParseLine(int arg1, std::string arg2, std::string arg3)
double get_double(const std::string &entry_name) const
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:346
std::string get_current_full_path(const std::string &name) const
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:335
std::unique_ptr< boost::property_tree::ptree > entries
#define DeclException5(Exception5, type1, type2, type3, type4, type5, outsequence)
Definition: exceptions.h:394
std::unique_ptr< PatternBase > pattern_factory(const std::string &description)
Definition: patterns.cc:137
bool get_bool(const std::string &entry_name) const
virtual void parse_input_from_string(const char *s, const std::string &last_line="")
static ::ExceptionBase & ExcAlreadyAtTopLevel()
std::vector< std::unique_ptr< const Patterns::PatternBase > > patterns
void add_parameter(const std::string &entry, ParameterType &parameter, const std::string &documentation=std::string(), const Patterns::PatternBase &pattern= *Patterns::Tools::Convert< ParameterType >::to_pattern())
void add_action(const std::string &entry, const std::function< void(const std::string &value)> &action)
void print_parameters_section(std::ostream &out, const OutputStyle style, const unsigned int indent_level, const bool include_top_level_elements=false)
void save(Archive &ar, const unsigned int version) const
void declare_entry(const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation=std::string())
void recursively_print_parameters(const std::vector< std::string > &target_subsection_path, const OutputStyle style, const unsigned int indent_level, std::ostream &out) const
std::vector< std::string > different_values
std::vector< std::function< void(const std::string &)> > actions
void scan_line(std::string line, const std::string &input_filename, const unsigned int current_line_n)
#define DeclException3(Exception3, type1, type2, type3, outsequence)
Definition: exceptions.h:370
std::size_t memory_consumption() const
static ::ExceptionBase & ExcInvalidEntryForPattern(int arg1, std::string arg2, std::string arg3, std::string arg4, std::string arg5)
static ::ExceptionBase & ExcNoSubsection(int arg1, std::string arg2, std::string arg3)
void declare_alias(const std::string &existing_entry_name, const std::string &alias_name, const bool alias_is_deprecated=false)
ParameterHandler & operator=(const ParameterHandler &)=delete
virtual void parse_input(std::istream &input, const std::string &filename="input file", const std::string &last_line="")