libMesh
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 (testRowCol)
 
 CPPUNIT_TEST (testIsZero)
 
 CPPUNIT_TEST (testIsHPD)
 
 CPPUNIT_TEST (testReplaceAlgebraicType)
 
 CPPUNIT_TEST_SUITE_END ()
 

Private Member Functions

void testInverse ()
 
void testLeftMultiply ()
 
void testOuterProduct ()
 
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/7]

TypeTensorTest::CPPUNIT_TEST ( testInverse  )

◆ CPPUNIT_TEST() [2/7]

TypeTensorTest::CPPUNIT_TEST ( testLeftMultiply  )

◆ CPPUNIT_TEST() [3/7]

TypeTensorTest::CPPUNIT_TEST ( testRotation  )

◆ CPPUNIT_TEST() [4/7]

TypeTensorTest::CPPUNIT_TEST ( testRowCol  )

◆ CPPUNIT_TEST() [5/7]

TypeTensorTest::CPPUNIT_TEST ( testIsZero  )

◆ CPPUNIT_TEST() [6/7]

TypeTensorTest::CPPUNIT_TEST ( testIsHPD  )

◆ CPPUNIT_TEST() [7/7]

TypeTensorTest::CPPUNIT_TEST ( testReplaceAlgebraicType  )

◆ 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 {}

◆ testInverse()

void TypeTensorTest::testInverse ( )
inlineprivate

Definition at line 35 of file type_tensor_test.C.

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

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

◆ testIsHPD()

void TypeTensorTest::testIsHPD ( )
inlineprivate

Definition at line 108 of file type_tensor_test.C.

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

109  {
110  LOG_UNIT_TEST;
111 
112  {
113 #if LIBMESH_DIM == 3
114  {
115  TensorValue<double> tensor(2., 1., 0.,
116  1., 2., 1.,
117  0., 1., 2.);
118 
119  CPPUNIT_ASSERT(tensor.is_hpd(/*rel_tol=*/0.));
120  }
121  {
122  // Symmetric but not positive-definite, because the upper 2x2
123  // principal minor is zero.
124  TensorValue<double> tensor(1, 0., 0.,
125  0., 0., 1.,
126  0., 1., 0.);
127 
128  CPPUNIT_ASSERT(!tensor.is_hpd());
129  }
130  {
131  // Random matrix that is SPD by construction
132 
133  auto get_random = [&]()
134  {
135  return static_cast<Real>(std::rand()) / RAND_MAX; // in range [0,1]
136  };
137 
138  TensorValue<double> tensor(get_random(), get_random(), get_random(),
139  get_random(), get_random(), get_random(),
140  get_random(), get_random(), get_random());
141 
142  // Make symmetric
143  tensor = 0.5*(tensor + tensor.transpose());
144 
145  // Make positive definite
146  for (auto i : make_range(LIBMESH_DIM))
147  tensor(i,i) += 3.;
148 
149  CPPUNIT_ASSERT(tensor.is_hpd());
150  }
151 
152 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
153 
154  auto i = Complex(0, 1);
155  auto one = Complex(1, 0);
156  {
157  // Symmetric but not Hermitian, because Hermitian matrices must
158  // have real-valued entries on the diagonal.
159  TensorValue<Complex> tensor(i, 0., 0.,
160  0., 1., 0.,
161  0., 0., 1.);
162 
163  CPPUNIT_ASSERT(!tensor.is_hpd());
164  }
165  {
166  // Symmetric but not Hermitian, because the off-diagonal
167  // entries must be complex conjugates of one another.
168  TensorValue<Complex> tensor(2, i, 0.,
169  i, 2., i,
170  0., i, 2.);
171 
172  CPPUNIT_ASSERT(!tensor.is_hpd());
173  }
174  {
175  // Both Hermitian and positive-definite
176  TensorValue<Complex> tensor(2., one+i, 0.,
177  one-i, 2., one+i,
178  0., one-i, 2.);
179 
180  CPPUNIT_ASSERT(tensor.is_hpd());
181  }
182 
183 #endif
184 
185 #endif // LIBMESH_DIM == 3
186  }
187  }
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:173
This class defines a tensor in LIBMESH_DIM dimensional Real or Complex space.

◆ testIsZero()

void TypeTensorTest::testIsZero ( )
inlineprivate

Definition at line 94 of file type_tensor_test.C.

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

95  {
96  LOG_UNIT_TEST;
97 
98  {
99  TensorValue<double> tensor;
100  CPPUNIT_ASSERT(tensor.is_zero());
101  }
102  {
103  TensorValue<double> tensor(0,1,2,3,4,5,6,7,8);
104  CPPUNIT_ASSERT(!tensor.is_zero());
105  }
106  }
bool is_zero() const
Definition: type_tensor.h:1296
This class defines a tensor in LIBMESH_DIM dimensional Real or Complex space.

