libMesh
Loading...
Searching...
No Matches
petsc_mffd_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_mffd_matrix.h>
23#include <libmesh/petsc_matrix_shell_matrix.h>
24
25#include "test_comm.h"
26#include "libmesh_cppunit.h"
27
28using namespace libMesh;
29
30class PetscMFFDMatrixTest : public CppUnit::TestCase
31{
32public:
34
37
39
40public:
41
42 void setUp() {}
43 void tearDown() {}
44
46 {
47 LOG_UNIT_TEST;
48
49 // A real, owned Mat for the wrapper to adopt. init() attaches its own
50 // context (pointing at owner) as a side effect.
52 owner.init(TestCommWorld->size(), TestCommWorld->size(), 1, 1);
53
55 mffd.assign(owner.mat(), /*set_context=*/true);
56
57 CPPUNIT_ASSERT(mffd.mat() == owner.mat());
58 CPPUNIT_ASSERT(PetscMatrixBase<Number>::get_context(owner.mat(), *TestCommWorld) == &mffd);
59 }
60
62 {
63 LOG_UNIT_TEST;
64
66 owner.init(TestCommWorld->size(), TestCommWorld->size(), 1, 1);
67
68 {
70 mffd.assign(owner.mat(), /*set_context=*/false);
71 CPPUNIT_ASSERT(mffd.mat() == owner.mat());
72 }
73
74 // mffd is now destroyed. Since assign() above didn't attach a context,
75 // owner's own context (set by its own init()) must still be intact --
76 // this is the dangling-context scenario set_context=false exists to avoid.
77 CPPUNIT_ASSERT(PetscMatrixBase<Number>::get_context(owner.mat(), *TestCommWorld) == &owner);
78 }
79};
80
82
83#endif // LIBMESH_HAVE_PETSC
CPPUNIT_TEST(testAssignWithoutContext)
CPPUNIT_TEST(testAssignWithContext)
LIBMESH_CPPUNIT_TEST_SUITE(PetscMFFDMatrixTest)
This class allows to use a PETSc shell matrix as a PetscMatrix.
void assign(Mat m, bool set_context)
Adopt an existing, externally-owned Mat, without destroying it when this object goes out of scope.
This class provides a nice interface to the PETSc C-based data structures for parallel,...
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.
Communicator * TestCommWorld
The libMesh namespace provides an interface to certain functionality in the library.
CPPUNIT_TEST_SUITE_REGISTRATION(PetscMFFDMatrixTest)