Reference documentation for deal.II version 9.2.0
\(\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,
60  ParameterHandler & prm,
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
191 {
192  const auto sections = get_section_path();
193  for (const auto &sec : sections)
194  {
196  }
197 }
198 
199 void
202 {
203  const auto sections = get_section_path();
204  for (unsigned int i = 0; i < sections.size(); ++i)
205  {
207  }
208 }
209 
210 
211 
ParameterHandler::OutputStyle
OutputStyle
Definition: parameter_handler.h:863
ParameterAcceptor::parse_all_parameters
static void parse_all_parameters(ParameterHandler &prm=ParameterAcceptor::prm)
Definition: parameter_acceptor.cc:121
ParameterAcceptor::parse_parameters
virtual void parse_parameters(ParameterHandler &prm)
Definition: parameter_acceptor.cc:115
StandardExceptions::ExcIO
static ::ExceptionBase & ExcIO()
ParameterAcceptor::enter_my_subsection
void enter_my_subsection(ParameterHandler &prm)
Definition: parameter_acceptor.cc:189
utilities.h
ParameterAcceptor::class_list
static std::vector< SmartPointer< ParameterAcceptor > > class_list
Definition: parameter_acceptor.h:539
ParameterHandler::clear
void clear()
Definition: parameter_handler.cc:775
ParameterAcceptor::declare_all_parameters
static void declare_all_parameters(ParameterHandler &prm=ParameterAcceptor::prm)
Definition: parameter_acceptor.cc:134
ParameterAcceptor::clear
static void clear()
Definition: parameter_acceptor.cc:100
ParameterAcceptor::ParameterAcceptor
ParameterAcceptor(const std::string &section_name="")
Definition: parameter_acceptor.cc:32
parameter_acceptor.h
ParameterAcceptor::initialize
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)
Definition: parameter_acceptor.cc:56
StandardExceptions::ExcMessage
static ::ExceptionBase & ExcMessage(std::string arg1)
DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
ParameterAcceptor::~ParameterAcceptor
virtual ~ParameterAcceptor() override
Definition: parameter_acceptor.cc:42
ParameterHandler::print_parameters
std::ostream & print_parameters(std::ostream &out, const OutputStyle style) const
Definition: parameter_handler.cc:1238
ParameterAcceptor::get_section_name
std::string get_section_name() const
Definition: parameter_acceptor.cc:48
ParameterAcceptor::get_section_path
std::vector< std::string > get_section_path() const
Definition: parameter_acceptor.cc:148
ParameterAcceptor::prm
static ParameterHandler prm
Definition: parameter_acceptor.h:519
ParameterAcceptor::sep
static const char sep
Definition: parameter_acceptor.h:547
SmartPointer< ParameterAcceptor >
ParameterAcceptor::declare_parameters
virtual void declare_parameters(ParameterHandler &prm)
Definition: parameter_acceptor.cc:109
StandardExceptions::ExcInternalError
static ::ExceptionBase & ExcInternalError()
Utilities::split_string_list
std::vector< std::string > split_string_list(const std::string &s, const std::string &delimiter=",")
Definition: utilities.cc:705
Assert
#define Assert(cond, exc)
Definition: exceptions.h:1419
ParameterAcceptor::section_name
const std::string section_name
Definition: parameter_acceptor.h:551
path_search.h
ParameterHandler
Definition: parameter_handler.h:845
ParameterHandler::enter_subsection
void enter_subsection(const std::string &subsection)
Definition: parameter_handler.cc:927
DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
ParameterHandler::leave_subsection
void leave_subsection()
Definition: parameter_handler.cc:941
AssertThrow
#define AssertThrow(cond, exc)
Definition: exceptions.h:1531
ParameterAcceptor::leave_my_subsection
void leave_my_subsection(ParameterHandler &prm)
Definition: parameter_acceptor.cc:200
ParameterHandler::parse_input
virtual void parse_input(std::istream &input, const std::string &filename="input file", const std::string &last_line="", const bool skip_undefined=false)
Definition: parameter_handler.cc:399
Differentiation::SD::sec
Expression sec(const Expression &x)
Definition: symengine_math.cc:119
ParameterAcceptor::acceptor_id
const unsigned int acceptor_id
Definition: parameter_acceptor.h:542