libMesh
Public Member Functions | Protected Attributes | Private Attributes | List of all members
EigenSparseMatrixTest Class Reference
Inheritance diagram for EigenSparseMatrixTest:
[legend]

Public Member Functions

 EigenSparseMatrixTest ()
 
void setUp ()
 
 CPPUNIT_TEST_SUITE (EigenSparseMatrixTest)
 
SPARSEMATRIXTEST CPPUNIT_TEST_SUITE_END ()
 
void tearDown ()
 
void setValues ()
 
void testValues ()
 
void testGetAndSet ()
 
void testReadMatlab (const std::string &filename)
 
void testReadMatlab1 ()
 
void testReadMatlab2 ()
 
void testReadMatlab4 ()
 
void testReadHDF5 ()
 
void testTransposeNorms ()
 
void testWriteAndRead ()
 
void testClone ()
 

Protected Attributes

std::string libmesh_suite_name
 
libMesh::Parallel::Communicatormy_comm
 
std::unique_ptr< EigenSparseMatrix< Number > > matrix
 
libMesh::numeric_index_type nonsquare
 
libMesh::numeric_index_type local_m
 
libMesh::numeric_index_type local_n
 
libMesh::numeric_index_type global_m
 
libMesh::numeric_index_type global_n
 
const libMesh::Real _tolerance
 

Private Attributes

Parallel::Communicator comm_self
 

Detailed Description

Definition at line 11 of file eigen_sparse_matrix_test.C.

Constructor & Destructor Documentation

◆ EigenSparseMatrixTest()

EigenSparseMatrixTest::EigenSparseMatrixTest ( )
inline

Definition at line 14 of file eigen_sparse_matrix_test.C.

References libMesh::PerfLog::summarized_logs_enabled(), and unitlog.

14  :
17  this->libmesh_suite_name = "SparseMatrixTest";
18  else
19  this->libmesh_suite_name = "EigenSparseMatrixTest";
20  }
bool summarized_logs_enabled()
Definition: perf_log.h:202
libMesh::PerfLog * unitlog
Definition: driver.C:173

Member Function Documentation

◆ CPPUNIT_TEST_SUITE()

EigenSparseMatrixTest::CPPUNIT_TEST_SUITE ( EigenSparseMatrixTest  )

◆ CPPUNIT_TEST_SUITE_END()

SPARSEMATRIXTEST EigenSparseMatrixTest::CPPUNIT_TEST_SUITE_END ( )

◆ setUp()

void EigenSparseMatrixTest::setUp ( )
inline

Definition at line 22 of file eigen_sparse_matrix_test.C.

23  {
24  // EigenSparseMatrix is serial; we'll tell it to use MPI_COMM_SELF
25  // so we just do these tests embarrassingly parallel
26  my_comm = &comm_self;
27 
28  // EigenSparseMatrix doesn't support non-square matrices?
29  nonsquare = 0;
30 
32  }
Parallel::Communicator comm_self

◆ setValues()

void SparseMatrixTest< EigenSparseMatrix< Number > >::setValues ( )
inlineinherited

Definition at line 79 of file sparse_matrix_test.h.

80  {
81  std::vector<libMesh::numeric_index_type> rows(local_m);
82  std::iota(rows.begin(), rows.end(), matrix->row_start());
83  std::vector<libMesh::numeric_index_type> cols(local_n);
84  std::iota(cols.begin(), cols.end(), matrix->col_start());
85 
87 
88  for (auto i : libMesh::make_range(local_m))
89  for (auto j : libMesh::make_range(local_n))
90  local(i, j) = (i + 1) * (j + 1) * (my_comm->rank() + 1);
91 
92  matrix->zero();
93 
94  matrix->add_matrix(local, rows, cols);
95  matrix->close();
96  }
processor_id_type rank() const
void iota(ForwardIter first, ForwardIter last, T value)
Utility::iota was created back when std::iota was just an SGI STL extension.
Definition: utility.h:229
std::unique_ptr< EigenSparseMatrix< Number > > matrix
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
Defines a dense matrix for use in Finite Element-type computations.
Definition: dof_map.h:75

◆ tearDown()

void SparseMatrixTest< EigenSparseMatrix< Number > >::tearDown ( )
inlineinherited

Definition at line 76 of file sparse_matrix_test.h.

76 {}

◆ testClone()

void SparseMatrixTest< EigenSparseMatrix< Number > >::testClone ( )
inlineinherited

Definition at line 259 of file sparse_matrix_test.h.

