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

Public Member Functions

 LaspackMatrixTest ()
 
void setUp ()
 
 CPPUNIT_TEST_SUITE (LaspackMatrixTest)
 
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 testWriteAndRead ()
 
void testClone ()
 

Protected Attributes

std::string libmesh_suite_name
 
libMesh::Parallel::Communicatormy_comm
 
std::unique_ptr< LaspackMatrix< 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 laspack_matrix_test.C.

Constructor & Destructor Documentation

◆ LaspackMatrixTest()

LaspackMatrixTest::LaspackMatrixTest ( )
inline

Definition at line 14 of file laspack_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 = "LaspackMatrixTest";
20  }
bool summarized_logs_enabled()
Definition: perf_log.h:202
libMesh::PerfLog * unitlog
Definition: driver.C:173

Member Function Documentation

◆ CPPUNIT_TEST_SUITE()

LaspackMatrixTest::CPPUNIT_TEST_SUITE ( LaspackMatrixTest  )

◆ CPPUNIT_TEST_SUITE_END()

SPARSEMATRIXTEST LaspackMatrixTest::CPPUNIT_TEST_SUITE_END ( )

◆ setUp()

void LaspackMatrixTest::setUp ( )
inline

Definition at line 22 of file laspack_matrix_test.C.

23  {
24  // LaspackMatrix 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  // LaspackMatrix doesn't support non-square matrices?
29  nonsquare = 0;
30 
32  }
libMesh::Parallel::Communicator * my_comm
Parallel::Communicator comm_self

◆ setValues()

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

Definition at line 78 of file sparse_matrix_test.h.

References libMesh::Utility::iota(), SparseMatrixTest< DerivedClass >::local_m, SparseMatrixTest< DerivedClass >::local_n, libMesh::make_range(), SparseMatrixTest< DerivedClass >::matrix, SparseMatrixTest< DerivedClass >::my_comm, and TIMPI::Communicator::rank().

79  {
80  std::vector<libMesh::numeric_index_type> rows(local_m);
81  std::iota(rows.begin(), rows.end(), matrix->row_start());
82  std::vector<libMesh::numeric_index_type> cols(local_n);
83  std::iota(cols.begin(), cols.end(), matrix->col_start());
84 
86 
87  for (auto i : libMesh::make_range(local_m))
88  for (auto j : libMesh::make_range(local_n))
89  local(i, j) = (i + 1) * (j + 1) * (my_comm->rank() + 1);
90 
91  matrix->zero();
92 
93  matrix->add_matrix(local, rows, cols);
94  matrix->close();
95  }
libMesh::Parallel::Communicator * my_comm
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< LaspackMatrix< 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< LaspackMatrix< Number > >::tearDown ( )
inlineinherited

Definition at line 75 of file sparse_matrix_test.h.

75 {}

◆ testClone()

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

Definition at line 244 of file sparse_matrix_test.h.

References SparseMatrixTest< DerivedClass >::_tolerance, SparseMatrixTest< DerivedClass >::matrix, libMesh::relative_fuzzy_equals(), and SparseMatrixTest< DerivedClass >::setValues().

245  {
246  LOG_UNIT_TEST;
247 
248  setValues();
249 
250  // Matrix must be closed before it can be cloned.
251  matrix->close();
252 
253  {
254  // Create copy, test that it can go out of scope
255  auto copy = matrix->clone();
256 
257  // Check that matrices have the same local/global sizes
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());
263 
264  // Check that copy has same values as original
265  LIBMESH_ASSERT_FP_EQUAL(copy->l1_norm(), matrix->l1_norm(), _tolerance);
266  CPPUNIT_ASSERT(relative_fuzzy_equals(*matrix, *copy));
267  copy->scale(2);
268  CPPUNIT_ASSERT(!relative_fuzzy_equals(*matrix, *copy));
269  }
270 
271  {
272  // Create zero copy
273  auto zero_copy = matrix->zero_clone();
274 
275  // Check that matrices have the same local/global sizes
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());
281 
282  // Check that zero_copy has same values as original
283  LIBMESH_ASSERT_FP_EQUAL(0.0, zero_copy->l1_norm(), _tolerance);
284  }
285  }
std::unique_ptr< LaspackMatrix< 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< LaspackMatrix< Number > >::testGetAndSet ( )
inlineinherited

◆ testReadHDF5()

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

Definition at line 192 of file sparse_matrix_test.h.

References SparseMatrixTest< DerivedClass >::matrix.

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

◆ testReadMatlab()

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

Definition at line 147 of file sparse_matrix_test.h.

