libMesh
lumped_mass_matrix_test.C
Go to the documentation of this file.
1 // libmesh includes
2 #include <libmesh/lumped_mass_matrix.h>
3 #include <libmesh/parallel.h>
4 #include <libmesh/id_types.h>
5 #include <libmesh/int_range.h>
6 #include <libmesh/numeric_vector.h>
7 
8 #include "libmesh_cppunit.h"
9 #include "test_comm.h"
10 
11 #include <memory>
12 
13 #define UNUSED 0
14 
15 using namespace libMesh;
16 
17 class LumpedMassMatrixTest : public CppUnit::TestCase
18 {
19 public:
20  LIBMESH_CPPUNIT_TEST_SUITE(LumpedMassMatrixTest);
21 
22  CPPUNIT_TEST(testNumerics);
23 
24  CPPUNIT_TEST_SUITE_END();
25 
26 public:
27  void setUp()
28  {
29  _comm = TestCommWorld;
30  _matrix = std::make_unique<LumpedMassMatrix<Number>>(*_comm);
31 
32  numeric_index_type root_block_size = 2;
33  _local_size = root_block_size + static_cast<numeric_index_type>(_comm->rank());
34  _global_size = 0;
35 
36  for (processor_id_type p = 0; p < _comm->size(); ++p)
37  {
38  numeric_index_type block_size = root_block_size + static_cast<numeric_index_type>(p);
39  _global_size += block_size;
40  }
41 
42  _matrix->init(_global_size, UNUSED, _local_size, UNUSED);
43  }
44 
45  void tearDown() {}
46 
47  void testNumerics()
48  {
49  LOG_UNIT_TEST;
50 
51  numeric_index_type beginning_index = _matrix->row_start();
52  numeric_index_type end_index = _matrix->row_stop();
53 
54  CPPUNIT_ASSERT_EQUAL(_local_size, numeric_index_type(_matrix->row_stop() - _matrix->row_start()));
55 
56  _matrix->zero();
57 
58  std::vector<Real> gold_values(_local_size, 0);
59 
60  // Test add
61  for (const auto i : make_range(beginning_index, end_index))
62  for (const auto j : make_range(beginning_index, end_index))
63  {
64  const Real sgn = j % 2 ? 1 : -1;
65  _matrix->add(i, j, sgn * j);
66  gold_values[i - beginning_index] += j;
67  }
68 
69  _matrix->close();
70 
71  for (const auto i : make_range(beginning_index, end_index))
72  for (const auto j : make_range(beginning_index, end_index))
73  {
74  if (i != j)
75  LIBMESH_ASSERT_NUMBERS_EQUAL(0, (*_matrix)(i, j), _tolerance);
76  else
77  LIBMESH_ASSERT_NUMBERS_EQUAL(gold_values[i - beginning_index], (*_matrix)(i, i), _tolerance);
78  }
79 
80  // Test set
81  for (const auto i : make_range(beginning_index, end_index))
82  {
83  const Real sgn = i % 2 ? 1 : -1;
84  _matrix->set(i, i, sgn * gold_values[i - beginning_index]);
85  }
86 
87  _matrix->close();
88 
89  for (const auto i : make_range(beginning_index, end_index))
90  LIBMESH_ASSERT_NUMBERS_EQUAL(gold_values[i - beginning_index], (*_matrix)(i, i), _tolerance);
91 
92  // Test clone
93  auto copy = _matrix->clone();
94  // Check that matrices have the same local/global sizes
95  CPPUNIT_ASSERT_EQUAL(copy->m(), _matrix->m());
96  CPPUNIT_ASSERT_EQUAL(copy->n(), _matrix->n());
97  CPPUNIT_ASSERT_EQUAL(copy->local_m(), _matrix->local_m());
98  CPPUNIT_ASSERT_EQUAL(copy->row_start(), _matrix->row_start());
99  CPPUNIT_ASSERT_EQUAL(copy->row_stop(), _matrix->row_stop());
100 
101  for (const auto i : make_range(beginning_index, end_index))
102  for (const auto j : make_range(beginning_index, end_index))
103  LIBMESH_ASSERT_NUMBERS_EQUAL((*_matrix)(i, j), (*copy)(i, j), _tolerance);
104 
105  // Test zero clone
106  auto zero_copy = _matrix->zero_clone();
107  // Check that matrices have the same local/global sizes
108  CPPUNIT_ASSERT_EQUAL(zero_copy->m(), _matrix->m());
109  CPPUNIT_ASSERT_EQUAL(zero_copy->n(), _matrix->n());
110  CPPUNIT_ASSERT_EQUAL(zero_copy->local_m(), _matrix->local_m());
111  CPPUNIT_ASSERT_EQUAL(zero_copy->row_start(), _matrix->row_start());
112  CPPUNIT_ASSERT_EQUAL(zero_copy->row_stop(), _matrix->row_stop());
113 
114  for (const auto i : make_range(beginning_index, end_index))
115  for (const auto j : make_range(beginning_index, end_index))
116  LIBMESH_ASSERT_NUMBERS_EQUAL(0, (*zero_copy)(i, j), _tolerance);
117  }
118 
119 private:
120 
122  std::unique_ptr<LumpedMassMatrix<Number>> _matrix;
124  const Real _tolerance = TOLERANCE * TOLERANCE;
125 };
126 
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:171
static constexpr Real TOLERANCE
numeric_index_type _local_size
The libMesh namespace provides an interface to certain functionality in the library.
uint8_t processor_id_type
Parallel::Communicator * _comm
dof_id_type numeric_index_type
Definition: id_types.h:99
std::unique_ptr< LumpedMassMatrix< Number > > _matrix
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:140
CPPUNIT_TEST_SUITE_REGISTRATION(LumpedMassMatrixTest)