Line data Source code
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 : #ifndef LIBMESH_PETSC_MFFD_MATRIX_H
19 : #define LIBMESH_PETSC_MFFD_MATRIX_H
20 :
21 : #include "libmesh/libmesh_config.h"
22 :
23 : #ifdef LIBMESH_HAVE_PETSC
24 :
25 : #include "libmesh/petsc_matrix_base.h"
26 :
27 : namespace libMesh
28 : {
29 :
30 : /**
31 : * This class allows to use a PETSc shell matrix as a PetscMatrix
32 : *
33 : * \author Alexander Lindsay
34 : * \date 2024
35 : */
36 : template <typename T>
37 74028 : class PetscMFFDMatrix : public PetscMatrixBase<T>
38 : {
39 : public:
40 : /**
41 : * Constructor. Creates a PetscMFFDMatrix assuming you already have a
42 : * valid Mat object. In this case, m is NOT destroyed by the
43 : * PetscMFFDMatrix destructor when this object goes out of scope. This
44 : * allows ownership of m to remain with the original creator, and to
45 : * simply provide additional functionality with the PetscMFFDMatrix.
46 : */
47 : explicit PetscMFFDMatrix(Mat m, const Parallel::Communicator & comm_in);
48 :
49 : explicit PetscMFFDMatrix(const Parallel::Communicator & comm_in);
50 :
51 : /**
52 : * Adopt an existing, externally-owned Mat, without destroying it when this
53 : * object goes out of scope. \p set_context controls whether we attach a
54 : * context pointer to \p m allowing \p get_context() to recover this object
55 : * from the Mat later; skip this when this wrapper is short-lived (e.g. a
56 : * function-local variable) so we don't leave a dangling context on \p m
57 : * after we're destroyed.
58 : */
59 : void assign(Mat m, bool set_context);
60 :
61 : virtual void init(const numeric_index_type,
62 : const numeric_index_type,
63 : const numeric_index_type,
64 : const numeric_index_type,
65 : const numeric_index_type = 30,
66 : const numeric_index_type = 10,
67 : const numeric_index_type = 1) override;
68 :
69 : /**
70 : * Initialize this matrix using the sparsity structure computed by \p dof_map.
71 : * @param type The serial/parallel/ghosted type of the matrix
72 : */
73 : virtual void init(ParallelType = PARALLEL) override;
74 :
75 : virtual SparseMatrix<T> & operator=(const SparseMatrix<T> &) override;
76 :
77 : virtual void zero() override;
78 : virtual std::unique_ptr<SparseMatrix<T>> zero_clone() const override;
79 : virtual std::unique_ptr<SparseMatrix<T>> clone() const override;
80 : virtual void set(const numeric_index_type i, const numeric_index_type j, const T value) override;
81 : virtual void add(const numeric_index_type i, const numeric_index_type j, const T value) override;
82 : virtual void add_matrix(const DenseMatrix<T> & dm,
83 : const std::vector<numeric_index_type> & rows,
84 : const std::vector<numeric_index_type> & cols) override;
85 : virtual void add_matrix(const DenseMatrix<T> & dm,
86 : const std::vector<numeric_index_type> & dof_indices) override;
87 : virtual void add(const T a, const SparseMatrix<T> & X) override;
88 : virtual T operator()(const numeric_index_type i, const numeric_index_type j) const override;
89 : virtual Real l1_norm() const override;
90 : virtual Real linfty_norm() const override;
91 : virtual void print_personal(std::ostream & os = libMesh::out) const override;
92 : virtual void get_diagonal(NumericVector<T> & dest) const override;
93 : virtual void get_transpose(SparseMatrix<T> & dest) const override;
94 : virtual void get_row(numeric_index_type i,
95 : std::vector<numeric_index_type> & indices,
96 : std::vector<T> & values) const override;
97 : };
98 :
99 : template <typename T>
100 : PetscMFFDMatrix<T>::PetscMFFDMatrix(Mat m, const Parallel::Communicator & comm_in)
101 : : PetscMatrixBase<T>(m, comm_in)
102 : {
103 : }
104 :
105 : template <typename T>
106 76088 : PetscMFFDMatrix<T>::PetscMFFDMatrix(const Parallel::Communicator & comm_in)
107 76088 : : PetscMatrixBase<T>(comm_in)
108 : {
109 2060 : }
110 :
111 : template <typename T>
112 : void
113 408 : PetscMFFDMatrix<T>::assign(Mat m, bool set_context)
114 : {
115 14154 : this->_mat = m;
116 14154 : this->_is_initialized = true;
117 14154 : this->_destroy_mat_on_exit = false;
118 408 : if (set_context)
119 0 : this->set_context();
120 408 : }
121 :
122 : template <typename T>
123 : void
124 0 : PetscMFFDMatrix<T>::init(const numeric_index_type,
125 : const numeric_index_type,
126 : const numeric_index_type,
127 : const numeric_index_type,
128 : const numeric_index_type,
129 : const numeric_index_type,
130 : const numeric_index_type)
131 : {
132 0 : libmesh_error();
133 : }
134 :
135 : template <typename T>
136 : void
137 0 : PetscMFFDMatrix<T>::init(ParallelType)
138 : {
139 0 : libmesh_error();
140 : }
141 :
142 : template <typename T>
143 : SparseMatrix<T> &
144 0 : PetscMFFDMatrix<T>::operator=(const SparseMatrix<T> &)
145 : {
146 0 : libmesh_error();
147 : }
148 :
149 : template <typename T>
150 : void
151 0 : PetscMFFDMatrix<T>::zero()
152 : {
153 0 : libmesh_error();
154 : }
155 :
156 : template <typename T>
157 : std::unique_ptr<SparseMatrix<T>>
158 0 : PetscMFFDMatrix<T>::zero_clone() const
159 : {
160 0 : libmesh_error();
161 : }
162 :
163 : template <typename T>
164 : std::unique_ptr<SparseMatrix<T>>
165 0 : PetscMFFDMatrix<T>::clone() const
166 : {
167 0 : libmesh_not_implemented();
168 : }
169 :
170 : template <typename T>
171 : void
172 0 : PetscMFFDMatrix<T>::set(const numeric_index_type, const numeric_index_type, const T)
173 : {
174 0 : libmesh_error();
175 : }
176 :
177 : template <typename T>
178 : void
179 0 : PetscMFFDMatrix<T>::add(const numeric_index_type, const numeric_index_type, const T)
180 : {
181 0 : libmesh_error();
182 : }
183 :
184 : template <typename T>
185 : void
186 0 : PetscMFFDMatrix<T>::add_matrix(const DenseMatrix<T> &,
187 : const std::vector<numeric_index_type> &,
188 : const std::vector<numeric_index_type> &)
189 : {
190 0 : libmesh_error();
191 : }
192 :
193 : template <typename T>
194 : void
195 0 : PetscMFFDMatrix<T>::add_matrix(const DenseMatrix<T> &, const std::vector<numeric_index_type> &)
196 : {
197 0 : libmesh_error();
198 : }
199 :
200 : template <typename T>
201 : void
202 0 : PetscMFFDMatrix<T>::add(const T, const SparseMatrix<T> &)
203 : {
204 0 : libmesh_error();
205 : }
206 :
207 : template <typename T>
208 : T
209 0 : PetscMFFDMatrix<T>::operator()(const numeric_index_type, const numeric_index_type) const
210 : {
211 0 : libmesh_error();
212 : }
213 :
214 : template <typename T>
215 : Real
216 0 : PetscMFFDMatrix<T>::l1_norm() const
217 : {
218 0 : libmesh_error();
219 : }
220 :
221 : template <typename T>
222 : Real
223 0 : PetscMFFDMatrix<T>::linfty_norm() const
224 : {
225 0 : libmesh_error();
226 : }
227 :
228 : template <typename T>
229 : void
230 0 : PetscMFFDMatrix<T>::print_personal(std::ostream &) const
231 : {
232 0 : libmesh_error();
233 : }
234 :
235 : template <typename T>
236 : void
237 0 : PetscMFFDMatrix<T>::get_diagonal(NumericVector<T> &) const
238 : {
239 0 : libmesh_error();
240 : }
241 :
242 : template <typename T>
243 : void
244 0 : PetscMFFDMatrix<T>::get_transpose(SparseMatrix<T> &) const
245 : {
246 0 : libmesh_error();
247 : }
248 :
249 : template <typename T>
250 : void
251 0 : PetscMFFDMatrix<T>::get_row(numeric_index_type,
252 : std::vector<numeric_index_type> &,
253 : std::vector<T> &) const
254 : {
255 0 : libmesh_error();
256 : }
257 :
258 : } // namespace libMesh
259 :
260 : #endif // LIBMESH_HAVE_PETSC
261 : #endif // LIBMESH_SPARSE_SHELL_MATRIX_H
|