References libMesh::LASPACK_SOLVERS, and SparseMatrixTest< DerivedClass >::matrix.

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

◆ testReadMatlab1()

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

Definition at line 171 of file sparse_matrix_test.h.

References SparseMatrixTest< DerivedClass >::testReadMatlab().

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

◆ testReadMatlab2()

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

Definition at line 178 of file sparse_matrix_test.h.

References SparseMatrixTest< DerivedClass >::testReadMatlab().

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

◆ testReadMatlab4()

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

Definition at line 185 of file sparse_matrix_test.h.

References SparseMatrixTest< DerivedClass >::testReadMatlab().

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

◆ testValues()

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

Definition at line 98 of file sparse_matrix_test.h.

References SparseMatrixTest< DerivedClass >::_tolerance, libMesh::index_range(), libMesh::make_range(), SparseMatrixTest< DerivedClass >::matrix, SparseMatrixTest< DerivedClass >::my_comm, and TIMPI::Communicator::rank().

99  {
100  auto functor = [this]()
101  {
102  std::vector<libMesh::numeric_index_type> cols_to_get;
103  std::vector<libMesh::Number> values;
104  const libMesh::numeric_index_type col_start =
105  matrix->col_start();
107  libMesh::make_range(matrix->row_start(),
108  matrix->row_stop()))
109  {
110  matrix->get_row(i, cols_to_get, values);
111  for (libMesh::numeric_index_type col_j :
112  libMesh::index_range(cols_to_get))
113  {
114  CPPUNIT_ASSERT_EQUAL(cols_to_get[col_j], col_start + col_j);
115  LIBMESH_ASSERT_NUMBERS_EQUAL
116  ((i - matrix->row_start() + 1) * (col_j + 1) * (my_comm->rank() + 1),
117  values[col_j], _tolerance);
118  }
119  }
120  };
121 
122 #ifdef LIBMESH_HAVE_CXX11_THREAD
123  auto num_threads = std::min(unsigned(2),
124  std::max(
125  std::thread::hardware_concurrency(),
126  unsigned(1)));
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();});
132 #else
133  functor();
134 #endif
135  }
libMesh::Parallel::Communicator * my_comm
processor_id_type rank() const
dof_id_type numeric_index_type
Definition: id_types.h:99
std::unique_ptr< LaspackMatrix< 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< LaspackMatrix< Number > >::testWriteAndRead ( )
inlineinherited

Definition at line 209 of file sparse_matrix_test.h.

References TIMPI::Communicator::barrier(), SparseMatrixTest< DerivedClass >::matrix, TIMPI::Communicator::rank(), SparseMatrixTest< DerivedClass >::setValues(), TestCommWorld, and SparseMatrixTest< DerivedClass >::testValues().

210  {
211  LOG_UNIT_TEST;
212 
213  setValues();
214 
215  // If we're working with serial matrices then just print one of
216  // them so they don't step on the others' toes.
217  //
218  // Use a very short filename, because we had a bug with that and
219  // we want to test it.
220  if (matrix->n_processors() > 1 ||
221  TestCommWorld->rank() == 0)
222  matrix->print_matlab("M.m");
223 
224  matrix->clear();
225 
226  // Let's make sure we don't have any race conditions; we have
227  // multiple SparseMatrix subclasses that might be trying to read
228  // and write the same file.
229 
231 
232 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
233  // We're not supporting complex reads quite yet
234  return;
235 #endif
236 
237  matrix->read("M.m");
238 
240 
241  testValues();
242  }
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:171
void barrier() const
processor_id_type rank() const
std::unique_ptr< LaspackMatrix< Number > > matrix

Member Data Documentation

◆ _tolerance

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

Definition at line 299 of file sparse_matrix_test.h.

◆ comm_self

Parallel::Communicator LaspackMatrixTest::comm_self
private

Definition at line 42 of file laspack_matrix_test.C.

◆ global_m

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

Definition at line 295 of file sparse_matrix_test.h.

◆ global_n

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

Definition at line 295 of file sparse_matrix_test.h.

◆ libmesh_suite_name

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

Definition at line 289 of file sparse_matrix_test.h.

◆ local_m

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

Definition at line 295 of file sparse_matrix_test.h.

◆ local_n

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

Definition at line 295 of file sparse_matrix_test.h.

◆ matrix

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

Definition at line 293 of file sparse_matrix_test.h.

◆ my_comm

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

Definition at line 291 of file sparse_matrix_test.h.

◆ nonsquare

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

Definition at line 295 of file sparse_matrix_test.h.


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