libMesh
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | List of all members
TypeTensorTest Class Reference
Inheritance diagram for TypeTensorTest:
[legend]

Public Member Functions

void setUp ()
 
void tearDown ()
 
 LIBMESH_CPPUNIT_TEST_SUITE (TypeTensorTest)
 
 CPPUNIT_TEST (testInverse)
 
 CPPUNIT_TEST (testLeftMultiply)
 
 CPPUNIT_TEST (testRotation)
 
 CPPUNIT_TEST (testClassifiers)
 
 CPPUNIT_TEST (testRowCol)
 
 CPPUNIT_TEST (testIsZero)
 
 CPPUNIT_TEST (testIsHPD)
 
 CPPUNIT_TEST (testReplaceAlgebraicType)
 
 CPPUNIT_TEST_SUITE_END ()
 

Private Member Functions

void testInverse ()
 
void testLeftMultiply ()
 
void testOuterProduct ()
 
void testClassifiers ()
 
void testIsZero ()
 
void testIsHPD ()
 
void testRotation ()
 
void testRowCol ()
 
void testReplaceAlgebraicType ()
 

Detailed Description

Definition at line 11 of file type_tensor_test.C.

Member Function Documentation

◆ CPPUNIT_TEST() [1/8]

TypeTensorTest::CPPUNIT_TEST ( testClassifiers  )

◆ CPPUNIT_TEST() [2/8]

TypeTensorTest::CPPUNIT_TEST ( testInverse  )

◆ CPPUNIT_TEST() [3/8]

TypeTensorTest::CPPUNIT_TEST ( testIsHPD  )

◆ CPPUNIT_TEST() [4/8]

TypeTensorTest::CPPUNIT_TEST ( testIsZero  )

◆ CPPUNIT_TEST() [5/8]

TypeTensorTest::CPPUNIT_TEST ( testLeftMultiply  )

◆ CPPUNIT_TEST() [6/8]

TypeTensorTest::CPPUNIT_TEST ( testReplaceAlgebraicType  )

◆ CPPUNIT_TEST() [7/8]

TypeTensorTest::CPPUNIT_TEST ( testRotation  )

◆ CPPUNIT_TEST() [8/8]

TypeTensorTest::CPPUNIT_TEST ( testRowCol  )

◆ CPPUNIT_TEST_SUITE_END()

TypeTensorTest::CPPUNIT_TEST_SUITE_END ( )

◆ LIBMESH_CPPUNIT_TEST_SUITE()

TypeTensorTest::LIBMESH_CPPUNIT_TEST_SUITE ( TypeTensorTest  )

◆ setUp()

void TypeTensorTest::setUp ( )
inline

Definition at line 14 of file type_tensor_test.C.

14{}

◆ tearDown()

void TypeTensorTest::tearDown ( )
inline

Definition at line 16 of file type_tensor_test.C.

16{}

◆ testClassifiers()

void TypeTensorTest::testClassifiers ( )
inlineprivate

Definition at line 95 of file type_tensor_test.C.

96 {
97 LOG_UNIT_TEST;
98
99 {
100 TensorValue<double> tensor;
101 CPPUNIT_ASSERT(isfinite(tensor));
102 CPPUNIT_ASSERT(!isinf(tensor));
103 CPPUNIT_ASSERT(!isnan(tensor));
104 }
105 {
106 TensorValue<double> tensor(0,1,0,2,3);
107 tensor(0,0) = std::numeric_limits<double>::infinity();
108 CPPUNIT_ASSERT(!isfinite(tensor));
109 CPPUNIT_ASSERT(isinf(tensor));
110 CPPUNIT_ASSERT(!isnan(tensor));
111 }
112 }
This class defines a tensor in LIBMESH_DIM dimensional Real or Complex space.
bool isfinite(std::complex< T > a)
bool isnan(std::complex< T > a)
bool isinf(std::complex< T > a)

References libMesh::isfinite(), libMesh::isinf(), and libMesh::isnan().

◆ testInverse()

void TypeTensorTest::testInverse ( )
inlineprivate

