libMesh
Loading...
Searching...
No Matches
petsc_vector_test.C
Go to the documentation of this file.
1#include <libmesh/petsc_vector.h>
2
3#ifdef LIBMESH_HAVE_PETSC
4
6
7
8using namespace libMesh;
9
10class PetscVectorTest : public NumericVectorTest<PetscVector<Number>> {
11public:
15 this->libmesh_suite_name = "NumericVectorTest";
16 else
17 this->libmesh_suite_name = "PetscVectorTest";
18 }
19
21
22 NUMERICVECTORTEST
23
28
30
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
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 {
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
libMesh::Parallel::Communicator * my_comm
NUMERICVECTORTEST CPPUNIT_TEST(testGetArray)
CPPUNIT_TEST(testSubvectorsBase)
CPPUNIT_TEST_SUITE(PetscVectorTest)
CPPUNIT_TEST(testSubvectors)
CPPUNIT_TEST(testPetscOperations)
processor_id_type size() const
processor_id_type rank() const
bool summarized_logs_enabled()
Definition perf_log.h:210
This class provides a nice interface to PETSc's Vec object.
virtual void set(const numeric_index_type i, const T value) override
Sets v(i) = value.
virtual void close() override
Calls the NumericVector's internal assembly routines, ensuring that the values are consistent across ...
virtual numeric_index_type first_local_index() const override
void restore_array()
Restore the data array.
virtual numeric_index_type last_local_index() const override
virtual std::unique_ptr< NumericVector< T > > clone() const override
const PetscScalar * get_array_read() const
Get read only access to the raw PETSc Vector data array.
PetscScalar * get_array()
Get read/write access to the raw PETSc Vector data array.
libMesh::PerfLog * unitlog
Definition driver.C:220
The libMesh namespace provides an interface to certain functionality in the library.
static constexpr Real TOLERANCE
uint8_t dof_id_type
Definition id_types.h:67
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
uint8_t processor_id_type
Definition id_types.h:104
CPPUNIT_TEST_SUITE_REGISTRATION(PetscVectorTest)