1#ifndef SPARSE_MATRIX_TEST_H
2#define SPARSE_MATRIX_TEST_H
8#include <libmesh/dense_matrix.h>
9#include <libmesh/sparse_matrix.h>
10#include <libmesh/fuzzy_equals.h>
16#ifdef LIBMESH_HAVE_CXX11_THREAD
22#ifdef LIBMESH_HAVE_HDF5
23#define SPARSEMATRIXTEST \
24 CPPUNIT_TEST(testGetAndSet); \
25 CPPUNIT_TEST(testReadHDF5); \
26 CPPUNIT_TEST(testReadMatlab1); \
27 CPPUNIT_TEST(testReadMatlab2); \
28 CPPUNIT_TEST(testReadMatlab4); \
29 CPPUNIT_TEST(testTransposeNorms); \
30 CPPUNIT_TEST(testWriteAndReadHDF5); \
31 CPPUNIT_TEST(testWriteAndReadMatlab); \
32 CPPUNIT_TEST(testWriteAndReadZippedMatlab);\
33 CPPUNIT_TEST(testClone);
35#define SPARSEMATRIXTEST \
36 CPPUNIT_TEST(testGetAndSet); \
37 CPPUNIT_TEST(testReadMatlab1); \
38 CPPUNIT_TEST(testReadMatlab2); \
39 CPPUNIT_TEST(testReadMatlab4); \
40 CPPUNIT_TEST(testTransposeNorms); \
41 CPPUNIT_TEST(testWriteAndReadMatlab); \
42 CPPUNIT_TEST(testWriteAndReadZippedMatlab);\
43 CPPUNIT_TEST(testClone);
48template <
class DerivedClass>
95 std::vector<libMesh::numeric_index_type> rows(
local_m);
96 std::iota(rows.begin(), rows.end(),
matrix->row_start());
97 std::vector<libMesh::numeric_index_type> cols(
local_n);
98 std::iota(cols.begin(), cols.end(),
matrix->col_start());
104 local(i, j) = (i + 1) * (j + 1) * (
my_comm->
rank() + 1);
108 matrix->add_matrix(local, rows, cols);
115 auto functor = [
this]()
117 std::vector<libMesh::numeric_index_type> cols_to_get;
118 std::vector<libMesh::Number> values;
125 matrix->get_row(i, cols_to_get, values);
129 CPPUNIT_ASSERT_EQUAL(cols_to_get[col_j], col_start + col_j);
130 LIBMESH_ASSERT_NUMBERS_EQUAL
137#ifdef LIBMESH_HAVE_CXX11_THREAD
138 auto num_threads = std::min(
unsigned(2),
140 std::thread::hardware_concurrency(),
142 std::vector<std::thread> threads(num_threads);
143 for (
unsigned int thread = 0; thread < num_threads; ++thread)
144 threads[thread] = std::thread(functor);
145 std::for_each(threads.begin(), threads.end(),
146 [](std::thread & x){x.join();});
171 auto matrix2 = std::make_unique<DerivedClass>(*
my_comm);
175#ifndef LIBMESH_HAVE_GZSTREAM
179 matrix2->read(std::string(filename)+
".gz");
182 CPPUNIT_ASSERT(
matrix->l1_norm() == matrix2->l1_norm());
183 CPPUNIT_ASSERT(
matrix->linfty_norm() == matrix2->linfty_norm());
208#ifdef LIBMESH_HAVE_HDF5
214 auto matrix2 = std::make_unique<DerivedClass>(*
my_comm);
215 matrix->read(
"matrices/geom_1_extraction_op.m");
216 matrix2->read(
"matrices/geom_1_extraction_op.h5");
219 CPPUNIT_ASSERT(
matrix->l1_norm() == matrix2->l1_norm());
220 CPPUNIT_ASSERT(
matrix->linfty_norm() == matrix2->linfty_norm());
231 auto matrix2 = std::make_unique<DerivedClass>(*
my_comm);
232 matrix->get_transpose(*matrix2);
233 LIBMESH_ASSERT_FP_EQUAL(
matrix->l1_norm(), matrix2->linfty_norm(),
_tolerance);
234 LIBMESH_ASSERT_FP_EQUAL(matrix2->l1_norm(),
matrix->linfty_norm(),
_tolerance);
246 if (
matrix->n_processors() > 1 ||
258#ifdef LIBMESH_USE_COMPLEX_NUMBERS
270#ifdef LIBMESH_HAVE_HDF5
275#ifndef LIBMESH_USE_COMPLEX_NUMBERS
291#ifdef LIBMESH_HAVE_GZSTREAM
307 auto copy =
matrix->clone();
310 CPPUNIT_ASSERT_EQUAL(copy->m(),
matrix->m());
311 CPPUNIT_ASSERT_EQUAL(copy->n(),
matrix->n());
312 CPPUNIT_ASSERT_EQUAL(copy->local_m(),
matrix->local_m());
313 CPPUNIT_ASSERT_EQUAL(copy->row_start(),
matrix->row_start());
314 CPPUNIT_ASSERT_EQUAL(copy->row_stop(),
matrix->row_stop());
318 CPPUNIT_ASSERT(relative_fuzzy_equals(*
matrix, *copy));
320 CPPUNIT_ASSERT(!relative_fuzzy_equals(*
matrix, *copy));
325 auto zero_copy =
matrix->zero_clone();
328 CPPUNIT_ASSERT_EQUAL(zero_copy->m(),
matrix->m());
329 CPPUNIT_ASSERT_EQUAL(zero_copy->n(),
matrix->n());
330 CPPUNIT_ASSERT_EQUAL(zero_copy->local_m(),
matrix->local_m());
331 CPPUNIT_ASSERT_EQUAL(zero_copy->row_start(),
matrix->row_start());
332 CPPUNIT_ASSERT_EQUAL(zero_copy->row_stop(),
matrix->row_stop());
335 LIBMESH_ASSERT_FP_EQUAL(0.0, zero_copy->l1_norm(),
_tolerance);
void testWriteAndReadZippedMatlab()
libMesh::numeric_index_type local_m
libMesh::numeric_index_type global_n
void testWriteAndReadHDF5()
const libMesh::Real _tolerance
libMesh::numeric_index_type local_n
void testWriteAndReadMatlab()
void testWriteAndRead(const std::string &filename)
libMesh::numeric_index_type nonsquare
libMesh::numeric_index_type global_m
libMesh::Parallel::Communicator * my_comm
std::string libmesh_suite_name
void testTransposeNorms()
std::unique_ptr< DerivedClass > matrix
void testReadMatlab(const std::string &filename)
processor_id_type size() const
processor_id_type rank() const
Defines a dense matrix for use in Finite Element-type computations.
virtual void zero() override final
Sets all elements of the matrix to 0 and resets any decomposition flag which may have been previously...
Communicator * TestCommWorld
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
dof_id_type numeric_index_type
static constexpr Real TOLERANCE
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...