20#ifndef LIBMESH_TYPE_TENSOR_H
21#define LIBMESH_TYPE_TENSOR_H
24#include "libmesh/libmesh_common.h"
25#include "libmesh/libmesh_device.h"
26#include "libmesh/type_vector.h"
33#ifdef LIBMESH_HAVE_METAPHYSICL
34#include "metaphysicl/dualnumber_decl.h"
41template <
typename T>
class TypeTensorColumn;
42template <
typename T>
class ConstTypeTensorColumn;
43template <
unsigned int N,
typename T>
class TypeNTensor;
55 template <
typename T2>
86 template <
typename Scalar>
97 const Scalar>::type & zz=0);
104 template<
typename T2>
105 LIBMESH_DEVICE_INLINE
108 template<
typename T2>
109 LIBMESH_DEVICE_INLINE
113 template<
typename T2>
114 LIBMESH_DEVICE_INLINE
134 template<
typename T2>
145 template<
typename T2>
146 LIBMESH_DEVICE_INLINE
154 template <
typename Scalar>
155 LIBMESH_DEVICE_INLINE
156 typename std::enable_if<
160 { libmesh_assert_equal_to (p, Scalar(0)); this->
zero();
return *
this; }
165 const T &
operator () (
const unsigned int i,
const unsigned int j)
const;
175 LIBMESH_DEVICE_INLINE
181 LIBMESH_DEVICE_INLINE
192 LIBMESH_DEVICE_INLINE
200 template<
typename T2>
209 template<
typename T2>
215 template<
typename T2>
221 template <
typename T2>
222 LIBMESH_DEVICE_INLINE
230 template<
typename T2>
239 template<
typename T2>
245 template<
typename T2>
252 template <
typename T2>
253 LIBMESH_DEVICE_INLINE
266 template <
typename Scalar>
268 operator * (
const Scalar & scalar)
const ->
typename std::enable_if<
277 template <
typename Scalar,
typename std::enable_if<
279 LIBMESH_DEVICE_INLINE
282 for (
unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
293 template <
typename Scalar>
294 typename std::enable_if<
312 template <
typename T2>
321 template <
typename T2>
333 template <
typename T2>
334 LIBMESH_DEVICE_INLINE
344 template <
typename T2>
354 template <
typename T2>
355 LIBMESH_DEVICE_INLINE
375 LIBMESH_DEVICE_INLINE
393 LIBMESH_DEVICE_INLINE
412 LIBMESH_DEVICE_INLINE
418 LIBMESH_DEVICE_INLINE
455 const bool newline =
true)
const;
497 for (
unsigned int i=0; i != LIBMESH_DIM; ++i)
504 const unsigned int _j;
523 const T &
slice (
const unsigned int i)
const
528 const unsigned int _j;
574#elif LIBMESH_DIM == 1
575 libmesh_assert_equal_to (xy, 0);
576 libmesh_assert_equal_to (yx, 0);
577 libmesh_assert_equal_to (yy, 0);
591 libmesh_assert_equal_to (xz, 0);
592 libmesh_assert_equal_to (yz, 0);
593 libmesh_assert_equal_to (zx, 0);
594 libmesh_assert_equal_to (zy, 0);
595 libmesh_assert_equal_to (zz, 0);
602template <
typename Scalar>
614 const Scalar>::type & zz)
622#elif LIBMESH_DIM == 1
623 libmesh_assert_equal_to (xy, 0);
624 libmesh_assert_equal_to (yx, 0);
625 libmesh_assert_equal_to (yy, 0);
639 libmesh_assert_equal_to (xz, 0);
640 libmesh_assert_equal_to (yz, 0);
641 libmesh_assert_equal_to (zx, 0);
642 libmesh_assert_equal_to (zy, 0);
643 libmesh_assert_equal_to (zz, 0);
656 for (
unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
662template <
typename T2>
666 libmesh_assert_equal_to (LIBMESH_DIM, 1);
671template <
typename T2>
676 libmesh_assert_equal_to (LIBMESH_DIM, 2);
688template <
typename T2>
694 libmesh_assert_equal_to (LIBMESH_DIM, 3);
718 for (
unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
727 const unsigned int j)
const
729 libmesh_assert_less (i, 3);
730 libmesh_assert_less (j, 3);
733 const static T my_zero = 0;
734 if (i >= LIBMESH_DIM || j >= LIBMESH_DIM)
738 return _coords[i*LIBMESH_DIM+j];
746 const unsigned int j)
750 libmesh_error_msg_if(i >= LIBMESH_DIM || j >= LIBMESH_DIM,
751 "ERROR: You are assigning to a tensor component that is out of range for the compiled LIBMESH_DIM!");
755 libmesh_assert_less (i, LIBMESH_DIM);
756 libmesh_assert_less (j, LIBMESH_DIM);
758 return _coords[i*LIBMESH_DIM+j];
767 libmesh_assert_less (i, LIBMESH_DIM);
777 libmesh_assert_less (i, LIBMESH_DIM);
789 for (
unsigned int j=0; j<LIBMESH_DIM; j++)
790 return_vector.
_coords[j] = _coords[r*LIBMESH_DIM + j];
792 return return_vector;
803 for (
unsigned int i=0; i<LIBMESH_DIM; i++)
804 return_vector.
_coords[i] = _coords[i*LIBMESH_DIM + r];
806 return return_vector;
862 for (
unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
869template <
typename T2>
873 for (
unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
874 _coords[i] += factor*p.
_coords[i];
916template <
typename T2>
928template <
typename T2>
932 for (
unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
939template <
typename T2>
943 for (
unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
944 _coords[i] -= factor*p.
_coords[i];
982template <
typename Scalar>
989 typedef decltype((*this)(0, 0) * factor) TS;
1018template <
typename T,
typename Scalar>
1019LIBMESH_DEVICE_INLINE
1020typename std::enable_if<
1029template <
typename T>
1030template <
typename Scalar>
1031LIBMESH_DEVICE_INLINE
1032typename std::enable_if<
1034 TypeTensor<typename CompareTypes<T, Scalar>::supertype>>::type
1037 libmesh_assert_not_equal_to (factor,
static_cast<T
>(0.));
1068template <
typename T>
1069LIBMESH_DEVICE_INLINE
1098template <
typename T>
1099LIBMESH_DEVICE_INLINE
1103 if (_coords[0] ==
static_cast<T
>(0.))
1104 libmesh_convergence_failure();
1113 T a = A(0,0),
b = A(0,1),
1114 c = A(1,0), d = A(1,1);
1117 T my_det = a*d -
b*c;
1119 if (my_det ==
static_cast<T
>(0.))
1120 libmesh_convergence_failure();
1122 return TypeTensor(d/my_det, -
b/my_det, -c/my_det, a/my_det);
1129 T a11 = A(0,0), a12 = A(0,1), a13 = A(0,2),
1130 a21 = A(1,0), a22 = A(1,1), a23 = A(1,2),
1131 a31 = A(2,0), a32 = A(2,1), a33 = A(2,2);
1133 T my_det = a11*(a33*a22-a32*a23) - a21*(a33*a12-a32*a13) + a31*(a23*a12-a22*a13);
1135 if (my_det ==
static_cast<T
>(0.))
1136 libmesh_convergence_failure();
1139 return TypeTensor( (a33*a22-a32*a23)/my_det, -(a33*a12-a32*a13)/my_det, (a23*a12-a22*a13)/my_det,
1140 -(a33*a21-a31*a23)/my_det, (a33*a11-a31*a13)/my_det, -(a23*a11-a21*a13)/my_det,
1141 (a32*a21-a31*a22)/my_det, -(a32*a11-a31*a12)/my_det, (a22*a11-a21*a12)/my_det);
1147template <
typename T>
1148LIBMESH_DEVICE_INLINE
1152 if (_coords[0] ==
static_cast<T
>(0.))
1153 libmesh_convergence_failure();
1154 x(0) =
b(0) / _coords[0];
1158 T my_det = _coords[0]*_coords[3] - _coords[1]*_coords[2];
1160 if (my_det ==
static_cast<T
>(0.))
1161 libmesh_convergence_failure();
1163 T my_det_inv = 1./my_det;
1165 x(0) = my_det_inv*( _coords[3]*
b(0) - _coords[1]*
b(1));
1166 x(1) = my_det_inv*(-_coords[2]*
b(0) + _coords[0]*
b(1));
1172 _coords[0]*(_coords[8]*_coords[4] - _coords[7]*_coords[5])
1174 -_coords[3]*(_coords[8]*_coords[1] - _coords[7]*_coords[2]) +
1176 +_coords[6]*(_coords[5]*_coords[1] - _coords[4]*_coords[2]);
1178 if (my_det ==
static_cast<T
>(0.))
1179 libmesh_convergence_failure();
1181 T my_det_inv = 1./my_det;
1182 x(0) = my_det_inv*((_coords[8]*_coords[4] - _coords[7]*_coords[5])*
b(0) -
1183 (_coords[8]*_coords[1] - _coords[7]*_coords[2])*
b(1) +
1184 (_coords[5]*_coords[1] - _coords[4]*_coords[2])*
b(2));
1186 x(1) = my_det_inv*((_coords[6]*_coords[5] - _coords[8]*_coords[3])*
b(0) +
1187 (_coords[8]*_coords[0] - _coords[6]*_coords[2])*
b(1) -
1188 (_coords[5]*_coords[0] - _coords[3]*_coords[2])*
b(2));
1190 x(2) = my_det_inv*((_coords[7]*_coords[3] - _coords[6]*_coords[4])*
b(0) -
1191 (_coords[7]*_coords[0] - _coords[6]*_coords[1])*
b(1) +
1192 (_coords[4]*_coords[0] - _coords[3]*_coords[1])*
b(2));
1198template <
typename T>
1199LIBMESH_DEVICE_INLINE
1202 libmesh_assert_not_equal_to (factor,
static_cast<T
>(0.));
1204 for (
unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
1205 _coords[i] /= factor;
1213template <
typename T>
1214template <
typename T2>
1215LIBMESH_DEVICE_INLINE
1220 for (
unsigned int i=0; i<LIBMESH_DIM; i++)
1221 for (
unsigned int j=0; j<LIBMESH_DIM; j++)
1222 returnval(i) += (*this)(i,j)*p(j);
1227template <
typename T>
1228template <
typename T2>
1229LIBMESH_DEVICE_INLINE
1234 for (
unsigned int i=0; i<LIBMESH_DIM; i++)
1235 for (
unsigned int j=0; j<LIBMESH_DIM; j++)
1236 returnval(i) += p(j)*(*this)(j,i);
1241template <
typename T,
typename T2>
1242LIBMESH_DEVICE_INLINE
1246 return b.left_multiply(a);
1249template <
typename T>
1250template <
typename T2>
1251LIBMESH_DEVICE_INLINE
1252TypeTensor<typename CompareTypes<T, T2>::supertype>
1256 for (
unsigned int i=0; i<LIBMESH_DIM; i++)
1257 for (
unsigned int j=0; j<LIBMESH_DIM; j++)
1258 for (
unsigned int k=0; k<LIBMESH_DIM; k++)
1259 returnval(i,j) += (*this)(i,k)*p(k,j);
1264template <
typename T>
1265template <
typename T2>
1266LIBMESH_DEVICE_INLINE
1270 for (
unsigned int i=0; i<LIBMESH_DIM; i++)
1271 for (
unsigned int j=0; j<LIBMESH_DIM; j++)
1272 for (
unsigned int k=0; k<LIBMESH_DIM; k++)
1273 temp(i,j) += (*this)(i,k)*p(k,j);
1284template <
typename T>
1285template <
typename T2>
1286LIBMESH_DEVICE_INLINE
1291 for (
unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
1292 sum += _coords[i]*t.
_coords[i];
1298template <
typename T>
1299LIBMESH_DEVICE_INLINE
1302 using portable_math::sqrt;
1303 return sqrt(this->norm_sq());
1307template <
typename T>
1308LIBMESH_DEVICE_INLINE
1311 for (
const auto & val : _coords)
1317template <
typename T>
1318LIBMESH_DEVICE_INLINE
1326 return (_coords[0] * _coords[3]
1327 - _coords[1] * _coords[2]);
1331 return (_coords[0] * _coords[4] * _coords[8]
1332 + _coords[1] * _coords[5] * _coords[6]
1333 + _coords[2] * _coords[3] * _coords[7]
1334 - _coords[0] * _coords[5] * _coords[7]
1335 - _coords[1] * _coords[3] * _coords[8]
1336 - _coords[2] * _coords[4] * _coords[6]);
1340template <
typename T>
1341LIBMESH_DEVICE_INLINE
1349 return _coords[0] + _coords[3];
1353 return _coords[0] + _coords[4] + _coords[8];
1357template <
typename T>
1358LIBMESH_DEVICE_INLINE
1361 for (
unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
1367template <
typename T>
1368LIBMESH_DEVICE_INLINE
1372 for (
unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
1379template <
typename T>
1380LIBMESH_DEVICE_INLINE
1383 using portable_math::abs;
1386 return (abs(_coords[0] - rhs.
_coords[0])
1391 return ((abs(_coords[0] - rhs.
_coords[0]) +
1392 abs(_coords[1] - rhs.
_coords[1]) +
1393 abs(_coords[2] - rhs.
_coords[2]) +
1394 abs(_coords[3] - rhs.
_coords[3]))
1399 return ((abs(_coords[0] - rhs.
_coords[0]) +
1400 abs(_coords[1] - rhs.
_coords[1]) +
1401 abs(_coords[2] - rhs.
_coords[2]) +
1402 abs(_coords[3] - rhs.
_coords[3]) +
1403 abs(_coords[4] - rhs.
_coords[4]) +
1404 abs(_coords[5] - rhs.
_coords[5]) +
1405 abs(_coords[6] - rhs.
_coords[6]) +
1406 abs(_coords[7] - rhs.
_coords[7]) +
1407 abs(_coords[8] - rhs.
_coords[8]))
1413template <
typename T>
1418 os <<
"x=" << (*this)(0,0) << std::endl;
1424 << std::setw(8) << (*this)(0,0) <<
", "
1425 << std::setw(8) << (*this)(0,1) <<
")"
1428 << std::setw(8) << (*this)(1,0) <<
", "
1429 << std::setw(8) << (*this)(1,1) <<
")"
1435 os <<
"(xx,xy,xz)=("
1436 << std::setw(8) << (*this)(0,0) <<
", "
1437 << std::setw(8) << (*this)(0,1) <<
", "
1438 << std::setw(8) << (*this)(0,2) <<
")"
1440 os <<
"(yx,yy,yz)=("
1441 << std::setw(8) << (*this)(1,0) <<
", "
1442 << std::setw(8) << (*this)(1,1) <<
", "
1443 << std::setw(8) << (*this)(1,2) <<
")"
1445 os <<
"(zx,zy,zz)=("
1446 << std::setw(8) << (*this)(2,0) <<
", "
1447 << std::setw(8) << (*this)(2,1) <<
", "
1448 << std::setw(8) << (*this)(2,2) <<
")"
1455template <
typename T>
1458 using std::isfinite;
1475template <
typename T>
1482 bool has_inf =
false;
1487 if (
isnan(var(i,j)))
1489 has_inf = has_inf ||
isinf(var(i,j));
1502template <
typename T>
1509 if (
isnan(var(i,j)))
1516template <
typename T,
typename T2>
1517LIBMESH_DEVICE_INLINE
1518TypeTensor<typename CompareTypes<T, T2>::supertype>
1522 for (
unsigned int i=0; i<LIBMESH_DIM; i++)
1523 for (
unsigned int j=0; j<LIBMESH_DIM; j++)
1532#ifdef LIBMESH_HAVE_METAPHYSICL
1535template <
typename T>
1543 for (
unsigned int i = 0; i < LIBMESH_DIM; ++i)
1544 for (
unsigned int j = 0; j < LIBMESH_DIM; ++j)
1545 ret(i,j) = raw_value(in(i,j));
1551template <
typename T,
typename U>
1552struct ReplaceAlgebraicType<
libMesh::TypeTensor<T>, U>
const TypeTensor< T > * _tensor
const T & slice(const unsigned int i) const
ConstTypeTensorColumn(const TypeTensor< T > &tensor, unsigned int j)
const T & operator()(const unsigned int i) const
Return the element of the tensor.
T & slice(const unsigned int i)
TypeTensorColumn< T > & operator=(const TypeVector< T > &rhs)
Assign values to this column of the tensor.
TypeTensorColumn(TypeTensor< T > &tensor, unsigned int j)
TypeTensor< T > * _tensor
T & operator()(const unsigned int i)
This class defines a tensor in LIBMESH_DIM dimensional space of type T.
bool operator>(const TypeTensor< T > &rhs) const
const TypeTensor< T > & operator-=(const TypeTensor< T2 > &)
Subtract from this tensor.
LIBMESH_DEVICE_INLINE bool operator==(const TypeTensor< T > &rhs) const
bool operator<(const TypeTensor< T > &rhs) const
LIBMESH_DEVICE_INLINE ConstTypeTensorColumn< T > slice(const unsigned int i) const
LIBMESH_DEVICE_INLINE TypeTensorColumn< T > slice(const unsigned int i)
LIBMESH_DEVICE_INLINE void add_scaled(const TypeTensor< T2 > &, const T &)
Add a scaled tensor to this tensor without creating a temporary.
TypeTensor< T > inverse() const
LIBMESH_DEVICE_INLINE TypeTensor(const TypeVector< T2 > &vx, const TypeVector< T2 > &vy, const TypeVector< T2 > &vz)
const TypeTensor< T > & operator+=(const TypeTensor< T2 > &)
Add to this tensor.
void add(const TypeTensor< T2 > &)
Add to this tensor without creating a temporary.
LIBMESH_DEVICE_INLINE TypeTensor(const TypeVector< T2 > &vx, const TypeVector< T2 > &vy)
TypeTensor(const T &xx, const T &xy=0, const T &xz=0, const T &yx=0, const T &yy=0, const T &yz=0, const T &zx=0, const T &zy=0, const T &zz=0)
Constructor-from-T.
T value_type
Helper typedef for C++98 generic programming.
auto operator*(const Scalar &scalar) const -> typename std::enable_if< ScalarTraits< Scalar >::value, TypeTensor< decltype(T() *scalar)> >::type
Multiply this tensor by a scalar value.
LIBMESH_DEVICE_INLINE void assign(const TypeTensor< T2 > &)
Assign to this tensor without creating a temporary.
TypeTensor< T > transpose() const
TypeTensor(const TypeTensor< T2 > &p)
Copy-constructor.
std::enable_if< ScalarTraits< Scalar >::value, TypeTensor< typenameCompareTypes< T, Scalar >::supertype > >::type operator/(const Scalar &) const
Divide each entry of this tensor by a scalar value.
std::tuple< unsigned int, unsigned int > index_type
Helper typedef for generic index programming.
void subtract(const TypeTensor< T2 > &)
Subtract from this tensor without creating a temporary.
void print(std::ostream &os=libMesh::out) const
Formatted print to a stream which defaults to libMesh::out.
void write_unformatted(std::ostream &out_stream, const bool newline=true) const
Unformatted print to the stream out.
TypeTensor(const Scalar &xx, const Scalar &xy=0, const Scalar &xz=0, const Scalar &yx=0, const Scalar &yy=0, const Scalar &yz=0, const Scalar &zx=0, const Scalar &zy=0, typename std::enable_if< ScalarTraits< Scalar >::value, const Scalar >::type &zz=0)
Constructor-from-Scalar.
LIBMESH_DEVICE_INLINE TypeTensor(const TypeVector< T2 > &vx)
Constructor.
TypeTensor()
Empty constructor.
const T & operator()(const unsigned int i, const unsigned int j) const
LIBMESH_DEVICE_INLINE const TypeTensor< T > & operator*=(const Scalar &factor)
Multiply this tensor by a scalar value in place.
const TypeTensor< T > & operator/=(const T &)
Divide each entry of this tensor by a scalar value.
T _coords[LIBMESH_DIM *LIBMESH_DIM]
The coordinates of the TypeTensor.
TypeTensor< typename CompareTypes< T, T2 >::supertype > operator+(const TypeTensor< T2 > &) const
Add another tensor to this tensor.
LIBMESH_DEVICE_INLINE void subtract_scaled(const TypeTensor< T2 > &, const T &)
Subtract a scaled value from this tensor without creating a temporary.
friend std::ostream & operator<<(std::ostream &os, const TypeTensor< T > &t)
Formatted print as above but supports the syntax:
TypeTensor< T > operator-() const
LIBMESH_DEVICE_INLINE bool is_zero() const
bool is_hpd(Real rel_tol=TOLERANCE *TOLERANCE) const
Returns true if the TypeTensor is Hermitian (or symmetric, when T==Real) and positive-definite to wit...
LIBMESH_DEVICE_INLINE void solve(const TypeVector< T > &b, TypeVector< T > &x) const
Solve the 2x2 or 3x3 system of equations A*x = b for x by directly inverting A and multiplying it by ...
LIBMESH_DEVICE_INLINE CompareTypes< T, T2 >::supertype contract(const TypeTensor< T2 > &) const
Multiply 2 tensors together to return a scalar, i.e.
TypeVector< T > row(const unsigned int r) const
~TypeTensor()=default
Destructor.
LIBMESH_DEVICE_INLINE TypeVector< typename CompareTypes< T, T2 >::supertype > left_multiply(const TypeVector< T2 > &p) const
Left-multiply this tensor by a vector, i.e.
LIBMESH_DEVICE_INLINE void zero()
Set all entries of the tensor to 0.
LIBMESH_DEVICE_INLINE TypeVector< T > column(const unsigned int r) const
LIBMESH_DEVICE_INLINE std::enable_if< ScalarTraits< Scalar >::value, TypeTensor & >::type operator=(const Scalar &libmesh_dbg_var(p))
Assignment-from-scalar operator.
This class defines a vector in LIBMESH_DIM dimensional space of type T.
T _coords[LIBMESH_DIM]
The coordinates of the TypeVector.
The libMesh namespace provides an interface to certain functionality in the library.
void libmesh_ignore(const Args &...)
bool isfinite(std::complex< T > a)
bool isnan(std::complex< T > a)
std::enable_if< ScalarTraits< Scalar >::value, TypeNTensor< N, typenameCompareTypes< Scalar, T >::supertype > >::type operator*(const Scalar &, const TypeNTensor< N, T > &)
LIBMESH_DEVICE_INLINE TypeTensor< typename CompareTypes< T, T2 >::supertype > outer_product(const TypeVector< T > &a, const TypeVector< T2 > &b)
static constexpr Real TOLERANCE
bool isinf(std::complex< T > a)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
LIBMESH_DEVICE_INLINE T libmesh_conj(T a)