libMesh
Loading...
Searching...
No Matches
type_tensor.h
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19
20#ifndef LIBMESH_TYPE_TENSOR_H
21#define LIBMESH_TYPE_TENSOR_H
22
23// Local includes
24#include "libmesh/libmesh_common.h"
25#include "libmesh/libmesh_device.h"
26#include "libmesh/type_vector.h"
27
28// C++ includes
29#include <cstdlib> // *must* precede <cmath> for proper std:abs() on PGI, Sun Studio CC
30#include <cmath>
31#include <tuple>
32
33#ifdef LIBMESH_HAVE_METAPHYSICL
34#include "metaphysicl/dualnumber_decl.h"
35#endif
36
37namespace libMesh
38{
39
40// Forward declarations
41template <typename T> class TypeTensorColumn;
42template <typename T> class ConstTypeTensorColumn;
43template <unsigned int N, typename T> class TypeNTensor;
44
52template <typename T>
54{
55 template <typename T2>
56 friend class TypeTensor;
57
58public:
64
65protected:
72 explicit TypeTensor (const T & xx,
73 const T & xy=0,
74 const T & xz=0,
75 const T & yx=0,
76 const T & yy=0,
77 const T & yz=0,
78 const T & zx=0,
79 const T & zy=0,
80 const T & zz=0);
81
82
86 template <typename Scalar>
87 explicit TypeTensor (const Scalar & xx,
88 const Scalar & xy=0,
89 const Scalar & xz=0,
90 const Scalar & yx=0,
91 const Scalar & yy=0,
92 const Scalar & yz=0,
93 const Scalar & zx=0,
94 const Scalar & zy=0,
95 typename
96 std::enable_if<ScalarTraits<Scalar>::value,
97 const Scalar>::type & zz=0);
98
104 template<typename T2>
105 LIBMESH_DEVICE_INLINE
107
108 template<typename T2>
109 LIBMESH_DEVICE_INLINE
111 const TypeVector<T2> & vy);
112
113 template<typename T2>
114 LIBMESH_DEVICE_INLINE
116 const TypeVector<T2> & vy,
117 const TypeVector<T2> & vz);
118
119public:
120
124 typedef T value_type;
125
129 typedef std::tuple<unsigned int, unsigned int> index_type;
130
134 template<typename T2>
136
140 ~TypeTensor() = default;
141
145 template<typename T2>
146 LIBMESH_DEVICE_INLINE
147 void assign (const TypeTensor<T2> &);
148
154 template <typename Scalar>
155 LIBMESH_DEVICE_INLINE
156 typename std::enable_if<
158 TypeTensor &>::type
159 operator = (const Scalar & libmesh_dbg_var(p))
160 { libmesh_assert_equal_to (p, Scalar(0)); this->zero(); return *this; }
161
165 const T & operator () (const unsigned int i, const unsigned int j) const;
166
170 T & operator () (const unsigned int i, const unsigned int j);
171
175 LIBMESH_DEVICE_INLINE
176 ConstTypeTensorColumn<T> slice (const unsigned int i) const;
177
181 LIBMESH_DEVICE_INLINE
182 TypeTensorColumn<T> slice (const unsigned int i);
183
187 TypeVector<T> row(const unsigned int r) const;
188
192 LIBMESH_DEVICE_INLINE
193 TypeVector<T> column(const unsigned int r) const;
194
200 template<typename T2>
203
209 template<typename T2>
211
215 template<typename T2>
216 void add (const TypeTensor<T2> &);
217
221 template <typename T2>
222 LIBMESH_DEVICE_INLINE
223 void add_scaled (const TypeTensor<T2> &, const T &);
224
230 template<typename T2>
233
239 template<typename T2>
241
245 template<typename T2>
246 void subtract (const TypeTensor<T2> &);
247
252 template <typename T2>
253 LIBMESH_DEVICE_INLINE
254 void subtract_scaled (const TypeTensor<T2> &, const T &);
255
260
266 template <typename Scalar>
267 auto
268 operator * (const Scalar & scalar) const -> typename std::enable_if<
270 TypeTensor<decltype(T() * scalar)>>::type;
271
277 template <typename Scalar, typename std::enable_if<
278 ScalarTraits<Scalar>::value, int>::type = 0>
279 LIBMESH_DEVICE_INLINE
280 const TypeTensor<T> & operator *= (const Scalar & factor)
281 {
282 for (unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
283 _coords[i] *= factor;
284
285 return *this;
286 }
287
293 template <typename Scalar>
294 typename std::enable_if<
297 operator / (const Scalar &) const;
298
304 const TypeTensor<T> & operator /= (const T &);
305
312 template <typename T2>
315
321 template <typename T2>
323
333 template <typename T2>
334 LIBMESH_DEVICE_INLINE
336 contract (const TypeTensor<T2> &) const;
337
344 template <typename T2>
347
354 template <typename T2>
355 LIBMESH_DEVICE_INLINE
357 left_multiply (const TypeVector<T2> & p) const;
358
363
368
375 LIBMESH_DEVICE_INLINE
376 void solve(const TypeVector<T> & b, TypeVector<T> & x) const;
377
382 auto norm() const;
383
388 auto norm_sq() const;
389
393 LIBMESH_DEVICE_INLINE
394 bool is_zero() const;
395
402 T det() const;
403
407 T tr() const;
408
412 LIBMESH_DEVICE_INLINE
413 void zero();
414
418 LIBMESH_DEVICE_INLINE
419 bool operator == (const TypeTensor<T> & rhs) const;
420
425 bool operator < (const TypeTensor<T> & rhs) const;
426
430 bool operator > (const TypeTensor<T> & rhs) const;
431
435 void print(std::ostream & os = libMesh::out) const;
436
444 friend std::ostream & operator << (std::ostream & os, const TypeTensor<T> & t)
445 {
446 t.print(os);
447 return os;
448 }
449
454 void write_unformatted (std::ostream & out_stream,
455 const bool newline = true) const;
456
462 bool is_hpd(Real rel_tol = TOLERANCE*TOLERANCE) const;
463
464protected:
465
469 T _coords[LIBMESH_DIM*LIBMESH_DIM];
470};
471
472
473template <typename T>
475{
476public:
477
479 unsigned int j) :
480 _tensor(&tensor), _j(j) {}
481
486 T & operator () (const unsigned int i)
487 { return (*_tensor)(i,_j); }
488
489 T & slice (const unsigned int i)
490 { return (*_tensor)(i,_j); }
491
496 {
497 for (unsigned int i=0; i != LIBMESH_DIM; ++i)
498 (*this)(i) = rhs(i);
499 return *this;
500 }
501
502private:
504 const unsigned int _j;
505};
506
507
508template <typename T>
510{
511public:
512
514 unsigned int j) :
515 _tensor(&tensor), _j(j) {}
516
520 const T & operator () (const unsigned int i) const
521 { return (*_tensor)(i,_j); }
522
523 const T & slice (const unsigned int i) const
524 { return (*_tensor)(i,_j); }
525
526private:
528 const unsigned int _j;
529};
530
531//------------------------------------------------------
532// Inline functions
533template <typename T>
534LIBMESH_DEVICE_INLINE
536{
537 _coords[0] = {};
538
539#if LIBMESH_DIM > 1
540 _coords[1] = {};
541 _coords[2] = {};
542 _coords[3] = {};
543#endif
544
545#if LIBMESH_DIM > 2
546 _coords[4] = {};
547 _coords[5] = {};
548 _coords[6] = {};
549 _coords[7] = {};
550 _coords[8] = {};
551#endif
552}
553
554
555
556template <typename T>
557LIBMESH_DEVICE_INLINE
559 const T & xy,
560 const T & xz,
561 const T & yx,
562 const T & yy,
563 const T & yz,
564 const T & zx,
565 const T & zy,
566 const T & zz)
567{
568 _coords[0] = xx;
569
570#if LIBMESH_DIM == 2
571 _coords[1] = xy;
572 _coords[2] = yx;
573 _coords[3] = yy;
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);
578 libmesh_ignore(xy, yx, yy);
579#endif
580
581#if LIBMESH_DIM == 3
582 _coords[1] = xy;
583 _coords[2] = xz;
584 _coords[3] = yx;
585 _coords[4] = yy;
586 _coords[5] = yz;
587 _coords[6] = zx;
588 _coords[7] = zy;
589 _coords[8] = zz;
590#else
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);
596 libmesh_ignore(xz, yz, zx, zy, zz);
597#endif
598}
599
600
601template <typename T>
602template <typename Scalar>
603LIBMESH_DEVICE_INLINE
604TypeTensor<T>::TypeTensor (const Scalar & xx,
605 const Scalar & xy,
606 const Scalar & xz,
607 const Scalar & yx,
608 const Scalar & yy,
609 const Scalar & yz,
610 const Scalar & zx,
611 const Scalar & zy,
612 typename
613 std::enable_if<ScalarTraits<Scalar>::value,
614 const Scalar>::type & zz)
615{
616 _coords[0] = xx;
617
618#if LIBMESH_DIM == 2
619 _coords[1] = xy;
620 _coords[2] = yx;
621 _coords[3] = yy;
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);
626 libmesh_ignore(xy, yx, yy);
627#endif
628
629#if LIBMESH_DIM == 3
630 _coords[1] = xy;
631 _coords[2] = xz;
632 _coords[3] = yx;
633 _coords[4] = yy;
634 _coords[5] = yz;
635 _coords[6] = zx;
636 _coords[7] = zy;
637 _coords[8] = zz;
638#else
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);
644 libmesh_ignore(xz, yz, zx, zy, zz);
645#endif
646}
647
648
649
650template <typename T>
651template<typename T2>
652LIBMESH_DEVICE_INLINE
653TypeTensor<T>::TypeTensor (const TypeTensor <T2> & p)
654{
655 // copy the nodes from vector p to me
656 for (unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
657 _coords[i] = p._coords[i];
658}
659
660
661template <typename T>
662template <typename T2>
663LIBMESH_DEVICE_INLINE
665{
666 libmesh_assert_equal_to (LIBMESH_DIM, 1);
667 _coords[0] = vx(0);
668}
669
670template <typename T>
671template <typename T2>
672LIBMESH_DEVICE_INLINE
674 const TypeVector<T2> & vy)
675{
676 libmesh_assert_equal_to (LIBMESH_DIM, 2);
677#if LIBMESH_DIM > 1
678 _coords[0] = vx(0);
679 _coords[1] = vx(1);
680 _coords[2] = vy(0);
681 _coords[3] = vy(1);
682#else
683 libmesh_ignore(vx, vy);
684#endif
685}
686
687template <typename T>
688template <typename T2>
689LIBMESH_DEVICE_INLINE
691 const TypeVector<T2> & vy,
692 const TypeVector<T2> & vz)
693{
694 libmesh_assert_equal_to (LIBMESH_DIM, 3);
695#if LIBMESH_DIM > 2
696 _coords[0] = vx(0);
697 _coords[1] = vx(1);
698 _coords[2] = vx(2);
699 _coords[3] = vy(0);
700 _coords[4] = vy(1);
701 _coords[5] = vy(2);
702 _coords[6] = vz(0);
703 _coords[7] = vz(1);
704 _coords[8] = vz(2);
705#else
706 libmesh_ignore(vx, vy, vz);
707#endif
708}
709
710
711
712
713template <typename T>
714template<typename T2>
715LIBMESH_DEVICE_INLINE
717{
718 for (unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
719 _coords[i] = p._coords[i];
720}
721
722
723
724template <typename T>
725LIBMESH_DEVICE_INLINE
726const T & TypeTensor<T>::operator () (const unsigned int i,
727 const unsigned int j) const
728{
729 libmesh_assert_less (i, 3);
730 libmesh_assert_less (j, 3);
731
732#if LIBMESH_DIM < 3
733 const static T my_zero = 0;
734 if (i >= LIBMESH_DIM || j >= LIBMESH_DIM)
735 return my_zero;
736#endif
737
738 return _coords[i*LIBMESH_DIM+j];
739}
740
741
742
743template <typename T>
744LIBMESH_DEVICE_INLINE
745T & TypeTensor<T>::operator () (const unsigned int i,
746 const unsigned int j)
747{
748#if LIBMESH_DIM < 3
749
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!");
752
753#endif
754
755 libmesh_assert_less (i, LIBMESH_DIM);
756 libmesh_assert_less (j, LIBMESH_DIM);
757
758 return _coords[i*LIBMESH_DIM+j];
759}
760
761
762template <typename T>
763LIBMESH_DEVICE_INLINE
765TypeTensor<T>::slice (const unsigned int i) const
766{
767 libmesh_assert_less (i, LIBMESH_DIM);
768 return ConstTypeTensorColumn<T>(*this, i);
769}
770
771
772template <typename T>
773LIBMESH_DEVICE_INLINE
775TypeTensor<T>::slice (const unsigned int i)
776{
777 libmesh_assert_less (i, LIBMESH_DIM);
778 return TypeTensorColumn<T>(*this, i);
779}
780
781
782template <typename T>
783LIBMESH_DEVICE_INLINE
785TypeTensor<T>::row(const unsigned int r) const
786{
787 TypeVector<T> return_vector;
788
789 for (unsigned int j=0; j<LIBMESH_DIM; j++)
790 return_vector._coords[j] = _coords[r*LIBMESH_DIM + j];
791
792 return return_vector;
793}
794
795
796template <typename T>
797LIBMESH_DEVICE_INLINE
799TypeTensor<T>::column(const unsigned int r) const
800{
801 TypeVector<T> return_vector;
802
803 for (unsigned int i=0; i<LIBMESH_DIM; i++)
804 return_vector._coords[i] = _coords[i*LIBMESH_DIM + r];
805
806 return return_vector;
807}
808
809
810template <typename T>
811template<typename T2>
812LIBMESH_DEVICE_INLINE
815{
816
817#if LIBMESH_DIM == 1
819#endif
820
821#if LIBMESH_DIM == 2
823 _coords[1] + p._coords[1],
824 0.,
825 _coords[2] + p._coords[2],
826 _coords[3] + p._coords[3]);
827#endif
828
829#if LIBMESH_DIM == 3
831 _coords[1] + p._coords[1],
832 _coords[2] + p._coords[2],
833 _coords[3] + p._coords[3],
834 _coords[4] + p._coords[4],
835 _coords[5] + p._coords[5],
836 _coords[6] + p._coords[6],
837 _coords[7] + p._coords[7],
838 _coords[8] + p._coords[8]);
839#endif
840
841}
842
843
844
845template <typename T>
846template<typename T2>
847LIBMESH_DEVICE_INLINE
849{
850 this->add (p);
851
852 return *this;
853}
854
855
856
857template <typename T>
858template<typename T2>
859LIBMESH_DEVICE_INLINE
861{
862 for (unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
863 _coords[i] += p._coords[i];
864}
865
866
867
868template <typename T>
869template <typename T2>
870LIBMESH_DEVICE_INLINE
871void TypeTensor<T>::add_scaled (const TypeTensor<T2> & p, const T & factor)
872{
873 for (unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
874 _coords[i] += factor*p._coords[i];
875
876}
877
878
879
880template <typename T>
881template<typename T2>
882LIBMESH_DEVICE_INLINE
885{
886
887#if LIBMESH_DIM == 1
889#endif
890
891#if LIBMESH_DIM == 2
893 _coords[1] - p._coords[1],
894 0.,
895 _coords[2] - p._coords[2],
896 _coords[3] - p._coords[3]);
897#endif
898
899#if LIBMESH_DIM == 3
901 _coords[1] - p._coords[1],
902 _coords[2] - p._coords[2],
903 _coords[3] - p._coords[3],
904 _coords[4] - p._coords[4],
905 _coords[5] - p._coords[5],
906 _coords[6] - p._coords[6],
907 _coords[7] - p._coords[7],
908 _coords[8] - p._coords[8]);
909#endif
910
911}
912
913
914
915template <typename T>
916template <typename T2>
917LIBMESH_DEVICE_INLINE
919{
920 this->subtract (p);
921
922 return *this;
923}
924
925
926
927template <typename T>
928template <typename T2>
929LIBMESH_DEVICE_INLINE
931{
932 for (unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
933 _coords[i] -= p._coords[i];
934}
935
936
937
938template <typename T>
939template <typename T2>
940LIBMESH_DEVICE_INLINE
941void TypeTensor<T>::subtract_scaled (const TypeTensor<T2> & p, const T & factor)
942{
943 for (unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
944 _coords[i] -= factor*p._coords[i];
945}
946
947
948
949template <typename T>
950LIBMESH_DEVICE_INLINE
952{
953
954#if LIBMESH_DIM == 1
955 return TypeTensor(-_coords[0]);
956#endif
957
958#if LIBMESH_DIM == 2
959 return TypeTensor(-_coords[0],
960 -_coords[1],
961 -_coords[2],
962 -_coords[3]);
963#endif
964
965#if LIBMESH_DIM == 3
966 return TypeTensor(-_coords[0],
967 -_coords[1],
968 -_coords[2],
969 -_coords[3],
970 -_coords[4],
971 -_coords[5],
972 -_coords[6],
973 -_coords[7],
974 -_coords[8]);
975#endif
976
977}
978
979
980
981template <typename T>
982template <typename Scalar>
983LIBMESH_DEVICE_INLINE
984auto
985TypeTensor<T>::operator * (const Scalar & factor) const -> typename std::enable_if<
987 TypeTensor<decltype(T() * factor)>>::type
988{
989 typedef decltype((*this)(0, 0) * factor) TS;
990
991
992#if LIBMESH_DIM == 1
993 return TypeTensor<TS>(_coords[0]*factor);
994#endif
995
996#if LIBMESH_DIM == 2
997 return TypeTensor<TS>(_coords[0]*factor,
998 _coords[1]*factor,
999 _coords[2]*factor,
1000 _coords[3]*factor);
1001#endif
1002
1003#if LIBMESH_DIM == 3
1004 return TypeTensor<TS>(_coords[0]*factor,
1005 _coords[1]*factor,
1006 _coords[2]*factor,
1007 _coords[3]*factor,
1008 _coords[4]*factor,
1009 _coords[5]*factor,
1010 _coords[6]*factor,
1011 _coords[7]*factor,
1012 _coords[8]*factor);
1013#endif
1014}
1015
1016
1017
1018template <typename T, typename Scalar>
1019LIBMESH_DEVICE_INLINE
1020typename std::enable_if<
1023operator * (const Scalar & factor,
1024 const TypeTensor<T> & t)
1025{
1026 return t * factor;
1027}
1028
1029template <typename T>
1030template <typename Scalar>
1031LIBMESH_DEVICE_INLINE
1032typename std::enable_if<
1034 TypeTensor<typename CompareTypes<T, Scalar>::supertype>>::type
1035TypeTensor<T>::operator / (const Scalar & factor) const
1036{
1037 libmesh_assert_not_equal_to (factor, static_cast<T>(0.));
1038
1039 typedef typename CompareTypes<T, Scalar>::supertype TS;
1040
1041#if LIBMESH_DIM == 1
1042 return TypeTensor<TS>(_coords[0]/factor);
1043#endif
1044
1045#if LIBMESH_DIM == 2
1046 return TypeTensor<TS>(_coords[0]/factor,
1047 _coords[1]/factor,
1048 _coords[2]/factor,
1049 _coords[3]/factor);
1050#endif
1051
1052#if LIBMESH_DIM == 3
1053 return TypeTensor<TS>(_coords[0]/factor,
1054 _coords[1]/factor,
1055 _coords[2]/factor,
1056 _coords[3]/factor,
1057 _coords[4]/factor,
1058 _coords[5]/factor,
1059 _coords[6]/factor,
1060 _coords[7]/factor,
1061 _coords[8]/factor);
1062#endif
1063
1064}
1065
1066
1067
1068template <typename T>
1069LIBMESH_DEVICE_INLINE
1071{
1072#if LIBMESH_DIM == 1
1073 return TypeTensor(_coords[0]);
1074#endif
1075
1076#if LIBMESH_DIM == 2
1077 return TypeTensor(_coords[0],
1078 _coords[2],
1079 _coords[1],
1080 _coords[3]);
1081#endif
1082
1083#if LIBMESH_DIM == 3
1084 return TypeTensor(_coords[0],
1085 _coords[3],
1086 _coords[6],
1087 _coords[1],
1088 _coords[4],
1089 _coords[7],
1090 _coords[2],
1091 _coords[5],
1092 _coords[8]);
1093#endif
1094}
1095
1096
1097
1098template <typename T>
1099LIBMESH_DEVICE_INLINE
1101{
1102#if LIBMESH_DIM == 1
1103 if (_coords[0] == static_cast<T>(0.))
1104 libmesh_convergence_failure();
1105 return TypeTensor(1. / _coords[0]);
1106#endif
1107
1108#if LIBMESH_DIM == 2
1109 // Get convenient reference to this.
1110 const TypeTensor<T> & A = *this;
1111
1112 // Use temporary variables, avoid multiple accesses
1113 T a = A(0,0), b = A(0,1),
1114 c = A(1,0), d = A(1,1);
1115
1116 // Make sure det = ad - bc is not zero
1117 T my_det = a*d - b*c;
1118
1119 if (my_det == static_cast<T>(0.))
1120 libmesh_convergence_failure();
1121
1122 return TypeTensor(d/my_det, -b/my_det, -c/my_det, a/my_det);
1123#endif
1124
1125#if LIBMESH_DIM == 3
1126 // Get convenient reference to this.
1127 const TypeTensor<T> & A = *this;
1128
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);
1132
1133 T my_det = a11*(a33*a22-a32*a23) - a21*(a33*a12-a32*a13) + a31*(a23*a12-a22*a13);
1134
1135 if (my_det == static_cast<T>(0.))
1136 libmesh_convergence_failure();
1137
1138 // Inline comment characters are for lining up columns.
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);
1142#endif
1143}
1144
1145
1146
1147template <typename T>
1148LIBMESH_DEVICE_INLINE
1150{
1151#if LIBMESH_DIM == 1
1152 if (_coords[0] == static_cast<T>(0.))
1153 libmesh_convergence_failure();
1154 x(0) = b(0) / _coords[0];
1155#endif
1156
1157#if LIBMESH_DIM == 2
1158 T my_det = _coords[0]*_coords[3] - _coords[1]*_coords[2];
1159
1160 if (my_det == static_cast<T>(0.))
1161 libmesh_convergence_failure();
1162
1163 T my_det_inv = 1./my_det;
1164
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));
1167#endif
1168
1169#if LIBMESH_DIM == 3
1170 T my_det =
1171 // a11*(a33 *a22 - a32 *a23)
1172 _coords[0]*(_coords[8]*_coords[4] - _coords[7]*_coords[5])
1173 // -a21*(a33 *a12 - a32 *a13)
1174 -_coords[3]*(_coords[8]*_coords[1] - _coords[7]*_coords[2]) +
1175 // +a31*(a23 *a12 - a22 *a13)
1176 +_coords[6]*(_coords[5]*_coords[1] - _coords[4]*_coords[2]);
1177
1178 if (my_det == static_cast<T>(0.))
1179 libmesh_convergence_failure();
1180
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));
1185
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));
1189
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));
1193#endif
1194}
1195
1196
1197
1198template <typename T>
1199LIBMESH_DEVICE_INLINE
1201{
1202 libmesh_assert_not_equal_to (factor, static_cast<T>(0.));
1203
1204 for (unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
1205 _coords[i] /= factor;
1206
1207 return *this;
1208}
1209
1210
1211
1212
1213template <typename T>
1214template <typename T2>
1215LIBMESH_DEVICE_INLINE
1218{
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);
1223
1224 return returnval;
1225}
1226
1227template <typename T>
1228template <typename T2>
1229LIBMESH_DEVICE_INLINE
1232{
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);
1237
1238 return returnval;
1239}
1240
1241template <typename T, typename T2>
1242LIBMESH_DEVICE_INLINE
1245{
1246 return b.left_multiply(a);
1247}
1248
1249template <typename T>
1250template <typename T2>
1251LIBMESH_DEVICE_INLINE
1252TypeTensor<typename CompareTypes<T, T2>::supertype>
1254{
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);
1260
1261 return returnval;
1262}
1263
1264template <typename T>
1265template <typename T2>
1266LIBMESH_DEVICE_INLINE
1268{
1269 TypeTensor<T> temp;
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);
1274
1275 this->assign(temp);
1276 return *this;
1277}
1278
1279
1284template <typename T>
1285template <typename T2>
1286LIBMESH_DEVICE_INLINE
1289{
1290 typename CompareTypes<T,T2>::supertype sum = 0.;
1291 for (unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
1292 sum += _coords[i]*t._coords[i];
1293 return sum;
1294}
1295
1296
1297
1298template <typename T>
1299LIBMESH_DEVICE_INLINE
1301{
1302 using portable_math::sqrt;
1303 return sqrt(this->norm_sq());
1304}
1305
1306
1307template <typename T>
1308LIBMESH_DEVICE_INLINE
1310{
1311 for (const auto & val : _coords)
1312 if (val != T(0))
1313 return false;
1314 return true;
1315}
1316
1317template <typename T>
1318LIBMESH_DEVICE_INLINE
1320{
1321#if LIBMESH_DIM == 1
1322 return _coords[0];
1323#endif
1324
1325#if LIBMESH_DIM == 2
1326 return (_coords[0] * _coords[3]
1327 - _coords[1] * _coords[2]);
1328#endif
1329
1330#if LIBMESH_DIM == 3
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]);
1337#endif
1338}
1339
1340template <typename T>
1341LIBMESH_DEVICE_INLINE
1343{
1344#if LIBMESH_DIM == 1
1345 return _coords[0];
1346#endif
1347
1348#if LIBMESH_DIM == 2
1349 return _coords[0] + _coords[3];
1350#endif
1351
1352#if LIBMESH_DIM == 3
1353 return _coords[0] + _coords[4] + _coords[8];
1354#endif
1355}
1356
1357template <typename T>
1358LIBMESH_DEVICE_INLINE
1360{
1361 for (unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
1362 _coords[i] = 0.;
1363}
1364
1365
1366
1367template <typename T>
1368LIBMESH_DEVICE_INLINE
1370{
1371 Real sum = 0.;
1372 for (unsigned int i=0; i<LIBMESH_DIM*LIBMESH_DIM; i++)
1373 sum += TensorTools::norm_sq(_coords[i]);
1374 return sum;
1375}
1376
1377
1378
1379template <typename T>
1380LIBMESH_DEVICE_INLINE
1382{
1383 using portable_math::abs;
1384
1385#if LIBMESH_DIM == 1
1386 return (abs(_coords[0] - rhs._coords[0])
1387 < TOLERANCE);
1388#endif
1389
1390#if LIBMESH_DIM == 2
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]))
1395 < 4.*TOLERANCE);
1396#endif
1397
1398#if LIBMESH_DIM == 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]))
1408 < 9.*TOLERANCE);
1409#endif
1410
1411}
1412
1413template <typename T>
1414void TypeTensor<T>::print(std::ostream & os) const
1415{
1416#if LIBMESH_DIM == 1
1417
1418 os << "x=" << (*this)(0,0) << std::endl;
1419
1420#endif
1421#if LIBMESH_DIM == 2
1422
1423 os << "(xx,xy)=("
1424 << std::setw(8) << (*this)(0,0) << ", "
1425 << std::setw(8) << (*this)(0,1) << ")"
1426 << std::endl;
1427 os << "(yx,yy)=("
1428 << std::setw(8) << (*this)(1,0) << ", "
1429 << std::setw(8) << (*this)(1,1) << ")"
1430 << std::endl;
1431
1432#endif
1433#if LIBMESH_DIM == 3
1434
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) << ")"
1439 << std::endl;
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) << ")"
1444 << std::endl;
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) << ")"
1449 << std::endl;
1450#endif
1451}
1452
1453
1454// A matrix is finite iff every component is
1455template <typename T>
1456bool isfinite (const TypeTensor<T> & var)
1457{
1458 using std::isfinite;
1459 using libMesh::isfinite; // for T==complex
1460 for (auto i : make_range(LIBMESH_DIM))
1461 for (auto j : make_range(LIBMESH_DIM))
1462 if (!isfinite(var(i,j)))
1463 return false;
1464 return true;
1465}
1466
1467
1468// A matrix is infinite iff some component is infinite but no
1469// component is NaN.
1470//
1471// This is arguably inconsistent with our std::complex overload (and
1472// the C99 Annex G recommendations for _Complex, and C++ std::complex
1473// arithmetic), which treats mixed (inf,NaN) pairs as infinite, but
1474// this is probably safer for users.
1475template <typename T>
1476bool isinf (const TypeTensor<T> & var)
1477{
1478 using std::isinf;
1479 using libMesh::isinf; // for T==complex
1480 using std::isnan;
1481 using libMesh::isnan;
1482 bool has_inf = false;
1483 for (auto i : make_range(LIBMESH_DIM))
1484 for (auto j : make_range(LIBMESH_DIM))
1485 {
1486 // NaN anywhere makes us NaN, not inf
1487 if (isnan(var(i,j)))
1488 return false;
1489 has_inf = has_inf || isinf(var(i,j));
1490 }
1491 return has_inf;
1492}
1493
1494
1495
1496// A matrix is NaN iff some component is NaN
1497//
1498// This is arguably inconsistent with our std::complex overload (and
1499// the C99 Annex G recommendations for _Complex, and C++ std::complex
1500// arithmetic), which treats mixed (inf,NaN) pairs as infinite, but
1501// this is probably safer for users.
1502template <typename T>
1503bool isnan (const TypeTensor<T> & var)
1504{
1505 using std::isnan;
1506 using libMesh::isnan; // for T==complex
1507 for (auto i : make_range(LIBMESH_DIM))
1508 for (auto j : make_range(LIBMESH_DIM))
1509 if (isnan(var(i,j)))
1510 return true;
1511 return false;
1512}
1513
1514
1515
1516template <typename T, typename T2>
1517LIBMESH_DEVICE_INLINE
1518TypeTensor<typename CompareTypes<T, T2>::supertype>
1520{
1522 for (unsigned int i=0; i<LIBMESH_DIM; i++)
1523 for (unsigned int j=0; j<LIBMESH_DIM; j++)
1524 ret(i,j) = a(i) * libmesh_conj(b(j));
1525
1526 return ret;
1527}
1528
1529
1530} // namespace libMesh
1531
1532#ifdef LIBMESH_HAVE_METAPHYSICL
1533namespace MetaPhysicL
1534{
1535template <typename T>
1536struct RawType<libMesh::TypeTensor<T>>
1537{
1539
1541 {
1542 value_type ret;
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));
1546
1547 return ret;
1548 }
1549};
1550
1551template <typename T, typename U>
1552struct ReplaceAlgebraicType<libMesh::TypeTensor<T>, U>
1553{
1554 typedef U type;
1555};
1556}
1557#endif
1558
1559#endif // LIBMESH_TYPE_TENSOR_H
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)
const 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.
Definition type_tensor.h:54
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
friend class TypeTensor
Definition type_tensor.h:56
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.
auto norm_sq() const
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.
Definition type_tensor.C:39
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...
Definition type_tensor.C:67
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.
Definition type_vector.h:64
T _coords[LIBMESH_DIM]
The coordinates of the TypeVector.
static const Real b
LIBMESH_DEVICE_INLINE auto norm_sq(const T &a)
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)
OStreamProxy out
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...
Definition int_range.h:176
LIBMESH_DEVICE_INLINE T libmesh_conj(T a)
libMesh::TypeTensor< typename RawType< T >::value_type > value_type
static value_type value(const libMesh::TypeTensor< T > &in)
static const bool value