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 74004 : 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 : * Calls \p assign with \p set_context equal to \p false as generally speaking
53 : * an MFFD matrix will have been created within the PETSc library, and if we
54 : * are assigning ourselves to it then we are unlikely to outlive it, and we
55 : * don't want to leave dangling context. If you want to set the Mat's context
56 : * to \p this, then directly call \p assign with \p set_context equal to true.
57 : */
58 : PetscMFFDMatrix & operator=(Mat m);
59 :
60 : /**
61 : * Adopt an existing, externally-owned Mat, without destroying it when this
62 : * object goes out of scope. Any Mat this object currently owns is destroyed
63 : * first. \p set_context controls whether we attach a context pointer to \p m
64 : * allowing \p get_context() to recover this object from the Mat later; skip
65 : * this when this wrapper is short-lived (e.g. a function-local variable) so
66 : * we don't leave a dangling context on \p m after we're destroyed.
67 : */
68 : void assign(Mat m, bool set_context);
69 :
70 : virtual void init(const numeric_index_type,
71 : const numeric_index_type,
72 : const numeric_index_type,
73 : const numeric_index_type,
74 : const numeric_index_type = 30,
75 : const numeric_index_type = 10,
76 : const numeric_index_type = 1) override;
77 :
78 : /**
79 : * Initialize this matrix using the sparsity structure computed by \p dof_map.
80 : * @param type The serial/parallel/ghosted type of the matrix
81 : */
82 : virtual void init(ParallelType = PARALLEL) override;
83 :
84 : virtual SparseMatrix<T> & operator=(const SparseMatrix<T> &) override;
85 :
86 : virtual void zero() override;
87 : virtual std::unique_ptr<SparseMatrix<T>> zero_clone() const override;
88 : virtual std::unique_ptr<SparseMatrix<T>> clone() const override;
89 : virtual void set(const numeric_index_type i, const numeric_index_type j, const T value) override;
90 : virtual void add(const numeric_index_type i, const numeric_index_type j, const T value) override;
91 : virtual void add_matrix(const DenseMatrix<T> & dm,
92 : const std::vector<numeric_index_type> & rows,
93 : const std::vector<numeric_index_type> & cols) override;
94 : virtual void add_matrix(const DenseMatrix<T> & dm,
95 : const std::vector<numeric_index_type> & dof_indices) override;
96 : virtual void add(const T a, const SparseMatrix<T> & X) override;
97 : virtual T operator()(const numeric_index_type i, const numeric_index_type j) const override;
98 : virtual Real l1_norm() const override;
99 : virtual Real linfty_norm() const override;
100 : virtual void print_personal(std::ostream & os = libMesh::out) const override;
101 : virtual void get_diagonal(NumericVector<T> & dest) const override;
102 : virtual void get_transpose(SparseMatrix<T> & dest) const override;
103 : virtual void get_row(numeric_index_type i,
104 : std::vector<numeric_index_type> & indices,
105 : std::vector<T> & values) const override;
106 : };
107 :
108 : template <typename T>
109 : PetscMFFDMatrix<T>::PetscMFFDMatrix(Mat m, const Parallel::Communicator & comm_in)
110 : : PetscMatrixBase<T>(m, comm_in)
111 : {
112 : }
113 :
114 : template <typename T>
115 76064 : PetscMFFDMatrix<T>::PetscMFFDMatrix(const Parallel::Communicator & comm_in)
116 76064 : : PetscMatrixBase<T>(comm_in)
117 : {
118 2060 : }
119 :
120 : template <typename T>
121 : void
122 14154 : PetscMFFDMatrix<T>::assign(Mat m, bool set_context)
123 : {
124 14154 : if (this->_mat != m)
125 14154 : this->clear();
126 :
127 14154 : this->_mat = m;
128 14154 : this->_is_initialized = true;
129 14154 : this->_destroy_mat_on_exit = false;
130 14154 : if (set_context)
131 0 : this->set_context();
132 14154 : }
133 :
134 : template <typename T>
135 : PetscMFFDMatrix<T> &
136 : PetscMFFDMatrix<T>::operator=(Mat m)
137 : {
138 : this->assign(m, false);
139 : return *this;
140 : }
141 :
142 : template <typename T>
143 : void
144 0 : PetscMFFDMatrix<T>::init(const numeric_index_type,
145 : const numeric_index_type,
146 : const numeric_index_type,
147 : const numeric_index_type,
148 : const numeric_index_type,
149 : const numeric_index_type,
150 : const numeric_index_type)
151 : {
152 0 : libmesh_error();
153 : }
154 :
155 : template <typename T>
156 : void
157 0 : PetscMFFDMatrix<T>::init(ParallelType)
158 : {
159 0 : libmesh_error();
160 : }
161 :
162 : template <typename T>
163 : SparseMatrix<T> &
164 0 : PetscMFFDMatrix<T>::operator=(const SparseMatrix<T> &)
165 : {
166 0 : libmesh_error();
167 : }
168 :
169 : template <typename T>
170 : void
171 0 : PetscMFFDMatrix<T>::zero()
172 : {
173 0 : libmesh_error();
174 : }
175 :
176 : template <typename T>
177 : std::unique_ptr<SparseMatrix<T>>
178 0 : PetscMFFDMatrix<T>::zero_clone() const
179 : {
180 0 : libmesh_error();
181 : }
182 :
183 : template <typename T>
184 : std::unique_ptr<SparseMatrix<T>>
185 0 : PetscMFFDMatrix<T>::clone() const
186 : {
187 0 : libmesh_not_implemented();
188 : }
189 :
190 : template <typename T>
191 : void
192 0 : PetscMFFDMatrix<T>::set(const numeric_index_type, const numeric_index_type, const T)
193 : {
194 0 : libmesh_error();
195 : }
196 :
197 : template <typename T>
198 : void
199 0 : PetscMFFDMatrix<T>::add(const numeric_index_type, const numeric_index_type, const T)
200 : {
201 0 : libmesh_error();
202 : }
203 :
204 : template <typename T>
205 : void
206 0 : PetscMFFDMatrix<T>::add_matrix(const DenseMatrix<T> &,
207 : const std::vector<numeric_index_type> &,
208 : const std::vector<numeric_index_type> &)
209 : {
210 0 : libmesh_error();
211 : }
212 :
213 : template <typename T>
214 : void
215 0 : PetscMFFDMatrix<T>::add_matrix(const DenseMatrix<T> &, const std::vector<numeric_index_type> &)
216 : {
217 0 : libmesh_error();
218 : }
219 :
220 : template <typename T>
221 : void
222 0 : PetscMFFDMatrix<T>::add(const T, const SparseMatrix<T> &)
223 : {
224 0 : libmesh_error();
225 : }
226 :
227 : template <typename T>
228 : T
229 0 : PetscMFFDMatrix<T>::operator()(const numeric_index_type, const numeric_index_type) const
230 : {
231 0 : libmesh_error();
232 : }
233 :
234 : template <typename T>
235 : Real
236 0 : PetscMFFDMatrix<T>::l1_norm() const
237 : {
238 0 : libmesh_error();
239 : }
240 :
241 : template <typename T>
242 : Real
243 0 : PetscMFFDMatrix<T>::linfty_norm() const
244 : {
245 0 : libmesh_error();
246 : }
247 :
248 : template <typename T>
249 : void
250 0 : PetscMFFDMatrix<T>::print_personal(std::ostream &) const
251 : {
252 0 : libmesh_error();
253 : }
254 :
255 : template <typename T>
256 : void
257 0 : PetscMFFDMatrix<T>::get_diagonal(NumericVector<T> &) const
258 : {
259 0 : libmesh_error();
260 : }
261 :
262 : template <typename T>
263 : void
264 0 : PetscMFFDMatrix<T>::get_transpose(SparseMatrix<T> &) const
265 : {
266 0 : libmesh_error();
267 : }
268 :
269 : template <typename T>
270 : void
271 0 : PetscMFFDMatrix<T>::get_row(numeric_index_type,
272 : std::vector<numeric_index_type> &,
273 : std::vector<T> &) const
274 : {
275 0 : libmesh_error();
276 : }
277 :
278 : } // namespace libMesh
279 :
280 : #endif // LIBMESH_HAVE_PETSC
281 : #endif // LIBMESH_SPARSE_SHELL_MATRIX_H
|