20#ifndef LIBMESH_PETSC_LINEAR_SOLVER_H
21#define LIBMESH_PETSC_LINEAR_SOLVER_H
23#include "libmesh/libmesh_config.h"
25#ifdef LIBMESH_HAVE_PETSC
27#include "libmesh/petsc_macro.h"
28#include "libmesh/petsc_solver_exception.h"
29#include "libmesh/wrapped_petsc.h"
41#include "libmesh/linear_solver.h"
74template <
typename T>
class PetscMatrixBase;
101 virtual void clear ()
override;
108 virtual void init (
const char * name =
nullptr)
override;
114 const char * name =
nullptr);
141 virtual std::pair<unsigned int, Real>
145 const std::optional<double> tol = std::nullopt,
146 const std::optional<unsigned int> m_its = std::nullopt)
override
148 return this->
solve(matrix_in, matrix_in, solution_in, rhs_in, tol, m_its);
156 virtual std::pair<unsigned int, Real>
160 const std::optional<double> tol = std::nullopt,
161 const std::optional<unsigned int> m_its = std::nullopt)
override;
179 virtual std::pair<unsigned int, Real>
184 const std::optional<double> tol = std::nullopt,
185 const std::optional<unsigned int> m_its = std::nullopt)
override;
190 virtual std::pair<unsigned int, Real>
194 const std::optional<double> tol = std::nullopt,
195 const std::optional<unsigned int> m_its = std::nullopt)
override;
202 virtual std::pair<unsigned int, Real>
207 const std::optional<double> tol = std::nullopt,
208 const std::optional<unsigned int> m_its = std::nullopt)
override;
254 virtual std::pair<unsigned int, Real>
259 const double rel_tol,
260 const double abs_tol,
261 const unsigned int m_its,
268 virtual std::pair<unsigned int, Real>
273 const double rel_tol,
274 const double abs_tol,
275 const unsigned int m_its);
280 std::pair<unsigned int, Real>
286 const double rel_tol,
287 const double abs_tol,
288 const unsigned int m_its,
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.
Real get_initial_residual()
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.
Manages consistently variables, degrees of freedom, and coefficient vectors.
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.