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 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 77 of file sparse_matrix_test.h.

78  {
79  std::vector<libMesh::numeric_index_type> rows(local_m);
80  std::iota(rows.begin(), rows.end(), matrix->row_start());
81  std::vector<libMesh::numeric_index_type> cols(local_n);
82  std::iota(cols.begin(), cols.end(), matrix->col_start());
83 
85 
86  for (auto i : libMesh::make_range(local_m))
87  for (auto j : libMesh::make_range(local_n))
88  local(i, j) = (i + 1) * (j + 1) * (my_comm->rank() + 1);
89 
90  matrix->zero();
91 
92  matrix->add_matrix(local, rows, cols);
93  matrix->close();
94  }
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 74 of file sparse_matrix_test.h.

74 {}

◆ testClone()

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

Definition at line 223 of file sparse_matrix_test.h.

224  {
225  LOG_UNIT_TEST;
226 
227  setValues();
228 
229  // Matrix must be closed before it can be cloned.
230  matrix->close();
231 
232  {
233  // Create copy, test that it can go out of scope
234  auto copy = matrix->clone();
235 
236  // Check that matrices have the same local/global sizes
237  CPPUNIT_ASSERT_EQUAL(copy->m(), matrix->m());
238  CPPUNIT_ASSERT_EQUAL(copy->n(), matrix->n());
239  CPPUNIT_ASSERT_EQUAL(copy->local_m(), matrix->local_m());
240  CPPUNIT_ASSERT_EQUAL(copy->row_start(), matrix->row_start());
241  CPPUNIT_ASSERT_EQUAL(copy->row_stop(), matrix->row_stop());
242 
243  // Check that copy has same values as original
244  LIBMESH_ASSERT_FP_EQUAL(copy->l1_norm(), matrix->l1_norm(), _tolerance);
245  CPPUNIT_ASSERT(relative_fuzzy_equals(*matrix, *copy));
246  copy->scale(2);
247  CPPUNIT_ASSERT(!relative_fuzzy_equals(*matrix, *copy));
248  }
249 
250  {
251  // Create zero copy
252  auto zero_copy = matrix->zero_clone();
253 
254  // Check that matrices have the same local/global sizes
255  CPPUNIT_ASSERT_EQUAL(zero_copy->m(), matrix->m());
256  CPPUNIT_ASSERT_EQUAL(zero_copy->n(), matrix->n());
257  CPPUNIT_ASSERT_EQUAL(zero_copy->local_m(), matrix->local_m());
258  CPPUNIT_ASSERT_EQUAL(zero_copy->row_start(), matrix->row_start());
259  CPPUNIT_ASSERT_EQUAL(zero_copy->row_stop(), matrix->row_stop());
260 
261  // Check that zero_copy has same values as original
262  LIBMESH_ASSERT_FP_EQUAL(0.0, zero_copy->l1_norm(), _tolerance);
263  }
264  }
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 137 of file sparse_matrix_test.h.

138  {
139  LOG_UNIT_TEST;
140 
141  setValues();
142 
143  testValues();
144  }

◆ testReadMatlab()

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

Definition at line 146 of file sparse_matrix_test.h.

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

◆ testReadMatlab1()

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

Definition at line 170 of file sparse_matrix_test.h.

171  {
172  LOG_UNIT_TEST;
173  testReadMatlab("matrices/geom_1_extraction_op.m");
174  }
void testReadMatlab(const std::string &filename)

◆ testReadMatlab2()

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

Definition at line 177 of file sparse_matrix_test.h.

178  {
179  LOG_UNIT_TEST;
180  testReadMatlab("matrices/geom_2_extraction_op.m");
181  }
void testReadMatlab(const std::string &filename)

◆ testReadMatlab4()

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

Definition at line 184 of file sparse_matrix_test.h.

185  {
186  LOG_UNIT_TEST;
187  testReadMatlab("matrices/geom_4_extraction_op.m");
188  }
void testReadMatlab(const std::string &filename)

◆ testValues()

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

Definition at line 97 of file sparse_matrix_test.h.

98  {
99  auto functor = [this]()
100  {
101  std::vector<libMesh::numeric_index_type> cols_to_get;
102  std::vector<libMesh::Number> values;
103  const libMesh::numeric_index_type col_start =
104  matrix->col_start();
106  libMesh::make_range(matrix->row_start(),
107  matrix->row_stop()))
108  {
109  matrix->get_row(i, cols_to_get, values);
110  for (libMesh::numeric_index_type col_j :
111  libMesh::index_range(cols_to_get))
112  {
113  CPPUNIT_ASSERT_EQUAL(cols_to_get[col_j], col_start + col_j);
114  LIBMESH_ASSERT_NUMBERS_EQUAL
115  ((i - matrix->row_start() + 1) * (col_j + 1) * (my_comm->rank() + 1),
116  values[col_j], _tolerance);
117  }
118  }
119  };
120 
121 #ifdef LIBMESH_HAVE_CXX11_THREAD
122  auto num_threads = std::min(unsigned(2),
123  std::max(
124  std::thread::hardware_concurrency(),
125  unsigned(1)));
126  std::vector<std::thread> threads(num_threads);
127  for (unsigned int thread = 0; thread < num_threads; ++thread)
128  threads[thread] = std::thread(functor);
129  std::for_each(threads.begin(), threads.end(),
130  [](std::thread & x){x.join();});
131 #else
132  functor();
133 #endif
134  }
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 191 of file sparse_matrix_test.h.

192  {
193  LOG_UNIT_TEST;
194 
195  setValues();
196 
197  // If we're working with serial matrices then just print one of
198  // them so they don't step on the others' toes.
199  if (matrix->n_processors() > 1 ||
200  TestCommWorld->rank() == 0)
201  matrix->print_matlab(libmesh_suite_name+"_matrix.m");
202 
203  matrix->clear();
204 
205  // Let's make sure we don't have any race conditions; we have
206  // multiple SparseMatrix subclasses that might be trying to read
207  // and write the same file.
208 
210 
211 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
212  // We're not supporting complex reads quite yet
213  return;
214 #endif
215 
216  matrix->read(libmesh_suite_name+"_matrix.m");
217 
219 
220  testValues();
221  }
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 278 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 274 of file sparse_matrix_test.h.

◆ global_n

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

Definition at line 274 of file sparse_matrix_test.h.

◆ libmesh_suite_name

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

Definition at line 268 of file sparse_matrix_test.h.

◆ local_m

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

Definition at line 274 of file sparse_matrix_test.h.

◆ local_n

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

Definition at line 274 of file sparse_matrix_test.h.

◆ matrix

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

Definition at line 272 of file sparse_matrix_test.h.

◆ my_comm

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

Definition at line 270 of file sparse_matrix_test.h.

◆ nonsquare

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

Definition at line 274 of file sparse_matrix_test.h.


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