◆ testLeftMultiply()

void TypeTensorTest::testLeftMultiply ( )
inlineprivate

Definition at line 61 of file type_tensor_test.C.

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

◆ testOuterProduct()

void TypeTensorTest::testOuterProduct ( )
inlineprivate

Definition at line 75 of file type_tensor_test.C.

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

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

◆ testReplaceAlgebraicType()

void TypeTensorTest::testReplaceAlgebraicType ( )
inlineprivate

Definition at line 250 of file type_tensor_test.C.

References value.

251  {
252  typedef typename MetaPhysicL::ReplaceAlgebraicType<
253  std::vector<TypeTensor<double>>,
255  typename MetaPhysicL::ValueType<std::vector<TypeTensor<double>>>::type>::type>::type
256  ReplacedType;
257  constexpr bool assertion =
258  std::is_same<ReplacedType, std::vector<TypeNTensor<3,double>>>::value;
259  CPPUNIT_ASSERT(assertion);
260  }
static const bool value
Definition: xdr_io.C:55

◆ testRotation()

void TypeTensorTest::testRotation ( )
inlineprivate

Definition at line 189 of file type_tensor_test.C.

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

190  {
191  LOG_UNIT_TEST;
192 
193  {
194  Point x(1, 0, 0);
195  const auto R = RealTensorValue::extrinsic_rotation_matrix(90, 0, 0);
196  auto rotated = R * x;
197  constexpr auto tol = TOLERANCE * TOLERANCE;
198  LIBMESH_ASSERT_FP_EQUAL(0, rotated(0), tol);
199  LIBMESH_ASSERT_FP_EQUAL(1, rotated(1), tol);
200  LIBMESH_ASSERT_FP_EQUAL(0, rotated(2), tol);
201 
202  const auto invR = RealTensorValue::inverse_extrinsic_rotation_matrix(90, 0, 0);
203  rotated = invR * rotated;
204  LIBMESH_ASSERT_FP_EQUAL(1, rotated(0), tol);
205  LIBMESH_ASSERT_FP_EQUAL(0, rotated(1), tol);
206  LIBMESH_ASSERT_FP_EQUAL(0, rotated(2), tol);
207  }
208 
209  {
210  Point x(1, 1, 1);
211  const auto R = RealTensorValue::extrinsic_rotation_matrix(90, 90, 90);
212  auto rotated = R * x;
213 
214  constexpr auto tol = TOLERANCE * TOLERANCE;
215  LIBMESH_ASSERT_FP_EQUAL(1, rotated(0), tol);
216  LIBMESH_ASSERT_FP_EQUAL(-1, rotated(1), tol);
217  LIBMESH_ASSERT_FP_EQUAL(1, rotated(2), tol);
218 
219  const auto invR = RealTensorValue::inverse_extrinsic_rotation_matrix(90, 90, 90);
220  rotated = invR * rotated;
221  LIBMESH_ASSERT_FP_EQUAL(1, rotated(0), tol);
222  LIBMESH_ASSERT_FP_EQUAL(1, rotated(1), tol);
223  LIBMESH_ASSERT_FP_EQUAL(1, rotated(2), tol);
224  }
225  }
static constexpr Real TOLERANCE
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39

◆ testRowCol()

void TypeTensorTest::testRowCol ( )
inlineprivate

Definition at line 227 of file type_tensor_test.C.

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

228  {
229  LOG_UNIT_TEST;
230 
232  for (unsigned int i = 0; i < LIBMESH_DIM; i++)
233  for (unsigned int j = 0; j < LIBMESH_DIM; j++)
234  t(i, j) = Real(i * LIBMESH_DIM + j);
235 
236  constexpr Real tol = TOLERANCE * TOLERANCE;
237  for (unsigned int k = 0; k < LIBMESH_DIM; k++)
238  {
239  const auto row = t.row(k);
240  for (unsigned int l = 0; l < LIBMESH_DIM; l++)
241  LIBMESH_ASSERT_FP_EQUAL(Real(k * LIBMESH_DIM + l), row(l), tol);
242 
243  const auto col = t.column(k);
244  for (unsigned int l = 0; l < LIBMESH_DIM; l++)
245  LIBMESH_ASSERT_FP_EQUAL(Real(l * LIBMESH_DIM + k), col(l), tol);
246  }
247  }
static constexpr Real TOLERANCE
TypeVector< T > row(const unsigned int r) const
Definition: type_tensor.h:772
TypeVector< T > column(const unsigned int r) const
Definition: type_tensor.h:786
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
This class defines a tensor in LIBMESH_DIM dimensional Real or Complex space.

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