Definition at line 36 of file type_tensor_test.C.

37 {
38 LOG_UNIT_TEST;
39
40 // This random input tensor and its inverse came from Octave/Matlab:
41 // > format long e
42 // > A = rand(3)
43 // > inv(A)
44
45 // The base class, TypeTensor, has a protected constructor. We
46 // are using the derived class, TensorValue, for our tests...
47 TensorValue<double> tensor(9.08973348886179e-01, 3.36455579239923e-01, 5.16389236893863e-01,
48 9.44156071777472e-01, 1.35610910092516e-01, 1.49881119060538e-02,
49 1.15988384086146e-01, 6.79845197685518e-03, 3.77028969454745e-01);
50
51 TensorValue<double> inverse = tensor.inverse();
52
53 TensorValue<double> true_inverse(-6.57484735104482e-01, 1.58926633961497e+00, 8.37330721137561e-01,
54 4.56430940967411e+00, -3.64404559823061e+00, -6.10654107858520e+00,
55 1.19965194510943e-01, -4.23210359257434e-01, 2.50483242797707e+00);
56
57 for (unsigned i=0; i<3; ++i)
58 for (unsigned j=0; j<3; ++j)
59 LIBMESH_ASSERT_FP_EQUAL(inverse(i,j), true_inverse(i,j), 1e-12);
60 }
TypeTensor< T > inverse() const

References libMesh::TypeTensor< T >::inverse().

◆ testIsHPD()

void TypeTensorTest::testIsHPD ( )
inlineprivate

Definition at line 128 of file type_tensor_test.C.

129 {
130 LOG_UNIT_TEST;
131
132 {
133#if LIBMESH_DIM == 3
134 {
135 TensorValue<double> tensor(2., 1., 0.,
136 1., 2., 1.,
137 0., 1., 2.);
138
139 CPPUNIT_ASSERT(tensor.is_hpd(/*rel_tol=*/0.));
140 }
141 {
142 // Symmetric but not positive-definite, because the upper 2x2
143 // principal minor is zero.
144 TensorValue<double> tensor(1, 0., 0.,
145 0., 0., 1.,
146 0., 1., 0.);
147
148 CPPUNIT_ASSERT(!tensor.is_hpd());
149 }
150 {
151 // Random matrix that is SPD by construction
152
153 auto get_random = [&]()
154 {
155 return static_cast<Real>(std::rand()) / RAND_MAX; // in range [0,1]
156 };
157
158 TensorValue<double> tensor(get_random(), get_random(), get_random(),
159 get_random(), get_random(), get_random(),
160 get_random(), get_random(), get_random());
161
162 // Make symmetric
163 tensor = 0.5*(tensor + tensor.transpose());
164
165 // Make positive definite
166 for (auto i : make_range(LIBMESH_DIM))
167 tensor(i,i) += 3.;
168
169 CPPUNIT_ASSERT(tensor.is_hpd());
170 }
171
172#ifdef LIBMESH_USE_COMPLEX_NUMBERS
173
174 auto i = Complex(0, 1);
175 auto one = Complex(1, 0);
176 {
177 // Symmetric but not Hermitian, because Hermitian matrices must
178 // have real-valued entries on the diagonal.
179 TensorValue<Complex> tensor(i, 0., 0.,
180 0., 1., 0.,
181 0., 0., 1.);
182
183 CPPUNIT_ASSERT(!tensor.is_hpd());
184 }
185 {
186 // Symmetric but not Hermitian, because the off-diagonal
187 // entries must be complex conjugates of one another.
188 TensorValue<Complex> tensor(2, i, 0.,
189 i, 2., i,
190 0., i, 2.);
191
192 CPPUNIT_ASSERT(!tensor.is_hpd());
193 }
194 {
195 // Both Hermitian and positive-definite
196 TensorValue<Complex> tensor(2., one+i, 0.,
197 one-i, 2., one+i,
198 0., one-i, 2.);
199
200 CPPUNIT_ASSERT(tensor.is_hpd());
201 }
202
203#endif
204
205#endif // LIBMESH_DIM == 3
206 }
207 }
std::complex< Real > Complex
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

