libMesh
petsc_shell_matrix.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 #ifndef LIBMESH_PETSC_SHELL_MATRIX_H
19 #define LIBMESH_PETSC_SHELL_MATRIX_H
20 
21 
22 #include "libmesh/libmesh_config.h"
23 #ifdef LIBMESH_HAVE_PETSC
24 
25 
26 // Local includes
27 #include "libmesh/libmesh_common.h"
28 #include "libmesh/reference_counted_object.h"
29 #include "libmesh/libmesh.h"
30 #include "libmesh/shell_matrix.h"
31 #include "libmesh/petsc_macro.h"
32 #include "libmesh/petsc_solver_exception.h"
33 #include "libmesh/petsc_vector.h"
34 #include "libmesh/libmesh_common.h"
35 
36 // Petsc include files.
37 #ifdef I
38 # define LIBMESH_SAW_I
39 #endif
40 #include <petscmat.h>
41 #ifndef LIBMESH_SAW_I
42 # undef I // Avoid complex.h contamination
43 #endif
44 
45 namespace libMesh
46 {
47 
56 template <typename T>
57 class PetscShellMatrix : public ShellMatrix<T>
58 {
59 public:
60 
61  PetscShellMatrix (const Parallel::Communicator & comm_in);
62 
66  virtual ~PetscShellMatrix ();
67 
68  virtual numeric_index_type m () const override;
69 
70  virtual numeric_index_type n () const override;
71 
72  virtual numeric_index_type local_m () const;
73 
74  virtual numeric_index_type local_n () const;
75 
76  virtual void vector_mult (NumericVector<T> & dest,
77  const NumericVector<T> & arg) const override;
78 
79  virtual void vector_mult_add (NumericVector<T> & dest,
80  const NumericVector<T> & arg) const override;
81 
82  virtual void get_diagonal (NumericVector<T> & dest) const override;
83 
84  virtual void clear () override;
85 
86  virtual void init () override;
87 
92  virtual bool initialized() const { return _is_initialized; }
93 
94  Mat mat() { if (_mat) return _mat; else libmesh_error_msg("A petsc shell matrix is not created yet. Please init() "); }
95 
96 protected:
97 
102  Mat _mat;
103 
105 };
106 
107 
108 //-----------------------------------------------------------------------
109 // PetscShellMatrix inline members
110 template <typename T>
111 inline
112 PetscShellMatrix<T>::PetscShellMatrix (const Parallel::Communicator & comm_in):
113  ShellMatrix<T>(comm_in),
114  _mat(nullptr),
115  _is_initialized(false)
116 {}
117 
118 
119 
120 template <typename T>
121 inline
123 {
124  this->clear();
125 }
126 
127 
128 
129 template <typename T>
130 inline
132 {
133  PetscErrorCode ierr;
134  PetscInt m;
135 
136  ierr = MatGetSize(_mat,&m,nullptr);
137  LIBMESH_CHKERR(ierr);
138 
139  return m;
140 }
141 
142 
143 
144 template <typename T>
145 inline
147 {
148  PetscErrorCode ierr;
149  PetscInt n;
150 
151  ierr = MatGetSize(_mat,nullptr,&n);
152  LIBMESH_CHKERR(ierr);
153 
154  return n;
155 }
156 
157 
158 template <typename T>
159 inline
161 {
162  PetscErrorCode ierr;
163  PetscInt m;
164 
165  ierr = MatGetLocalSize(_mat,&m,nullptr);
166  LIBMESH_CHKERR(ierr);
167 
168  return m;
169 }
170 
171 
172 
173 template <typename T>
174 inline
176 {
177  PetscErrorCode ierr;
178  PetscInt n;
179 
180  ierr = MatGetLocalSize(_mat,nullptr,&n);
181  LIBMESH_CHKERR(ierr);
182 
183  return n;
184 }
185 
186 
187 template <typename T>
188 inline
190 {
191  // Make sure the NumericVector passed in is really a PetscVector
192  PetscVector<T> & petsc_dest = cast_ref<PetscVector<T> &>(dest);
193 
194  PetscErrorCode ierr =
195  MatGetDiagonal(_mat,petsc_dest.vec()); LIBMESH_CHKERR(ierr);
196 }
197 
198 
199 
200 } // namespace libMesh
201 
202 #endif // LIBMESH_HAVE_PETSC
203 #endif // LIBMESH_SPARSE_SHELL_MATRIX_H
libMesh::PetscShellMatrix::vector_mult
virtual void vector_mult(NumericVector< T > &dest, const NumericVector< T > &arg) const override
Multiplies the matrix with arg and stores the result in dest.
Definition: petsc_shell_matrix.C:28
libMesh::ShellMatrix
Generic shell matrix, i.e.
Definition: eigen_preconditioner.h:36
libMesh::PetscShellMatrix::_is_initialized
bool _is_initialized
Definition: petsc_shell_matrix.h:104
libMesh::PetscShellMatrix::~PetscShellMatrix
virtual ~PetscShellMatrix()
Destructor.
Definition: petsc_shell_matrix.h:122
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::PetscShellMatrix::clear
virtual void clear() override
Definition: petsc_shell_matrix.C:59
libMesh::ierr
ierr
Definition: petsc_dm_wrapper.C:72
libMesh::PetscShellMatrix::local_m
virtual numeric_index_type local_m() const
Definition: petsc_shell_matrix.h:160
libMesh::NumericVector
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
Definition: vector_fe_ex5.C:43
libMesh::PetscShellMatrix
This class allows to use a PETSc shell matrix.
Definition: petsc_shell_matrix.h:57
libMesh::PetscShellMatrix::PetscShellMatrix
PetscShellMatrix(const Parallel::Communicator &comm_in)
Definition: petsc_shell_matrix.h:112
libMesh::PetscVector
This class provides a nice interface to PETSc's Vec object.
Definition: petsc_vector.h:72
libMesh::PetscShellMatrix::local_n
virtual numeric_index_type local_n() const
Definition: petsc_shell_matrix.h:175
libMesh::numeric_index_type
dof_id_type numeric_index_type
Definition: id_types.h:99
libMesh::PetscShellMatrix::m
virtual numeric_index_type m() const override
Definition: petsc_shell_matrix.h:131
libMesh::PetscShellMatrix::_mat
Mat _mat
Petsc Shell Matrix.
Definition: petsc_shell_matrix.h:102
libMesh::libMeshPrivateData::_is_initialized
bool _is_initialized
Flag that tells if init() has been called.
Definition: libmesh.C:246
libMesh::PetscShellMatrix::init
virtual void init() override
Definition: petsc_shell_matrix.C:74
libMesh::PetscShellMatrix::vector_mult_add
virtual void vector_mult_add(NumericVector< T > &dest, const NumericVector< T > &arg) const override
Multiplies the matrix with arg and adds the result to dest.
Definition: petsc_shell_matrix.C:44
libMesh::PetscShellMatrix::initialized
virtual bool initialized() const
Definition: petsc_shell_matrix.h:92
libMesh::PetscVector::vec
Vec vec()
Definition: petsc_vector.h:335
libMesh::PetscShellMatrix::n
virtual numeric_index_type n() const override
Definition: petsc_shell_matrix.h:146
libMesh::PetscShellMatrix::get_diagonal
virtual void get_diagonal(NumericVector< T > &dest) const override
Copies the diagonal part of the matrix into dest.
Definition: petsc_shell_matrix.h:189
libMesh::PetscShellMatrix::mat
Mat mat()
Definition: petsc_shell_matrix.h:94