Reference documentation for deal.II version 8.5.1
parameter_handler.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2016 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/std_cxx11/shared_ptr.h>
24 #include <deal.II/base/std_cxx11/unique_ptr.h>
25 
26 #include <boost/property_tree/ptree_fwd.hpp>
27 #include <boost/serialization/split_member.hpp>
28 
29 #include <map>
30 #include <vector>
31 #include <string>
32 
33 DEAL_II_NAMESPACE_OPEN
34 
35 // forward declarations for interfaces and friendship
36 class LogStream;
38 
39 
48 namespace Patterns
49 {
50 
58  {
59  public:
63  virtual ~PatternBase ();
64 
68  virtual bool match (const std::string &test_string) const = 0;
69 
76  {
93  };
94 
98  virtual std::string description (const OutputStyle style=Machine) const = 0;
99 
109  virtual PatternBase *clone () const = 0;
110 
126  virtual std::size_t memory_consumption () const;
127  };
128 
132  PatternBase *pattern_factory (const std::string &description);
133 
150  class Integer : public PatternBase
151  {
152  public:
158  static const int min_int_value;
159 
165  static const int max_int_value;
166 
174  Integer (const int lower_bound = min_int_value,
175  const int upper_bound = max_int_value);
176 
181  virtual bool match (const std::string &test_string) const;
182 
188  virtual std::string description (const OutputStyle style=Machine) const;
189 
195  virtual PatternBase *clone () const;
196 
202  static Integer *create (const std::string &description);
203 
204  private:
211  const int lower_bound;
212 
219  const int upper_bound;
220 
224  static const char *description_init;
225  };
226 
244  class Double : public PatternBase
245  {
246  public:
251  static const double min_double_value;
252 
257  static const double max_double_value;
258 
266  Double (const double lower_bound = min_double_value,
267  const double upper_bound = max_double_value);
268 
273  virtual bool match (const std::string &test_string) const;
274 
280  virtual std::string description (const OutputStyle style=Machine) const;
281 
287  virtual PatternBase *clone () const;
288 
296  static Double *create (const std::string &description);
297 
298  private:
305  const double lower_bound;
306 
313  const double upper_bound;
314 
318  static const char *description_init;
319  };
320 
330  class Selection : public PatternBase
331  {
332  public:
337  Selection (const std::string &seq);
338 
343  virtual bool match (const std::string &test_string) const;
344 
350  virtual std::string description (const OutputStyle style=Machine) const;
351 
357  virtual PatternBase *clone () const;
358 
363  std::size_t memory_consumption () const;
364 
370  static Selection *create (const std::string &description);
371 
372  private:
377  std::string sequence;
378 
382  static const char *description_init;
383  };
384 
385 
393  class List : public PatternBase
394  {
395  public:
401  static const unsigned int max_int_value;
402 
411  List (const PatternBase &base_pattern,
412  const unsigned int min_elements = 0,
413  const unsigned int max_elements = max_int_value,
414  const std::string &separator = ",");
415 
419  virtual ~List ();
420 
425  virtual bool match (const std::string &test_string) const;
426 
431  virtual std::string description (const OutputStyle style=Machine) const;
432 
438  virtual PatternBase *clone () const;
439 
445  static List *create (const std::string &description);
446 
451  std::size_t memory_consumption () const;
452 
462  int, int,
463  << "The values " << arg1 << " and " << arg2
464  << " do not form a valid range.");
466  private:
471 
475  const unsigned int min_elements;
476 
480  const unsigned int max_elements;
481 
485  const std::string separator;
486 
490  static const char *description_init;
491  };
492 
493 
507  class Map : public PatternBase
508  {
509  public:
515  static const unsigned int max_int_value;
516 
525  Map (const PatternBase &key_pattern,
526  const PatternBase &value_pattern,
527  const unsigned int min_elements = 0,
528  const unsigned int max_elements = max_int_value,
529  const std::string &separator = ",");
530 
534  virtual ~Map ();
535 
540  virtual bool match (const std::string &test_string) const;
541 
546  virtual std::string description (const OutputStyle style=Machine) const;
547 
553  virtual PatternBase *clone () const;
554 
560  static Map *create (const std::string &description);
561 
566  std::size_t memory_consumption () const;
567 
577  int, int,
578  << "The values " << arg1 << " and " << arg2
579  << " do not form a valid range.");
581  private:
587  PatternBase *value_pattern;
588 
592  const unsigned int min_elements;
593 
597  const unsigned int max_elements;
598 
602  const std::string separator;
603 
607  static const char *description_init;
608  };
609 
610 
622  {
623  public:
627  MultipleSelection (const std::string &seq);
628 
633  virtual bool match (const std::string &test_string) const;
634 
640  virtual std::string description (const OutputStyle style=Machine) const;
641 
647  virtual PatternBase *clone () const;
648 
654  static MultipleSelection *create (const std::string &description);
655 
660  std::size_t memory_consumption () const;
661 
671  int,
672  << "A comma was found at position " << arg1
673  << " of your input string, but commas are not allowed here.");
675  private:
680  std::string sequence;
681 
685  static const char *description_init;
686  };
687 
692  class Bool : public Selection
693  {
694  public:
698  Bool ();
699 
704  virtual std::string description (const OutputStyle style=Machine) const;
705 
711  virtual PatternBase *clone () const;
712 
718  static Bool *create (const std::string &description);
719 
720  private:
724  static const char *description_init;
725  };
726 
730  class Anything : public PatternBase
731  {
732  public:
737  Anything ();
738 
743  virtual bool match (const std::string &test_string) const;
744 
749  virtual std::string description (const OutputStyle style=Machine) const;
750 
756  virtual PatternBase *clone () const;
757 
763  static Anything *create (const std::string &description);
764 
765  private:
769  static const char *description_init;
770  };
771 
772 
787  class FileName : public PatternBase
788  {
789  public:
794  enum FileType
795  {
799  input = 0,
803  output = 1
804  };
805 
810  FileName (const FileType type = input);
811 
816  virtual bool match (const std::string &test_string) const;
817 
822  virtual std::string description (const OutputStyle style=Machine) const;
823 
829  virtual PatternBase *clone () const;
830 
835 
841  static FileName *create (const std::string &description);
842 
843  private:
847  static const char *description_init;
848  };
849 
850 
863  class DirectoryName : public PatternBase
864  {
865  public:
869  DirectoryName ();
870 
875  virtual bool match (const std::string &test_string) const;
876 
881  virtual std::string description (const OutputStyle style=Machine) const;
882 
888  virtual PatternBase *clone () const;
889 
895  static DirectoryName *create (const std::string &description);
896 
897  private:
901  static const char *description_init;
902  };
903 }
904 
905 
1560 {
1561 private:
1566 
1571 
1572 public:
1581  {
1586  Text = 1,
1590  LaTeX = 2,
1595 
1602  XML = 4,
1603 
1608  JSON = 5,
1609 
1615  };
1616 
1617 
1618 
1622  ParameterHandler ();
1623 
1629  virtual ~ParameterHandler ();
1630 
1645  virtual bool read_input (std::istream &input,
1646  const std::string &filename = "input file",
1647  const std::string &last_line = "") DEAL_II_DEPRECATED;
1648 
1660  virtual void parse_input (std::istream &input,
1661  const std::string &filename = "input file",
1662  const std::string &last_line = "");
1663 
1685  virtual bool read_input (const std::string &filename,
1686  const bool optional = false,
1687  const bool write_stripped_file = false,
1688  const std::string &last_line = "") DEAL_II_DEPRECATED;
1689 
1698  virtual void parse_input (const std::string &filename,
1699  const std::string &last_line = "");
1700 
1713  virtual bool read_input_from_string (const char *s,
1714  const std::string &last_line = "");
1715 
1724  virtual void parse_input_from_string (const char *s,
1725  const std::string &last_line = "");
1726 
1738  virtual bool read_input_from_xml (std::istream &input) DEAL_II_DEPRECATED;
1739 
1747  virtual void parse_input_from_xml (std::istream &input);
1748 
1752  void clear ();
1753 
1754 
1774  void declare_entry (const std::string &entry,
1775  const std::string &default_value,
1776  const Patterns::PatternBase &pattern = Patterns::Anything(),
1777  const std::string &documentation = std::string());
1778 
1822  void declare_alias (const std::string &existing_entry_name,
1823  const std::string &alias_name,
1824  const bool alias_is_deprecated = false);
1825 
1829  void enter_subsection (const std::string &subsection);
1830 
1834  void leave_subsection ();
1835 
1841  std::string get (const std::string &entry_string) const;
1842 
1848  long int get_integer (const std::string &entry_string) const;
1849 
1853  double get_double (const std::string &entry_name) const;
1854 
1860  bool get_bool (const std::string &entry_name) const;
1861 
1871  void set (const std::string &entry_name,
1872  const std::string &new_value);
1873 
1884  void set (const std::string &entry_name,
1885  const char *new_value);
1886 
1896  void set (const std::string &entry_name,
1897  const long int &new_value);
1898 
1912  void set (const std::string &entry_name,
1913  const double &new_value);
1914 
1924  void set (const std::string &entry_name,
1925  const bool &new_value);
1926 
1927 
1971  std::ostream &print_parameters (std::ostream &out,
1972  const OutputStyle style);
1973 
1988  void print_parameters_section (std::ostream &out,
1989  const OutputStyle style,
1990  const unsigned int indent_level,
1991  const bool include_top_level_elements = false);
1992 
1998  void log_parameters (LogStream &out);
1999 
2009  void log_parameters_section (LogStream &out);
2010 
2015  std::size_t memory_consumption () const;
2016 
2021  template <class Archive>
2022  void save (Archive &ar, const unsigned int version) const;
2023 
2028  template <class Archive>
2029  void load (Archive &ar, const unsigned int version);
2030 
2031  BOOST_SERIALIZATION_SPLIT_MEMBER()
2032 
2036  bool operator == (const ParameterHandler &prm2) const;
2037 
2047  std::string,
2048  << "The following entry already exists: " << arg1 << ".");
2053  std::string, std::string,
2054  << "The string <" << arg1
2055  << "> does not match the given pattern <" << arg2 << ">.");
2060  "You can't leave a subsection if you are already at the top level "
2061  "of the subsection hierarchy.");
2066  std::string,
2067  << "You can't ask for entry <" << arg1 << "> you have not yet declared.");
2068 
2076  std::string, std::string,
2077  << "There are unequal numbers of 'subsection' and 'end' "
2078  "statements in the parameter file <" << arg1 << ">."
2079  << (arg2.size() > 0 ? "\n" + arg2 : ""));
2080 
2086  int, std::string, std::string,
2087  << "Line <" << arg1 << "> of file <" << arg2 << ": There is "
2088  "no such subsection to be entered: " << arg3);
2089 
2096  int, std::string, std::string, << "Line <" << arg1 <<
2097  "> of file <" << arg2 << ">: " << arg3);
2098 
2105  int, std::string, std::string, std::string, std::string,
2106  << "Line <" << arg1 << "> of file <" << arg2 << ">:\n"
2107  " The entry value \n" << " " << arg3 << '\n' <<
2108  " for the entry named\n" << " " << arg4 << '\n' <<
2109  " does not match the given pattern:\n" << " " <<
2110  arg5);
2111 
2118  "The provided file could not be parsed as a "
2119  "ParameterHandler description.");
2120 
2127  std::string, std::string, std::string,
2128  << " The entry value \n" << " " << arg1 << '\n' <<
2129  " for the entry named\n" << " " << arg2 << '\n' <<
2130  " does not match the given pattern:\n" << " " <<
2131  arg3);
2132 
2140  int, std::string, std::string,
2141  << "Line <" << arg1 << "> of file <" << arg2 << ">: This line "
2142  "contains an 'include' or 'INCLUDE' statement, but the given "
2143  "file to include <" << arg3 << "> cannot be opened.");
2144 
2146 private:
2151  static const char path_separator = '.';
2152 
2161  std_cxx11::unique_ptr<boost::property_tree::ptree> entries;
2162 
2167  std::vector<std_cxx11::shared_ptr<const Patterns::PatternBase> > patterns;
2168 
2173  static std::string mangle (const std::string &s);
2174 
2178  static std::string demangle (const std::string &s);
2179 
2183  std::vector<std::string> subsection_path;
2184 
2190  std::string get_current_path () const;
2191 
2196  std::string get_current_full_path (const std::string &name) const;
2197 
2210  void scan_line (std::string line,
2211  const std::string &input_filename,
2212  const unsigned int current_line_n);
2213 
2214  friend class MultipleParameterLoop;
2215 };
2216 
2217 
2218 
2436 {
2437 public:
2443  {
2444  public:
2449  virtual ~UserClass ();
2450 
2455  virtual void create_new (const unsigned int run_no) = 0;
2456 
2460  virtual void run (ParameterHandler &prm) = 0;
2461  };
2462 
2467 
2472  virtual ~MultipleParameterLoop ();
2473 
2497  virtual bool read_input (std::istream &input,
2498  const std::string &filename = "input file",
2499  const std::string &last_line = "") DEAL_II_DEPRECATED;
2500 
2517  virtual void parse_input (std::istream &input,
2518  const std::string &filename = "input file",
2519  const std::string &last_line = "");
2520 
2529 
2534 
2538  void loop (UserClass &uc);
2539 
2544  std::size_t memory_consumption () const;
2545 
2546 private:
2547 
2551  class Entry
2552  {
2553  public:
2560  {
2568  array
2569  };
2570 
2574  Entry () : type (array) {}
2575 
2581  Entry (const std::vector<std::string> &Path,
2582  const std::string &Name,
2583  const std::string &Value);
2584 
2588  void split_different_values ();
2589 
2593  std::vector<std::string> subsection_path;
2594 
2598  std::string entry_name;
2599 
2603  std::string entry_value;
2604 
2609  std::vector<std::string> different_values;
2610 
2615 
2620  std::size_t memory_consumption () const;
2621  };
2622 
2626  std::vector<Entry> multiple_choices;
2627 
2632  unsigned int n_branches;
2633 
2637  void init_branches ();
2638 
2645  void init_branches_current_section ();
2646 
2650  void fill_entry_values (const unsigned int run_no);
2651 };
2652 
2653 
2654 template <class Archive>
2655 inline
2656 void
2657 ParameterHandler::save (Archive &ar, const unsigned int) const
2658 {
2659  // Forward to serialization
2660  // function in the base class.
2661  ar &static_cast<const Subscriptor &>(*this);
2662 
2663  ar & *entries.get();
2664 
2665  std::vector<std::string> descriptions;
2666 
2667  for (unsigned int j=0; j<patterns.size(); ++j)
2668  descriptions.push_back (patterns[j]->description());
2669 
2670  ar &descriptions;
2671 }
2672 
2673 
2674 template <class Archive>
2675 inline
2676 void
2677 ParameterHandler::load (Archive &ar, const unsigned int)
2678 {
2679  // Forward to serialization
2680  // function in the base class.
2681  ar &static_cast<Subscriptor &>(*this);
2682 
2683  ar & *entries.get();
2684 
2685  std::vector<std::string> descriptions;
2686  ar &descriptions;
2687 
2688  patterns.clear ();
2689  for (unsigned int j=0; j<descriptions.size(); ++j)
2690  patterns.push_back (std_cxx11::shared_ptr<const Patterns::PatternBase>(Patterns::pattern_factory(descriptions[j])));
2691 }
2692 
2693 
2694 DEAL_II_NAMESPACE_CLOSE
2695 
2696 #endif
std::vector< Entry > multiple_choices
std::size_t memory_consumption() const
long int get_integer(const std::string &entry_string) const
virtual bool match(const std::string &test_string) const
static const char path_separator
PatternBase * key_pattern
virtual bool match(const std::string &test_string) const
#define DeclException2(Exception2, type1, type2, outsequence)
Definition: exceptions.h:576
static ::ExceptionBase & ExcEntryAlreadyExists(std::string arg1)
const double upper_bound
static const char * description_init
static const char * description_init
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
virtual bool match(const std::string &test_string) const
static ::ExceptionBase & ExcEntryUndeclared(std::string arg1)
std::string get(const std::string &entry_string) const
static ::ExceptionBase & ExcUnbalancedSubsections(std::string arg1, std::string arg2)
std::size_t memory_consumption() const
const std::string separator
static Map * create(const std::string &description)
void log_parameters(LogStream &out)
static const unsigned int max_int_value
static const char * description_init
void set(const std::string &entry_name, const std::string &new_value)
virtual PatternBase * clone() const
virtual bool match(const std::string &test_string) const
void log_parameters_section(LogStream &out)
virtual std::string description(const OutputStyle style=Machine) const
STL namespace.
static Double * create(const std::string &description)
std::vector< std::string > subsection_path
static const int max_int_value
const unsigned int max_elements
static ::ExceptionBase & ExcCommasNotAllowed(int arg1)
static const char * description_init
static ::ExceptionBase & ExcValueDoesNotMatchPattern(std::string arg1, std::string arg2)
void load(Archive &ar, const unsigned int version)
std::vector< std::string > subsection_path
static const int min_int_value
static ::ExceptionBase & ExcInvalidRange(int arg1, int arg2)
const double lower_bound
virtual PatternBase * clone() const =0
void enter_subsection(const std::string &subsection)
virtual void parse_input_from_xml(std::istream &input)
FileName(const FileType type=input)
static ::ExceptionBase & ExcInvalidXMLParameterFile()
static const char * description_init
virtual PatternBase * clone() const
static const char * description_init
static Integer * create(const std::string &description)
PatternBase * pattern
static ::ExceptionBase & ExcCannotParseLine(int arg1, std::string arg2, std::string arg3)
double get_double(const std::string &entry_name) const
virtual bool read_input(std::istream &input, const std::string &filename="input file", const std::string &last_line="") 1
static const char * description_init
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:564
static Bool * create(const std::string &description)
ParameterHandler & operator=(const ParameterHandler &)
virtual std::string description(const OutputStyle style=Machine) const
virtual bool read_input_from_xml(std::istream &input) 1
virtual std::string description(const OutputStyle style=Machine) const
Double(const double lower_bound=min_double_value, const double upper_bound=max_double_value)
std::string get_current_full_path(const std::string &name) const
List(const PatternBase &base_pattern, const unsigned int min_elements=0, const unsigned int max_elements=max_int_value, const std::string &separator=",")
MultipleSelection(const std::string &seq)
virtual std::size_t memory_consumption() const
static const char * description_init
static Anything * create(const std::string &description)
static ::ExceptionBase & ExcInvalidRange(int arg1, int arg2)
Map(const PatternBase &key_pattern, const PatternBase &value_pattern, const unsigned int min_elements=0, const unsigned int max_elements=max_int_value, const std::string &separator=",")
PatternBase * pattern_factory(const std::string &description)
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:553
void loop(ITERATOR begin, typename identity< ITERATOR >::type end, DOFINFO &dinfo, INFOBOX &info, const std_cxx11::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &cell_worker, const std_cxx11::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &boundary_worker, const std_cxx11::function< void(DOFINFO &, DOFINFO &, typename INFOBOX::CellInfo &, typename INFOBOX::CellInfo &)> &face_worker, ASSEMBLER &assembler, const LoopControl &lctrl=LoopControl())
Definition: loop.h:383
static std::string mangle(const std::string &s)
virtual std::string description(const OutputStyle style=Machine) const
virtual PatternBase * clone() const
std::size_t memory_consumption() const
static const double min_double_value
virtual std::string description(const OutputStyle style=Machine) const
#define DeclException5(Exception5, type1, type2, type3, type4, type5, outsequence)
Definition: exceptions.h:612
bool get_bool(const std::string &entry_name) const
virtual bool match(const std::string &test_string) const
static const char * description_init
virtual void parse_input_from_string(const char *s, const std::string &last_line="")
static ::ExceptionBase & ExcAlreadyAtTopLevel()
virtual PatternBase * clone() const
virtual std::string description(const OutputStyle style=Machine) const
virtual bool match(const std::string &test_string) const =0
virtual PatternBase * clone() const
virtual std::string description(const OutputStyle style=Machine) const
virtual bool match(const std::string &test_string) const
static const char * description_init
std::size_t memory_consumption() const
virtual PatternBase * clone() const
static const double max_double_value
virtual std::string description(const OutputStyle style=Machine) const =0
virtual bool match(const std::string &test_string) const
virtual PatternBase * clone() const
static std::string demangle(const std::string &s)
std::ostream & print_parameters(std::ostream &out, const OutputStyle style)
Integer(const int lower_bound=min_int_value, const int upper_bound=max_int_value)
void print_parameters_section(std::ostream &out, const OutputStyle style, const unsigned int indent_level, const bool include_top_level_elements=false)
virtual PatternBase * clone() const
void save(Archive &ar, const unsigned int version) const
std_cxx11::unique_ptr< boost::property_tree::ptree > entries
void declare_entry(const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation=std::string())
Selection(const std::string &seq)
static DirectoryName * create(const std::string &description)
static FileName * create(const std::string &description)
static MultipleSelection * create(const std::string &description)
virtual bool match(const std::string &test_string) const
std::vector< std::string > different_values
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:588
const unsigned int min_elements
std::size_t memory_consumption() const
static ::ExceptionBase & ExcInvalidEntryForPattern(int arg1, std::string arg2, std::string arg3, std::string arg4, std::string arg5)
static List * create(const std::string &description)
virtual bool read_input_from_string(const char *s, const std::string &last_line="")
static const unsigned int max_int_value
static Selection * create(const std::string &description)
virtual PatternBase * clone() const
static ::ExceptionBase & ExcNoSubsection(int arg1, std::string arg2, std::string arg3)
const std::string separator
virtual std::string description(const OutputStyle style=Machine) const
void declare_alias(const std::string &existing_entry_name, const std::string &alias_name, const bool alias_is_deprecated=false)
const unsigned int max_elements
virtual std::string description(const OutputStyle style=Machine) const
virtual bool match(const std::string &test_string) const
const unsigned int min_elements
virtual std::string description(const OutputStyle style=Machine) const
std::vector< std_cxx11::shared_ptr< const Patterns::PatternBase > > patterns
virtual PatternBase * clone() const
virtual void parse_input(std::istream &input, const std::string &filename="input file", const std::string &last_line="")