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 #include <libmesh/ignore_warnings.h>
8 #include <cppunit/extensions/HelperMacros.h>
9 #include <cppunit/TestCase.h>
10 #include <libmesh/restore_warnings.h>
11 
12 // THE CPPUNIT_TEST_SUITE_END macro expands to code that involves
13 // std::auto_ptr, which in turn produces -Wdeprecated-declarations
14 // warnings. These can be ignored in GCC as long as we wrap the
15 // offending code in appropriate pragmas. We can't get away with a
16 // single ignore_warnings.h inclusion at the beginning of this file,
17 // since the libmesh headers pull in a restore_warnings.h at some
18 // point. We also don't bother restoring warnings at the end of this
19 // file since it's not a header.
20 #include <libmesh/ignore_warnings.h>
21 
22 using namespace libMesh;
23 
24 class PetscVectorTest : public NumericVectorTest<PetscVector<Number>> {
25 public:
26  CPPUNIT_TEST_SUITE( PetscVectorTest );
27 
28  NUMERICVECTORTEST
29 
30  CPPUNIT_TEST( testGetArray );
31 
32  CPPUNIT_TEST_SUITE_END();
33 
34  void testGetArray()
35  {
36  unsigned int block_size = 2;
37 
38  // a different size on each processor.
39  unsigned int local_size = block_size;
40  unsigned int global_size = 0;
41 
42  for (libMesh::processor_id_type p=0; p<my_comm->size(); p++)
43  global_size += (block_size + static_cast<unsigned int>(p));
44 
45  PetscVector<Number> v(*my_comm, global_size, local_size);
46 
47  PetscScalar * values = v.get_array();
48 
49  for (unsigned int i=0; i<local_size; i++)
50  values[i] = i;
51 
52  v.restore_array();
53 
54  v.close();
55 
56  // Check the values through the interface
57  for (unsigned int i=0; i<local_size; i++)
58  CPPUNIT_ASSERT_DOUBLES_EQUAL(std::abs(v(my_comm->rank()*2 + i)), i, TOLERANCE*TOLERANCE);
59 
60  // Check that we can see the same thing with get_array_read
61  const PetscScalar * read_only_values = v.get_array_read();
62 
63  for (unsigned int i=0; i<local_size; i++)
64  CPPUNIT_ASSERT_DOUBLES_EQUAL(std::abs(read_only_values[i]), i, TOLERANCE*TOLERANCE);
65 
66  v.restore_array();
67 
68  // Test getting a read only array after getting a writable array
69  values = v.get_array();
70  read_only_values = v.get_array_read();
71  CPPUNIT_ASSERT_EQUAL((intptr_t)read_only_values, (intptr_t)values);
72 
73  v.restore_array();
74  }
75 
76 };
77 
79 
80 #endif // #ifdef LIBMESH_HAVE_PETSC
double abs(double a)
uint8_t processor_id_type
Definition: id_types.h:99
static const Real TOLERANCE
The libMesh namespace provides an interface to certain functionality in the library.
CPPUNIT_TEST_SUITE_REGISTRATION(PetscVectorTest)