260  {
261  LOG_UNIT_TEST;
262 
263  setValues();
264 
265  // Matrix must be closed before it can be cloned.
266  matrix->close();
267 
268  {
269  // Create copy, test that it can go out of scope
270  auto copy = matrix->clone();
271 
272  // Check that matrices have the same local/global sizes
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());
278 
279  // Check that copy has same values as original
280  LIBMESH_ASSERT_FP_EQUAL(copy->l1_norm(), matrix->l1_norm(), _tolerance);
281  CPPUNIT_ASSERT(relative_fuzzy_equals(*matrix, *copy));
282  copy->scale(2);
283  CPPUNIT_ASSERT(!relative_fuzzy_equals(*matrix, *copy));
284  }
285 
286  {
287  // Create zero copy
288  auto zero_copy = matrix->zero_clone();
289 
290  // Check that matrices have the same local/global sizes
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());
296 
297  // Check that zero_copy has same values as original
298  LIBMESH_ASSERT_FP_EQUAL(0.0, zero_copy->l1_norm(), _tolerance);
299  }
300  }
std::unique_ptr< EigenSparseMatrix< Number > > matrix
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.
Definition: fuzzy_equals.h:78

◆ testGetAndSet()

void SparseMatrixTest< EigenSparseMatrix< Number > >::testGetAndSet ( )
inlineinherited

Definition at line 139 of file sparse_matrix_test.h.

140  {
141  LOG_UNIT_TEST;
142 
143  setValues();
144 
145  testValues();
146  }

◆ testReadHDF5()

void SparseMatrixTest< EigenSparseMatrix< Number > >::testReadHDF5 ( )
inlineinherited

Definition at line 194 of file sparse_matrix_test.h.

195  {
196 #ifdef LIBMESH_HAVE_HDF5
197  LOG_UNIT_TEST;
198 
199  matrix->clear();
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");
203 
204  // We need some more SparseMatrix operators, but not today
205  CPPUNIT_ASSERT(matrix->l1_norm() == matrix2->l1_norm());
206  CPPUNIT_ASSERT(matrix->linfty_norm() == matrix2->linfty_norm());
207 #endif // LIBMESH_HAVE_HDF5
208  }
std::unique_ptr< EigenSparseMatrix< Number > > matrix

◆ testReadMatlab()

void SparseMatrixTest< EigenSparseMatrix< Number > >::testReadMatlab ( const std::string &  filename)
inlineinherited

Definition at line 149 of file sparse_matrix_test.h.

150  {
151  // Laspack doesn't handle non-square matrices)
152  if (matrix->solver_package() == libMesh::LASPACK_SOLVERS)
153  return;
154 
155  matrix->clear();
156 
157  auto matrix2 = std::make_unique<DerivedClass>(*my_comm);
158 
159  matrix->read(filename);
160 
161 #ifndef LIBMESH_HAVE_GZSTREAM
162  return;
163 #endif
164 
165  matrix2->read(std::string(filename)+".gz");
166 
167  // We need some more SparseMatrix operators, but not today
168  CPPUNIT_ASSERT(matrix->l1_norm() == matrix2->l1_norm());
169  CPPUNIT_ASSERT(matrix->linfty_norm() == matrix2->linfty_norm());
170  }
std::unique_ptr< EigenSparseMatrix< Number > > matrix

◆ testReadMatlab1()

void SparseMatrixTest< EigenSparseMatrix< Number > >::testReadMatlab1 ( )
inlineinherited

Definition at line 173 of file sparse_matrix_test.h.

174  {
175  LOG_UNIT_TEST;
176  testReadMatlab("matrices/geom_1_extraction_op.m");
177  }
void testReadMatlab(const std::string &filename)

◆ testReadMatlab2()

void SparseMatrixTest< EigenSparseMatrix< Number > >::testReadMatlab2 ( )
inlineinherited

Definition at line 180 of file sparse_matrix_test.h.

181  {
182  LOG_UNIT_TEST;
183  testReadMatlab("matrices/geom_2_extraction_op.m");
184  }
void testReadMatlab(const std::string &filename)

◆ testReadMatlab4()

void SparseMatrixTest< EigenSparseMatrix< Number > >::testReadMatlab4 ( )
inlineinherited

Definition at line 187 of file sparse_matrix_test.h.

188  {
189  LOG_UNIT_TEST;
190  testReadMatlab("matrices/geom_4_extraction_op.m");
191  }
void testReadMatlab(const std::string &filename)

◆ testTransposeNorms()

void SparseMatrixTest< EigenSparseMatrix< Number > >::testTransposeNorms ( )
inlineinherited

Definition at line 211 of file sparse_matrix_test.h.

212  {
213  LOG_UNIT_TEST;
214 
215  setValues();
216 
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);
221  }
std::unique_ptr< EigenSparseMatrix< Number > > matrix

◆ testValues()

