Reference documentation for deal.II version Git 932f7faded 2020-11-28 20:02:43 +0100
\(\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\}}\)
parameter_acceptor.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------
2 //
3 // Copyright (C) 2017 - 2020 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.md at
12 // the top level directory of deal.II.
13 //
14 //-----------------------------------------------------------
15 
18 #include <deal.II/base/utilities.h>
19 
20 #include <boost/core/demangle.hpp>
21 
22 #include <fstream>
23 
25 
26 
27 // Static empty class list
28 std::vector<SmartPointer<ParameterAcceptor>> ParameterAcceptor::class_list;
29 // Static parameter handler
31 
32 ParameterAcceptor::ParameterAcceptor(const std::string &name)
33  : acceptor_id(class_list.size())
34  , section_name(name)
35 {
37  this, boost::core::demangle(typeid(*this).name()).c_str());
38  class_list.push_back(pt);
39 }
40 
41 
43 {
44  class_list[acceptor_id] = nullptr;
45 }
46 
47 std::string
49 {
50  return (!section_name.empty() ? section_name :
51  boost::core::demangle(typeid(*this).name()));
52 }
53 
54 
55 void
57  const std::string & filename,
58  const std::string & output_filename,
59  const ParameterHandler::OutputStyle output_style_for_output_filename,
61  const ParameterHandler::OutputStyle output_style_for_filename)
62 {
64  if (!filename.empty())
65  {
66  try
67  {
68  prm.parse_input(filename);
69  }
70  catch (const ::PathSearch::ExcFileNotFound &)
71  {
72  prm.print_parameters(filename, output_style_for_filename);
73  AssertThrow(false,
74  ExcMessage("You specified <" + filename + "> as input " +
75  "parameter file, but it does not exist. " +
76  "We created it for you."));
77  }
78  }
79 
80  if (!output_filename.empty())
81  prm.print_parameters(output_filename, output_style_for_output_filename);
82 
83  // Finally do the parsing.
85 }
86 
87 
88 
89 void
90 ParameterAcceptor::initialize(std::istream &input_stream, ParameterHandler &prm)
91 
92 {
93  AssertThrow(input_stream, ExcIO());
95  prm.parse_input(input_stream);
97 }
98 
99 void
101 {
102  class_list.clear();
103  prm.clear();
104 }
105 
106 
107 
108 void
110 {}
111 
112 
113 
114 void
116 {}
117 
118 
119 
120 void
122 {
123  for (const auto &instance : class_list)
124  if (instance != nullptr)
125  {
126  instance->enter_my_subsection(prm);
127  instance->parse_parameters(prm);
128  instance->parse_parameters_call_back();
129  instance->leave_my_subsection(prm);
130  }
131 }
132 
133 void
135 {
136  for (const auto &instance : class_list)
137  if (instance != nullptr)
138  {
139  instance->enter_my_subsection(prm);
140  instance->declare_parameters(prm);
141  instance->declare_parameters_call_back();
142  instance->leave_my_subsection(prm);
143  }
144 }
145 
146 
147 std::vector<std::string>
149 {
151  const auto my_section_name = get_section_name();
152  const bool is_absolute = (my_section_name.front() == sep);
153 
154  std::vector<std::string> sections =
155  Utilities::split_string_list(my_section_name, sep);
156 
157  // Split string list removes trailing empty strings, but not
158  // preceding ones. Make sure that if we had an absolute path,
159  // we don't store as first section the empty string.
160  if (is_absolute)
161  sections.erase(sections.begin());
162  else
163  {
164  // If we have a relative path, we prepend the path of the previous class
165  // to ours. This is tricky. If the previous class has a path with a
166  // trailing /, then the full path is used, else only the path except the
167  // last one
168  for (int i = acceptor_id - 1; i >= 0; --i)
169  if (class_list[i] != nullptr)
170  {
171  bool has_trailing = class_list[i]->get_section_name().back() == sep;
172  auto previous_path = class_list[i]->get_section_path();
173 
174  // See if we need to remove last piece of the path
175  if ((previous_path.size() > 0) && has_trailing == false)
176  previous_path.resize(previous_path.size() - 1);
177 
178  sections.insert(sections.begin(),
179  previous_path.begin(),
180  previous_path.end());
181  // Exit the for cycle
182  break;
183  }
184  }
185  return sections;
186 }
187 
188 void
190  ParameterHandler &prm = ParameterAcceptor::prm)
191 {
192  const auto sections = get_section_path();
193  for (const auto &sec : sections)
194  {
196  }
197 }
198 
199 void
201  ParameterHandler &prm = ParameterAcceptor::prm)
202 {
203  const auto sections = get_section_path();
204  for (unsigned int i = 0; i < sections.size(); ++i)
205  {
207  }
208 }
209 
210 
211 
std::vector< std::string > split_string_list(const std::string &s, const std::string &delimiter=",")
Definition: utilities.cc:705
const unsigned int acceptor_id
void enter_my_subsection(ParameterHandler &prm)
static ::ExceptionBase & ExcIO()
static void declare_all_parameters(ParameterHandler &prm=ParameterAcceptor::prm)
virtual void parse_parameters(ParameterHandler &prm)
std::ostream & print_parameters(std::ostream &out, const OutputStyle style) const
#define AssertThrow(cond, exc)
Definition: exceptions.h:1576
ParameterAcceptor(const std::string &section_name="")
static std::vector< SmartPointer< ParameterAcceptor > > class_list
virtual ~ParameterAcceptor() override
void enter_subsection(const std::string &subsection)
static void initialize(const std::string &filename="", const std::string &output_filename="", const ParameterHandler::OutputStyle output_style_for_output_filename=ParameterHandler::Short, ParameterHandler &prm=ParameterAcceptor::prm, const ParameterHandler::OutputStyle output_style_for_filename=ParameterHandler::DefaultStyle)
static ::ExceptionBase & ExcMessage(std::string arg1)
std::string get_section_name() const
#define Assert(cond, exc)
Definition: exceptions.h:1466
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:372
virtual void parse_input(std::istream &input, const std::string &filename="input file", const std::string &last_line="", const bool skip_undefined=false)
static const char sep
std::vector< std::string > get_section_path() const
virtual void declare_parameters(ParameterHandler &prm)
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:371
static ParameterHandler prm
static void parse_all_parameters(ParameterHandler &prm=ParameterAcceptor::prm)
const std::string section_name
void leave_my_subsection(ParameterHandler &prm)
static ::ExceptionBase & ExcInternalError()
Expression sec(const Expression &x)