Loading [MathJax]/extensions/tex2jax.js
Fluid structure interaction suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
symbolic_function.h
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 #ifndef parsed_tools__symbolic_function_h
17 #define parsed_tools__symbolic_function_h
18 
19 #include <deal.II/base/config.h>
20 
21 #ifdef DEAL_II_WITH_SYMENGINE
22 
25 
26 namespace dealii
27 {
28  namespace Patterns
29  {
30  namespace Tools
31  {
35  template <>
36  struct Convert<Differentiation::SD::Expression>
37  {
42 
49  static std::unique_ptr<dealii::Patterns::PatternBase>
51  {
52  return Patterns::Anything().clone();
53  }
54 
58  static std::string
59  to_string(const T &t,
60  const dealii::Patterns::PatternBase &pattern = *to_pattern())
61  {
62  std::stringstream ss;
63  ss << t;
65  pattern.match(ss.str()),
66  ExcMessage(
67  "The expression does not satisfy the requirements of the "
68  "pattern."));
69  return ss.str();
70  }
71 
75  static T
76  to_value(const std::string &s,
77  const dealii::Patterns::PatternBase &pattern = *to_pattern())
78  {
79  AssertThrow(pattern.match(s), ExcMessage("Invalid string."));
80  return T(s, true);
81  }
82  };
83 
84  namespace internal
85  {
86  // Rank of SymbolicFunction
87  template <int dim>
88  struct RankInfo<
89  std::unique_ptr<dealii::Functions::SymbolicFunction<dim>>>
90  {
91  static constexpr int list_rank = 1;
92  static constexpr int map_rank = 0;
93  };
94  } // namespace internal
95 
100  template <int dim>
101  struct Convert<std::unique_ptr<dealii::Functions::SymbolicFunction<dim>>>
102  {
106  using T = std::unique_ptr<dealii::Functions::SymbolicFunction<dim>>;
107 
111  static std::unique_ptr<dealii::Patterns::PatternBase>
113  {
115  0,
116  dealii::Patterns::List::max_int_value,
117  ";")
118  .clone();
119  }
120 
128  static std::string
129  to_string(const T &t,
130  const dealii::Patterns::PatternBase &pattern =
131  *Convert<T>::to_pattern())
132  {
133  auto expr = t->get_symbolic_function_expressions();
134  return Convert<decltype(expr)>::to_string(expr, pattern);
135  }
136 
144  static T
145  to_value(const std::string &s,
146  const dealii::Patterns::PatternBase &pattern =
147  *Convert<T>::to_pattern())
148  {
149  AssertThrow(pattern.match(s), ExcMessage("Invalid string."));
150  return std::make_unique<dealii::Functions::SymbolicFunction<dim>>(s);
151  }
152  };
153  } // namespace Tools
154  } // namespace Patterns
155 } // namespace dealii
156 
157 
158 
159 namespace ParsedTools
160 {
164  template <int dim>
165  class SymbolicFunction : public dealii::ParameterAcceptor
166  {
167  public:
188  const std::string &section_name = "",
189  const std::string &expression = "",
190  const std::string &function_description = "Function expression");
191 
197  operator dealii::Functions::SymbolicFunction<dim> &()
198  {
199  return *symbolic_function;
200  }
201 
205  dealii::Functions::SymbolicFunction<dim> &
207  {
208  return *symbolic_function;
209  }
210 
211  private:
215  std::unique_ptr<dealii::Functions::SymbolicFunction<dim>> symbolic_function;
216 
223  const unsigned int n_components;
224  };
225 } // namespace ParsedTools
226 #endif
227 #endif
const std::string section_name
virtual std::unique_ptr< PatternBase > clone() const override
virtual std::unique_ptr< PatternBase > clone() const override
A wrapper for the Functions::SymbolicFunction class.
std::unique_ptr< Functions::SymbolicFunction< dim > > symbolic_function
The actual Functions::SymbolicFunction object.
SymbolicFunction(const std::string &section_name="", const std::string &expression="", const std::string &function_description="Function expression")
Build a SymbolicFunction based on the ParameterAcceptor class.
const unsigned int n_components
The number of components of the function.
Functions::SymbolicFunction< dim > & operator()()
Act as an actual Functions::SymbolicFunction.
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
We collect in this namespace some wrappers around commonly used deal.II classes, derived from the Par...
STL namespace.
static std::string to_string(const T &t, const Patterns::PatternBase &pattern= *to_pattern())
Convert a Differentiation::SD::Expression expression to a string.
static T to_value(const std::string &s, const Patterns::PatternBase &pattern= *to_pattern())
Convert a string to a Differentiation::SD::Expression expression.
static std::unique_ptr< Patterns::PatternBase > to_pattern()
Default pattern for parsing symbolic expressions.
std::unique_ptr< Functions::SymbolicFunction< dim > > T
Shorthand for the type of the parsed expression.
static std::string to_string(const T &t, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
Convert a SymbolicFunction to a string.
static std::unique_ptr< Patterns::PatternBase > to_pattern()
Pattern for a SymbolicFunction.
static T to_value(const std::string &s, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
Convert a string to a SymbolicFunction.