libMesh
Loading...
Searching...
No Matches
petsc_matrix_shell_matrix_test.C
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18#include <libmesh/libmesh_config.h>
19
20#ifdef LIBMESH_HAVE_PETSC
21
22#include <libmesh/petsc_matrix_shell_matrix.h>
23#include <libmesh/petsc_vector.h>
24
25#include "test_comm.h"
26#include "libmesh_cppunit.h"
27
28using namespace libMesh;
29
30namespace
31{
32// A trivial MATOP_MULT: y = 2*x. Just enough to prove a PetscMatrixShellMatrix
33// is a genuinely usable MatMult operand, e.g. as a matrix-free SNES Jacobian.
34PetscErrorCode
35petsc_matrix_shell_matrix_test_mult(Mat, Vec x, Vec y)
36{
37 PetscFunctionBeginUser;
38 LibmeshPetscCallQ(VecCopy(x, y));
39 LibmeshPetscCallQ(VecScale(y, 2.));
40 PetscFunctionReturn(LIBMESH_PETSC_SUCCESS);
41}
42}
43
44class PetscMatrixShellMatrixTest : public CppUnit::TestCase
45{
46public:
48
52
54
55public:
56
57 void setUp() {}
58 void tearDown() {}
59
61 {
62 LOG_UNIT_TEST;
63
65 const numeric_index_type m = TestCommWorld->size();
66 mat.init(m, m, /*m_l=*/1, /*n_l=*/1);
67
68 CPPUNIT_ASSERT_EQUAL(mat.m(), m);
69 CPPUNIT_ASSERT_EQUAL(mat.n(), m);
70 CPPUNIT_ASSERT_EQUAL(mat.local_m(), numeric_index_type(1));
71 CPPUNIT_ASSERT_EQUAL(mat.local_n(), numeric_index_type(1));
72
73 MatType type;
74 LibmeshPetscCall2((*TestCommWorld), MatGetType(mat.mat(), &type));
75 CPPUNIT_ASSERT(std::string(type) == MATSHELL);
76 }
77
79 {
80 LOG_UNIT_TEST;
81
83 const numeric_index_type m = TestCommWorld->size();
84 mat.init(m, m, 1, 1);
85
86 // init() composes libmesh's own context onto the shell Mat, letting
87 // get_context() recover the wrapping object later given only the raw Mat.
88 CPPUNIT_ASSERT(PetscMatrixBase<Number>::get_context(mat.mat(), *TestCommWorld) == &mat);
89 }
90
91 void testMult()
92 {
93 LOG_UNIT_TEST;
94
96 const numeric_index_type m = TestCommWorld->size();
97 mat.init(m, m, 1, 1);
98
99 LibmeshPetscCall2((*TestCommWorld),
100 MatShellSetOperation(mat.mat(), MATOP_MULT,
101 (void (*)(void))petsc_matrix_shell_matrix_test_mult));
102
105 x.set(TestCommWorld->rank(), 3.);
106 x.close();
107
108 LibmeshPetscCall2((*TestCommWorld), MatMult(mat.mat(), x.vec(), y.vec()));
109
110 LIBMESH_ASSERT_NUMBERS_EQUAL(Number(6.), y(TestCommWorld->rank()), TOLERANCE);
111 }
112};
113
115
116#endif // LIBMESH_HAVE_PETSC
CPPUNIT_TEST(testGetContext)
LIBMESH_CPPUNIT_TEST_SUITE(PetscMatrixShellMatrixTest)
CPPUNIT_TEST(testInitSizes)
This class provides a nice interface to the PETSc C-based data structures for parallel,...
virtual numeric_index_type n() const override
virtual numeric_index_type local_m() const final
Get the number of rows owned by this process.
virtual numeric_index_type m() const override
virtual numeric_index_type local_n() const final
Get the number of columns owned by this process.
This class allows to use a PETSc shell matrix as a PetscMatrix.
virtual void init(const numeric_index_type m, const numeric_index_type n, const numeric_index_type m_l, const numeric_index_type n_l, const numeric_index_type=30, const numeric_index_type=10, const numeric_index_type blocksize=1) override
Initialize SparseMatrix with the specified sizes.
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 ...
Communicator * TestCommWorld
The libMesh namespace provides an interface to certain functionality in the library.
PetscFunctionReturn(LIBMESH_PETSC_SUCCESS)
dof_id_type numeric_index_type
Definition id_types.h:99
static constexpr Real TOLERANCE
LibmeshPetscCallQ(DMShellGetContext(dm, &ctx))
CPPUNIT_TEST_SUITE_REGISTRATION(PetscMatrixShellMatrixTest)