deal.II version GIT relicensing-7182-g951947b1c4 2026-11-26 10:30: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
symmetric_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) 2005 - 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_symmetric_tensor_h
14#define dealii_symmetric_tensor_h
15
16
17#include <deal.II/base/config.h>
18
24#include <deal.II/base/tensor.h>
25#include <deal.II/base/types.h>
26
27#if DEAL_II_KOKKOS_VERSION_GTE(4, 3, 0)
28# include <Kokkos_Swap.hpp>
29#endif
30
31#include <array>
32
34
35// Forward declaration
36#ifndef DOXYGEN
37template <int rank, int dim, typename Number = double>
38class SymmetricTensor;
39#endif
40
51template <int dim, typename Number = double>
55
112template <int dim, typename Number = double>
116
154template <int dim, typename Number = double>
158
159template <int dim, typename Number>
163
164template <int dim, typename Number>
168
178template <int dim2, typename Number>
179DEAL_II_HOST_DEVICE constexpr inline DEAL_II_ALWAYS_INLINE Number
181
220template <int dim, typename Number>
224
238template <int dim, typename Number>
241
242
243
244namespace internal
245{
246 // Workaround: The following 4 overloads are necessary to be able to
247 // compile the library with Apple Clang 8 and older. We should remove
248 // these overloads again when we bump the minimal required version to
249 // something later than clang-3.6 / Apple Clang 6.3.
250 template <int rank, int dim, typename T, typename U>
251 struct ProductTypeImpl<SymmetricTensor<rank, dim, T>, std::complex<U>>
252 {
253 using type =
254 SymmetricTensor<rank,
255 dim,
256 std::complex<typename ProductType<T, U>::type>>;
257 };
258
259 template <int rank, int dim, typename T, typename U>
260 struct ProductTypeImpl<SymmetricTensor<rank, dim, std::complex<T>>,
261 std::complex<U>>
262 {
263 using type =
264 SymmetricTensor<rank,
265 dim,
266 std::complex<typename ProductType<T, U>::type>>;
267 };
268
269 template <typename T, int rank, int dim, typename U>
270 struct ProductTypeImpl<std::complex<T>, SymmetricTensor<rank, dim, U>>
271 {
272 using type =
273 SymmetricTensor<rank,
274 dim,
275 std::complex<typename ProductType<T, U>::type>>;
276 };
277
278 template <int rank, int dim, typename T, typename U>
279 struct ProductTypeImpl<std::complex<T>,
280 SymmetricTensor<rank, dim, std::complex<U>>>
281 {
282 using type =
283 SymmetricTensor<rank,
284 dim,
285 std::complex<typename ProductType<T, U>::type>>;
286 };
287 // end workaround
288
293 namespace SymmetricTensorImplementation
294 {
299 template <int rank, int dim, typename Number>
300 struct Inverse;
301 } // namespace SymmetricTensorImplementation
302
307 namespace SymmetricTensorAccessors
308 {
317 merge(const TableIndices<2> &previous_indices,
318 const unsigned int new_index,
319 const unsigned int position)
320 {
321 AssertIndexRange(position, 2);
322
323 if (position == 0)
324 return {new_index, numbers::invalid_unsigned_int};
325 else
326 return {previous_indices[0], new_index};
327 }
328
329
330
339 merge(const TableIndices<4> &previous_indices,
340 const unsigned int new_index,
341 const unsigned int position)
342 {
343 AssertIndexRange(position, 4);
344
345 switch (position)
346 {
347 case 0:
348 return {new_index,
352 case 1:
353 return {previous_indices[0],
354 new_index,
357 case 2:
358 return {previous_indices[0],
359 previous_indices[1],
360 new_index,
362 case 3:
363 return {previous_indices[0],
364 previous_indices[1],
365 previous_indices[2],
366 new_index};
367 default:
369 return {};
370 }
371 }
372
373
380 template <int rank1,
381 int rank2,
382 int dim,
383 typename Number,
384 typename OtherNumber = Number>
386 {
388 using type =
389 ::SymmetricTensor<rank1 + rank2 - 4, dim, value_type>;
390 };
391
392
399 template <int dim, typename Number, typename OtherNumber>
400 struct double_contraction_result<2, 2, dim, Number, OtherNumber>
401 {
403 };
404
405
406
419 template <int rank, int dim, typename Number>
421
425 template <int dim, typename Number>
426 struct StorageType<2, dim, Number>
427 {
432 static const unsigned int n_independent_components =
433 (dim * dim + dim) / 2;
434
439 };
440
441
442
446 template <int dim, typename Number>
447 struct StorageType<4, dim, Number>
448 {
454 static const unsigned int n_rank2_components = (dim * dim + dim) / 2;
455
459 static const unsigned int n_independent_components =
460 (n_rank2_components *
462
470 };
471
472
473
478 template <int rank, int dim, bool constness, typename Number>
480
487 template <int rank, int dim, typename Number>
488 struct AccessorTypes<rank, dim, true, Number>
489 {
490 using tensor_type = const ::SymmetricTensor<rank, dim, Number>;
491
492 using reference = const Number &;
493 };
494
501 template <int rank, int dim, typename Number>
502 struct AccessorTypes<rank, dim, false, Number>
503 {
505
506 using reference = Number &;
507 };
508
509
542 template <int rank, int dim, bool constness, int P, typename Number>
544 {
545 public:
549 using reference =
553
554 private:
576
581 constexpr DEAL_II_ALWAYS_INLINE
582 Accessor(const Accessor &) = default;
583
584 public:
589 constexpr Accessor<rank, dim, constness, P - 1, Number>
590 operator[](const unsigned int i);
591
596 constexpr Accessor<rank, dim, constness, P - 1, Number>
597 operator[](const unsigned int i) const;
598
599 private:
605
606 // Declare some other classes as friends. Make sure to work around bugs
607 // in some compilers:
608 template <int, int, typename>
609 friend class ::SymmetricTensor;
610 template <int, int, bool, int, typename>
611 friend class Accessor;
612 friend class ::SymmetricTensor<rank, dim, Number>;
613 friend class Accessor<rank, dim, constness, P + 1, Number>;
614 };
615
616
617
625 template <int rank, int dim, bool constness, typename Number>
626 class Accessor<rank, dim, constness, 1, Number>
627 {
628 public:
632 using reference =
636
637 private:
662
667 constexpr DEAL_II_ALWAYS_INLINE
668 Accessor(const Accessor &) = default;
669
670 public:
675 constexpr reference
676 operator[](const unsigned int);
677
682 constexpr reference
683 operator[](const unsigned int) const;
684
685 private:
691
692 // Declare some other classes as friends. Make sure to work around bugs
693 // in some compilers:
694 template <int, int, typename>
695 friend class ::SymmetricTensor;
696 template <int, int, bool, int, typename>
698 friend class ::SymmetricTensor<rank, dim, Number>;
699 friend class SymmetricTensorAccessors::
700 Accessor<rank, dim, constness, 2, Number>;
701 };
702 } // namespace SymmetricTensorAccessors
703} // namespace internal
704
705
706
779template <int rank_, int dim, typename Number>
781{
782public:
783 static_assert(rank_ % 2 == 0, "A SymmetricTensor must have even rank!");
784
793 static constexpr unsigned int dimension = dim;
794
798 static const unsigned int rank = rank_;
799
805 static constexpr unsigned int n_independent_components =
807 n_independent_components;
808
813 constexpr DEAL_II_ALWAYS_INLINE
814 SymmetricTensor() = default;
815
829 template <typename OtherNumber>
830 explicit DEAL_II_HOST_DEVICE
832
849 constexpr SymmetricTensor(const Number (&array)[n_independent_components]);
850
856 template <typename OtherNumber>
859
866 template <typename OtherNumber>
869
877 constexpr SymmetricTensor &
878 operator=(const Number &d);
879
885 constexpr
887
892 constexpr bool
894
899 constexpr bool
901
905 template <typename OtherNumber>
908
912 template <typename OtherNumber>
915
920 template <typename OtherNumber>
922 operator*=(const OtherNumber &factor);
923
927 template <typename OtherNumber>
929 operator/=(const OtherNumber &factor);
930
935 constexpr SymmetricTensor
936 operator-() const;
937
990 template <typename OtherNumber>
992 typename internal::SymmetricTensorAccessors::
993 double_contraction_result<rank_, 2, dim, Number, OtherNumber>::type
995
1000 template <typename OtherNumber>
1002 typename internal::SymmetricTensorAccessors::
1003 double_contraction_result<rank_, 4, dim, Number, OtherNumber>::type
1005
1010 constexpr Number &
1012
1017 constexpr const Number &
1018 operator()(const TableIndices<rank_> &indices) const;
1019
1025 constexpr internal::SymmetricTensorAccessors::
1026 Accessor<rank_, dim, true, rank_ - 1, Number>
1027 operator[](const unsigned int row) const;
1028
1034 constexpr internal::SymmetricTensorAccessors::
1035 Accessor<rank_, dim, false, rank_ - 1, Number>
1036 operator[](const unsigned int row);
1037
1044 constexpr const Number &
1045 operator[](const TableIndices<rank_> &indices) const;
1046
1053 constexpr Number &
1055
1063 constexpr const Number &
1064 access_raw_entry(const unsigned int unrolled_index) const;
1065
1073 constexpr Number &
1074 access_raw_entry(const unsigned int unrolled_index);
1075
1087 norm() const;
1088
1096 static DEAL_II_HOST_DEVICE constexpr unsigned int
1098
1105 unrolled_to_component_indices(const unsigned int i);
1106
1120 constexpr void
1122
1127 static DEAL_II_HOST_DEVICE constexpr std::size_t
1129
1135 template <class Archive>
1136 void
1137 serialize(Archive &ar, const unsigned int version);
1138
1139private:
1145
1149 using base_tensor_type = typename base_tensor_descriptor::base_tensor_type;
1150
1155
1156#ifndef DOXYGEN
1157
1158 // Make all other symmetric tensors friends.
1159 template <int, int, typename>
1160 friend class SymmetricTensor;
1161
1162 // Make a few more functions friends.
1163 template <int dim2, typename Number2>
1164 friend DEAL_II_HOST_DEVICE constexpr Number2
1166
1167 template <int dim2, typename Number2>
1170
1171 template <int dim2, typename Number2>
1174
1175 template <int dim2, typename Number2>
1178
1179 template <int dim2, typename Number2>
1182
1183 template <int dim2, typename Number2>
1186
1187
1188 // Make a few helper classes friends as well.
1190 Inverse<2, dim, Number>;
1191
1193 Inverse<4, dim, Number>;
1194#endif
1195};
1196
1197
1198
1199// ------------------------- inline functions ------------------------
1200
1201#ifndef DOXYGEN
1202
1203// provide declarations for static members
1204template <int rank, int dim, typename Number>
1205const unsigned int SymmetricTensor<rank, dim, Number>::dimension;
1206
1207template <int rank_, int dim, typename Number>
1208constexpr unsigned int
1209 SymmetricTensor<rank_, dim, Number>::n_independent_components;
1210
1211namespace internal
1212{
1213 namespace SymmetricTensorAccessors
1214 {
1215 template <int rank_, int dim, bool constness, int P, typename Number>
1217 Accessor<rank_, dim, constness, P, Number>::Accessor(
1218 tensor_type &tensor,
1219 const TableIndices<rank_> &previous_indices)
1220 : tensor(tensor)
1221 , previous_indices(previous_indices)
1222 {}
1223
1224
1225
1226 template <int rank_, int dim, bool constness, int P, typename Number>
1228 Accessor<rank_, dim, constness, P - 1, Number>
1229 Accessor<rank_, dim, constness, P, Number>::operator[](
1230 const unsigned int i)
1231 {
1232 return Accessor<rank_, dim, constness, P - 1, Number>(
1233 tensor, merge(previous_indices, i, rank_ - P));
1234 }
1235
1236
1237
1238 template <int rank_, int dim, bool constness, int P, typename Number>
1240 Accessor<rank_, dim, constness, P - 1, Number>
1241 Accessor<rank_, dim, constness, P, Number>::operator[](
1242 const unsigned int i) const
1243 {
1244 return Accessor<rank_, dim, constness, P - 1, Number>(
1245 tensor, merge(previous_indices, i, rank_ - P));
1246 }
1247
1248
1249
1250 template <int rank_, int dim, bool constness, typename Number>
1252 Accessor<rank_, dim, constness, 1, Number>::Accessor(
1253 tensor_type &tensor,
1254 const TableIndices<rank_> &previous_indices)
1255 : tensor(tensor)
1256 , previous_indices(previous_indices)
1257 {}
1258
1259
1260
1261 template <int rank_, int dim, bool constness, typename Number>
1263 typename Accessor<rank_, dim, constness, 1, Number>::reference
1264 Accessor<rank_, dim, constness, 1, Number>::operator[](
1265 const unsigned int i)
1266 {
1267 return tensor(merge(previous_indices, i, rank_ - 1));
1268 }
1269
1270
1271 template <int rank_, int dim, bool constness, typename Number>
1273 typename Accessor<rank_, dim, constness, 1, Number>::reference
1274 Accessor<rank_, dim, constness, 1, Number>::operator[](
1275 const unsigned int i) const
1276 {
1277 return tensor(merge(previous_indices, i, rank_ - 1));
1278 }
1279 } // namespace SymmetricTensorAccessors
1280} // namespace internal
1281
1282
1283
1284template <int rank_, int dim, typename Number>
1285template <typename OtherNumber>
1289{
1290 static_assert(rank == 2, "This function is only implemented for rank==2");
1291 for (unsigned int d = 0; d < dim; ++d)
1292 for (unsigned int e = 0; e < d; ++e)
1293 Assert(t[d][e] == t[e][d],
1294 ExcMessage("The incoming Tensor must be exactly symmetric."));
1295
1296 for (unsigned int d = 0; d < dim; ++d)
1297 data[d] = t[d][d];
1298
1299 for (unsigned int d = 0, c = 0; d < dim; ++d)
1300 for (unsigned int e = d + 1; e < dim; ++e, ++c)
1301 data[dim + c] = t[d][e];
1302}
1303
1304
1305
1306template <int rank_, int dim, typename Number>
1307template <typename OtherNumber>
1311 : data(initializer.data)
1312{}
1313
1314
1315
1316template <int rank_, int dim, typename Number>
1319 const Number (&array)[n_independent_components])
1320 : data(
1321 *reinterpret_cast<const typename base_tensor_type::array_type *>(array))
1322{
1323 // ensure that the reinterpret_cast above actually works
1324 static_assert(sizeof(typename base_tensor_type::array_type) == sizeof(array));
1325}
1326
1327
1328
1329template <int rank_, int dim, typename Number>
1330template <typename OtherNumber>
1335{
1336 data = t.data;
1337 return *this;
1338}
1339
1340
1341
1342template <int rank_, int dim, typename Number>
1346{
1348 ExcMessage("Only assignment with zero is allowed"));
1349 (void)d;
1350
1352
1353 return *this;
1354}
1355
1356
1357namespace internal
1358{
1359 namespace SymmetricTensorImplementation
1360 {
1361 template <int dim, typename Number>
1362 constexpr inline DEAL_II_ALWAYS_INLINE ::Tensor<2, dim, Number>
1363 convert_to_tensor(const ::SymmetricTensor<2, dim, Number> &s)
1364 {
1366
1367 // diagonal entries are stored first
1368 for (unsigned int d = 0; d < dim; ++d)
1369 t[d][d] = s.access_raw_entry(d);
1370
1371 // off-diagonal entries come next, row by row
1372 for (unsigned int d = 0, c = 0; d < dim; ++d)
1373 for (unsigned int e = d + 1; e < dim; ++e, ++c)
1374 {
1375 t[d][e] = s.access_raw_entry(dim + c);
1376 t[e][d] = s.access_raw_entry(dim + c);
1377 }
1378 return t;
1379 }
1380
1381
1382 template <int dim, typename Number>
1383 constexpr ::Tensor<4, dim, Number>
1384 convert_to_tensor(const ::SymmetricTensor<4, dim, Number> &st)
1385 {
1386 // utilize the symmetry properties of SymmetricTensor<4,dim>
1387 // discussed in the class documentation to avoid accessing all
1388 // independent elements of the input tensor more than once
1390
1391 for (unsigned int i = 0; i < dim; ++i)
1392 for (unsigned int j = i; j < dim; ++j)
1393 for (unsigned int k = 0; k < dim; ++k)
1394 for (unsigned int l = k; l < dim; ++l)
1395 t[TableIndices<4>(i, j, k, l)] = t[TableIndices<4>(i, j, l, k)] =
1396 t[TableIndices<4>(j, i, k, l)] =
1397 t[TableIndices<4>(j, i, l, k)] =
1398 st[TableIndices<4>(i, j, k, l)];
1399
1400 return t;
1401 }
1402
1403
1404 template <typename Number>
1405 struct Inverse<2, 1, Number>
1406 {
1407 DEAL_II_HOST_DEVICE constexpr static inline DEAL_II_ALWAYS_INLINE
1408 ::SymmetricTensor<2, 1, Number>
1409 value(const ::SymmetricTensor<2, 1, Number> &t)
1410 {
1412
1413 tmp[0][0] = 1.0 / t[0][0];
1414
1415 return tmp;
1416 }
1417 };
1418
1419
1420 template <typename Number>
1421 struct Inverse<2, 2, Number>
1422 {
1423 DEAL_II_HOST_DEVICE constexpr static inline DEAL_II_ALWAYS_INLINE
1424 ::SymmetricTensor<2, 2, Number>
1425 value(const ::SymmetricTensor<2, 2, Number> &t)
1426 {
1428
1429 // Sympy result: ([
1430 // [ t11/(t00*t11 - t01**2), -t01/(t00*t11 - t01**2)],
1431 // [-t01/(t00*t11 - t01**2), t00/(t00*t11 - t01**2)] ])
1432 const TableIndices<2> idx_00(0, 0);
1433 const TableIndices<2> idx_01(0, 1);
1434 const TableIndices<2> idx_11(1, 1);
1435 const Number inv_det_t =
1436 1.0 / (t[idx_00] * t[idx_11] - t[idx_01] * t[idx_01]);
1437 tmp[idx_00] = t[idx_11];
1438 tmp[idx_01] = -t[idx_01];
1439 tmp[idx_11] = t[idx_00];
1440 tmp *= inv_det_t;
1441
1442 return tmp;
1443 }
1444 };
1445
1446
1447 template <typename Number>
1448 struct Inverse<2, 3, Number>
1449 {
1450 DEAL_II_HOST_DEVICE constexpr static ::SymmetricTensor<2, 3, Number>
1451 value(const ::SymmetricTensor<2, 3, Number> &t)
1452 {
1454
1455 // Sympy result: ([
1456 // [ (t11*t22 - t12**2)/(t00*t11*t22 - t00*t12**2 - t01**2*t22 +
1457 // 2*t01*t02*t12 - t02**2*t11),
1458 // (-t01*t22 + t02*t12)/(t00*t11*t22 - t00*t12**2 - t01**2*t22 +
1459 // 2*t01*t02*t12 - t02**2*t11),
1460 // (t01*t12 - t02*t11)/(t00*t11*t22 - t00*t12**2 - t01**2*t22 +
1461 // 2*t01*t02*t12 - t02**2*t11)],
1462 // [ (-t01*t22 + t02*t12)/(t00*t11*t22 - t00*t12**2 - t01**2*t22 +
1463 // 2*t01*t02*t12 - t02**2*t11),
1464 // (t00*t22 - t02**2)/(t00*t11*t22 - t00*t12**2 - t01**2*t22 +
1465 // 2*t01*t02*t12 - t02**2*t11),
1466 // (t00*t12 - t01*t02)/(-t00*t11*t22 + t00*t12**2 + t01**2*t22 -
1467 // 2*t01*t02*t12 + t02**2*t11)],
1468 // [ (t01*t12 - t02*t11)/(t00*t11*t22 - t00*t12**2 - t01**2*t22 +
1469 // 2*t01*t02*t12 - t02**2*t11),
1470 // (t00*t12 - t01*t02)/(-t00*t11*t22 + t00*t12**2 + t01**2*t22 -
1471 // 2*t01*t02*t12 + t02**2*t11),
1472 // (-t00*t11 + t01**2)/(-t00*t11*t22 + t00*t12**2 + t01**2*t22 -
1473 // 2*t01*t02*t12 + t02**2*t11)] ])
1474 //
1475 // =
1476 //
1477 // [ (t11*t22 - t12**2)/det_t,
1478 // (-t01*t22 + t02*t12)/det_t,
1479 // (t01*t12 - t02*t11)/det_t],
1480 // [ (-t01*t22 + t02*t12)/det_t,
1481 // (t00*t22 - t02**2)/det_t,
1482 // (-t00*t12 + t01*t02)/det_t],
1483 // [ (t01*t12 - t02*t11)/det_t,
1484 // (-t00*t12 + t01*t02)/det_t,
1485 // (t00*t11 - t01**2)/det_t] ])
1486 //
1487 // with det_t = (t00*t11*t22 - t00*t12**2 - t01**2*t22 +
1488 // 2*t01*t02*t12 - t02**2*t11)
1489 const TableIndices<2> idx_00(0, 0);
1490 const TableIndices<2> idx_01(0, 1);
1491 const TableIndices<2> idx_02(0, 2);
1492 const TableIndices<2> idx_11(1, 1);
1493 const TableIndices<2> idx_12(1, 2);
1494 const TableIndices<2> idx_22(2, 2);
1495 const Number inv_det_t =
1496 1.0 / (t[idx_00] * t[idx_11] * t[idx_22] -
1497 t[idx_00] * t[idx_12] * t[idx_12] -
1498 t[idx_01] * t[idx_01] * t[idx_22] +
1499 2.0 * t[idx_01] * t[idx_02] * t[idx_12] -
1500 t[idx_02] * t[idx_02] * t[idx_11]);
1501 tmp[idx_00] = t[idx_11] * t[idx_22] - t[idx_12] * t[idx_12];
1502 tmp[idx_01] = -t[idx_01] * t[idx_22] + t[idx_02] * t[idx_12];
1503 tmp[idx_02] = t[idx_01] * t[idx_12] - t[idx_02] * t[idx_11];
1504 tmp[idx_11] = t[idx_00] * t[idx_22] - t[idx_02] * t[idx_02];
1505 tmp[idx_12] = -t[idx_00] * t[idx_12] + t[idx_01] * t[idx_02];
1506 tmp[idx_22] = t[idx_00] * t[idx_11] - t[idx_01] * t[idx_01];
1507 tmp *= inv_det_t;
1508
1509 return tmp;
1510 }
1511 };
1512
1513
1514 template <typename Number>
1515 struct Inverse<4, 1, Number>
1516 {
1517 DEAL_II_HOST_DEVICE constexpr static inline ::
1518 SymmetricTensor<4, 1, Number>
1519 value(const ::SymmetricTensor<4, 1, Number> &t)
1520 {
1522 tmp.data[0][0] = 1.0 / t.data[0][0];
1523 return tmp;
1524 }
1525 };
1526
1527
1528 template <typename Number>
1529 struct Inverse<4, 2, Number>
1530 {
1531 DEAL_II_HOST_DEVICE constexpr static inline ::
1532 SymmetricTensor<4, 2, Number>
1533 value(const ::SymmetricTensor<4, 2, Number> &t)
1534 {
1536
1537 // Inverting this tensor is a little more complicated than necessary,
1538 // since we store the data of 't' as a 3x3 matrix t.data, but the
1539 // product between a rank-4 and a rank-2 tensor is really not the
1540 // product between this matrix and the 3-vector of a rhs, but rather
1541 //
1542 // B.vec = t.data * mult * A.vec
1543 //
1544 // where mult is a 3x3 matrix with entries [[1,0,0],[0,1,0],[0,0,2]] to
1545 // capture the fact that we need to add up both the c_ij12*a_12 and the
1546 // c_ij21*a_21 terms.
1547 //
1548 // In addition, in this scheme, the identity tensor has the matrix
1549 // representation mult^-1.
1550 //
1551 // The inverse of 't' therefore has the matrix representation
1552 //
1553 // inv.data = mult^-1 * t.data^-1 * mult^-1
1554 //
1555 // in order to compute it, let's first compute the inverse of t.data and
1556 // put it into tmp.data; at the end of the function we then scale the
1557 // last row and column of the inverse by 1/2, corresponding to the left
1558 // and right multiplication with mult^-1.
1559 const Number t4 = t.data[0][0] * t.data[1][1],
1560 t6 = t.data[0][0] * t.data[1][2],
1561 t8 = t.data[0][1] * t.data[1][0],
1562 t00 = t.data[0][2] * t.data[1][0],
1563 t01 = t.data[0][1] * t.data[2][0],
1564 t04 = t.data[0][2] * t.data[2][0],
1565 t07 = 1.0 / (t4 * t.data[2][2] - t6 * t.data[2][1] -
1566 t8 * t.data[2][2] + t00 * t.data[2][1] +
1567 t01 * t.data[1][2] - t04 * t.data[1][1]);
1568 tmp.data[0][0] =
1569 (t.data[1][1] * t.data[2][2] - t.data[1][2] * t.data[2][1]) * t07;
1570 tmp.data[0][1] =
1571 -(t.data[0][1] * t.data[2][2] - t.data[0][2] * t.data[2][1]) * t07;
1572 tmp.data[0][2] =
1573 -(-t.data[0][1] * t.data[1][2] + t.data[0][2] * t.data[1][1]) * t07;
1574 tmp.data[1][0] =
1575 -(t.data[1][0] * t.data[2][2] - t.data[1][2] * t.data[2][0]) * t07;
1576 tmp.data[1][1] = (t.data[0][0] * t.data[2][2] - t04) * t07;
1577 tmp.data[1][2] = -(t6 - t00) * t07;
1578 tmp.data[2][0] =
1579 -(-t.data[1][0] * t.data[2][1] + t.data[1][1] * t.data[2][0]) * t07;
1580 tmp.data[2][1] = -(t.data[0][0] * t.data[2][1] - t01) * t07;
1581 tmp.data[2][2] = (t4 - t8) * t07;
1582
1583 // scale last row and column as mentioned
1584 // above
1585 tmp.data[2][0] /= 2;
1586 tmp.data[2][1] /= 2;
1587 tmp.data[0][2] /= 2;
1588 tmp.data[1][2] /= 2;
1589 tmp.data[2][2] /= 4;
1590
1591 return tmp;
1592 }
1593 };
1594
1595
1596
1601 template <class NumberType>
1603 device_swap(NumberType &x, NumberType &y) noexcept
1604 {
1605 if constexpr (std::is_same_v<NumberType, double> ||
1606 std::is_same_v<NumberType, float> ||
1607 std::is_same_v<NumberType, unsigned int>)
1608 {
1609# if DEAL_II_KOKKOS_VERSION_GTE(4, 3, 0)
1610 Kokkos::kokkos_swap(x, y);
1611# else
1612 NumberType tmp = x;
1613 x = y;
1614 y = tmp;
1615# endif
1616 }
1617 else
1619 }
1620
1621
1622
1623 template <typename Number>
1624 struct Inverse<4, 3, Number>
1625 {
1626 DEAL_II_HOST_DEVICE static ::SymmetricTensor<4, 3, Number>
1627 value(const ::SymmetricTensor<4, 3, Number> &t)
1628 {
1630
1631 // This function follows the exact same scheme as the 2d case, except
1632 // that hardcoding the inverse of a 6x6 matrix is pretty wasteful.
1633 // Instead, we use the Gauss-Jordan algorithm implemented for
1634 // FullMatrix. For historical reasons the following code is copied from
1635 // there, with the tangential benefit that we do not need to copy the
1636 // tensor entries to and from the FullMatrix.
1637 const unsigned int N = 6;
1638
1639 // First get an estimate of the size of the elements of this matrix,
1640 // for later checks whether the pivot element is large enough, or
1641 // whether we have to fear that the matrix is not regular.
1642 Number diagonal_sum = internal::NumberType<Number>::value(0.0);
1643 for (unsigned int i = 0; i < N; ++i)
1644 diagonal_sum += numbers::NumberTraits<Number>::abs(tmp.data[i][i]);
1645 const Number typical_diagonal_element =
1646 diagonal_sum / static_cast<double>(N);
1647 (void)typical_diagonal_element;
1648
1649 unsigned int p[N];
1650 for (unsigned int i = 0; i < N; ++i)
1651 p[i] = i;
1652
1653 for (unsigned int j = 0; j < N; ++j)
1654 {
1655 // Pivot search: search that part of the line on and right of the
1656 // diagonal for the largest element.
1658 unsigned int r = j;
1659 for (unsigned int i = j + 1; i < N; ++i)
1660 if (numbers::NumberTraits<Number>::abs(tmp.data[i][j]) > max)
1661 {
1663 r = i;
1664 }
1665
1666 // Check whether the pivot is too small
1667 Assert(max > 1.e-16 * typical_diagonal_element,
1668 ExcMessage("This tensor seems to be noninvertible"));
1669
1670 // Row interchange
1671 if (r > j)
1672 {
1673 for (unsigned int k = 0; k < N; ++k)
1674 device_swap(tmp.data[j][k], tmp.data[r][k]);
1675
1676 device_swap(p[j], p[r]);
1677 }
1678
1679 // Transformation
1680 const Number hr = 1. / tmp.data[j][j];
1681 tmp.data[j][j] = hr;
1682 for (unsigned int k = 0; k < N; ++k)
1683 {
1684 if (k == j)
1685 continue;
1686 for (unsigned int i = 0; i < N; ++i)
1687 {
1688 if (i == j)
1689 continue;
1690 tmp.data[i][k] -= tmp.data[i][j] * tmp.data[j][k] * hr;
1691 }
1692 }
1693 for (unsigned int i = 0; i < N; ++i)
1694 {
1695 tmp.data[i][j] *= hr;
1696 tmp.data[j][i] *= -hr;
1697 }
1698 tmp.data[j][j] = hr;
1699 }
1700
1701 // Column interchange
1702 Number hv[N];
1703 for (unsigned int i = 0; i < N; ++i)
1704 {
1705 for (unsigned int k = 0; k < N; ++k)
1706 hv[p[k]] = tmp.data[i][k];
1707 for (unsigned int k = 0; k < N; ++k)
1708 tmp.data[i][k] = hv[k];
1709 }
1710
1711 // Scale rows and columns. The mult matrix
1712 // here is diag[1, 1, 1, 1/2, 1/2, 1/2].
1713 for (unsigned int i = 3; i < 6; ++i)
1714 for (unsigned int j = 0; j < 3; ++j)
1715 tmp.data[i][j] /= 2;
1716
1717 for (unsigned int i = 0; i < 3; ++i)
1718 for (unsigned int j = 3; j < 6; ++j)
1719 tmp.data[i][j] /= 2;
1720
1721 for (unsigned int i = 3; i < 6; ++i)
1722 for (unsigned int j = 3; j < 6; ++j)
1723 tmp.data[i][j] /= 4;
1724
1725 return tmp;
1726 }
1727 };
1728
1729 } // namespace SymmetricTensorImplementation
1730} // namespace internal
1731
1732
1733
1734template <int rank_, int dim, typename Number>
1737 operator Tensor<rank_, dim, Number>() const
1738{
1739 return internal::SymmetricTensorImplementation::convert_to_tensor(*this);
1740}
1741
1742
1743
1744template <int rank_, int dim, typename Number>
1745DEAL_II_HOST_DEVICE constexpr bool
1748{
1749 return data == t.data;
1750}
1751
1752
1753
1754template <int rank_, int dim, typename Number>
1755DEAL_II_HOST_DEVICE constexpr bool
1758{
1759 return data != t.data;
1760}
1761
1762
1763
1764template <int rank_, int dim, typename Number>
1765template <typename OtherNumber>
1770{
1771 data += t.data;
1772 return *this;
1773}
1774
1775
1776
1777template <int rank_, int dim, typename Number>
1778template <typename OtherNumber>
1783{
1784 data -= t.data;
1785 return *this;
1786}
1787
1788
1789
1790template <int rank_, int dim, typename Number>
1791template <typename OtherNumber>
1795{
1796 data *= d;
1797 return *this;
1798}
1799
1800
1801
1802template <int rank_, int dim, typename Number>
1803template <typename OtherNumber>
1807{
1808 data /= d;
1809 return *this;
1810}
1811
1812
1813
1814template <int rank_, int dim, typename Number>
1818{
1819 SymmetricTensor tmp = *this;
1820 tmp.data = -tmp.data;
1821 return tmp;
1822}
1823
1824
1825
1826template <int rank_, int dim, typename Number>
1827DEAL_II_HOST_DEVICE constexpr inline DEAL_II_ALWAYS_INLINE void
1829{
1830 data.clear();
1831}
1832
1833
1834
1835template <int rank_, int dim, typename Number>
1836DEAL_II_HOST_DEVICE constexpr std::size_t
1838{
1839 // all memory consists of statically allocated memory of the current
1840 // object, no pointers
1842}
1843
1844
1845
1846namespace internal
1847{
1851 template <int dim, typename Number, typename OtherNumber = Number>
1853 typename SymmetricTensorAccessors::
1854 double_contraction_result<2, 2, dim, Number, OtherNumber>::type
1855 perform_double_contraction(
1856 const typename SymmetricTensorAccessors::StorageType<2, dim, Number>::
1857 base_tensor_type &data,
1858 const typename SymmetricTensorAccessors::
1859 StorageType<2, dim, OtherNumber>::base_tensor_type &sdata)
1860 {
1861 using result_type = typename SymmetricTensorAccessors::
1862 double_contraction_result<2, 2, dim, Number, OtherNumber>::type;
1863
1864 switch (dim)
1865 {
1866 case 1:
1867 return data[0] * sdata[0];
1868
1869 default:
1870 // Start with the non-diagonal part. These values appear
1871 // twice in the matrix, but are only stored once. So we can
1872 // get the double-contraction sum for these elements using
1873 // only one multiplication each, and at the end multiplying
1874 // things by 2.
1875 result_type sum = data[dim] * sdata[dim];
1876 for (unsigned int d = dim + 1; d < (dim * (dim + 1) / 2); ++d)
1877 sum += data[d] * sdata[d];
1878 sum += sum; // sum *= 2
1879
1880 // Now add the contributions from the diagonal
1881 for (unsigned int d = 0; d < dim; ++d)
1882 sum += data[d] * sdata[d];
1883 return sum;
1884 }
1885 }
1886
1887
1888
1893 template <int dim, typename Number, typename OtherNumber = Number>
1895 typename SymmetricTensorAccessors::
1896 double_contraction_result<4, 2, dim, Number, OtherNumber>::type
1897 perform_double_contraction(
1898 const typename SymmetricTensorAccessors::StorageType<4, dim, Number>::
1899 base_tensor_type &data,
1900 const typename SymmetricTensorAccessors::
1901 StorageType<2, dim, OtherNumber>::base_tensor_type &sdata)
1902 {
1903 using result_type = typename SymmetricTensorAccessors::
1904 double_contraction_result<4, 2, dim, Number, OtherNumber>::type;
1905 using value_type = typename SymmetricTensorAccessors::
1906 double_contraction_result<4, 2, dim, Number, OtherNumber>::value_type;
1907
1908 const unsigned int data_dim = SymmetricTensorAccessors::
1909 StorageType<2, dim, value_type>::n_independent_components;
1910 value_type tmp[data_dim]{};
1911 for (unsigned int i = 0; i < data_dim; ++i)
1912 tmp[i] =
1913 perform_double_contraction<dim, Number, OtherNumber>(data[i], sdata);
1914 return result_type(tmp);
1915 }
1916
1917
1918
1923 template <int dim, typename Number, typename OtherNumber = Number>
1925 typename SymmetricTensorAccessors::StorageType<
1926 2,
1927 dim,
1928 typename SymmetricTensorAccessors::
1929 double_contraction_result<2, 4, dim, Number, OtherNumber>::value_type>::
1930 base_tensor_type
1931 perform_double_contraction(
1932 const typename SymmetricTensorAccessors::StorageType<2, dim, Number>::
1933 base_tensor_type &data,
1934 const typename SymmetricTensorAccessors::
1935 StorageType<4, dim, OtherNumber>::base_tensor_type &sdata)
1936 {
1937 using value_type = typename SymmetricTensorAccessors::
1938 double_contraction_result<2, 4, dim, Number, OtherNumber>::value_type;
1939 using base_tensor_type = typename SymmetricTensorAccessors::
1940 StorageType<2, dim, value_type>::base_tensor_type;
1941
1942 base_tensor_type tmp;
1943 for (unsigned int i = 0; i < tmp.dimension; ++i)
1944 {
1945 // Start with the non-diagonal part. These values appear
1946 // twice in the matrix, but are only stored once. So we can
1947 // get the double-contraction sum for these elements using
1948 // only one multiplication each, and at the end multiplying
1949 // things by 2.
1950 value_type sum = data[dim] * sdata[dim][i];
1951 for (unsigned int d = dim + 1; d < (dim * (dim + 1) / 2); ++d)
1952 sum += data[d] * sdata[d][i];
1953 sum += sum; // sum *= 2
1954
1955 // Now add the contributions from the diagonal
1956 for (unsigned int d = 0; d < dim; ++d)
1957 sum += data[d] * sdata[d][i];
1958 tmp[i] = sum;
1959 }
1960 return tmp;
1961 }
1962
1963
1964
1968 template <int dim, typename Number, typename OtherNumber = Number>
1970 typename SymmetricTensorAccessors::StorageType<
1971 4,
1972 dim,
1973 typename SymmetricTensorAccessors::
1974 double_contraction_result<4, 4, dim, Number, OtherNumber>::value_type>::
1975 base_tensor_type
1976 perform_double_contraction(
1977 const typename SymmetricTensorAccessors::StorageType<4, dim, Number>::
1978 base_tensor_type &data,
1979 const typename SymmetricTensorAccessors::
1980 StorageType<4, dim, OtherNumber>::base_tensor_type &sdata)
1981 {
1982 using value_type = typename SymmetricTensorAccessors::
1983 double_contraction_result<4, 4, dim, Number, OtherNumber>::value_type;
1984 using base_tensor_type = typename SymmetricTensorAccessors::
1985 StorageType<4, dim, value_type>::base_tensor_type;
1986
1987 const unsigned int data_dim = SymmetricTensorAccessors::
1988 StorageType<2, dim, value_type>::n_independent_components;
1989 base_tensor_type tmp;
1990 for (unsigned int i = 0; i < data_dim; ++i)
1991 for (unsigned int j = 0; j < data_dim; ++j)
1992 {
1993 // Start with the non-diagonal part
1994 for (unsigned int d = dim; d < (dim * (dim + 1) / 2); ++d)
1995 tmp[i][j] += data[i][d] * sdata[d][j];
1996 tmp[i][j] += tmp[i][j]; // tmp[i][j] *= 2;
1997
1998 // Now add the contributions from the diagonal
1999 for (unsigned int d = 0; d < dim; ++d)
2000 tmp[i][j] += data[i][d] * sdata[d][j];
2001 }
2002 return tmp;
2003 }
2004
2005} // end of namespace internal
2006
2007
2008
2009template <int rank_, int dim, typename Number>
2010template <typename OtherNumber>
2012 typename internal::SymmetricTensorAccessors::
2013 double_contraction_result<rank_, 2, dim, Number, OtherNumber>::type
2016{
2017 // Dispatch to functions that know the types of the involved
2018 // arguments via overloads.
2019 return internal::perform_double_contraction<dim, Number, OtherNumber>(data,
2020 s.data);
2021}
2022
2023
2024
2025template <int rank_, int dim, typename Number>
2026template <typename OtherNumber>
2028 typename internal::SymmetricTensorAccessors::
2029 double_contraction_result<rank_, 4, dim, Number, OtherNumber>::type
2032{
2033 typename internal::SymmetricTensorAccessors::
2034 double_contraction_result<rank_, 4, dim, Number, OtherNumber>::type tmp;
2035 tmp.data =
2036 internal::perform_double_contraction<dim, Number, OtherNumber>(data,
2037 s.data);
2038 return tmp;
2039}
2040
2041
2042
2043// internal namespace to switch between the
2044// access of different tensors. There used to
2045// be explicit instantiations before for
2046// different ranks and dimensions, but since
2047// we now allow for templates on the data
2048// type, and since we cannot partially
2049// specialize the implementation, this got
2050// into a separate namespace
2051namespace internal
2052{
2053 namespace SymmetricTensorImplementation
2054 {
2055 // a function to do the unrolling from a set of indices to a
2056 // scalar index into the array in which we store the elements of
2057 // a symmetric tensor
2058 //
2059 // this function is for rank-2 tensors
2060 template <int dim>
2062 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE unsigned int
2064 {
2065 AssertIndexRange(indices[0], dim);
2066 AssertIndexRange(indices[1], dim);
2067
2068 switch (dim)
2069 {
2070 case 1:
2071 {
2072 return 0;
2073 }
2074 case 2:
2075 {
2076 constexpr unsigned int table[2][2] = {{0, 2}, {2, 1}};
2077 return table[indices[0]][indices[1]];
2078 }
2079 case 3:
2080 {
2081 // Split the following table to avoid compiler warnings about
2082 // uninitialized values:
2083 // constexpr unsigned int table[3][3] = {{0, 3, 4},
2084 // {3, 1, 5},
2085 // {4, 5, 2}};
2086
2087 const unsigned int i = indices[0];
2088 const unsigned int j = indices[1];
2089 if (i == 0)
2090 {
2091 constexpr unsigned int table[3] = {0, 3, 4};
2092 return table[j];
2093 }
2094 else if (i == 1)
2095 {
2096 constexpr unsigned int table[3] = {3, 1, 5};
2097 return table[j];
2098 }
2099 else
2100 {
2101 constexpr unsigned int table[3] = {4, 5, 2};
2102 return table[j];
2103 }
2104 }
2105 case 4:
2106 {
2107 constexpr unsigned int table[4][4] = {{0, 4, 5, 6},
2108 {4, 1, 7, 8},
2109 {5, 7, 2, 9},
2110 {6, 8, 9, 3}};
2111 return table[indices[0]][indices[1]];
2112 }
2113 default:
2114 // for the remainder, manually figure out the numbering
2115 {
2116 if (indices[0] == indices[1])
2117 return indices[0];
2118
2119 const TableIndices<2> sorted_indices(
2120 std::min(indices[0], indices[1]),
2121 std::max(indices[0], indices[1]));
2122
2123 // Here (d, e) are the row and column of the symmetric matrix and
2124 // 'dim + c' is the index into the Tensor<1, dim> actually used
2125 // for storage.
2126 unsigned int c = 0;
2127 for (unsigned int d = 0; d < dim; ++d)
2128 for (unsigned int e = d + 1; e < dim; ++e, ++c)
2129 if ((sorted_indices[0] == d) && (sorted_indices[1] == e))
2130 return dim + c;
2131
2132 // should never get here:
2134 return 0;
2135 }
2136 }
2137 }
2138
2139 // a function to do the unrolling from a set of indices to a
2140 // scalar index into the array in which we store the elements of
2141 // a symmetric tensor
2142 //
2143 // this function is for tensors of ranks not already handled
2144 // above
2145 template <int dim, int rank_>
2146 DEAL_II_HOST_DEVICE DEAL_II_CONSTEXPR inline unsigned int
2148 {
2149 (void)indices;
2152 }
2153 } // namespace SymmetricTensorImplementation
2154
2155 template <int dim, typename Number>
2157 symmetric_tensor_access(const TableIndices<2> &indices,
2158 typename SymmetricTensorAccessors::
2159 StorageType<2, dim, Number>::base_tensor_type &data)
2160 {
2161 return data[SymmetricTensorImplementation::component_to_unrolled_index<dim>(
2162 indices)];
2163 }
2164
2165
2166
2167 template <int dim, typename Number>
2170 symmetric_tensor_access(
2171 const TableIndices<2> &indices,
2172 const typename SymmetricTensorAccessors::StorageType<2, dim, Number>::
2173 base_tensor_type &data)
2174 {
2175 return data[SymmetricTensorImplementation::component_to_unrolled_index<dim>(
2176 indices)];
2177 }
2178
2179
2180
2181 template <int dim, typename Number>
2183 symmetric_tensor_access(const TableIndices<4> &indices,
2184 typename SymmetricTensorAccessors::
2185 StorageType<4, dim, Number>::base_tensor_type &data)
2186 {
2187 switch (dim)
2188 {
2189 case 1:
2190 return data[0][0];
2191
2192 case 2:
2193 // each entry of the tensor can be thought of as an entry in a
2194 // matrix that maps the rolled-out rank-2 tensors into rolled-out
2195 // rank-2 tensors. this is the format in which we store rank-4
2196 // tensors. determine which position the present entry is
2197 // stored in
2198 {
2199 constexpr std::size_t base_index[2][2] = {{0, 2}, {2, 1}};
2200 return data[base_index[indices[0]][indices[1]]]
2201 [base_index[indices[2]][indices[3]]];
2202 }
2203 case 3:
2204 // each entry of the tensor can be thought of as an entry in a
2205 // matrix that maps the rolled-out rank-2 tensors into rolled-out
2206 // rank-2 tensors. this is the format in which we store rank-4
2207 // tensors. determine which position the present entry is
2208 // stored in
2209 {
2210 constexpr std::size_t base_index[3][3] = {{0, 3, 4},
2211 {3, 1, 5},
2212 {4, 5, 2}};
2213 return data[base_index[indices[0]][indices[1]]]
2214 [base_index[indices[2]][indices[3]]];
2215 }
2216
2217 default:
2219 }
2220
2221 // The code should never reach here.
2222 // We cannot return a static variable, as this class must support number
2223 // types that require no instances of the number type to be in scope during
2224 // a reinitialization procedure (e.g. ADOL-C adtl::adouble).
2225 return data[0][0];
2226 }
2227
2228
2229 template <int dim, typename Number>
2232 symmetric_tensor_access(
2233 const TableIndices<4> &indices,
2234 const typename SymmetricTensorAccessors::StorageType<4, dim, Number>::
2235 base_tensor_type &data)
2236 {
2237 switch (dim)
2238 {
2239 case 1:
2240 return data[0][0];
2241
2242 case 2:
2243 // each entry of the tensor can be thought of as an entry in a
2244 // matrix that maps the rolled-out rank-2 tensors into rolled-out
2245 // rank-2 tensors. this is the format in which we store rank-4
2246 // tensors. determine which position the present entry is
2247 // stored in
2248 {
2249 constexpr std::size_t base_index[2][2] = {{0, 2}, {2, 1}};
2250 return data[base_index[indices[0]][indices[1]]]
2251 [base_index[indices[2]][indices[3]]];
2252 }
2253 case 3:
2254 // each entry of the tensor can be thought of as an entry in a
2255 // matrix that maps the rolled-out rank-2 tensors into rolled-out
2256 // rank-2 tensors. this is the format in which we store rank-4
2257 // tensors. determine which position the present entry is
2258 // stored in
2259 {
2260 constexpr std::size_t base_index[3][3] = {{0, 3, 4},
2261 {3, 1, 5},
2262 {4, 5, 2}};
2263 return data[base_index[indices[0]][indices[1]]]
2264 [base_index[indices[2]][indices[3]]];
2265 }
2266
2267 default:
2269 }
2270
2271 // The code should never reach here.
2272 // We cannot return a static variable, as this class must support number
2273 // types that require no instances of the number type to be in scope during
2274 // a reinitialization procedure (e.g. ADOL-C adtl::adouble).
2275 return data[0][0];
2276 }
2277
2278} // end of namespace internal
2279
2280
2281
2282template <int rank_, int dim, typename Number>
2285 const TableIndices<rank_> &indices)
2286{
2287 for (unsigned int r = 0; r < rank; ++r)
2288 AssertIndexRange(indices[r], dim);
2289 return internal::symmetric_tensor_access<dim, Number>(indices, data);
2290}
2291
2292
2293
2294template <int rank_, int dim, typename Number>
2295DEAL_II_HOST_DEVICE constexpr inline DEAL_II_ALWAYS_INLINE const Number &
2297 const TableIndices<rank_> &indices) const
2298{
2299 for (unsigned int r = 0; r < rank; ++r)
2300 AssertIndexRange(indices[r], dim);
2301 return internal::symmetric_tensor_access<dim, Number>(indices, data);
2302}
2303
2304
2305
2306namespace internal
2307{
2308 namespace SymmetricTensorImplementation
2309 {
2310 template <int rank_>
2311 constexpr TableIndices<rank_>
2312 get_partially_filled_indices(const unsigned int row,
2313 const std::integral_constant<int, 2> &)
2314 {
2316 }
2317
2318
2319 template <int rank_>
2320 constexpr TableIndices<rank_>
2321 get_partially_filled_indices(const unsigned int row,
2322 const std::integral_constant<int, 4> &)
2323 {
2324 return TableIndices<rank_>(row,
2328 }
2329 } // namespace SymmetricTensorImplementation
2330} // namespace internal
2331
2332
2333template <int rank_, int dim, typename Number>
2334DEAL_II_HOST_DEVICE constexpr DEAL_II_ALWAYS_INLINE internal::
2335 SymmetricTensorAccessors::Accessor<rank_, dim, true, rank_ - 1, Number>
2336 SymmetricTensor<rank_, dim, Number>::operator[](const unsigned int row) const
2337{
2338 return internal::SymmetricTensorAccessors::
2339 Accessor<rank_, dim, true, rank_ - 1, Number>(
2340 *this,
2341 internal::SymmetricTensorImplementation::get_partially_filled_indices<
2342 rank_>(row, std::integral_constant<int, rank_>()));
2343}
2344
2345
2346
2347template <int rank_, int dim, typename Number>
2348DEAL_II_HOST_DEVICE constexpr inline DEAL_II_ALWAYS_INLINE internal::
2349 SymmetricTensorAccessors::Accessor<rank_, dim, false, rank_ - 1, Number>
2351{
2352 return internal::SymmetricTensorAccessors::
2353 Accessor<rank_, dim, false, rank_ - 1, Number>(
2354 *this,
2355 internal::SymmetricTensorImplementation::get_partially_filled_indices<
2356 rank_>(row, std::integral_constant<int, rank_>()));
2357}
2358
2359
2360
2361template <int rank_, int dim, typename Number>
2364 const TableIndices<rank_> &indices) const
2365{
2366 return operator()(indices);
2367}
2368
2369
2370
2371template <int rank_, int dim, typename Number>
2374 const TableIndices<rank_> &indices)
2375{
2376 return operator()(indices);
2377}
2378
2379
2380
2381template <int rank_, int dim, typename Number>
2382DEAL_II_HOST_DEVICE constexpr inline const Number &
2384 const unsigned int index) const
2385{
2386 // make nvcc happy:
2387 constexpr unsigned int my_n_independent_components = n_independent_components;
2388 AssertIndexRange(index, my_n_independent_components);
2389
2390 if constexpr (rank == 2)
2391 return data[index];
2392 else
2393 return data[decltype(data)::unrolled_to_component_indices(index)];
2394}
2395
2396
2397
2398template <int rank_, int dim, typename Number>
2399DEAL_II_HOST_DEVICE constexpr inline Number &
2401{
2402 // make nvcc happy:
2403 constexpr unsigned int my_n_independent_components = n_independent_components;
2404 AssertIndexRange(index, my_n_independent_components);
2405
2406 if constexpr (rank == 2)
2407 return data[index];
2408 else
2409 return data[decltype(data)::unrolled_to_component_indices(index)];
2410}
2411
2412
2413
2414namespace internal
2415{
2416 template <int dim, typename Number>
2417 constexpr inline typename numbers::NumberTraits<Number>::real_type
2418 compute_norm(const typename SymmetricTensorAccessors::
2419 StorageType<2, dim, Number>::base_tensor_type &data)
2420 {
2421 // Make things work with AD types
2422 using std::sqrt;
2423 switch (dim)
2424 {
2425 case 1:
2427
2428 case 2:
2432
2433 case 3:
2440
2441 default:
2442 {
2443 typename numbers::NumberTraits<Number>::real_type return_value =
2445
2446 for (unsigned int d = 0; d < dim; ++d)
2447 return_value +=
2449 for (unsigned int d = dim; d < (dim * dim + dim) / 2; ++d)
2450 return_value +=
2452
2453 return sqrt(return_value);
2454 }
2455 }
2456 }
2457
2458
2459
2460 template <int dim, typename Number>
2461 constexpr inline typename numbers::NumberTraits<Number>::real_type
2462 compute_norm(const typename SymmetricTensorAccessors::
2463 StorageType<4, dim, Number>::base_tensor_type &data)
2464 {
2465 // Make things work with AD types
2466 using std::sqrt;
2467 switch (dim)
2468 {
2469 case 1:
2471
2472 default:
2473 {
2474 typename numbers::NumberTraits<Number>::real_type return_value =
2476
2477 const unsigned int n_independent_components = data.dimension;
2478
2479 for (unsigned int i = 0; i < dim; ++i)
2480 for (unsigned int j = 0; j < dim; ++j)
2481 return_value +=
2483 for (unsigned int i = 0; i < dim; ++i)
2484 for (unsigned int j = dim; j < n_independent_components; ++j)
2485 return_value +=
2487 for (unsigned int i = dim; i < n_independent_components; ++i)
2488 for (unsigned int j = 0; j < dim; ++j)
2489 return_value +=
2491 for (unsigned int i = dim; i < n_independent_components; ++i)
2492 for (unsigned int j = dim; j < n_independent_components; ++j)
2493 return_value +=
2495
2496 return sqrt(return_value);
2497 }
2498 }
2499 }
2500
2501} // end of namespace internal
2502
2503
2504
2505template <int rank_, int dim, typename Number>
2508{
2509 return internal::compute_norm<dim, Number>(data);
2510}
2511
2512
2513
2514template <int rank_, int dim, typename Number>
2515DEAL_II_HOST_DEVICE constexpr unsigned int
2517 const TableIndices<rank_> &indices)
2518{
2519 return internal::SymmetricTensorImplementation::component_to_unrolled_index<
2520 dim>(indices);
2521}
2522
2523
2524
2525namespace internal
2526{
2527 namespace SymmetricTensorImplementation
2528 {
2529 // a function to do the inverse of the unrolling from a set of
2530 // indices to a scalar index into the array in which we store
2531 // the elements of a symmetric tensor. in other words, it goes
2532 // from the scalar index into the array to a set of indices of
2533 // the tensor
2534 //
2535 // this function is for rank-2 tensors
2536 template <int dim>
2537 constexpr inline DEAL_II_ALWAYS_INLINE TableIndices<2>
2538 unrolled_to_component_indices(const unsigned int i,
2539 const std::integral_constant<int, 2> &)
2540 {
2541 Assert(
2544 i,
2545 0,
2547 switch (dim)
2548 {
2549 case 1:
2550 {
2551 return {0, 0};
2552 }
2553
2554 case 2:
2555 {
2556 const TableIndices<2> table[3] = {TableIndices<2>(0, 0),
2557 TableIndices<2>(1, 1),
2558 TableIndices<2>(0, 1)};
2559 return table[i];
2560 }
2561
2562 case 3:
2563 {
2564 const TableIndices<2> table[6] = {TableIndices<2>(0, 0),
2565 TableIndices<2>(1, 1),
2566 TableIndices<2>(2, 2),
2567 TableIndices<2>(0, 1),
2568 TableIndices<2>(0, 2),
2569 TableIndices<2>(1, 2)};
2570 return table[i];
2571 }
2572
2573 default:
2574 if (i < dim)
2575 return {i, i};
2576
2577 for (unsigned int d = 0, c = dim; d < dim; ++d)
2578 for (unsigned int e = d + 1; e < dim; ++e, ++c)
2579 if (c == i)
2580 return {d, e};
2581
2582 // should never get here:
2584 return {0, 0};
2585 }
2586 }
2587
2588 // a function to do the inverse of the unrolling from a set of
2589 // indices to a scalar index into the array in which we store
2590 // the elements of a symmetric tensor. in other words, it goes
2591 // from the scalar index into the array to a set of indices of
2592 // the tensor
2593 //
2594 // this function is for tensors of a rank not already handled
2595 // above
2596 template <int dim, int rank_>
2597 constexpr inline std::enable_if_t<rank_ != 2, TableIndices<rank_>>
2598 unrolled_to_component_indices(const unsigned int i,
2599 const std::integral_constant<int, rank_> &)
2600 {
2601 (void)i;
2602 Assert(
2603 (i <
2605 ExcIndexRange(i,
2606 0,
2608 n_independent_components));
2610 return TableIndices<rank_>();
2611 }
2612
2613 } // namespace SymmetricTensorImplementation
2614} // namespace internal
2615
2616template <int rank_, int dim, typename Number>
2619 const unsigned int i)
2620{
2621 return internal::SymmetricTensorImplementation::unrolled_to_component_indices<
2622 dim>(i, std::integral_constant<int, rank_>());
2623}
2624
2625
2626
2627template <int rank_, int dim, typename Number>
2628template <class Archive>
2629inline void
2630SymmetricTensor<rank_, dim, Number>::serialize(Archive &ar, const unsigned int)
2631{
2632 ar & data;
2633}
2634
2635
2636#endif // DOXYGEN
2637
2638/* ----------------- Non-member functions operating on tensors. ------------ */
2639
2640
2653template <int rank_, int dim, typename Number, typename OtherNumber>
2664
2665
2678template <int rank_, int dim, typename Number, typename OtherNumber>
2689
2690
2698template <int rank_, int dim, typename Number, typename OtherNumber>
2703{
2704 return Tensor<rank_, dim, Number>(left) + right;
2705}
2706
2707
2715template <int rank_, int dim, typename Number, typename OtherNumber>
2720{
2721 return left + Tensor<rank_, dim, OtherNumber>(right);
2722}
2723
2724
2732template <int rank_, int dim, typename Number, typename OtherNumber>
2737{
2738 return Tensor<rank_, dim, Number>(left) - right;
2739}
2740
2741
2749template <int rank_, int dim, typename Number, typename OtherNumber>
2754{
2755 return left - Tensor<rank_, dim, OtherNumber>(right);
2756}
2757
2758
2759
2760template <int dim, typename Number>
2763{
2764 switch (dim)
2765 {
2766 case 1:
2767 return t.data[0];
2768 case 2:
2769 return (t.data[0] * t.data[1] - t.data[2] * t.data[2]);
2770 case 3:
2771 {
2772 // in analogy to general tensors, but
2773 // there's something to be simplified for
2774 // the present case
2775 const Number tmp = t.data[3] * t.data[4] * t.data[5];
2776 return (tmp + tmp + t.data[0] * t.data[1] * t.data[2] -
2777 t.data[0] * t.data[5] * t.data[5] -
2778 t.data[1] * t.data[4] * t.data[4] -
2779 t.data[2] * t.data[3] * t.data[3]);
2780 }
2781 default:
2784 }
2785}
2786
2787
2788
2800template <int dim, typename Number>
2806
2807
2808
2809template <int dim, typename Number>
2810DEAL_II_HOST_DEVICE constexpr inline DEAL_II_ALWAYS_INLINE Number
2812{
2813 Number t = d.data[0];
2814 for (unsigned int i = 1; i < dim; ++i)
2815 t += d.data[i];
2816 return t;
2817}
2818
2819
2831template <int dim, typename Number>
2834{
2835 return trace(t);
2836}
2837
2838
2850template <typename Number>
2856
2857
2858
2877template <typename Number>
2880{
2881 return t[0][0] * t[1][1] - t[0][1] * t[0][1];
2882}
2883
2884
2885
2894template <typename Number>
2897{
2898 return (t[0][0] * t[1][1] + t[1][1] * t[2][2] + t[2][2] * t[0][0] -
2899 t[0][1] * t[0][1] - t[0][2] * t[0][2] - t[1][2] * t[1][2]);
2900}
2901
2902
2903
2911template <typename Number>
2912std::array<Number, 1>
2914
2915
2916
2939template <typename Number>
2940std::array<Number, 2>
2942
2943
2944
2967template <typename Number>
2968std::array<Number, 3>
2970
2971
2972
2973namespace internal
2974{
2975 namespace SymmetricTensorImplementation
2976 {
2988 template <int dim, typename Number>
2989 void
2990 tridiagonalize(const ::SymmetricTensor<2, dim, Number> &A,
2992 std::array<Number, dim> &d,
2993 std::array<Number, dim - 1> &e);
2994
2995
2996
3010 template <int dim, typename Number>
3011 std::array<std::pair<Number, Tensor<1, dim, Number>>, dim>
3012 ql_implicit_shifts(const ::SymmetricTensor<2, dim, Number> &A);
3013
3014
3015
3029 template <int dim, typename Number>
3030 std::array<std::pair<Number, Tensor<1, dim, Number>>, dim>
3032
3033
3034
3048 template <typename Number>
3049 std::array<std::pair<Number, Tensor<1, 2, Number>>, 2>
3050 hybrid(const ::SymmetricTensor<2, 2, Number> &A);
3051
3052
3053
3068 template <typename Number>
3069 std::array<std::pair<Number, Tensor<1, 3, Number>>, 3>
3070 hybrid(const ::SymmetricTensor<2, 3, Number> &A);
3071
3076 template <int dim, typename Number>
3078 {
3079 using EigValsVecs = std::pair<Number, Tensor<1, dim, Number>>;
3080 bool
3081 operator()(const EigValsVecs &lhs, const EigValsVecs &rhs)
3082 {
3083 return lhs.first > rhs.first;
3084 }
3085 };
3086
3087 } // namespace SymmetricTensorImplementation
3088
3089} // namespace internal
3090
3091
3092
3093// The line below is to ensure that doxygen puts the full description
3094// of this global enumeration into the documentation
3095// See https://stackoverflow.com/a/1717984
3107{
3117 hybrid,
3135 jacobi
3136};
3137
3138
3139
3150template <int dim, typename Number>
3151std::array<std::pair<Number, Tensor<1, dim, Number>>, dim>
3155
3156
3157
3166template <int rank_, int dim, typename Number>
3170{
3171 return t;
3172}
3173
3174
3175
3176template <int dim, typename Number>
3180{
3182
3183 // subtract scaled trace from the diagonal
3184 const Number tr = trace(t) * internal::NumberType<Number>::value(1.0 / dim);
3185 for (unsigned int i = 0; i < dim; ++i)
3186 tmp.data[i] -= tr;
3187
3188 return tmp;
3189}
3190
3191
3192
3193template <int dim, typename Number>
3197{
3198 // create a default constructed matrix filled with
3199 // zeros, then set the diagonal elements to one
3201 switch (dim)
3202 {
3203 case 1:
3205 break;
3206 case 2:
3207 tmp.data[0] = tmp.data[1] = internal::NumberType<Number>::value(1.);
3208 break;
3209 case 3:
3210 tmp.data[0] = tmp.data[1] = tmp.data[2] =
3212 break;
3213 default:
3214 for (unsigned int d = 0; d < dim; ++d)
3216 }
3217 return tmp;
3218}
3219
3220
3221
3222template <int dim, typename Number>
3225{
3227
3228 // fill the elements treating the diagonal
3229 for (unsigned int i = 0; i < dim; ++i)
3230 for (unsigned int j = 0; j < dim; ++j)
3231 tmp.data[i][j] =
3232 internal::NumberType<Number>::value((i == j ? 1. : 0.) - 1. / dim);
3233
3234 // then fill the ones that copy over the
3235 // non-diagonal elements. note that during
3236 // the double-contraction, we handle the
3237 // off-diagonal elements twice, so simply
3238 // copying requires a weight of 1/2
3239 for (unsigned int i = dim;
3240 i < internal::SymmetricTensorAccessors::StorageType<4, dim, Number>::
3241 n_rank2_components;
3242 ++i)
3244
3245 return tmp;
3246}
3247
3248
3249
3250template <int dim, typename Number>
3254{
3256
3257 // fill the elements treating the diagonal
3258 for (unsigned int i = 0; i < dim; ++i)
3260
3261 // then fill the ones that copy over the
3262 // non-diagonal elements. note that during
3263 // the double-contraction, we handle the
3264 // off-diagonal elements twice, so simply
3265 // copying requires a weight of 1/2
3266 for (unsigned int i = dim;
3267 i < internal::SymmetricTensorAccessors::StorageType<4, dim, Number>::
3268 n_rank2_components;
3269 ++i)
3271
3272 return tmp;
3273}
3274
3275
3276
3286template <int dim, typename Number>
3294
3295
3296
3307template <int dim, typename Number>
3314
3315
3316
3338template <int dim, typename Number>
3342{
3344
3345 // fill only the elements really needed
3346 for (unsigned int i = 0; i < dim; ++i)
3347 for (unsigned int j = i; j < dim; ++j)
3348 for (unsigned int k = 0; k < dim; ++k)
3349 for (unsigned int l = k; l < dim; ++l)
3350 tmp[i][j][k][l] = t1[i][j] * t2[k][l];
3351
3352 return tmp;
3353}
3354
3380template <int dim, typename Number>
3381std::pair<SymmetricTensor<2, dim, Number>, SymmetricTensor<2, dim, Number>>
3383{
3384 Assert(dim <= 3, ExcNotImplemented());
3385
3386 const std::array<std::pair<Number, Tensor<1, dim, Number>>, dim>
3387 eigen_system = eigenvectors(original_tensor);
3388
3389 std::pair<SymmetricTensor<2, dim, Number>, SymmetricTensor<2, dim, Number>>
3390 positive_negative_tensors;
3391
3392 auto &[positive_part_tensor, negative_part_tensor] =
3393 positive_negative_tensors;
3394
3395 positive_part_tensor = 0;
3396 for (unsigned int i = 0; i < dim; ++i)
3397 if (eigen_system[i].first > 0)
3398 positive_part_tensor += eigen_system[i].first *
3399 symmetrize(outer_product(eigen_system[i].second,
3400 eigen_system[i].second));
3401
3402 negative_part_tensor = 0;
3403 for (unsigned int i = 0; i < dim; ++i)
3404 if (eigen_system[i].first < 0)
3405 negative_part_tensor += eigen_system[i].first *
3406 symmetrize(outer_product(eigen_system[i].second,
3407 eigen_system[i].second));
3408
3409 return positive_negative_tensors;
3410}
3411
3444template <int dim, typename Number>
3445std::tuple<SymmetricTensor<2, dim, Number>,
3450 const SymmetricTensor<2, dim, Number> &original_tensor)
3451{
3452 Assert(dim <= 3, ExcNotImplemented());
3453
3454 auto heaviside_function{[](const double x) {
3455 if (std::fabs(x) < 1.0e-16)
3456 return 0.5;
3457 if (x > 0)
3458 return 1.0;
3459 else
3460 return 0.0;
3461 }};
3462
3463 std::tuple<SymmetricTensor<2, dim, Number>,
3467 positive_negative_tensors_projectors;
3468
3469 auto &[positive_part_tensor,
3470 negative_part_tensor,
3471 positive_projector,
3472 negative_projector] = positive_negative_tensors_projectors;
3473
3474 const std::array<std::pair<Number, Tensor<1, dim, Number>>, dim>
3475 eigen_system = eigenvectors(original_tensor);
3476
3477 positive_part_tensor = 0;
3478 for (unsigned int i = 0; i < dim; ++i)
3479 if (eigen_system[i].first > 0)
3480 positive_part_tensor += eigen_system[i].first *
3481 symmetrize(outer_product(eigen_system[i].second,
3482 eigen_system[i].second));
3483
3484 negative_part_tensor = 0;
3485 for (unsigned int i = 0; i < dim; ++i)
3486 if (eigen_system[i].first < 0)
3487 negative_part_tensor += eigen_system[i].first *
3488 symmetrize(outer_product(eigen_system[i].second,
3489 eigen_system[i].second));
3490
3491 std::array<SymmetricTensor<2, dim, Number>, dim> M;
3492 for (unsigned int a = 0; a < dim; ++a)
3493 M[a] =
3494 symmetrize(outer_product(eigen_system[a].second, eigen_system[a].second));
3495
3496 std::array<SymmetricTensor<4, dim, Number>, dim> Q;
3497 for (unsigned int a = 0; a < dim; ++a)
3498 Q[a] = outer_product(M[a], M[a]);
3499
3500 std::array<std::array<SymmetricTensor<4, dim, Number>, dim>, dim> G;
3501 for (unsigned int a = 0; a < dim; ++a)
3502 for (unsigned int b = 0; b < dim; ++b)
3503 for (unsigned int i = 0; i < dim; ++i)
3504 for (unsigned int j = 0; j < dim; ++j)
3505 for (unsigned int k = 0; k < dim; ++k)
3506 for (unsigned int l = 0; l < dim; ++l)
3507 G[a][b][i][j][k][l] =
3508 M[a][i][k] * M[b][j][l] + M[a][i][l] * M[b][j][k];
3509
3510 // positive P
3511 positive_projector = 0;
3512 for (unsigned int a = 0; a < dim; ++a)
3513 {
3514 double lambda_a = eigen_system[a].first;
3515 positive_projector += heaviside_function(lambda_a) * Q[a];
3516 for (unsigned int b = 0; b < dim; ++b)
3517 {
3518 if (b != a)
3519 {
3520 double lambda_b = eigen_system[b].first;
3521
3522 double v_ab = 0.0;
3523 if (std::fabs(lambda_a - lambda_b) > 1.0e-12)
3524 v_ab = (std::fmax(lambda_a, 0.0) - std::fmax(lambda_b, 0.0)) /
3525 (lambda_a - lambda_b);
3526 else
3527 v_ab = 0.5 * (heaviside_function(lambda_a) +
3528 heaviside_function(lambda_b));
3529
3530 positive_projector += 0.5 * v_ab * 0.5 * (G[a][b] + G[b][a]);
3531 }
3532 }
3533 }
3534
3535 // negative P
3536 negative_projector = 0;
3537 for (unsigned int a = 0; a < dim; ++a)
3538 {
3539 double lambda_a = eigen_system[a].first;
3540 negative_projector += heaviside_function(-lambda_a) * Q[a];
3541 for (unsigned int b = 0; b < dim; ++b)
3542 {
3543 if (b != a)
3544 {
3545 double lambda_b = eigen_system[b].first;
3546
3547 double v_ab = 0.0;
3548 if (std::fabs(lambda_a - lambda_b) > 1.0e-12)
3549 v_ab = (std::fmin(lambda_a, 0.0) - std::fmin(lambda_b, 0.0)) /
3550 (lambda_a - lambda_b);
3551 else
3552 v_ab = 0.5 * (heaviside_function(-lambda_a) +
3553 heaviside_function(-lambda_b));
3554
3555 negative_projector += 0.5 * v_ab * 0.5 * (G[a][b] + G[b][a]);
3556 }
3557 }
3558 }
3559
3560 return positive_negative_tensors_projectors;
3561}
3562
3570template <int dim, typename Number>
3574{
3576 for (unsigned int d = 0; d < dim; ++d)
3577 result[d][d] = t[d][d];
3578
3579 const Number half = internal::NumberType<Number>::value(0.5);
3580 for (unsigned int d = 0; d < dim; ++d)
3581 for (unsigned int e = d + 1; e < dim; ++e)
3582 result[d][e] = (t[d][e] + t[e][d]) * half;
3583 return result;
3584}
3585
3586
3587
3599template <int dim, typename Number>
3602 symmetrize(const Tensor<4, dim, Number> &t, const bool major_symmetry)
3603{
3605
3606 const Number half = internal::NumberType<Number>::value(0.5);
3607
3608 // minor symmetry - A_{ijkl}=A_{jikl}=A_{ijlk}=A_{jilk}
3609 for (unsigned int i = 0; i < dim; ++i)
3610 for (unsigned int j = 0; j < dim; ++j)
3611 for (unsigned int k = 0; k < dim; ++k)
3612 for (unsigned int l = 0; l < dim; ++l)
3613 {
3614 if (i != j && k == l)
3615 {
3616 // A_{ijkk}=A_{jikk}
3617 result[i][j][k][k] = (t[i][j][k][k] + t[j][i][k][k]) * half;
3618 }
3619 else if (i == j && k != l)
3620 {
3621 // A_{iikl}=A_{iilk}
3622 result[i][i][k][l] = (t[i][i][k][l] + t[i][i][l][k]) * half;
3623 }
3624 else if (i != j && k != l)
3625 {
3626 // A_{ijkl}=A_{jilk}
3627 result[i][j][k][l] = (t[i][j][k][l] + t[j][i][k][l] +
3628 t[i][j][l][k] + t[j][i][l][k]) *
3629 half * half;
3630 }
3631 else
3632 {
3633 // A_{iijj} and A_{iiii} unchanged
3634 result[i][j][k][l] = t[i][j][k][l];
3635 }
3636 }
3637
3638 // in case major symmetry is also required
3639 if (major_symmetry)
3640 {
3641 // major symmetry - A_{ijkl}=A_{klij}
3642 for (unsigned int i = 0; i < dim; ++i)
3643 for (unsigned int j = i; j < dim; ++j)
3644 for (unsigned int k = 0; k < dim; ++k)
3645 for (unsigned int l = k; l < dim; ++l)
3646 result[i][j][k][l] = (t[i][j][k][l] + t[k][l][i][j]) * half;
3647 }
3648 return result;
3649}
3650
3651
3652
3660template <int rank_, int dim, typename Number>
3663 operator*(const SymmetricTensor<rank_, dim, Number> &t, const Number &factor)
3664{
3666 tt *= factor;
3667 return tt;
3668}
3669
3670
3671
3679template <int rank_, int dim, typename Number>
3682 operator*(const Number &factor, const SymmetricTensor<rank_, dim, Number> &t)
3683{
3684 // simply forward to the other operator
3685 return t * factor;
3686}
3687
3688
3689
3714template <int rank_, int dim, typename Number, typename OtherNumber>
3716 rank_,
3717 dim,
3718 typename ProductType<Number,
3719 typename EnableIfScalar<OtherNumber>::type>::type>
3721 const OtherNumber &factor)
3722{
3723 // form the product. we have to convert the two factors into the final
3724 // type via explicit casts because, for awkward reasons, the C++
3725 // standard committee saw it fit to not define an
3726 // operator*(float,std::complex<double>)
3727 // (as well as with switched arguments and double<->float).
3728 using product_type = typename ProductType<Number, OtherNumber>::type;
3731 return tt;
3732}
3733
3734
3735
3743template <int rank_, int dim, typename Number, typename OtherNumber>
3745 rank_,
3746 dim,
3747 typename ProductType<OtherNumber,
3748 typename EnableIfScalar<Number>::type>::type>
3749operator*(const Number &factor,
3751{
3752 // simply forward to the other operator with switched arguments
3753 return (t * factor);
3754}
3755
3756
3757
3763template <int rank_, int dim, typename Number, typename OtherNumber>
3764DEAL_II_HOST_DEVICE constexpr inline SymmetricTensor<
3765 rank_,
3766 dim,
3767 typename ProductType<Number,
3768 typename EnableIfScalar<OtherNumber>::type>::type>
3770 const OtherNumber &factor)
3771{
3772 using product_type = typename ProductType<Number, OtherNumber>::type;
3775 return tt;
3776}
3777
3778
3779
3786template <int rank_, int dim>
3789 operator*(const SymmetricTensor<rank_, dim> &t, const double factor)
3790{
3792 tt *= factor;
3793 return tt;
3794}
3795
3796
3797
3804template <int rank_, int dim>
3807 operator*(const double factor, const SymmetricTensor<rank_, dim> &t)
3808{
3810 tt *= factor;
3811 return tt;
3812}
3813
3814
3815
3821template <int rank_, int dim>
3823operator/(const SymmetricTensor<rank_, dim> &t, const double factor)
3824{
3826 tt /= factor;
3827 return tt;
3828}
3829
3839template <int dim, typename Number, typename OtherNumber>
3844{
3845 return (t1 * t2);
3846}
3847
3848
3861template <int dim, typename Number, typename OtherNumber>
3866{
3868 typename ProductType<Number, OtherNumber>::type>::value(0.0);
3869 for (unsigned int i = 0; i < dim; ++i)
3870 for (unsigned int j = 0; j < dim; ++j)
3871 s += t1[i][j] * t2[i][j];
3872 return s;
3873}
3874
3875
3888template <int dim, typename Number, typename OtherNumber>
3893{
3894 return scalar_product(t2, t1);
3895}
3896
3897
3912template <typename Number, typename OtherNumber>
3913DEAL_II_HOST_DEVICE constexpr inline DEAL_II_ALWAYS_INLINE void
3918{
3919 tmp[0][0] = t[0][0][0][0] * s[0][0];
3920}
3921
3922
3923
3938template <typename Number, typename OtherNumber>
3939DEAL_II_HOST_DEVICE constexpr inline void
3944{
3945 tmp[0][0] = t[0][0][0][0] * s[0][0];
3946}
3947
3948
3949
3964template <typename Number, typename OtherNumber>
3965DEAL_II_HOST_DEVICE constexpr inline void
3970{
3971 const unsigned int dim = 2;
3972
3973 for (unsigned int i = 0; i < dim; ++i)
3974 for (unsigned int j = i; j < dim; ++j)
3975 tmp[i][j] = t[i][j][0][0] * s[0][0] + t[i][j][1][1] * s[1][1] +
3976 2 * t[i][j][0][1] * s[0][1];
3977}
3978
3979
3980
3995template <typename Number, typename OtherNumber>
3996DEAL_II_HOST_DEVICE constexpr inline void
4001{
4002 const unsigned int dim = 2;
4003
4004 for (unsigned int i = 0; i < dim; ++i)
4005 for (unsigned int j = i; j < dim; ++j)
4006 tmp[i][j] = s[0][0] * t[0][0][i][j] * +s[1][1] * t[1][1][i][j] +
4007 2 * s[0][1] * t[0][1][i][j];
4008}
4009
4010
4011
4026template <typename Number, typename OtherNumber>
4027DEAL_II_HOST_DEVICE constexpr inline void
4032{
4033 const unsigned int dim = 3;
4034
4035 for (unsigned int i = 0; i < dim; ++i)
4036 for (unsigned int j = i; j < dim; ++j)
4037 tmp[i][j] = t[i][j][0][0] * s[0][0] + t[i][j][1][1] * s[1][1] +
4038 t[i][j][2][2] * s[2][2] + 2 * t[i][j][0][1] * s[0][1] +
4039 2 * t[i][j][0][2] * s[0][2] + 2 * t[i][j][1][2] * s[1][2];
4040}
4041
4042
4043
4058template <typename Number, typename OtherNumber>
4059DEAL_II_HOST_DEVICE constexpr inline void
4064{
4065 const unsigned int dim = 3;
4066
4067 for (unsigned int i = 0; i < dim; ++i)
4068 for (unsigned int j = i; j < dim; ++j)
4069 tmp[i][j] = s[0][0] * t[0][0][i][j] + s[1][1] * t[1][1][i][j] +
4070 s[2][2] * t[2][2][i][j] + 2 * s[0][1] * t[0][1][i][j] +
4071 2 * s[0][2] * t[0][2][i][j] + 2 * s[1][2] * t[1][2][i][j];
4072}
4073
4074
4075
4082template <int dim, typename Number, typename OtherNumber>
4083DEAL_II_HOST_DEVICE constexpr Tensor<
4084 1,
4085 dim,
4088 const Tensor<1, dim, OtherNumber> &src2)
4089{
4091 for (unsigned int i = 0; i < dim; ++i)
4092 {
4093 dest[i] = src1[i][0] * src2[0];
4094 for (unsigned int j = 1; j < dim; ++j)
4095 dest[i] += src1[i][j] * src2[j];
4096 }
4097 return dest;
4098}
4099
4100
4107template <int dim, typename Number, typename OtherNumber>
4108DEAL_II_HOST_DEVICE constexpr Tensor<
4109 1,
4110 dim,
4114{
4115 // this is easy for symmetric tensors:
4116 return src2 * src1;
4117}
4118
4119
4120
4140template <int rank_1,
4141 int rank_2,
4142 int dim,
4143 typename Number,
4144 typename OtherNumber>
4146 typename Tensor<rank_1 + rank_2 - 2,
4147 dim,
4148 typename ProductType<Number, OtherNumber>::type>::tensor_type
4154
4155
4156
4176template <int rank_1,
4177 int rank_2,
4178 int dim,
4179 typename Number,
4180 typename OtherNumber>
4182 typename Tensor<rank_1 + rank_2 - 2,
4183 dim,
4184 typename ProductType<Number, OtherNumber>::type>::tensor_type
4187{
4188 return Tensor<rank_1, dim, Number>(src1) * src2;
4189}
4190
4191
4192
4202template <int dim, typename Number>
4203inline std::ostream &
4204operator<<(std::ostream &out, const SymmetricTensor<2, dim, Number> &t)
4205{
4206 // make our lives a bit simpler by outputting
4207 // the tensor through the operator for the
4208 // general Tensor class
4210
4211 for (unsigned int i = 0; i < dim; ++i)
4212 for (unsigned int j = 0; j < dim; ++j)
4213 tt[i][j] = t[i][j];
4214
4215 return out << tt;
4216}
4217
4218
4219
4229template <int dim, typename Number>
4230inline std::ostream &
4231operator<<(std::ostream &out, const SymmetricTensor<4, dim, Number> &t)
4232{
4233 // make our lives a bit simpler by outputting
4234 // the tensor through the operator for the
4235 // general Tensor class
4237
4238 for (unsigned int i = 0; i < dim; ++i)
4239 for (unsigned int j = 0; j < dim; ++j)
4240 for (unsigned int k = 0; k < dim; ++k)
4241 for (unsigned int l = 0; l < dim; ++l)
4242 tt[i][j][k][l] = t[i][j][k][l];
4243
4244 return out << tt;
4245}
4246
4247
4249
4250#endif
*  *  Point< dim > operator()(const Point< dim > &p) const * 
constexpr SymmetricTensor< 2, dim, Number > deviator(const SymmetricTensor< 2, dim, Number > &)
constexpr bool operator==(const SymmetricTensor &) const
constexpr Number first_invariant(const SymmetricTensor< 2, dim, Number > &t)
constexpr void double_contract(SymmetricTensor< 2, 2, typename ProductType< Number, OtherNumber >::type > &tmp, const SymmetricTensor< 2, 2, Number > &s, const SymmetricTensor< 4, 2, OtherNumber > &t)
constexpr void double_contract(SymmetricTensor< 2, 1, typename ProductType< Number, OtherNumber >::type > &tmp, const SymmetricTensor< 4, 1, Number > &t, const SymmetricTensor< 2, 1, OtherNumber > &s)
constexpr ProductType< Number, OtherNumber >::type scalar_product(const Tensor< 2, dim, Number > &t1, const SymmetricTensor< 2, dim, OtherNumber > &t2)
std::pair< SymmetricTensor< 2, dim, Number >, SymmetricTensor< 2, dim, Number > > positive_negative_split(const SymmetricTensor< 2, dim, Number > &original_tensor)
constexpr SymmetricTensor< 4, dim, Number > invert(const SymmetricTensor< 4, dim, Number > &t)
constexpr SymmetricTensor< rank_, dim > operator*(const SymmetricTensor< rank_, dim > &t, const double factor)
constexpr Tensor< 1, dim, typename ProductType< Number, OtherNumber >::type > operator*(const SymmetricTensor< 2, dim, Number > &src1, const Tensor< 1, dim, OtherNumber > &src2)
constexpr SymmetricTensor< 2, dim, Number > symmetrize(const Tensor< 2, dim, Number > &t)
constexpr SymmetricTensor(const Number(&array)[n_independent_components])
constexpr Tensor< rank_, dim, typename ProductType< Number, OtherNumber >::type > operator-(const SymmetricTensor< rank_, dim, Number > &left, const Tensor< rank_, dim, OtherNumber > &right)
constexpr void double_contract(SymmetricTensor< 2, 2, typename ProductType< Number, OtherNumber >::type > &tmp, const SymmetricTensor< 4, 2, Number > &t, const SymmetricTensor< 2, 2, OtherNumber > &s)
static constexpr unsigned int component_to_unrolled_index(const TableIndices< rank_ > &indices)
constexpr Number determinant(const SymmetricTensor< 2, dim, Number > &)
constexpr SymmetricTensor< 2, dim, Number > invert(const SymmetricTensor< 2, dim, Number > &t)
constexpr Tensor< 1, dim, typename ProductType< Number, OtherNumber >::type > operator*(const Tensor< 1, dim, Number > &src1, const SymmetricTensor< 2, dim, OtherNumber > &src2)
std::array< Number, 2 > eigenvalues(const SymmetricTensor< 2, 2, Number > &T)
void serialize(Archive &ar, const unsigned int version)
constexpr Tensor< rank_, dim, typename ProductType< Number, OtherNumber >::type > operator-(const Tensor< rank_, dim, Number > &left, const SymmetricTensor< rank_, dim, OtherNumber > &right)
constexpr Number trace(const SymmetricTensor< 2, dim2, Number > &)
std::array< Number, 1 > eigenvalues(const SymmetricTensor< 2, 1, Number > &T)
std::tuple< SymmetricTensor< 2, dim, Number >, SymmetricTensor< 2, dim, Number >, SymmetricTensor< 4, dim, Number >, SymmetricTensor< 4, dim, Number > > positive_negative_projectors(const SymmetricTensor< 2, dim, Number > &original_tensor)
std::array< std::pair< Number, Tensor< 1, dim, Number > >, dim > eigenvectors(const SymmetricTensor< 2, dim, Number > &T, const SymmetricTensorEigenvectorMethod method=SymmetricTensorEigenvectorMethod::ql_implicit_shifts)
constexpr internal::SymmetricTensorAccessors::Accessor< rank_, dim, false, rank_ - 1, Number > operator[](const unsigned int row)
typename base_tensor_descriptor::base_tensor_type base_tensor_type
constexpr const Number & operator()(const TableIndices< rank_ > &indices) const
constexpr SymmetricTensor< 4, dim, Number > deviator_tensor()
constexpr bool operator!=(const SymmetricTensor &) const
constexpr Number & operator[](const TableIndices< rank_ > &indices)
constexpr void double_contract(SymmetricTensor< 2, 1, typename ProductType< Number, OtherNumber >::type > &tmp, const SymmetricTensor< 2, 1, Number > &s, const SymmetricTensor< 4, 1, OtherNumber > &t)
constexpr SymmetricTensor< rank_, dim, typename ProductType< OtherNumber, typename EnableIfScalar< Number >::type >::type > operator*(const Number &factor, const SymmetricTensor< rank_, dim, OtherNumber > &t)
constexpr SymmetricTensor< rank_, dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator/(const SymmetricTensor< rank_, dim, Number > &t, const OtherNumber &factor)
static constexpr std::size_t memory_consumption()
constexpr ProductType< Number, OtherNumber >::type scalar_product(const SymmetricTensor< 2, dim, Number > &t1, const Tensor< 2, dim, OtherNumber > &t2)
constexpr Tensor< rank_, dim, typename ProductType< Number, OtherNumber >::type > operator+(const SymmetricTensor< rank_, dim, Number > &left, const Tensor< rank_, dim, OtherNumber > &right)
base_tensor_type data
constexpr const Number & operator[](const TableIndices< rank_ > &indices) const
constexpr SymmetricTensor & operator=(const Number &d)
SymmetricTensor(const Tensor< 2, dim, OtherNumber > &t)
static constexpr TableIndices< rank_ > unrolled_to_component_indices(const unsigned int i)
constexpr SymmetricTensor< 4, dim, Number > outer_product(const SymmetricTensor< 2, dim, Number > &t1, const SymmetricTensor< 2, dim, Number > &t2)
constexpr Number & access_raw_entry(const unsigned int unrolled_index)
constexpr numbers::NumberTraits< Number >::real_type norm() const
constexpr SymmetricTensor operator-() const
constexpr internal::SymmetricTensorAccessors::double_contraction_result< rank_, 2, dim, Number, OtherNumber >::type operator*(const SymmetricTensor< 2, dim, OtherNumber > &s) const
constexpr SymmetricTensor()=default
constexpr Number second_invariant(const SymmetricTensor< 2, 1, Number > &)
constexpr Number third_invariant(const SymmetricTensor< 2, dim, Number > &t)
constexpr SymmetricTensor(const SymmetricTensor< rank_, dim, OtherNumber > &initializer)
constexpr SymmetricTensor & operator-=(const SymmetricTensor< rank_, dim, OtherNumber > &)
constexpr SymmetricTensor< rank_, dim, Number > operator*(const Number &factor, const SymmetricTensor< rank_, dim, Number > &t)
constexpr Tensor< rank_1+rank_2-2, dim, typenameProductType< Number, OtherNumber >::type >::tensor_type operator*(const Tensor< rank_1, dim, Number > &src1, const SymmetricTensor< rank_2, dim, OtherNumber > &src2)
constexpr SymmetricTensor< rank_, dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator*(const SymmetricTensor< rank_, dim, Number > &t, const OtherNumber &factor)
constexpr Tensor< rank_1+rank_2-2, dim, typenameProductType< Number, OtherNumber >::type >::tensor_type operator*(const SymmetricTensor< rank_1, dim, Number > &src1, const Tensor< rank_2, dim, OtherNumber > &src2)
constexpr ProductType< Number, OtherNumber >::type scalar_product(const SymmetricTensor< 2, dim, Number > &t1, const SymmetricTensor< 2, dim, OtherNumber > &t2)
constexpr SymmetricTensor< 4, dim, Number > identity_tensor()
constexpr void double_contract(SymmetricTensor< 2, 3, typename ProductType< Number, OtherNumber >::type > &tmp, const SymmetricTensor< 4, 3, Number > &t, const SymmetricTensor< 2, 3, OtherNumber > &s)
constexpr SymmetricTensor< rank_, dim, typename ProductType< Number, OtherNumber >::type > operator-(const SymmetricTensor< rank_, dim, Number > &left, const SymmetricTensor< rank_, dim, OtherNumber > &right)
constexpr SymmetricTensor< rank_, dim, typename ProductType< Number, OtherNumber >::type > operator+(const SymmetricTensor< rank_, dim, Number > &left, const SymmetricTensor< rank_, dim, OtherNumber > &right)
constexpr Number second_invariant(const SymmetricTensor< 2, 3, Number > &t)
constexpr SymmetricTensor< rank_, dim, Number > operator*(const SymmetricTensor< rank_, dim, Number > &t, const Number &factor)
constexpr const Number & access_raw_entry(const unsigned int unrolled_index) const
constexpr SymmetricTensor & operator=(const SymmetricTensor< rank_, dim, OtherNumber > &rhs)
constexpr SymmetricTensor< rank_, dim, Number > transpose(const SymmetricTensor< rank_, dim, Number > &t)
constexpr SymmetricTensor< 4, dim, Number > symmetrize(const Tensor< 4, dim, Number > &t, const bool major_symmetry)
constexpr SymmetricTensor< rank_, dim > operator*(const double factor, const SymmetricTensor< rank_, dim > &t)
constexpr SymmetricTensor & operator/=(const OtherNumber &factor)
constexpr void clear()
constexpr Tensor< rank_, dim, typename ProductType< Number, OtherNumber >::type > operator+(const Tensor< rank_, dim, Number > &left, const SymmetricTensor< rank_, dim, OtherNumber > &right)
constexpr Number second_invariant(const SymmetricTensor< 2, 2, Number > &t)
constexpr SymmetricTensor & operator+=(const SymmetricTensor< rank_, dim, OtherNumber > &)
constexpr SymmetricTensor< 2, dim, Number > unit_symmetric_tensor()
constexpr internal::SymmetricTensorAccessors::double_contraction_result< rank_, 4, dim, Number, OtherNumber >::type operator*(const SymmetricTensor< 4, dim, OtherNumber > &s) const
constexpr SymmetricTensor< rank_, dim > operator/(const SymmetricTensor< rank_, dim > &t, const double factor)
std::array< Number, 3 > eigenvalues(const SymmetricTensor< 2, 3, Number > &T)
constexpr internal::SymmetricTensorAccessors::Accessor< rank_, dim, true, rank_ - 1, Number > operator[](const unsigned int row) const
constexpr void double_contract(SymmetricTensor< 2, 3, typename ProductType< Number, OtherNumber >::type > &tmp, const SymmetricTensor< 2, 3, Number > &s, const SymmetricTensor< 4, 3, OtherNumber > &t)
constexpr SymmetricTensor & operator*=(const OtherNumber &factor)
constexpr Number & operator()(const TableIndices< rank_ > &indices)
typename AccessorTypes< rank, dim, constness, Number >::reference reference
constexpr Accessor(tensor_type &tensor, const TableIndices< rank > &previous_indices)
typename AccessorTypes< rank, dim, constness, Number >::tensor_type tensor_type
typename AccessorTypes< rank, dim, constness, Number >::tensor_type tensor_type
constexpr Accessor< rank, dim, constness, P - 1, Number > operator[](const unsigned int i)
constexpr Accessor(tensor_type &tensor, const TableIndices< rank > &previous_indices)
typename AccessorTypes< rank, dim, constness, Number >::reference reference
constexpr Accessor< rank, dim, constness, P - 1, Number > operator[](const unsigned int i) const
DEAL_II_HOST constexpr Accessor(const Accessor &)=default
#define DEAL_II_ALWAYS_INLINE
Definition config.h:166
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_HOST_DEVICE
Definition config.h:171
#define DEAL_II_CONSTEXPR
Definition config.h:282
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define DEAL_II_HOST
Definition config.h:182
std::ostream & operator<<(std::ostream &out, const DerivativeForm< order, dim, spacedim, Number > &df)
#define DEAL_II_ASSERT_UNREACHABLE()
#define DEAL_II_NOT_IMPLEMENTED()
Point< 2 > second
Definition grid_out.cc:4640
Point< 2 > first
Definition grid_out.cc:4639
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcIndexRange(std::size_t arg1, std::size_t arg2, std::size_t arg3)
static ::ExceptionBase & ExcMessage(std::string arg1)
std::vector< index_type > data
Definition mpi.cc:734
constexpr char N
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
Tensor< 2, dim, Number > l(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
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
T sum(const T &t, const MPI_Comm mpi_communicator)
constexpr TableIndices< 2 > merge(const TableIndices< 2 > &previous_indices, const unsigned int new_index, const unsigned int position)
void tridiagonalize(const ::SymmetricTensor< 2, dim, Number > &A, ::Tensor< 2, dim, Number > &Q, std::array< Number, dim > &d, std::array< Number, dim - 1 > &e)
constexpr unsigned int invalid_unsigned_int
Definition types.h:228
constexpr bool value_is_zero(const Number &value)
Definition numbers.h:874
STL namespace.
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sqrt(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:662
typename ProductType< Number, OtherNumber >::type value_type
std::pair< Number, Tensor< 1, dim, Number > > EigValsVecs
bool operator()(const EigValsVecs &lhs, const EigValsVecs &rhs)
static real_type abs(const number &x)
Definition numbers.h:560
constexpr SymmetricTensor< 2, dim, Number > deviator(const SymmetricTensor< 2, dim, Number > &)
constexpr SymmetricTensor< 2, dim, Number > symmetrize(const Tensor< 2, dim, Number > &t)
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 > &)
std::array< std::pair< Number, Tensor< 1, dim, Number > >, dim > eigenvectors(const SymmetricTensor< 2, dim, Number > &T, const SymmetricTensorEigenvectorMethod method=SymmetricTensorEigenvectorMethod::ql_implicit_shifts)
constexpr SymmetricTensor< 4, dim, Number > deviator_tensor()
constexpr SymmetricTensor< 4, dim, Number > outer_product(const SymmetricTensor< 2, dim, Number > &t1, const SymmetricTensor< 2, dim, Number > &t2)
constexpr SymmetricTensor< 4, dim, Number > identity_tensor()
SymmetricTensorEigenvectorMethod
constexpr SymmetricTensor< 2, dim, Number > unit_symmetric_tensor()