18 #include "libmesh/libmesh_config.h" 19 #ifdef LIBMESH_HAVE_PETSC 22 #include "libmesh/petsc_shell_matrix.h" 23 #include "libmesh/petsc_matrix_shell_matrix.h" 38 const PetscVector<T> & petsc_arg = cast_ref<const PetscVector<T> &>(arg);
40 LibmeshPetscCall(MatMult(_mat, petsc_arg.vec(), petsc_dest.
vec()));
50 const PetscVector<T> & petsc_arg = cast_ref<const PetscVector<T> &>(arg);
52 LibmeshPetscCall(MatMultAdd(_mat, petsc_arg.vec(), petsc_dest.
vec(), petsc_dest.
vec()));
63 PetscErrorCode ierr = MatDestroy (&_mat);
65 libmesh_warning(
"Warning: MatDestroy returned a non-zero error code which we ignored.");
71 template <
typename Obj>
80 if (obj.initialized())
83 PetscInt m_global =
static_cast<PetscInt
>(m);
84 PetscInt n_global =
static_cast<PetscInt
>(n);
85 PetscInt m_local =
static_cast<PetscInt
>(m_l);
86 PetscInt n_local =
static_cast<PetscInt
>(n_l);
87 PetscInt blocksize =
static_cast<PetscInt
>(blocksize_in);
89 LibmeshPetscCall2(obj.comm(), MatCreate(obj.comm().get(), &obj._mat));
90 LibmeshPetscCall2(obj.comm(), MatSetSizes(obj._mat, m_local, n_local, m_global, n_global));
91 LibmeshPetscCall2(obj.comm(), MatSetBlockSize(obj._mat, blocksize));
92 LibmeshPetscCall2(obj.comm(), MatSetType(obj._mat, MATSHELL));
95 LibmeshPetscCall2(obj.comm(), MatSetOptionsPrefix(obj._mat,
""));
96 LibmeshPetscCall2(obj.comm(), MatSetFromOptions(obj._mat));
97 LibmeshPetscCall2(obj.comm(), MatSetUp(obj._mat));
98 LibmeshPetscCall2(obj.comm(), MatShellSetContext(obj._mat, &obj));
100 obj._is_initialized =
true;
103 template <
typename Obj>
110 if (obj._omit_constrained_dofs)
112 my_m -= obj._dof_map->n_constrained_dofs();
113 m_l -= obj._dof_map->n_local_constrained_dofs();
119 init_shell_mat(obj, my_m, my_n, m_l, n_l, obj._dof_map->block_size());
122 template <
typename T>
128 template <
typename T>
134 template <
typename T>
137 libmesh_error_msg_if(!_mat,
"A petsc shell matrix is not created yet. Please call init() first.");
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.
This class provides a nice interface to PETSc's Vec object.
Mat mat()
Returns a pointer to the underlying PETSc Mat object.
This class allows to use a PETSc shell matrix.
virtual void clear() override
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
The libMesh namespace provides an interface to certain functionality in the library.
void init_shell_mat(Obj &obj, const numeric_index_type m, const numeric_index_type n, const numeric_index_type m_l, const numeric_index_type n_l, const numeric_index_type blocksize_in)
Initialize a shell matrix object.
virtual void vector_mult(NumericVector< T > &dest, const NumericVector< T > &arg) const override
Multiplies the matrix with arg and stores the result in dest.
virtual bool initialized() const
dof_id_type numeric_index_type
bool _is_initialized
Flag that tells if init() has been called.
virtual ~PetscShellMatrix()
bool initialized()
Checks that library initialization has been done.
virtual void init() override