libMesh
numeric_vector_test.h
Go to the documentation of this file.
1 #ifndef __numeric_vector_test_h__
2 #define __numeric_vector_test_h__
3 
4 // test includes
5 #include "test_comm.h"
6 
7 // libMesh includes
8 #include <libmesh/parallel.h>
9 #include <libmesh/auto_ptr.h>
10 
11 // Ignore unused parameter warnings coming from cppuint headers
12 #include <libmesh/ignore_warnings.h>
13 #include <cppunit/extensions/HelperMacros.h>
14 #include <cppunit/TestCase.h>
15 #include <libmesh/restore_warnings.h>
16 
17 #define NUMERICVECTORTEST \
18  CPPUNIT_TEST( testLocalize ); \
19  CPPUNIT_TEST( testLocalizeBase ); \
20  CPPUNIT_TEST( testLocalizeToOne ); \
21  CPPUNIT_TEST( testLocalizeToOneBase );
22 
23 template <class DerivedClass>
24 class NumericVectorTest : public CppUnit::TestCase {
25 
26 protected:
28 
29 public:
30  void setUp()
31  {
32  // By default we'll use the whole communicator in parallel;
33  // Serial-only NumericVector subclasses will need to override
34  // this.
35  my_comm = TestCommWorld;
36  }
37 
38  void tearDown()
39  {}
40 
41  template <class Base, class Derived>
42  void Localize(bool to_one=false)
43  {
44  const libMesh::processor_id_type root_pid = 0;
45  unsigned int block_size = 10;
46 
47  // a different size on each processor.
48  unsigned int local_size = block_size +
49  static_cast<unsigned int>(my_comm->rank());
50  unsigned int global_size = 0;
51 
52  for (libMesh::processor_id_type p=0; p<my_comm->size(); p++)
53  global_size += (block_size + static_cast<unsigned int>(p));
54 
55  {
56  libMesh::UniquePtr<Base> v_ptr(new Derived(*my_comm, global_size, local_size));
57  Base & v = *v_ptr;
58  std::vector<libMesh::Number> l(global_size);
59 
61  first = v.first_local_index(),
62  last = v.last_local_index();
63 
64  for (libMesh::dof_id_type n=first; n != last; n++)
65  v.set (n, static_cast<libMesh::Number>(n));
66  v.close();
67 
68  if (!to_one)
69  v.localize(l);
70  else
71  v.localize_to_one(l,root_pid);
72 
73  if (!to_one || my_comm->rank() == root_pid)
74  // Yes I really mean v.size()
75  for (libMesh::dof_id_type i=0; i<v.size(); i++)
76  CPPUNIT_ASSERT_DOUBLES_EQUAL(libMesh::libmesh_real(i),
79  }
80  }
81 
82  void testLocalize()
83  {
84  Localize<DerivedClass,DerivedClass>();
85  }
86 
88  {
89  Localize<libMesh::NumericVector<libMesh::Number>,DerivedClass>();
90  }
91 
93  {
94  Localize<DerivedClass,DerivedClass >(true);
95  }
96 
98  {
99  Localize<libMesh::NumericVector<libMesh::Number>,DerivedClass>(true);
100  }
101 };
102 
103 #endif // #ifdef __numeric_vector_test_h__
T libmesh_real(T a)
Encapsulates the MPI_Comm object.
Definition: parallel.h:657
unsigned int size() const
Definition: parallel.h:726
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:28
uint8_t processor_id_type
Definition: id_types.h:99
static const Real TOLERANCE
std::unique_ptr< T > UniquePtr
Definition: auto_ptr.h:46
void Localize(bool to_one=false)
libMesh::Parallel::Communicator * my_comm
unsigned int rank() const
Definition: parallel.h:724
uint8_t dof_id_type
Definition: id_types.h:64