References libMesh::TypeTensor< T >::is_hpd(), libMesh::make_range(), libMesh::Real, and libMesh::TypeTensor< T >::transpose().

◆ testIsZero()

void TypeTensorTest::testIsZero ( )
inlineprivate

Definition at line 114 of file type_tensor_test.C.

115 {
116 LOG_UNIT_TEST;
117
118 {
119 TensorValue<double> tensor;
120 CPPUNIT_ASSERT(tensor.is_zero());
121 }
122 {
123 TensorValue<double> tensor(0,1,2,3,4,5,6,7,8);
124 CPPUNIT_ASSERT(!tensor.is_zero());
125 }
126 }
bool is_zero() const

References libMesh::TypeTensor< T >::is_zero().

◆ testLeftMultiply()

void TypeTensorTest::testLeftMultiply ( )
inlineprivate

Definition at line 62 of file type_tensor_test.C.

63 {
64 LOG_UNIT_TEST;
65
66 TensorValue<Real> tensor(1, 2, 0, 3, 4, 0);
67 VectorValue<Real> vector(5, 6, 0);
68 auto left_mult = vector * tensor;
69 auto right_mult = tensor * vector;
70 LIBMESH_ASSERT_FP_EQUAL(23, left_mult(0), 1e-12);
71 LIBMESH_ASSERT_FP_EQUAL(34, left_mult(1), 1e-12);
72 LIBMESH_ASSERT_FP_EQUAL(17, right_mult(0), 1e-12);
73 LIBMESH_ASSERT_FP_EQUAL(39, right_mult(1), 1e-12);
74 }
This class defines a vector in LIBMESH_DIM dimensional Real or Complex space.

◆ testOuterProduct()

void TypeTensorTest::testOuterProduct ( )
inlineprivate

Definition at line 76 of file type_tensor_test.C.

77 {
78 LOG_UNIT_TEST;
79
80 auto tol = TOLERANCE * TOLERANCE;
81 VectorValue<Real> a(2, 3, 4);
82 VectorValue<Real> b(5, 6, 7);
83 auto product = outer_product(a, b);
84 LIBMESH_ASSERT_FP_EQUAL(10, product(0, 0), tol);
85 LIBMESH_ASSERT_FP_EQUAL(12, product(0, 1), tol);
86 LIBMESH_ASSERT_FP_EQUAL(14, product(0, 2), tol);
87 LIBMESH_ASSERT_FP_EQUAL(15, product(1, 0), tol);
88 LIBMESH_ASSERT_FP_EQUAL(18, product(1, 1), tol);
89 LIBMESH_ASSERT_FP_EQUAL(21, product(1, 2), tol);
90 LIBMESH_ASSERT_FP_EQUAL(20, product(2, 0), tol);
91 LIBMESH_ASSERT_FP_EQUAL(24, product(2, 1), tol);
92 LIBMESH_ASSERT_FP_EQUAL(28, product(2, 2), tol);
93 }
static const Real b
TypeTensor< typename CompareTypes< T, T2 >::supertype > outer_product(const TypeVector< T > &a, const TypeVector< T2 > &b)
static constexpr Real TOLERANCE

References b, libMesh::outer_product(), and libMesh::TOLERANCE.

◆ testReplaceAlgebraicType()

void TypeTensorTest::testReplaceAlgebraicType ( )
inlineprivate

Definition at line 270 of file type_tensor_test.C.

271 {
272 typedef typename MetaPhysicL::ReplaceAlgebraicType<
273 std::vector<TypeTensor<double>>,
275 typename MetaPhysicL::ValueType<std::vector<TypeTensor<double>>>::type>::type>::type
276 ReplacedType;
277 constexpr bool assertion =
278 std::is_same<ReplacedType, std::vector<TypeNTensor<3,double>>>::value;
279 CPPUNIT_ASSERT(assertion);
280 }
static const bool value
Definition xdr_io.C:55

References value.

◆ testRotation()

void TypeTensorTest::testRotation ( )
inlineprivate

Definition at line 209 of file type_tensor_test.C.

210 {
211 LOG_UNIT_TEST;
212
213 {
214 Point x(1, 0, 0);
215 const auto R = RealTensorValue::extrinsic_rotation_matrix(90, 0, 0);
216 auto rotated = R * x;
217 constexpr auto tol = TOLERANCE * TOLERANCE;
218 LIBMESH_ASSERT_FP_EQUAL(0, rotated(0), tol);
219 LIBMESH_ASSERT_FP_EQUAL(1, rotated(1), tol);
220 LIBMESH_ASSERT_FP_EQUAL(0, rotated(2), tol);
221
222 const auto invR = RealTensorValue::inverse_extrinsic_rotation_matrix(90, 0, 0);
223 rotated = invR * rotated;
224 LIBMESH_ASSERT_FP_EQUAL(1, rotated(0), tol);
225 LIBMESH_ASSERT_FP_EQUAL(0, rotated(1), tol);
226 LIBMESH_ASSERT_FP_EQUAL(0, rotated(2), tol);
227 }
228
229 {
230 Point x(1, 1, 1);
231 const auto R = RealTensorValue::extrinsic_rotation_matrix(90, 90, 90);
232 auto rotated = R * x;
233
234 constexpr auto tol = TOLERANCE * TOLERANCE;
235 LIBMESH_ASSERT_FP_EQUAL(1, rotated(0), tol);
236 LIBMESH_ASSERT_FP_EQUAL(-1, rotated(1), tol);
237 LIBMESH_ASSERT_FP_EQUAL(1, rotated(2), tol);
238
239 const auto invR = RealTensorValue::inverse_extrinsic_rotation_matrix(90, 90, 90);
240 rotated = invR * rotated;
241 LIBMESH_ASSERT_FP_EQUAL(1, rotated(0), tol);
242 LIBMESH_ASSERT_FP_EQUAL(1, rotated(1), tol);
243 LIBMESH_ASSERT_FP_EQUAL(1, rotated(2), tol);
244 }
245 }
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition point.h:40
static TensorValue< Real > extrinsic_rotation_matrix(Real angle1_deg, Real angle2_deg, Real angle3_deg)
Generate the extrinsic rotation matrix associated with the provided Euler angles.
static TensorValue< Real > inverse_extrinsic_rotation_matrix(Real angle1_deg, Real angle2_deg, Real angle3_deg)
Invert the rotation that would occur if the same angles were provided to extrinsic_rotation_matrix,...

References libMesh::TensorValue< T >::extrinsic_rotation_matrix(), libMesh::TensorValue< T >::inverse_extrinsic_rotation_matrix(), and libMesh::TOLERANCE.

◆ testRowCol()

void TypeTensorTest::testRowCol ( )
inlineprivate

Definition at line 247 of file type_tensor_test.C.

248 {
249 LOG_UNIT_TEST;
250
252 for (unsigned int i = 0; i < LIBMESH_DIM; i++)
253 for (unsigned int j = 0; j < LIBMESH_DIM; j++)
254 t(i, j) = Real(i * LIBMESH_DIM + j);
255
256 constexpr Real tol = TOLERANCE * TOLERANCE;
257 for (unsigned int k = 0; k < LIBMESH_DIM; k++)
258 {
259 const auto row = t.row(k);
260 for (unsigned int l = 0; l < LIBMESH_DIM; l++)
261 LIBMESH_ASSERT_FP_EQUAL(Real(k * LIBMESH_DIM + l), row(l), tol);
262
263 const auto col = t.column(k);
264 for (unsigned int l = 0; l < LIBMESH_DIM; l++)
265 LIBMESH_ASSERT_FP_EQUAL(Real(l * LIBMESH_DIM + k), col(l), tol);
266 }
267 }
TypeVector< T > column(const unsigned int r) const
TypeVector< T > row(const unsigned int r) const

References libMesh::TypeTensor< T >::column(), libMesh::Real, libMesh::TypeTensor< T >::row(), and libMesh::TOLERANCE.


The documentation for this class was generated from the following file: