libMesh
petsc_preconditioner.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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_PRECONDITIONER_H
21 #define LIBMESH_PETSC_PRECONDITIONER_H
22 
23 #include "libmesh/libmesh_config.h"
24 
25 #ifdef LIBMESH_HAVE_PETSC
26 
27 // Local includes
28 #include "libmesh/preconditioner.h"
29 #include "libmesh/libmesh_common.h"
30 #include "libmesh/reference_counted_object.h"
31 #include "libmesh/libmesh.h"
32 #include "libmesh/petsc_macro.h"
33 
34 #ifdef LIBMESH_FORWARD_DECLARE_ENUMS
35 namespace libMesh
36 {
37 enum PreconditionerType : int;
38 }
39 #else
40 #include "libmesh/enum_preconditioner_type.h"
41 #endif
42 
43 // Petsc includes
44 #include "petscpc.h"
45 
46 namespace libMesh
47 {
48 
49 // forward declarations
50 template <typename T> class SparseMatrix;
51 template <typename T> class NumericVector;
52 template <typename T> class ShellMatrix;
53 
62 template <typename T>
64 {
65 public:
66 
70  PetscPreconditioner (const libMesh::Parallel::Communicator & comm_in);
71 
75  virtual ~PetscPreconditioner ();
76 
77  virtual void apply(const NumericVector<T> & x, NumericVector<T> & y) override;
78 
79  virtual void clear () override;
80 
81  virtual void init () override;
82 
87  PC pc() { return _pc; }
88 
92  static void set_petsc_preconditioner_type (const PreconditionerType & preconditioner_type, PC & pc);
93 
94 protected:
95 
99  PC _pc;
100 
104  Mat _mat;
105 
106 private:
114 #if PETSC_VERSION_LESS_THAN(3,0,0)
115  // In Petsc 2.3.3, PCType was #define'd as const char *
116  static void set_petsc_subpreconditioner_type(PCType type, PC & pc);
117 #else
118  // In later versions, PCType is #define'd as char *, so we need the const
119  static void set_petsc_subpreconditioner_type(const PCType type, PC & pc);
120 #endif
121 };
122 
123 
124 
125 
126 /*----------------------- inline functions ----------------------------------*/
127 template <typename T>
128 inline
129 PetscPreconditioner<T>::PetscPreconditioner (const libMesh::Parallel::Communicator & comm_in) :
130  Preconditioner<T>(comm_in),
131  _pc(PETSC_NULL)
132 {
133 }
134 
135 
136 
137 template <typename T>
138 inline
140 {
141  this->clear ();
142 }
143 
144 } // namespace libMesh
145 
146 #endif // #ifdef LIBMESH_HAVE_PETSC
147 #endif // LIBMESH_PETSC_PRECONDITIONER_H
libMesh::PetscPreconditioner
This class provides an interface to the suite of preconditioners available from PETSc.
Definition: petsc_preconditioner.h:63
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::PetscPreconditioner::pc
PC pc()
Definition: petsc_preconditioner.h:87
libMesh::PetscPreconditioner::PetscPreconditioner
PetscPreconditioner(const libMesh::Parallel::Communicator &comm_in)
Constructor.
Definition: petsc_preconditioner.h:129
libMesh::PetscPreconditioner::init
virtual void init() override
Initialize data structures if not done so already.
Definition: petsc_preconditioner.C:57
libMesh::PetscPreconditioner::_pc
PC _pc
Preconditioner context.
Definition: petsc_preconditioner.h:99
libMesh::PetscPreconditioner::set_petsc_subpreconditioner_type
static void set_petsc_subpreconditioner_type(PCType type, PC &pc)
Some PETSc preconditioners (ILU, LU) don't work in parallel.
libMesh::NumericVector
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
Definition: vector_fe_ex5.C:43
libMesh::PetscPreconditioner::clear
virtual void clear() override
Release all memory and clear data structures.
Definition: petsc_preconditioner.C:102
libMesh::PetscPreconditioner::~PetscPreconditioner
virtual ~PetscPreconditioner()
Destructor.
Definition: petsc_preconditioner.h:139
libMesh::Preconditioner
This class provides a uniform interface for preconditioners.
Definition: preconditioner.h:66
libMesh::PreconditionerType
PreconditionerType
Defines an enum for preconditioner types.
Definition: enum_preconditioner_type.h:33
libMesh::PetscPreconditioner::apply
virtual void apply(const NumericVector< T > &x, NumericVector< T > &y) override
Computes the preconditioned vector y based on input vector x.
Definition: petsc_preconditioner.C:41
libMesh::PetscPreconditioner::_mat
Mat _mat
PETSc Mat pulled out of the _matrix object during init().
Definition: petsc_preconditioner.h:104
libMesh::Preconditioner::type
PreconditionerType type() const
Definition: preconditioner.h:142
libMesh::PetscPreconditioner::set_petsc_preconditioner_type
static void set_petsc_preconditioner_type(const PreconditionerType &preconditioner_type, PC &pc)
Tells PETSc to use the user-specified preconditioner.
Definition: petsc_preconditioner.C:115
int
void ErrorVector unsigned int
Definition: adjoints_ex3.C:360