libMesh
petsc_shell_matrix.C
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 #include "libmesh/libmesh_config.h"
19 #ifdef LIBMESH_HAVE_PETSC
20 
21 // Local includes
22 #include "libmesh/petsc_shell_matrix.h"
23 
24 namespace libMesh
25 {
26 
27 template <typename T>
29  const NumericVector<T> & arg) const
30 {
31  PetscErrorCode ierr=0;
32 
33  PetscVector<T> & petsc_dest = cast_ref<PetscVector<T> &>(dest);
34 
35  const PetscVector<T> & petsc_arg = cast_ref<const PetscVector<T> &>(arg);
36 
37  ierr = MatMult(_mat, petsc_arg.vec(), petsc_dest.vec());
38  LIBMESH_CHKERR(ierr);
39 }
40 
41 
42 
43 template <typename T>
45  const NumericVector<T> & arg) const
46 {
47  PetscErrorCode ierr=0;
48 
49  PetscVector<T> & petsc_dest = cast_ref<PetscVector<T> &>(dest);
50 
51  const PetscVector<T> & petsc_arg = cast_ref<const PetscVector<T> &>(arg);
52 
53  ierr = MatMultAdd(_mat, petsc_arg.vec(), petsc_dest.vec(), petsc_dest.vec());
54  LIBMESH_CHKERR(ierr);
55 }
56 
57 
58 template <typename T>
60 {
61  PetscErrorCode ierr=0;
62 
63  if ((this->initialized()))
64  {
65  ierr = LibMeshMatDestroy (&_mat);
66  LIBMESH_CHKERR(ierr);
67 
68  this->_is_initialized = false;
69  }
70 }
71 
72 
73 template <typename T>
75 {
76  libmesh_assert(this->_dof_map);
77 
78  // Clear initialized matrices
79  if (this->initialized())
80  this->clear();
81 
82  this->_is_initialized = true;
83 
84 
85  const numeric_index_type my_m = this->_dof_map->n_dofs();
86  const numeric_index_type my_n = my_m;
87  const numeric_index_type n_l = this->_dof_map->n_dofs_on_processor(this->processor_id());
88  const numeric_index_type m_l = n_l;
89 
90 
91  PetscErrorCode ierr = 0;
92  PetscInt m_global = static_cast<PetscInt>(my_m);
93  PetscInt n_global = static_cast<PetscInt>(my_n);
94  PetscInt m_local = static_cast<PetscInt>(m_l);
95  PetscInt n_local = static_cast<PetscInt>(n_l);
96 
97  ierr = MatCreate(this->comm().get(), &_mat);
98  LIBMESH_CHKERR(ierr);
99  ierr = MatSetSizes(_mat, m_local, n_local, m_global, n_global);
100  LIBMESH_CHKERR(ierr);
101  PetscInt blocksize = static_cast<PetscInt>(this->_dof_map->block_size());
102  ierr = MatSetBlockSize(_mat,blocksize);
103  LIBMESH_CHKERR(ierr);
104 
105  ierr = MatSetType(_mat, MATSHELL);
106  LIBMESH_CHKERR(ierr);
107 
108  // Is prefix information available somewhere? Perhaps pass in the system name?
109  ierr = MatSetOptionsPrefix(_mat, "");
110  LIBMESH_CHKERR(ierr);
111  ierr = MatSetFromOptions(_mat);
112  LIBMESH_CHKERR(ierr);
113  ierr = MatSetUp(_mat);
114  LIBMESH_CHKERR(ierr);
115 }
116 
117 //------------------------------------------------------------------
118 // Explicit instantiations
119 template class PetscShellMatrix<Number>;
120 
121 } // namespace libMesh
122 
123 #endif
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
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::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::libmesh_assert
libmesh_assert(ctx)
libMesh::PetscVector
This class provides a nice interface to PETSc's Vec object.
Definition: petsc_vector.h:72
libMesh::numeric_index_type
dof_id_type numeric_index_type
Definition: id_types.h:99
libMesh::initialized
bool initialized()
Checks that library initialization has been done.
Definition: libmesh.C:265
libMesh::ReferenceElem::get
const Elem & get(const ElemType type_in)
Definition: reference_elem.C:237
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::PetscVector::vec
Vec vec()
Definition: petsc_vector.h:335