Reference documentation for deal.II version 8.4.2
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 
73  virtual std::string description () const = 0;
74 
84  virtual PatternBase *clone () const = 0;
85 
101  virtual std::size_t memory_consumption () const;
102  };
103 
107  PatternBase *pattern_factory (const std::string &description);
108 
126  class Integer : public PatternBase
127  {
128  public:
134  static const int min_int_value;
135 
141  static const int max_int_value;
142 
149  Integer (const int lower_bound = min_int_value,
150  const int upper_bound = max_int_value);
151 
156  virtual bool match (const std::string &test_string) const;
157 
163  virtual std::string description () const;
164 
170  virtual PatternBase *clone () const;
171 
177  static Integer *create (const std::string &description);
178 
179  private:
186  const int lower_bound;
187 
194  const int upper_bound;
195 
199  static const char *description_init;
200  };
201 
220  class Double : public PatternBase
221  {
222  public:
228  static const double min_double_value;
229 
235  static const double max_double_value;
236 
243  Double (const double lower_bound = min_double_value,
244  const double upper_bound = max_double_value);
245 
250  virtual bool match (const std::string &test_string) const;
251 
257  virtual std::string description () const;
258 
264  virtual PatternBase *clone () const;
265 
271  static Double *create (const std::string &description);
272 
273  private:
280  const double lower_bound;
281 
288  const double upper_bound;
289 
293  static const char *description_init;
294  };
295 
305  class Selection : public PatternBase
306  {
307  public:
312  Selection (const std::string &seq);
313 
318  virtual bool match (const std::string &test_string) const;
319 
325  virtual std::string description () const;
326 
332  virtual PatternBase *clone () const;
333 
338  std::size_t memory_consumption () const;
339 
345  static Selection *create (const std::string &description);
346 
347  private:
352  std::string sequence;
353 
357  static const char *description_init;
358  };
359 
360 
368  class List : public PatternBase
369  {
370  public:
376  static const unsigned int max_int_value;
377 
386  List (const PatternBase &base_pattern,
387  const unsigned int min_elements = 0,
388  const unsigned int max_elements = max_int_value,
389  const std::string &separator = ",");
390 
394  virtual ~List ();
395 
400  virtual bool match (const std::string &test_string) const;
401 
406  virtual std::string description () const;
407 
413  virtual PatternBase *clone () const;
414 
420  static List *create (const std::string &description);
421 
426  std::size_t memory_consumption () const;
427 
436  DeclException2 (ExcInvalidRange,
437  int, int,
438  << "The values " << arg1 << " and " << arg2
439  << " do not form a valid range.");
441  private:
446 
450  const unsigned int min_elements;
451 
455  const unsigned int max_elements;
456 
460  const std::string separator;
461 
465  static const char *description_init;
466  };
467 
468 
482  class Map : public PatternBase
483  {
484  public:
490  static const unsigned int max_int_value;
491 
500  Map (const PatternBase &key_pattern,
501  const PatternBase &value_pattern,
502  const unsigned int min_elements = 0,
503  const unsigned int max_elements = max_int_value,
504  const std::string &separator = ",");
505 
509  virtual ~Map ();
510 
515  virtual bool match (const std::string &test_string) const;
516 
521  virtual std::string description () const;
522 
528  virtual PatternBase *clone () const;
529 
535  static Map *create (const std::string &description);
536 
541  std::size_t memory_consumption () const;
542 
551  DeclException2 (ExcInvalidRange,
552  int, int,
553  << "The values " << arg1 << " and " << arg2
554  << " do not form a valid range.");
556  private:
562  PatternBase *value_pattern;
563 
567  const unsigned int min_elements;
568 
572  const unsigned int max_elements;
573 
577  const std::string separator;
578 
582  static const char *description_init;
583  };
584 
585 
597  {
598  public:
602  MultipleSelection (const std::string &seq);
603 
608  virtual bool match (const std::string &test_string) const;
609 
615  virtual std::string description () const;
616 
622  virtual PatternBase *clone () const;
623 
629  static MultipleSelection *create (const std::string &description);
630 
635  std::size_t memory_consumption () const;
636 
645  DeclException1 (ExcCommasNotAllowed,
646  int,
647  << "A comma was found at position " << arg1
648  << " of your input string, but commas are not allowed here.");
650  private:
655  std::string sequence;
656 
660  static const char *description_init;
661  };
662 
667  class Bool : public Selection
668  {
669  public:
673  Bool ();
674 
679  virtual std::string description () const;
680 
686  virtual PatternBase *clone () const;
687 
693  static Bool *create (const std::string &description);
694 
695  private:
699  static const char *description_init;
700  };
701 
705  class Anything : public PatternBase
706  {
707  public:
712  Anything ();
713 
718  virtual bool match (const std::string &test_string) const;
719 
724  virtual std::string description () const;
725 
731  virtual PatternBase *clone () const;
732 
738  static Anything *create (const std::string &description);
739 
740  private:
744  static const char *description_init;
745  };
746 
747 
762  class FileName : public PatternBase
763  {
764  public:
769  enum FileType {input = 0, output = 1};
770 
775  FileName (const FileType type = input);
776 
781  virtual bool match (const std::string &test_string) const;
782 
787  virtual std::string description () const;
788 
794  virtual PatternBase *clone () const;
795 
800 
806  static FileName *create (const std::string &description);
807 
808  private:
812  static const char *description_init;
813  };
814 
815 
828  class DirectoryName : public PatternBase
829  {
830  public:
834  DirectoryName ();
835 
840  virtual bool match (const std::string &test_string) const;
841 
846  virtual std::string description () const;
847 
853  virtual PatternBase *clone () const;
854 
860  static DirectoryName *create (const std::string &description);
861 
862  private:
866  static const char *description_init;
867  };
868 }
869 
870 
1502 {
1503 private:
1508 
1512  ParameterHandler &operator= (const ParameterHandler &);
1513 
1514 public:
1523  {
1528  Text = 1,
1532  LaTeX = 2,
1536  Description = 3,
1537 
1544  XML = 4,
1545 
1550  JSON = 5,
1551 
1556  ShortText = 193
1557  };
1558 
1559 
1560 
1564  ParameterHandler ();
1565 
1571  virtual ~ParameterHandler ();
1572 
1581  virtual bool read_input (std::istream &input,
1582  const std::string &filename = "input file");
1583 
1595  virtual bool read_input (const std::string &filename,
1596  const bool optional = false,
1597  const bool write_stripped_file = false);
1598 
1605  virtual bool read_input_from_string (const char *s);
1606 
1616  virtual bool read_input_from_xml (std::istream &input);
1617 
1621  void clear ();
1622 
1623 
1643  void declare_entry (const std::string &entry,
1644  const std::string &default_value,
1645  const Patterns::PatternBase &pattern = Patterns::Anything(),
1646  const std::string &documentation = std::string());
1647 
1691  void declare_alias (const std::string &existing_entry_name,
1692  const std::string &alias_name,
1693  const bool alias_is_deprecated = false);
1694 
1698  void enter_subsection (const std::string &subsection);
1699 
1703  void leave_subsection ();
1704 
1710  std::string get (const std::string &entry_string) const;
1711 
1717  long int get_integer (const std::string &entry_string) const;
1718 
1722  double get_double (const std::string &entry_name) const;
1723 
1729  bool get_bool (const std::string &entry_name) const;
1730 
1740  void set (const std::string &entry_name,
1741  const std::string &new_value);
1742 
1753  void set (const std::string &entry_name,
1754  const char *new_value);
1755 
1765  void set (const std::string &entry_name,
1766  const long int &new_value);
1767 
1781  void set (const std::string &entry_name,
1782  const double &new_value);
1783 
1793  void set (const std::string &entry_name,
1794  const bool &new_value);
1795 
1796 
1840  std::ostream &print_parameters (std::ostream &out,
1841  const OutputStyle style);
1842 
1857  void print_parameters_section (std::ostream &out,
1858  const OutputStyle style,
1859  const unsigned int indent_level,
1860  const bool include_top_level_elements = false);
1861 
1867  void log_parameters (LogStream &out);
1868 
1878  void log_parameters_section (LogStream &out);
1879 
1884  std::size_t memory_consumption () const;
1885 
1890  template <class Archive>
1891  void save (Archive &ar, const unsigned int version) const;
1892 
1897  template <class Archive>
1898  void load (Archive &ar, const unsigned int version);
1899 
1900  BOOST_SERIALIZATION_SPLIT_MEMBER()
1901 
1902 
1905  bool operator == (const ParameterHandler &prm2) const;
1906 
1915  DeclException1 (ExcEntryAlreadyExists,
1916  std::string,
1917  << "The following entry already exists: " << arg1);
1921  DeclException2 (ExcValueDoesNotMatchPattern,
1922  std::string, std::string,
1923  << "The string <" << arg1
1924  << "> does not match the given pattern <" << arg2 << ">");
1928  DeclException0 (ExcAlreadyAtTopLevel);
1932  DeclException1 (ExcEntryUndeclared,
1933  std::string,
1934  << "You can't ask for entry <" << arg1 << "> you have not yet declared");
1936 private:
1941  static const char path_separator = '.';
1942 
1951  std_cxx11::unique_ptr<boost::property_tree::ptree> entries;
1952 
1957  std::vector<std_cxx11::shared_ptr<const Patterns::PatternBase> > patterns;
1958 
1963  static std::string mangle (const std::string &s);
1964 
1968  static std::string demangle (const std::string &s);
1969 
1973  std::vector<std::string> subsection_path;
1974 
1980  std::string get_current_path () const;
1981 
1986  std::string get_current_full_path (const std::string &name) const;
1987 
2001  bool scan_line (std::string line,
2002  const std::string &input_filename,
2003  const unsigned int current_line_n);
2004 
2005  friend class MultipleParameterLoop;
2006 };
2007 
2008 
2009 
2226 class MultipleParameterLoop : public ParameterHandler
2227 {
2228 public:
2234  {
2235  public:
2240  virtual ~UserClass ();
2241 
2246  virtual void create_new (const unsigned int run_no) = 0;
2247 
2251  virtual void run (ParameterHandler &prm) = 0;
2252  };
2253 
2257  MultipleParameterLoop ();
2258 
2263  virtual ~MultipleParameterLoop ();
2264 
2278  virtual bool read_input (std::istream &input,
2279  const std::string &filename = "input file");
2280 
2289 
2293  void loop (UserClass &uc);
2294 
2299  std::size_t memory_consumption () const;
2300 
2301 private:
2302 
2306  class Entry
2307  {
2308  public:
2315  {
2316  variant, array
2317  };
2318 
2322  Entry () : type (array) {}
2323 
2329  Entry (const std::vector<std::string> &Path,
2330  const std::string &Name,
2331  const std::string &Value);
2332 
2336  void split_different_values ();
2337 
2341  std::vector<std::string> subsection_path;
2342 
2346  std::string entry_name;
2347 
2351  std::string entry_value;
2352 
2357  std::vector<std::string> different_values;
2358 
2363 
2368  std::size_t memory_consumption () const;
2369  };
2370 
2374  std::vector<Entry> multiple_choices;
2375 
2380  unsigned int n_branches;
2381 
2385  void init_branches ();
2386 
2393  void init_branches_current_section ();
2394 
2398  void fill_entry_values (const unsigned int run_no);
2399 };
2400 
2401 
2402 template <class Archive>
2403 inline
2404 void
2405 ParameterHandler::save (Archive &ar, const unsigned int) const
2406 {
2407  // Forward to serialization
2408  // function in the base class.
2409  ar &static_cast<const Subscriptor &>(*this);
2410 
2411  ar & *entries.get();
2412 
2413  std::vector<std::string> descriptions;
2414 
2415  for (unsigned int j=0; j<patterns.size(); ++j)
2416  descriptions.push_back (patterns[j]->description());
2417 
2418  ar &descriptions;
2419 }
2420 
2421 
2422 template <class Archive>
2423 inline
2424 void
2425 ParameterHandler::load (Archive &ar, const unsigned int)
2426 {
2427  // Forward to serialization
2428  // function in the base class.
2429  ar &static_cast<Subscriptor &>(*this);
2430 
2431  ar & *entries.get();
2432 
2433  std::vector<std::string> descriptions;
2434  ar &descriptions;
2435 
2436  patterns.clear ();
2437  for (unsigned int j=0; j<descriptions.size(); ++j)
2438  patterns.push_back (std_cxx11::shared_ptr<const Patterns::PatternBase>(Patterns::pattern_factory(descriptions[j])));
2439 }
2440 
2441 
2442 DEAL_II_NAMESPACE_CLOSE
2443 
2444 #endif
std::vector< Entry > multiple_choices
PatternBase * key_pattern
#define DeclException2(Exception2, type1, type2, outsequence)
Definition: exceptions.h:552
const double upper_bound
static const char * description_init
static const char * description_init
virtual std::string description() const =0
const std::string separator
static const unsigned int max_int_value
static const char * description_init
STL namespace.
std::vector< std::string > subsection_path
static const int max_int_value
const unsigned int max_elements
static const char * description_init
void load(Archive &ar, const unsigned int version)
static const int min_int_value
const double lower_bound
virtual PatternBase * clone() const =0
static const char * description_init
static const char * description_init
PatternBase * pattern
static const char * description_init
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:542
virtual std::size_t memory_consumption() const
static const char * description_init
PatternBase * pattern_factory(const std::string &description)
#define DeclException0(Exception0)
Definition: exceptions.h:522
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:370
static const double min_double_value
virtual bool read_input(std::istream &input, const std::string &filename="input file")
static const char * description_init
virtual bool match(const std::string &test_string) const =0
static const char * description_init
static const double max_double_value
void save(Archive &ar, const unsigned int version) const
std::vector< std::string > different_values
const unsigned int min_elements
static const unsigned int max_int_value
const std::string separator
const unsigned int max_elements
const unsigned int min_elements