Reference documentation for deal.II version Git d9b14c2d54 2019-09-14 18:13:29 -0600
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
tensor.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2019 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_tensor_h
17 #define dealii_tensor_h
18 
19 #include <deal.II/base/config.h>
20 
21 #include <deal.II/base/exceptions.h>
22 #include <deal.II/base/numbers.h>
23 #include <deal.II/base/std_cxx14/utility.h>
24 #include <deal.II/base/table_indices.h>
25 #include <deal.II/base/template_constraints.h>
26 #include <deal.II/base/tensor_accessors.h>
27 #include <deal.II/base/utilities.h>
28 
29 #ifdef DEAL_II_WITH_ADOLC
30 # include <adolc/adouble.h> // Taped double
31 #endif
32 
33 #include <cmath>
34 #include <ostream>
35 #include <vector>
36 
37 
38 DEAL_II_NAMESPACE_OPEN
39 
40 // Forward declarations:
41 #ifndef DOXYGEN
42 template <int dim, typename Number>
43 class Point;
44 template <int rank_, int dim, typename Number = double>
45 class Tensor;
46 template <typename Number>
47 class Vector;
48 namespace Differentiation
49 {
50  namespace SD
51  {
52  class Expression;
53  }
54 } // namespace Differentiation
55 #endif
56 
57 #ifndef DOXYGEN
58 // Overload invalid tensor types of negative rank that come up during
59 // overload resolution of operator* and related contraction variants.
60 template <int dim, typename Number>
61 class Tensor<-2, dim, Number>
62 {};
63 
64 template <int dim, typename Number>
65 class Tensor<-1, dim, Number>
66 {};
67 #endif /* DOXYGEN */
68 
69 
100 template <int dim, typename Number>
101 class Tensor<0, dim, Number>
102 {
103 public:
112  static constexpr unsigned int dimension = dim;
113 
117  static constexpr unsigned int rank = 0;
118 
122  static constexpr unsigned int n_independent_components = 1;
123 
133 
138  using value_type = Number;
139 
145  using array_type = Number;
146 
152  constexpr DEAL_II_CUDA_HOST_DEV
153  Tensor();
154 
162  template <typename OtherNumber>
163  constexpr DEAL_II_CUDA_HOST_DEV
164  Tensor(const Tensor<0, dim, OtherNumber> &initializer);
165 
171  template <typename OtherNumber>
172  constexpr DEAL_II_CUDA_HOST_DEV
173  Tensor(const OtherNumber &initializer);
174 
178  Number *
179  begin_raw();
180 
184  const Number *
185  begin_raw() const;
186 
190  Number *
191  end_raw();
192 
197  const Number *
198  end_raw() const;
199 
209  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV operator Number &();
210 
219  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV operator const Number &() const;
220 
228  template <typename OtherNumber>
229  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV Tensor &
230  operator=(const Tensor<0, dim, OtherNumber> &rhs);
231 
232 #ifdef __INTEL_COMPILER
233 
241  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV Tensor &
242  operator=(const Tensor<0, dim, Number> &rhs);
243 #endif
244 
251  template <typename OtherNumber>
252  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV Tensor &
253  operator=(const OtherNumber &d);
254 
258  template <typename OtherNumber>
259  DEAL_II_CONSTEXPR bool
260  operator==(const Tensor<0, dim, OtherNumber> &rhs) const;
261 
265  template <typename OtherNumber>
266  constexpr bool
267  operator!=(const Tensor<0, dim, OtherNumber> &rhs) const;
268 
274  template <typename OtherNumber>
275  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV Tensor &
276  operator+=(const Tensor<0, dim, OtherNumber> &rhs);
277 
283  template <typename OtherNumber>
284  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV Tensor &
285  operator-=(const Tensor<0, dim, OtherNumber> &rhs);
286 
292  template <typename OtherNumber>
293  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV Tensor &
294  operator*=(const OtherNumber &factor);
295 
301  template <typename OtherNumber>
302  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV Tensor &
303  operator/=(const OtherNumber &factor);
304 
310  constexpr DEAL_II_CUDA_HOST_DEV Tensor
311  operator-() const;
312 
325  DEAL_II_CONSTEXPR void
326  clear();
327 
333  real_type
334  norm() const;
335 
342  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV real_type
343  norm_square() const;
344 
349  template <class Archive>
350  void
351  serialize(Archive &ar, const unsigned int version);
352 
357  using tensor_type = Number;
358 
359 private:
363  Number value;
364 
368  template <typename OtherNumber>
369  void
370  unroll_recursion(Vector<OtherNumber> &result,
371  unsigned int & start_index) const;
372 
373  // Allow an arbitrary Tensor to access the underlying values.
374  template <int, int, typename>
375  friend class Tensor;
376 };
377 
378 
379 
421 template <int rank_, int dim, typename Number>
422 class Tensor
423 {
424 public:
433  static constexpr unsigned int dimension = dim;
434 
438  static constexpr unsigned int rank = rank_;
439 
444  static constexpr unsigned int n_independent_components =
445  Tensor<rank_ - 1, dim>::n_independent_components * dim;
446 
452  using value_type = typename Tensor<rank_ - 1, dim, Number>::tensor_type;
453 
458  using array_type =
459  typename Tensor<rank_ - 1, dim, Number>::array_type[(dim != 0) ? dim : 1];
460 
466  constexpr DEAL_II_ALWAYS_INLINE DEAL_II_CUDA_HOST_DEV
467  Tensor() = default;
468 
474  constexpr DEAL_II_CUDA_HOST_DEV explicit Tensor(
475  const array_type &initializer);
476 
484  template <typename OtherNumber>
485  constexpr DEAL_II_CUDA_HOST_DEV
486  Tensor(const Tensor<rank_, dim, OtherNumber> &initializer);
487 
491  template <typename OtherNumber>
492  constexpr Tensor(
493  const Tensor<1, dim, Tensor<rank_ - 1, dim, OtherNumber>> &initializer);
494 
498  template <typename OtherNumber>
499  constexpr
500  operator Tensor<1, dim, Tensor<rank_ - 1, dim, OtherNumber>>() const;
501 
507  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV value_type &
508  operator[](const unsigned int i);
509 
515  constexpr DEAL_II_CUDA_HOST_DEV const value_type &
516  operator[](const unsigned int i) const;
517 
521  DEAL_II_CONSTEXPR const Number &
522  operator[](const TableIndices<rank_> &indices) const;
523 
527  DEAL_II_CONSTEXPR Number &operator[](const TableIndices<rank_> &indices);
528 
532  Number *
533  begin_raw();
534 
538  const Number *
539  begin_raw() const;
540 
544  Number *
545  end_raw();
546 
550  const Number *
551  end_raw() const;
552 
560  template <typename OtherNumber>
561  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV Tensor &
562  operator=(const Tensor<rank_, dim, OtherNumber> &rhs);
563 
570  DEAL_II_CONSTEXPR Tensor &
571  operator=(const Number &d);
572 
576  template <typename OtherNumber>
577  DEAL_II_CONSTEXPR bool
579 
583  template <typename OtherNumber>
584  constexpr bool
586 
592  template <typename OtherNumber>
593  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV Tensor &
594  operator+=(const Tensor<rank_, dim, OtherNumber> &);
595 
601  template <typename OtherNumber>
602  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV Tensor &
603  operator-=(const Tensor<rank_, dim, OtherNumber> &);
604 
611  template <typename OtherNumber>
612  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV Tensor &
613  operator*=(const OtherNumber &factor);
614 
620  template <typename OtherNumber>
621  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV Tensor &
622  operator/=(const OtherNumber &factor);
623 
629  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV Tensor
630  operator-() const;
631 
644  DEAL_II_CONSTEXPR void
645  clear();
646 
654  DEAL_II_CUDA_HOST_DEV
656  norm() const;
657 
664  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV
666  norm_square() const;
667 
675  template <typename OtherNumber>
676  void
677  unroll(Vector<OtherNumber> &result) const;
678 
683  static DEAL_II_CONSTEXPR unsigned int
684  component_to_unrolled_index(const TableIndices<rank_> &indices);
685 
690  static DEAL_II_CONSTEXPR TableIndices<rank_>
691  unrolled_to_component_indices(const unsigned int i);
692 
697  static constexpr std::size_t
698  memory_consumption();
699 
704  template <class Archive>
705  void
706  serialize(Archive &ar, const unsigned int version);
707 
713 
714 private:
718  Tensor<rank_ - 1, dim, Number> values[(dim != 0) ? dim : 1];
719  // ... avoid a compiler warning in case of dim == 0 and ensure that the
720  // array always has positive size.
721 
725  template <typename OtherNumber>
726  void
727  unroll_recursion(Vector<OtherNumber> &result,
728  unsigned int & start_index) const;
729 
736  template <typename ArrayLike, std::size_t... Indices>
737  constexpr DEAL_II_CUDA_HOST_DEV
738  Tensor(const ArrayLike &initializer, std_cxx14::index_sequence<Indices...>);
739 
740  // Allow an arbitrary Tensor to access the underlying values.
741  template <int, int, typename>
742  friend class Tensor;
743 
744  // Point is allowed access to the coordinates. This is supposed to improve
745  // speed.
746  friend class Point<dim, Number>;
747 };
748 
749 
750 namespace internal
751 {
756  template <int rank, int dim, typename T>
757  struct NumberType<Tensor<rank, dim, T>>
758  {
759  static constexpr DEAL_II_ALWAYS_INLINE const Tensor<rank, dim, T> &
760  value(const Tensor<rank, dim, T> &t)
761  {
762  return t;
763  }
764 
765  static DEAL_II_CONSTEXPR DEAL_II_ALWAYS_INLINE Tensor<rank, dim, T>
766  value(const T &t)
767  {
769  tmp = t;
770  return tmp;
771  }
772  };
773 } // namespace internal
774 
775 
776 /*---------------------- Inline functions: Tensor<0,dim> ---------------------*/
777 
778 
779 template <int dim, typename Number>
780 constexpr DEAL_II_ALWAYS_INLINE DEAL_II_CUDA_HOST_DEV
782  // Some auto-differentiable numbers need explicit
783  // zero initialization such as adtl::adouble.
784  : Tensor{0.0}
785 {}
786 
787 
788 
789 template <int dim, typename Number>
790 template <typename OtherNumber>
791 constexpr DEAL_II_ALWAYS_INLINE DEAL_II_CUDA_HOST_DEV
792  Tensor<0, dim, Number>::Tensor(const OtherNumber &initializer)
793  : value(internal::NumberType<Number>::value(initializer))
794 {}
795 
796 
797 
798 template <int dim, typename Number>
799 template <typename OtherNumber>
800 constexpr DEAL_II_ALWAYS_INLINE DEAL_II_CUDA_HOST_DEV
802  : Tensor{p.value}
803 {}
804 
805 
806 
807 template <int dim, typename Number>
808 inline Number *
810 {
811  return std::addressof(value);
812 }
813 
814 
815 
816 template <int dim, typename Number>
817 inline const Number *
819 {
820  return std::addressof(value);
821 }
822 
823 
824 
825 template <int dim, typename Number>
826 inline Number *
828 {
830 }
831 
832 
833 
834 template <int dim, typename Number>
835 const Number *
837 {
839 }
840 
841 
842 
843 template <int dim, typename Number>
844 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
845  DEAL_II_CUDA_HOST_DEV Tensor<0, dim, Number>::operator Number &()
846 {
847  // We cannot use Assert inside a CUDA kernel
848 #ifndef __CUDA_ARCH__
849  Assert(dim != 0,
850  ExcMessage("Cannot access an object of type Tensor<0,0,Number>"));
851 #endif
852  return value;
853 }
854 
855 
856 template <int dim, typename Number>
857 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
858  DEAL_II_CUDA_HOST_DEV Tensor<0, dim, Number>::operator const Number &() const
859 {
860  // We cannot use Assert inside a CUDA kernel
861 #ifndef __CUDA_ARCH__
862  Assert(dim != 0,
863  ExcMessage("Cannot access an object of type Tensor<0,0,Number>"));
864 #endif
865  return value;
866 }
867 
868 
869 template <int dim, typename Number>
870 template <typename OtherNumber>
871 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
872  DEAL_II_CUDA_HOST_DEV Tensor<0, dim, Number> &
874 {
875  value = internal::NumberType<Number>::value(p);
876  return *this;
877 }
878 
879 
880 #ifdef __INTEL_COMPILER
881 template <int dim, typename Number>
882 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
883  DEAL_II_CUDA_HOST_DEV Tensor<0, dim, Number> &
885 {
886  value = p.value;
887  return *this;
888 }
889 #endif
890 
891 
892 template <int dim, typename Number>
893 template <typename OtherNumber>
894 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
895  DEAL_II_CUDA_HOST_DEV Tensor<0, dim, Number> &
896  Tensor<0, dim, Number>::operator=(const OtherNumber &d)
897 {
898  value = internal::NumberType<Number>::value(d);
899  return *this;
900 }
901 
902 
903 template <int dim, typename Number>
904 template <typename OtherNumber>
905 DEAL_II_CONSTEXPR inline bool
907 {
908 #if defined(DEAL_II_ADOLC_WITH_ADVANCED_BRANCHING)
909  Assert(!(std::is_same<Number, adouble>::value ||
910  std::is_same<OtherNumber, adouble>::value),
911  ExcMessage(
912  "The Tensor equality operator for ADOL-C taped numbers has not yet "
913  "been extended to support advanced branching."));
914 #endif
915 
916  return numbers::values_are_equal(value, p.value);
917 }
918 
919 
920 template <int dim, typename Number>
921 template <typename OtherNumber>
922 constexpr bool
924 {
925  return !((*this) == p);
926 }
927 
928 
929 template <int dim, typename Number>
930 template <typename OtherNumber>
931 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
932  DEAL_II_CUDA_HOST_DEV Tensor<0, dim, Number> &
934 {
935  value += p.value;
936  return *this;
937 }
938 
939 
940 template <int dim, typename Number>
941 template <typename OtherNumber>
942 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
943  DEAL_II_CUDA_HOST_DEV Tensor<0, dim, Number> &
945 {
946  value -= p.value;
947  return *this;
948 }
949 
950 
951 
952 namespace internal
953 {
954  namespace ComplexWorkaround
955  {
956  template <typename Number, typename OtherNumber>
957  DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE DEAL_II_CUDA_HOST_DEV void
958  multiply_assign_scalar(Number &val, const OtherNumber &s)
959  {
960  val *= s;
961  }
962 
963 #ifdef __CUDA_ARCH__
964  template <typename Number, typename OtherNumber>
965  DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE DEAL_II_CUDA_HOST_DEV void
966  multiply_assign_scalar(std::complex<Number> &, const OtherNumber &)
967  {
968  printf("This function is not implemented for std::complex<Number>!\n");
969  assert(false);
970  }
971 #endif
972  } // namespace ComplexWorkaround
973 } // namespace internal
974 
975 
976 template <int dim, typename Number>
977 template <typename OtherNumber>
978 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
979  DEAL_II_CUDA_HOST_DEV Tensor<0, dim, Number> &
980  Tensor<0, dim, Number>::operator*=(const OtherNumber &s)
981 {
982  internal::ComplexWorkaround::multiply_assign_scalar(value, s);
983  return *this;
984 }
985 
986 
987 
988 template <int dim, typename Number>
989 template <typename OtherNumber>
990 DEAL_II_CONSTEXPR inline DEAL_II_CUDA_HOST_DEV Tensor<0, dim, Number> &
991 Tensor<0, dim, Number>::operator/=(const OtherNumber &s)
992 {
993  value /= s;
994  return *this;
995 }
996 
997 
998 template <int dim, typename Number>
999 constexpr DEAL_II_ALWAYS_INLINE DEAL_II_CUDA_HOST_DEV Tensor<0, dim, Number>
1001 {
1002  return -value;
1003 }
1004 
1005 
1006 template <int dim, typename Number>
1007 inline DEAL_II_ALWAYS_INLINE typename Tensor<0, dim, Number>::real_type
1009 {
1010  Assert(dim != 0,
1011  ExcMessage("Cannot access an object of type Tensor<0,0,Number>"));
1012  return numbers::NumberTraits<Number>::abs(value);
1013 }
1014 
1015 
1016 template <int dim, typename Number>
1017 DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV inline DEAL_II_ALWAYS_INLINE
1020 {
1021  // We cannot use Assert inside a CUDA kernel
1022 #ifndef __CUDA_ARCH__
1023  Assert(dim != 0,
1024  ExcMessage("Cannot access an object of type Tensor<0,0,Number>"));
1025 #endif
1027 }
1028 
1029 
1030 template <int dim, typename Number>
1031 template <typename OtherNumber>
1032 inline void
1033 Tensor<0, dim, Number>::unroll_recursion(Vector<OtherNumber> &result,
1034  unsigned int & index) const
1035 {
1036  Assert(dim != 0,
1037  ExcMessage("Cannot unroll an object of type Tensor<0,0,Number>"));
1038  result[index] = value;
1039  ++index;
1040 }
1041 
1042 
1043 template <int dim, typename Number>
1044 DEAL_II_CONSTEXPR inline void
1046 {
1047  // Some auto-differentiable numbers need explicit
1048  // zero initialization.
1049  value = internal::NumberType<Number>::value(0.0);
1050 }
1051 
1052 
1053 template <int dim, typename Number>
1054 template <class Archive>
1055 inline void
1056 Tensor<0, dim, Number>::serialize(Archive &ar, const unsigned int)
1057 {
1058  ar &value;
1059 }
1060 
1061 
1062 
1063 /*-------------------- Inline functions: Tensor<rank,dim> --------------------*/
1064 
1065 template <int rank_, int dim, typename Number>
1066 template <typename ArrayLike, std::size_t... indices>
1067 constexpr DEAL_II_ALWAYS_INLINE DEAL_II_CUDA_HOST_DEV
1068  Tensor<rank_, dim, Number>::Tensor(const ArrayLike &initializer,
1069  std_cxx14::index_sequence<indices...>)
1070  : values{Tensor<rank_ - 1, dim, Number>(initializer[indices])...}
1071 {
1072  static_assert(sizeof...(indices) == dim,
1073  "dim should match the number of indices");
1074 }
1075 
1076 
1077 template <int rank_, int dim, typename Number>
1078 constexpr DEAL_II_ALWAYS_INLINE DEAL_II_CUDA_HOST_DEV
1080  : Tensor(initializer, std_cxx14::make_index_sequence<dim>{})
1081 {}
1082 
1083 
1084 template <int rank_, int dim, typename Number>
1085 template <typename OtherNumber>
1086 constexpr DEAL_II_ALWAYS_INLINE DEAL_II_CUDA_HOST_DEV
1088  const Tensor<rank_, dim, OtherNumber> &initializer)
1089  : Tensor(initializer, std_cxx14::make_index_sequence<dim>{})
1090 {}
1091 
1092 
1093 template <int rank_, int dim, typename Number>
1094 template <typename OtherNumber>
1095 constexpr DEAL_II_ALWAYS_INLINE
1097  const Tensor<1, dim, Tensor<rank_ - 1, dim, OtherNumber>> &initializer)
1098  : Tensor(initializer, std_cxx14::make_index_sequence<dim>{})
1099 {}
1100 
1101 
1102 template <int rank_, int dim, typename Number>
1103 template <typename OtherNumber>
1104 constexpr DEAL_II_ALWAYS_INLINE Tensor<rank_, dim, Number>::
1105  operator Tensor<1, dim, Tensor<rank_ - 1, dim, OtherNumber>>() const
1106 {
1107  return Tensor<1, dim, Tensor<rank_ - 1, dim, Number>>(values);
1108 }
1109 
1110 
1111 
1112 namespace internal
1113 {
1114  namespace TensorSubscriptor
1115  {
1116  template <typename ArrayElementType, int dim>
1117  DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
1118  DEAL_II_CUDA_HOST_DEV ArrayElementType &
1119  subscript(ArrayElementType * values,
1120  const unsigned int i,
1121  std::integral_constant<int, dim>)
1122  {
1123  // We cannot use Assert in a CUDA kernel
1124 #ifndef __CUDA_ARCH__
1125  Assert(i < dim, ExcIndexRange(i, 0, dim));
1126 #endif
1127  return values[i];
1128  }
1129 
1130  // The variables within this struct will be referenced in the next function.
1131  // It is a workaround that allows returning a reference to a static variable
1132  // while allowing constexpr evaluation of the function.
1133  // It has to be defined outside the function because constexpr functions
1134  // cannot define static variables
1135  template <typename ArrayElementType>
1136  struct Uninitialized
1137  {
1138  static ArrayElementType value;
1139  };
1140 
1141  template <typename ArrayElementType>
1142  DEAL_II_CONSTEXPR inline ArrayElementType &
1143  subscript(ArrayElementType *,
1144  const unsigned int,
1145  std::integral_constant<int, 0>)
1146  {
1147  Assert(
1148  false,
1149  ExcMessage(
1150  "Cannot access elements of an object of type Tensor<rank,0,Number>."));
1151  return Uninitialized<ArrayElementType>::value;
1152  }
1153  } // namespace TensorSubscriptor
1154 } // namespace internal
1155 
1156 
1157 template <int rank_, int dim, typename Number>
1158 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE DEAL_II_CUDA_HOST_DEV //
1160  operator[](const unsigned int i)
1161 {
1162  return ::internal::TensorSubscriptor::subscript(
1163  values, i, std::integral_constant<int, dim>());
1164 }
1165 
1166 
1167 template <int rank_, int dim, typename Number>
1168 constexpr DEAL_II_ALWAYS_INLINE
1169  DEAL_II_CUDA_HOST_DEV const typename Tensor<rank_, dim, Number>::value_type &
1170  Tensor<rank_, dim, Number>::operator[](const unsigned int i) const
1171 {
1172  return values[i];
1173 }
1174 
1175 
1176 template <int rank_, int dim, typename Number>
1177 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE const Number &
1179  operator[](const TableIndices<rank_> &indices) const
1180 {
1181  Assert(dim != 0,
1182  ExcMessage("Cannot access an object of type Tensor<rank_,0,Number>"));
1183 
1184  return TensorAccessors::extract<rank_>(*this, indices);
1185 }
1186 
1187 
1188 
1189 template <int rank_, int dim, typename Number>
1190 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE Number &
1192 {
1193  Assert(dim != 0,
1194  ExcMessage("Cannot access an object of type Tensor<rank_,0,Number>"));
1195 
1196  return TensorAccessors::extract<rank_>(*this, indices);
1197 }
1198 
1199 
1200 
1201 template <int rank_, int dim, typename Number>
1202 inline Number *
1204 {
1205  return std::addressof(
1206  this->operator[](this->unrolled_to_component_indices(0)));
1207 }
1208 
1209 
1210 
1211 template <int rank_, int dim, typename Number>
1212 inline const Number *
1214 {
1215  return std::addressof(
1216  this->operator[](this->unrolled_to_component_indices(0)));
1217 }
1218 
1219 
1220 
1221 template <int rank_, int dim, typename Number>
1222 inline Number *
1224 {
1226 }
1227 
1228 
1229 
1230 template <int rank_, int dim, typename Number>
1231 inline const Number *
1233 {
1235 }
1236 
1237 
1238 
1239 template <int rank_, int dim, typename Number>
1240 template <typename OtherNumber>
1241 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE Tensor<rank_, dim, Number> &
1243 {
1244  // The following loop could be written more concisely using std::copy, but
1245  // that function is only constexpr from C++20 on.
1246  for (unsigned int i = 0; i < dim; ++i)
1247  values[i] = t.values[i];
1248  return *this;
1249 }
1250 
1251 
1252 template <int rank_, int dim, typename Number>
1253 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE Tensor<rank_, dim, Number> &
1255 {
1257  ExcMessage("Only assignment with zero is allowed"));
1258  (void)d;
1259 
1260  for (unsigned int i = 0; i < dim; ++i)
1261  values[i] = internal::NumberType<Number>::value(0.0);
1262  return *this;
1263 }
1264 
1265 
1266 template <int rank_, int dim, typename Number>
1267 template <typename OtherNumber>
1268 DEAL_II_CONSTEXPR inline bool
1271 {
1272  for (unsigned int i = 0; i < dim; ++i)
1273  if (values[i] != p.values[i])
1274  return false;
1275  return true;
1276 }
1277 
1278 
1279 // At some places in the library, we have Point<0> for formal reasons
1280 // (e.g., we sometimes have Quadrature<dim-1> for faces, so we have
1281 // Quadrature<0> for dim=1, and then we have Point<0>). To avoid warnings
1282 // in the above function that the loop end check always fails, we
1283 // implement this function here
1284 template <>
1285 template <>
1286 DEAL_II_CONSTEXPR inline bool
1288 {
1289  return true;
1290 }
1291 
1292 
1293 template <int rank_, int dim, typename Number>
1294 template <typename OtherNumber>
1295 constexpr bool
1298 {
1299  return !((*this) == p);
1300 }
1301 
1302 
1303 template <int rank_, int dim, typename Number>
1304 template <typename OtherNumber>
1305 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
1306  DEAL_II_CUDA_HOST_DEV Tensor<rank_, dim, Number> &
1309 {
1310  for (unsigned int i = 0; i < dim; ++i)
1311  values[i] += p.values[i];
1312  return *this;
1313 }
1314 
1315 
1316 template <int rank_, int dim, typename Number>
1317 template <typename OtherNumber>
1318 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
1319  DEAL_II_CUDA_HOST_DEV Tensor<rank_, dim, Number> &
1322 {
1323  for (unsigned int i = 0; i < dim; ++i)
1324  values[i] -= p.values[i];
1325  return *this;
1326 }
1327 
1328 
1329 template <int rank_, int dim, typename Number>
1330 template <typename OtherNumber>
1331 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
1332  DEAL_II_CUDA_HOST_DEV Tensor<rank_, dim, Number> &
1333  Tensor<rank_, dim, Number>::operator*=(const OtherNumber &s)
1334 {
1335  for (unsigned int i = 0; i < dim; ++i)
1336  values[i] *= s;
1337  return *this;
1338 }
1339 
1340 
1341 namespace internal
1342 {
1343  namespace TensorImplementation
1344  {
1345  template <int rank,
1346  int dim,
1347  typename Number,
1348  typename OtherNumber,
1349  typename std::enable_if<
1350  !std::is_integral<
1351  typename ProductType<Number, OtherNumber>::type>::value &&
1352  !std::is_same<Number, Differentiation::SD::Expression>::value,
1353  int>::type = 0>
1354  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV inline DEAL_II_ALWAYS_INLINE void
1355  division_operator(Tensor<rank, dim, Number> (&t)[dim],
1356  const OtherNumber &factor)
1357  {
1358  const Number inverse_factor = Number(1.) / factor;
1359  // recurse over the base objects
1360  for (unsigned int d = 0; d < dim; ++d)
1361  t[d] *= inverse_factor;
1362  }
1363 
1364 
1365  template <int rank,
1366  int dim,
1367  typename Number,
1368  typename OtherNumber,
1369  typename std::enable_if<
1370  std::is_integral<
1371  typename ProductType<Number, OtherNumber>::type>::value ||
1372  std::is_same<Number, Differentiation::SD::Expression>::value,
1373  int>::type = 0>
1374  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV inline DEAL_II_ALWAYS_INLINE void
1375  division_operator(::Tensor<rank, dim, Number> (&t)[dim],
1376  const OtherNumber &factor)
1377  {
1378  // recurse over the base objects
1379  for (unsigned int d = 0; d < dim; ++d)
1380  t[d] /= factor;
1381  }
1382  } // namespace TensorImplementation
1383 } // namespace internal
1384 
1385 
1386 template <int rank_, int dim, typename Number>
1387 template <typename OtherNumber>
1388 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
1389  DEAL_II_CUDA_HOST_DEV Tensor<rank_, dim, Number> &
1390  Tensor<rank_, dim, Number>::operator/=(const OtherNumber &s)
1391 {
1392  internal::TensorImplementation::division_operator(values, s);
1393  return *this;
1394 }
1395 
1396 
1397 template <int rank_, int dim, typename Number>
1398 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
1399  DEAL_II_CUDA_HOST_DEV Tensor<rank_, dim, Number>
1401 {
1403 
1404  for (unsigned int i = 0; i < dim; ++i)
1405  tmp.values[i] = -values[i];
1406 
1407  return tmp;
1408 }
1409 
1410 
1411 template <int rank_, int dim, typename Number>
1414 {
1415  return std::sqrt(norm_square());
1416 }
1417 
1418 
1419 template <int rank_, int dim, typename Number>
1420 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE DEAL_II_CUDA_HOST_DEV
1423 {
1424  typename numbers::NumberTraits<Number>::real_type s = internal::NumberType<
1425  typename numbers::NumberTraits<Number>::real_type>::value(0.0);
1426  for (unsigned int i = 0; i < dim; ++i)
1427  s += values[i].norm_square();
1428 
1429  return s;
1430 }
1431 
1432 
1433 template <int rank_, int dim, typename Number>
1434 template <typename OtherNumber>
1435 inline void
1436 Tensor<rank_, dim, Number>::unroll(Vector<OtherNumber> &result) const
1437 {
1438  AssertDimension(result.size(),
1439  (Utilities::fixed_power<rank_, unsigned int>(dim)));
1440 
1441  unsigned int index = 0;
1442  unroll_recursion(result, index);
1443 }
1444 
1445 
1446 template <int rank_, int dim, typename Number>
1447 template <typename OtherNumber>
1448 inline void
1450  unsigned int & index) const
1451 {
1452  for (unsigned int i = 0; i < dim; ++i)
1453  values[i].unroll_recursion(result, index);
1454 }
1455 
1456 
1457 template <int rank_, int dim, typename Number>
1458 DEAL_II_CONSTEXPR inline unsigned int
1460  const TableIndices<rank_> &indices)
1461 {
1462  unsigned int index = 0;
1463  for (int r = 0; r < rank_; ++r)
1464  index = index * dim + indices[r];
1465 
1466  return index;
1467 }
1468 
1469 
1470 
1471 namespace internal
1472 {
1473  // unrolled_to_component_indices is instantiated from DataOut for dim==0
1474  // and rank=2. Make sure we don't have compiler warnings.
1475 
1476  template <int dim>
1477  inline DEAL_II_CONSTEXPR unsigned int
1478  mod(const unsigned int x)
1479  {
1480  return x % dim;
1481  }
1482 
1483  template <>
1484  inline unsigned int
1485  mod<0>(const unsigned int x)
1486  {
1487  Assert(false, ExcInternalError());
1488  return x;
1489  }
1490 
1491  template <int dim>
1492  inline DEAL_II_CONSTEXPR unsigned int
1493  div(const unsigned int x)
1494  {
1495  return x / dim;
1496  }
1497 
1498  template <>
1499  inline unsigned int
1500  div<0>(const unsigned int x)
1501  {
1502  Assert(false, ExcInternalError());
1503  return x;
1504  }
1505 
1506 } // namespace internal
1507 
1508 
1509 
1510 template <int rank_, int dim, typename Number>
1511 DEAL_II_CONSTEXPR inline TableIndices<rank_>
1513 {
1516 
1517  TableIndices<rank_> indices;
1518 
1519  unsigned int remainder = i;
1520  for (int r = rank_ - 1; r >= 0; --r)
1521  {
1522  indices[r] = internal::mod<dim>(remainder);
1523  remainder = internal::div<dim>(remainder);
1524  }
1525  Assert(remainder == 0, ExcInternalError());
1526 
1527  return indices;
1528 }
1529 
1530 
1531 template <int rank_, int dim, typename Number>
1532 DEAL_II_CONSTEXPR inline void
1534 {
1535  for (unsigned int i = 0; i < dim; ++i)
1536  values[i] = internal::NumberType<Number>::value(0.0);
1537 }
1538 
1539 
1540 template <int rank_, int dim, typename Number>
1541 constexpr std::size_t
1543 {
1544  return sizeof(Tensor<rank_, dim, Number>);
1545 }
1546 
1547 
1548 template <int rank_, int dim, typename Number>
1549 template <class Archive>
1550 inline void
1551 Tensor<rank_, dim, Number>::serialize(Archive &ar, const unsigned int)
1552 {
1553  ar &values;
1554 }
1555 
1556 
1557 /* ----------------- Non-member functions operating on tensors. ------------ */
1558 
1563 
1571 template <int rank_, int dim, typename Number>
1572 inline std::ostream &
1573 operator<<(std::ostream &out, const Tensor<rank_, dim, Number> &p)
1574 {
1575  for (unsigned int i = 0; i < dim; ++i)
1576  {
1577  out << p[i];
1578  if (i != dim - 1)
1579  out << ' ';
1580  }
1581 
1582  return out;
1583 }
1584 
1585 
1592 template <int dim, typename Number>
1593 inline std::ostream &
1594 operator<<(std::ostream &out, const Tensor<0, dim, Number> &p)
1595 {
1596  out << static_cast<const Number &>(p);
1597  return out;
1598 }
1599 
1600 
1602 
1606 
1607 
1618 template <int dim, typename Number, typename Other>
1619 DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV inline DEAL_II_ALWAYS_INLINE
1620  typename ProductType<Other, Number>::type
1621  operator*(const Other &object, const Tensor<0, dim, Number> &t)
1622 {
1623  return object * static_cast<const Number &>(t);
1624 }
1625 
1626 
1627 
1638 template <int dim, typename Number, typename Other>
1639 DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV inline DEAL_II_ALWAYS_INLINE
1640  typename ProductType<Number, Other>::type
1641  operator*(const Tensor<0, dim, Number> &t, const Other &object)
1642 {
1643  return static_cast<const Number &>(t) * object;
1644 }
1645 
1646 
1658 template <int dim, typename Number, typename OtherNumber>
1659 DEAL_II_CUDA_HOST_DEV constexpr DEAL_II_ALWAYS_INLINE
1660  typename ProductType<Number, OtherNumber>::type
1662  const Tensor<0, dim, OtherNumber> &src2)
1663 {
1664  return static_cast<const Number &>(src1) *
1665  static_cast<const OtherNumber &>(src2);
1666 }
1667 
1668 
1676 template <int dim, typename Number, typename OtherNumber>
1677 DEAL_II_CUDA_HOST_DEV constexpr DEAL_II_ALWAYS_INLINE
1678  Tensor<0,
1679  dim,
1680  typename ProductType<Number,
1681  typename EnableIfScalar<OtherNumber>::type>::type>
1682  operator/(const Tensor<0, dim, Number> &t, const OtherNumber &factor)
1683 {
1684  return static_cast<const Number &>(t) / factor;
1685 }
1686 
1687 
1695 template <int dim, typename Number, typename OtherNumber>
1696 constexpr DEAL_II_ALWAYS_INLINE DEAL_II_CUDA_HOST_DEV
1699  const Tensor<0, dim, OtherNumber> &q)
1700 {
1701  return static_cast<const Number &>(p) + static_cast<const OtherNumber &>(q);
1702 }
1703 
1704 
1712 template <int dim, typename Number, typename OtherNumber>
1713 constexpr DEAL_II_ALWAYS_INLINE DEAL_II_CUDA_HOST_DEV
1716  const Tensor<0, dim, OtherNumber> &q)
1717 {
1718  return static_cast<const Number &>(p) - static_cast<const OtherNumber &>(q);
1719 }
1720 
1721 
1734 template <int rank, int dim, typename Number, typename OtherNumber>
1735 DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV inline DEAL_II_ALWAYS_INLINE
1736  Tensor<rank,
1737  dim,
1738  typename ProductType<Number,
1739  typename EnableIfScalar<OtherNumber>::type>::type>
1740  operator*(const Tensor<rank, dim, Number> &t, const OtherNumber &factor)
1741 {
1742  // recurse over the base objects
1744  for (unsigned int d = 0; d < dim; ++d)
1745  tt[d] = t[d] * factor;
1746  return tt;
1747 }
1748 
1749 
1762 template <int rank, int dim, typename Number, typename OtherNumber>
1763 DEAL_II_CUDA_HOST_DEV DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
1764  Tensor<rank,
1765  dim,
1767  OtherNumber>::type>
1768  operator*(const Number &factor, const Tensor<rank, dim, OtherNumber> &t)
1769 {
1770  // simply forward to the operator above
1771  return t * factor;
1772 }
1773 
1774 
1775 namespace internal
1776 {
1777  namespace TensorImplementation
1778  {
1779  template <int rank,
1780  int dim,
1781  typename Number,
1782  typename OtherNumber,
1783  typename std::enable_if<
1784  !std::is_integral<
1785  typename ProductType<Number, OtherNumber>::type>::value,
1786  int>::type = 0>
1787  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV inline DEAL_II_ALWAYS_INLINE
1789  division_operator(const Tensor<rank, dim, Number> &t,
1790  const OtherNumber & factor)
1791  {
1793  const Number inverse_factor = Number(1.) / factor;
1794  // recurse over the base objects
1795  for (unsigned int d = 0; d < dim; ++d)
1796  tt[d] = t[d] * inverse_factor;
1797  return tt;
1798  }
1799 
1800 
1801  template <int rank,
1802  int dim,
1803  typename Number,
1804  typename OtherNumber,
1805  typename std::enable_if<
1806  std::is_integral<
1807  typename ProductType<Number, OtherNumber>::type>::value,
1808  int>::type = 0>
1809  DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV inline DEAL_II_ALWAYS_INLINE
1811  division_operator(const Tensor<rank, dim, Number> &t,
1812  const OtherNumber & factor)
1813  {
1815  // recurse over the base objects
1816  for (unsigned int d = 0; d < dim; ++d)
1817  tt[d] = t[d] / factor;
1818  return tt;
1819  }
1820  } // namespace TensorImplementation
1821 } // namespace internal
1822 
1823 
1833 template <int rank, int dim, typename Number, typename OtherNumber>
1834 DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV inline DEAL_II_ALWAYS_INLINE
1835  Tensor<rank,
1836  dim,
1837  typename ProductType<Number,
1838  typename EnableIfScalar<OtherNumber>::type>::type>
1839  operator/(const Tensor<rank, dim, Number> &t, const OtherNumber &factor)
1840 {
1841  return internal::TensorImplementation::division_operator(t, factor);
1842 }
1843 
1844 
1854 template <int rank, int dim, typename Number, typename OtherNumber>
1855 DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV inline DEAL_II_ALWAYS_INLINE
1859 {
1861 
1862  for (unsigned int i = 0; i < dim; ++i)
1863  tmp[i] += q[i];
1864 
1865  return tmp;
1866 }
1867 
1868 
1878 template <int rank, int dim, typename Number, typename OtherNumber>
1879 DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV inline DEAL_II_ALWAYS_INLINE
1883 {
1885 
1886  for (unsigned int i = 0; i < dim; ++i)
1887  tmp[i] -= q[i];
1888 
1889  return tmp;
1890 }
1891 
1898 template <int dim, typename Number, typename OtherNumber>
1899 inline DEAL_II_CONSTEXPR DEAL_II_ALWAYS_INLINE
1902  const Tensor<0, dim, OtherNumber> &src2)
1903 {
1905 
1906  tmp *= src2;
1907 
1908  return tmp;
1909 }
1910 
1916 template <int dim, typename Number, typename OtherNumber>
1917 inline DEAL_II_CONSTEXPR DEAL_II_ALWAYS_INLINE
1920  const Tensor<1, dim, OtherNumber> &src2)
1921 {
1923 
1924  for (unsigned int i = 0; i < dim; ++i)
1925  tmp[i] *= src2[i];
1926 
1927  return tmp;
1928 }
1929 
1946 template <int rank, int dim, typename Number, typename OtherNumber>
1947 inline DEAL_II_CONSTEXPR DEAL_II_ALWAYS_INLINE
1950  const Tensor<rank, dim, OtherNumber> &src2)
1951 {
1953 
1954  for (unsigned int i = 0; i < dim; ++i)
1955  tmp[i] = schur_product(src1[i], src2[i]);
1956 
1957  return tmp;
1958 }
1959 
1961 
1965 
1966 
1990 template <int rank_1,
1991  int rank_2,
1992  int dim,
1993  typename Number,
1994  typename OtherNumber>
1995 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
1996  typename Tensor<rank_1 + rank_2 - 2,
1997  dim,
1998  typename ProductType<Number, OtherNumber>::type>::tensor_type
2001 {
2002  typename Tensor<rank_1 + rank_2 - 2,
2003  dim,
2004  typename ProductType<Number, OtherNumber>::type>::tensor_type
2005  result{};
2006 
2007  TensorAccessors::internal::
2008  ReorderedIndexView<0, rank_2, const Tensor<rank_2, dim, OtherNumber>>
2009  reordered = TensorAccessors::reordered_index_view<0, rank_2>(src2);
2010  TensorAccessors::contract<1, rank_1, rank_2, dim>(result, src1, reordered);
2011 
2012  return result;
2013 }
2014 
2015 
2045 template <int index_1,
2046  int index_2,
2047  int rank_1,
2048  int rank_2,
2049  int dim,
2050  typename Number,
2051  typename OtherNumber>
2052 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
2053  typename Tensor<rank_1 + rank_2 - 2,
2054  dim,
2055  typename ProductType<Number, OtherNumber>::type>::tensor_type
2058 {
2059  Assert(0 <= index_1 && index_1 < rank_1,
2060  ExcMessage(
2061  "The specified index_1 must lie within the range [0,rank_1)"));
2062  Assert(0 <= index_2 && index_2 < rank_2,
2063  ExcMessage(
2064  "The specified index_2 must lie within the range [0,rank_2)"));
2065 
2066  using namespace TensorAccessors;
2067  using namespace TensorAccessors::internal;
2068 
2069  // Reorder index_1 to the end of src1:
2070  ReorderedIndexView<index_1, rank_1, const Tensor<rank_1, dim, Number>>
2071  reord_01 = reordered_index_view<index_1, rank_1>(src1);
2072 
2073  // Reorder index_2 to the end of src2:
2074  ReorderedIndexView<index_2, rank_2, const Tensor<rank_2, dim, OtherNumber>>
2075  reord_02 = reordered_index_view<index_2, rank_2>(src2);
2076 
2077  typename Tensor<rank_1 + rank_2 - 2,
2078  dim,
2079  typename ProductType<Number, OtherNumber>::type>::tensor_type
2080  result{};
2081  TensorAccessors::contract<1, rank_1, rank_2, dim>(result, reord_01, reord_02);
2082  return result;
2083 }
2084 
2085 
2117 template <int index_1,
2118  int index_2,
2119  int index_3,
2120  int index_4,
2121  int rank_1,
2122  int rank_2,
2123  int dim,
2124  typename Number,
2125  typename OtherNumber>
2126 DEAL_II_CONSTEXPR inline
2127  typename Tensor<rank_1 + rank_2 - 4,
2128  dim,
2129  typename ProductType<Number, OtherNumber>::type>::tensor_type
2132 {
2133  Assert(0 <= index_1 && index_1 < rank_1,
2134  ExcMessage(
2135  "The specified index_1 must lie within the range [0,rank_1)"));
2136  Assert(0 <= index_3 && index_3 < rank_1,
2137  ExcMessage(
2138  "The specified index_3 must lie within the range [0,rank_1)"));
2139  Assert(index_1 != index_3,
2140  ExcMessage("index_1 and index_3 must not be the same"));
2141  Assert(0 <= index_2 && index_2 < rank_2,
2142  ExcMessage(
2143  "The specified index_2 must lie within the range [0,rank_2)"));
2144  Assert(0 <= index_4 && index_4 < rank_2,
2145  ExcMessage(
2146  "The specified index_4 must lie within the range [0,rank_2)"));
2147  Assert(index_2 != index_4,
2148  ExcMessage("index_2 and index_4 must not be the same"));
2149 
2150  using namespace TensorAccessors;
2151  using namespace TensorAccessors::internal;
2152 
2153  // Reorder index_1 to the end of src1:
2154  ReorderedIndexView<index_1, rank_1, const Tensor<rank_1, dim, Number>>
2155  reord_1 = TensorAccessors::reordered_index_view<index_1, rank_1>(src1);
2156 
2157  // Reorder index_2 to the end of src2:
2158  ReorderedIndexView<index_2, rank_2, const Tensor<rank_2, dim, OtherNumber>>
2159  reord_2 = TensorAccessors::reordered_index_view<index_2, rank_2>(src2);
2160 
2161  // Now, reorder index_3 to the end of src1. We have to make sure to
2162  // preserve the original ordering: index_1 has been removed. If
2163  // index_3 > index_1, we have to use (index_3 - 1) instead:
2164  ReorderedIndexView<
2165  (index_3 < index_1 ? index_3 : index_3 - 1),
2166  rank_1,
2167  ReorderedIndexView<index_1, rank_1, const Tensor<rank_1, dim, Number>>>
2168  reord_3 =
2169  TensorAccessors::reordered_index_view < index_3 < index_1 ? index_3 :
2170  index_3 - 1,
2171  rank_1 > (reord_1);
2172 
2173  // Now, reorder index_4 to the end of src2. We have to make sure to
2174  // preserve the original ordering: index_2 has been removed. If
2175  // index_4 > index_2, we have to use (index_4 - 1) instead:
2176  ReorderedIndexView<
2177  (index_4 < index_2 ? index_4 : index_4 - 1),
2178  rank_2,
2179  ReorderedIndexView<index_2, rank_2, const Tensor<rank_2, dim, OtherNumber>>>
2180  reord_4 =
2181  TensorAccessors::reordered_index_view < index_4 < index_2 ? index_4 :
2182  index_4 - 1,
2183  rank_2 > (reord_2);
2184 
2185  typename Tensor<rank_1 + rank_2 - 4,
2186  dim,
2187  typename ProductType<Number, OtherNumber>::type>::tensor_type
2188  result{};
2189  TensorAccessors::contract<2, rank_1, rank_2, dim>(result, reord_3, reord_4);
2190  return result;
2191 }
2192 
2193 
2207 template <int rank, int dim, typename Number, typename OtherNumber>
2208 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
2209  typename ProductType<Number, OtherNumber>::type
2211  const Tensor<rank, dim, OtherNumber> &right)
2212 {
2213  typename ProductType<Number, OtherNumber>::type result{};
2214  TensorAccessors::contract<rank, rank, rank, dim>(result, left, right);
2215  return result;
2216 }
2217 
2218 
2237 template <template <int, int, typename> class TensorT1,
2238  template <int, int, typename> class TensorT2,
2239  template <int, int, typename> class TensorT3,
2240  int rank_1,
2241  int rank_2,
2242  int dim,
2243  typename T1,
2244  typename T2,
2245  typename T3>
2246 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
2248  contract3(const TensorT1<rank_1, dim, T1> & left,
2249  const TensorT2<rank_1 + rank_2, dim, T2> &middle,
2250  const TensorT3<rank_2, dim, T3> & right)
2251 {
2252  using return_type =
2254  return TensorAccessors::contract3<rank_1, rank_2, dim, return_type>(left,
2255  middle,
2256  right);
2257 }
2258 
2259 
2271 template <int rank_1,
2272  int rank_2,
2273  int dim,
2274  typename Number,
2275  typename OtherNumber>
2276 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
2280 {
2281  typename Tensor<rank_1 + rank_2,
2282  dim,
2283  typename ProductType<Number, OtherNumber>::type>::tensor_type
2284  result{};
2285  TensorAccessors::contract<0, rank_1, rank_2, dim>(result, src1, src2);
2286  return result;
2287 }
2288 
2289 
2291 
2295 
2296 
2308 template <int dim, typename Number>
2309 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE Tensor<1, dim, Number>
2311 {
2312  Assert(dim == 2, ExcInternalError());
2313 
2314  Tensor<1, dim, Number> result;
2315 
2316  result[0] = src[1];
2317  result[1] = -src[0];
2318 
2319  return result;
2320 }
2321 
2322 
2333 template <int dim, typename Number1, typename Number2>
2334 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE
2337  const Tensor<1, dim, Number2> &src2)
2338 {
2339  Assert(dim == 3, ExcInternalError());
2340 
2342 
2343  result[0] = src1[1] * src2[2] - src1[2] * src2[1];
2344  result[1] = src1[2] * src2[0] - src1[0] * src2[2];
2345  result[2] = src1[0] * src2[1] - src1[1] * src2[0];
2346 
2347  return result;
2348 }
2349 
2350 
2352 
2356 
2357 
2364 template <int dim, typename Number>
2365 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE Number
2367 {
2368  // Compute the determinant using the Laplace expansion of the
2369  // determinant. We expand along the last row.
2370  Number det = internal::NumberType<Number>::value(0.0);
2371 
2372  for (unsigned int k = 0; k < dim; ++k)
2373  {
2374  Tensor<2, dim - 1, Number> minor;
2375  for (unsigned int i = 0; i < dim - 1; ++i)
2376  for (unsigned int j = 0; j < dim - 1; ++j)
2377  minor[i][j] = t[i][j < k ? j : j + 1];
2378 
2379  const Number cofactor = ((k % 2 == 0) ? -1. : 1.) * determinant(minor);
2380 
2381  det += t[dim - 1][k] * cofactor;
2382  }
2383 
2384  return ((dim % 2 == 0) ? 1. : -1.) * det;
2385 }
2386 
2392 template <typename Number>
2393 constexpr DEAL_II_ALWAYS_INLINE Number
2395 {
2396  return t[0][0];
2397 }
2398 
2399 
2407 template <int dim, typename Number>
2408 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE Number
2410 {
2411  Number t = d[0][0];
2412  for (unsigned int i = 1; i < dim; ++i)
2413  t += d[i][i];
2414  return t;
2415 }
2416 
2417 
2427 template <int dim, typename Number>
2428 DEAL_II_CONSTEXPR inline Tensor<2, dim, Number>
2430 {
2431  Number return_tensor[dim][dim];
2432 
2433  // if desired, take over the
2434  // inversion of a 4x4 tensor
2435  // from the FullMatrix
2436  AssertThrow(false, ExcNotImplemented());
2437 
2438  return Tensor<2, dim, Number>(return_tensor);
2439 }
2440 
2441 
2442 #ifndef DOXYGEN
2443 
2444 template <typename Number>
2445 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE Tensor<2, 1, Number>
2446  invert(const Tensor<2, 1, Number> &t)
2447 {
2448  Tensor<2, 1, Number> return_tensor;
2449 
2450  return_tensor[0][0] = internal::NumberType<Number>::value(1.0 / t[0][0]);
2451 
2452  return return_tensor;
2453 }
2454 
2455 
2456 template <typename Number>
2457 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE Tensor<2, 2, Number>
2458  invert(const Tensor<2, 2, Number> &t)
2459 {
2460  Tensor<2, 2, Number> return_tensor;
2461 
2462  // this is Maple output,
2463  // thus a bit unstructured
2464  const Number inv_det_t = internal::NumberType<Number>::value(
2465  1.0 / (t[0][0] * t[1][1] - t[1][0] * t[0][1]));
2466  return_tensor[0][0] = t[1][1];
2467  return_tensor[0][1] = -t[0][1];
2468  return_tensor[1][0] = -t[1][0];
2469  return_tensor[1][1] = t[0][0];
2470  return_tensor *= inv_det_t;
2471 
2472  return return_tensor;
2473 }
2474 
2475 
2476 template <typename Number>
2477 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE Tensor<2, 3, Number>
2478  invert(const Tensor<2, 3, Number> &t)
2479 {
2480  Tensor<2, 3, Number> return_tensor;
2481 
2482  const Number t4 = internal::NumberType<Number>::value(t[0][0] * t[1][1]),
2483  t6 = internal::NumberType<Number>::value(t[0][0] * t[1][2]),
2484  t8 = internal::NumberType<Number>::value(t[0][1] * t[1][0]),
2485  t00 = internal::NumberType<Number>::value(t[0][2] * t[1][0]),
2486  t01 = internal::NumberType<Number>::value(t[0][1] * t[2][0]),
2487  t04 = internal::NumberType<Number>::value(t[0][2] * t[2][0]),
2488  inv_det_t = internal::NumberType<Number>::value(
2489  1.0 / (t4 * t[2][2] - t6 * t[2][1] - t8 * t[2][2] +
2490  t00 * t[2][1] + t01 * t[1][2] - t04 * t[1][1]));
2491  return_tensor[0][0] = internal::NumberType<Number>::value(t[1][1] * t[2][2]) -
2492  internal::NumberType<Number>::value(t[1][2] * t[2][1]);
2493  return_tensor[0][1] = internal::NumberType<Number>::value(t[0][2] * t[2][1]) -
2494  internal::NumberType<Number>::value(t[0][1] * t[2][2]);
2495  return_tensor[0][2] = internal::NumberType<Number>::value(t[0][1] * t[1][2]) -
2496  internal::NumberType<Number>::value(t[0][2] * t[1][1]);
2497  return_tensor[1][0] = internal::NumberType<Number>::value(t[1][2] * t[2][0]) -
2498  internal::NumberType<Number>::value(t[1][0] * t[2][2]);
2499  return_tensor[1][1] =
2500  internal::NumberType<Number>::value(t[0][0] * t[2][2]) - t04;
2501  return_tensor[1][2] = t00 - t6;
2502  return_tensor[2][0] = internal::NumberType<Number>::value(t[1][0] * t[2][1]) -
2503  internal::NumberType<Number>::value(t[1][1] * t[2][0]);
2504  return_tensor[2][1] =
2505  t01 - internal::NumberType<Number>::value(t[0][0] * t[2][1]);
2506  return_tensor[2][2] = internal::NumberType<Number>::value(t4 - t8);
2507  return_tensor *= inv_det_t;
2508 
2509  return return_tensor;
2510 }
2511 
2512 #endif /* DOXYGEN */
2513 
2514 
2521 template <int dim, typename Number>
2522 DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE Tensor<2, dim, Number>
2524 {
2526  for (unsigned int i = 0; i < dim; ++i)
2527  {
2528  tt[i][i] = t[i][i];
2529  for (unsigned int j = i + 1; j < dim; ++j)
2530  {
2531  tt[i][j] = t[j][i];
2532  tt[j][i] = t[i][j];
2533  };
2534  }
2535  return tt;
2536 }
2537 
2538 
2553 template <int dim, typename Number>
2554 constexpr Tensor<2, dim, Number>
2556 {
2557  return determinant(t) * invert(t);
2558 }
2559 
2560 
2575 template <int dim, typename Number>
2576 constexpr Tensor<2, dim, Number>
2578 {
2579  return transpose(adjugate(t));
2580 }
2581 
2582 
2590 template <int dim, typename Number>
2591 inline Number
2593 {
2594  Number max = internal::NumberType<Number>::value(0.0);
2595  for (unsigned int j = 0; j < dim; ++j)
2596  {
2597  Number sum = internal::NumberType<Number>::value(0.0);
2598  for (unsigned int i = 0; i < dim; ++i)
2599  sum += std::fabs(t[i][j]);
2600 
2601  if (sum > max)
2602  max = sum;
2603  }
2604 
2605  return max;
2606 }
2607 
2608 
2616 template <int dim, typename Number>
2617 inline Number
2619 {
2620  Number max = internal::NumberType<Number>::value(0.0);
2621  for (unsigned int i = 0; i < dim; ++i)
2622  {
2623  Number sum = internal::NumberType<Number>::value(0.0);
2624  for (unsigned int j = 0; j < dim; ++j)
2625  sum += std::fabs(t[i][j]);
2626 
2627  if (sum > max)
2628  max = sum;
2629  }
2630 
2631  return max;
2632 }
2633 
2635 
2636 
2637 #ifndef DOXYGEN
2638 
2639 
2640 # ifdef DEAL_II_ADOLC_WITH_ADVANCED_BRANCHING
2641 
2642 // Specialization of functions for ADOL-C number types when
2643 // the advanced branching feature is used
2644 template <int dim>
2645 inline adouble
2647 {
2648  adouble max = internal::NumberType<adouble>::value(0.0);
2649  for (unsigned int j = 0; j < dim; ++j)
2650  {
2651  adouble sum = internal::NumberType<adouble>::value(0.0);
2652  for (unsigned int i = 0; i < dim; ++i)
2653  sum += std::fabs(t[i][j]);
2654 
2655  condassign(max, (sum > max), sum, max);
2656  }
2657 
2658  return max;
2659 }
2660 
2661 
2662 template <int dim>
2663 inline adouble
2665 {
2666  adouble max = internal::NumberType<adouble>::value(0.0);
2667  for (unsigned int i = 0; i < dim; ++i)
2668  {
2669  adouble sum = internal::NumberType<adouble>::value(0.0);
2670  for (unsigned int j = 0; j < dim; ++j)
2671  sum += std::fabs(t[i][j]);
2672 
2673  condassign(max, (sum > max), sum, max);
2674  }
2675 
2676  return max;
2677 }
2678 
2679 # endif // DEAL_II_ADOLC_WITH_ADVANCED_BRANCHING
2680 
2681 
2682 #endif // DOXYGEN
2683 
2684 DEAL_II_NAMESPACE_CLOSE
2685 
2686 #endif
constexpr Tensor()=default
DEAL_II_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:1949
Tensor< rank, dim, Number > sum(const Tensor< rank, dim, Number > &local, const MPI_Comm &mpi_communicator)
DEAL_II_CONSTEXPR ProductType< Number, Other >::type operator*(const Tensor< 0, dim, Number > &t, const Other &object)
Definition: tensor.h:1641
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1567
DEAL_II_CONSTEXPR ProductType< Number, OtherNumber >::type scalar_product(const Tensor< rank, dim, Number > &left, const Tensor< rank, dim, OtherNumber > &right)
Definition: tensor.h:2210
DEAL_II_CONSTEXPR ProductType< Other, Number >::type operator*(const Other &object, const Tensor< 0, dim, Number > &t)
Definition: tensor.h:1621
DEAL_II_CONSTEXPR Tensor< 2, dim, Number > transpose(const Tensor< 2, dim, Number > &t)
Definition: tensor.h:2523
constexpr Tensor< 2, dim, Number > cofactor(const Tensor< 2, dim, Number > &t)
Definition: tensor.h:2577
constexpr bool values_are_equal(const Number1 &value_1, const Number2 &value_2)
Definition: numbers.h:943
DEAL_II_CONSTEXPR Tensor< rank_1+rank_2 - 2, dim, typename ProductType< Number, OtherNumber >::type >::tensor_type contract(const Tensor< rank_1, dim, Number > &src1, const Tensor< rank_2, dim, OtherNumber > &src2)
Definition: tensor.h:2056
DEAL_II_CONSTEXPR Tensor< 2, dim, Number > invert(const Tensor< 2, dim, Number > &)
Definition: tensor.h:2429
DEAL_II_CONSTEXPR Tensor & operator/=(const OtherNumber &factor)
static constexpr std::enable_if< std::is_same< Dummy, number >::value &&is_cuda_compatible< Dummy >::value, real_type >::type abs_square(const number &x)
Definition: numbers.h:605
Number l1_norm(const Tensor< 2, dim, Number > &t)
Definition: tensor.h:2592
DEAL_II_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:1768
Number linfty_norm(const Tensor< 2, dim, Number > &t)
Definition: tensor.h:2618
Tensor< rank_ - 1, dim, Number > values[(dim !=0) ? dim :1]
Definition: tensor.h:718
DEAL_II_CONSTEXPR Tensor & operator=(const Tensor< rank_, dim, OtherNumber > &rhs)
typename Tensor< rank_ - 1, dim, Number >::array_type[(dim !=0) ? dim :1] array_type
Definition: tensor.h:459
DEAL_II_CONSTEXPR Tensor< 1, dim, typename ProductType< Number1, Number2 >::type > cross_product_3d(const Tensor< 1, dim, Number1 > &src1, const Tensor< 1, dim, Number2 > &src2)
Definition: tensor.h:2336
#define AssertThrow(cond, exc)
Definition: exceptions.h:1519
static real_type abs(const number &x)
Definition: numbers.h:627
numbers::NumberTraits< Number >::real_type norm() const
Definition: tensor.h:1413
void unroll_recursion(Vector< OtherNumber > &result, unsigned int &start_index) const
Definition: tensor.h:1449
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
Definition: point.h:110
LinearAlgebra::distributed::Vector< Number > Vector
DEAL_II_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:1857
DEAL_II_CONSTEXPR void clear()
Definition: tensor.h:1533
DEAL_II_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:1881
DEAL_II_CONSTEXPR value_type & operator[](const unsigned int i)
Definition: tensor.h:1160
DEAL_II_CONSTEXPR Number determinant(const Tensor< 2, dim, Number > &t)
Definition: tensor.h:2366
static ::ExceptionBase & ExcMessage(std::string arg1)
DEAL_II_CONSTEXPR Tensor & operator*=(const OtherNumber &factor)
DEAL_II_CONSTEXPR Tensor< 1, dim, Number > cross_product_2d(const Tensor< 1, dim, Number > &src)
Definition: tensor.h:2310
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:1715
DEAL_II_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:1839
DEAL_II_CONSTEXPR Tensor & operator-=(const Tensor< rank_, dim, OtherNumber > &)
DEAL_II_CONSTEXPR ProductType< T1, typename ProductType< T2, T3 >::type >::type contract3(const TensorT1< rank_1, dim, T1 > &left, const TensorT2< rank_1+rank_2, dim, T2 > &middle, const TensorT3< rank_2, dim, T3 > &right)
Definition: tensor.h:2248
#define Assert(cond, exc)
Definition: exceptions.h:1407
constexpr internal::ReorderedIndexView< index, rank, T > reordered_index_view(T &t)
void serialize(Archive &ar, const unsigned int version)
Definition: tensor.h:1551
static DEAL_II_CONSTEXPR unsigned int component_to_unrolled_index(const TableIndices< rank_ > &indices)
Definition: tensor.h:1459
Number * end_raw()
Definition: tensor.h:1223
typename Tensor< rank_ - 1, dim, Number >::tensor_type value_type
Definition: tensor.h:452
DEAL_II_CONSTEXPR Tensor< rank_1+rank_2 - 2, dim, typename ProductType< Number, OtherNumber >::type >::tensor_type operator*(const Tensor< rank_1, dim, Number > &src1, const Tensor< rank_2, dim, OtherNumber > &src2)
Definition: tensor.h:1999
static constexpr unsigned int rank
Definition: tensor.h:438
constexpr Tensor< 2, dim, Number > adjugate(const Tensor< 2, dim, Number > &t)
Definition: tensor.h:2555
constexpr ProductType< Number, OtherNumber >::type operator*(const Tensor< 0, dim, Number > &src1, const Tensor< 0, dim, OtherNumber > &src2)
Definition: tensor.h:1661
DEAL_II_CONSTEXPR Tensor< rank_1+rank_2, dim, typename ProductType< Number, OtherNumber >::type > outer_product(const Tensor< rank_1, dim, Number > &src1, const Tensor< rank_2, dim, OtherNumber > &src2)
Definition: tensor.h:2278
DEAL_II_CONSTEXPR Tensor operator-() const
Definition: tensor.h:1400
constexpr bool operator!=(const Tensor< rank_, dim, OtherNumber > &) const
Definition: tensor.h:1297
typename numbers::NumberTraits< Number >::real_type real_type
Definition: tensor.h:132
DEAL_II_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:1901
DEAL_II_CONSTEXPR numbers::NumberTraits< Number >::real_type norm_square() const
Definition: tensor.h:1422
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:1682
Definition: tensor.h:422
Expression operator==(const Expression &lhs, const Expression &rhs)
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:1698
DEAL_II_CONSTEXPR Tensor & operator+=(const Tensor< rank_, dim, OtherNumber > &)
constexpr bool value_is_zero(const Number &value)
Definition: numbers.h:959
Number * begin_raw()
Definition: tensor.h:1203
DEAL_II_CONSTEXPR Tensor< rank_1+rank_2 - 4, dim, typename ProductType< Number, OtherNumber >::type >::tensor_type double_contract(const Tensor< rank_1, dim, Number > &src1, const Tensor< rank_2, dim, OtherNumber > &src2)
Definition: tensor.h:2130
Expression operator-(Expression lhs, const Expression &rhs)
static ::ExceptionBase & ExcNotImplemented()
void unroll(Vector< OtherNumber > &result) const
Definition: tensor.h:1436
static DEAL_II_CONSTEXPR TableIndices< rank_ > unrolled_to_component_indices(const unsigned int i)
Definition: tensor.h:1512
static constexpr unsigned int n_independent_components
Definition: tensor.h:444
DEAL_II_CONSTEXPR bool operator==(const Tensor< rank_, dim, OtherNumber > &) const
Definition: tensor.h:1270
Expression operator!=(const Expression &lhs, const Expression &rhs)
DEAL_II_CONSTEXPR Number trace(const Tensor< 2, dim, Number > &d)
Definition: tensor.h:2409
static constexpr std::size_t memory_consumption()
Definition: tensor.h:1542
constexpr Number determinant(const Tensor< 2, 1, Number > &t)
Definition: tensor.h:2394
DEAL_II_CONSTEXPR Tensor< 1, dim, typename ProductType< Number, OtherNumber >::type > schur_product(const Tensor< 1, dim, Number > &src1, const Tensor< 1, dim, OtherNumber > &src2)
Definition: tensor.h:1919
static ::ExceptionBase & ExcInternalError()
DEAL_II_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:1740