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(testWriteAndRead); \ 29 CPPUNIT_TEST(testClone); 33 template <
class DerivedClass>
46 matrix = std::make_unique<DerivedClass>(*my_comm);
80 std::vector<libMesh::numeric_index_type> rows(
local_m);
82 std::vector<libMesh::numeric_index_type> cols(
local_n);
89 local(i, j) = (i + 1) * (j + 1) * (
my_comm->
rank() + 1);
93 matrix->add_matrix(local, rows, cols);
100 auto functor = [
this]()
102 std::vector<libMesh::numeric_index_type> cols_to_get;
103 std::vector<libMesh::Number> values;
110 matrix->get_row(i, cols_to_get, values);
114 CPPUNIT_ASSERT_EQUAL(cols_to_get[col_j], col_start + col_j);
115 LIBMESH_ASSERT_NUMBERS_EQUAL
122 #ifdef LIBMESH_HAVE_CXX11_THREAD 123 auto num_threads = std::min(
unsigned(2),
125 std::thread::hardware_concurrency(),
127 std::vector<std::thread> threads(num_threads);
128 for (
unsigned int thread = 0; thread < num_threads; ++thread)
129 threads[thread] = std::thread(functor);
130 std::for_each(threads.begin(), threads.end(),
131 [](std::thread & x){x.join();});
155 auto matrix2 = std::make_unique<DerivedClass>(*my_comm);
159 #ifndef LIBMESH_HAVE_GZSTREAM 163 matrix2->read(std::string(filename)+
".gz");
166 CPPUNIT_ASSERT(
matrix->l1_norm() == matrix2->l1_norm());
167 CPPUNIT_ASSERT(
matrix->linfty_norm() == matrix2->linfty_norm());
194 #ifdef LIBMESH_HAVE_HDF5 198 auto matrix2 = std::make_unique<DerivedClass>(*my_comm);
199 matrix->read(
"matrices/geom_1_extraction_op.m");
200 matrix2->read(
"matrices/geom_1_extraction_op.h5");
203 CPPUNIT_ASSERT(
matrix->l1_norm() == matrix2->l1_norm());
204 CPPUNIT_ASSERT(
matrix->linfty_norm() == matrix2->linfty_norm());
205 #endif // LIBMESH_HAVE_HDF5 220 if (
matrix->n_processors() > 1 ||
222 matrix->print_matlab(
"M.m");
232 #ifdef LIBMESH_USE_COMPLEX_NUMBERS 255 auto copy =
matrix->clone();
258 CPPUNIT_ASSERT_EQUAL(copy->m(),
matrix->m());
259 CPPUNIT_ASSERT_EQUAL(copy->n(),
matrix->n());
260 CPPUNIT_ASSERT_EQUAL(copy->local_m(),
matrix->local_m());
261 CPPUNIT_ASSERT_EQUAL(copy->row_start(),
matrix->row_start());
262 CPPUNIT_ASSERT_EQUAL(copy->row_stop(),
matrix->row_stop());
273 auto zero_copy =
matrix->zero_clone();
276 CPPUNIT_ASSERT_EQUAL(zero_copy->m(),
matrix->m());
277 CPPUNIT_ASSERT_EQUAL(zero_copy->n(),
matrix->n());
278 CPPUNIT_ASSERT_EQUAL(zero_copy->local_m(),
matrix->local_m());
279 CPPUNIT_ASSERT_EQUAL(zero_copy->row_start(),
matrix->row_start());
280 CPPUNIT_ASSERT_EQUAL(zero_copy->row_stop(),
matrix->row_stop());
283 LIBMESH_ASSERT_FP_EQUAL(0.0, zero_copy->l1_norm(),
_tolerance);
302 #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
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