libMesh
Loading...
Searching...
No Matches
petsc_linear_solver.h
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
19
20#ifndef LIBMESH_PETSC_LINEAR_SOLVER_H
21#define LIBMESH_PETSC_LINEAR_SOLVER_H
22
23#include "libmesh/libmesh_config.h"
24
25#ifdef LIBMESH_HAVE_PETSC
26
27#include "libmesh/petsc_macro.h"
28#include "libmesh/petsc_solver_exception.h"
29#include "libmesh/wrapped_petsc.h"
30
31// Petsc include files.
32#ifdef I
33# define LIBMESH_SAW_I
34#endif
35#include <petscksp.h>
36#ifndef LIBMESH_SAW_I
37# undef I // Avoid complex.h contamination
38#endif
39
40// Local includes
41#include "libmesh/linear_solver.h"
42
43// C++ includes
44#include <cstddef>
45#include <vector>
46#include <memory>
47
48//--------------------------------------------------------------------
49// Functions with C linkage to pass to PETSc. PETSc will call these
50// methods as needed for preconditioning
51//
52// Since they must have C linkage they have no knowledge of a namespace.
53// Give them an obscure name to avoid namespace pollution.
54extern "C"
55{
61
66 PetscErrorCode libmesh_petsc_preconditioner_apply(PC, Vec x, Vec y);
67} // end extern "C"
68
69
70namespace libMesh
71{
72
73// forward declarations
74template <typename T> class PetscMatrixBase;
75
84template <typename T>
86{
87public:
92
96 virtual ~PetscLinearSolver () = default;
97
101 virtual void clear () override;
102
108 virtual void init (const char * name = nullptr) override;
109
113 void init (PetscMatrixBase<T> * matrix,
114 const char * name = nullptr);
115
125 virtual void init_systems (const System &) override;
126
134 virtual void restrict_solve_to (const std::vector<unsigned int> * const dofs,
135 const SubsetSolveMode subset_solve_mode=SUBSET_ZERO) override;
136
141 virtual std::pair<unsigned int, Real>
142 solve (SparseMatrix<T> & matrix_in,
143 NumericVector<T> & solution_in,
144 NumericVector<T> & rhs_in,
145 const std::optional<double> tol = std::nullopt,
146 const std::optional<unsigned int> m_its = std::nullopt) override
147 {
148 return this->solve(matrix_in, matrix_in, solution_in, rhs_in, tol, m_its);
149 }
150
151
156 virtual std::pair<unsigned int, Real>
157 adjoint_solve (SparseMatrix<T> & matrix_in,
158 NumericVector<T> & solution_in,
159 NumericVector<T> & rhs_in,
160 const std::optional<double> tol = std::nullopt,
161 const std::optional<unsigned int> m_its = std::nullopt) override;
162
179 virtual std::pair<unsigned int, Real>
180 solve (SparseMatrix<T> & matrix,
181 SparseMatrix<T> & preconditioner,
182 NumericVector<T> & solution,
183 NumericVector<T> & rhs,
184 const std::optional<double> tol = std::nullopt,
185 const std::optional<unsigned int> m_its = std::nullopt) override;
186
190 virtual std::pair<unsigned int, Real>
191 solve (const ShellMatrix<T> & shell_matrix,
192 NumericVector<T> & solution_in,
193 NumericVector<T> & rhs_in,
194 const std::optional<double> tol = std::nullopt,
195 const std::optional<unsigned int> m_its = std::nullopt) override;
196
202 virtual std::pair<unsigned int, Real>
203 solve (const ShellMatrix<T> & shell_matrix,
204 const SparseMatrix<T> & precond_matrix,
205 NumericVector<T> & solution_in,
206 NumericVector<T> & rhs_in,
207 const std::optional<double> tol = std::nullopt,
208 const std::optional<unsigned int> m_its = std::nullopt) override;
209
217 PC pc() { this->init(); return _pc; }
218
225 KSP ksp();
226
231 void get_residual_history(std::vector<double> & hist);
232
241
245 virtual LinearConvergenceReason get_converged_reason() const override;
246
247private:
248
249 typedef PetscErrorCode (*ksp_solve_func_type)(KSP,Vec,Vec);
250
251 /*
252 * Helper function to run solve() and adjoint_solve()
253 */
254 virtual std::pair<unsigned int, Real>
255 solve_common (SparseMatrix<T> & matrix_in,
256 SparseMatrix<T> & precond_in,
257 NumericVector<T> & solution_in,
258 NumericVector<T> & rhs_in,
259 const double rel_tol,
260 const double abs_tol,
261 const unsigned int m_its,
262 ksp_solve_func_type solve_func);
263
264 /*
265 * Helper function to run ShellMatrix solve() with or without a
266 * separate preconditioner matrix
267 */
268 virtual std::pair<unsigned int, Real>
269 shell_solve_common (const ShellMatrix<T> & shell_matrix,
270 PetscMatrixBase<T> * precond_matrix,
271 NumericVector<T> & solution_in,
272 NumericVector<T> & rhs_in,
273 const double rel_tol,
274 const double abs_tol,
275 const unsigned int m_its);
276
277 /*
278 * Helper function for the helper functions
279 */
280 std::pair<unsigned int, Real>
281 solve_base (SparseMatrix<T> * matrix,
282 PetscMatrixBase<T> * precond,
283 Mat mat,
284 NumericVector<T> & solution_in,
285 NumericVector<T> & rhs_in,
286 const double rel_tol,
287 const double abs_tol,
288 const unsigned int m_its,
289 ksp_solve_func_type solve_func);
290
291
296 void set_petsc_solver_type ();
297
301 static PetscErrorCode _petsc_shell_matrix_mult(Mat mat, Vec arg, Vec dest);
302
306 static PetscErrorCode _petsc_shell_matrix_mult_add(Mat mat, Vec arg, Vec add, Vec dest);
307
311 static PetscErrorCode _petsc_shell_matrix_get_diagonal(Mat mat, Vec dest);
312
316 PC _pc;
317
322
328
335
339 PetscInt restrict_solve_to_is_local_size() const;
340
346 void create_complement_is (const NumericVector<T> & vec_in);
347
353};
354
355} // namespace libMesh
356
357
358#endif // #ifdef LIBMESH_HAVE_PETSC
359#endif // LIBMESH_PETSC_LINEAR_SOLVER_H
This base class can be inherited from to provide interfaces to linear solvers from different packages...
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
This class provides an interface to PETSc iterative solvers that is compatible with the libMesh Linea...
PetscErrorCode(* ksp_solve_func_type)(KSP, Vec, Vec)
virtual std::pair< unsigned int, Real > shell_solve_common(const ShellMatrix< T > &shell_matrix, PetscMatrixBase< T > *precond_matrix, NumericVector< T > &solution_in, NumericVector< T > &rhs_in, const double rel_tol, const double abs_tol, const unsigned int m_its)
void get_residual_history(std::vector< double > &hist)
Fills the input vector with the sequence of residual norms from the latest iterative solve.
void create_complement_is(const NumericVector< T > &vec_in)
Creates _restrict_solve_to_is_complement to contain all indices that are local in vec_in,...
WrappedPetsc< IS > _restrict_solve_to_is_complement
PETSc index set, complement to _restrict_solve_to_is.
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &matrix_in, NumericVector< T > &solution_in, NumericVector< T > &rhs_in, const std::optional< double > tol=std::nullopt, const std::optional< unsigned int > m_its=std::nullopt) override
Call the Petsc solver.
virtual std::pair< unsigned int, Real > solve_common(SparseMatrix< T > &matrix_in, SparseMatrix< T > &precond_in, NumericVector< T > &solution_in, NumericVector< T > &rhs_in, const double rel_tol, const double abs_tol, const unsigned int m_its, ksp_solve_func_type solve_func)
WrappedPetsc< KSP > _ksp
Krylov subspace context.
virtual ~PetscLinearSolver()=default
Destructor.
static PetscErrorCode _petsc_shell_matrix_mult(Mat mat, Vec arg, Vec dest)
Internal function if shell matrix mode is used.
PetscInt restrict_solve_to_is_local_size() const
void set_petsc_solver_type()
Tells PETSC to use the user-specified solver stored in _solver_type.
virtual LinearConvergenceReason get_converged_reason() const override
static PetscErrorCode _petsc_shell_matrix_get_diagonal(Mat mat, Vec dest)
Internal function if shell matrix mode is used.
virtual void clear() override
Release all memory and clear data structures.
virtual std::pair< unsigned int, Real > adjoint_solve(SparseMatrix< T > &matrix_in, NumericVector< T > &solution_in, NumericVector< T > &rhs_in, const std::optional< double > tol=std::nullopt, const std::optional< unsigned int > m_its=std::nullopt) override
Call the Petsc solver.
std::pair< unsigned int, Real > solve_base(SparseMatrix< T > *matrix, PetscMatrixBase< T > *precond, Mat mat, NumericVector< T > &solution_in, NumericVector< T > &rhs_in, const double rel_tol, const double abs_tol, const unsigned int m_its, ksp_solve_func_type solve_func)
SubsetSolveMode _subset_solve_mode
If restrict-solve-to-subset mode is active, this member decides what happens with the dofs outside th...
WrappedPetsc< IS > _restrict_solve_to_is
PETSc index set containing the dofs on which to solve (nullptr means solve on all dofs).
virtual void restrict_solve_to(const std::vector< unsigned int > *const dofs, const SubsetSolveMode subset_solve_mode=SUBSET_ZERO) override
After calling this method, all successive solves will be restricted to the given set of dofs,...
static PetscErrorCode _petsc_shell_matrix_mult_add(Mat mat, Vec arg, Vec add, Vec dest)
Internal function if shell matrix mode is used.
virtual void init_systems(const System &) override
Apply names to the system to be solved and set auxiliary preconditioner data.
virtual void init(const char *name=nullptr) override
Initialize data structures if not done so already.
PC _pc
Preconditioner context.
This class provides a nice interface to the PETSc C-based data structures for parallel,...
Generic shell matrix, i.e.
Generic sparse matrix.
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition system.h:100
The libMesh namespace provides an interface to certain functionality in the library.
SubsetSolveMode
defines an enum for the question what happens to the dofs outside the given subset when a system is s...
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
LinearConvergenceReason
Linear solver convergence flags (taken from the PETSc flags).
PetscErrorCode libmesh_petsc_preconditioner_apply(PC, Vec x, Vec y)
This function is called by PETSc to actually apply the preconditioner.
PetscErrorCode libmesh_petsc_preconditioner_setup(PC)
This function is called by PETSc to initialize the preconditioner.