libMesh
Public Member Functions | Protected Attributes | List of all members
NumericVectorTest< DerivedClass > Class Template Reference

#include <numeric_vector_test.h>

Inheritance diagram for NumericVectorTest< DerivedClass >:
[legend]

Public Member Functions

void setUp ()
 
void tearDown ()
 
template<class Base , class Derived >
void Operations ()
 
template<class Base , class Derived >
void WriteAndRead ()
 
template<class Base , class Derived >
void Norms ()
 
template<class Base , class Derived >
void Localize (bool to_one=false)
 
template<class Base , class Derived >
void LocalizeIndices ()
 
void testLocalize ()
 
void testLocalizeBase ()
 
void testLocalizeToOne ()
 
void testLocalizeToOneBase ()
 
void testLocalizeIndices ()
 
void testLocalizeIndicesBase ()
 
void testNorms ()
 
void testNormsBase ()
 
void testOperations ()
 
void testOperationsBase ()
 
void testWriteAndRead ()
 
void testWriteAndReadBase ()
 

Protected Attributes

libMesh::Parallel::Communicatormy_comm = nullptr
 
std::string libmesh_suite_name
 
unsigned int block_size
 
unsigned int local_size
 
unsigned int global_size
 

Detailed Description

template<class DerivedClass>
class NumericVectorTest< DerivedClass >

Definition at line 31 of file numeric_vector_test.h.

Member Function Documentation

◆ Localize()

template<class DerivedClass>
template<class Base , class Derived >
void NumericVectorTest< DerivedClass >::Localize ( bool  to_one = false)
inline

Definition at line 245 of file numeric_vector_test.h.

246  {
247  const libMesh::processor_id_type root_pid = 0;
248 
249  {
250  auto v_ptr = std::make_unique<Derived>(*my_comm, global_size, local_size);
251  Base & v = *v_ptr;
252  std::vector<libMesh::Number> l(global_size);
253 
255  first = v.first_local_index(),
256  last = v.last_local_index();
257 
258  for (libMesh::dof_id_type n=first; n != last; n++)
259  v.set (n, static_cast<libMesh::Number>(n));
260  v.close();
261  for (libMesh::dof_id_type n=first; n != last; n++)
262  v.add (n, static_cast<libMesh::Number>(n));
263  v.close();
264 
265  if (!to_one)
266  v.localize(l);
267  else
268  v.localize_to_one(l,root_pid);
269 
270  if (!to_one || my_comm->rank() == root_pid)
271  // Yes I really mean v.size()
272  for (libMesh::dof_id_type i=0; i<v.size(); i++)
273  LIBMESH_ASSERT_NUMBERS_EQUAL
275 
276  for (libMesh::dof_id_type n=first; n != last; n++)
277  {
278  const auto value = static_cast<libMesh::Number>(n);
279  v.insert (&value, std::vector<libMesh::numeric_index_type>({n}));
280  }
281  v.close();
282 
283  if (!to_one)
284  v.localize(l);
285  else
286  v.localize_to_one(l,root_pid);
287 
288  if (!to_one || my_comm->rank() == root_pid)
289  // Yes I really mean v.size()
290  for (libMesh::dof_id_type i=0; i<v.size(); i++)
291  LIBMESH_ASSERT_NUMBERS_EQUAL
293  }
294  }
static constexpr Real TOLERANCE
processor_id_type rank() const
uint8_t processor_id_type
Definition: id_types.h:104
libMesh::Parallel::Communicator * my_comm
unsigned int global_size
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
static const bool value
Definition: xdr_io.C:54
uint8_t dof_id_type
Definition: id_types.h:67

◆ LocalizeIndices()

template<class DerivedClass>
template<class Base , class Derived >
void NumericVectorTest< DerivedClass >::LocalizeIndices ( )
inline

Definition at line 298 of file numeric_vector_test.h.