void SparseMatrixTest< EigenSparseMatrix< Number > >::testValues ( )
inlineinherited

Definition at line 99 of file sparse_matrix_test.h.

100  {
101  auto functor = [this]()
102  {
103  std::vector<libMesh::numeric_index_type> cols_to_get;
104  std::vector<libMesh::Number> values;
105  const libMesh::numeric_index_type col_start =
106  matrix->col_start();
108  libMesh::make_range(matrix->row_start(),
109  matrix->row_stop()))
110  {
111  matrix->get_row(i, cols_to_get, values);
112  for (libMesh::numeric_index_type col_j :
113  libMesh::index_range(cols_to_get))
114  {
115  CPPUNIT_ASSERT_EQUAL(cols_to_get[col_j], col_start + col_j);
116  LIBMESH_ASSERT_NUMBERS_EQUAL
117  ((i - matrix->row_start() + 1) * (col_j + 1) * (my_comm->rank() + 1),
118  values[col_j], _tolerance);
119  }
120  }
121  };
122 
123 #ifdef LIBMESH_HAVE_CXX11_THREAD
124  auto num_threads = std::min(unsigned(2),
125  std::max(
126  std::thread::hardware_concurrency(),
127  unsigned(1)));
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();});
133 #else
134  functor();
135 #endif
136  }
processor_id_type rank() const
dof_id_type numeric_index_type
Definition: id_types.h:99
std::unique_ptr< EigenSparseMatrix< Number > > matrix
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
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition: int_range.h:117

◆ testWriteAndRead()

void SparseMatrixTest< EigenSparseMatrix< Number > >::testWriteAndRead ( )
inlineinherited

Definition at line 224 of file sparse_matrix_test.h.

225  {
226  LOG_UNIT_TEST;
227 
228  setValues();
229 
230  // If we're working with serial matrices then just print one of
231  // them so they don't step on the others' toes.
232  //
233  // Use a very short filename, because we had a bug with that and
234  // we want to test it.
235  if (matrix->n_processors() > 1 ||
236  TestCommWorld->rank() == 0)
237  matrix->print_matlab("M.m");
238 
239  matrix->clear();
240 
241  // Let's make sure we don't have any race conditions; we have
242  // multiple SparseMatrix subclasses that might be trying to read
243  // and write the same file.
244 
246 
247 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
248  // We're not supporting complex reads quite yet
249  return;
250 #endif
251 
252  matrix->read("M.m");
253 
255 
256  testValues();
257  }
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:171
void barrier() const
processor_id_type rank() const
std::unique_ptr< EigenSparseMatrix< Number > > matrix

Member Data Documentation

◆ _tolerance

const libMesh::Real SparseMatrixTest< EigenSparseMatrix< Number > >::_tolerance
protectedinherited

Definition at line 314 of file sparse_matrix_test.h.

◆ comm_self

Parallel::Communicator EigenSparseMatrixTest::comm_self
private

Definition at line 42 of file eigen_sparse_matrix_test.C.

◆ global_m

libMesh::numeric_index_type SparseMatrixTest< EigenSparseMatrix< Number > >::global_m
protectedinherited

Definition at line 310 of file sparse_matrix_test.h.

◆ global_n

libMesh::numeric_index_type SparseMatrixTest< EigenSparseMatrix< Number > >::global_n
protectedinherited

Definition at line 310 of file sparse_matrix_test.h.

◆ libmesh_suite_name

std::string SparseMatrixTest< EigenSparseMatrix< Number > >::libmesh_suite_name
protectedinherited

Definition at line 304 of file sparse_matrix_test.h.

◆ local_m

libMesh::numeric_index_type SparseMatrixTest< EigenSparseMatrix< Number > >::local_m
protectedinherited

Definition at line 310 of file sparse_matrix_test.h.

◆ local_n

libMesh::numeric_index_type SparseMatrixTest< EigenSparseMatrix< Number > >::local_n
protectedinherited

Definition at line 310 of file sparse_matrix_test.h.

◆ matrix

std::unique_ptr<EigenSparseMatrix< Number > > SparseMatrixTest< EigenSparseMatrix< Number > >::matrix
protectedinherited

Definition at line 308 of file sparse_matrix_test.h.

◆ my_comm

libMesh::Parallel::Communicator* SparseMatrixTest< EigenSparseMatrix< Number > >::my_comm
protectedinherited

Definition at line 306 of file sparse_matrix_test.h.

◆ nonsquare

libMesh::numeric_index_type SparseMatrixTest< EigenSparseMatrix< Number > >::nonsquare
protectedinherited

Definition at line 310 of file sparse_matrix_test.h.


The documentation for this class was generated from the following file: