deal.II version GIT relicensing-2206-gaa53ff9447 2024-12-02 09:10:00+00:00
\(\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\}}\)
Loading...
Searching...
No Matches
parsed_function.cc
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2007 - 2024 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
18
20
21namespace Functions
22{
23 template <int dim>
24 ParsedFunction<dim>::ParsedFunction(const unsigned int n_components,
25 const double h)
26 : AutoDerivativeFunction<dim>(h, n_components)
27 , function_object(n_components)
28 {}
29
30
31
32 template <int dim>
33 void
35 const unsigned int n_components)
36 {
37 Assert(n_components > 0, ExcZero());
38
39 std::string vnames;
40 switch (dim)
41 {
42 case 1:
43 vnames = "x,t";
44 break;
45 case 2:
46 vnames = "x,y,t";
47 break;
48 case 3:
49 vnames = "x,y,z,t";
50 break;
51 default:
53 break;
54 }
55 prm.declare_entry(
56 "Variable names",
57 vnames,
59 "The names of the variables as they will be used in the "
60 "function, separated by commas. By default, the names of variables "
61 "at which the function will be evaluated are `x' (in 1d), `x,y' (in 2d) or "
62 "`x,y,z' (in 3d) for spatial coordinates and `t' for time. You can then "
63 "use these variable names in your function expression and they will be "
64 "replaced by the values of these variables at which the function is "
65 "currently evaluated. However, you can also choose a different set "
66 "of names for the independent variables at which to evaluate your function "
67 "expression. For example, if you work in spherical coordinates, you may "
68 "wish to set this input parameter to `r,phi,theta,t' and then use these "
69 "variable names in your function expression.");
70
71 // The expression of the function
72 std::string expr = "0";
73 for (unsigned int i = 1; i < n_components; ++i)
74 expr += "; 0";
75
76 prm.declare_entry(
77 "Function expression",
78 expr,
80 "The formula that denotes the function you want to evaluate for "
81 "particular values of the independent variables. This expression "
82 "may contain any of the usual operations such as addition or "
83 "multiplication, as well as all of the common functions such as "
84 "`sin' or `cos'. In addition, it may contain expressions like "
85 "`if(x>0, 1, -1)' where the expression evaluates to the second "
86 "argument if the first argument is true, and to the third argument "
87 "otherwise. For a full overview of possible expressions accepted "
88 "see the documentation of the muparser library at http://muparser.beltoforion.de/."
89 "\n\n"
90 "If the function you are describing represents a vector-valued "
91 "function with multiple components, then separate the expressions "
92 "for individual components by a semicolon.");
93 prm.declare_entry(
94 "Function constants",
95 "",
97 "Sometimes it is convenient to use symbolic constants in the "
98 "expression that describes the function, rather than having to "
99 "use its numeric value everywhere the constant appears. These "
100 "values can be defined using this parameter, in the form "
101 "`var1=value1, var2=value2, ...'."
102 "\n\n"
103 "A typical example would be to set this runtime parameter to "
104 "`pi=3.1415926536' and then use `pi' in the expression of the "
105 "actual formula. (That said, for convenience this class actually "
106 "defines both `pi' and `Pi' by default, but you get the idea.)");
107 }
108
109
110
111 template <int dim>
112 void
114 {
115 std::string vnames = prm.get("Variable names");
116 std::string expression = prm.get("Function expression");
117 std::string constants_list = prm.get("Function constants");
118
119 std::vector<std::string> const_list =
120 Utilities::split_string_list(constants_list, ',');
121 std::map<std::string, double> constants;
122 for (const auto &constant : const_list)
123 {
124 std::vector<std::string> this_c =
125 Utilities::split_string_list(constant, '=');
126 AssertThrow(this_c.size() == 2,
127 ExcMessage("The list of constants, <" + constants_list +
128 ">, is not a comma-separated list of "
129 "entries of the form 'name=value'."));
130 constants[this_c[0]] = Utilities::string_to_double(this_c[1]);
131 }
132
133 // set pi and Pi as synonyms for the corresponding value. note that
134 // this overrides any value a user may have given
135 constants["pi"] = numbers::PI;
136 constants["Pi"] = numbers::PI;
137
138 const unsigned int nn = (Utilities::split_string_list(vnames)).size();
139 switch (nn)
140 {
141 case dim:
142 // Time independent function
143 function_object.initialize(vnames, expression, constants);
144 break;
145 case dim + 1:
146 // Time dependent function
147 function_object.initialize(vnames, expression, constants, true);
148 break;
149 default:
150 AssertThrow(false,
152 "The list of variables specified is <" + vnames +
153 "> which is a list of length " +
155 " but it has to be a list of length equal to" +
156 " either dim (for a time-independent function)" +
157 " or dim+1 (for a time-dependent function)."));
158 }
159 }
160
161
162
163 template <int dim>
164 void
166 Vector<double> &values) const
167 {
168 function_object.vector_value(p, values);
169 }
170
171
172
173 template <int dim>
174 double
175 ParsedFunction<dim>::value(const Point<dim> &p, unsigned int comp) const
176 {
177 return function_object.value(p, comp);
178 }
179
180
181
182 template <int dim>
183 void
184 ParsedFunction<dim>::set_time(const double newtime)
185 {
186 function_object.set_time(newtime);
188 }
189
190
191 // Explicit instantiations
192 template class ParsedFunction<1>;
193 template class ParsedFunction<2>;
194 template class ParsedFunction<3>;
195} // namespace Functions
virtual void set_time(const Number new_time)
virtual void vector_value(const Point< dim > &p, Vector< double > &values) const override
static void declare_parameters(ParameterHandler &prm, const unsigned int n_components=1)
ParsedFunction(const unsigned int n_components=1, const double h=1e-8)
virtual double value(const Point< dim > &p, const unsigned int component=0) const override
virtual void set_time(const double newtime) override
void parse_parameters(ParameterHandler &prm)
void declare_entry(const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation="", const bool has_to_be_set=false)
std::string get(const std::string &entry_string) const
Definition point.h:111
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:498
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
static ::ExceptionBase & ExcZero()
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
std::size_t size
Definition mpi.cc:734
std::vector< std::string > split_string_list(const std::string &s, const std::string &delimiter=",")
Definition utilities.cc:701
std::string int_to_string(const unsigned int value, const unsigned int digits=numbers::invalid_unsigned_int)
Definition utilities.cc:470
double string_to_double(const std::string &s)
Definition utilities.cc:653
static constexpr double PI
Definition numbers.h:254