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:
12  CPPUNIT_TEST_SUITE( PetscVectorTest );
13 
14  NUMERICVECTORTEST
15 
16  CPPUNIT_TEST( testGetArray );
17 
18  CPPUNIT_TEST_SUITE_END();
19 
20  void testGetArray()
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  }
69 
70 };
71 
73 
74 #endif // #ifdef LIBMESH_HAVE_PETSC
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::PetscVector::restore_array
void restore_array()
Restore the data array.
Definition: petsc_vector.h:1142
libMesh::TOLERANCE
static const Real TOLERANCE
Definition: libmesh_common.h:128
std::abs
MetaPhysicL::DualNumber< T, D > abs(const MetaPhysicL::DualNumber< T, D > &in)
libMesh::PetscVector::close
virtual void close() override
Calls the NumericVector's internal assembly routines, ensuring that the values are consistent across ...
Definition: petsc_vector.h:821
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(PetscVectorTest)
PetscVectorTest::testGetArray
void testGetArray()
Definition: petsc_vector_test.C:20
numeric_vector_test.h
libMesh::PetscVector::get_array
PetscScalar * get_array()
Get read/write access to the raw PETSc Vector data array.
Definition: petsc_vector.h:1121
libMesh::processor_id_type
uint8_t processor_id_type
Definition: id_types.h:104
PetscVectorTest
Definition: petsc_vector_test.C:10
libMesh::PetscVector
This class provides a nice interface to PETSc's Vec object.
Definition: petsc_vector.h:72
NumericVectorTest
Definition: numeric_vector_test.h:27
libMesh::PetscVector::get_array_read
const PetscScalar * get_array_read() const
Get read only access to the raw PETSc Vector data array.
Definition: petsc_vector.h:1132