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 #define SPARSEMATRIXTEST \ 23 CPPUNIT_TEST(testGetAndSet); \ 24 CPPUNIT_TEST(testReadMatlab1); \ 25 CPPUNIT_TEST(testReadMatlab2); \ 26 CPPUNIT_TEST(testReadMatlab4); \ 27 CPPUNIT_TEST(testReadHDF5); \ 28 CPPUNIT_TEST(testTransposeNorms); \ 29 CPPUNIT_TEST(testWriteAndRead); \ 30 CPPUNIT_TEST(testClone); 34 template <
class DerivedClass>
47 matrix = std::make_unique<DerivedClass>(*my_comm);
81 std::vector<libMesh::numeric_index_type> rows(
local_m);
83 std::vector<libMesh::numeric_index_type> cols(
local_n);
90 local(i, j) = (i + 1) * (j + 1) * (
my_comm->
rank() + 1);
94 matrix->add_matrix(local, rows, cols);
101 auto functor = [
this]()
103 std::vector<libMesh::numeric_index_type> cols_to_get;
104 std::vector<libMesh::Number> values;
111 matrix->get_row(i, cols_to_get, values);
115 CPPUNIT_ASSERT_EQUAL(cols_to_get[col_j], col_start + col_j);
116 LIBMESH_ASSERT_NUMBERS_EQUAL
123 #ifdef LIBMESH_HAVE_CXX11_THREAD 124 auto num_threads = std::min(
unsigned(2),
126 std::thread::hardware_concurrency(),
128 std::vector<std::thread> threads(num_threads);
129 for (
unsigned int thread = 0; thread < num_threads; ++thread)
130 threads[thread] = std::thread(functor);
131 std::for_each(threads.begin(), threads.end(),
132 [](std::thread & x){x.join();});
157 auto matrix2 = std::make_unique<DerivedClass>(*my_comm);
161 #ifndef LIBMESH_HAVE_GZSTREAM 165 matrix2->read(std::string(filename)+
".gz");
168 CPPUNIT_ASSERT(
matrix->l1_norm() == matrix2->l1_norm());
169 CPPUNIT_ASSERT(
matrix->linfty_norm() == matrix2->linfty_norm());
196 #ifdef LIBMESH_HAVE_HDF5 200 auto matrix2 = std::make_unique<DerivedClass>(*my_comm);
201 matrix->read(
"matrices/geom_1_extraction_op.m");
202 matrix2->read(
"matrices/geom_1_extraction_op.h5");
205 CPPUNIT_ASSERT(
matrix->l1_norm() == matrix2->l1_norm());
206 CPPUNIT_ASSERT(
matrix->linfty_norm() == matrix2->linfty_norm());
207 #endif // LIBMESH_HAVE_HDF5 217 auto matrix2 = std::make_unique<DerivedClass>(*my_comm);
218 matrix->get_transpose(*matrix2);
219 LIBMESH_ASSERT_FP_EQUAL(
matrix->l1_norm(), matrix2->linfty_norm(),
_tolerance);
220 LIBMESH_ASSERT_FP_EQUAL(matrix2->l1_norm(),
matrix->linfty_norm(),
_tolerance);
235 if (
matrix->n_processors() > 1 ||
237 matrix->print_matlab(
"M.m");
247 #ifdef LIBMESH_USE_COMPLEX_NUMBERS 270 auto copy =
matrix->clone();
273 CPPUNIT_ASSERT_EQUAL(copy->m(),
matrix->m());
274 CPPUNIT_ASSERT_EQUAL(copy->n(),
matrix->n());
275 CPPUNIT_ASSERT_EQUAL(copy->local_m(),
matrix->local_m());
276 CPPUNIT_ASSERT_EQUAL(copy->row_start(),
matrix->row_start());
277 CPPUNIT_ASSERT_EQUAL(copy->row_stop(),
matrix->row_stop());
288 auto zero_copy =
matrix->zero_clone();
291 CPPUNIT_ASSERT_EQUAL(zero_copy->m(),
matrix->m());
292 CPPUNIT_ASSERT_EQUAL(zero_copy->n(),
matrix->n());
293 CPPUNIT_ASSERT_EQUAL(zero_copy->local_m(),
matrix->local_m());
294 CPPUNIT_ASSERT_EQUAL(zero_copy->row_start(),
matrix->row_start());
295 CPPUNIT_ASSERT_EQUAL(zero_copy->row_stop(),
matrix->row_stop());
298 LIBMESH_ASSERT_FP_EQUAL(0.0, zero_copy->l1_norm(),
_tolerance);
317 #endif // SPARSE_MATRIX_TEST_H
libMesh::Parallel::Communicator * TestCommWorld
static constexpr Real TOLERANCE
libMesh::Parallel::Communicator * my_comm
processor_id_type rank() const
libMesh::numeric_index_type local_n
void iota(ForwardIter first, ForwardIter last, T value)
Utility::iota was created back when std::iota was just an SGI STL extension.
processor_id_type size() const
dof_id_type numeric_index_type
void testReadMatlab(const std::string &filename)
std::unique_ptr< DerivedClass > matrix
void testTransposeNorms()
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
libMesh::numeric_index_type global_m
std::string libmesh_suite_name
const libMesh::Real _tolerance
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...
libMesh::numeric_index_type nonsquare
bool relative_fuzzy_equals(const T &var1, const T2 &var2, const Real tol=TOLERANCE *TOLERANCE)
Function to check whether two variables are equal within a relative tolerance.
libMesh::numeric_index_type global_n
Defines a dense matrix for use in Finite Element-type computations.
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
libMesh::numeric_index_type local_m