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 
26  CPPUNIT_TEST( testPetscOperations );
27 
28  CPPUNIT_TEST_SUITE_END();
29 
30  void testGetArray()
31  {
32  LOG_UNIT_TEST;
33 
34  unsigned int min_block_size = 2;
35 
36  // a different size on each processor.
37  unsigned int my_p = my_comm->rank();
38  unsigned int local_size = (min_block_size + my_p);
39  unsigned int global_size = 0;
40  unsigned int my_offset = 0;
41 
42  for (libMesh::processor_id_type p=0; p<my_comm->size(); p++)
43  {
44  const unsigned int p_size =
45  (min_block_size + static_cast<unsigned int>(p));
46  global_size += p_size;
47  if (p < my_p)
48  my_offset += p_size;
49  }
50 
51  PetscVector<Number> v(*my_comm, global_size, local_size);
52 
53  PetscScalar * values = v.get_array();
54 
55  for (unsigned int i=0; i<local_size; i++)
56  values[i] = i;
57 
58  v.restore_array();
59 
60  v.close();
61 
62  // Check the values through the interface
63  for (unsigned int i=0; i<local_size; i++)
64  LIBMESH_ASSERT_FP_EQUAL(i, std::abs(v(my_offset + i)), TOLERANCE*TOLERANCE);
65 
66  // Check that we can see the same thing with get_array_read
67  const PetscScalar * read_only_values = v.get_array_read();
68 
69  for (unsigned int i=0; i<local_size; i++)
70  LIBMESH_ASSERT_FP_EQUAL(i, std::abs(read_only_values[i]), TOLERANCE*TOLERANCE);
71 
72  v.restore_array();
73 
74  // Test getting a read only array after getting a writable array
75  values = v.get_array();
76  read_only_values = v.get_array_read();
77  CPPUNIT_ASSERT_EQUAL(read_only_values, const_cast<const PetscScalar *>(values));
78 
79  v.restore_array();
80 
81  // Test to make sure we can get arrays after other operators
82  for (unsigned int i = 0; i < local_size; i++)
83  v.set(my_offset + i, i * 2.0);
84  v.close();
85  for (unsigned int i = 0; i < local_size; i++)
86  LIBMESH_ASSERT_FP_EQUAL(i * 2.0, std::abs(v(my_offset + i)), TOLERANCE * TOLERANCE);
87  values = v.get_array();
88  read_only_values = v.get_array_read();
89  for (unsigned int i = 0; i < local_size; i++)
90  {
91  LIBMESH_ASSERT_FP_EQUAL(i * 2.0, std::abs(values[i]), TOLERANCE * TOLERANCE);
92  LIBMESH_ASSERT_FP_EQUAL(i * 2.0, std::abs(read_only_values[i]), TOLERANCE * TOLERANCE);
93  }
94  v.restore_array();
95  }
96 
98  {
99  PetscVector<Number> v1(*my_comm, global_size, local_size);
100  auto v2 = v1.clone();
101 
103  first = v1.first_local_index(),
104  last = v1.last_local_index();
105 
106  for (libMesh::dof_id_type n=first; n != last; n++)
107  {
108  v1.set (n, static_cast<libMesh::Number>(n+1));
109  v2->set (n, static_cast<libMesh::Number>(2*(n+1)));
110  }
111  v1.close();
112  v2->close();
113 
114  auto v_working_ptr = v1.clone();
115  auto & v_working = *v_working_ptr;
116 
117  v_working.pointwise_mult(v1, *v2);
118 
119  for (libMesh::dof_id_type n=first; n != last; n++)
120  LIBMESH_ASSERT_NUMBERS_EQUAL
121  (v_working(n), libMesh::Real((n+1)*2*(n+1)),
123 
124  v_working.pointwise_divide(v1, *v2);
125 
126  for (libMesh::dof_id_type n=first; n != last; n++)
127  LIBMESH_ASSERT_NUMBERS_EQUAL
128  (v_working(n), libMesh::Real(0.5),
130  }
131 
132 };
133 
135 
136 #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
Definition: petsc_vector.h:997
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:202
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:951
libMesh::PerfLog * unitlog
Definition: driver.C:173
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:869
uint8_t dof_id_type
Definition: id_types.h:67