deal.II version GIT relicensing-6839-g338455934c 2026-10-02 12:10:01+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
symengine_tensor_operations.cc
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2019 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13
14#include <deal.II/base/config.h>
15
16#ifdef DEAL_II_WITH_SYMENGINE
17
19# include <deal.II/base/tensor.h>
21
27
28
29#endif // DEAL_II_WITH_SYMENGINE
30
32
33#ifdef DEAL_II_WITH_SYMENGINE
34
35namespace Differentiation
36{
37 namespace SD
38 {
39 // --- Symbolic variable creation ---
40
41 namespace internal
42 {
43 namespace
44 {
45 template <int rank>
46 std::string
47 make_index_string(const TableIndices<rank> &indices)
48 {
49 std::string out;
50 for (unsigned int i = 0; i < rank; ++i)
51 out += std::to_string(indices[i]);
52 return out;
53 }
54
55
56 template <int rank, int dim>
57 struct Symbol_Tensor;
58
59
60 template <int rank, int dim>
61 struct Symbol_Tensor
62 {
64 create(const std::string &sym)
65 {
67 for (unsigned int i = 0; i < out.n_independent_components; ++i)
68 {
69 const TableIndices<rank> indices(
71 out[indices] =
72 Expression(sym + "_" + internal::make_index_string(indices));
73 }
74 return out;
75 }
76 };
77
78
79 template <int dim>
80 struct Symbol_Tensor<0, dim>
81 {
83 create(const std::string &sym)
84 {
85 return make_symbol(sym);
86 }
87 };
88
89
90 template <int rank, int dim>
91 struct Symbol_SymmetricTensor;
92
93
94 template <int rank, int dim>
95 struct Symbol_SymmetricTensor
96 {
98 create(const std::string &sym)
99 {
101 for (unsigned int i = 0; i < out.n_independent_components; ++i)
102 {
103 const TableIndices<rank> indices(
105 out[indices] =
106 Expression(sym + "_" + internal::make_index_string(indices));
107 }
108 return out;
109 }
110 };
111
112
113 template <int dim>
114 struct Symbol_SymmetricTensor<4, dim>
115 {
117 create(const std::string &sym)
118 {
120 for (unsigned int i = 0;
121 i < SymmetricTensor<2, dim>::n_independent_components;
122 ++i)
123 for (unsigned int j = 0;
124 j < SymmetricTensor<2, dim>::n_independent_components;
125 ++j)
126 {
127 const TableIndices<4> indices =
128 make_rank_4_tensor_indices<dim>(i, j);
129 out[indices] = Expression(
130 sym + "_" + internal::make_index_string(indices));
131 }
132 return out;
133 }
134 };
135
136
137 template <int rank, int dim>
138 struct Symbol_Function_Tensor;
139
140
141 template <int rank, int dim>
142 struct Symbol_Function_Tensor
143 {
145 create(const std::string &sym,
146 const types::substitution_map &arguments)
147 {
149 const types::symbol_vector args =
151 for (unsigned int i = 0; i < out.n_independent_components; ++i)
152 {
153 const TableIndices<rank> indices(
155 out[indices] =
156 Expression(sym + "_" + internal::make_index_string(indices),
157 args);
158 }
159 return out;
160 }
161 };
162
163
164 template <int dim>
165 struct Symbol_Function_Tensor<0, dim>
166 {
168 create(const std::string &sym,
169 const types::substitution_map &arguments)
170 {
171 return make_symbolic_function(sym, arguments);
172 }
173 };
174
175
176 template <int rank, int dim>
177 struct Symbol_Function_SymmetricTensor;
178
179
180 template <int rank, int dim>
181 struct Symbol_Function_SymmetricTensor
182 {
184 create(const std::string &sym,
185 const types::substitution_map &arguments)
186 {
188 const types::symbol_vector args =
190 for (unsigned int i = 0; i < out.n_independent_components; ++i)
191 {
192 const TableIndices<rank> indices(
194 out[indices] =
195 Expression(sym + "_" + internal::make_index_string(indices),
196 args);
197 }
198 return out;
199 }
200 };
201
202
203 template <int dim>
204 struct Symbol_Function_SymmetricTensor<4, dim>
205 {
207 create(const std::string &sym,
208 const types::substitution_map &arguments)
209 {
211 const types::symbol_vector args =
213 for (unsigned int i = 0;
214 i < SymmetricTensor<2, dim>::n_independent_components;
215 ++i)
216 for (unsigned int j = 0;
217 j < SymmetricTensor<2, dim>::n_independent_components;
218 ++j)
219 {
220 const TableIndices<4> indices =
221 make_rank_4_tensor_indices<dim>(i, j);
222 out[indices] =
223 Expression(sym + "_" + internal::make_index_string(indices),
224 args);
225 }
226 return out;
227 }
228 };
229 } // namespace
230 } // namespace internal
231
232
233 template <int dim>
235 make_vector_of_symbols(const std::string &sym)
236 {
237 return internal::Symbol_Tensor<1, dim>::create(sym);
238 }
239
240
241# ifndef DOXYGEN
242 template <int dim>
244 make_vector_of_symbolic_functions(const std::string &sym,
245 const types::substitution_map &arguments)
246 {
247 return internal::Symbol_Function_Tensor<1, dim>::create(sym, arguments);
248 }
249# endif
250
251
252 template <int rank, int dim>
254 make_tensor_of_symbols(const std::string &sym)
255 {
256 return internal::Symbol_Tensor<rank, dim>::create(sym);
257 }
258
259
260 template <int rank, int dim>
262 make_symmetric_tensor_of_symbols(const std::string &sym)
263 {
264 return internal::Symbol_SymmetricTensor<rank, dim>::create(sym);
265 }
266
267
268# ifndef DOXYGEN
269 template <int rank, int dim>
271 make_tensor_of_symbolic_functions(const std::string &sym,
272 const types::substitution_map &arguments)
273 {
274 return internal::Symbol_Function_Tensor<rank, dim>::create(sym,
275 arguments);
276 }
277
278
279 template <int rank, int dim>
282 const std::string &sym,
283 const types::substitution_map &arguments)
284 {
285 return internal::Symbol_Function_SymmetricTensor<rank, dim>::create(
286 sym, arguments);
287 }
288# endif // DOXYGEN
289
290 } // namespace SD
291} // namespace Differentiation
292
293/* --- Explicit instantiations --- */
294
295# include "differentiation/sd/symengine_tensor_operations.inst"
296
297
298#endif // DEAL_II_WITH_SYMENGINE
static constexpr TableIndices< rank_ > unrolled_to_component_indices(const unsigned int i)
static constexpr unsigned int n_independent_components
static constexpr TableIndices< rank_ > unrolled_to_component_indices(const unsigned int i)
static constexpr unsigned int n_independent_components
Definition tensor.h:496
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
SD::types::symbol_vector extract_symbols(const SD::types::substitution_map &substitution_values)
std::vector< SD::Expression > symbol_vector
std::map< SD::Expression, SD::Expression, internal::ExpressionKeyLess > substitution_map
Tensor< rank, dim, Expression > make_tensor_of_symbols(const std::string &symbol)
Expression make_symbolic_function(const std::string &symbol, const types::symbol_vector &arguments)
SymmetricTensor< rank, dim, Expression > make_symmetric_tensor_of_symbols(const std::string &symbol)
Tensor< 1, dim, Expression > make_vector_of_symbolic_functions(const std::string &symbol, const types::substitution_map &arguments)
SymmetricTensor< rank, dim, Expression > make_symmetric_tensor_of_symbolic_functions(const std::string &symbol, const types::substitution_map &arguments)
Tensor< 1, dim, Expression > make_vector_of_symbols(const std::string &symbol)
Tensor< rank, dim, Expression > make_tensor_of_symbolic_functions(const std::string &symbol, const types::substitution_map &arguments)
Expression make_symbol(const std::string &symbol)