libMesh
preconditioner.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 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  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  void set_matrix(SparseMatrix<Number> & mat);
116 
121 
125  void set_type (const PreconditionerType pct);
126 
127 protected:
128 
134 
139 
144 };
145 
146 
147 
148 
149 /*----------------------- inline functions ----------------------------------*/
150 template <typename T>
151 inline
153 {
154  this->clear ();
155 }
156 
157 template <typename T>
158 void
160 {
161  //If the matrix is changing then we (probably) need to reinitialize.
162  _is_initialized = false;
163  _matrix = &mat;
164 }
165 
166 template <typename T>
167 void
169 {
170  //If the preconditioner type changes we (probably) need to reinitialize.
171  _is_initialized = false;
172  _preconditioner_type = pct;
173 }
174 
175 } // namespace libMesh
176 
177 
178 #endif // LIBMESH_PRECONDITIONER_H
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:43
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.
SolverPackage default_solver_package()
Definition: libmesh.C:1050
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:247
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...