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" // libmesh_make_unique
10 
11 #include "libmesh_cppunit.h"
12 
13 
14 #define NUMERICVECTORTEST \
15  CPPUNIT_TEST( testLocalize ); \
16  CPPUNIT_TEST( testLocalizeBase ); \
17  CPPUNIT_TEST( testLocalizeIndices ); \
18  CPPUNIT_TEST( testLocalizeIndicesBase ); \
19  CPPUNIT_TEST( testLocalizeToOne ); \
20  CPPUNIT_TEST( testLocalizeToOneBase );
21 
22 #ifndef LIBMESH_HAVE_CXX14_MAKE_UNIQUE
24 #endif
25 
26 template <class DerivedClass>
27 class NumericVectorTest : public CppUnit::TestCase {
28 
29 protected:
30  libMesh::Parallel::Communicator *my_comm;
31 
32 public:
33  void setUp()
34  {
35  // By default we'll use the whole communicator in parallel;
36  // Serial-only NumericVector subclasses will need to override
37  // this.
39  }
40 
41  void tearDown()
42  {}
43 
44  template <class Base, class Derived>
45  void Localize(bool to_one=false)
46  {
47  const libMesh::processor_id_type root_pid = 0;
48  unsigned int block_size = 10;
49 
50  // a different size on each processor.
51  unsigned int local_size = block_size +
52  static_cast<unsigned int>(my_comm->rank());
53  unsigned int global_size = 0;
54 
55  for (libMesh::processor_id_type p=0; p<my_comm->size(); p++)
56  global_size += (block_size + static_cast<unsigned int>(p));
57 
58  {
59  auto v_ptr = libmesh_make_unique<Derived>(*my_comm, global_size, local_size);
60  Base & v = *v_ptr;
61  std::vector<libMesh::Number> l(global_size);
62 
64  first = v.first_local_index(),
65  last = v.last_local_index();
66 
67  for (libMesh::dof_id_type n=first; n != last; n++)
68  v.set (n, static_cast<libMesh::Number>(n));
69  v.close();
70 
71  if (!to_one)
72  v.localize(l);
73  else
74  v.localize_to_one(l,root_pid);
75 
76  if (!to_one || my_comm->rank() == root_pid)
77  // Yes I really mean v.size()
78  for (libMesh::dof_id_type i=0; i<v.size(); i++)
79  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(i),
82  }
83  }
84 
85 
86  template <class Base, class Derived>
88  {
89  unsigned int block_size = 10;
90 
91  // a different size on each processor.
92  unsigned int local_size = block_size +
93  static_cast<unsigned int>(my_comm->rank());
94  unsigned int global_size = 0;
95 
96  for (libMesh::processor_id_type p=0; p<my_comm->size(); p++)
97  global_size += (block_size + static_cast<unsigned int>(p));
98 
99  {
100  auto v_ptr = libmesh_make_unique<Derived>(*my_comm, global_size, local_size);
101  Base & v = *v_ptr;
102 
103  // Let's try pulling the same number of entries from each processor
104  std::vector<libMesh::Number> values(block_size * my_comm->size());
105  std::vector<libMesh::dof_id_type> indices;
106  indices.reserve(block_size * my_comm->size());
107 
109  first = v.first_local_index(),
110  last = v.last_local_index();
111 
112  for (libMesh::dof_id_type n=first; n != last; n++)
113  v.set (n, static_cast<libMesh::Number>(n));
114  v.close();
115 
116  libMesh::dof_id_type end_index = 0;
117  for (libMesh::processor_id_type p=0; p<my_comm->size(); p++)
118  {
119  end_index += block_size + p;
120  for (unsigned int j = 0; j != block_size; ++j)
121  indices.push_back(end_index-j-1);
122  }
123 
124  v.localize(values, indices);
125 
126  end_index = 0;
127  for (libMesh::processor_id_type p=0; p<my_comm->size(); p++)
128  {
129  end_index += block_size + p;
130  for (unsigned int j = 0; j != block_size; ++j)
131  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(values[p*block_size+j]),
132  libMesh::libmesh_real(end_index-j-1),
134  }
135  }
136  }
137 
139  {
140  Localize<DerivedClass,DerivedClass>();
141  }
142 
144  {
145  Localize<libMesh::NumericVector<libMesh::Number>,DerivedClass>();
146  }
147 
149  {
150  Localize<DerivedClass,DerivedClass >(true);
151  }
152 
154  {
155  Localize<libMesh::NumericVector<libMesh::Number>,DerivedClass>(true);
156  }
157 
159  {
160  LocalizeIndices<DerivedClass,DerivedClass >();
161  }
162 
164  {
165  LocalizeIndices<libMesh::NumericVector<libMesh::Number>,DerivedClass>();
166  }
167 };
168 
169 #endif // #ifdef __numeric_vector_test_h__
libMesh::dof_id_type
uint8_t dof_id_type
Definition: id_types.h:67
libMesh::libmesh_real
T libmesh_real(T a)
Definition: libmesh_common.h:166
libMesh::TOLERANCE
static const Real TOLERANCE
Definition: libmesh_common.h:128
NumericVectorTest::testLocalizeIndicesBase
void testLocalizeIndicesBase()
Definition: numeric_vector_test.h:163
TestCommWorld
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:111
NumericVectorTest::testLocalizeToOne
void testLocalizeToOne()
Definition: numeric_vector_test.h:148
libMesh::processor_id_type
uint8_t processor_id_type
Definition: id_types.h:104
NumericVectorTest::tearDown
void tearDown()
Definition: numeric_vector_test.h:41
NumericVectorTest::testLocalizeToOneBase
void testLocalizeToOneBase()
Definition: numeric_vector_test.h:153
NumericVectorTest::testLocalizeBase
void testLocalizeBase()
Definition: numeric_vector_test.h:143
NumericVectorTest::Localize
void Localize(bool to_one=false)
Definition: numeric_vector_test.h:45
NumericVectorTest::my_comm
libMesh::Parallel::Communicator * my_comm
Definition: numeric_vector_test.h:30
libMesh::make_unique
std::unique_ptr< T > make_unique(Args &&... args)
Definition: auto_ptr.h:45
libmesh_cppunit.h
NumericVectorTest::testLocalize
void testLocalize()
Definition: numeric_vector_test.h:138
NumericVectorTest
Definition: numeric_vector_test.h:27
test_comm.h
NumericVectorTest::testLocalizeIndices
void testLocalizeIndices()
Definition: numeric_vector_test.h:158
NumericVectorTest::LocalizeIndices
void LocalizeIndices()
Definition: numeric_vector_test.h:87
NumericVectorTest::setUp
void setUp()
Definition: numeric_vector_test.h:33