Debian Codegraph @

include/deal.II/differentiation/sd/symengine_tensor_operations.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2019 - 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
15 #ifndef dealii_differentiation_sd_symengine_tensor_operations_h
16 #define dealii_differentiation_sd_symengine_tensor_operations_h
17
18 #include <deal.II/base/config.h>
19
20 #ifdef DEAL_II_WITH_SYMENGINE
21
22 # include <deal.II/base/symmetric_tensor.h>
23 # include <deal.II/base/tensor.h>
24
25 # include <deal.II/differentiation/sd/symengine_number_types.h>
26 # include <deal.II/differentiation/sd/symengine_scalar_operations.h>
27 # include <deal.II/differentiation/sd/symengine_types.h>
28
29 # include <utility>
30 # include <vector>
31
32 DEAL_II_NAMESPACE_OPEN
33
34 namespace Differentiation
35 {
36 namespace SD
37 {
38 /**
39 * @name Symbolic variable creation
40 */
41 /** @{ */
42
43 /**
44 * Return a vector of Expressions representing a vectorial symbolic
45 * variable with the identifier specified by @p symbol.
46 *
47 * For example, if the @p symbol is the string `"v"` then the vectorial
48 * symbolic variable that is returned represents the vector $v$. Each
49 * component of $v$ is prefixed by the given @p symbol, and has a suffix that
50 * indicates its component index.
51 *
52 * @tparam dim The dimension of the returned tensor.
53 * @param[in] symbol An identifier (or name) for the vector of returned
54 * symbolic variables.
55 * @return A vector (a rank-1 tensor) of symbolic variables with the name of
56 * each individual component prefixed by @p symbol.
57 *
58 * @warning It is up to the user to ensure that there is no ambiguity in the
59 * symbols used within a section of code.
60 */
61 template <int dim>
62 Tensor<1, dim, Expression>
63 make_vector_of_symbols(const std::string &symbol);
64
65 /**
66 * Return a tensor of Expressions representing a tensorial symbolic
67 * variable with the identifier specified by @p symbol.
68 *
69 * For example, if the @p symbol is the string `"T"` then the tensorial
70 * symbolic variable that is returned represents the vector $T$. Each
71 * component of $T$ is prefixed by the given @p symbol, and has a suffix that
72 * indicates its component indices.
73 *
74 * @tparam rank The rank of the returned tensor.
75 * @tparam dim The dimension of the returned tensor.
76 * @param[in] symbol An identifier (or name) for the tensor of returned
77 * symbolic variables.
78 * @return A tensor of symbolic variables with the name of each individual
79 * component prefixed by @p symbol.
80 *
81 * @warning It is up to the user to ensure that there is no ambiguity in the
82 * symbols used within a section of code.
83 */
84 template <int rank, int dim>
85 Tensor<rank, dim, Expression>
86 make_tensor_of_symbols(const std::string &symbol);
87
88 /**
89 * Return a symmetric tensor of Expressions representing a tensorial
90 * symbolic variable with the identifier specified by @p symbol.
91 *
92 * For example, if the @p symbol is the string `"S"` then the tensorial
93 * symbolic variable that is returned represents the vector $S$. Each
94 * component of $S$ is prefixed by the given @p symbol, and has a suffix that
95 * indicates its component indices.
96 *
97 * @tparam rank The rank of the returned tensor.
98 * @tparam dim The dimension of the returned tensor.
99 * @param[in] symbol An identifier (or name) for the tensor of returned
100 * symbolic variables.
101 * @return A tensor of symbolic variables with the name of each individual
102 * component prefixed by @p symbol.
103 *
104 * @warning It is up to the user to ensure that there is no ambiguity in the
105 * symbols used within a section of code.
106 */
107 template <int rank, int dim>
108 SymmetricTensor<rank, dim, Expression>
109 make_symmetric_tensor_of_symbols(const std::string &symbol);
110
111 /**
112 * Return a vector of Expression representing a vectorial symbolic
113 * function with the identifier specified by @p symbol. The functions'
114 * symbolic dependencies are specified by the keys to the input
115 * @p arguments map; the values stored in the map are ignored.
116 *
117 * @tparam dim The dimension of the returned tensor.
118 * @param[in] symbol An identifier (or name) for the vector of returned
119 * symbolic functions.
120 * @param[in] arguments A map of input arguments to the returned
121 * symbolic functions.
122 * @return A vector (a rank-1 tensor) of generic symbolic functions with the
123 * name of each individual component prefixed by @p symbol, a suffix
124 * that indicates its component index, and the number of input
125 * arguments equal to the length of @p arguments.
126 *
127 * @warning It is up to the user to ensure that there is no ambiguity in the
128 * symbols used within a section of code.
129 */
130 template <int dim>
131 Tensor<1, dim, Expression>
132 make_vector_of_symbolic_functions(const std::string &symbol,
133 const types::substitution_map &arguments);
134
135 /**
136 * Return a tensor of Expression representing a tensorial symbolic
137 * function with the identifier specified by @p symbol. The functions'
138 * symbolic dependencies are specified by the keys to the input
139 * @p arguments map; the values stored in the map are ignored.
140 *
141 * @tparam rank The rank of the returned tensor.
142 * @tparam dim The dimension of the returned tensor.
143 * @param[in] symbol An identifier (or name) for the tensor of returned
144 * symbolic functions.
145 * @param[in] arguments A map of input arguments to the returned
146 * symbolic functions.
147 * @return A tensor of generic symbolic functions with the name of each
148 * individual component prefixed by @p symbol, a suffix that
149 * indicates its component indeices, and the number of input
150 * arguments equal to the length of @p arguments.
151 *
152 * @warning It is up to the user to ensure that there is no ambiguity in the
153 * symbols used within a section of code.
154 */
155 template <int rank, int dim>
156 Tensor<rank, dim, Expression>
157 make_tensor_of_symbolic_functions(const std::string &symbol,
158 const types::substitution_map &arguments);
159
160 /**
161 * Return a symmetric tensor of Expression representing a tensorial
162 * symbolic function with the identifier specified by @p symbol. The
163 * functions' symbolic dependencies are specified by the keys to the input
164 * @p arguments map; the values stored in the map are ignored.
165 *
166 * @tparam rank The rank of the returned tensor.
167 * @tparam dim The dimension of the returned tensor.
168 * @param[in] symbol An identifier (or name) for the tensor of returned
169 * symbolic functions.
170 * @param[in] arguments A map of input arguments to the returned
171 * symbolic functions.
172 * @return A symmetric tensor of generic symbolic functions with the name of
173 * each individual component prefixed by @p symbol, a suffix that
174 * indicates its component indeices, and the number of input
175 * arguments equal to the length of @p arguments.
176 *
177 * @warning It is up to the user to ensure that there is no ambiguity in the
178 * symbols used within a section of code.
179 */
180 template <int rank, int dim>
181 SymmetricTensor<rank, dim, Expression>
182 make_symmetric_tensor_of_symbolic_functions(
183 const std::string &symbol,
184 const types::substitution_map &arguments);
185
186 /** @} */
187
188 /**
189 * @name Symbolic differentiation
190 */
191 /** @{ */
192
193 /**
194 * Return the symbolic result of computing the partial derivative of the
195 * scalar @p f with respect to the tensor @p T.
196 *
197 * @param[in] f A scalar symbolic function or (dependent) expression.
198 * @param[in] T A tensor of symbolic (independent) variables.
199 * @return The tensor of symbolic functions or expressions representing
200 * the result $\frac{\partial f}{\partial \mathbf{T}}$.
201 */
202 template <int rank, int dim>
203 Tensor<rank, dim, Expression>
204 differentiate(const Expression &f, const Tensor<rank, dim, Expression> &T);
205
206 /**
207 * Return the symbolic result of computing the partial derivative of the
208 * scalar @p f with respect to the symmetric tensor @p S.
209 *
210 * @param[in] f A scalar symbolic function or (dependent) expression.
211 * @param[in] S A symmetric tensor of symbolic (independent) variables.
212 * @return The symmetric tensor of symbolic functions or expressions representing
213 * the result $\frac{\partial f}{\partial \mathbf{S}}$.
214 */
215 template <int rank, int dim>
216 SymmetricTensor<rank, dim, Expression>
217 differentiate(const Expression &f,
218 const SymmetricTensor<rank, dim, Expression> &S);
219
220 /**
221 * Return the symbolic result of computing the partial derivative of the
222 * rank-0 tensor (or scalar) @p f with respect to the tensor @p T.
223 *
224 * @param[in] f A rank-0 tensor symbolic function or (dependent) expression.
225 * @param[in] T A tensor of symbolic (independent) variables.
226 * @return The tensor of symbolic functions or expressions representing
227 * the result $\frac{\partial f}{\partial \mathbf{T}}$.
228 */
229 template <int rank, int dim>
230 Tensor<rank, dim, Expression>
231 differentiate(const Tensor<0, dim, Expression> &f,
232 const Tensor<rank, dim, Expression> &T);
233
234 /**
235 * Return the symbolic result of computing the partial derivative of the
236 * rank-0 tensor (or scalar) @p f with respect to the symmetric tensor @p S.
237 *
238 * @param[in] f A rank-0 tensor symbolic function or (dependent) expression.
239 * @param[in] S A symmetric tensor of symbolic (independent) variables.
240 * @return The symmetric tensor of symbolic functions or expressions representing
241 * the result $\frac{\partial f}{\partial \mathbf{S}}$.
242 */
243 template <int rank, int dim>
244 SymmetricTensor<rank, dim, Expression>
245 differentiate(const Tensor<0, dim, Expression> &f,
246 const SymmetricTensor<rank, dim, Expression> &S);
247
248 /**
249 * Return the symbolic result of computing the partial derivative of the
250 * tensor @p T with respect to the scalar @p x.
251 *
252 * @param[in] T A tensor of symbolic functions or (dependent) expressions.
253 * @param[in] x A scalar symbolic (independent) variable.
254 * @return The tensor of symbolic functions or expressions representing
255 * the result $\frac{\partial \mathbf{T}}{\partial x}$.
256 */
257 template <int rank, int dim>
258 Tensor<rank, dim, Expression>
259 differentiate(const Tensor<rank, dim, Expression> &T, const Expression &x);
260
261 /**
262 * Return the symbolic result of computing the partial derivative of the
263 * symmetric tensor @p S with respect to the scalar @p x.
264 *
265 * @param[in] S A symmetric tensor of symbolic functions or (dependent)
266 * expressions.
267 * @param[in] x A scalar symbolic (independent) variable.
268 * @return The symmetric tensor of symbolic functions or expressions representing
269 * the result $\frac{\partial \mathbf{S}}{\partial x}$.
270 */
271 template <int rank, int dim>
272 SymmetricTensor<rank, dim, Expression>
273 differentiate(const SymmetricTensor<rank, dim, Expression> &S,
274 const Expression &x);
275
276 /**
277 * Return the symbolic result of computing the partial derivative of the
278 * tensor @p T with respect to the rank-0 tensor @p x.
279 *
280 * @param[in] T A tensor of symbolic functions or (dependent) expressions.
281 * @param[in] x A rank-0 tensor containing a symbolic (independent)
282 * variable.
283 * @return The tensor of symbolic functions or expressions representing
284 * the result $\frac{\partial \mathbf{T}}{\partial x}$.
285 */
286 template <int rank, int dim>
287 Tensor<rank, dim, Expression>
288 differentiate(const Tensor<rank, dim, Expression> &T,
289 const Tensor<0, dim, Expression> &x);
290
291 /**
292 * Return the symbolic result of computing the partial derivative of the
293 * symmetric tensor @p S with respect to the rank-0 tensor @p x.
294 *
295 * @param[in] S A symmetric tensor of symbolic functions or (dependent)
296 * expressions.
297 * @param[in] x A rank-0 tensor containing a symbolic (independent)
298 * variable.
299 * @return The symmetric tensor of symbolic functions or expressions representing
300 * the result $\frac{\partial \mathbf{S}}{\partial x}$.
301 */
302 template <int rank, int dim>
303 SymmetricTensor<rank, dim, Expression>
304 differentiate(const SymmetricTensor<rank, dim, Expression> &S,
305 const Tensor<0, dim, Expression> &x);
306
307 /**
308 * Return the symbolic result of computing the partial derivative of the
309 * tensor @p T1 with respect to the tensor @p T2.
310 *
311 * @param[in] T1 A tensor of symbolic functions or (dependent) expressions.
312 * @param[in] T2 A tensor of symbolic (independent) variables.
313 * @return The tensor of symbolic functions or variables representing
314 * the result $\frac{\partial \mathbf{T}_{1}}{\partial
315 * \mathbf{T}_{2}}$.
316 */
317 template <int rank_1, int rank_2, int dim>
318 Tensor<rank_1 + rank_2, dim, Expression>
319 differentiate(const Tensor<rank_1, dim, Expression> &T1,
320 const Tensor<rank_2, dim, Expression> &T2);
321
322 /**
323 * Return the symbolic result of computing the partial derivative of the
324 * symmetric tensor @p S1 with respect to the symmetric tensor @p S2.
325 *
326 * @param[in] S1 A symmetric tensor of symbolic functions or (dependent)
327 * expressions.
328 * @param[in] S2 A symmetric tensor of symbolic (independent)
329 * variables.
330 * @return The symmetric tensor of symbolic functions or variables representing
331 * the result $\frac{\partial \mathbf{S}_{1}}{\partial
332 * \mathbf{S}_{2}}$.
333 */
334 template <int rank_1, int rank_2, int dim>
335 SymmetricTensor<rank_1 + rank_2, dim, Expression>
336 differentiate(const SymmetricTensor<rank_1, dim, Expression> &S1,
337 const SymmetricTensor<rank_2, dim, Expression> &S2);
338
339 /**
340 * Return the symbolic result of computing the partial derivative of the
341 * tensor @p T with respect to the symmetric tensor @p S.
342 *
343 * @param[in] T A tensor of symbolic functions or (dependent) expressions.
344 * @param[in] S A symmetric tensor of symbolic (independent)
345 * variables.
346 * @return The tensor of symbolic functions or variables representing
347 * the result $\frac{\partial \mathbf{T}}{\partial \mathbf{S}}$.
348 */
349 template <int rank_1, int rank_2, int dim>
350 Tensor<rank_1 + rank_2, dim, Expression>
351 differentiate(const Tensor<rank_1, dim, Expression> &T,
352 const SymmetricTensor<rank_2, dim, Expression> &S);
353
354 /**
355 * Return the symbolic result of computing the partial derivative of the
356 * symmetric tensor @p S with respect to the tensor @p T.
357 *
358 * @param[in] S A symmetric tensor of symbolic functions or (dependent)
359 * expressions.
360 * @param[in] T A tensor of symbolic (independent) variables.
361 * @return The tensor of symbolic functions or variables representing
362 * the result $\frac{\partial \mathbf{S}}{\partial \mathbf{T}}$.
363 */
364 template <int rank_1, int rank_2, int dim>
365 Tensor<rank_1 + rank_2, dim, Expression>
366 differentiate(const SymmetricTensor<rank_1, dim, Expression> &S,
367 const Tensor<rank_2, dim, Expression> &T);
368
369 /** @} */
370
371 /**
372 * @name Symbol map creation and manipulation
373 */
374 /** @{ */
375
376 /**
377 * A convenience function for adding empty entries, with the key values
378 * equal to the entries in the @p symbol_tensor, to the symbolic
379 * map @p symbol_map.
380 *
381 * For more context which this function is used, see the other
382 * `add_to_symbol_map(types::substitution_map &, const Expression &)`
383 * function.
384 *
385 * @tparam ignore_invalid_symbols See the other
386 * `add_to_symbol_map(types::substitution_map &, const Expression &)`
387 * function for a detailed discussion on the role of this
388 * template argument.
389 *
390 * @tparam SymbolicType A type that represents a symbolic variable.
391 * The Differentiation::SD::Expression class is often suitable for
392 * this purpose, although if the @p ValueType is not supported
393 * by this class then a user-defined @p SymbolicType should be
394 * used.
395 * @tparam ValueType A type that corresponds to the @p value that the
396 * @p symbol is to represent. This @p ValueType is somewhat
397 * arbitrary as it is only used to create default-constructed
398 * values as entries in the map.
399 */
400 template <bool ignore_invalid_symbols = false,
401 typename ValueType = double,
402 int rank,
403 int dim,
404 typename SymbolicType>
405 void
406 add_to_symbol_map(types::substitution_map &symbol_map,
407 const Tensor<rank, dim, SymbolicType> &symbol_tensor);
408
409 /**
410 * A convenience function for adding empty entries, with the key values
411 * equal to the entries in the @p symbol_tensor, to the symbolic
412 * map @p symbol_map.
413 *
414 * For more context which this function is used, see the other
415 * `add_to_symbol_map(types::substitution_map &, const Expression &)`
416 * function.
417 *
418 * @tparam ignore_invalid_symbols See the other
419 * `add_to_symbol_map(types::substitution_map &, const Expression &)`
420 * function for a detailed discussion on the role of this
421 * template argument.
422 *
423 * @tparam SymbolicType A type that represents a symbolic variable.
424 * The Differentiation::SD::Expression class is often suitable for
425 * this purpose, although if the @p ValueType is not supported
426 * by this class then a user-defined @p SymbolicType should be
427 * used.
428 * @tparam ValueType A type that corresponds to the @p value that the
429 * @p symbol is to represent. This @p ValueType is somewhat
430 * arbitrary as it is only used to create default-constructed
431 * values as entries in the map.
432 */
433 template <bool ignore_invalid_symbols = false,
434 typename ValueType = double,
435 int rank,
436 int dim,
437 typename SymbolicType>
438 void
439 add_to_symbol_map(
440 types::substitution_map &symbol_map,
441 const SymmetricTensor<rank, dim, SymbolicType> &symbol_tensor);
442
443 /**
444 * Find the input @p symbols in the @p substitution_map and set the entries
445 * corresponding to the key values given by @p symbol_tensor to the values
446 * given by @p value_tensor.
447 *
448 * This function may be used to safely transform an existing or null
449 * symbolic map (one with uninitialized entries) into one that can be used
450 * to conduct symbolic substitution operations (i.e., a substitution map).
451 *
452 * @tparam SymbolicType A type that represents a symbolic variable.
453 * The Differentiation::SD::Expression class is often suitable for
454 * this purpose, although if the @p ValueType is not supported
455 * by this class then a user-defined @p SymbolicType should be
456 * used.
457 * @tparam ValueType A type that corresponds to the @p value that the
458 * @p symbol is to represent. Although it is typically
459 * arithmetic in nature, it may also represent another symbolic
460 * expression type or be a special type that a user-defined
461 * @p ExpressionType can be constructed from.
462 */
463 template <int rank, int dim, typename SymbolicType, typename ValueType>
464 void
465 set_value_in_symbol_map(
466 types::substitution_map &substitution_map,
467 const Tensor<rank, dim, SymbolicType> &symbol_tensor,
468 const Tensor<rank, dim, ValueType> &value_tensor);
469
470 /**
471 * Find the input @p symbols in the @p substitution_map and set the entries
472 * corresponding to the key values given by @p symbol_tensor to the values
473 * given by @p value_tensor.
474 *
475 * This function may be used to safely transform an existing or null
476 * symbolic map (one with uninitialized entries) into one that can be used
477 * to conduct symbolic substitution operations (i.e., a substitution map).
478 *
479 * @tparam SymbolicType A type that represents a symbolic variable.
480 * The Differentiation::SD::Expression class is often suitable for
481 * this purpose, although if the @p ValueType is not supported
482 * by this class then a user-defined @p SymbolicType should be
483 * used.
484 * @tparam ValueType A type that corresponds to the @p value that the
485 * @p symbol is to represent. Although it is typically
486 * arithmetic in nature, it may also represent another symbolic
487 * expression type or be a special type that a user-defined
488 * @p ExpressionType can be constructed from.
489 */
490 template <int rank, int dim, typename SymbolicType, typename ValueType>
491 void
492 set_value_in_symbol_map(
493 types::substitution_map &substitution_map,
494 const SymmetricTensor<rank, dim, SymbolicType> &symbol_tensor,
495 const SymmetricTensor<rank, dim, ValueType> &value_tensor);
496
497 /** @} */
498
499 /**
500 * @name Symbol substitution map creation
501 */
502 /** @{ */
503
504 /**
505 * Return a substitution map that has the entry keys given by the
506 * @p symbol_tensor and the values given by the @p value_tensor. It is
507 * expected that all key entries be valid symbols or symbolic expressions.
508 *
509 * It is possible to map symbolic types to other symbolic types
510 * using this function. For more details on this, see the other
511 * `make_substitution_map(const Expression &,const ValueType &)`
512 * function.
513 *
514 * @tparam ExpressionType A type that represents a symbolic expression.
515 * The Differentiation::SD::Expression class is often suitable for
516 * this purpose, although if the @p ValueType is not supported
517 * by this class then a user-defined @p ExpressionType should be
518 * used.
519 * @tparam ValueType A type that corresponds to the @p value that the
520 * @p symbol is to represent. Although it is typically
521 * arithmetic in nature, it may also represent another symbolic
522 * expression type or be a special type that a user-defined
523 * @p ExpressionType can be constructed from.
524 */
525 template <int rank, int dim, typename ExpressionType, typename ValueType>
526 types::substitution_map
527 make_substitution_map(
528 const Tensor<rank, dim, ExpressionType> &symbol_tensor,
529 const Tensor<rank, dim, ValueType> &value_tensor);
530
531 /**
532 * Return a substitution map that has the entry keys given by the
533 * @p symbol_tensor and the values given by the @p value_tensor. It is
534 * expected that all key entries be valid symbols or symbolic expressions.
535 *
536 * It is possible to map symbolic types to other symbolic types
537 * using this function. For more details on this, see the other
538 * `make_substitution_map(const Expression &,const ValueType &)`
539 * function.
540 *
541 * @tparam ExpressionType A type that represents a symbolic expression.
542 * The Differentiation::SD::Expression class is often suitable for
543 * this purpose, although if the @p ValueType is not supported
544 * by this class then a user-defined @p ExpressionType should be
545 * used.
546 * @tparam ValueType A type that corresponds to the @p value that the
547 * @p symbol is to represent. Although it is typically
548 * arithmetic in nature, it may also represent another symbolic
549 * expression type or be a special type that a user-defined
550 * @p ExpressionType can be constructed from.
551 */
552 template <int rank, int dim, typename ExpressionType, typename ValueType>
553 types::substitution_map
554 make_substitution_map(
555 const SymmetricTensor<rank, dim, ExpressionType> &symbol_tensor,
556 const SymmetricTensor<rank, dim, ValueType> &value_tensor);
557
558 /** @} */
559
560 /**
561 * @name Symbol substitution map enlargement
562 */
563 /** @{ */
564
565 /**
566 * A convenience function for adding an entry to the @p substitution_map.
567 * The new entries will have the keys given in the @p symbol_tensor with
568 * their paired values extracted from the corresponding elements of the
569 * @p value_tensor.
570 *
571 * For more context which this function is used, see the other
572 * `add_to_substitution_map(types::substitution_map &, const Expression &,
573 * const Expression &)` function.
574 *
575 * @tparam ignore_invalid_symbols See the other
576 * `add_to_substitution_map(types::substitution_map &, const Expression &,
577 * const Expression &)` function for a detailed discussion on the role of
578 * this template argument.
579 *
580 * @tparam ExpressionType A type that represents a symbolic expression.
581 * The Differentiation::SD::Expression class is often suitable for
582 * this purpose, although if the @p ValueType is not supported
583 * by this class then a user-defined @p ExpressionType should be
584 * used.
585 * @tparam ValueType A type that corresponds to the @p value that the
586 * @p symbol is to represent. Although it is typically
587 * arithmetic in nature, it may also represent another symbolic
588 * expression type or be a special type that a user-defined
589 * @p ExpressionType can be constructed from.
590 */
591 template <bool ignore_invalid_symbols = false,
592 int rank,
593 int dim,
594 typename ExpressionType,
595 typename ValueType>
596 void
597 add_to_substitution_map(
598 types::substitution_map &substitution_map,
599 const Tensor<rank, dim, ExpressionType> &symbol_tensor,
600 const Tensor<rank, dim, ValueType> &value_tensor);
601
602 /**
603 * A convenience function for adding an entry to the @p substitution_map.
604 * The new entries will have the keys given in the @p symbol_tensor with
605 * their paired values extracted from the corresponding elements of the @p value_tensor.
606 *
607 * For more context which this function is used, see the other
608 * `add_to_substitution_map(types::substitution_map &,const Expression &,
609 * const Expression &)` function.
610 *
611 * @tparam ignore_invalid_symbols See the other
612 * `add_to_substitution_map(types::substitution_map &, const Expression &,
613 * const Expression &)` function for a detailed discussion on the role of
614 * this template argument.
615 *
616 * @tparam ExpressionType A type that represents a symbolic expression.
617 * The Differentiation::SD::Expression class is often suitable for
618 * this purpose, although if the @p ValueType is not supported
619 * by this class then a user-defined @p ExpressionType should be
620 * used.
621 * @tparam ValueType A type that corresponds to the @p value that the
622 * @p symbol is to represent. Although it is typically
623 * arithmetic in nature, it may also represent another symbolic
624 * expression type or be a special type that a user-defined
625 * @p ExpressionType can be constructed from.
626 */
627 template <bool ignore_invalid_symbols = false,
628 int rank,
629 int dim,
630 typename ExpressionType,
631 typename ValueType>
632 void
633 add_to_substitution_map(
634 types::substitution_map &substitution_map,
635 const SymmetricTensor<rank, dim, ExpressionType> &symbol_tensor,
636 const SymmetricTensor<rank, dim, ValueType> &value_tensor);
637
638 /** @} */
639
640 /**
641 * @name Symbol substitution and evaluation
642 */
643 /** @{ */
644
645 /**
646 * Perform a single substitution sweep of a set of symbols into the given
647 * tensor of symbolic expressions.
648 * The symbols in the @p expression_tensor that correspond to the entry keys
649 * of the @p substitution_map are substituted with the map entry's associated
650 * value.
651 * This substitution function may be used to give a set of symbolic
652 * variables either a numeric interpretation or some symbolic definition.
653 *
654 * For more information regarding the performance of symbolic substitution,
655 * and the outcome of evaluation using a substitution map with cyclic
656 * dependencies, see the
657 * `substitute(const Expression &, const types::substitution_map &)`
658 * function.
659 *
660 * @note It is not required that all symbolic expressions be fully resolved
661 * when using this function. In other words, partial substitutions are
662 * valid.
663 */
664 template <int rank, int dim>
665 Tensor<rank, dim, Expression>
666 substitute(const Tensor<rank, dim, Expression> &expression_tensor,
667 const types::substitution_map &substitution_map);
668
669 /**
670 * Perform a single substitution sweep of a set of symbols into the given
671 * symmetric tensor of symbolic expressions.
672 * The symbols in the @p expression_tensor that correspond to the entry keys
673 * of the @p substitution_map are substituted with the map entry's associated
674 * value.
675 * This substitution function may be used to give a set of symbolic
676 * variables either a numeric interpretation or some symbolic definition.
677 *
678 * For more information regarding the performance of symbolic substitution,
679 * and the outcome of evaluation using a substitution map with cyclic
680 * dependencies, see the
681 * `substitute(const Expression &, const types::substitution_map &)`
682 * function.
683 *
684 * @note It is not required that all symbolic expressions be fully resolved
685 * when using this function. In other words, partial substitutions are
686 * valid.
687 */
688 template <int rank, int dim>
689 SymmetricTensor<rank, dim, Expression>
690 substitute(const SymmetricTensor<rank, dim, Expression> &expression_tensor,
691 const types::substitution_map &substitution_map);
692
693 /**
694 * Perform a single substitution sweep of a set of symbols into the given
695 * tensor of symbolic expressions, and immediately evaluate the tensorial
696 * result.
697 * The symbols in the @p expression_tensor that correspond to the entry keys
698 * of the @p substitution_map are substituted with the map entry's associated
699 * value.
700 * This substitution function is used to give a set of symbolic variables
701 * a numeric interpretation with the returned result being of the type
702 * specified by the @p ValueType template argument.
703 *
704 * For more information regarding the performance of symbolic substitution,
705 * and the outcome of evaluation using a substitution map with cyclic
706 * dependencies, see the
707 * `substitute(const Expression &, const types::substitution_map &)`
708 * function.
709 *
710 * @note It is required that all symbols in @p expression_tensor be
711 * successfully resolved by the @p substitution_map.
712 * If only partial substitution is performed, then an error is thrown.
713 *
714 * @tparam ValueType A type that corresponds to the @p value that the
715 * @p symbol is to represent. In the context of this particular
716 * function, this template parameter is typically arithmetic in
717 * nature.
718 */
719 template <typename ValueType, int rank, int dim>
720 Tensor<rank, dim, ValueType>
721 substitute_and_evaluate(
722 const Tensor<rank, dim, Expression> &expression_tensor,
723 const types::substitution_map &substitution_map);
724
725 /**
726 * Perform a single substitution sweep of a set of symbols into the given
727 * symmetric tensor of symbolic expressions, and immediately evaluate the
728 * tensorial result.
729 * The symbols in the @p expression_tensor that correspond to the entry keys
730 * of the @p substitution_map are substituted with the map entry's associated
731 * value.
732 * This substitution function is used to give a set of symbolic variables
733 * a numeric interpretation, with the returned result being of the type
734 * specified by the @p ValueType template argument.
735 *
736 * For more information regarding the performance of symbolic substitution,
737 * and the outcome of evaluation using a substitution map with cyclic
738 * dependencies, see the
739 * `substitute(const Expression &, const types::substitution_map &)`
740 * function.
741 *
742 * @note It is required that all symbols in @p expression_tensor be
743 * successfully resolved by the @p substitution_map.
744 * If only partial substitution is performed, then an error is thrown.
745 *
746 * @tparam ValueType A type that corresponds to the @p value that the
747 * @p symbol is to represent. In the context of this particular
748 * function, this template parameter is typically arithmetic in
749 * nature.
750 */
751 template <typename ValueType, int rank, int dim>
752 SymmetricTensor<rank, dim, ValueType>
753 substitute_and_evaluate(
754 const SymmetricTensor<rank, dim, Expression> &expression_tensor,
755 const types::substitution_map &substitution_map);
756
757 /** @} */
758
759 } // namespace SD
760 } // namespace Differentiation
761
762
763 /* -------------------- inline and template functions ------------------ */
764
765
766 # ifndef DOXYGEN
767
768 namespace Differentiation
769 {
770 namespace SD
771 {
772 /* ---------------- Symbolic differentiation --------------*/
773
774
775 namespace internal
776 {
777 template <int dim>
778 TableIndices<4>
779 make_rank_4_tensor_indices(const unsigned int idx_i,
780 const unsigned int idx_j)
781 {
782 const TableIndices<2> indices_i(
783 SymmetricTensor<2, dim>::unrolled_to_component_indices(idx_i));
784 const TableIndices<2> indices_j(
785 SymmetricTensor<2, dim>::unrolled_to_component_indices(idx_j));
786 return TableIndices<4>(indices_i[0],
787 indices_i[1],
788 indices_j[0],
789 indices_j[1]);
790 }
791
792
793 template <int rank_1, int rank_2>
794 TableIndices<rank_1 + rank_2>
795 concatenate_indices(const TableIndices<rank_1> &indices_1,
796 const TableIndices<rank_2> &indices_2)
797 {
798 TableIndices<rank_1 + rank_2> indices_out;
799 for (unsigned int i = 0; i < rank_1; ++i)
800 indices_out[i] = indices_1[i];
801 for (unsigned int j = 0; j < rank_2; ++j)
802 indices_out[rank_1 + j] = indices_2[j];
803 return indices_out;
804 }
805
806
807 template <int rank>
808 TableIndices<rank>
809 transpose_indices(const TableIndices<rank> &indices)
810 {
811 return indices;
812 }
813
814
815 template <>
816 inline TableIndices<2>
817 transpose_indices(const TableIndices<2> &indices)
818 {
819 return TableIndices<2>(indices[1], indices[0]);
820 }
821
822
823 template <int rank, int dim, typename ValueType>
824 bool
825 is_symmetric_component(const TableIndices<rank> &,
826 const Tensor<rank, dim, ValueType> &)
827 {
828 return false;
829 }
830
831
832 template <int rank, int dim, typename ValueType>
833 bool
834 is_symmetric_component(const TableIndices<rank> &,
835 const SymmetricTensor<rank, dim, ValueType> &)
836 {
837 static_assert(
838 rank == 0 || rank == 2,
839 "Querying symmetric component for non rank-2 symmetric tensor index is not allowed.");
840 return false;
841 }
842
843
844 template <int dim, typename ValueType>
845 bool
846 is_symmetric_component(const TableIndices<2> &table_indices,
847 const SymmetricTensor<2, dim, ValueType> &)
848 {
849 return table_indices[0] != table_indices[1];
850 }
851
852
853 template <int dim,
854 typename ValueType = Expression,
855 template <int, int, typename>
856 class TensorType>
857 TensorType<0, dim, ValueType>
858 scalar_diff_tensor(const ValueType &func,
859 const TensorType<0, dim, ValueType> &op)
860 {
861 return differentiate(func, op);
862 }
863
864
865 template <int rank,
866 int dim,
867 typename ValueType = Expression,
868 template <int, int, typename>
869 class TensorType>
870 TensorType<rank, dim, ValueType>
871 scalar_diff_tensor(const ValueType &func,
872 const TensorType<rank, dim, ValueType> &op)
873 {
874 TensorType<rank, dim, ValueType> out;
875 for (unsigned int i = 0; i < out.n_independent_components; ++i)
876 {
877 const TableIndices<rank> indices(
878 out.unrolled_to_component_indices(i));
879 out[indices] = differentiate(func, op[indices]);
880
881 if (is_symmetric_component(indices, op))
882 out[indices] *= 0.5;
883 }
884 return out;
885 }
886
887
888 // Specialization for rank-0 tensor
889 template <int rank,
890 int dim,
891 typename ValueType = Expression,
892 template <int, int, typename>
893 class TensorType>
894 TensorType<rank, dim, ValueType>
895 tensor_diff_tensor(const TensorType<0, dim, ValueType> &func,
896 const TensorType<rank, dim, ValueType> &op)
897 {
898 TensorType<rank, dim, ValueType> out;
899 for (unsigned int i = 0; i < out.n_independent_components; ++i)
900 {
901 const TableIndices<rank> indices(
902 out.unrolled_to_component_indices(i));
903 out[indices] = differentiate(func, op[indices]);
904
905 if (is_symmetric_component(indices, op))
906 out[indices] *= 0.5;
907 }
908 return out;
909 }
910
911
912 template <int rank,
913 int dim,
914 typename ValueType = Expression,
915 template <int, int, typename>
916 class TensorType>
917 TensorType<rank, dim, ValueType>
918 tensor_diff_scalar(const TensorType<rank, dim, ValueType> &funcs,
919 const ValueType &op)
920 {
921 TensorType<rank, dim, ValueType> out;
922 for (unsigned int i = 0; i < out.n_independent_components; ++i)
923 {
924 const TableIndices<rank> indices(
925 out.unrolled_to_component_indices(i));
926 out[indices] = differentiate(funcs[indices], op);
927 }
928 return out;
929 }
930
931
932 // Specialization for rank-0 tensor
933 template <int rank,
934 int dim,
935 typename ValueType = Expression,
936 template <int, int, typename>
937 class TensorType>
938 TensorType<rank, dim, ValueType>
939 tensor_diff_tensor(const TensorType<rank, dim, ValueType> &funcs,
940 const TensorType<0, dim, ValueType> &op)
941 {
942 TensorType<rank, dim, ValueType> out;
943 for (unsigned int i = 0; i < out.n_independent_components; ++i)
944 {
945 const TableIndices<rank> indices(
946 out.unrolled_to_component_indices(i));
947 out[indices] = differentiate(funcs[indices], op);
948 }
949 return out;
950 }
951
952
953 // For either symmetric or normal tensors
954 template <int rank_1,
955 int rank_2,
956 int dim,
957 typename ValueType = Expression,
958 template <int, int, typename>
959 class TensorType>
960 TensorType<rank_1 + rank_2, dim, ValueType>
961 tensor_diff_tensor(const TensorType<rank_1, dim, ValueType> &funcs,
962 const TensorType<rank_2, dim, ValueType> &op)
963 {
964 TensorType<rank_1 + rank_2, dim, ValueType> out;
965 for (unsigned int i = 0; i < funcs.n_independent_components; ++i)
966 {
967 const TableIndices<rank_1> indices_i(
968 funcs.unrolled_to_component_indices(i));
969 for (unsigned int j = 0; j < op.n_independent_components; ++j)
970 {
971 const TableIndices<rank_2> indices_j(
972 op.unrolled_to_component_indices(j));
973 const TableIndices<rank_1 + rank_2> indices_out =
974 concatenate_indices(indices_i, indices_j);
975
976 out[indices_out] =
977 differentiate(funcs[indices_i], op[indices_j]);
978
979 if (is_symmetric_component(indices_j, op))
980 out[indices_out] *= 0.5;
981 }
982 }
983 return out;
984 }
985
986
987 // For mixed symmetric/standard tensors
988 // The return type is always a standard tensor, since we cannot be sure
989 // that any symmetries exist in either the function tensor or the
990 // differential operator.
991 template <int rank_1,
992 int rank_2,
993 int dim,
994 typename ValueType = Expression,
995 template <int, int, typename>
996 class TensorType_1,
997 template <int, int, typename>
998 class TensorType_2>
999 Tensor<rank_1 + rank_2, dim, ValueType>
1000 tensor_diff_tensor(const TensorType_1<rank_1, dim, ValueType> &funcs,
1001 const TensorType_2<rank_2, dim, ValueType> &op)
1002 {
1003 Tensor<rank_1 + rank_2, dim, ValueType> out;
1004 for (unsigned int i = 0; i < funcs.n_independent_components; ++i)
1005 {
1006 const TableIndices<rank_1> indices_i(
1007 funcs.unrolled_to_component_indices(i));
1008 for (unsigned int j = 0; j < op.n_independent_components; ++j)
1009 {
1010 const TableIndices<rank_2> indices_j(
1011 op.unrolled_to_component_indices(j));
1012 const TableIndices<rank_1 + rank_2> indices_out =
1013 concatenate_indices(indices_i, indices_j);
1014
1015 out[indices_out] =
1016 differentiate(funcs[indices_i], op[indices_j]);
1017
1018 if (is_symmetric_component(indices_j, op))
1019 out[indices_out] *= 0.5;
1020
1021 // TODO: Implement for SymmetricTensor<4,dim,...>
1022 if (std::is_same_v<
1023 TensorType_1<rank_1, dim, ValueType>,
1024 SymmetricTensor<2, dim, ValueType>>) // Symmetric function
1025 {
1026 const TableIndices<rank_1 + rank_2> indices_out_t =
1027 concatenate_indices(transpose_indices(indices_i),
1028 indices_j);
1029 out[indices_out_t] = out[indices_out];
1030 }
1031 else if (std::is_same_v<
1032 TensorType_2<rank_2, dim, ValueType>,
1033 SymmetricTensor<2, dim, ValueType>>) // Symmetric
1034 // operator
1035 {
1036 const TableIndices<rank_1 + rank_2> indices_out_t =
1037 concatenate_indices(indices_i,
1038 transpose_indices(indices_j));
1039 out[indices_out_t] = out[indices_out];
1040 }
1041 else
1042 {
1043 Assert(
1044 false,
1045 ExcMessage(
1046 "Expect mixed tensor differentiation to have at least "
1047 "one component stemming from a symmetric tensor."));
1048 }
1049 }
1050 }
1051 return out;
1052 }
1053
1054 } // namespace internal
1055
1056
1057 template <int rank, int dim>
1058 Tensor<rank, dim, Expression>
1059 differentiate(const Expression &func,
1060 const Tensor<rank, dim, Expression> &op)
1061 {
1062 return internal::scalar_diff_tensor(func, op);
1063 }
1064
1065
1066 template <int rank, int dim>
1067 SymmetricTensor<rank, dim, Expression>
1068 differentiate(const Expression &func,
1069 const SymmetricTensor<rank, dim, Expression> &op)
1070 {
1071 return internal::scalar_diff_tensor(func, op);
1072 }
1073
1074
1075 template <int rank, int dim>
1076 Tensor<rank, dim, Expression>
1077 differentiate(const Tensor<0, dim, Expression> &func,
1078 const Tensor<rank, dim, Expression> &op)
1079 {
1080 return internal::scalar_diff_tensor(func, op);
1081 }
1082
1083
1084 template <int rank, int dim>
1085 SymmetricTensor<rank, dim, Expression>
1086 differentiate(const Tensor<0, dim, Expression> &func,
1087 const SymmetricTensor<rank, dim, Expression> &op)
1088 {
1089 // Ensure that this returns a symmetric tensor by
1090 // invoking the scalar value function
1091 const Expression tmp = func;
1092 return internal::scalar_diff_tensor(tmp, op);
1093 }
1094
1095
1096 template <int rank, int dim>
1097 Tensor<rank, dim, Expression>
1098 differentiate(const Tensor<rank, dim, Expression> &symbol_tensor,
1099 const Expression &op)
1100 {
1101 return internal::tensor_diff_scalar(symbol_tensor, op);
1102 }
1103
1104
1105 template <int rank, int dim>
1106 Tensor<rank, dim, Expression>
1107 differentiate(const Tensor<rank, dim, Expression> &symbol_tensor,
1108 const Tensor<0, dim, Expression> &op)
1109 {
1110 return internal::tensor_diff_scalar(symbol_tensor, op);
1111 }
1112
1113
1114 template <int rank, int dim>
1115 SymmetricTensor<rank, dim, Expression>
1116 differentiate(const SymmetricTensor<rank, dim, Expression> &symbol_tensor,
1117 const Expression &op)
1118 {
1119 return internal::tensor_diff_scalar(symbol_tensor, op);
1120 }
1121
1122
1123 template <int rank, int dim>
1124 SymmetricTensor<rank, dim, Expression>
1125 differentiate(const SymmetricTensor<rank, dim, Expression> &symbol_tensor,
1126 const Tensor<0, dim, Expression> &op)
1127 {
1128 return internal::tensor_diff_scalar(symbol_tensor, op);
1129 }
1130
1131
1132 template <int rank_1, int rank_2, int dim>
1133 Tensor<rank_1 + rank_2, dim, Expression>
1134 differentiate(const Tensor<rank_1, dim, Expression> &symbol_tensor,
1135 const Tensor<rank_2, dim, Expression> &op)
1136 {
1137 return internal::tensor_diff_tensor(symbol_tensor, op);
1138 }
1139
1140
1141 template <int rank_1, int rank_2, int dim>
1142 SymmetricTensor<rank_1 + rank_2, dim, Expression>
1143 differentiate(const SymmetricTensor<rank_1, dim, Expression> &symbol_tensor,
1144 const SymmetricTensor<rank_2, dim, Expression> &op)
1145 {
1146 return internal::tensor_diff_tensor(symbol_tensor, op);
1147 }
1148
1149
1150 template <int rank_1, int rank_2, int dim>
1151 Tensor<rank_1 + rank_2, dim, Expression>
1152 differentiate(const Tensor<rank_1, dim, Expression> &symbol_tensor,
1153 const SymmetricTensor<rank_2, dim, Expression> &op)
1154 {
1155 return internal::tensor_diff_tensor(symbol_tensor, op);
1156 }
1157
1158
1159 template <int rank_1, int rank_2, int dim>
1160 Tensor<rank_1 + rank_2, dim, Expression>
1161 differentiate(const SymmetricTensor<rank_1, dim, Expression> &symbol_tensor,
1162 const Tensor<rank_2, dim, Expression> &op)
1163 {
1164 return internal::tensor_diff_tensor(symbol_tensor, op);
1165 }
1166
1167
1168 /* ---------------- Symbol map creation and manipulation --------------*/
1169
1170
1171 namespace internal
1172 {
1173 template <typename SymbolicType,
1174 typename ValueType,
1175 int rank,
1176 int dim,
1177 template <int, int, typename>
1178 class TensorType>
1179 void
1180 set_tensor_value_in_symbol_map(
1181 types::substitution_map &substitution_map,
1182 const TensorType<rank, dim, SymbolicType> &symbol_tensor,
1183 const TensorType<rank, dim, ValueType> &value_tensor)
1184 {
1185 TensorType<rank, dim, Expression> out;
1186 for (unsigned int i = 0; i < out.n_independent_components; ++i)
1187 {
1188 const TableIndices<rank> indices(
1189 out.unrolled_to_component_indices(i));
1190 set_value_in_symbol_map(substitution_map,
1191 symbol_tensor[indices],
1192 value_tensor[indices]);
1193 }
1194 }
1195
1196
1197 template <typename SymbolicType, typename ValueType, int dim>
1198 void
1199 set_tensor_value_in_symbol_map(
1200 types::substitution_map &substitution_map,
1201 const SymmetricTensor<4, dim, SymbolicType> &symbol_tensor,
1202 const SymmetricTensor<4, dim, ValueType> &value_tensor)
1203 {
1204 SymmetricTensor<4, dim, Expression> out;
1205 for (unsigned int i = 0;
1206 i < SymmetricTensor<2, dim>::n_independent_components;
1207 ++i)
1208 for (unsigned int j = 0;
1209 j < SymmetricTensor<2, dim>::n_independent_components;
1210 ++j)
1211 {
1212 const TableIndices<4> indices =
1213 make_rank_4_tensor_indices<dim>(i, j);
1214 set_value_in_symbol_map(substitution_map,
1215 symbol_tensor[indices],
1216 value_tensor[indices]);
1217 }
1218 }
1219 } // namespace internal
1220
1221
1222 template <bool ignore_invalid_symbols,
1223 typename ValueType,
1224 int rank,
1225 int dim,
1226 typename SymbolicType>
1227 void
1228 add_to_symbol_map(types::substitution_map &symbol_map,
1229 const Tensor<rank, dim, SymbolicType> &symbol_tensor)
1230 {
1231 // Call the above function
1232 add_to_substitution_map<ignore_invalid_symbols>(
1233 symbol_map, symbol_tensor, Tensor<rank, dim, ValueType>());
1234 }
1235
1236
1237 template <bool ignore_invalid_symbols,
1238 typename ValueType,
1239 int rank,
1240 int dim,
1241 typename SymbolicType>
1242 void
1243 add_to_symbol_map(
1244 types::substitution_map &symbol_map,
1245 const SymmetricTensor<rank, dim, SymbolicType> &symbol_tensor)
1246 {
1247 // Call the above function
1248 add_to_substitution_map<ignore_invalid_symbols>(
1249 symbol_map, symbol_tensor, SymmetricTensor<rank, dim, ValueType>());
1250 }
1251
1252
1253 template <int rank, int dim, typename SymbolicType, typename ValueType>
1254 void
1255 set_value_in_symbol_map(
1256 types::substitution_map &substitution_map,
1257 const Tensor<rank, dim, SymbolicType> &symbol_tensor,
1258 const Tensor<rank, dim, ValueType> &value_tensor)
1259 {
1260 internal::set_tensor_value_in_symbol_map(substitution_map,
1261 symbol_tensor,
1262 value_tensor);
1263 }
1264
1265
1266 template <int rank, int dim, typename SymbolicType, typename ValueType>
1267 void
1268 set_value_in_symbol_map(
1269 types::substitution_map &substitution_map,
1270 const SymmetricTensor<rank, dim, SymbolicType> &symbol_tensor,
1271 const SymmetricTensor<rank, dim, ValueType> &value_tensor)
1272 {
1273 internal::set_tensor_value_in_symbol_map(substitution_map,
1274 symbol_tensor,
1275 value_tensor);
1276 }
1277
1278
1279 /* ------------------ Symbol substitution map creation ----------------*/
1280
1281
1282 template <int rank, int dim, typename ExpressionType, typename ValueType>
1283 types::substitution_map
1284 make_substitution_map(
1285 const Tensor<rank, dim, ExpressionType> &symbol_tensor,
1286 const Tensor<rank, dim, ValueType> &value_tensor)
1287 {
1288 types::substitution_map substitution_map;
1289 add_to_substitution_map(substitution_map, symbol_tensor, value_tensor);
1290 return substitution_map;
1291 }
1292
1293
1294 template <int rank, int dim, typename ExpressionType, typename ValueType>
1295 types::substitution_map
1296 make_substitution_map(
1297 const SymmetricTensor<rank, dim, ExpressionType> &symbol_tensor,
1298 const SymmetricTensor<rank, dim, ValueType> &value_tensor)
1299 {
1300 types::substitution_map substitution_map;
1301 add_to_substitution_map(substitution_map, symbol_tensor, value_tensor);
1302 return substitution_map;
1303 }
1304
1305
1306 /* ---------------- Symbolic substitution map enlargement --------------*/
1307
1308
1309 namespace internal
1310 {
1311 template <int rank,
1312 int dim,
1313 typename ExpressionType,
1314 typename ValueType,
1315 template <int, int, typename>
1316 class TensorType>
1317 std::vector<std::pair<ExpressionType, ValueType>>
1318 make_tensor_entries_for_substitution_map(
1319 const TensorType<rank, dim, ExpressionType> &symbol_tensor,
1320 const TensorType<rank, dim, ValueType> &value_tensor)
1321 {
1322 std::vector<std::pair<ExpressionType, ValueType>> symbol_values;
1323 for (unsigned int i = 0; i < symbol_tensor.n_independent_components;
1324 ++i)
1325 {
1326 const TableIndices<rank> indices(
1327 symbol_tensor.unrolled_to_component_indices(i));
1328 symbol_values.push_back(
1329 std::make_pair(symbol_tensor[indices], value_tensor[indices]));
1330 }
1331 return symbol_values;
1332 }
1333
1334
1335 template <int dim, typename ExpressionType, typename ValueType>
1336 std::vector<std::pair<ExpressionType, ValueType>>
1337 make_tensor_entries_for_substitution_map(
1338 const Tensor<0, dim, ExpressionType> &symbol_tensor,
1339 const Tensor<0, dim, ValueType> &value_tensor)
1340 {
1341 const ExpressionType &expression = symbol_tensor;
1342 const ValueType &value = value_tensor;
1343 return {std::make_pair(expression, value)};
1344 }
1345
1346
1347 template <int dim, typename ExpressionType, typename ValueType>
1348 std::vector<std::pair<ExpressionType, ValueType>>
1349 make_tensor_entries_for_substitution_map(
1350 const SymmetricTensor<4, dim, ExpressionType> &symbol_tensor,
1351 const SymmetricTensor<4, dim, ValueType> &value_tensor)
1352 {
1353 std::vector<std::pair<ExpressionType, ValueType>> symbol_values;
1354 for (unsigned int i = 0;
1355 i < SymmetricTensor<2, dim>::n_independent_components;
1356 ++i)
1357 for (unsigned int j = 0;
1358 j < SymmetricTensor<2, dim>::n_independent_components;
1359 ++j)
1360 {
1361 const TableIndices<4> indices =
1362 make_rank_4_tensor_indices<dim>(i, j);
1363 symbol_values.push_back(
1364 std::make_pair(symbol_tensor[indices], value_tensor[indices]));
1365 }
1366 return symbol_values;
1367 }
1368 } // namespace internal
1369
1370
1371 template <bool ignore_invalid_symbols,
1372 int rank,
1373 int dim,
1374 typename ExpressionType,
1375 typename ValueType>
1376 void
1377 add_to_substitution_map(
1378 types::substitution_map &substitution_map,
1379 const Tensor<rank, dim, ExpressionType> &symbol_tensor,
1380 const Tensor<rank, dim, ValueType> &value_tensor)
1381 {
1382 add_to_substitution_map<ignore_invalid_symbols>(
1383 substitution_map,
1384 internal::make_tensor_entries_for_substitution_map(symbol_tensor,
1385 value_tensor));
1386 }
1387
1388
1389 template <bool ignore_invalid_symbols,
1390 int rank,
1391 int dim,
1392 typename ExpressionType,
1393 typename ValueType>
1394 void
1395 add_to_substitution_map(
1396 types::substitution_map &substitution_map,
1397 const SymmetricTensor<rank, dim, ExpressionType> &symbol_tensor,
1398 const SymmetricTensor<rank, dim, ValueType> &value_tensor)
1399 {
1400 add_to_substitution_map<ignore_invalid_symbols>(
1401 substitution_map,
1402 internal::make_tensor_entries_for_substitution_map(symbol_tensor,
1403 value_tensor));
1404 }
1405
1406
1407 /* ---------------- Symbol substitution and evaluation --------------*/
1408
1409
1410 namespace internal
1411 {
1412 template <int rank,
1413 int dim,
1414 template <int, int, typename>
1415 class TensorType>
1416 TensorType<rank, dim, Expression>
1417 substitute_tensor(
1418 const TensorType<rank, dim, Expression> &expression_tensor,
1419 const types::substitution_map &substitution_map)
1420 {
1421 TensorType<rank, dim, Expression> out;
1422 for (unsigned int i = 0; i < out.n_independent_components; ++i)
1423 {
1424 const TableIndices<rank> indices(
1425 out.unrolled_to_component_indices(i));
1426 out[indices] =
1427 substitute(expression_tensor[indices], substitution_map);
1428 }
1429 return out;
1430 }
1431
1432
1433 template <int dim>
1434 Tensor<0, dim, Expression>
1435 substitute_tensor(const Tensor<0, dim, Expression> &expression_tensor,
1436 const types::substitution_map &substitution_map)
1437 {
1438 const Expression &expression = expression_tensor;
1439 return substitute(expression, substitution_map);
1440 }
1441
1442
1443 template <int dim>
1444 SymmetricTensor<4, dim, Expression>
1445 substitute_tensor(
1446 const SymmetricTensor<4, dim, Expression> &expression_tensor,
1447 const types::substitution_map &substitution_map)
1448 {
1449 SymmetricTensor<4, dim, Expression> out;
1450 for (unsigned int i = 0;
1451 i < SymmetricTensor<2, dim>::n_independent_components;
1452 ++i)
1453 for (unsigned int j = 0;
1454 j < SymmetricTensor<2, dim>::n_independent_components;
1455 ++j)
1456 {
1457 const TableIndices<4> indices =
1458 make_rank_4_tensor_indices<dim>(i, j);
1459 out[indices] =
1460 substitute(expression_tensor[indices], substitution_map);
1461 }
1462 return out;
1463 }
1464
1465
1466 template <typename ValueType,
1467 int rank,
1468 int dim,
1469 template <int, int, typename>
1470 class TensorType>
1471 TensorType<rank, dim, ValueType>
1472 substitute_and_evaluate_tensor(
1473 const TensorType<rank, dim, Expression> &expression_tensor,
1474 const types::substitution_map &substitution_map)
1475 {
1476 TensorType<rank, dim, ValueType> out;
1477 for (unsigned int i = 0; i < out.n_independent_components; ++i)
1478 {
1479 const TableIndices<rank> indices(
1480 out.unrolled_to_component_indices(i));
1481 out[indices] =
1482 substitute_and_evaluate<ValueType>(expression_tensor[indices],
1483 substitution_map);
1484 }
1485 return out;
1486 }
1487
1488
1489 template <typename ValueType, int dim>
1490 Tensor<0, dim, ValueType>
1491 substitute_and_evaluate_tensor(
1492 const Tensor<0, dim, Expression> &expression_tensor,
1493 const types::substitution_map &substitution_map)
1494 {
1495 const Expression &expression = expression_tensor;
1496 return substitute_and_evaluate<ValueType>(expression, substitution_map);
1497 }
1498
1499
1500 template <typename ValueType, int dim>
1501 SymmetricTensor<4, dim, ValueType>
1502 substitute_and_evaluate_tensor(
1503 const SymmetricTensor<4, dim, Expression> &expression_tensor,
1504 const types::substitution_map &substitution_map)
1505 {
1506 SymmetricTensor<4, dim, ValueType> out;
1507 for (unsigned int i = 0;
1508 i < SymmetricTensor<2, dim>::n_independent_components;
1509 ++i)
1510 for (unsigned int j = 0;
1511 j < SymmetricTensor<2, dim>::n_independent_components;
1512 ++j)
1513 {
1514 const TableIndices<4> indices =
1515 make_rank_4_tensor_indices<dim>(i, j);
1516 out[indices] =
1517 substitute_and_evaluate<ValueType>(expression_tensor[indices],
1518 substitution_map);
1519 }
1520 return out;
1521 }
1522 } // namespace internal
1523
1524
1525 template <int rank, int dim>
1526 Tensor<rank, dim, Expression>
1527 substitute(const Tensor<rank, dim, Expression> &expression_tensor,
1528 const types::substitution_map &substitution_map)
1529 {
1530 return internal::substitute_tensor(expression_tensor, substitution_map);
1531 }
1532
1533
1534 template <int rank, int dim>
1535 SymmetricTensor<rank, dim, Expression>
1536 substitute(const SymmetricTensor<rank, dim, Expression> &expression_tensor,
1537 const types::substitution_map &substitution_map)
1538 {
1539 return internal::substitute_tensor(expression_tensor, substitution_map);
1540 }
1541
1542
1543 template <typename ValueType, int rank, int dim>
1544 Tensor<rank, dim, ValueType>
1545 substitute_and_evaluate(
1546 const Tensor<rank, dim, Expression> &expression_tensor,
1547 const types::substitution_map &substitution_map)
1548 {
1549 return internal::substitute_and_evaluate_tensor<ValueType>(
1550 expression_tensor, substitution_map);
1551 }
1552
1553
1554 template <typename ValueType, int rank, int dim>
1555 SymmetricTensor<rank, dim, ValueType>
1556 substitute_and_evaluate(
1557 const SymmetricTensor<rank, dim, Expression> &expression_tensor,
1558 const types::substitution_map &substitution_map)
1559 {
1560 return internal::substitute_and_evaluate_tensor<ValueType>(
1561 expression_tensor, substitution_map);
1562 }
1563
1564
1565
1566 } // namespace SD
1567 } // namespace Differentiation
1568
1569 # endif // DOXYGEN
1570
1571 DEAL_II_NAMESPACE_CLOSE
1572
1573 #else
1574
1575 // Make sure the scripts that create the C++20 module input files have
1576 // something to latch on if the preprocessor #ifdef above would
1577 // otherwise lead to an empty content of the file.
1578 DEAL_II_NAMESPACE_OPEN
1579 DEAL_II_NAMESPACE_CLOSE
1580
1581 #endif // DEAL_II_WITH_SYMENGINE
1582
1583 #endif