libMesh
preconditioner.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 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_PRECONDITIONER_H
21 #define LIBMESH_PRECONDITIONER_H
22 
23 
24 // Local includes
25 #include "libmesh/libmesh_common.h"
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 
33 namespace libMesh
34 {
35 
36 // forward declarations
37 template <typename T> class SparseMatrix;
38 template <typename T> class NumericVector;
39 template <typename T> class ShellMatrix;
40 enum SolverPackage : int;
41 enum PreconditionerType : int;
42 
55 template <typename T>
56 class Preconditioner : public ReferenceCountedObject<Preconditioner<T>>,
57  public ParallelObject
58 {
59 public:
60 
65 
69  virtual ~Preconditioner ();
70 
76  static std::unique_ptr<Preconditioner<T>>
78  const SolverPackage solver_package = libMesh::default_solver_package());
79 
84  virtual bool initialized () const { return _is_initialized; }
85 
91  virtual void apply(const NumericVector<T> & x, NumericVector<T> & y) = 0;
92 
96  virtual void clear () {}
97 
103  virtual void init () {}
104 
110  virtual void setup () {}
111 
115  virtual void zero() {}
116 
120  void set_matrix(SparseMatrix<Number> & mat);
121 
126 
130  void set_type (const PreconditionerType pct);
131 
132 protected:
133 
139 
144 
149 };
150 
151 
152 
153 
154 /*----------------------- inline functions ----------------------------------*/
155 template <typename T>
156 inline
158 {
159  this->clear ();
160 }
161 
162 template <typename T>
163 void
165 {
166  //If the matrix is changing then we (probably) need to reinitialize.
167  _is_initialized = false;
168  _matrix = &mat;
169 }
170 
171 template <typename T>
172 void
174 {
175  //If the preconditioner type changes we (probably) need to reinitialize.
176  _is_initialized = false;
177  _preconditioner_type = pct;
178 }
179 
180 } // namespace libMesh
181 
182 
183 #endif // LIBMESH_PRECONDITIONER_H
virtual void zero()
Can be used to zero items relevant to the preconditioner.
virtual void clear()
Release all memory and clear data structures.
PreconditionerType type() const
virtual void apply(const NumericVector< T > &x, NumericVector< T > &y)=0
Computes the preconditioned vector y based on input vector x.
PreconditionerType _preconditioner_type
Enum stating with type of preconditioner to use.
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
Definition: vector_fe_ex5.C:44
const Parallel::Communicator & comm() const
void set_matrix(SparseMatrix< Number > &mat)
Sets the matrix to be preconditioned.
The libMesh namespace provides an interface to certain functionality in the library.
virtual bool initialized() const
SolverPackage default_solver_package()
Definition: libmesh.C:1117
This class provides a uniform interface for preconditioners.
Preconditioner(const libMesh::Parallel::Communicator &comm)
Constructor.
bool _is_initialized
Flag that tells if init() has been called.
Definition: libmesh.C:257
This class implements reference counting.
An object whose state is distributed along a set of processors.
bool _is_initialized
Flag indicating if the data structures have been initialized.
PreconditionerType
Defines an enum for preconditioner types.
void set_type(const PreconditionerType pct)
Sets the type of preconditioner to use.
static std::unique_ptr< Preconditioner< T > > build_preconditioner(const libMesh::Parallel::Communicator &comm, const SolverPackage solver_package=libMesh::default_solver_package())
Builds a Preconditioner using the linear solver package specified by solver_package, returning the result wrapped in a std::unique_ptr for safety.
SolverPackage
Defines an enum for various linear solver packages.
virtual void init()
Initialize data structures if not done so already.
virtual void setup()
This is called every time the "operator might have changed".
void ErrorVector unsigned int
Definition: adjoints_ex3.C:360
virtual ~Preconditioner()
Destructor.
SparseMatrix< T > * _matrix
The matrix P...