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 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 ()
 

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 28 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 201 of file numeric_vector_test.h.

202  {
203  const libMesh::processor_id_type root_pid = 0;
204 
205  {
206  auto v_ptr = std::make_unique<Derived>(*my_comm, global_size, local_size);
207  Base & v = *v_ptr;
208  std::vector<libMesh::Number> l(global_size);
209 
211  first = v.first_local_index(),
212  last = v.last_local_index();
213 
214  for (libMesh::dof_id_type n=first; n != last; n++)
215  v.set (n, static_cast<libMesh::Number>(n));
216  v.close();
217  for (libMesh::dof_id_type n=first; n != last; n++)
218  v.add (n, static_cast<libMesh::Number>(n));
219  v.close();
220 
221  if (!to_one)
222  v.localize(l);
223  else
224  v.localize_to_one(l,root_pid);
225 
226  if (!to_one || my_comm->rank() == root_pid)
227  // Yes I really mean v.size()
228  for (libMesh::dof_id_type i=0; i<v.size(); i++)
229  LIBMESH_ASSERT_FP_EQUAL(2.*libMesh::libmesh_real(i),
230  libMesh::libmesh_real(l[i]),
232 
233  for (libMesh::dof_id_type n=first; n != last; n++)
234  {
235  const auto value = static_cast<libMesh::Number>(n);
236  v.insert (&value, std::vector<libMesh::numeric_index_type>({n}));
237  }
238  v.close();
239 
240  if (!to_one)
241  v.localize(l);
242  else
243  v.localize_to_one(l,root_pid);
244 
245  if (!to_one || my_comm->rank() == root_pid)
246  // Yes I really mean v.size()
247  for (libMesh::dof_id_type i=0; i<v.size(); i++)
248  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(i),
249  libMesh::libmesh_real(l[i]),
251  }
252  }
T libmesh_real(T a)
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
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 256 of file numeric_vector_test.h.

257  {
258  {
259  auto v_ptr = std::make_unique<Derived>(*my_comm, global_size, local_size);
260  Base & v = *v_ptr;
261 
262  // Let's try pulling the same number of entries from each processor
263  std::vector<libMesh::Number> values(block_size * my_comm->size());
264  std::vector<libMesh::dof_id_type> indices;
265  indices.reserve(block_size * my_comm->size());
266 
268  first = v.first_local_index(),
269  last = v.last_local_index();
270 
271  for (libMesh::dof_id_type n=first; n != last; n++)
272  v.set (n, static_cast<libMesh::Number>(n));
273  v.close();
274 
275  libMesh::dof_id_type end_index = 0;
276  for (libMesh::processor_id_type p=0; p<my_comm->size(); p++)
277  {
278  end_index += block_size + p;
279  for (unsigned int j = 0; j != block_size; ++j)
280  indices.push_back(end_index-j-1);
281  }
282 
283  v.localize(values, indices);
284 
285  end_index = 0;
286  for (libMesh::processor_id_type p=0; p<my_comm->size(); p++)
287  {
288  end_index += block_size + p;
289  for (unsigned int j = 0; j != block_size; ++j)
290  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(values[p*block_size+j]),
291  libMesh::libmesh_real(end_index-j-1),
293  }
294  }
295  }
T libmesh_real(T a)
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
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 147 of file numeric_vector_test.h.

148  {
149  auto v_ptr = std::make_unique<Derived>(*my_comm, global_size, local_size);
150  Base & v = *v_ptr;
151 
153  first = v.first_local_index(),
154  last = v.last_local_index();
155 
156  for (libMesh::dof_id_type n=first; n != last; n++)
157  v.set (n, -static_cast<libMesh::Number>(n));
158  v.close();
159  for (libMesh::dof_id_type n=first; n != last; n++)
160  v.add (n, -static_cast<libMesh::Number>(n));
161  v.close();
162 
163  const libMesh::Real exact_l1 =
165  const libMesh::Real exact_l2 =
167  (2*global_size) *
168  (2*global_size-1) / 3);
169  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(v.sum()), -exact_l1,
171  LIBMESH_ASSERT_FP_EQUAL(v.l1_norm(), exact_l1,
173  LIBMESH_ASSERT_FP_EQUAL(v.l2_norm(), exact_l2,
175  LIBMESH_ASSERT_FP_EQUAL(v.linfty_norm(), 2*(global_size-libMesh::Real(1)),
177 
178  auto u_ptr = std::make_unique<Derived>(*my_comm, global_size, local_size);
179  Base & u = *u_ptr;
180  for (libMesh::dof_id_type n=first; n != last; n++)
181  u.set (n, -static_cast<libMesh::Number>(n * n));
182  u.close();
183 
184  Derived diff_derived(*my_comm, global_size, local_size);
185  Base & diff = diff_derived;
186  diff = u;
187  diff -= v;
188  diff.close();
189 
190  // Use a relative tolerance here; the norms are O(1e5) when our
191  // processor count gets big enough, at which point O(1e-12) is
192  // testing for exact equality...
193  const auto diff_norm = diff.l2_norm();
194  const auto norm_diff = u.l2_norm_diff(v);
195  LIBMESH_ASSERT_FP_EQUAL(diff_norm, norm_diff,
196  (std::abs(diff_norm)+std::abs(norm_diff)) *
198  }
T libmesh_real(T a)
static constexpr Real TOLERANCE
ADRealEigenVector< T, D, asd > sqrt(const ADRealEigenVector< T, D, asd > &)
Definition: type_vector.h:53
ADRealEigenVector< T, D, asd > abs(const ADRealEigenVector< T, D, asd > &)
Definition: type_vector.h:57
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 61 of file numeric_vector_test.h.

62  {
63  auto v_ptr = std::make_unique<Derived>(*my_comm, global_size, local_size);
64  Base & v = *v_ptr;
65 
67  first = v.first_local_index(),
68  last = v.last_local_index();
69 
70  for (libMesh::dof_id_type n=first; n != last; n++)
71  v.set (n, static_cast<libMesh::Number>(n+1));
72  v.close();
73 
74  auto v_clone = v.clone();
75  auto & vorig = *v_clone;
76 
77  v += v;
78 
79  for (libMesh::dof_id_type n=first; n != last; n++)
80  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(v(n)),
81  libMesh::Real(2*n+2),
83 
84  v *= v;
85 
86  for (libMesh::dof_id_type n=first; n != last; n++)
87  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(v(n)),
88  libMesh::Real(4*n*n+8*n+4),
90 
91  v -= vorig;
92 
93  for (libMesh::dof_id_type n=first; n != last; n++)
94  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(v(n)),
95  libMesh::Real(4*n*n+7*n+3),
97 
98  v /= vorig;
99 
100  for (libMesh::dof_id_type n=first; n != last; n++)
101  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(v(n)),
102  libMesh::Real(4*n+3),
104 
105  v.add(1);
106  for (libMesh::dof_id_type n=first; n != last; n++)
107  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(v(n)),
108  libMesh::Real(4*n+4),
110 
111  v.add(2, vorig);
112  for (libMesh::dof_id_type n=first; n != last; n++)
113  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(v(n)),
114  libMesh::Real(6*n+6),
116 
117  v.scale(1/libMesh::Real(3));
118  for (libMesh::dof_id_type n=first; n != last; n++)
119  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(v(n)),
120  libMesh::Real(2*n+2),
122 
123  v = 4;
124  for (libMesh::dof_id_type n=first; n != last; n++)
125  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(v(n)),
126  libMesh::Real(4),
128 
129  v = vorig;
130  for (libMesh::dof_id_type n=first; n != last; n++)
131  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(v(n)),
132  libMesh::Real(n+1),
134 
135  v.reciprocal();
136  for (libMesh::dof_id_type n=first; n != last; n++)
137  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(v(n)),
138  1/libMesh::Real(n+1),
140 
141  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(v.dot(vorig)),
144  }
T libmesh_real(T a)
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

◆ setUp()

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

Definition at line 38 of file numeric_vector_test.h.

39  {
40  // By default we'll use the whole communicator in parallel;
41  // Serial-only NumericVector subclasses will need to override
42  // this and set something else first.
43  if (!my_comm)
45 
46  block_size = 10;
47 
48  // a different size on each processor.
50  static_cast<unsigned int>(my_comm->rank());
51 
52  global_size = 0;
53  for (libMesh::processor_id_type p=0; p<my_comm->size(); p++)
54  global_size += (block_size + static_cast<unsigned int>(p));
55  }
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:159
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 57 of file numeric_vector_test.h.

58  {}

◆ testLocalize()

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

Definition at line 297 of file numeric_vector_test.h.

298  {
299  LOG_UNIT_TEST;
300 
301  Localize<DerivedClass,DerivedClass>();
302  }

◆ testLocalizeBase()

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

Definition at line 304 of file numeric_vector_test.h.

305  {
306  LOG_UNIT_TEST;
307 
308  Localize<libMesh::NumericVector<libMesh::Number>,DerivedClass>();
309  }

◆ testLocalizeIndices()

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

Definition at line 325 of file numeric_vector_test.h.

326  {
327  LOG_UNIT_TEST;
328 
329  LocalizeIndices<DerivedClass,DerivedClass >();
330  }

◆ testLocalizeIndicesBase()

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

Definition at line 332 of file numeric_vector_test.h.

333  {
334  LOG_UNIT_TEST;
335 
336  LocalizeIndices<libMesh::NumericVector<libMesh::Number>,DerivedClass>();
337  }

◆ testLocalizeToOne()

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

Definition at line 311 of file numeric_vector_test.h.

312  {
313  LOG_UNIT_TEST;
314 
315  Localize<DerivedClass,DerivedClass >(true);
316  }

◆ testLocalizeToOneBase()

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

Definition at line 318 of file numeric_vector_test.h.

319  {
320  LOG_UNIT_TEST;
321 
322  Localize<libMesh::NumericVector<libMesh::Number>,DerivedClass>(true);
323  }

◆ testNorms()

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

Definition at line 339 of file numeric_vector_test.h.

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

◆ testNormsBase()

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

Definition at line 346 of file numeric_vector_test.h.

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

◆ testOperations()

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

Definition at line 353 of file numeric_vector_test.h.

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

◆ testOperationsBase()

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

Definition at line 360 of file numeric_vector_test.h.

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

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

Definition at line 33 of file numeric_vector_test.h.

◆ 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: