2 #include <libmesh/diagonal_matrix.h> 3 #include <libmesh/parallel.h> 4 #include <libmesh/dense_matrix.h> 5 #include <libmesh/id_types.h> 6 #include <libmesh/numeric_vector.h> 24 return n * (n + 1) / 2;
33 CPPUNIT_TEST(testSizes);
34 CPPUNIT_TEST(testNumerics);
35 CPPUNIT_TEST(testClone);
37 CPPUNIT_TEST_SUITE_END();
43 _matrix = std::make_unique<DiagonalMatrix<Number>>(*_comm);
53 _global_size += block_size;
54 _i.push_back(_global_size);
57 _matrix->init(_global_size, UNUSED, _local_size, UNUSED);
66 CPPUNIT_ASSERT_EQUAL(_global_size, _matrix->m());
67 CPPUNIT_ASSERT_EQUAL(_global_size, _matrix->n());
68 CPPUNIT_ASSERT_EQUAL(_i[_comm->rank()], _matrix->row_start());
69 CPPUNIT_ASSERT_EQUAL(_i[_comm->rank() + 1], _matrix->row_stop());
79 CPPUNIT_ASSERT_EQUAL(_local_size,
numeric_index_type(_matrix->row_stop() - _matrix->row_start()));
85 _matrix->add(beginning_index, beginning_index + 1, 1);
86 _matrix->set(beginning_index + 1, beginning_index, 1);
89 LIBMESH_ASSERT_NUMBERS_EQUAL(0, (*_matrix)(beginning_index, beginning_index + 1), _tolerance);
90 LIBMESH_ASSERT_NUMBERS_EQUAL(0, (*_matrix)(beginning_index + 1, beginning_index), _tolerance);
95 _matrix->add(i, i, i);
98 _matrix->add(0, 0, 1);
100 CPPUNIT_ASSERT(!_matrix->closed());
103 CPPUNIT_ASSERT(_matrix->closed());
108 LIBMESH_ASSERT_NUMBERS_EQUAL((*_matrix)(i, i), i, _tolerance);
110 LIBMESH_ASSERT_NUMBERS_EQUAL((*_matrix)(i, i), _comm->size(), _tolerance);
117 if (i != _global_size - 1)
118 _matrix->set(i, i, i);
120 if (_comm->rank() == 0)
121 _matrix->set(_global_size - 1, _global_size - 1, 0);
123 CPPUNIT_ASSERT(!_matrix->closed());
126 CPPUNIT_ASSERT(_matrix->closed());
130 if (i != _global_size - 1)
131 LIBMESH_ASSERT_NUMBERS_EQUAL(i, (*_matrix)(i, i), _tolerance);
133 LIBMESH_ASSERT_NUMBERS_EQUAL(0, (*_matrix)(i, i), _tolerance);
137 std::vector<numeric_index_type> rows(_local_size);
138 std::iota(rows.begin(), rows.end(), beginning_index);
145 local_index < _local_size;
146 ++local_index, ++global_index)
147 dense(local_index, local_index) = global_index;
152 _matrix->add_matrix(dense, rows, rows);
159 LIBMESH_ASSERT_NUMBERS_EQUAL(i, (*_matrix)(i, i), _tolerance);
164 diagonal->init(_matrix->diagonal());
166 _matrix->get_diagonal(*diagonal);
173 copy = std::move(*diagonal);
176 _matrix->add_matrix(dense, rows);
180 LIBMESH_ASSERT_NUMBERS_EQUAL(2 * i, (*_matrix)(i, i), _tolerance);
183 _matrix->add(-1, copy);
186 LIBMESH_ASSERT_NUMBERS_EQUAL(i, (*_matrix)(i, i), _tolerance);
188 _matrix->get_transpose(copy);
191 LIBMESH_ASSERT_NUMBERS_EQUAL(i, copy(i, i), _tolerance);
196 LIBMESH_ASSERT_FP_EQUAL(termial(_global_size - 1), _matrix->l1_norm(), _tolerance);
197 LIBMESH_ASSERT_FP_EQUAL(_global_size - 1, _matrix->linfty_norm(), _tolerance);
202 _matrix->zero_rows(rows, 1);
207 LIBMESH_ASSERT_NUMBERS_EQUAL(1, (*_matrix)(i, j), _tolerance);
209 LIBMESH_ASSERT_NUMBERS_EQUAL(0, (*_matrix)(i, j), _tolerance);
220 auto copy = _matrix->clone();
223 CPPUNIT_ASSERT_EQUAL(copy->m(), _matrix->m());
224 CPPUNIT_ASSERT_EQUAL(copy->n(), _matrix->n());
225 CPPUNIT_ASSERT_EQUAL(copy->local_m(), _matrix->local_m());
226 CPPUNIT_ASSERT_EQUAL(copy->row_start(), _matrix->row_start());
227 CPPUNIT_ASSERT_EQUAL(copy->row_stop(), _matrix->row_stop());
230 LIBMESH_ASSERT_FP_EQUAL(copy->l1_norm(), _matrix->l1_norm(), _tolerance);
235 auto zero_copy = _matrix->zero_clone();
238 CPPUNIT_ASSERT_EQUAL(zero_copy->m(), _matrix->m());
239 CPPUNIT_ASSERT_EQUAL(zero_copy->n(), _matrix->n());
240 CPPUNIT_ASSERT_EQUAL(zero_copy->local_m(), _matrix->local_m());
241 CPPUNIT_ASSERT_EQUAL(zero_copy->row_start(), _matrix->row_start());
242 CPPUNIT_ASSERT_EQUAL(zero_copy->row_stop(), _matrix->row_stop());
245 LIBMESH_ASSERT_FP_EQUAL(0.0, zero_copy->l1_norm(), _tolerance);
252 std::unique_ptr<DiagonalMatrix<Number>>
_matrix;
254 std::vector<numeric_index_type>
_i;
virtual void zero() override final
Sets all elements of the matrix to 0 and resets any decomposition flag which may have been previously...
libMesh::Parallel::Communicator * TestCommWorld
static constexpr Real TOLERANCE
The libMesh namespace provides an interface to certain functionality in the library.
void iota(ForwardIter first, ForwardIter last, T value)
Utility::iota was created back when std::iota was just an SGI STL extension.
std::vector< numeric_index_type > _i
uint8_t processor_id_type
dof_id_type numeric_index_type
std::unique_ptr< DiagonalMatrix< Number > > _matrix
Diagonal matrix class whose underlying storage is a vector.
CPPUNIT_TEST_SUITE_REGISTRATION(DiagonalMatrixTest)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Parallel::Communicator * _comm
static std::unique_ptr< NumericVector< T > > build(const Parallel::Communicator &comm, SolverPackage solver_package=libMesh::default_solver_package(), ParallelType parallel_type=AUTOMATIC)
Builds a NumericVector on the processors in communicator comm using the linear solver package specifi...
virtual void init(const numeric_index_type m, const numeric_index_type n, const numeric_index_type m_l, const numeric_index_type n_l, const numeric_index_type nnz=30, const numeric_index_type noz=10, const numeric_index_type blocksize=1) override
Initialize SparseMatrix with the specified sizes.
numeric_index_type _local_size