Fluid structure interaction suite
function.cc
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2022 by Luca Heltai
4 //
5 // This file is part of the FSI-suite platform, based on the deal.II library.
6 //
7 // The FSI-suite platform is free software; you can use it, redistribute it,
8 // and/or modify it under the terms of the GNU Lesser General Public License as
9 // published by the Free Software Foundation; either version 3.0 of the License,
10 // or (at your option) any later version. The full text of the license can be
11 // found in the file LICENSE at the top level of the FSI-suite platform
12 // distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 
17 #include "parsed_tools/function.h"
18 
19 using namespace dealii;
20 
21 namespace ParsedTools
22 {
23  template <int dim>
24  Function<dim>::Function(const std::string &section_name,
25  const std::string &expression,
26  const std::string &function_description,
27  const std::map<std::string, double> &constants,
28  const std::string &variable_names,
29  const double h)
30  : ParameterAcceptor(section_name)
31  , FunctionParser<dim>(
32  expression,
33  Patterns::Tools::Convert<std::map<std::string, double>>::to_string(
34  constants,
35  Patterns::Map(Patterns::Anything(),
36  Patterns::Double(),
37  0,
38  Patterns::Map::max_int_value,
39  ",",
40  "=")),
41  variable_names,
42  h)
43  , expression(expression)
44  , variable_names(variable_names)
45  {
47  std::string doc =
48  function_description + ", with input variables (" + variable_names +
49  ")." +
50  (constants.empty() ? "" : " You can use the following constants: ");
51  std::string sep = "";
52  for (const auto &constant : constants)
53  {
54  doc += sep + constant.first;
55  sep = ", ";
56  }
57 
58  add_parameter(function_description, this->expression, doc);
59 
61  ParameterAcceptor::prm.add_action(function_description,
62  [&](const std::string &) { reinit(); });
64  }
65 
66 
67 
68  template <int dim>
69  void
71  {
73  variable_names,
74  expression,
75  constants,
76  Utilities::split_string_list(variable_names, ",").size() > dim);
77  }
78 
79 
80 
81  template <int dim>
82  void
84  const std::map<std::string, double> &constants)
85  {
86  this->constants = constants;
87  this->constants["E"] = numbers::E;
88  this->constants["LOG2E"] = numbers::LOG2E;
89  this->constants["LOG10E"] = numbers::LOG10E;
90  this->constants["LN2"] = numbers::LN2;
91  this->constants["LN10"] = numbers::LN10;
92  this->constants["PI"] = numbers::PI;
93  this->constants["PI_2"] = numbers::PI_2;
94  this->constants["PI_4"] = numbers::PI_4;
95  this->constants["SQRT2"] = numbers::SQRT2;
96  this->constants["SQRT1_2"] = numbers::SQRT1_2;
97  reinit();
98  }
99 
100 
101 
102  template <int dim>
103  void
104  Function<dim>::update_expression(const std::string &expression)
105  {
106  this->expression = expression;
107  reinit();
108  }
109 
110  template class Function<1>;
111  template class Function<2>;
112  template class Function<3>;
113 } // namespace ParsedTools
virtual void initialize(const std::string &vars, const std::vector< std::string > &expressions, const ConstMap &constants, const bool time_dependent=false) override
Function(const unsigned int n_components=1, const time_type initial_time=0.0)
static const char sep
static ParameterHandler prm
void leave_my_subsection(ParameterHandler &prm)
void enter_my_subsection(ParameterHandler &prm)
void add_parameter(const std::string &entry, ParameterType &parameter, const std::string &documentation="", ParameterHandler &prm_=prm, const Patterns::PatternBase &pattern=*Patterns::Tools::Convert< ParameterType >::to_pattern())
void add_action(const std::string &entry, const std::function< void(const std::string &value)> &action, const bool execute_action=true)
A wrapper for the FunctionParser class.
Definition: function.h:30
std::map< std::string, double > constants
Constants that can be used in the expression.
Definition: function.h:98
void reinit()
Reset the Function object using the expression, the constants, and the variables stored in this class...
Definition: function.cc:70
void update_expression(const std::string &expr)
Reinitialize the function with the new expression.
Definition: function.cc:104
void update_constants(const std::map< std::string, double > &constants)
Reinitialize the function with the new constants.
Definition: function.cc:83
const std::string variable_names
Keep variable names around to re-initialize the FunctionParser class.
Definition: function.h:103
std::vector< std::string > split_string_list(const std::string &s, const std::string &delimiter=",")
We collect in this namespace some wrappers around commonly used deal.II classes, derived from the Par...
static constexpr double LOG10E
static constexpr double PI_2
static constexpr double E
static constexpr double PI
static constexpr double SQRT2
static constexpr double SQRT1_2
static constexpr double PI_4
static constexpr double LN10
static constexpr double LN2
static constexpr double LOG2E
STL namespace.