299  {
300  {
301  auto v_ptr = std::make_unique<Derived>(*my_comm, global_size, local_size);
302  Base & v = *v_ptr;
303 
304  // Let's try pulling the same number of entries from each processor
305  std::vector<libMesh::Number> values(block_size * my_comm->size());
306  std::vector<libMesh::dof_id_type> indices;
307  indices.reserve(block_size * my_comm->size());
308 
310  first = v.first_local_index(),
311  last = v.last_local_index();
312 
313  for (libMesh::dof_id_type n=first; n != last; n++)
314  v.set (n, static_cast<libMesh::Number>(n));
315  v.close();
316 
317  libMesh::dof_id_type end_index = 0;
318  for (libMesh::processor_id_type p=0; p<my_comm->size(); p++)
319  {
320  end_index += block_size + p;
321  for (unsigned int j = 0; j != block_size; ++j)
322  indices.push_back(end_index-j-1);
323  }
324 
325  v.localize(values, indices);
326 
327  end_index = 0;
328  for (libMesh::processor_id_type p=0; p<my_comm->size(); p++)
329  {
330  end_index += block_size + p;
331  for (unsigned int j = 0; j != block_size; ++j)
332  LIBMESH_ASSERT_NUMBERS_EQUAL
333  (values[p*block_size+j], libMesh::Real(end_index-j-1),
335  }
336  }
337  }
static constexpr Real TOLERANCE
uint8_t processor_id_type
Definition: id_types.h:104
processor_id_type size() const
libMesh::Parallel::Communicator * my_comm
unsigned int global_size
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
uint8_t dof_id_type
Definition: id_types.h:67

◆ Norms()

template<class DerivedClass>
template<class Base , class Derived >
void NumericVectorTest< DerivedClass >::Norms ( )
inline

Definition at line 191 of file numeric_vector_test.h.

192  {
193  auto v_ptr = std::make_unique<Derived>(*my_comm, global_size, local_size);
194  Base & v = *v_ptr;
195 
197  first = v.first_local_index(),
198  last = v.last_local_index();
199 
200  for (libMesh::dof_id_type n=first; n != last; n++)
201  v.set (n, -static_cast<libMesh::Number>(n));
202  v.close();
203  for (libMesh::dof_id_type n=first; n != last; n++)
204  v.add (n, -static_cast<libMesh::Number>(n));
205  v.close();
206 
207  const libMesh::Real exact_l1 =
209  const libMesh::Real exact_l2 =
210  std::sqrt(libMesh::Real(global_size-1) *
211  (2*global_size) *
212  (2*global_size-1) / 3);
213  LIBMESH_ASSERT_NUMBERS_EQUAL(v.sum(), -exact_l1,
215  LIBMESH_ASSERT_FP_EQUAL(v.l1_norm(), exact_l1,
217  LIBMESH_ASSERT_FP_EQUAL(v.l2_norm(), exact_l2,
219  LIBMESH_ASSERT_FP_EQUAL(v.linfty_norm(), 2*(global_size-libMesh::Real(1)),
221 
222  auto u_ptr = std::make_unique<Derived>(*my_comm, global_size, local_size);
223  Base & u = *u_ptr;
224  for (libMesh::dof_id_type n=first; n != last; n++)
225  u.set (n, -static_cast<libMesh::Number>(n * n));
226  u.close();
227 
228  Derived diff_derived(*my_comm, global_size, local_size);
229  Base & diff = diff_derived;
230  diff = u;
231  diff -= v;
232  diff.close();
233 
234  // Use a relative tolerance here; the norms are O(1e5) when our
235  // processor count gets big enough, at which point O(1e-12) is
236  // testing for exact equality...
237  const auto diff_norm = diff.l2_norm();
238  const auto norm_diff = u.l2_norm_diff(v);
239  LIBMESH_ASSERT_FP_EQUAL(diff_norm, norm_diff,
240  (std::abs(diff_norm)+std::abs(norm_diff)) *
242  }
static constexpr Real TOLERANCE
libMesh::Parallel::Communicator * my_comm
unsigned int global_size
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
uint8_t dof_id_type
Definition: id_types.h:67

◆ Operations()

template<class DerivedClass>
template<class Base , class Derived >
void NumericVectorTest< DerivedClass >::Operations ( )
inline

Definition at line 64 of file numeric_vector_test.h.

65  {
66  auto v_ptr = std::make_unique<Derived>(*my_comm, global_size, local_size);
67  Base & v = *v_ptr;
68 
70  first = v.first_local_index(),
71  last = v.last_local_index();
72 
73  for (libMesh::dof_id_type n=first; n != last; n++)
74  v.set (n, static_cast<libMesh::Number>(n+1));
75  v.close();
76 
77  auto v_clone = v.clone();
78  auto & vorig = *v_clone;
79 
80  CPPUNIT_ASSERT(relative_fuzzy_equals(v, vorig));
81 
82  v += v;
83 
84  CPPUNIT_ASSERT(!relative_fuzzy_equals(v, vorig));
85 
86  for (libMesh::dof_id_type n=first; n != last; n++)
87  LIBMESH_ASSERT_NUMBERS_EQUAL
88  (v(n), libMesh::Real(2*n+2),
90 
91  v *= v;
92 
93  for (libMesh::dof_id_type n=first; n != last; n++)
94  LIBMESH_ASSERT_NUMBERS_EQUAL
95  (v(n), libMesh::Real(4*n*n+8*n+4),
97 
98  v -= vorig;
99 
100  for (libMesh::dof_id_type n=first; n != last; n++)
101  LIBMESH_ASSERT_NUMBERS_EQUAL
102  (v(n), libMesh::Real(4*n*n+7*n+3),
104 
105  v /= vorig;
106 
107  for (libMesh::dof_id_type n=first; n != last; n++)
108  LIBMESH_ASSERT_NUMBERS_EQUAL
109  (v(n), libMesh::Real(4*n+3),
111 
112  v.add(1);
113  for (libMesh::dof_id_type n=first; n != last; n++)
114  LIBMESH_ASSERT_NUMBERS_EQUAL
115  (v(n), libMesh::Real(4*n+4),
117 
118  v.add(2, vorig);
119  for (libMesh::dof_id_type n=first; n != last; n++)
120  LIBMESH_ASSERT_NUMBERS_EQUAL
121  (v(n), libMesh::Real(6*n+6),
123 
124  v.scale(1/libMesh::Real(3));
125  for (libMesh::dof_id_type n=first; n != last; n++)
126  LIBMESH_ASSERT_NUMBERS_EQUAL
127  (v(n), libMesh::Real(2*n+2),
129 
130  v = 4;
131  for (libMesh::dof_id_type n=first; n != last; n++)
132  LIBMESH_ASSERT_NUMBERS_EQUAL
133  (v(n), libMesh::Real(4),
135 
136  v = vorig;
137  for (libMesh::dof_id_type n=first; n != last; n++)
138  LIBMESH_ASSERT_NUMBERS_EQUAL
139  (v(n), libMesh::Real(n+1),
141 
142  v.reciprocal();
143  for (libMesh::dof_id_type n=first; n != last; n++)
144  LIBMESH_ASSERT_NUMBERS_EQUAL
145  (v(n), 1/libMesh::Real(n+1),
147 
148  LIBMESH_ASSERT_NUMBERS_EQUAL
149  (v.dot(vorig), libMesh::Real(global_size),
151  }
static constexpr Real TOLERANCE
libMesh::Parallel::Communicator * my_comm
unsigned int global_size
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
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
uint8_t dof_id_type
Definition: id_types.h:67

◆ setUp()

template<class DerivedClass>
void NumericVectorTest< DerivedClass >::setUp ( )
inline

Definition at line 41 of file numeric_vector_test.h.

42  {
43  // By default we'll use the whole communicator in parallel;
44  // Serial-only NumericVector subclasses will need to override
45  // this and set something else first.
46  if (!my_comm)
48 
49  block_size = 10;
50 
51  // a different size on each processor.
53  static_cast<unsigned int>(my_comm->rank());
54 
55  global_size = 0;
56  for (libMesh::processor_id_type p=0; p<my_comm->size(); p++)
57  global_size += (block_size + static_cast<unsigned int>(p));
58  }
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:171
processor_id_type rank() const
uint8_t processor_id_type
Definition: id_types.h:104
processor_id_type size() const
libMesh::Parallel::Communicator * my_comm
unsigned int global_size

◆ tearDown()

template<class DerivedClass>
void NumericVectorTest< DerivedClass >::tearDown ( )
inline

Definition at line 60 of file numeric_vector_test.h.

61  {}

◆ testLocalize()

template<class DerivedClass>
void NumericVectorTest< DerivedClass >::testLocalize ( )
inline

Definition at line 339 of file numeric_vector_test.h.

340  {
341  LOG_UNIT_TEST;
342 
343  Localize<DerivedClass,DerivedClass>();
344  }

◆ testLocalizeBase()

template<class DerivedClass>
void NumericVectorTest< DerivedClass >::testLocalizeBase ( )
inline

Definition at line 346 of file numeric_vector_test.h.

347  {
348  LOG_UNIT_TEST;
349 
350  Localize<libMesh::NumericVector<libMesh::Number>,DerivedClass>();
351  }

◆ testLocalizeIndices()

template<class DerivedClass>
void NumericVectorTest< DerivedClass >::testLocalizeIndices ( )
inline

Definition at line 367 of file numeric_vector_test.h.

368  {
369  LOG_UNIT_TEST;
370 
371  LocalizeIndices<DerivedClass,DerivedClass >();
372  }

◆ testLocalizeIndicesBase()

template<class DerivedClass>
void NumericVectorTest< DerivedClass >::testLocalizeIndicesBase ( )
inline

Definition at line 374 of file numeric_vector_test.h.

375  {
376  LOG_UNIT_TEST;
377 
378  LocalizeIndices<libMesh::NumericVector<libMesh::Number>,DerivedClass>();
379  }

◆ testLocalizeToOne()

template<class DerivedClass>
void NumericVectorTest< DerivedClass >::testLocalizeToOne ( )
inline

Definition at line 353 of file numeric_vector_test.h.

354  {
355  LOG_UNIT_TEST;
356 
357  Localize<DerivedClass,DerivedClass >(true);
358  }

◆ testLocalizeToOneBase()

template<class DerivedClass>
void NumericVectorTest< DerivedClass >::testLocalizeToOneBase ( )
inline

Definition at line 360 of file numeric_vector_test.h.

361  {
362  LOG_UNIT_TEST;
363 
364  Localize<libMesh::NumericVector<libMesh::Number>,DerivedClass>(true);
365  }

◆ testNorms()

template<class DerivedClass>
void NumericVectorTest< DerivedClass >::testNorms ( )
inline

Definition at line 381 of file numeric_vector_test.h.

382  {
383  LOG_UNIT_TEST;
384 
385  Norms<DerivedClass,DerivedClass >();
386  }

◆ testNormsBase()

template<class DerivedClass>
void NumericVectorTest< DerivedClass >::testNormsBase ( )
inline

Definition at line 388 of file numeric_vector_test.h.

389  {
390  LOG_UNIT_TEST;
391 
392  Norms<libMesh::NumericVector<libMesh::Number>,DerivedClass>();
393  }

◆ testOperations()

template<class DerivedClass>
void NumericVectorTest< DerivedClass >::testOperations ( )
inline

Definition at line 395 of file numeric_vector_test.h.

396  {
397  LOG_UNIT_TEST;
398 
399  Operations<DerivedClass,DerivedClass >();
400  }

◆ testOperationsBase()

template<class DerivedClass>
void NumericVectorTest< DerivedClass >::testOperationsBase ( )
inline

Definition at line 402 of file numeric_vector_test.h.

403  {
404  LOG_UNIT_TEST;
405 
406  Operations<libMesh::NumericVector<libMesh::Number>,DerivedClass>();
407  }

◆ testWriteAndRead()

template<class DerivedClass>
void NumericVectorTest< DerivedClass >::testWriteAndRead ( )
inline

Definition at line 409 of file numeric_vector_test.h.

410  {
411  LOG_UNIT_TEST;
412 
413  WriteAndRead<DerivedClass,DerivedClass >();
414  }

◆ testWriteAndReadBase()

template<class DerivedClass>
void NumericVectorTest< DerivedClass >::testWriteAndReadBase ( )
inline

Definition at line 416 of file numeric_vector_test.h.

417  {
418  LOG_UNIT_TEST;
419 
420  WriteAndRead<libMesh::NumericVector<libMesh::Number>,DerivedClass>();
421  }

◆ WriteAndRead()

template<class DerivedClass>
template<class Base , class Derived >
void NumericVectorTest< DerivedClass >::WriteAndRead ( )
inline

Definition at line 154 of file numeric_vector_test.h.

155  {
156  auto v_ptr = std::make_unique<Derived>(*my_comm, global_size, local_size);
157  Base & v = *v_ptr;
158 
160  first = v.first_local_index(),
161  last = v.last_local_index();
162 
163  for (libMesh::dof_id_type n=first; n != last; n++)
164  v.set (n, static_cast<libMesh::Number>(n+1));
165  v.close();
166 
167  v.print_matlab(libmesh_suite_name+"_vector.m");
168 
169  // Let's make sure we don't have any race conditions; we have
170  // multiple SparseMatrix subclasses that might be trying to read
171  // and write the same file.
172 
174 
175 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
176  // We're not supporting complex reads quite yet
177  return;
178 #endif
179 
180  auto v_new_ptr = std::make_unique<Derived>(*my_comm, global_size, local_size);
181  Base & v_new = *v_new_ptr;
182 
183  v_new.read_matlab(libmesh_suite_name+"_vector.m");
184 
186 
187  CPPUNIT_ASSERT_EQUAL(v.l1_norm_diff(v_new), libMesh::Real(0));
188  }
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:171
void barrier() const
libMesh::Parallel::Communicator * my_comm
unsigned int global_size
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::string libmesh_suite_name
uint8_t dof_id_type
Definition: id_types.h:67

Member Data Documentation

◆ block_size

template<class DerivedClass>
unsigned int NumericVectorTest< DerivedClass >::block_size
protected

◆ global_size

template<class DerivedClass>
unsigned int NumericVectorTest< DerivedClass >::global_size
protected

◆ libmesh_suite_name

template<class DerivedClass>
std::string NumericVectorTest< DerivedClass >::libmesh_suite_name
protected

◆ local_size

template<class DerivedClass>
unsigned int NumericVectorTest< DerivedClass >::local_size
protected

◆ my_comm

template<class DerivedClass>
libMesh::Parallel::Communicator* NumericVectorTest< DerivedClass >::my_comm = nullptr
protected

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