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

Public Member Functions

 CPPUNIT_TEST_SUITE (PetscVectorTest)
 
NUMERICVECTORTEST CPPUNIT_TEST (testGetArray)
 
 CPPUNIT_TEST_SUITE_END ()
 
void testGetArray ()
 
void setUp ()
 
void tearDown ()
 
void Localize (bool to_one=false)
 
void LocalizeIndices ()
 
void testLocalize ()
 
void testLocalizeBase ()
 
void testLocalizeToOne ()
 
void testLocalizeToOneBase ()
 
void testLocalizeIndices ()
 
void testLocalizeIndicesBase ()
 

Protected Attributes

libMesh::Parallel::Communicator * my_comm
 

Detailed Description

Definition at line 10 of file petsc_vector_test.C.

Member Function Documentation

◆ CPPUNIT_TEST()

NUMERICVECTORTEST PetscVectorTest::CPPUNIT_TEST ( testGetArray  )

◆ CPPUNIT_TEST_SUITE()

PetscVectorTest::CPPUNIT_TEST_SUITE ( PetscVectorTest  )

◆ CPPUNIT_TEST_SUITE_END()

PetscVectorTest::CPPUNIT_TEST_SUITE_END ( )

◆ Localize()

void NumericVectorTest< PetscVector< Number > >::Localize ( bool  to_one = false)
inlineinherited

Definition at line 45 of file numeric_vector_test.h.

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  }

◆ LocalizeIndices()

void NumericVectorTest< PetscVector< Number > >::LocalizeIndices ( )
inlineinherited

Definition at line 87 of file numeric_vector_test.h.

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  }

◆ setUp()

void NumericVectorTest< PetscVector< Number > >::setUp ( )
inlineinherited

Definition at line 33 of file numeric_vector_test.h.

34  {
35  // By default we'll use the whole communicator in parallel;
36  // Serial-only NumericVector subclasses will need to override
37  // this.
39  }

◆ tearDown()

void NumericVectorTest< PetscVector< Number > >::tearDown ( )
inlineinherited

Definition at line 41 of file numeric_vector_test.h.

42  {}

◆ testGetArray()

void PetscVectorTest::testGetArray ( )
inline

Definition at line 20 of file petsc_vector_test.C.

21  {
22  unsigned int min_block_size = 2;
23 
24  // a different size on each processor.
25  unsigned int my_p = my_comm->rank();
26  unsigned int local_size = (min_block_size + my_p);
27  unsigned int global_size = 0;
28  unsigned int my_offset = 0;
29 
30  for (libMesh::processor_id_type p=0; p<my_comm->size(); p++)
31  {
32  const unsigned int p_size =
33  (min_block_size + static_cast<unsigned int>(p));
34  global_size += p_size;
35  if (p < my_p)
36  my_offset += p_size;
37  }
38 
39  PetscVector<Number> v(*my_comm, global_size, local_size);
40 
41  PetscScalar * values = v.get_array();
42 
43  for (unsigned int i=0; i<local_size; i++)
44  values[i] = i;
45 
46  v.restore_array();
47 
48  v.close();
49 
50  // Check the values through the interface
51  for (unsigned int i=0; i<local_size; i++)
52  LIBMESH_ASSERT_FP_EQUAL(std::abs(v(my_offset + i)), i, TOLERANCE*TOLERANCE);
53 
54  // Check that we can see the same thing with get_array_read
55  const PetscScalar * read_only_values = v.get_array_read();
56 
57  for (unsigned int i=0; i<local_size; i++)
58  LIBMESH_ASSERT_FP_EQUAL(std::abs(read_only_values[i]), i, TOLERANCE*TOLERANCE);
59 
60  v.restore_array();
61 
62  // Test getting a read only array after getting a writable array
63  values = v.get_array();
64  read_only_values = v.get_array_read();
65  CPPUNIT_ASSERT_EQUAL((intptr_t)read_only_values, (intptr_t)values);
66 
67  v.restore_array();
68  }

References std::abs(), libMesh::PetscVector< T >::close(), libMesh::PetscVector< T >::get_array(), libMesh::PetscVector< T >::get_array_read(), libMesh::PetscVector< T >::restore_array(), and libMesh::TOLERANCE.

◆ testLocalize()

void NumericVectorTest< PetscVector< Number > >::testLocalize ( )
inlineinherited

Definition at line 138 of file numeric_vector_test.h.

139  {
140  Localize<DerivedClass,DerivedClass>();
141  }

◆ testLocalizeBase()

void NumericVectorTest< PetscVector< Number > >::testLocalizeBase ( )
inlineinherited

Definition at line 143 of file numeric_vector_test.h.

144  {
145  Localize<libMesh::NumericVector<libMesh::Number>,DerivedClass>();
146  }

◆ testLocalizeIndices()

void NumericVectorTest< PetscVector< Number > >::testLocalizeIndices ( )
inlineinherited

Definition at line 158 of file numeric_vector_test.h.

159  {
160  LocalizeIndices<DerivedClass,DerivedClass >();
161  }

◆ testLocalizeIndicesBase()

void NumericVectorTest< PetscVector< Number > >::testLocalizeIndicesBase ( )
inlineinherited

Definition at line 163 of file numeric_vector_test.h.

164  {
165  LocalizeIndices<libMesh::NumericVector<libMesh::Number>,DerivedClass>();
166  }

◆ testLocalizeToOne()

void NumericVectorTest< PetscVector< Number > >::testLocalizeToOne ( )
inlineinherited

Definition at line 148 of file numeric_vector_test.h.

149  {
150  Localize<DerivedClass,DerivedClass >(true);
151  }

◆ testLocalizeToOneBase()

void NumericVectorTest< PetscVector< Number > >::testLocalizeToOneBase ( )
inlineinherited

Definition at line 153 of file numeric_vector_test.h.

154  {
155  Localize<libMesh::NumericVector<libMesh::Number>,DerivedClass>(true);
156  }

Member Data Documentation

◆ my_comm

libMesh::Parallel::Communicator* NumericVectorTest< PetscVector< Number > >::my_comm
protectedinherited

Definition at line 30 of file numeric_vector_test.h.


The documentation for this class was generated from the following file:
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
std::abs
MetaPhysicL::DualNumber< T, D > abs(const MetaPhysicL::DualNumber< T, D > &in)
TestCommWorld
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:111
libMesh::processor_id_type
uint8_t processor_id_type
Definition: id_types.h:104
libMesh::PetscVector
This class provides a nice interface to PETSc's Vec object.
Definition: petsc_vector.h:72
NumericVectorTest< PetscVector< Number > >::my_comm
libMesh::Parallel::Communicator * my_comm
Definition: numeric_vector_test.h:30