libMesh
petsc_vector_test.C
Go to the documentation of this file.
1 #include <libmesh/petsc_vector.h>
2 
3 #ifdef LIBMESH_HAVE_PETSC
4 
5 #include "numeric_vector_test.h"
6 
7 
8 using namespace libMesh;
9 
10 class PetscVectorTest : public NumericVectorTest<PetscVector<Number>> {
11 public:
15  this->libmesh_suite_name = "NumericVectorTest";
16  else
17  this->libmesh_suite_name = "PetscVectorTest";
18  }
19 
20  CPPUNIT_TEST_SUITE( PetscVectorTest );
21 
22  NUMERICVECTORTEST
23 
24  CPPUNIT_TEST( testGetArray );
25  CPPUNIT_TEST( testPetscOperations );
26  CPPUNIT_TEST( testSubvectors );
27  CPPUNIT_TEST( testSubvectorsBase );
28 
29  CPPUNIT_TEST_SUITE_END();
30 
31  void testGetArray()
32  {
33  LOG_UNIT_TEST;
34 
35  unsigned int min_block_size = 2;
36 
37  // a different size on each processor.
38  unsigned int my_p = my_comm->rank();
39  unsigned int local_size = (min_block_size + my_p);
40  unsigned int global_size = 0;
41  unsigned int my_offset = 0;
42 
43  for (libMesh::processor_id_type p=0; p<my_comm->size(); p++)
44  {
45  const unsigned int p_size =
46  (min_block_size + static_cast<unsigned int>(p));
47  global_size += p_size;
48  if (p < my_p)
49  my_offset += p_size;
50  }
51 
52  PetscVector<Number> v(*my_comm, global_size, local_size);
53 
54  PetscScalar * values = v.get_array();
55 
56  for (unsigned int i=0; i<local_size; i++)
57  values[i] = i;
58 
59  v.restore_array();
60 
61  v.close();
62 
63  // Check the values through the interface
64  for (unsigned int i=0; i<local_size; i++)
65  LIBMESH_ASSERT_FP_EQUAL(i, std::abs(v(my_offset + i)), TOLERANCE*TOLERANCE);
66 
67  // Check that we can see the same thing with get_array_read
68  const PetscScalar * read_only_values = v.get_array_read();
69 
70  for (unsigned int i=0; i<local_size; i++)
71  LIBMESH_ASSERT_FP_EQUAL(i, std::abs(read_only_values[i]), TOLERANCE*TOLERANCE);
72 
73  v.restore_array();
74 
75  // Test getting a read only array after getting a writable array
76  values = v.get_array();
77  read_only_values = v.get_array_read();
78  CPPUNIT_ASSERT_EQUAL(read_only_values, const_cast<const PetscScalar *>(values));
79 
80  v.restore_array();
81 
82  // Test to make sure we can get arrays after other operators
83  for (unsigned int i = 0; i < local_size; i++)
84  v.set(my_offset + i, i * 2.0);
85  v.close();
86  for (unsigned int i = 0; i < local_size; i++)
87  LIBMESH_ASSERT_FP_EQUAL(i * 2.0, std::abs(v(my_offset + i)), TOLERANCE * TOLERANCE);
88  values = v.get_array();
89  read_only_values = v.get_array_read();
90  for (unsigned int i = 0; i < local_size; i++)
91  {
92  LIBMESH_ASSERT_FP_EQUAL(i * 2.0, std::abs(values[i]), TOLERANCE * TOLERANCE);
93  LIBMESH_ASSERT_FP_EQUAL(i * 2.0, std::abs(read_only_values[i]), TOLERANCE * TOLERANCE);
94  }
95  v.restore_array();
96  }
97 
99  {
100  PetscVector<Number> v1(*my_comm, global_size, local_size);
101  auto v2 = v1.clone();
102 
104  first = v1.first_local_index(),
105  last = v1.last_local_index();
106 
107  for (libMesh::dof_id_type n=first; n != last; n++)
108  {
109  v1.set (n, static_cast<libMesh::Number>(n+1));
110  v2->set (n, static_cast<libMesh::Number>(2*(n+1)));
111  }
112  v1.close();
113  v2->close();
114 
115  auto v_working_ptr = v1.clone();
116  auto & v_working = *v_working_ptr;
117 
118  v_working.pointwise_mult(v1, *v2);
119 
120  for (libMesh::dof_id_type n=first; n != last; n++)
121  LIBMESH_ASSERT_NUMBERS_EQUAL
122  (v_working(n), libMesh::Real((n+1)*2*(n+1)),
124 
125  v_working.pointwise_divide(v1, *v2);
126 
127  for (libMesh::dof_id_type n=first; n != last; n++)
128  LIBMESH_ASSERT_NUMBERS_EQUAL
129  (v_working(n), libMesh::Real(0.5),
131  }
132 
133 };
134 
136 
137 #endif // #ifdef LIBMESH_HAVE_PETSC
This class provides a nice interface to PETSc&#39;s Vec object.
Definition: petsc_vector.h:73
static constexpr Real TOLERANCE
virtual numeric_index_type first_local_index() const override
The libMesh namespace provides an interface to certain functionality in the library.
uint8_t processor_id_type
Definition: id_types.h:104
CPPUNIT_TEST_SUITE_REGISTRATION(PetscVectorTest)
bool summarized_logs_enabled()
Definition: perf_log.h:210
const PetscScalar * get_array_read() const
Get read only access to the raw PETSc Vector data array.
virtual numeric_index_type last_local_index() const override
void restore_array()
Restore the data array.
virtual void set(const numeric_index_type i, const T value) override
Sets v(i) = value.
Definition: petsc_vector.C:124
virtual std::unique_ptr< NumericVector< T > > clone() const override
Definition: petsc_vector.h:993
libMesh::PerfLog * unitlog
Definition: driver.C:220
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
PetscScalar * get_array()
Get read/write access to the raw PETSc Vector data array.
virtual void close() override
Calls the NumericVector&#39;s internal assembly routines, ensuring that the values are consistent across ...
Definition: petsc_vector.h:911
uint8_t dof_id_type
Definition: id_types.h:67