Debian Codegraph @

include/deal.II/base/symbolic_function.templates.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2020 - 2023 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
15 #ifndef dealii_symbolic_function_templates_h
16 #define dealii_symbolic_function_templates_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/base/symbolic_function.h>
21
22 DEAL_II_NAMESPACE_OPEN
23
24 namespace Functions
25 {
26 #ifdef DEAL_II_WITH_SYMENGINE
27
28 namespace internal
29 {
30 inline std::vector<Differentiation::SD::Expression>
31 to_expressions(const std::vector<std::string> &strings)
32 {
33 std::vector<Differentiation::SD::Expression> ret(strings.size());
34 for (unsigned int i = 0; i < strings.size(); ++i)
35 ret[i] = Differentiation::SD::Expression(strings[i], true);
36 return ret;
37 }
38 } // namespace internal
39
40
41
42 template <int dim, typename RangeNumberType>
43 SymbolicFunction<dim, RangeNumberType>::SymbolicFunction(
44 const std::vector<Differentiation::SD::Expression> &functions,
45 const Tensor<1, dim, Differentiation::SD::Expression> &argument,
46 const Differentiation::SD::Expression &time,
47 const Differentiation::SD::types::substitution_map &user_substitution_map)
48 : Function<dim, RangeNumberType>(functions.size(), 0.0)
49 , user_function(functions)
50 , user_substitution_map(user_substitution_map)
51 , coordinate_symbols(argument)
52 , time_symbol(time)
53 {}
54
55
56
57 template <int dim, typename RangeNumberType>
58 SymbolicFunction<dim, RangeNumberType>::SymbolicFunction(
59 const std::string &expressions)
60 : Function<dim, RangeNumberType>(
61 Utilities::split_string_list(expressions, ';').size(),
62 0.0)
63 , user_function(internal::to_expressions(
64 Utilities::split_string_list(expressions, ';')))
65 , coordinate_symbols(get_default_coordinate_symbols())
66 , time_symbol(Differentiation::SD::make_symbol("t"))
67 {}
68
69
70
71 template <int dim, typename RangeNumberType>
72 void
73 SymbolicFunction<dim, RangeNumberType>::update_values() const
74 {
75 // This is only necessary if the function vector is empty. A call to
76 // update_user_substitution_map() will clear all internal vectors.
77 if (function.empty())
78 {
79 function.resize(user_function.size());
80 for (unsigned int i = 0; i < user_function.size(); ++i)
81 {
82 function[i] = user_function[i].substitute(user_substitution_map);
83 }
84 }
85 }
86
87
88
89 template <int dim, typename RangeNumberType>
90 void
91 SymbolicFunction<dim, RangeNumberType>::update_first_derivatives() const
92 {
93 // This is only necessary if the gradient vector is empty. A call to
94 // update_user_substitution_map() will clear all internal vectors.
95 if (function_gradient.empty())
96 {
97 update_values();
98 function_gradient.resize(user_function.size());
99 for (unsigned int i = 0; i < user_function.size(); ++i)
100 {
101 function_gradient[i] =
102 Differentiation::SD::differentiate(function[i],
103 coordinate_symbols);
104 }
105 }
106 }
107
108
109
110 template <int dim, typename RangeNumberType>
111 void
112 SymbolicFunction<dim, RangeNumberType>::update_second_derivatives() const
113 {
114 // This is only necessary if the gradient vector is empty. A call to
115 // update_user_substitution_map() will clear all internal vectors.
116 if (function_hessian.empty())
117 {
118 update_first_derivatives();
119 function_hessian.resize(user_function.size());
120 function_laplacian.resize(user_function.size());
121
122 for (unsigned int i = 0; i < user_function.size(); ++i)
123 {
124 function_hessian[i] =
125 Differentiation::SD::differentiate(function_gradient[i],
126 coordinate_symbols);
127 function_laplacian[i] = trace(function_hessian[i]);
128 }
129 }
130 }
131
132
133 template <int dim, typename RangeNumberType>
134 void
135 SymbolicFunction<dim, RangeNumberType>::update_user_substitution_map(
136 const Differentiation::SD::types::substitution_map &substitutions)
137 {
138 user_substitution_map = substitutions;
139
140 // Now reset all internal vectors.
141 function.resize(0);
142 function_gradient.resize(0);
143 function_hessian.resize(0);
144 function_laplacian.resize(0);
145 }
146
147
148
149 template <int dim, typename RangeNumberType>
150 void
151 SymbolicFunction<dim, RangeNumberType>::set_additional_function_arguments(
152 const Differentiation::SD::types::substitution_map &arguments)
153 {
154 additional_function_arguments = arguments;
155 }
156
157
158
159 template <int dim, typename RangeNumberType>
160 Tensor<1, dim, Differentiation::SD::Expression>
161 SymbolicFunction<dim, RangeNumberType>::get_default_coordinate_symbols()
162 {
163 static_assert(dim <= 3, "Not implemented yet.");
164 const std::vector<std::string> names = {"x", "y", "z"};
165
166 Tensor<1, dim, Differentiation::SD::Expression> x;
167 for (unsigned int i = 0; i < dim; ++i)
168 x[i] = Differentiation::SD::make_symbol(names[i]);
169 return x;
170 }
171
172
173
174 template <int dim, typename RangeNumberType>
175 const Tensor<1, dim, Differentiation::SD::Expression> &
176 SymbolicFunction<dim, RangeNumberType>::get_coordinate_symbols() const
177 {
178 return coordinate_symbols;
179 }
180
181
182
183 template <int dim, typename RangeNumberType>
184 const Differentiation::SD::Expression &
185 SymbolicFunction<dim, RangeNumberType>::get_time_symbol() const
186 {
187 return time_symbol;
188 }
189
190
191
192 template <int dim, typename RangeNumberType>
193 const std::vector<Differentiation::SD::Expression> &
194 SymbolicFunction<dim, RangeNumberType>::get_symbolic_function_expressions()
195 const
196 {
197 return user_function;
198 }
199
200
201
202 template <int dim, typename RangeNumberType>
203 const Differentiation::SD::types::substitution_map &
204 SymbolicFunction<dim, RangeNumberType>::get_user_substitution_map() const
205 {
206 return user_substitution_map;
207 }
208
209
210
211 template <int dim, typename RangeNumberType>
212 SymbolicFunction<dim, RangeNumberType>
213 SymbolicFunction<dim, RangeNumberType>::time_derivative() const
214 {
215 std::vector<Differentiation::SD::Expression> df_dt(user_function.size());
216 for (unsigned int i = 0; i < user_function.size(); ++i)
217 df_dt[i] = user_function[i].differentiate(time_symbol);
218 return SymbolicFunction<dim, RangeNumberType>(df_dt,
219 coordinate_symbols,
220 time_symbol,
221 user_substitution_map);
222 }
223
224
225
226 template <int dim, typename RangeNumberType>
227 RangeNumberType
228 SymbolicFunction<dim, RangeNumberType>::value(
229 const Point<dim> &p,
230 const unsigned int component) const
231 {
232 update_values();
233 AssertIndexRange(component, function.size());
234 return Differentiation::SD::substitute_and_evaluate<RangeNumberType>(
235 function[component], create_evaluation_substitution_map(p));
236 }
237
238
239
240 template <int dim, typename RangeNumberType>
241 Tensor<1, dim, RangeNumberType>
242 SymbolicFunction<dim, RangeNumberType>::gradient(
243 const Point<dim> &p,
244 const unsigned int component) const
245 {
246 update_first_derivatives();
247 AssertIndexRange(component, function.size());
248 return Differentiation::SD::substitute_and_evaluate<RangeNumberType>(
249 function_gradient[component], create_evaluation_substitution_map(p));
250 }
251
252
253
254 template <int dim, typename RangeNumberType>
255 RangeNumberType
256 SymbolicFunction<dim, RangeNumberType>::laplacian(
257 const Point<dim> &p,
258 const unsigned int component) const
259 {
260 update_second_derivatives();
261 AssertIndexRange(component, function.size());
262 return Differentiation::SD::substitute_and_evaluate<RangeNumberType>(
263 function_laplacian[component], create_evaluation_substitution_map(p));
264 }
265
266
267
268 template <int dim, typename RangeNumberType>
269 SymmetricTensor<2, dim, RangeNumberType>
270 SymbolicFunction<dim, RangeNumberType>::hessian(
271 const Point<dim> &p,
272 const unsigned int component) const
273 {
274 update_second_derivatives();
275 AssertIndexRange(component, function.size());
276 return SymmetricTensor<2, dim, RangeNumberType>(
277 Differentiation::SD::substitute_and_evaluate<RangeNumberType>(
278 function_hessian[component], create_evaluation_substitution_map(p)));
279 }
280
281
282
283 template <int dim, typename RangeNumberType>
284 Differentiation::SD::types::substitution_map
285 SymbolicFunction<dim, RangeNumberType>::create_evaluation_substitution_map(
286 const Point<dim> &p) const
287 {
288 auto map = additional_function_arguments;
289 Differentiation::SD::add_to_substitution_map(
290 map,
291 std::make_pair(time_symbol, this->get_time()),
292 std::make_pair(coordinate_symbols, p));
293 return map;
294 }
295 #endif
296 } // namespace Functions
297
298 DEAL_II_NAMESPACE_CLOSE
299
300 #endif