Skip to content

Commit 4175ba9

Browse files
committed
Implement the 6 tests for order N tensors
to support idaholab/moose#32023
1 parent 7b9b3b4 commit 4175ba9

File tree

1 file changed

+66
-14
lines changed

1 file changed

+66
-14
lines changed

tests/numerics/type_N_tensor_test.C

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// libmesh includes
2+
#include <libmesh/type_n_tensor.h>
23
#include <libmesh/tensor_value.h>
34
#include <libmesh/vector_value.h>
45
#include <libmesh/point.h>
@@ -16,11 +17,14 @@ public:
1617

1718
LIBMESH_CPPUNIT_TEST_SUITE(TypeNTensorTest);
1819

20+
#if LIBMESH_DIM > 2
1921
CPPUNIT_TEST(testOperatorsScalar);
2022
CPPUNIT_TEST(testOperatorsTensor);
2123
CPPUNIT_TEST(testCastVector);
2224
CPPUNIT_TEST(testZero);
25+
CPPUNIT_TEST(testNorm);
2326
CPPUNIT_TEST(testSlice);
27+
#endif
2428

2529
CPPUNIT_TEST_SUITE_END();
2630

@@ -29,51 +33,99 @@ private:
2933
{
3034
LOG_UNIT_TEST;
3135

32-
// Add a number
33-
34-
// Subtract a number
36+
std::vector<Real> values = {0, 1, 2, 3, 4, 5, 6, 7, 8};
37+
TypeNTensor<2, Real> T(values);
3538

3639
// Multiply by a number
40+
T *= 3.1;
41+
for (const auto i : index_range(values))
42+
LIBMESH_ASSERT_FP_EQUAL(3.1 * values[i], T._coords[i], 1e-12);
3743

3844
// Divide by a number
39-
40-
// TensorValue<Real> tensor(1, 2, 0, 3, 4, 0);
41-
// VectorValue<Real> vector(5, 6, 0);
42-
// auto left_mult = vector * tensor;
43-
// auto right_mult = tensor * vector;
44-
// LIBMESH_ASSERT_FP_EQUAL(23, left_mult(0), 1e-12);
45-
// LIBMESH_ASSERT_FP_EQUAL(34, left_mult(1), 1e-12);
46-
// LIBMESH_ASSERT_FP_EQUAL(17, right_mult(0), 1e-12);
47-
// LIBMESH_ASSERT_FP_EQUAL(39, right_mult(1), 1e-12);
45+
T /= 3.1;
46+
for (const auto i : index_range(values))
47+
LIBMESH_ASSERT_FP_EQUAL(values[i], T._coords[i], 1e-12);
4848
}
4949

5050
void testOperatorsTensor()
5151
{
52+
std::vector<Real> values = {0, 1, 2, 3, 4, 5, 6, 7, 8};
53+
std::vector<Real> values2 = {0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9};
54+
TypeNTensor<2, Real> T(values);
55+
TypeNTensor<2, Real> T2(values2);
56+
5257
// Add a tensor
58+
T += T2;
59+
for (const auto i : index_range(values))
60+
LIBMESH_ASSERT_FP_EQUAL(values[i] + values2[i], T._coords[i], 1e-12);
5361

5462
// Return sum of two tensors
63+
const auto T3 = T + T2;
64+
for (const auto i : index_range(values))
65+
LIBMESH_ASSERT_FP_EQUAL(values[i] + 2 * values2[i], T3._coords[i], 1e-12);
5566

5667
// Subtract a tensor
68+
T -= T2;
69+
for (const auto i : index_range(values))
70+
LIBMESH_ASSERT_FP_EQUAL(values[i], T._coords[i], 1e-12);
5771

5872
// Return subtraction of one tensor by another
73+
const auto T4 = T - T2;
74+
for (const auto i : index_range(values))
75+
LIBMESH_ASSERT_FP_EQUAL(values[i] - values2[i], T4._coords[i], 1e-12);
5976

6077
// Add a scaled tensor
61-
78+
T.add_scaled(T2, 3);
79+
for (const auto i : index_range(values))
80+
LIBMESH_ASSERT_FP_EQUAL(values[i] + 3 * values2[i], T._coords[i], 1e-12);
6281
}
6382

6483
void testCastVector()
6584
{
85+
std::vector<Real> values = {0, 1, 2};
86+
TypeNTensor<1, Real> T(values);
87+
VectorValue<Real> v2 = T;
6688

89+
for (const auto i : index_range(values))
90+
LIBMESH_ASSERT_FP_EQUAL(values[i], v2(i), 1e-12);
6791
}
6892

6993
void testZero()
7094
{
95+
std::vector<Real> values = {0, 1, 2, 3, 4, 5, 6, 7, 8};
96+
TypeNTensor<2, Real> T(values);
97+
TypeNTensor<2, Real> T2(values);
98+
99+
T = 0;
100+
for (const auto i : index_range(values))
101+
LIBMESH_ASSERT_FP_EQUAL(0., T._coords[i], 1e-12);
102+
T2.zero();
103+
for (const auto i : index_range(values))
104+
LIBMESH_ASSERT_FP_EQUAL(0., T2._coords[i], 1e-12);
105+
}
71106

107+
void testNorm()
108+
{
109+
std::vector<Real> values = {0, 1, 2, 3, 4, 5, 6, 7, 8};
110+
TypeNTensor<2, Real> T(values);
111+
Real sum_sq = (8 * 9 * 17 / 6.);
112+
LIBMESH_ASSERT_FP_EQUAL(sum_sq, T.norm_sq(), 1e-12);
113+
LIBMESH_ASSERT_FP_EQUAL(std::sqrt(sum_sq), T.norm(), 1e-12);
72114
}
73115

74116
void testSlice()
75117
{
76-
118+
std::vector<Real> values = {0, 1, 2, 3, 4, 5, 6, 7, 8};
119+
TypeNTensor<2, Real> T(values);
120+
const auto s0 = T.slice(0);
121+
const auto s1 = T.slice(1);
122+
const auto s2 = T.slice(2);
123+
for (const auto i : make_range(3))
124+
LIBMESH_ASSERT_FP_EQUAL(values[i], s0._coords[i], 1e-12);
125+
for (const auto i : make_range(3))
126+
LIBMESH_ASSERT_FP_EQUAL(values[3 + i], s1._coords[i], 1e-12);
127+
for (const auto i : make_range(3))
128+
LIBMESH_ASSERT_FP_EQUAL(values[6 + i], s2._coords[i], 1e-12);
77129
}
78130
};
79131

0 commit comments

Comments
 (0)