libMesh
Loading...
Searching...
No Matches
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_LINEAR_SOLVER_H
21#define LIBMESH_LINEAR_SOLVER_H
22
23// Local includes
24#include "libmesh/libmesh_common.h"
25#include "libmesh/enum_subset_solve_mode.h" // SUBSET_ZERO
26#include "libmesh/reference_counted_object.h"
27#include "libmesh/libmesh.h"
28#include "libmesh/parallel_object.h"
29
30// C++ includes
31#include <cstddef>
32#include <vector>
33#include <memory>
34#include <optional>
35
36namespace libMesh
37{
38
39// forward declarations
40template <typename T> class SparseMatrix;
41template <typename T> class NumericVector;
42template <typename T> class ShellMatrix;
43template <typename T> class Preconditioner;
44class System;
45class SolverConfiguration;
46enum SolverPackage : int;
48enum SolverType : int;
50
58template <typename T>
59class LinearSolver : public ReferenceCountedObject<LinearSolver<T>>,
60 public ParallelObject
61{
62public:
63
68
72 virtual ~LinearSolver ();
73
78 static std::unique_ptr<LinearSolver<T>> build(const libMesh::Parallel::Communicator & comm_in,
79 const SolverPackage solver_package = libMesh::default_solver_package());
80
85 bool initialized () const { return _is_initialized; }
86
90 virtual void clear () {}
91
96 virtual void init (const char * name = nullptr) = 0;
97
108 virtual void init_systems (const System &) {}
109
114
119 { _solver_type = st; }
120
125
130
134 void attach_preconditioner(Preconditioner<T> * preconditioner);
135
140 virtual void reuse_preconditioner(bool );
141
147
155 virtual void restrict_solve_to (const std::vector<unsigned int> * const dofs,
156 const SubsetSolveMode subset_solve_mode=SUBSET_ZERO);
157
165 virtual std::pair<unsigned int, Real> solve (SparseMatrix<T> &, // System Matrix
166 NumericVector<T> &, // Solution vector
167 NumericVector<T> &, // RHS vector
168 const std::optional<double> tol = std::nullopt,
169 const std::optional<unsigned int> m_its = std::nullopt) = 0; // N. Iterations
170
178 virtual std::pair<unsigned int, Real> adjoint_solve (SparseMatrix<T> &, // System Matrix
179 NumericVector<T> &, // Solution vector
180 NumericVector<T> &, // RHS vector
181 const std::optional<double> tol = std::nullopt,
182 const std::optional<unsigned int> m_its = std::nullopt); // N. Iterations
183
189 virtual std::pair<unsigned int, Real> solve (SparseMatrix<T> &, // System Matrix
190 SparseMatrix<T> &, // Preconditioning Matrix
191 NumericVector<T> &, // Solution vector
192 NumericVector<T> &, // RHS vector
193 const std::optional<double> tol = std::nullopt,
194 const std::optional<unsigned int> m_its = std::nullopt) = 0; // N. Iterations
195
202 std::pair<unsigned int, Real> solve (SparseMatrix<T> & matrix,
203 SparseMatrix<T> * precond_matrix,
204 NumericVector<T> &, // Solution vector
205 NumericVector<T> &, // RHS vector
206 const std::optional<double> tol = std::nullopt,
207 const std::optional<unsigned int> m_its = std::nullopt); // N. Iterations
208
209
210
214 virtual std::pair<unsigned int, Real> solve (const ShellMatrix<T> & shell_matrix,
215 NumericVector<T> &, // Solution vector
216 NumericVector<T> &, // RHS vector
217 const std::optional<double> tol = std::nullopt,
218 const std::optional<unsigned int> m_its = std::nullopt) = 0; // N. Iterations
219
220
226 virtual std::pair<unsigned int, Real> solve (const ShellMatrix<T> & shell_matrix,
227 const SparseMatrix<T> & precond_matrix,
228 NumericVector<T> &, // Solution vector
229 NumericVector<T> &, // RHS vector
230 const std::optional<double> tol = std::nullopt,
231 const std::optional<unsigned int> m_its = std::nullopt) = 0; // N. Iterations
232
233
238 std::pair<unsigned int, Real> solve (const ShellMatrix<T> & matrix,
239 const SparseMatrix<T> * precond_matrix,
240 NumericVector<T> &, // Solution vector
241 NumericVector<T> &, // RHS vector
242 const std::optional<double> tol = std::nullopt,
243 const std::optional<unsigned int> m_its = std::nullopt); // N. Iterations
244
245
250 virtual void print_converged_reason() const;
251
256
260 void set_solver_configuration(SolverConfiguration & solver_configuration);
261
262protected:
267
272
277
282
290
296
301 double get_real_solver_setting(const std::string & setting_name,
302 const std::optional<double> & setting,
303 const std::optional<double> default_value = std::nullopt);
304
309 int get_int_solver_setting(const std::string & setting_name,
310 const std::optional<int> & setting,
311 const std::optional<int> default_value = std::nullopt);
312};
313
314
315
316
317/*----------------------- inline functions ----------------------------------*/
318template <typename T>
319inline
324
325template <typename T>
326inline
328{
329 return same_preconditioner;
330}
331
332template <typename T>
333inline
334std::pair<unsigned int, Real>
336 SparseMatrix<T> * pc_mat,
337 NumericVector<T> & sol,
338 NumericVector<T> & rhs,
339 const std::optional<double> tol,
340 const std::optional<unsigned int> n_iter)
341{
342 if (pc_mat)
343 return this->solve(mat, *pc_mat, sol, rhs, tol, n_iter);
344 else
345 return this->solve(mat, sol, rhs, tol, n_iter);
346}
347
348
349template <typename T>
350inline
351std::pair<unsigned int, Real>
353 const SparseMatrix<T> * pc_mat,
354 NumericVector<T> & sol,
355 NumericVector<T> & rhs,
356 const std::optional<double> tol,
357 const std::optional<unsigned int> n_iter)
358{
359 if (pc_mat)
360 return this->solve(mat, *pc_mat, sol, rhs, tol, n_iter);
361 else
362 return this->solve(mat, sol, rhs, tol, n_iter);
363}
364
365
366} // namespace libMesh
367
368
369#endif // LIBMESH_LINEAR_SOLVER_H
void ErrorVector unsigned int
This base class can be inherited from to provide interfaces to linear solvers from different packages...
virtual std::pair< unsigned int, Real > solve(const ShellMatrix< T > &shell_matrix, const SparseMatrix< T > &precond_matrix, NumericVector< T > &, NumericVector< T > &, const std::optional< double > tol=std::nullopt, const std::optional< unsigned int > m_its=std::nullopt)=0
This function solves a system whose matrix is a shell matrix, but a sparse matrix is used as precondi...
virtual void restrict_solve_to(const std::vector< unsigned int > *const dofs, const SubsetSolveMode subset_solve_mode=SUBSET_ZERO)
After calling this method, all successive solves will be restricted to the given set of dofs,...
virtual void clear()
Release all memory and clear data structures.
Preconditioner< T > * _preconditioner
Holds the Preconditioner object to be used for the linear solves.
int get_int_solver_setting(const std::string &setting_name, const std::optional< int > &setting, const std::optional< int > default_value=std::nullopt)
Get solver settings based on optional parameters and the solver configuration object.
bool initialized() const
PreconditionerType preconditioner_type() const
static std::unique_ptr< LinearSolver< T > > build(const libMesh::Parallel::Communicator &comm_in, const SolverPackage solver_package=libMesh::default_solver_package())
Builds a LinearSolver using the linear solver package specified by solver_package.
std::pair< unsigned int, Real > solve(const ShellMatrix< T > &matrix, const SparseMatrix< T > *precond_matrix, NumericVector< T > &, NumericVector< T > &, const std::optional< double > tol=std::nullopt, const std::optional< unsigned int > m_its=std::nullopt)
This function solves a system whose matrix is a shell matrix, but an optional sparse matrix may be us...
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &, SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const std::optional< double > tol=std::nullopt, const std::optional< unsigned int > m_its=std::nullopt)=0
This function calls the solver "_solver_type" preconditioned with the "_preconditioner_type" precondi...
double get_real_solver_setting(const std::string &setting_name, const std::optional< double > &setting, const std::optional< double > default_value=std::nullopt)
Get solver settings based on optional parameters and the solver configuration object.
virtual LinearConvergenceReason get_converged_reason() const =0
SolverType _solver_type
Enum stating which type of iterative solver to use.
virtual void init(const char *name=nullptr)=0
Initialize data structures if not done so already.
virtual void print_converged_reason() const
Prints a useful message about why the latest linear solve con(di)verged.
bool same_preconditioner
Boolean flag to indicate whether we want to use an identical preconditioner to the previous solve.
PreconditionerType _preconditioner_type
Enum stating with type of preconditioner to use.
virtual void init_systems(const System &)
Apply names to the system to be solved and set auxiliary preconditioner data.
bool _is_initialized
Flag indicating if the data structures have been initialized.
void set_preconditioner_type(const PreconditionerType pct)
Sets the type of preconditioner to use.
SolverConfiguration * _solver_configuration
Optionally store a SolverOptions object that can be used to set parameters like solver type,...
SolverType solver_type() const
void set_solver_configuration(SolverConfiguration &solver_configuration)
Set the solver configuration object.
virtual void reuse_preconditioner(bool)
Set the same_preconditioner flag, which indicates if we reuse the same preconditioner for subsequent ...
virtual ~LinearSolver()
Destructor.
virtual std::pair< unsigned int, Real > solve(const ShellMatrix< T > &shell_matrix, NumericVector< T > &, NumericVector< T > &, const std::optional< double > tol=std::nullopt, const std::optional< unsigned int > m_its=std::nullopt)=0
This function solves a system whose matrix is a shell matrix.
std::pair< unsigned int, Real > solve(SparseMatrix< T > &matrix, SparseMatrix< T > *precond_matrix, NumericVector< T > &, NumericVector< T > &, const std::optional< double > tol=std::nullopt, const std::optional< unsigned int > m_its=std::nullopt)
This function calls the solver "_solver_type" preconditioned with the "_preconditioner_type" precondi...
void set_solver_type(const SolverType st)
Sets the type of solver to use.
void attach_preconditioner(Preconditioner< T > *preconditioner)
Attaches a Preconditioner object to be used.
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const std::optional< double > tol=std::nullopt, const std::optional< unsigned int > m_its=std::nullopt)=0
This function calls the solver _solver_type preconditioned with the _preconditioner_type precondition...
virtual std::pair< unsigned int, Real > adjoint_solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const std::optional< double > tol=std::nullopt, const std::optional< unsigned int > m_its=std::nullopt)
Function to solve the adjoint system.
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
An object whose state is distributed along a set of processors.
This class provides a uniform interface for preconditioners.
This class implements reference counting.
Generic shell matrix, i.e.
This class stores solver configuration data, e.g.
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...
SolverPackage default_solver_package()
Definition libmesh.C:1064
SolverPackage
Defines an enum for various linear solver packages.
PreconditionerType
Defines an enum for preconditioner types.
SolverType
Defines an enum for iterative solver types.
LinearConvergenceReason
Linear solver convergence flags (taken from the PETSc flags).