deal.II version GIT relicensing-7229-gca0444b871 2026-11-28 02:50: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
tensor.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) 1998 - 2025 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_tensor_h
14#define dealii_tensor_h
15
16#include <deal.II/base/config.h>
17
19#include <deal.II/base/kokkos.h>
24
25#include <Kokkos_Array.hpp>
26
27#ifdef DEAL_II_WITH_ADOLC
28# include <adolc/adouble.h> // Taped double
29#endif
30
31#include <cmath>
32#include <complex>
33#include <ostream>
34#include <type_traits>
35
37
38// Forward declarations:
39#ifndef DOXYGEN
40template <typename ElementType, typename MemorySpace>
41class ArrayView;
42
43template <int dim, typename Number>
45class Point;
46
47template <int rank_, int dim, typename Number = double>
48class Tensor;
49template <typename Number>
50class Vector;
51template <typename number>
52class FullMatrix;
53namespace Differentiation
54{
55 namespace SD
56 {
57 class Expression;
58 }
59} // namespace Differentiation
60#endif
61
62
92template <int dim, typename Number>
93class Tensor<0, dim, Number>
94{
95public:
96 static_assert(dim >= 0,
97 "Tensors must have a dimension greater than or equal to one.");
98
107 static constexpr unsigned int dimension = dim;
108
112 static constexpr unsigned int rank = 0;
113
117 static constexpr unsigned int n_independent_components = 1;
118
128
133 using value_type = Number;
134
140 using array_type = Number;
141
147 constexpr DEAL_II_HOST_DEVICE
149
157 template <typename OtherNumber>
158 constexpr DEAL_II_HOST_DEVICE
160
166 template <typename OtherNumber>
167 constexpr DEAL_II_HOST_DEVICE
168 Tensor(const OtherNumber &initializer);
169
170#ifdef DEAL_II_DELETED_MOVE_CONSTRUCTOR_BUG
174 constexpr DEAL_II_HOST_DEVICE
175 Tensor(const Tensor<0, dim, Number> &other);
176
180 constexpr DEAL_II_HOST_DEVICE
181 Tensor(Tensor<0, dim, Number> &&other) noexcept;
182#endif
183
193 constexpr DEAL_II_HOST_DEVICE
194 operator Number &();
195
204 constexpr DEAL_II_HOST_DEVICE
205 operator const Number &() const;
206
214 template <typename OtherNumber>
215 constexpr DEAL_II_HOST_DEVICE Tensor &
217
218#if defined(__INTEL_COMPILER) || defined(DEAL_II_DELETED_MOVE_CONSTRUCTOR_BUG)
227 constexpr DEAL_II_HOST_DEVICE Tensor &
229#endif
230
231#ifdef DEAL_II_DELETED_MOVE_CONSTRUCTOR_BUG
236 operator=(Tensor<0, dim, Number> &&other) noexcept;
237#endif
238
245 template <typename OtherNumber>
246 constexpr DEAL_II_HOST_DEVICE Tensor &
247 operator=(const OtherNumber &d) &;
248
254 template <typename OtherNumber>
255 constexpr DEAL_II_HOST_DEVICE Tensor &
256 operator=(const OtherNumber &d) && = delete;
257
261 template <typename OtherNumber>
262 constexpr bool
264
268 template <typename OtherNumber>
269 constexpr bool
271
277 template <typename OtherNumber>
278 constexpr DEAL_II_HOST_DEVICE Tensor &
280
286 template <typename OtherNumber>
287 constexpr DEAL_II_HOST_DEVICE Tensor &
289
295 template <typename OtherNumber>
296 constexpr DEAL_II_HOST_DEVICE Tensor &
297 operator*=(const OtherNumber &factor);
298
304 template <typename OtherNumber>
305 constexpr DEAL_II_HOST_DEVICE Tensor &
306 operator/=(const OtherNumber &factor);
307
314 operator-() const;
315
328 constexpr void
330
337 norm() const;
338
346 norm_square() const;
347
355 template <class Iterator>
356 void
357 unroll(const Iterator begin, const Iterator end) const;
358
364 template <class Archive>
365 void
366 serialize(Archive &ar, const unsigned int version);
367
372 using tensor_type = Number;
373
374private:
378 Number value;
379
380 // Allow an arbitrary Tensor to access the underlying values.
381 template <int, int, typename>
382 friend class Tensor;
383};
384
385
386
460template <int rank_, int dim, typename Number>
462{
463public:
464 static_assert(rank_ >= 1,
465 "Tensors must have a rank greater than or equal to one.");
466 static_assert(dim >= 0,
467 "Tensors must have a dimension greater than or equal to zero.");
476 static constexpr unsigned int dimension = dim;
477
481 static constexpr unsigned int rank = rank_;
482
497 static constexpr unsigned int n_independent_components =
498 Tensor<rank_ - 1, dim>::n_independent_components * dim;
499
506 std::conditional_t<rank_ == 1, Number, Tensor<rank_ - 1, dim, Number>>;
507
519 using array_type = std::conditional_t<
520 rank_ == 1,
521 Number[(dim != 0) ? dim : 1],
522 typename Tensor<rank_ - 1, dim, Number>::array_type[(dim != 0) ? dim : 1]>;
523
531
537 constexpr DEAL_II_HOST_DEVICE explicit Tensor(const array_type &initializer);
538
551 template <typename ElementType, typename MemorySpace>
552 constexpr DEAL_II_HOST_DEVICE explicit Tensor(
553 const ArrayView<ElementType, MemorySpace> &initializer);
554
562 template <typename OtherNumber>
563 constexpr DEAL_II_HOST_DEVICE
565
569 template <typename OtherNumber>
570 constexpr Tensor(
571 const Tensor<1, dim, Tensor<rank_ - 1, dim, OtherNumber>> &initializer);
572
576 template <typename OtherNumber>
577 constexpr
578 operator Tensor<1, dim, Tensor<rank_ - 1, dim, OtherNumber>>() const;
579
580#ifdef DEAL_II_DELETED_MOVE_CONSTRUCTOR_BUG
584 constexpr Tensor(const Tensor<rank_, dim, Number> &);
585
589 constexpr Tensor(Tensor<rank_, dim, Number> &&) noexcept;
590#endif
591
598 operator[](const unsigned int i);
599
605 constexpr DEAL_II_HOST_DEVICE const value_type &
606 operator[](const unsigned int i) const;
607
611 constexpr const Number &
612 operator[](const TableIndices<rank_> &indices) const;
613
617 constexpr Number &
619
624 Number *
626
631 const Number *
632 begin_raw() const;
633
638 Number *
640
645 const Number *
646 end_raw() const;
647
655 template <typename OtherNumber>
656 constexpr DEAL_II_HOST_DEVICE Tensor &
658
665 constexpr DEAL_II_HOST_DEVICE Tensor &
666 operator=(const Number &d) &;
667
673 constexpr DEAL_II_HOST_DEVICE Tensor &
674 operator=(const Number &d) && = delete;
675
676#ifdef DEAL_II_DELETED_MOVE_CONSTRUCTOR_BUG
682
688#endif
689
693 template <typename OtherNumber>
694 constexpr bool
696
700 template <typename OtherNumber>
701 constexpr bool
703
709 template <typename OtherNumber>
710 constexpr DEAL_II_HOST_DEVICE Tensor &
712
718 template <typename OtherNumber>
719 constexpr DEAL_II_HOST_DEVICE Tensor &
721
728 template <typename OtherNumber>
729 constexpr DEAL_II_HOST_DEVICE Tensor &
730 operator*=(const OtherNumber &factor);
731
737 template <typename OtherNumber>
738 constexpr DEAL_II_HOST_DEVICE Tensor &
739 operator/=(const OtherNumber &factor);
740
747 operator-() const;
748
761 constexpr void
763
773 norm() const;
774
781 constexpr DEAL_II_HOST_DEVICE
783 norm_square() const;
784
795 template <class Iterator>
796 void
797 unroll(const Iterator begin, const Iterator end) const;
798
803 static constexpr DEAL_II_HOST_DEVICE unsigned int
805
812 unrolled_to_component_indices(const unsigned int i);
813
818 static constexpr std::size_t
820
826 template <class Archive>
827 void
828 serialize(Archive &ar, const unsigned int version);
829
835
836private:
842#if DEAL_II_KOKKOS_VERSION_GTE(3, 7, 0)
843 std::conditional_t<rank_ == 1,
844 Kokkos::Array<Number, dim>,
845 Kokkos::Array<Tensor<rank_ - 1, dim, Number>, dim>>
846#else
847 std::conditional_t<rank_ == 1,
848 std::array<Number, dim>,
849 std::array<Tensor<rank_ - 1, dim, Number>, dim>>
850#endif
852
859 template <typename ArrayLike, std::size_t... Indices>
860 constexpr DEAL_II_HOST_DEVICE
861 Tensor(const ArrayLike &initializer, std::index_sequence<Indices...>);
862
863 // Allow an arbitrary Tensor to access the underlying values.
864 template <int, int, typename>
865 friend class Tensor;
866
867 // Point is allowed access to the coordinates. This is supposed to improve
868 // speed.
869 friend class Point<dim, Number>;
870};
871
872
873#ifndef DOXYGEN
874namespace internal
875{
876 // Workaround: The following 4 overloads are necessary to be able to
877 // compile the library with Apple Clang 8 and older. We should remove
878 // these overloads again when we bump the minimal required version to
879 // something later than clang-3.6 / Apple Clang 6.3.
880 template <int rank, int dim, typename T, typename U>
881 struct ProductTypeImpl<Tensor<rank, dim, T>, std::complex<U>>
882 {
883 using type =
885 };
886
887 template <int rank, int dim, typename T, typename U>
888 struct ProductTypeImpl<Tensor<rank, dim, std::complex<T>>, std::complex<U>>
889 {
890 using type =
892 };
893
894 template <typename T, int rank, int dim, typename U>
895 struct ProductTypeImpl<std::complex<T>, Tensor<rank, dim, U>>
896 {
897 using type =
899 };
900
901 template <int rank, int dim, typename T, typename U>
902 struct ProductTypeImpl<std::complex<T>, Tensor<rank, dim, std::complex<U>>>
903 {
904 using type =
906 };
907 // end workaround
908
913 template <int rank, int dim, typename T>
914 struct NumberType<Tensor<rank, dim, T>>
915 {
916 static constexpr DEAL_II_HOST_DEVICE_ALWAYS_INLINE const
919 {
920 return t;
921 }
922
924 value(const T &t)
925 {
927 tmp = t;
928 return tmp;
929 }
930 };
931} // namespace internal
932
933
934/*---------------------- Inline functions: Tensor<0,dim> ---------------------*/
935
936
937template <int dim, typename Number>
940 // Some auto-differentiable numbers need explicit
941 // zero initialization such as adtl::adouble.
942 : Tensor{0.0}
943{}
944
945
946
947template <int dim, typename Number>
948template <typename OtherNumber>
950Tensor<0, dim, Number>::Tensor(const OtherNumber &initializer)
951 : value(internal::NumberType<Number>::value(initializer))
952{}
953
954
955
956template <int dim, typename Number>
957template <typename OtherNumber>
960 : Tensor{p.value}
961{}
962
963
964# ifdef DEAL_II_DELETED_MOVE_CONSTRUCTOR_BUG
965template <int dim, typename Number>
968 : value{other.value}
969{}
970
971
972
973template <int dim, typename Number>
976 : value{std::move(other.value)}
977{}
978# endif
979
980
981
982template <int dim, typename Number>
984operator Number &()
985{
986 Assert(dim != 0,
987 ExcMessage("Cannot access an object of type Tensor<0,0,Number>"));
988 return value;
989}
990
991
992template <int dim, typename Number>
993constexpr inline DEAL_II_ALWAYS_INLINE
995 operator const Number &() const
996{
997 Assert(dim != 0,
998 ExcMessage("Cannot access an object of type Tensor<0,0,Number>"));
999 return value;
1000}
1001
1002
1003
1004template <int dim, typename Number>
1005template <typename OtherNumber>
1008{
1010 return *this;
1011}
1012
1013
1014# if defined(__INTEL_COMPILER) || defined(DEAL_II_DELETED_MOVE_CONSTRUCTOR_BUG)
1015template <int dim, typename Number>
1018{
1019 value = p.value;
1020 return *this;
1021}
1022# endif
1023
1024# ifdef DEAL_II_DELETED_MOVE_CONSTRUCTOR_BUG
1025template <int dim, typename Number>
1028{
1029 value = std::move(other.value);
1030 return *this;
1031}
1032# endif
1033
1034
1035
1036template <int dim, typename Number>
1037template <typename OtherNumber>
1039Tensor<0, dim, Number>::operator=(const OtherNumber &d) &
1040{
1042 return *this;
1043}
1044
1045
1046template <int dim, typename Number>
1047template <typename OtherNumber>
1048constexpr inline bool
1050{
1051# ifdef DEAL_II_ADOLC_WITH_ADVANCED_BRANCHING
1052 Assert(!(std::is_same_v<Number, adouble> ||
1053 std::is_same_v<OtherNumber, adouble>),
1054 ExcMessage(
1055 "The Tensor equality operator for ADOL-C taped numbers has not yet "
1056 "been extended to support advanced branching."));
1057# endif
1058
1059 return numbers::values_are_equal(value, p.value);
1060}
1061
1062
1063template <int dim, typename Number>
1064template <typename OtherNumber>
1065constexpr bool
1067{
1068 return !((*this) == p);
1069}
1070
1071
1072template <int dim, typename Number>
1073template <typename OtherNumber>
1076{
1077 value += p.value;
1078 return *this;
1079}
1080
1081
1082template <int dim, typename Number>
1083template <typename OtherNumber>
1086{
1087 value -= p.value;
1088 return *this;
1089}
1090
1091
1092
1093namespace internal
1094{
1095 namespace ComplexWorkaround
1096 {
1097 template <typename Number, typename OtherNumber>
1099 multiply_assign_scalar(Number &val, const OtherNumber &s)
1100 {
1101 val *= s;
1102 }
1103
1104 template <typename Number, typename OtherNumber>
1106 multiply_assign_scalar(std::complex<Number> &val, const OtherNumber &s)
1107 {
1108# if DEAL_II_KOKKOS_VERSION_GTE(3, 6, 0)
1109 KOKKOS_IF_ON_HOST((val *= s;))
1110 KOKKOS_IF_ON_DEVICE(({
1111 (void)val;
1112 (void)s;
1113 Kokkos::abort(
1114 "This function is not implemented for std::complex<Number>!\n");
1115 }))
1116# else
1117 // We do not support device code for Kokkos < 3.7:
1118 val *= s;
1119# endif
1120 }
1121 } // namespace ComplexWorkaround
1122} // namespace internal
1123
1124
1125template <int dim, typename Number>
1126template <typename OtherNumber>
1128Tensor<0, dim, Number>::operator*=(const OtherNumber &s)
1129{
1130 internal::ComplexWorkaround::multiply_assign_scalar(value, s);
1131 return *this;
1132}
1133
1134
1135
1136template <int dim, typename Number>
1137template <typename OtherNumber>
1139Tensor<0, dim, Number>::operator/=(const OtherNumber &s)
1140{
1141 value /= s;
1142 return *this;
1143}
1144
1145
1146template <int dim, typename Number>
1149{
1150 return -value;
1151}
1152
1153
1154template <int dim, typename Number>
1157{
1158 Assert(dim != 0,
1159 ExcMessage("Cannot access an object of type Tensor<0,0,Number>"));
1161}
1162
1163
1164template <int dim, typename Number>
1168{
1169 Assert(dim != 0,
1170 ExcMessage("Cannot access an object of type Tensor<0,0,Number>"));
1172}
1173
1174
1175
1176template <int dim, typename Number>
1177constexpr inline void
1179{
1180 // Some auto-differentiable numbers need explicit
1181 // zero initialization.
1183}
1184
1185
1186
1187template <int dim, typename Number>
1188template <class Iterator>
1189inline void
1190Tensor<0, dim, Number>::unroll(const Iterator begin, const Iterator end) const
1191{
1192 AssertDimension(std::distance(begin, end), n_independent_components);
1193 Assert(dim != 0,
1194 ExcMessage("Cannot unroll an object of type Tensor<0,0,Number>"));
1195 Assert(std::distance(begin, end) >= 1,
1196 ExcMessage("The provided iterator range must contain at least one "
1197 "element."));
1198 *begin = value;
1199}
1200
1201
1202
1203template <int dim, typename Number>
1204template <class Archive>
1205inline void
1206Tensor<0, dim, Number>::serialize(Archive &ar, const unsigned int)
1207{
1208 ar & value;
1209}
1210
1211
1212template <int dim, typename Number>
1214
1215
1216/*-------------------- Inline functions: Tensor<rank,dim> --------------------*/
1217
1218template <int rank_, int dim, typename Number>
1219template <typename ArrayLike, std::size_t... indices>
1221Tensor<rank_, dim, Number>::Tensor(const ArrayLike &initializer,
1222 std::index_sequence<indices...>)
1223 // Extract from the 'initializer' a sequence of elements via template
1224 // pack evaluation. This could be as easy as
1225 // values{{ (initializer[indices])... }}
1226 // but of course in practice it is not. The challenge is that if rank>1,
1227 // we want to pass the elements initializer[indices] down to the next
1228 // lower rank tensor for evaluation unchanged. But at the rank==1 level,
1229 // we need to convert to the scalar type 'Number'. This would all be
1230 // relatively straightforward if we could rely on automatic type
1231 // conversion, but for some autodifferentiation types, the conversion
1232 // from the AD to double (i.e., the extraction of a scalar value) is
1233 // not implicit, and we need to call internal::NumberType<Number>::value() --
1234 // but as mentioned, we can only do that for rank==1.
1235 //
1236 // We can achieve all of this by dispatching to a lambda function within
1237 // which we can use a 'if constexpr'.
1238 : values{{([&initializer]() -> value_type {
1239 if constexpr (rank_ == 1)
1240 return internal::NumberType<Number>::value(initializer[indices]);
1241 else
1242 return value_type(initializer[indices]);
1243 }())...}}
1244{
1245 static_assert(sizeof...(indices) == dim,
1246 "dim should match the number of indices");
1247}
1248
1249
1250# if defined(DEAL_II_HAVE_CXX20) && !defined(__NVCC__)
1251
1252template <int rank_, int dim, typename Number>
1255 : values(
1256 // In order to initialize the Kokkos::Array<Number,dim>, we would need a
1257 // brace-enclosed list of length 'dim'. There is no way in C++ to create
1258 // such a list in-place, but we can come up with a lambda function that
1259 // expands such a list via template-pack expansion, and then uses this
1260 // list to initialize a Kokkos::Array which it then returns.
1261 //
1262 // The trick to come up with such a lambda function is to have a function
1263 // that takes an argument that depends on a template-pack of integers.
1264 // We will call the function with an integer list of length 'dim', and
1265 // in the function itself expand that pack in a way that it serves as
1266 // a brace-enclosed list of initializers for a Kokkos::Array.
1267 //
1268 // Of course, we do not want to initialize the array with the integers,
1269 // but with zeros. (Or, more correctly, a zero of the element type.)
1270 // The canonical way to do this would be using the comma operator:
1271 // (sequence_element, 0.0)
1272 // returns zero, and
1273 // (sequence, 0.0)...
1274 // returns a list of zeros of the right length. Unfortunately, some
1275 // compilers then warn that the left side of the comma expression has
1276 // no effect -- well, bummer, that was of course exactly the idea.
1277 // We could work around this by using
1278 // (sequence_element * 0.0)
1279 // instead, assuming that the compiler will optimize (known) integer
1280 // times zero to zero, and similarly for (known) integer times times
1281 // default-initialized tensor.
1282 //
1283 // But, instead of relying on compiler optimizations, a better way is
1284 // to simply have another (nested) lambda function that takes the
1285 // integer sequence element as an argument and ignores it, just
1286 // returning a zero instead.
1287 []<std::size_t... I>(
1288 const std::index_sequence<I...> &) constexpr -> decltype(values) {
1289 if constexpr (dim == 0)
1290 {
1291 return {};
1292 }
1293 else if constexpr (rank_ == 1)
1294 {
1295 auto get_zero_and_ignore_argument = [](int) {
1297 };
1298 return {{(get_zero_and_ignore_argument(I))...}};
1299 }
1300 else
1301 {
1302 auto get_zero_and_ignore_argument = [](int) {
1303 return Tensor<rank_ - 1, dim, Number>();
1304 };
1305 return {{(get_zero_and_ignore_argument(I))...}};
1306 }
1307 }(std::make_index_sequence<dim>()))
1308{}
1309
1310# else
1311
1312// The C++17 case works in essence the same, except that we can't use a
1313// lambda function with explicit template parameters, i.e., we can't do
1314// []<std::size_t... I>(const std::index_sequence<I...> &)
1315// as above because that's a C++20 feature. Lambda functions in C++17 can
1316// have template packs as arguments, but we need the ability to *name*
1317// that template pack (the 'I' above) and that's not possible in C++17.
1318//
1319// We work around this by moving the lambda function to a global function
1320// and using the traditional template syntax on it.
1321namespace internal
1322{
1323 namespace TensorInitialization
1324 {
1325 template <int rank, int dim, typename Number, std::size_t... I>
1326# if DEAL_II_KOKKOS_VERSION_GTE(3, 7, 0)
1327 constexpr Kokkos::Array<typename Tensor<rank, dim, Number>::value_type, dim>
1328# else
1329 constexpr std::array<typename Tensor<rank, dim, Number>::value_type, dim>
1330# endif
1331 make_zero_array(const std::index_sequence<I...> &)
1332 {
1333 static_assert(sizeof...(I) == dim, "This is bad.");
1334
1335 // First peel off the case dim==0. If we don't, some compilers
1336 // will warn below that we define these lambda functions but
1337 // never use them (because the expanded list has zero elements,
1338 // and the get_zero_and_ignore_argument() function is not used...)
1339 if constexpr (dim == 0)
1340 {
1341 return {};
1342 }
1343 else if constexpr (rank == 1)
1344 {
1345 auto get_zero_and_ignore_argument = [](int) {
1347 };
1348 return {{(get_zero_and_ignore_argument(I))...}};
1349 }
1350 else
1351 {
1352 auto get_zero_and_ignore_argument = [](int) {
1353 return Tensor<rank - 1, dim, Number>();
1354 };
1355 return {{(get_zero_and_ignore_argument(I))...}};
1356 }
1357 }
1358 } // namespace TensorInitialization
1359} // namespace internal
1360
1361
1362template <int rank_, int dim, typename Number>
1365 : values(internal::TensorInitialization::make_zero_array<rank_, dim, Number>(
1366 std::make_index_sequence<dim>()))
1367{}
1368
1369
1370# endif
1371
1372
1373template <int rank_, int dim, typename Number>
1375Tensor<rank_, dim, Number>::Tensor(const array_type &initializer)
1376 : Tensor(initializer, std::make_index_sequence<dim>{})
1377{}
1378
1379
1380
1381template <int rank_, int dim, typename Number>
1382template <typename ElementType, typename MemorySpace>
1385 const ArrayView<ElementType, MemorySpace> &initializer)
1386{
1387 // make nvcc happy
1388 const int my_n_independent_components = n_independent_components;
1389 AssertDimension(initializer.size(), my_n_independent_components);
1390
1391 for (unsigned int i = 0; i < my_n_independent_components; ++i)
1392 (*this)[unrolled_to_component_indices(i)] = initializer[i];
1393}
1394
1395
1396
1397template <int rank_, int dim, typename Number>
1398template <typename OtherNumber>
1401 const Tensor<rank_, dim, OtherNumber> &initializer)
1402 : Tensor(initializer, std::make_index_sequence<dim>{})
1403{}
1404
1405
1406
1407template <int rank_, int dim, typename Number>
1408template <typename OtherNumber>
1409constexpr DEAL_II_ALWAYS_INLINE
1411 const Tensor<1, dim, Tensor<rank_ - 1, dim, OtherNumber>> &initializer)
1412 : Tensor(initializer, std::make_index_sequence<dim>{})
1413{}
1414
1415
1416
1417template <int rank_, int dim, typename Number>
1418template <typename OtherNumber>
1420operator Tensor<1, dim, Tensor<rank_ - 1, dim, OtherNumber>>() const
1421{
1422 Tensor<1, dim, Tensor<rank_ - 1, dim, OtherNumber>> x;
1423 std::copy(values.data(), values.data() + values.size(), x.values.data());
1424 return x;
1425}
1426
1427
1428# ifdef DEAL_II_DELETED_MOVE_CONSTRUCTOR_BUG
1429template <int rank_, int dim, typename Number>
1430constexpr DEAL_II_ALWAYS_INLINE
1432 : values(other.values)
1433{}
1434
1435
1436
1437template <int rank_, int dim, typename Number>
1438constexpr DEAL_II_ALWAYS_INLINE
1440 : values(std::move(other.values))
1441{}
1442# endif
1443
1444
1445
1446template <int rank_, int dim, typename Number>
1449 Tensor<rank_, dim, Number>::operator[](const unsigned int i)
1450{
1451 Assert(dim != 0,
1452 ExcMessage("Cannot access an object of type Tensor<rank_,0,Number>"));
1453 AssertIndexRange(i, dim);
1454 DEAL_II_CXX23_ASSUME(i < dim);
1455
1456 return values[i];
1457}
1458
1459
1460template <int rank_, int dim, typename Number>
1461constexpr DEAL_II_ALWAYS_INLINE
1463 Tensor<rank_, dim, Number>::operator[](const unsigned int i) const
1464{
1465 Assert(dim != 0,
1466 ExcMessage("Cannot access an object of type Tensor<rank_,0,Number>"));
1467 AssertIndexRange(i, dim);
1468 DEAL_II_CXX23_ASSUME(i < dim);
1469
1470 return values[i];
1471}
1472
1473
1474template <int rank_, int dim, typename Number>
1475constexpr inline DEAL_II_ALWAYS_INLINE const Number &
1477{
1478 Assert(dim != 0,
1479 ExcMessage("Cannot access an object of type Tensor<rank_,0,Number>"));
1480
1481 return TensorAccessors::extract<rank_>(*this, indices);
1482}
1483
1484
1485
1486template <int rank_, int dim, typename Number>
1487constexpr inline DEAL_II_ALWAYS_INLINE Number &
1489{
1490 Assert(dim != 0,
1491 ExcMessage("Cannot access an object of type Tensor<rank_,0,Number>"));
1492
1493 return TensorAccessors::extract<rank_>(*this, indices);
1494}
1495
1496
1497
1498template <int rank_, int dim, typename Number>
1499inline Number *
1501{
1502 static_assert(rank_ == 1,
1503 "This function is only available for rank-1 tensors "
1504 "because higher-rank tensors may not store their elements "
1505 "in a contiguous array.");
1506
1507 return std::addressof(
1508 this->operator[](this->unrolled_to_component_indices(0)));
1509}
1510
1511
1512
1513template <int rank_, int dim, typename Number>
1514inline const Number *
1516{
1517 static_assert(rank_ == 1,
1518 "This function is only available for rank-1 tensors "
1519 "because higher-rank tensors may not store their elements "
1520 "in a contiguous array.");
1521
1522 return std::addressof(
1523 this->operator[](this->unrolled_to_component_indices(0)));
1524}
1525
1526
1527
1528template <int rank_, int dim, typename Number>
1529inline Number *
1531{
1532 static_assert(rank_ == 1,
1533 "This function is only available for rank-1 tensors "
1534 "because higher-rank tensors may not store their elements "
1535 "in a contiguous array.");
1536
1537 return begin_raw() + n_independent_components;
1538}
1539
1540
1541
1542template <int rank_, int dim, typename Number>
1543inline const Number *
1545{
1546 static_assert(rank_ == 1,
1547 "This function is only available for rank-1 tensors "
1548 "because higher-rank tensors may not store their elements "
1549 "in a contiguous array.");
1550
1551 return begin_raw() + n_independent_components;
1552}
1553
1554
1555
1556template <int rank_, int dim, typename Number>
1557template <typename OtherNumber>
1560{
1561 // The following loop could be written more concisely using std::copy, but
1562 // that function is only constexpr from C++20 on.
1563 for (unsigned int i = 0; i < dim; ++i)
1564 values[i] = t.values[i];
1565 return *this;
1566}
1567
1568
1569
1570template <int rank_, int dim, typename Number>
1573 Tensor<rank_, dim, Number>::operator=(const Number &d) &
1574{
1576
1577 for (unsigned int i = 0; i < dim; ++i)
1578 values[i] = internal::NumberType<Number>::value(0.0);
1579 return *this;
1580}
1581
1582
1583# ifdef DEAL_II_DELETED_MOVE_CONSTRUCTOR_BUG
1584template <int rank_, int dim, typename Number>
1587{
1588 for (unsigned int i = 0; i < dim; ++i)
1589 values[i] = other.values[i];
1590 return *this;
1591}
1592
1593
1594
1595template <int rank_, int dim, typename Number>
1598 Tensor<rank_, dim, Number> &&other) noexcept
1599{
1600 for (unsigned int i = 0; i < dim; ++i)
1601 values[i] = other.values[i];
1602 return *this;
1603}
1604# endif
1605
1606
1607template <int rank_, int dim, typename Number>
1608template <typename OtherNumber>
1609constexpr inline bool
1611 const Tensor<rank_, dim, OtherNumber> &p) const
1612{
1613# ifdef DEAL_II_ADOLC_WITH_ADVANCED_BRANCHING
1614 Assert(!(std::is_same_v<Number, adouble> ||
1615 std::is_same_v<OtherNumber, adouble>),
1616 ExcMessage(
1617 "The Tensor equality operator for ADOL-C taped numbers has not yet "
1618 "been extended to support advanced branching."));
1619# endif
1620
1621 for (unsigned int i = 0; i < dim; ++i)
1622 if (numbers::values_are_not_equal(values[i], p.values[i]))
1623 return false;
1624 return true;
1625}
1626
1627
1628// At some places in the library, we have Point<0> for formal reasons
1629// (e.g., we sometimes have Quadrature<dim-1> for faces, so we have
1630// Quadrature<0> for dim=1, and then we have Point<0>). To avoid warnings
1631// in the above function that the loop end check always fails, we
1632// implement this function here
1633template <>
1634template <>
1635constexpr inline bool
1637{
1638 return true;
1639}
1640
1641
1642template <int rank_, int dim, typename Number>
1643template <typename OtherNumber>
1644constexpr bool
1646 const Tensor<rank_, dim, OtherNumber> &p) const
1647{
1648 return !((*this) == p);
1649}
1650
1651
1652template <int rank_, int dim, typename Number>
1653template <typename OtherNumber>
1654constexpr inline DEAL_II_ALWAYS_INLINE
1658{
1659 for (unsigned int i = 0; i < dim; ++i)
1660 values[i] += p.values[i];
1661 return *this;
1662}
1663
1664
1665template <int rank_, int dim, typename Number>
1666template <typename OtherNumber>
1667constexpr inline DEAL_II_ALWAYS_INLINE
1671{
1672 for (unsigned int i = 0; i < dim; ++i)
1673 values[i] -= p.values[i];
1674 return *this;
1675}
1676
1677
1678template <int rank_, int dim, typename Number>
1679template <typename OtherNumber>
1680constexpr inline DEAL_II_ALWAYS_INLINE
1682 Tensor<rank_, dim, Number>::operator*=(const OtherNumber &s)
1683{
1684 for (unsigned int i = 0; i < dim; ++i)
1685 values[i] *= s;
1686 return *this;
1687}
1688
1689
1690
1691template <int rank_, int dim, typename Number>
1692template <typename OtherNumber>
1693constexpr inline DEAL_II_ALWAYS_INLINE
1695 Tensor<rank_, dim, Number>::operator/=(const OtherNumber &s)
1696{
1697 if constexpr (std::is_integral_v<
1699 std::is_same_v<Number, Differentiation::SD::Expression>)
1700 {
1701 // recurse over the base objects
1702 for (unsigned int d = 0; d < dim; ++d)
1703 values[d] /= s;
1704 }
1705 else
1706 {
1707 // If we can, avoid division by multiplying by the inverse of the given
1708 // factor:
1709 const Number inverse_factor = Number(1.) / s;
1710 for (unsigned int d = 0; d < dim; ++d)
1711 values[d] *= inverse_factor;
1712 }
1713
1714 return *this;
1715}
1716
1717
1718template <int rank_, int dim, typename Number>
1719constexpr inline DEAL_II_ALWAYS_INLINE
1722{
1724
1725 for (unsigned int i = 0; i < dim; ++i)
1726 tmp.values[i] = -values[i];
1727
1728 return tmp;
1729}
1730
1731
1732template <int rank_, int dim, typename Number>
1735{
1736 // Handle cases of a tensor consisting of just one number more
1737 // efficiently:
1738 if constexpr ((rank_ == 1) && (dim == 1) && std::is_arithmetic_v<Number>)
1739 {
1740 return std::abs(values[0]);
1741 }
1742 else if constexpr ((rank_ == 2) && (dim == 1) && std::is_arithmetic_v<Number>)
1743 {
1744 return std::abs(values[0][0]);
1745 }
1746 else
1747 {
1748 // Otherwise fall back to the naive algorithm of taking the square root of
1749 // the sum of squares.
1750
1751 // Make things work with AD types by letting the compiler look up
1752 // the symbol sqrt in namespace std and in the type-associated
1753 // namespaces
1754 using std::sqrt;
1755 return sqrt(norm_square());
1756 }
1757}
1758
1759
1760template <int rank_, int dim, typename Number>
1764{
1765 if constexpr (dim == 0)
1766 return internal::NumberType<
1767 typename numbers::NumberTraits<Number>::real_type>::value(0.0);
1768 else if constexpr (rank_ == 1)
1769 {
1770 // For rank-1 tensors, the square of the norm is simply the sum of
1771 // squares of the elements:
1774 for (unsigned int i = 1; i < dim; ++i)
1776
1777 return s;
1778 }
1779 else
1780 {
1781 // For higher-rank tensors, the square of the norm is the sum
1782 // of squares of sub-tensors
1784 values[0].norm_square();
1785 for (unsigned int i = 1; i < dim; ++i)
1786 s += values[i].norm_square();
1787
1788 return s;
1789 }
1790}
1791
1792
1793
1794template <int rank_, int dim, typename Number>
1795template <class Iterator>
1796inline void
1798 const Iterator end) const
1799{
1800 if constexpr (rank_ > 1)
1801 {
1802 // For higher-rank tensors, we recurse to the sub-tensors:
1803 Iterator next = begin;
1804 for (unsigned int i = 0; i < dim; ++i)
1805 {
1806 values[i].unroll(next, end);
1807 std::advance(
1809 }
1810 }
1811 else
1812 {
1813 // For rank-1 tensors, we can simply copy the current elements from
1814 // our linear array into the output range:
1815 Assert(std::distance(begin, end) >= dim,
1816 ExcMessage(
1817 "The provided iterator range must contain at least 'dim' "
1818 "elements."));
1819 std::copy(values.data(), values.data() + values.size(), begin);
1820 }
1821}
1822
1823
1824
1825template <int rank_, int dim, typename Number>
1826constexpr inline unsigned int
1828 const TableIndices<rank_> &indices)
1829{
1830 unsigned int index = 0;
1831 for (int r = 0; r < rank_; ++r)
1832 index = index * dim + indices[r];
1833
1834 return index;
1835}
1836
1837
1838
1839template <int rank_, int dim, typename Number>
1840constexpr inline TableIndices<rank_>
1842{
1843 // Work-around nvcc warning
1844 unsigned int dummy = n_independent_components;
1845 AssertIndexRange(i, dummy);
1846
1847 if constexpr (dim == 0)
1848 {
1849 Assert(false,
1850 ExcMessage(
1851 "A tensor with dimension 0 does not store any elements. "
1852 "There is no indexing that can address its elements."));
1853 return {};
1854 }
1855 else
1856 {
1857 TableIndices<rank_> indices;
1858
1859 unsigned int remainder = i;
1860 for (int r = rank_ - 1; r >= 0; --r)
1861 {
1862 indices[r] = remainder % dim;
1863 remainder = remainder / dim;
1864 }
1865 Assert(remainder == 0, ExcInternalError());
1866
1867 return indices;
1868 }
1869}
1870
1871
1872template <int rank_, int dim, typename Number>
1873constexpr inline void
1875{
1876 for (unsigned int i = 0; i < dim; ++i)
1877 values[i] = internal::NumberType<Number>::value(0.0);
1878}
1879
1880
1881template <int rank_, int dim, typename Number>
1882constexpr std::size_t
1884{
1885 return sizeof(Tensor<rank_, dim, Number>);
1886}
1887
1888
1889template <int rank_, int dim, typename Number>
1890template <class Archive>
1891inline void
1892Tensor<rank_, dim, Number>::serialize(Archive &ar, const unsigned int)
1893{
1894 for (int i = 0; i < dim; ++i)
1895 {
1896 ar &values[i];
1897 }
1898}
1899
1900
1901template <int rank_, int dim, typename Number>
1903
1904#endif // DOXYGEN
1905
1906/* ----------------- Non-member functions operating on tensors. ------------ */
1907
1920template <int rank_, int dim, typename Number>
1921inline std::ostream &
1922operator<<(std::ostream &out, const Tensor<rank_, dim, Number> &p)
1923{
1924 for (unsigned int i = 0; i < dim; ++i)
1925 {
1926 out << p[i];
1927 if (i != dim - 1)
1928 for (unsigned int j = 0; j < rank_; ++j)
1929 out << ' ';
1930 }
1931
1932 return out;
1933}
1934
1935
1942template <int dim, typename Number>
1943inline std::ostream &
1944operator<<(std::ostream &out, const Tensor<0, dim, Number> &p)
1945{
1946 out << static_cast<const Number &>(p);
1947 return out;
1948}
1949
1950
1969template <int dim, typename Number, typename Other>
1972 operator*(const Other &object, const Tensor<0, dim, Number> &t)
1973{
1974 return object * static_cast<const Number &>(t);
1975}
1976
1977
1978
1989template <int dim, typename Number, typename Other>
1992 operator*(const Tensor<0, dim, Number> &t, const Other &object)
1993{
1994 return static_cast<const Number &>(t) * object;
1995}
1996
1997
2009template <int dim, typename Number, typename OtherNumber>
2013 const Tensor<0, dim, OtherNumber> &src2)
2014{
2015 return static_cast<const Number &>(src1) *
2016 static_cast<const OtherNumber &>(src2);
2017}
2018
2019
2027template <int dim, typename Number, typename OtherNumber>
2029 Tensor<0,
2030 dim,
2031 typename ProductType<Number,
2032 typename EnableIfScalar<OtherNumber>::type>::type>
2033 operator/(const Tensor<0, dim, Number> &t, const OtherNumber &factor)
2034{
2035 return static_cast<const Number &>(t) / factor;
2036}
2037
2038
2046template <int dim, typename Number, typename OtherNumber>
2051{
2052 return static_cast<const Number &>(p) + static_cast<const OtherNumber &>(q);
2053}
2054
2055
2063template <int dim, typename Number, typename OtherNumber>
2068{
2069 return static_cast<const Number &>(p) - static_cast<const OtherNumber &>(q);
2070}
2071
2072
2085template <int rank, int dim, typename Number, typename OtherNumber>
2087 Tensor<rank,
2088 dim,
2089 typename ProductType<Number,
2090 typename EnableIfScalar<OtherNumber>::type>::type>
2091 operator*(const Tensor<rank, dim, Number> &t, const OtherNumber &factor)
2092{
2094 tt *= factor;
2095 return tt;
2096}
2097
2098
2111template <int rank, int dim, typename Number, typename OtherNumber>
2113 Tensor<rank,
2114 dim,
2116 OtherNumber>::type>
2117 operator*(const Number &factor, const Tensor<rank, dim, OtherNumber> &t)
2118{
2119 // simply forward to the operator above
2120 return t * factor;
2121}
2122
2123
2124
2134template <int rank, int dim, typename Number, typename OtherNumber>
2136 Tensor<rank,
2137 dim,
2138 typename ProductType<Number,
2139 typename EnableIfScalar<OtherNumber>::type>::type>
2140 operator/(const Tensor<rank, dim, Number> &t, const OtherNumber &factor)
2141{
2143 tt /= factor;
2144 return tt;
2145}
2146
2147
2157template <int rank, int dim, typename Number, typename OtherNumber>
2167
2168
2178template <int rank, int dim, typename Number, typename OtherNumber>
2188
2195template <int dim, typename Number, typename OtherNumber>
2196inline constexpr DEAL_II_ALWAYS_INLINE
2199 const Tensor<0, dim, OtherNumber> &src2)
2200{
2202
2203 tmp *= src2;
2204
2205 return tmp;
2206}
2207
2224template <int rank, int dim, typename Number, typename OtherNumber>
2225inline constexpr DEAL_II_ALWAYS_INLINE
2229{
2231
2232 for (unsigned int i = 0; i < dim; ++i)
2235
2236 return tmp;
2237}
2238
2283template <int rank_1,
2284 int rank_2,
2285 int dim,
2286 typename Number,
2287 typename OtherNumber,
2288 typename = std::enable_if_t<rank_1 >= 1 && rank_2 >= 1>>
2289constexpr inline DEAL_II_ALWAYS_INLINE
2290 typename Tensor<rank_1 + rank_2 - 2,
2291 dim,
2292 typename ProductType<Number, OtherNumber>::type>::tensor_type
2295{
2296 // Treat some common cases separately. Specifically, these are the dot
2297 // product between two rank-1 tensors, and the product between a
2298 // rank-2 tensor and a rank-1 tensor. Both of these lead to a linear
2299 // loop over adjacent memory and can be dealt with efficiently; in the
2300 // latter case (rank-2 times rank-1), we implement things by deferring
2301 // to rank-1 times rank-1 dot products.
2302 if constexpr ((rank_1 == 1) && (rank_2 == 1))
2303 {
2304 // This is a dot product between two rank-1 tensors. Write it out as
2305 // a linear loop:
2306 static_assert(dim > 0, "Tensors cannot have dimension zero.");
2307 typename ProductType<Number, OtherNumber>::type sum = src1[0] * src2[0];
2308 for (unsigned int i = 1; i < dim; ++i)
2309 sum += src1[i] * src2[i];
2310
2311 return sum;
2312 }
2313 else if constexpr ((rank_1 == 2) && (rank_2 == 1))
2314 {
2315 // This is a product between a rank-2 and a rank-1 tensor. This
2316 // corresponds to taking dot products between the rows of the former
2317 // and the latter.
2318 typename Tensor<
2319 rank_1 + rank_2 - 2,
2320 dim,
2321 typename ProductType<Number, OtherNumber>::type>::tensor_type result;
2322 for (unsigned int i = 0; i < dim; ++i)
2323 result[i] += src1[i] * src2;
2324
2325 return result;
2326 }
2327 else
2328 {
2329 // Treat all of the other cases using the more general contraction
2330 // machinery.
2331 typename Tensor<
2332 rank_1 + rank_2 - 2,
2333 dim,
2334 typename ProductType<Number, OtherNumber>::type>::tensor_type result{};
2335
2336 TensorAccessors::internal::
2337 ReorderedIndexView<0, rank_2, const Tensor<rank_2, dim, OtherNumber>>
2338 reordered = TensorAccessors::reordered_index_view<0, rank_2>(src2);
2339 TensorAccessors::contract<1, rank_1, rank_2, dim>(result,
2340 src1,
2341 reordered);
2342
2343 return result;
2344 }
2345}
2346
2347
2376template <int index_1,
2377 int index_2,
2378 int rank_1,
2379 int rank_2,
2380 int dim,
2381 typename Number,
2382 typename OtherNumber>
2383constexpr inline DEAL_II_ALWAYS_INLINE
2384 typename Tensor<rank_1 + rank_2 - 2,
2385 dim,
2386 typename ProductType<Number, OtherNumber>::type>::tensor_type
2389{
2390 Assert(0 <= index_1 && index_1 < rank_1,
2391 ExcMessage(
2392 "The specified index_1 must lie within the range [0,rank_1)"));
2393 Assert(0 <= index_2 && index_2 < rank_2,
2394 ExcMessage(
2395 "The specified index_2 must lie within the range [0,rank_2)"));
2396
2397 using namespace TensorAccessors;
2398 using namespace TensorAccessors::internal;
2399
2400 // Reorder index_1 to the end of src1:
2402 reord_01 = reordered_index_view<index_1, rank_1>(src1);
2403
2404 // Reorder index_2 to the end of src2:
2405 const ReorderedIndexView<index_2,
2406 rank_2,
2408 reord_02 = reordered_index_view<index_2, rank_2>(src2);
2409
2410 typename Tensor<rank_1 + rank_2 - 2,
2411 dim,
2412 typename ProductType<Number, OtherNumber>::type>::tensor_type
2413 result{};
2414 TensorAccessors::contract<1, rank_1, rank_2, dim>(result, reord_01, reord_02);
2415 return result;
2416}
2417
2418
2449template <int index_1,
2450 int index_2,
2451 int index_3,
2452 int index_4,
2453 int rank_1,
2454 int rank_2,
2455 int dim,
2456 typename Number,
2457 typename OtherNumber>
2458constexpr inline
2459 typename Tensor<rank_1 + rank_2 - 4,
2460 dim,
2461 typename ProductType<Number, OtherNumber>::type>::tensor_type
2462 double_contract(const Tensor<rank_1, dim, Number> &src1,
2464{
2465 Assert(0 <= index_1 && index_1 < rank_1,
2466 ExcMessage(
2467 "The specified index_1 must lie within the range [0,rank_1)"));
2468 Assert(0 <= index_3 && index_3 < rank_1,
2469 ExcMessage(
2470 "The specified index_3 must lie within the range [0,rank_1)"));
2471 Assert(index_1 != index_3,
2472 ExcMessage("index_1 and index_3 must not be the same"));
2473 Assert(0 <= index_2 && index_2 < rank_2,
2474 ExcMessage(
2475 "The specified index_2 must lie within the range [0,rank_2)"));
2476 Assert(0 <= index_4 && index_4 < rank_2,
2477 ExcMessage(
2478 "The specified index_4 must lie within the range [0,rank_2)"));
2479 Assert(index_2 != index_4,
2480 ExcMessage("index_2 and index_4 must not be the same"));
2481
2482 using namespace TensorAccessors;
2483 using namespace TensorAccessors::internal;
2484
2485 // Reorder index_1 to the end of src1:
2487 reord_1 = TensorAccessors::reordered_index_view<index_1, rank_1>(src1);
2488
2489 // Reorder index_2 to the end of src2:
2491 reord_2 = TensorAccessors::reordered_index_view<index_2, rank_2>(src2);
2492
2493 // Now, reorder index_3 to the end of src1. We have to make sure to
2494 // preserve the original ordering: index_1 has been removed. If
2495 // index_3 > index_1, we have to use (index_3 - 1) instead:
2497 (index_3 < index_1 ? index_3 : index_3 - 1),
2498 rank_1,
2499 ReorderedIndexView<index_1, rank_1, const Tensor<rank_1, dim, Number>>>
2501 index_3<index_1 ? index_3 : index_3 - 1, rank_1>(reord_1);
2502
2503 // Now, reorder index_4 to the end of src2. We have to make sure to
2504 // preserve the original ordering: index_2 has been removed. If
2505 // index_4 > index_2, we have to use (index_4 - 1) instead:
2507 (index_4 < index_2 ? index_4 : index_4 - 1),
2508 rank_2,
2511 index_4<index_2 ? index_4 : index_4 - 1, rank_2>(reord_2);
2512
2513 typename Tensor<rank_1 + rank_2 - 4,
2514 dim,
2515 typename ProductType<Number, OtherNumber>::type>::tensor_type
2516 result{};
2517 TensorAccessors::contract<2, rank_1, rank_2, dim>(result, reord_3, reord_4);
2518 return result;
2519}
2520
2521
2534template <int rank, int dim, typename Number, typename OtherNumber>
2535constexpr inline DEAL_II_ALWAYS_INLINE
2537 scalar_product(const Tensor<rank, dim, Number> &left,
2538 const Tensor<rank, dim, OtherNumber> &right)
2539{
2541 TensorAccessors::contract<rank, rank, rank, dim>(result, left, right);
2542 return result;
2543}
2544
2545
2563template <template <int, int, typename> class TensorT1,
2564 template <int, int, typename> class TensorT2,
2565 template <int, int, typename> class TensorT3,
2566 int rank_1,
2567 int rank_2,
2568 int dim,
2569 typename T1,
2570 typename T2,
2571 typename T3>
2572constexpr inline DEAL_II_ALWAYS_INLINE
2574 contract3(const TensorT1<rank_1, dim, T1> &left,
2575 const TensorT2<rank_1 + rank_2, dim, T2> &middle,
2576 const TensorT3<rank_2, dim, T3> &right)
2577{
2578 using return_type =
2580 return TensorAccessors::contract3<rank_1, rank_2, dim, return_type>(left,
2581 middle,
2582 right);
2583}
2584
2585
2596template <int rank_1,
2597 int rank_2,
2598 int dim,
2599 typename Number,
2600 typename OtherNumber>
2601constexpr inline DEAL_II_ALWAYS_INLINE
2605{
2606 typename Tensor<rank_1 + rank_2,
2607 dim,
2608 typename ProductType<Number, OtherNumber>::type>::tensor_type
2609 result{};
2610 TensorAccessors::contract<0, rank_1, rank_2, dim>(result, src1, src2);
2611 return result;
2612}
2613
2614
2633template <int dim, typename Number>
2635cross_product_2d(const Tensor<1, dim, Number> &src)
2636{
2637 Assert(dim == 2, ExcInternalError());
2638
2640
2641 result[0] = src[1];
2642 result[1] = -src[0];
2643
2644 return result;
2645}
2646
2647
2657template <int dim, typename Number1, typename Number2>
2658constexpr inline DEAL_II_ALWAYS_INLINE
2660 cross_product_3d(const Tensor<1, dim, Number1> &src1,
2661 const Tensor<1, dim, Number2> &src2)
2662{
2663 Assert(dim == 3, ExcInternalError());
2664
2666
2667 if constexpr (dim == 3)
2668 {
2669 result[0] = src1[1] * src2[2] - src1[2] * src2[1];
2670 result[1] = src1[2] * src2[0] - src1[0] * src2[2];
2671 result[2] = src1[0] * src2[1] - src1[1] * src2[0];
2672 }
2673
2674 return result;
2675}
2676
2677
2691template <int dim, typename Number>
2692constexpr inline DEAL_II_ALWAYS_INLINE Number
2694{
2695 // Compute the determinant using the Laplace expansion of the
2696 // determinant. We expand along the last row.
2698
2699 for (unsigned int k = 0; k < dim; ++k)
2700 {
2701 Tensor<2, dim - 1, Number> minor;
2702 for (unsigned int i = 0; i < dim - 1; ++i)
2703 for (unsigned int j = 0; j < dim - 1; ++j)
2704 minor[i][j] = t[i][j < k ? j : j + 1];
2705
2706 const Number cofactor = ((k % 2 == 0) ? -1. : 1.) * determinant(minor);
2707
2708 det += t[dim - 1][k] * cofactor;
2709 }
2710
2711 return ((dim % 2 == 0) ? 1. : -1.) * det;
2712}
2713
2719template <typename Number>
2722{
2723 return t[0][0];
2724}
2725
2731template <typename Number>
2734{
2735 // hard-coded for efficiency reasons
2736 return t[0][0] * t[1][1] - t[1][0] * t[0][1];
2737}
2738
2744template <typename Number>
2747{
2748 // hard-coded for efficiency reasons
2749 const Number C0 = internal::NumberType<Number>::value(t[1][1] * t[2][2]) -
2750 internal::NumberType<Number>::value(t[1][2] * t[2][1]);
2751 const Number C1 = internal::NumberType<Number>::value(t[1][2] * t[2][0]) -
2752 internal::NumberType<Number>::value(t[1][0] * t[2][2]);
2753 const Number C2 = internal::NumberType<Number>::value(t[1][0] * t[2][1]) -
2754 internal::NumberType<Number>::value(t[1][1] * t[2][0]);
2755 return t[0][0] * C0 + t[0][1] * C1 + t[0][2] * C2;
2756}
2757
2758
2765template <int dim, typename Number>
2766constexpr inline DEAL_II_ALWAYS_INLINE Number
2768{
2769 Number t = d[0][0];
2770 for (unsigned int i = 1; i < dim; ++i)
2771 t += d[i][i];
2772 return t;
2773}
2774
2775
2784template <int dim, typename Number>
2785constexpr inline Tensor<2, dim, Number>
2787{
2788 Number return_tensor[dim][dim];
2789
2790 // if desired, take over the
2791 // inversion of a 4x4 tensor
2792 // from the FullMatrix
2794
2795 return Tensor<2, dim, Number>(return_tensor);
2796}
2797
2798
2799#ifndef DOXYGEN
2800
2801template <typename Number>
2804{
2805 Tensor<2, 1, Number> return_tensor;
2806
2807 return_tensor[0][0] = internal::NumberType<Number>::value(1.0 / t[0][0]);
2808
2809 return return_tensor;
2810}
2811
2812
2813template <typename Number>
2816{
2817 Tensor<2, 2, Number> return_tensor;
2818
2820 1.0 / (t[0][0] * t[1][1] - t[1][0] * t[0][1]));
2821 return_tensor[0][0] = t[1][1];
2822 return_tensor[0][1] = -t[0][1];
2823 return_tensor[1][0] = -t[1][0];
2824 return_tensor[1][1] = t[0][0];
2825 return_tensor *= inv_det_t;
2826
2827 return return_tensor;
2828}
2829
2830template <typename Number>
2833{
2834 Tensor<2, 3, Number> return_tensor;
2835
2836 const auto value = [](const auto &t) {
2838 };
2839
2840 return_tensor[0][0] = value(t[1][1] * t[2][2]) - value(t[1][2] * t[2][1]);
2841 return_tensor[0][1] = value(t[0][2] * t[2][1]) - value(t[0][1] * t[2][2]);
2842 return_tensor[0][2] = value(t[0][1] * t[1][2]) - value(t[0][2] * t[1][1]);
2843 return_tensor[1][0] = value(t[1][2] * t[2][0]) - value(t[1][0] * t[2][2]);
2844 return_tensor[1][1] = value(t[0][0] * t[2][2]) - value(t[0][2] * t[2][0]);
2845 return_tensor[1][2] = value(t[0][2] * t[1][0]) - value(t[0][0] * t[1][2]);
2846 return_tensor[2][0] = value(t[1][0] * t[2][1]) - value(t[1][1] * t[2][0]);
2847 return_tensor[2][1] = value(t[0][1] * t[2][0]) - value(t[0][0] * t[2][1]);
2848 return_tensor[2][2] = value(t[0][0] * t[1][1]) - value(t[0][1] * t[1][0]);
2849
2850 const Number inv_det_t =
2851 value(1.0 / (t[0][0] * return_tensor[0][0] + t[0][1] * return_tensor[1][0] +
2852 t[0][2] * return_tensor[2][0]));
2853 return_tensor *= inv_det_t;
2854
2855 return return_tensor;
2856}
2857
2858#endif /* DOXYGEN */
2859
2860
2866template <int dim, typename Number>
2869{
2871 for (unsigned int i = 0; i < dim; ++i)
2872 {
2873 tt[i][i] = t[i][i];
2874 for (unsigned int j = i + 1; j < dim; ++j)
2875 {
2876 tt[i][j] = t[j][i];
2877 tt[j][i] = t[i][j];
2878 };
2879 }
2880 return tt;
2881}
2882
2883
2897template <int dim, typename Number>
2898constexpr Tensor<2, dim, Number>
2899adjugate(const Tensor<2, dim, Number> &t)
2900{
2901 return determinant(t) * invert(t);
2902}
2903
2904
2918template <int dim, typename Number>
2919constexpr Tensor<2, dim, Number>
2920cofactor(const Tensor<2, dim, Number> &t)
2921{
2922 return transpose(adjugate(t));
2923}
2924
2925
2989template <int dim, typename Number>
2992
2993
3001template <int dim, typename Number>
3002inline Number
3004{
3005 Number max = internal::NumberType<Number>::value(0.0);
3006 for (unsigned int j = 0; j < dim; ++j)
3007 {
3008 Number sum = internal::NumberType<Number>::value(0.0);
3009 for (unsigned int i = 0; i < dim; ++i)
3010 sum += numbers::NumberTraits<Number>::abs(t[i][j]);
3011
3012 if (sum > max)
3013 max = sum;
3014 }
3015
3016 return max;
3017}
3018
3019
3027template <int dim, typename Number>
3028inline Number
3030{
3031 Number max = internal::NumberType<Number>::value(0.0);
3032 for (unsigned int i = 0; i < dim; ++i)
3033 {
3034 Number sum = internal::NumberType<Number>::value(0.0);
3035 for (unsigned int j = 0; j < dim; ++j)
3036 sum += numbers::NumberTraits<Number>::abs(t[i][j]);
3037
3038 if (sum > max)
3039 max = sum;
3040 }
3041
3042 return max;
3043}
3044
3050#ifndef DOXYGEN
3051
3052
3053# ifdef DEAL_II_ADOLC_WITH_ADVANCED_BRANCHING
3054
3055// Specialization of functions for ADOL-C number types when
3056// the advanced branching feature is used
3057template <int dim>
3058inline adouble
3060{
3061 adouble max = internal::NumberType<adouble>::value(0.0);
3062 for (unsigned int j = 0; j < dim; ++j)
3063 {
3064 adouble sum = internal::NumberType<adouble>::value(0.0);
3065 for (unsigned int i = 0; i < dim; ++i)
3066 sum += fabs(t[i][j]);
3067
3068 condassign(max, (sum > max), sum, max);
3069 }
3070
3071 return max;
3072}
3073
3074
3075template <int dim>
3076inline adouble
3078{
3080 for (unsigned int i = 0; i < dim; ++i)
3081 {
3083 for (unsigned int j = 0; j < dim; ++j)
3084 sum += fabs(t[i][j]);
3085
3086 condassign(max, (sum > max), sum, max);
3087 }
3088
3089 return max;
3090}
3091
3092# endif // DEAL_II_ADOLC_WITH_ADVANCED_BRANCHING
3093
3094
3095#endif // DOXYGEN
3096
3098
3099#endif
*  iterator end()
*  *  iterator begin()
*  *  reference operator*() const
std::size_t size() const
Definition array_view.h:737
Definition point.h:111
constexpr Tensor & operator*=(const OtherNumber &factor)
constexpr Tensor & operator=(const OtherNumber &d) &&=delete
void serialize(Archive &ar, const unsigned int version)
constexpr Tensor & operator/=(const OtherNumber &factor)
constexpr Tensor & operator-=(const Tensor< 0, dim, OtherNumber > &rhs)
constexpr Tensor(const Tensor< 0, dim, OtherNumber > &initializer)
constexpr Tensor(const OtherNumber &initializer)
constexpr void clear()
constexpr real_type norm_square() const
constexpr bool operator!=(const Tensor< 0, dim, OtherNumber > &rhs) const
constexpr Tensor & operator=(const Tensor< 0, dim, OtherNumber > &rhs)
real_type norm() const
constexpr Tensor & operator+=(const Tensor< 0, dim, OtherNumber > &rhs)
void unroll(const Iterator begin, const Iterator end) const
constexpr bool operator==(const Tensor< 0, dim, OtherNumber > &rhs) const
typename numbers::NumberTraits< Number >::real_type real_type
Definition tensor.h:127
constexpr Tensor & operator=(const OtherNumber &d) &
constexpr Tensor operator-() const
constexpr Tensor & operator/=(const OtherNumber &factor)
constexpr Tensor(const ArrayView< ElementType, MemorySpace > &initializer)
constexpr bool operator==(const Tensor< rank_, dim, OtherNumber > &) const
constexpr Tensor< rank, dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator/(const Tensor< rank, dim, Number > &t, const OtherNumber &factor)
Definition tensor.h:2140
constexpr Tensor(const Tensor< 1, dim, Tensor< rank_ - 1, dim, OtherNumber > > &initializer)
constexpr const Number & operator[](const TableIndices< rank_ > &indices) const
constexpr Tensor< 0, dim, typename ProductType< Number, OtherNumber >::type > operator-(const Tensor< 0, dim, Number > &p, const Tensor< 0, dim, OtherNumber > &q)
Definition tensor.h:2066
constexpr Tensor< 0, dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator/(const Tensor< 0, dim, Number > &t, const OtherNumber &factor)
Definition tensor.h:2033
Number * begin_raw()
static constexpr unsigned int rank
Definition tensor.h:481
constexpr Tensor(const Tensor< rank_, dim, OtherNumber > &initializer)
std::conditional_t< rank_==1, Number, Tensor< rank_ - 1, dim, Number > > value_type
Definition tensor.h:506
numbers::NumberTraits< Number >::real_type norm() const
constexpr Tensor & operator-=(const Tensor< rank_, dim, OtherNumber > &)
constexpr void clear()
void unroll(const Iterator begin, const Iterator end) const
static constexpr unsigned int component_to_unrolled_index(const TableIndices< rank_ > &indices)
constexpr ProductType< Number, OtherNumber >::type operator*(const Tensor< 0, dim, Number > &src1, const Tensor< 0, dim, OtherNumber > &src2)
Definition tensor.h:2012
const Number * begin_raw() const
std::conditional_t< rank_==1, Number[(dim !=0) ? dim :1], typename Tensor< rank_ - 1, dim, Number >::array_type[(dim !=0) ? dim :1]> array_type
Definition tensor.h:522
constexpr bool operator!=(const Tensor< rank_, dim, OtherNumber > &) const
constexpr Tensor()
constexpr Tensor< 0, dim, typename ProductType< Number, OtherNumber >::type > schur_product(const Tensor< 0, dim, Number > &src1, const Tensor< 0, dim, OtherNumber > &src2)
Definition tensor.h:2198
constexpr value_type & operator[](const unsigned int i)
Number * end_raw()
friend class Tensor
Definition tensor.h:865
constexpr Tensor< rank, dim, typename ProductType< Number, OtherNumber >::type > operator+(const Tensor< rank, dim, Number > &p, const Tensor< rank, dim, OtherNumber > &q)
Definition tensor.h:2160
Number linfty_norm(const Tensor< 2, dim, Number > &t)
Definition tensor.h:3029
constexpr ProductType< Other, Number >::type operator*(const Other &object, const Tensor< 0, dim, Number > &t)
Definition tensor.h:1972
Number l1_norm(const Tensor< 2, dim, Number > &t)
Definition tensor.h:3003
static constexpr unsigned int dimension
Definition tensor.h:476
static constexpr TableIndices< rank_ > unrolled_to_component_indices(const unsigned int i)
constexpr Tensor & operator=(const Number &d) &&=delete
constexpr Tensor< rank, dim, typename ProductType< typename EnableIfScalar< Number >::type, OtherNumber >::type > operator*(const Number &factor, const Tensor< rank, dim, OtherNumber > &t)
Definition tensor.h:2117
static constexpr std::size_t memory_consumption()
constexpr Tensor & operator=(const Number &d) &
std::conditional_t< rank_==1, std::array< Number, dim >, std::array< Tensor< rank_ - 1, dim, Number >, dim > > values
Definition tensor.h:851
constexpr Tensor< 0, dim, typename ProductType< Number, OtherNumber >::type > operator+(const Tensor< 0, dim, Number > &p, const Tensor< 0, dim, OtherNumber > &q)
Definition tensor.h:2049
constexpr Tensor & operator+=(const Tensor< rank_, dim, OtherNumber > &)
const Number * end_raw() const
constexpr Number & operator[](const TableIndices< rank_ > &indices)
constexpr Tensor< rank, dim, typename ProductType< Number, OtherNumber >::type > schur_product(const Tensor< rank, dim, Number > &src1, const Tensor< rank, dim, OtherNumber > &src2)
Definition tensor.h:2227
constexpr numbers::NumberTraits< Number >::real_type norm_square() const
constexpr Tensor< rank, dim, typename ProductType< Number, OtherNumber >::type > operator-(const Tensor< rank, dim, Number > &p, const Tensor< rank, dim, OtherNumber > &q)
Definition tensor.h:2181
constexpr ProductType< Number, Other >::type operator*(const Tensor< 0, dim, Number > &t, const Other &object)
Definition tensor.h:1992
constexpr Tensor(const ArrayLike &initializer, std::index_sequence< Indices... >)
constexpr Tensor< rank, dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator*(const Tensor< rank, dim, Number > &t, const OtherNumber &factor)
Definition tensor.h:2091
void serialize(Archive &ar, const unsigned int version)
constexpr Tensor & operator*=(const OtherNumber &factor)
constexpr const value_type & operator[](const unsigned int i) const
constexpr Tensor(const array_type &initializer)
static constexpr unsigned int n_independent_components
Definition tensor.h:497
constexpr Tensor operator-() const
constexpr Tensor & operator=(const Tensor< rank_, dim, OtherNumber > &rhs)
#define DEAL_II_ALWAYS_INLINE
Definition config.h:166
#define DEAL_II_DEPRECATED
Definition config.h:294
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_CXX20_REQUIRES(condition)
Definition config.h:249
#define DEAL_II_CXX23_ASSUME(expr)
Definition config.h:266
#define DEAL_II_HOST_DEVICE
Definition config.h:171
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define DEAL_II_HOST_DEVICE_ALWAYS_INLINE
Definition config.h:172
DerivativeForm< 1, spacedim, dim, Number > transpose(const DerivativeForm< 1, dim, spacedim, Number > &DF)
static ::ExceptionBase & ExcNotImplemented()
static ::ExceptionBase & ExcScalarAssignmentOnlyForZeroValue()
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
Expression fabs(const Expression &x)
constexpr char T
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
*  *  *  RotationFunction< dim, Number >::RotationFunction Number(dim)
*  *  *  *  std::vector< Number > ThermoPlasticMaterial< dim, ViscoplasticYieldLaw, Number >::get_state_parameters   const
constexpr internal::ReorderedIndexView< index, rank, T > reordered_index_view(T &t)
constexpr void contract(T1 &result, const T2 &left, const T3 &right)
constexpr T1 contract3(const T2 &left, const T3 &middle, const T4 &right)
T sum(const T &t, const MPI_Comm mpi_communicator)
constexpr bool values_are_not_equal(const Number1 &value_1, const Number2 &value_2)
Definition numbers.h:872
constexpr bool value_is_zero(const Number &value)
Definition numbers.h:880
constexpr bool values_are_equal(const Number1 &value_1, const Number2 &value_2)
Definition numbers.h:864
STL namespace.
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
typename internal::ProductTypeImpl< std::decay_t< T >, std::decay_t< U > >::type type
static constexpr const T & value(const T &t)
Definition numbers.h:668
decltype(std::declval< T >() *std::declval< U >()) type
static real_type abs(const number &x)
Definition numbers.h:566
static constexpr real_type abs_square(const number &x)
Definition numbers.h:557
constexpr Number determinant(const SymmetricTensor< 2, dim, Number > &)
constexpr SymmetricTensor< 2, dim, Number > invert(const SymmetricTensor< 2, dim, Number > &)
constexpr Number trace(const SymmetricTensor< 2, dim2, Number > &)
constexpr SymmetricTensor< 4, dim, Number > outer_product(const SymmetricTensor< 2, dim, Number > &t1, const SymmetricTensor< 2, dim, Number > &t2)
Tensor< 2, dim, Number > project_onto_orthogonal_tensors(const Tensor< 2, dim, Number > &A)
Definition tensor.cc:87
std::ostream & operator<<(std::ostream &out, const Tensor< rank_, dim, Number > &p)
Definition tensor.h:1922
constexpr Tensor< 0, dim, typename ProductType< Number, OtherNumber >::type > schur_product(const Tensor< 0, dim, Number > &src1, const Tensor< 0, dim, OtherNumber > &src2)
Definition tensor.h:2198
Number linfty_norm(const Tensor< 2, dim, Number > &t)
Definition tensor.h:3029
Number l1_norm(const Tensor< 2, dim, Number > &t)
Definition tensor.h:3003