Reference documentation for deal.II version GIT relicensing-14-gd371fb467a 2024-02-26 20:50:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
parameter_handler.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 1998 - 2023 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#ifndef dealii_parameter_handler_h
16#define dealii_parameter_handler_h
17
18
19#include <deal.II/base/config.h>
20
24
25#include <boost/archive/basic_archive.hpp>
26#include <boost/property_tree/ptree_fwd.hpp>
27#include <boost/property_tree/ptree_serialization.hpp>
28#include <boost/serialization/split_member.hpp>
29
30#include <map>
31#include <memory>
32#include <string>
33#include <vector>
34
36
37// forward declarations for interfaces and friendship
38#ifndef DOXYGEN
39class LogStream;
41#endif
42
855{
856public:
873 {
878 DefaultStyle = 0x0000,
879
884 Short = 0x0001,
885
890
895 PRM = 0x0010,
896
904
908 LaTeX = 0x0020,
909
915 Description = 0x0040,
916
924 XML = 0x0080,
925
931 JSON = 0x0100,
932
938
946
952
958
964 };
965
966
967
972
978 virtual ~ParameterHandler() override = default;
979
984
989 operator=(const ParameterHandler &) = delete;
990
1025 virtual void
1026 parse_input(std::istream &input,
1027 const std::string &filename = "input file",
1028 const std::string &last_line = "",
1029 const bool skip_undefined = false);
1030
1052 virtual void
1053 parse_input(const std::string &filename,
1054 const std::string &last_line = "",
1055 const bool skip_undefined = false,
1056 const bool assert_mandatory_entries_are_found = false);
1057
1066 virtual void
1067 parse_input_from_string(const std::string &s,
1068 const std::string &last_line = "",
1069 const bool skip_undefined = false);
1070
1078 virtual void
1079 parse_input_from_xml(std::istream &input, const bool skip_undefined = false);
1080
1088 virtual void
1089 parse_input_from_json(std::istream &input, const bool skip_undefined = false);
1090
1094 void
1095 clear();
1096
1097
1123 void
1124 declare_entry(const std::string &entry,
1125 const std::string &default_value,
1126 const Patterns::PatternBase &pattern = Patterns::Anything(),
1127 const std::string &documentation = "",
1128 const bool has_to_be_set = false);
1129
1177 void
1178 add_action(const std::string &entry,
1179 const std::function<void(const std::string &value)> &action,
1180 const bool execute_action = true);
1181
1197 template <typename ParameterType>
1198 void
1199 add_parameter(const std::string &entry,
1200 ParameterType &parameter,
1201 const std::string &documentation = "",
1202 const Patterns::PatternBase &pattern =
1204 const bool has_to_be_set = false);
1205
1249 void
1250 declare_alias(const std::string &existing_entry_name,
1251 const std::string &alias_name,
1252 const bool alias_is_deprecated = false);
1253
1257 void
1258 enter_subsection(const std::string &subsection);
1259
1263 void
1265
1271 bool
1272 subsection_path_exists(const std::vector<std::string> &sub_path) const;
1273
1279 std::string
1280 get(const std::string &entry_string) const;
1281
1292 std::string
1293 get(const std::vector<std::string> &entry_subsection_path,
1294 const std::string &entry_string) const;
1295
1301 long int
1302 get_integer(const std::string &entry_string) const;
1303
1312 long int
1313 get_integer(const std::vector<std::string> &entry_subsection_path,
1314 const std::string &entry_string) const;
1315
1319 double
1320 get_double(const std::string &entry_name) const;
1321
1328 double
1329 get_double(const std::vector<std::string> &entry_subsection_path,
1330 const std::string &entry_string) const;
1336 bool
1337 get_bool(const std::string &entry_name) const;
1338
1347 bool
1348 get_bool(const std::vector<std::string> &entry_subsection_path,
1349 const std::string &entry_string) const;
1350
1360 void
1361 set(const std::string &entry_name, const std::string &new_value);
1362
1373 void
1374 set(const std::string &entry_name, const char *new_value);
1375
1385 void
1386 set(const std::string &entry_name, const long int new_value);
1387
1401 void
1402 set(const std::string &entry_name, const double new_value);
1403
1413 void
1414 set(const std::string &entry_name, const bool new_value);
1415
1488 std::ostream &
1489 print_parameters(std::ostream &out, const OutputStyle style) const;
1490
1491
1492
1513 void
1514 print_parameters(const std::string &filename,
1515 const OutputStyle style = DefaultStyle) const;
1516
1531 void
1532 log_parameters(LogStream &out, const OutputStyle style = DefaultStyle);
1533
1552 void
1554 const OutputStyle style = DefaultStyle);
1555
1560 std::size_t
1561 memory_consumption() const;
1562
1568 template <class Archive>
1569 void
1570 save(Archive &ar, const unsigned int version) const;
1571
1577 template <class Archive>
1578 void
1579 load(Archive &ar, const unsigned int version);
1580
1581#ifdef DOXYGEN
1587 template <class Archive>
1588 void
1589 serialize(Archive &archive, const unsigned int version);
1590#else
1591 // This macro defines the serialize() method that is compatible with
1592 // the templated save() and load() method that have been implemented.
1593 BOOST_SERIALIZATION_SPLIT_MEMBER()
1594#endif
1595
1599 bool
1600 operator==(const ParameterHandler &prm2) const;
1601
1610 std::set<std::string>
1612
1618 void
1620
1630 std::string,
1631 << "The following entry already exists: " << arg1 << '.');
1636 std::string,
1637 std::string,
1638 << "The string <" << arg1
1639 << "> does not match the given pattern <" << arg2 << ">.");
1645 "You can't leave a subsection if you are already at the top level "
1646 "of the subsection hierarchy.");
1651 std::string,
1652 << "You can't ask for entry <" << arg1
1653 << "> you have not yet declared.");
1654
1662 std::string,
1663 std::string,
1664 << "There are unequal numbers of 'subsection' and 'end' "
1665 "statements in the parameter file <"
1666 << arg1 << ">." << (arg2.size() > 0 ? "\n" + arg2 : ""));
1667
1673 int,
1674 std::string,
1675 std::string,
1676 << "Line <" << arg1 << "> of file <" << arg2
1677 << ">: You are trying to enter a subsection '" << arg3
1678 << "', but the ParameterHandler object does "
1679 << "not know of any such subsection.");
1680
1687 int,
1688 std::string,
1689 std::string,
1690 << "Line <" << arg1 << "> of file <" << arg2 << ">: " << arg3);
1691
1698 int,
1699 std::string,
1700 std::string,
1701 std::string,
1702 std::string,
1703 << "Line <" << arg1 << "> of file <" << arg2
1704 << ">:\n"
1705 " The entry value \n"
1706 << " " << arg3 << '\n'
1707 << " for the entry named\n"
1708 << " " << arg4 << '\n'
1709 << " does not match the given pattern:\n"
1710 << " " << arg5);
1711
1718 "The provided file could not be parsed as a "
1719 "ParameterHandler description.");
1720
1729 int,
1730 std::string,
1731 std::string,
1732 << "Line <" << arg1 << "> of file <" << arg2
1733 << ">: This line "
1734 "contains an 'include' or 'INCLUDE' statement, but the given "
1735 "file to include <"
1736 << arg3 << "> cannot be opened.");
1737
1740private:
1745 static const char path_separator = '.';
1746
1750 std::vector<std::string> subsection_path;
1751
1760 std::unique_ptr<boost::property_tree::ptree> entries;
1761
1770 std::map<std::string, std::pair<bool, bool>> entries_set_status;
1771
1777 std::vector<std::unique_ptr<const Patterns::PatternBase>> patterns;
1778
1785 std::vector<std::function<void(const std::string &)>> actions;
1786
1795 std::string
1796 get_current_path() const;
1797
1802 std::string
1803 get_current_full_path(const std::string &name) const;
1804
1809 std::string
1810 get_current_full_path(const std::vector<std::string> &sub_path,
1811 const std::string &name) const;
1812
1831 void
1832 scan_line(std::string line,
1833 const std::string &input_filename,
1834 const unsigned int current_line_n,
1835 const bool skip_undefined);
1836
1848 void
1850 const boost::property_tree::ptree &tree,
1851 const std::vector<std::string> &target_subsection_path,
1853 const unsigned int indent_level,
1854 std::ostream &out) const;
1855
1857};
1858
1869{
1870 return static_cast<ParameterHandler::OutputStyle>(
1871 static_cast<unsigned int>(f1) | static_cast<unsigned int>(f2));
1872}
1873
2090{
2091public:
2097 {
2098 public:
2103 virtual ~UserClass() = default;
2104
2109 virtual void
2110 create_new(const unsigned int run_no) = 0;
2111
2115 virtual void
2117 };
2118
2123
2128 virtual ~MultipleParameterLoop() override = default;
2129
2151 virtual void
2152 parse_input(std::istream &input,
2153 const std::string &filename = "input file",
2154 const std::string &last_line = "",
2155 const bool skip_undefined = false) override;
2156
2165
2169 void
2170 loop(UserClass &uc);
2171
2176 std::size_t
2177 memory_consumption() const;
2178
2179private:
2183 class Entry
2184 {
2185 public:
2202
2207 : type(array)
2208 {}
2209
2215 Entry(const std::vector<std::string> &Path,
2216 const std::string &Name,
2217 const std::string &Value);
2218
2222 void
2224
2228 std::vector<std::string> subsection_path;
2229
2233 std::string entry_name;
2234
2238 std::string entry_value;
2239
2244 std::vector<std::string> different_values;
2245
2250
2255 std::size_t
2256 memory_consumption() const;
2257 };
2258
2262 std::vector<Entry> multiple_choices;
2263
2268 unsigned int n_branches;
2269
2273 void
2274 init_branches();
2275
2282 void
2284
2288 void
2289 fill_entry_values(const unsigned int run_no);
2290};
2291
2292
2293// ---------------------- inline and template functions --------------------
2294template <class Archive>
2295inline void
2296ParameterHandler::save(Archive &ar, const unsigned int) const
2297{
2298 // Forward to serialization
2299 // function in the base class.
2300 ar &static_cast<const Subscriptor &>(*this);
2301
2302 ar &*entries.get();
2303
2304 std::vector<std::string> descriptions;
2305
2306 descriptions.reserve(patterns.size());
2307 for (const auto &pattern : patterns)
2308 descriptions.push_back(pattern->description());
2309
2310 ar &descriptions;
2311}
2312
2313
2314template <class Archive>
2315inline void
2316ParameterHandler::load(Archive &ar, const unsigned int)
2317{
2318 // Forward to serialization
2319 // function in the base class.
2320 ar &static_cast<Subscriptor &>(*this);
2321
2322 ar &*entries.get();
2323
2324 std::vector<std::string> descriptions;
2325 ar &descriptions;
2326
2327 patterns.clear();
2328 for (const auto &description : descriptions)
2329 patterns.push_back(Patterns::pattern_factory(description));
2330}
2331
2332
2333template <typename ParameterType>
2334void
2335ParameterHandler::add_parameter(const std::string &entry,
2336 ParameterType &parameter,
2337 const std::string &documentation,
2338 const Patterns::PatternBase &pattern,
2339 const bool has_to_be_set)
2340{
2341 static_assert(std::is_const_v<ParameterType> == false,
2342 "You tried to add a parameter using a type "
2343 "that is const. Use a non-const type.");
2344
2345 declare_entry(entry,
2347 pattern),
2348 pattern,
2349 documentation,
2350 has_to_be_set);
2351
2352 std::string path = get_current_full_path(entry);
2353 const unsigned int pattern_index =
2354 entries->get<unsigned int>(path + path_separator + "pattern");
2355
2356 auto action = [&, pattern_index](const std::string &val) {
2358 val, *patterns[pattern_index]);
2359 };
2360 add_action(entry, action, false);
2361}
2362
2364
2365#endif
std::vector< std::string > subsection_path
std::vector< std::string > different_values
std::size_t memory_consumption() const
virtual void create_new(const unsigned int run_no)=0
virtual void run(ParameterHandler &prm)=0
void fill_entry_values(const unsigned int run_no)
virtual void parse_input(std::istream &input, const std::string &filename="input file", const std::string &last_line="", const bool skip_undefined=false) override
std::size_t memory_consumption() const
virtual ~MultipleParameterLoop() override=default
void loop(UserClass &uc)
std::vector< Entry > multiple_choices
static const char path_separator
std::size_t memory_consumption() const
void add_parameter(const std::string &entry, ParameterType &parameter, const std::string &documentation="", const Patterns::PatternBase &pattern= *Patterns::Tools::Convert< ParameterType >::to_pattern(), const bool has_to_be_set=false)
void load(Archive &ar, const unsigned int version)
virtual void parse_input(std::istream &input, const std::string &filename="input file", const std::string &last_line="", const bool skip_undefined=false)
virtual void parse_input_from_xml(std::istream &input, const bool skip_undefined=false)
std::string get_current_path() const
ParameterHandler(const ParameterHandler &)=delete
std::vector< std::unique_ptr< const Patterns::PatternBase > > patterns
std::ostream & print_parameters(std::ostream &out, const OutputStyle style) const
void recursively_print_parameters(const boost::property_tree::ptree &tree, const std::vector< std::string > &target_subsection_path, const ParameterHandler::OutputStyle style, const unsigned int indent_level, std::ostream &out) const
void add_action(const std::string &entry, const std::function< void(const std::string &value)> &action, const bool execute_action=true)
void save(Archive &ar, const unsigned int version) const
long int get_integer(const std::string &entry_string) const
bool get_bool(const std::string &entry_name) const
void declare_entry(const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation="", const bool has_to_be_set=false)
virtual void parse_input_from_string(const std::string &s, const std::string &last_line="", const bool skip_undefined=false)
std::string get(const std::string &entry_string) const
std::set< std::string > get_entries_wrongly_not_set() const
void set(const std::string &entry_name, const std::string &new_value)
void log_parameters(LogStream &out, const OutputStyle style=DefaultStyle)
void serialize(Archive &archive, const unsigned int version)
std::map< std::string, std::pair< bool, bool > > entries_set_status
std::string get_current_full_path(const std::string &name) const
ParameterHandler & operator=(const ParameterHandler &)=delete
std::vector< std::function< void(const std::string &)> > actions
void log_parameters_section(LogStream &out, const OutputStyle style=DefaultStyle)
std::unique_ptr< boost::property_tree::ptree > entries
bool subsection_path_exists(const std::vector< std::string > &sub_path) const
virtual ~ParameterHandler() override=default
void scan_line(std::string line, const std::string &input_filename, const unsigned int current_line_n, const bool skip_undefined)
double get_double(const std::string &entry_name) const
void declare_alias(const std::string &existing_entry_name, const std::string &alias_name, const bool alias_is_deprecated=false)
bool operator==(const ParameterHandler &prm2) const
std::vector< std::string > subsection_path
void enter_subsection(const std::string &subsection)
void assert_that_entries_have_been_set() const
virtual void parse_input_from_json(std::istream &input, const bool skip_undefined=false)
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:502
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:503
static ::ExceptionBase & ExcNoSubsection(int arg1, std::string arg2, std::string arg3)
static ::ExceptionBase & ExcCannotOpenIncludeStatementFile(int arg1, std::string arg2, std::string arg3)
static ::ExceptionBase & ExcEntryAlreadyExists(std::string arg1)
static ::ExceptionBase & ExcAlreadyAtTopLevel()
#define DeclException2(Exception2, type1, type2, outsequence)
Definition exceptions.h:539
static ::ExceptionBase & ExcInvalidEntryForPattern(int arg1, std::string arg2, std::string arg3, std::string arg4, std::string arg5)
static ::ExceptionBase & ExcEntryUndeclared(std::string arg1)
#define DeclExceptionMsg(Exception, defaulttext)
Definition exceptions.h:494
static ::ExceptionBase & ExcValueDoesNotMatchPattern(std::string arg1, std::string arg2)
static ::ExceptionBase & ExcUnbalancedSubsections(std::string arg1, std::string arg2)
#define DeclException3(Exception3, type1, type2, type3, outsequence)
Definition exceptions.h:562
#define DeclException1(Exception1, type1, outsequence)
Definition exceptions.h:516
static ::ExceptionBase & ExcCannotParseLine(int arg1, std::string arg2, std::string arg3)
static ::ExceptionBase & ExcInvalidXMLParameterFile()
#define DeclException5( Exception5, type1, type2, type3, type4, type5, outsequence)
Definition exceptions.h:611
std::unique_ptr< PatternBase > pattern_factory(const std::string &description)
Definition patterns.cc:137
ParameterHandler::OutputStyle operator|(const ParameterHandler::OutputStyle f1, const ParameterHandler::OutputStyle f2)
static T to_value(const std::string &s, const Patterns::PatternBase &p= *Convert< T >::to_pattern())=delete