version GIT relicensing-7278-g091132d5eb 2026-12-02 03: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.h
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#ifndef dealii_differentiation_sd_symengine_tensor_operations_h
14#define dealii_differentiation_sd_symengine_tensor_operations_h
15
16#include <deal.II/base/config.h>
17
18#ifdef DEAL_II_WITH_SYMENGINE
19
21# include <deal.II/base/tensor.h>
22
26
27# include <utility>
28# include <vector>
29
30#endif // DEAL_II_WITH_SYMENGINE
31
33
34#ifdef DEAL_II_WITH_SYMENGINE
35namespace Differentiation
36{
37 namespace SD
38 {
62 template <int dim>
64 make_vector_of_symbols(const std::string &symbol);
65
85 template <int rank, int dim>
87 make_tensor_of_symbols(const std::string &symbol);
88
108 template <int rank, int dim>
110 make_symmetric_tensor_of_symbols(const std::string &symbol);
111
131 template <int dim>
133 make_vector_of_symbolic_functions(const std::string &symbol,
134 const types::substitution_map &arguments);
135
156 template <int rank, int dim>
158 make_tensor_of_symbolic_functions(const std::string &symbol,
159 const types::substitution_map &arguments);
160
181 template <int rank, int dim>
184 const std::string &symbol,
185 const types::substitution_map &arguments);
186
203 template <int rank, int dim>
206
216 template <int rank, int dim>
220
230 template <int rank, int dim>
234
244 template <int rank, int dim>
248
258 template <int rank, int dim>
261
272 template <int rank, int dim>
275 const Expression &x);
276
287 template <int rank, int dim>
291
303 template <int rank, int dim>
307
318 template <int rank_1, int rank_2, int dim>
322
335 template <int rank_1, int rank_2, int dim>
339
350 template <int rank_1, int rank_2, int dim>
354
365 template <int rank_1, int rank_2, int dim>
369
401 template <bool ignore_invalid_symbols = false,
402 typename ValueType = double,
403 int rank,
404 int dim,
405 typename SymbolicType>
406 void
408 const Tensor<rank, dim, SymbolicType> &symbol_tensor);
409
434 template <bool ignore_invalid_symbols = false,
435 typename ValueType = double,
436 int rank,
437 int dim,
438 typename SymbolicType>
439 void
441 types::substitution_map &symbol_map,
442 const SymmetricTensor<rank, dim, SymbolicType> &symbol_tensor);
443
464 template <int rank, int dim, typename SymbolicType, typename ValueType>
465 void
467 types::substitution_map &substitution_map,
468 const Tensor<rank, dim, SymbolicType> &symbol_tensor,
469 const Tensor<rank, dim, ValueType> &value_tensor);
470
491 template <int rank, int dim, typename SymbolicType, typename ValueType>
492 void
494 types::substitution_map &substitution_map,
495 const SymmetricTensor<rank, dim, SymbolicType> &symbol_tensor,
496 const SymmetricTensor<rank, dim, ValueType> &value_tensor);
497
526 template <int rank, int dim, typename ExpressionType, typename ValueType>
529 const Tensor<rank, dim, ExpressionType> &symbol_tensor,
530 const Tensor<rank, dim, ValueType> &value_tensor);
531
553 template <int rank, int dim, typename ExpressionType, typename ValueType>
557 const SymmetricTensor<rank, dim, ValueType> &value_tensor);
558
592 template <bool ignore_invalid_symbols = false,
593 int rank,
594 int dim,
595 typename ExpressionType,
596 typename ValueType>
597 void
599 types::substitution_map &substitution_map,
600 const Tensor<rank, dim, ExpressionType> &symbol_tensor,
601 const Tensor<rank, dim, ValueType> &value_tensor);
602
628 template <bool ignore_invalid_symbols = false,
629 int rank,
630 int dim,
631 typename ExpressionType,
632 typename ValueType>
633 void
635 types::substitution_map &substitution_map,
637 const SymmetricTensor<rank, dim, ValueType> &value_tensor);
638
665 template <int rank, int dim>
668 const types::substitution_map &substitution_map);
669
689 template <int rank, int dim>
692 const types::substitution_map &substitution_map);
693
720 template <typename ValueType, int rank, int dim>
723 const Tensor<rank, dim, Expression> &expression_tensor,
724 const types::substitution_map &substitution_map);
725
752 template <typename ValueType, int rank, int dim>
755 const SymmetricTensor<rank, dim, Expression> &expression_tensor,
756 const types::substitution_map &substitution_map);
757
760 } // namespace SD
761} // namespace Differentiation
762
763
764/* -------------------- inline and template functions ------------------ */
765
766
767# ifndef DOXYGEN
768
769namespace Differentiation
770{
771 namespace SD
772 {
773 /* ---------------- Symbolic differentiation --------------*/
774
775
776 namespace internal
777 {
778 template <int dim>
780 make_rank_4_tensor_indices(const unsigned int idx_i,
781 const unsigned int idx_j)
782 {
783 const TableIndices<2> indices_i(
785 const TableIndices<2> indices_j(
787 return TableIndices<4>(indices_i[0],
788 indices_i[1],
789 indices_j[0],
790 indices_j[1]);
791 }
792
793
794 template <int rank_1, int rank_2>
796 concatenate_indices(const TableIndices<rank_1> &indices_1,
797 const TableIndices<rank_2> &indices_2)
798 {
800 for (unsigned int i = 0; i < rank_1; ++i)
801 indices_out[i] = indices_1[i];
802 for (unsigned int j = 0; j < rank_2; ++j)
803 indices_out[rank_1 + j] = indices_2[j];
804 return indices_out;
805 }
806
807
808 template <int rank>
810 transpose_indices(const TableIndices<rank> &indices)
811 {
812 return indices;
813 }
814
815
816 template <>
817 inline TableIndices<2>
818 transpose_indices(const TableIndices<2> &indices)
819 {
820 return TableIndices<2>(indices[1], indices[0]);
821 }
822
823
824 template <int rank, int dim, typename ValueType>
825 bool
826 is_symmetric_component(const TableIndices<rank> &,
828 {
829 return false;
830 }
831
832
833 template <int rank, int dim, typename ValueType>
834 bool
835 is_symmetric_component(const TableIndices<rank> &,
837 {
838 static_assert(
839 rank == 0 || rank == 2,
840 "Querying symmetric component for non rank-2 symmetric tensor index is not allowed.");
841 return false;
842 }
843
844
845 template <int dim, typename ValueType>
846 bool
847 is_symmetric_component(const TableIndices<2> &table_indices,
849 {
850 return table_indices[0] != table_indices[1];
851 }
852
853
854 template <int dim,
855 typename ValueType = Expression,
856 template <int, int, typename> 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> class TensorType>
869 TensorType<rank, dim, ValueType>
870 scalar_diff_tensor(const ValueType &func,
871 const TensorType<rank, dim, ValueType> &op)
872 {
873 TensorType<rank, dim, ValueType> out;
874 for (unsigned int i = 0; i < out.n_independent_components; ++i)
875 {
876 const TableIndices<rank> indices(
877 out.unrolled_to_component_indices(i));
878 out[indices] = differentiate(func, op[indices]);
879
880 if (is_symmetric_component(indices, op))
881 out[indices] *= 0.5;
882 }
883 return out;
884 }
885
886
887 // Specialization for rank-0 tensor
888 template <int rank,
889 int dim,
890 typename ValueType = Expression,
891 template <int, int, typename> class TensorType>
892 TensorType<rank, dim, ValueType>
893 tensor_diff_tensor(const TensorType<0, dim, ValueType> &func,
894 const TensorType<rank, dim, ValueType> &op)
895 {
896 TensorType<rank, dim, ValueType> out;
897 for (unsigned int i = 0; i < out.n_independent_components; ++i)
898 {
899 const TableIndices<rank> indices(
900 out.unrolled_to_component_indices(i));
901 out[indices] = differentiate(func, op[indices]);
902
903 if (is_symmetric_component(indices, op))
904 out[indices] *= 0.5;
905 }
906 return out;
907 }
908
909
910 template <int rank,
911 int dim,
912 typename ValueType = Expression,
913 template <int, int, typename> class TensorType>
914 TensorType<rank, dim, ValueType>
915 tensor_diff_scalar(const TensorType<rank, dim, ValueType> &funcs,
916 const ValueType &op)
917 {
918 TensorType<rank, dim, ValueType> out;
919 for (unsigned int i = 0; i < out.n_independent_components; ++i)
920 {
921 const TableIndices<rank> indices(
922 out.unrolled_to_component_indices(i));
923 out[indices] = differentiate(funcs[indices], op);
924 }
925 return out;
926 }
927
928
929 // Specialization for rank-0 tensor
930 template <int rank,
931 int dim,
932 typename ValueType = Expression,
933 template <int, int, typename> class TensorType>
934 TensorType<rank, dim, ValueType>
935 tensor_diff_tensor(const TensorType<rank, dim, ValueType> &funcs,
936 const TensorType<0, dim, ValueType> &op)
937 {
938 TensorType<rank, dim, ValueType> out;
939 for (unsigned int i = 0; i < out.n_independent_components; ++i)
940 {
941 const TableIndices<rank> indices(
942 out.unrolled_to_component_indices(i));
943 out[indices] = differentiate(funcs[indices], op);
944 }
945 return out;
946 }
947
948
949 // For either symmetric or normal tensors
950 template <int rank_1,
951 int rank_2,
952 int dim,
953 typename ValueType = Expression,
954 template <int, int, typename> class TensorType>
955 TensorType<rank_1 + rank_2, dim, ValueType>
956 tensor_diff_tensor(const TensorType<rank_1, dim, ValueType> &funcs,
957 const TensorType<rank_2, dim, ValueType> &op)
958 {
959 TensorType<rank_1 + rank_2, dim, ValueType> out;
960 for (unsigned int i = 0; i < funcs.n_independent_components; ++i)
961 {
962 const TableIndices<rank_1> indices_i(
963 funcs.unrolled_to_component_indices(i));
964 for (unsigned int j = 0; j < op.n_independent_components; ++j)
965 {
966 const TableIndices<rank_2> indices_j(
967 op.unrolled_to_component_indices(j));
968 const TableIndices<rank_1 + rank_2> indices_out =
969 concatenate_indices(indices_i, indices_j);
970
971 out[indices_out] =
972 differentiate(funcs[indices_i], op[indices_j]);
973
974 if (is_symmetric_component(indices_j, op))
975 out[indices_out] *= 0.5;
976 }
977 }
978 return out;
979 }
980
981
982 // For mixed symmetric/standard tensors
983 // The return type is always a standard tensor, since we cannot be sure
984 // that any symmetries exist in either the function tensor or the
985 // differential operator.
986 template <int rank_1,
987 int rank_2,
988 int dim,
989 typename ValueType = Expression,
990 template <int, int, typename> class TensorType_1,
991 template <int, int, typename> class TensorType_2>
993 tensor_diff_tensor(const TensorType_1<rank_1, dim, ValueType> &funcs,
994 const TensorType_2<rank_2, dim, ValueType> &op)
995 {
997 for (unsigned int i = 0; i < funcs.n_independent_components; ++i)
998 {
999 const TableIndices<rank_1> indices_i(
1000 funcs.unrolled_to_component_indices(i));
1001 for (unsigned int j = 0; j < op.n_independent_components; ++j)
1002 {
1003 const TableIndices<rank_2> indices_j(
1004 op.unrolled_to_component_indices(j));
1005 const TableIndices<rank_1 + rank_2> indices_out =
1006 concatenate_indices(indices_i, indices_j);
1007
1008 out[indices_out] =
1009 differentiate(funcs[indices_i], op[indices_j]);
1010
1011 if (is_symmetric_component(indices_j, op))
1012 out[indices_out] *= 0.5;
1013
1014 // TODO: Implement for SymmetricTensor<4,dim,...>
1015 if (std::is_same_v<
1016 TensorType_1<rank_1, dim, ValueType>,
1017 SymmetricTensor<2, dim, ValueType>>) // Symmetric function
1018 {
1019 const TableIndices<rank_1 + rank_2> indices_out_t =
1020 concatenate_indices(transpose_indices(indices_i),
1021 indices_j);
1022 out[indices_out_t] = out[indices_out];
1023 }
1024 else if (std::is_same_v<
1025 TensorType_2<rank_2, dim, ValueType>,
1027 // operator
1028 {
1029 const TableIndices<rank_1 + rank_2> indices_out_t =
1030 concatenate_indices(indices_i,
1031 transpose_indices(indices_j));
1032 out[indices_out_t] = out[indices_out];
1033 }
1034 else
1035 {
1036 Assert(
1037 false,
1038 ExcMessage(
1039 "Expect mixed tensor differentiation to have at least "
1040 "one component stemming from a symmetric tensor."));
1041 }
1042 }
1043 }
1044 return out;
1045 }
1046
1047 } // namespace internal
1048
1049
1050 template <int rank, int dim>
1052 differentiate(const Expression &func,
1054 {
1055 return internal::scalar_diff_tensor(func, op);
1056 }
1057
1058
1059 template <int rank, int dim>
1061 differentiate(const Expression &func,
1063 {
1064 return internal::scalar_diff_tensor(func, op);
1065 }
1066
1067
1068 template <int rank, int dim>
1072 {
1073 return internal::scalar_diff_tensor(func, op);
1074 }
1075
1076
1077 template <int rank, int dim>
1081 {
1082 // Ensure that this returns a symmetric tensor by
1083 // invoking the scalar value function
1084 const Expression tmp = func;
1085 return internal::scalar_diff_tensor(tmp, op);
1086 }
1087
1088
1089 template <int rank, int dim>
1091 differentiate(const Tensor<rank, dim, Expression> &symbol_tensor,
1092 const Expression &op)
1093 {
1094 return internal::tensor_diff_scalar(symbol_tensor, op);
1095 }
1096
1097
1098 template <int rank, int dim>
1100 differentiate(const Tensor<rank, dim, Expression> &symbol_tensor,
1102 {
1103 return internal::tensor_diff_scalar(symbol_tensor, op);
1104 }
1105
1106
1107 template <int rank, int dim>
1110 const Expression &op)
1111 {
1112 return internal::tensor_diff_scalar(symbol_tensor, op);
1113 }
1114
1115
1116 template <int rank, int dim>
1120 {
1121 return internal::tensor_diff_scalar(symbol_tensor, op);
1122 }
1123
1124
1125 template <int rank_1, int rank_2, int dim>
1129 {
1130 return internal::tensor_diff_tensor(symbol_tensor, op);
1131 }
1132
1133
1134 template <int rank_1, int rank_2, int dim>
1138 {
1139 return internal::tensor_diff_tensor(symbol_tensor, op);
1140 }
1141
1142
1143 template <int rank_1, int rank_2, int dim>
1147 {
1148 return internal::tensor_diff_tensor(symbol_tensor, op);
1149 }
1150
1151
1152 template <int rank_1, int rank_2, int dim>
1156 {
1157 return internal::tensor_diff_tensor(symbol_tensor, op);
1158 }
1159
1160
1161 /* ---------------- Symbol map creation and manipulation --------------*/
1162
1163
1164 namespace internal
1165 {
1166 template <typename SymbolicType,
1167 typename ValueType,
1168 int rank,
1169 int dim,
1170 template <int, int, typename> class TensorType>
1171 void
1172 set_tensor_value_in_symbol_map(
1173 types::substitution_map &substitution_map,
1174 const TensorType<rank, dim, SymbolicType> &symbol_tensor,
1175 const TensorType<rank, dim, ValueType> &value_tensor)
1176 {
1177 TensorType<rank, dim, Expression> out;
1178 for (unsigned int i = 0; i < out.n_independent_components; ++i)
1179 {
1180 const TableIndices<rank> indices(
1181 out.unrolled_to_component_indices(i));
1182 set_value_in_symbol_map(substitution_map,
1183 symbol_tensor[indices],
1184 value_tensor[indices]);
1185 }
1186 }
1187
1188
1189 template <typename SymbolicType, typename ValueType, int dim>
1190 void
1191 set_tensor_value_in_symbol_map(
1192 types::substitution_map &substitution_map,
1193 const SymmetricTensor<4, dim, SymbolicType> &symbol_tensor,
1194 const SymmetricTensor<4, dim, ValueType> &value_tensor)
1195 {
1197 for (unsigned int i = 0;
1198 i < SymmetricTensor<2, dim>::n_independent_components;
1199 ++i)
1200 for (unsigned int j = 0;
1201 j < SymmetricTensor<2, dim>::n_independent_components;
1202 ++j)
1203 {
1204 const TableIndices<4> indices =
1205 make_rank_4_tensor_indices<dim>(i, j);
1206 set_value_in_symbol_map(substitution_map,
1207 symbol_tensor[indices],
1208 value_tensor[indices]);
1209 }
1210 }
1211 } // namespace internal
1212
1213
1214 template <bool ignore_invalid_symbols,
1215 typename ValueType,
1216 int rank,
1217 int dim,
1218 typename SymbolicType>
1219 void
1221 const Tensor<rank, dim, SymbolicType> &symbol_tensor)
1222 {
1223 // Call the above function
1224 add_to_substitution_map<ignore_invalid_symbols>(
1225 symbol_map, symbol_tensor, Tensor<rank, dim, ValueType>());
1226 }
1227
1228
1229 template <bool ignore_invalid_symbols,
1230 typename ValueType,
1231 int rank,
1232 int dim,
1233 typename SymbolicType>
1234 void
1236 types::substitution_map &symbol_map,
1237 const SymmetricTensor<rank, dim, SymbolicType> &symbol_tensor)
1238 {
1239 // Call the above function
1240 add_to_substitution_map<ignore_invalid_symbols>(
1241 symbol_map, symbol_tensor, SymmetricTensor<rank, dim, ValueType>());
1242 }
1243
1244
1245 template <int rank, int dim, typename SymbolicType, typename ValueType>
1246 void
1248 types::substitution_map &substitution_map,
1249 const Tensor<rank, dim, SymbolicType> &symbol_tensor,
1250 const Tensor<rank, dim, ValueType> &value_tensor)
1251 {
1252 internal::set_tensor_value_in_symbol_map(substitution_map,
1253 symbol_tensor,
1254 value_tensor);
1255 }
1256
1257
1258 template <int rank, int dim, typename SymbolicType, typename ValueType>
1259 void
1261 types::substitution_map &substitution_map,
1262 const SymmetricTensor<rank, dim, SymbolicType> &symbol_tensor,
1263 const SymmetricTensor<rank, dim, ValueType> &value_tensor)
1264 {
1265 internal::set_tensor_value_in_symbol_map(substitution_map,
1266 symbol_tensor,
1267 value_tensor);
1268 }
1269
1270
1271 /* ------------------ Symbol substitution map creation ----------------*/
1272
1273
1274 template <int rank, int dim, typename ExpressionType, typename ValueType>
1277 const Tensor<rank, dim, ExpressionType> &symbol_tensor,
1278 const Tensor<rank, dim, ValueType> &value_tensor)
1279 {
1281 add_to_substitution_map(substitution_map, symbol_tensor, value_tensor);
1282 return substitution_map;
1283 }
1284
1285
1286 template <int rank, int dim, typename ExpressionType, typename ValueType>
1289 const SymmetricTensor<rank, dim, ExpressionType> &symbol_tensor,
1290 const SymmetricTensor<rank, dim, ValueType> &value_tensor)
1291 {
1293 add_to_substitution_map(substitution_map, symbol_tensor, value_tensor);
1294 return substitution_map;
1295 }
1296
1297
1298 /* ---------------- Symbolic substitution map enlargement --------------*/
1299
1300
1301 namespace internal
1302 {
1303 template <int rank,
1304 int dim,
1305 typename ExpressionType,
1306 typename ValueType,
1307 template <int, int, typename> class TensorType>
1308 std::vector<std::pair<ExpressionType, ValueType>>
1309 make_tensor_entries_for_substitution_map(
1310 const TensorType<rank, dim, ExpressionType> &symbol_tensor,
1311 const TensorType<rank, dim, ValueType> &value_tensor)
1312 {
1313 std::vector<std::pair<ExpressionType, ValueType>> symbol_values;
1314 for (unsigned int i = 0; i < symbol_tensor.n_independent_components;
1315 ++i)
1316 {
1317 const TableIndices<rank> indices(
1318 symbol_tensor.unrolled_to_component_indices(i));
1319 symbol_values.push_back(
1320 std::make_pair(symbol_tensor[indices], value_tensor[indices]));
1321 }
1322 return symbol_values;
1323 }
1324
1325
1326 template <int dim, typename ExpressionType, typename ValueType>
1327 std::vector<std::pair<ExpressionType, ValueType>>
1328 make_tensor_entries_for_substitution_map(
1329 const Tensor<0, dim, ExpressionType> &symbol_tensor,
1330 const Tensor<0, dim, ValueType> &value_tensor)
1331 {
1332 const ExpressionType &expression = symbol_tensor;
1333 const ValueType &value = value_tensor;
1334 return {std::make_pair(expression, value)};
1335 }
1336
1337
1338 template <int dim, typename ExpressionType, typename ValueType>
1339 std::vector<std::pair<ExpressionType, ValueType>>
1340 make_tensor_entries_for_substitution_map(
1341 const SymmetricTensor<4, dim, ExpressionType> &symbol_tensor,
1342 const SymmetricTensor<4, dim, ValueType> &value_tensor)
1343 {
1344 std::vector<std::pair<ExpressionType, ValueType>> symbol_values;
1345 for (unsigned int i = 0;
1346 i < SymmetricTensor<2, dim>::n_independent_components;
1347 ++i)
1348 for (unsigned int j = 0;
1349 j < SymmetricTensor<2, dim>::n_independent_components;
1350 ++j)
1351 {
1352 const TableIndices<4> indices =
1353 make_rank_4_tensor_indices<dim>(i, j);
1354 symbol_values.push_back(
1355 std::make_pair(symbol_tensor[indices], value_tensor[indices]));
1356 }
1357 return symbol_values;
1358 }
1359 } // namespace internal
1360
1361
1362 template <bool ignore_invalid_symbols,
1363 int rank,
1364 int dim,
1365 typename ExpressionType,
1366 typename ValueType>
1367 void
1369 types::substitution_map &substitution_map,
1370 const Tensor<rank, dim, ExpressionType> &symbol_tensor,
1371 const Tensor<rank, dim, ValueType> &value_tensor)
1372 {
1373 add_to_substitution_map<ignore_invalid_symbols>(
1374 substitution_map,
1375 internal::make_tensor_entries_for_substitution_map(symbol_tensor,
1376 value_tensor));
1377 }
1378
1379
1380 template <bool ignore_invalid_symbols,
1381 int rank,
1382 int dim,
1383 typename ExpressionType,
1384 typename ValueType>
1385 void
1387 types::substitution_map &substitution_map,
1388 const SymmetricTensor<rank, dim, ExpressionType> &symbol_tensor,
1389 const SymmetricTensor<rank, dim, ValueType> &value_tensor)
1390 {
1391 add_to_substitution_map<ignore_invalid_symbols>(
1392 substitution_map,
1393 internal::make_tensor_entries_for_substitution_map(symbol_tensor,
1394 value_tensor));
1395 }
1396
1397
1398 /* ---------------- Symbol substitution and evaluation --------------*/
1399
1400
1401 namespace internal
1402 {
1403 template <int rank,
1404 int dim,
1405 template <int, int, typename> class TensorType>
1406 TensorType<rank, dim, Expression>
1407 substitute_tensor(
1408 const TensorType<rank, dim, Expression> &expression_tensor,
1409 const types::substitution_map &substitution_map)
1410 {
1411 TensorType<rank, dim, Expression> out;
1412 for (unsigned int i = 0; i < out.n_independent_components; ++i)
1413 {
1414 const TableIndices<rank> indices(
1415 out.unrolled_to_component_indices(i));
1416 out[indices] =
1417 substitute(expression_tensor[indices], substitution_map);
1418 }
1419 return out;
1420 }
1421
1422
1423 template <int dim>
1425 substitute_tensor(const Tensor<0, dim, Expression> &expression_tensor,
1426 const types::substitution_map &substitution_map)
1427 {
1428 const Expression &expression = expression_tensor;
1429 return substitute(expression, substitution_map);
1430 }
1431
1432
1433 template <int dim>
1435 substitute_tensor(
1436 const SymmetricTensor<4, dim, Expression> &expression_tensor,
1437 const types::substitution_map &substitution_map)
1438 {
1440 for (unsigned int i = 0;
1441 i < SymmetricTensor<2, dim>::n_independent_components;
1442 ++i)
1443 for (unsigned int j = 0;
1444 j < SymmetricTensor<2, dim>::n_independent_components;
1445 ++j)
1446 {
1447 const TableIndices<4> indices =
1448 make_rank_4_tensor_indices<dim>(i, j);
1449 out[indices] =
1450 substitute(expression_tensor[indices], substitution_map);
1451 }
1452 return out;
1453 }
1454
1455
1456 template <typename ValueType,
1457 int rank,
1458 int dim,
1459 template <int, int, typename> class TensorType>
1460 TensorType<rank, dim, ValueType>
1461 substitute_and_evaluate_tensor(
1462 const TensorType<rank, dim, Expression> &expression_tensor,
1463 const types::substitution_map &substitution_map)
1464 {
1465 TensorType<rank, dim, ValueType> out;
1466 for (unsigned int i = 0; i < out.n_independent_components; ++i)
1467 {
1468 const TableIndices<rank> indices(
1469 out.unrolled_to_component_indices(i));
1470 out[indices] =
1471 substitute_and_evaluate<ValueType>(expression_tensor[indices],
1472 substitution_map);
1473 }
1474 return out;
1475 }
1476
1477
1478 template <typename ValueType, int dim>
1480 substitute_and_evaluate_tensor(
1481 const Tensor<0, dim, Expression> &expression_tensor,
1482 const types::substitution_map &substitution_map)
1483 {
1484 const Expression &expression = expression_tensor;
1485 return substitute_and_evaluate<ValueType>(expression, substitution_map);
1486 }
1487
1488
1489 template <typename ValueType, int dim>
1491 substitute_and_evaluate_tensor(
1492 const SymmetricTensor<4, dim, Expression> &expression_tensor,
1493 const types::substitution_map &substitution_map)
1494 {
1496 for (unsigned int i = 0;
1497 i < SymmetricTensor<2, dim>::n_independent_components;
1498 ++i)
1499 for (unsigned int j = 0;
1500 j < SymmetricTensor<2, dim>::n_independent_components;
1501 ++j)
1502 {
1503 const TableIndices<4> indices =
1504 make_rank_4_tensor_indices<dim>(i, j);
1505 out[indices] =
1506 substitute_and_evaluate<ValueType>(expression_tensor[indices],
1507 substitution_map);
1508 }
1509 return out;
1510 }
1511 } // namespace internal
1512
1513
1514 template <int rank, int dim>
1516 substitute(const Tensor<rank, dim, Expression> &expression_tensor,
1517 const types::substitution_map &substitution_map)
1518 {
1519 return internal::substitute_tensor(expression_tensor, substitution_map);
1520 }
1521
1522
1523 template <int rank, int dim>
1525 substitute(const SymmetricTensor<rank, dim, Expression> &expression_tensor,
1526 const types::substitution_map &substitution_map)
1527 {
1528 return internal::substitute_tensor(expression_tensor, substitution_map);
1529 }
1530
1531
1532 template <typename ValueType, int rank, int dim>
1535 const Tensor<rank, dim, Expression> &expression_tensor,
1536 const types::substitution_map &substitution_map)
1537 {
1538 return internal::substitute_and_evaluate_tensor<ValueType>(
1539 expression_tensor, substitution_map);
1540 }
1541
1542
1543 template <typename ValueType, int rank, int dim>
1546 const SymmetricTensor<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
1555 } // namespace SD
1556} // namespace Differentiation
1557
1558# endif // DOXYGEN
1559
1560#endif // DEAL_II_WITH_SYMENGINE
1561
1563
1564#endif
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define Assert(cond, exc)
static ::ExceptionBase & ExcMessage(std::string arg1)
void set_value_in_symbol_map(types::substitution_map &substitution_map, const SymEngine::RCP< const SymEngine::Basic > &symbol, const SymEngine::RCP< const SymEngine::Basic > &value)
std::map< SD::Expression, SD::Expression, internal::ExpressionKeyLess > substitution_map
Tensor< rank, dim, Expression > make_tensor_of_symbols(const std::string &symbol)
Expression differentiate(const Expression &f, const Expression &x)
ValueType substitute_and_evaluate(const Expression &expression, const types::substitution_map &substitution_map)
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)
void set_value_in_symbol_map(types::substitution_map &substitution_map, const Expression &symbol, const Expression &value)
SymmetricTensor< rank, dim, Expression > make_symmetric_tensor_of_symbolic_functions(const std::string &symbol, const types::substitution_map &arguments)
void add_to_symbol_map(types::substitution_map &symbol_map, const Expression &symbol)
Tensor< 1, dim, Expression > make_vector_of_symbols(const std::string &symbol)
void add_to_substitution_map(types::substitution_map &substitution_map, const Expression &symbol, const Expression &value)
Expression substitute(const Expression &expression, const types::substitution_map &substitution_map)
Tensor< rank, dim, Expression > make_tensor_of_symbolic_functions(const std::string &symbol, const types::substitution_map &arguments)
types::substitution_map make_substitution_map(const Expression &symbol, const Expression &value)