13#ifndef dealii_differentiation_sd_symengine_tensor_operations_h
14#define dealii_differentiation_sd_symengine_tensor_operations_h
18#ifdef DEAL_II_WITH_SYMENGINE
34#ifdef DEAL_II_WITH_SYMENGINE
85 template <
int rank,
int dim>
108 template <
int rank,
int dim>
156 template <
int rank,
int dim>
181 template <
int rank,
int dim>
184 const std::string &symbol,
203 template <
int rank,
int dim>
216 template <
int rank,
int dim>
230 template <
int rank,
int dim>
244 template <
int rank,
int dim>
258 template <
int rank,
int dim>
272 template <
int rank,
int dim>
287 template <
int rank,
int dim>
303 template <
int rank,
int dim>
318 template <
int rank_1,
int rank_2,
int dim>
335 template <
int rank_1,
int rank_2,
int dim>
350 template <
int rank_1,
int rank_2,
int dim>
365 template <
int rank_1,
int rank_2,
int dim>
401 template <
bool ignore_invalid_symbols =
false,
402 typename ValueType = double,
405 typename SymbolicType>
434 template <
bool ignore_invalid_symbols =
false,
435 typename ValueType = double,
438 typename SymbolicType>
464 template <
int rank,
int dim,
typename SymbolicType,
typename ValueType>
491 template <
int rank,
int dim,
typename SymbolicType,
typename ValueType>
526 template <
int rank,
int dim,
typename ExpressionType,
typename ValueType>
553 template <
int rank,
int dim,
typename ExpressionType,
typename ValueType>
592 template <
bool ignore_invalid_symbols =
false,
595 typename ExpressionType,
628 template <
bool ignore_invalid_symbols =
false,
631 typename ExpressionType,
665 template <
int rank,
int dim>
689 template <
int rank,
int dim>
720 template <
typename ValueType,
int rank,
int dim>
752 template <
typename ValueType,
int rank,
int dim>
780 make_rank_4_tensor_indices(
const unsigned int idx_i,
781 const unsigned int idx_j)
794 template <
int rank_1,
int rank_2>
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];
824 template <
int rank,
int dim,
typename ValueType>
833 template <
int rank,
int dim,
typename ValueType>
839 rank == 0 || rank == 2,
840 "Querying symmetric component for non rank-2 symmetric tensor index is not allowed.");
845 template <
int dim,
typename ValueType>
850 return table_indices[0] != table_indices[1];
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)
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)
873 TensorType<rank, dim, ValueType> out;
874 for (
unsigned int i = 0; i < out.n_independent_components; ++i)
877 out.unrolled_to_component_indices(i));
880 if (is_symmetric_component(indices, op))
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)
896 TensorType<rank, dim, ValueType> out;
897 for (
unsigned int i = 0; i < out.n_independent_components; ++i)
900 out.unrolled_to_component_indices(i));
903 if (is_symmetric_component(indices, op))
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,
918 TensorType<rank, dim, ValueType> out;
919 for (
unsigned int i = 0; i < out.n_independent_components; ++i)
922 out.unrolled_to_component_indices(i));
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)
938 TensorType<rank, dim, ValueType> out;
939 for (
unsigned int i = 0; i < out.n_independent_components; ++i)
942 out.unrolled_to_component_indices(i));
950 template <
int rank_1,
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)
959 TensorType<rank_1 + rank_2, dim, ValueType> out;
960 for (
unsigned int i = 0; i < funcs.n_independent_components; ++i)
963 funcs.unrolled_to_component_indices(i));
964 for (
unsigned int j = 0; j < op.n_independent_components; ++j)
967 op.unrolled_to_component_indices(j));
969 concatenate_indices(indices_i, indices_j);
974 if (is_symmetric_component(indices_j, op))
975 out[indices_out] *= 0.5;
986 template <
int rank_1,
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)
997 for (
unsigned int i = 0; i < funcs.n_independent_components; ++i)
1000 funcs.unrolled_to_component_indices(i));
1001 for (
unsigned int j = 0; j < op.n_independent_components; ++j)
1004 op.unrolled_to_component_indices(j));
1006 concatenate_indices(indices_i, indices_j);
1011 if (is_symmetric_component(indices_j, op))
1012 out[indices_out] *= 0.5;
1016 TensorType_1<rank_1, dim, ValueType>,
1020 concatenate_indices(transpose_indices(indices_i),
1022 out[indices_out_t] = out[indices_out];
1024 else if (std::is_same_v<
1025 TensorType_2<rank_2, dim, ValueType>,
1030 concatenate_indices(indices_i,
1031 transpose_indices(indices_j));
1032 out[indices_out_t] = out[indices_out];
1039 "Expect mixed tensor differentiation to have at least "
1040 "one component stemming from a symmetric tensor."));
1050 template <
int rank,
int dim>
1055 return internal::scalar_diff_tensor(func, op);
1059 template <
int rank,
int dim>
1064 return internal::scalar_diff_tensor(func, op);
1068 template <
int rank,
int dim>
1073 return internal::scalar_diff_tensor(func, op);
1077 template <
int rank,
int dim>
1084 const Expression tmp = func;
1085 return internal::scalar_diff_tensor(tmp, op);
1089 template <
int rank,
int dim>
1092 const Expression &op)
1094 return internal::tensor_diff_scalar(symbol_tensor, op);
1098 template <
int rank,
int dim>
1103 return internal::tensor_diff_scalar(symbol_tensor, op);
1107 template <
int rank,
int dim>
1110 const Expression &op)
1112 return internal::tensor_diff_scalar(symbol_tensor, op);
1116 template <
int rank,
int dim>
1121 return internal::tensor_diff_scalar(symbol_tensor, op);
1125 template <
int rank_1,
int rank_2,
int dim>
1130 return internal::tensor_diff_tensor(symbol_tensor, op);
1134 template <
int rank_1,
int rank_2,
int dim>
1139 return internal::tensor_diff_tensor(symbol_tensor, op);
1143 template <
int rank_1,
int rank_2,
int dim>
1148 return internal::tensor_diff_tensor(symbol_tensor, op);
1152 template <
int rank_1,
int rank_2,
int dim>
1157 return internal::tensor_diff_tensor(symbol_tensor, op);
1166 template <
typename SymbolicType,
1170 template <
int,
int,
typename>
class TensorType>
1172 set_tensor_value_in_symbol_map(
1174 const TensorType<rank, dim, SymbolicType> &symbol_tensor,
1175 const TensorType<rank, dim, ValueType> &value_tensor)
1177 TensorType<rank, dim, Expression> out;
1178 for (
unsigned int i = 0; i < out.n_independent_components; ++i)
1181 out.unrolled_to_component_indices(i));
1183 symbol_tensor[indices],
1184 value_tensor[indices]);
1189 template <
typename SymbolicType,
typename ValueType,
int dim>
1191 set_tensor_value_in_symbol_map(
1197 for (
unsigned int i = 0;
1198 i < SymmetricTensor<2, dim>::n_independent_components;
1200 for (
unsigned int j = 0;
1201 j < SymmetricTensor<2, dim>::n_independent_components;
1205 make_rank_4_tensor_indices<dim>(i, j);
1207 symbol_tensor[indices],
1208 value_tensor[indices]);
1214 template <
bool ignore_invalid_symbols,
1218 typename SymbolicType>
1224 add_to_substitution_map<ignore_invalid_symbols>(
1229 template <
bool ignore_invalid_symbols,
1233 typename SymbolicType>
1240 add_to_substitution_map<ignore_invalid_symbols>(
1245 template <
int rank,
int dim,
typename SymbolicType,
typename ValueType>
1252 internal::set_tensor_value_in_symbol_map(substitution_map,
1258 template <
int rank,
int dim,
typename SymbolicType,
typename ValueType>
1265 internal::set_tensor_value_in_symbol_map(substitution_map,
1274 template <
int rank,
int dim,
typename ExpressionType,
typename ValueType>
1286 template <
int rank,
int dim,
typename ExpressionType,
typename ValueType>
1305 typename ExpressionType,
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)
1313 std::vector<std::pair<ExpressionType, ValueType>> symbol_values;
1314 for (
unsigned int i = 0; i < symbol_tensor.n_independent_components;
1318 symbol_tensor.unrolled_to_component_indices(i));
1319 symbol_values.push_back(
1320 std::make_pair(symbol_tensor[indices], value_tensor[indices]));
1322 return symbol_values;
1326 template <
int dim,
typename ExpressionType,
typename ValueType>
1327 std::vector<std::pair<ExpressionType, ValueType>>
1328 make_tensor_entries_for_substitution_map(
1332 const ExpressionType &expression = symbol_tensor;
1333 const ValueType &
value = value_tensor;
1334 return {std::make_pair(expression, value)};
1338 template <
int dim,
typename ExpressionType,
typename ValueType>
1339 std::vector<std::pair<ExpressionType, ValueType>>
1340 make_tensor_entries_for_substitution_map(
1344 std::vector<std::pair<ExpressionType, ValueType>> symbol_values;
1345 for (
unsigned int i = 0;
1346 i < SymmetricTensor<2, dim>::n_independent_components;
1348 for (
unsigned int j = 0;
1349 j < SymmetricTensor<2, dim>::n_independent_components;
1353 make_rank_4_tensor_indices<dim>(i, j);
1354 symbol_values.push_back(
1355 std::make_pair(symbol_tensor[indices], value_tensor[indices]));
1357 return symbol_values;
1362 template <
bool ignore_invalid_symbols,
1365 typename ExpressionType,
1373 add_to_substitution_map<ignore_invalid_symbols>(
1375 internal::make_tensor_entries_for_substitution_map(symbol_tensor,
1380 template <
bool ignore_invalid_symbols,
1383 typename ExpressionType,
1391 add_to_substitution_map<ignore_invalid_symbols>(
1393 internal::make_tensor_entries_for_substitution_map(symbol_tensor,
1405 template <
int,
int,
typename>
class TensorType>
1406 TensorType<rank, dim, Expression>
1408 const TensorType<rank, dim, Expression> &expression_tensor,
1411 TensorType<rank, dim, Expression> out;
1412 for (
unsigned int i = 0; i < out.n_independent_components; ++i)
1415 out.unrolled_to_component_indices(i));
1417 substitute(expression_tensor[indices], substitution_map);
1428 const Expression &expression = expression_tensor;
1429 return substitute(expression, substitution_map);
1440 for (
unsigned int i = 0;
1441 i < SymmetricTensor<2, dim>::n_independent_components;
1443 for (
unsigned int j = 0;
1444 j < SymmetricTensor<2, dim>::n_independent_components;
1448 make_rank_4_tensor_indices<dim>(i, j);
1450 substitute(expression_tensor[indices], substitution_map);
1456 template <
typename ValueType,
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,
1465 TensorType<rank, dim, ValueType> out;
1466 for (
unsigned int i = 0; i < out.n_independent_components; ++i)
1469 out.unrolled_to_component_indices(i));
1471 substitute_and_evaluate<ValueType>(expression_tensor[indices],
1478 template <
typename ValueType,
int dim>
1480 substitute_and_evaluate_tensor(
1484 const Expression &expression = expression_tensor;
1485 return substitute_and_evaluate<ValueType>(expression, substitution_map);
1489 template <
typename ValueType,
int dim>
1491 substitute_and_evaluate_tensor(
1496 for (
unsigned int i = 0;
1497 i < SymmetricTensor<2, dim>::n_independent_components;
1499 for (
unsigned int j = 0;
1500 j < SymmetricTensor<2, dim>::n_independent_components;
1504 make_rank_4_tensor_indices<dim>(i, j);
1506 substitute_and_evaluate<ValueType>(expression_tensor[indices],
1514 template <
int rank,
int dim>
1519 return internal::substitute_tensor(expression_tensor, substitution_map);
1523 template <
int rank,
int dim>
1528 return internal::substitute_tensor(expression_tensor, substitution_map);
1532 template <
typename ValueType,
int rank,
int dim>
1538 return internal::substitute_and_evaluate_tensor<ValueType>(
1539 expression_tensor, substitution_map);
1543 template <
typename ValueType,
int rank,
int dim>
1549 return internal::substitute_and_evaluate_tensor<ValueType>(
1550 expression_tensor, substitution_map);
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
#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)