libMesh
petsc_matrix.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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_MATRIX_H
21 #define LIBMESH_PETSC_MATRIX_H
22 
23 #include "libmesh/libmesh_common.h"
24 
25 #ifdef LIBMESH_HAVE_PETSC
26 
27 // Local includes
28 #include "libmesh/sparse_matrix.h"
29 #include "libmesh/petsc_macro.h"
30 #include "libmesh/parallel.h"
31 
32 // C++ includes
33 #include <algorithm>
34 
35 // Macro to identify and debug functions which should be called in
36 // parallel on parallel matrices but which may be called in serial on
37 // serial matrices. This macro will only be valid inside non-static
38 // PetscMatrix methods
39 #undef semiparallel_only
40 #ifndef NDEBUG
41 #include <cstring>
42 
43 #define semiparallel_only() do { if (this->initialized()) { const char * mytype; \
44  MatGetType(_mat,&mytype); \
45  if (!strcmp(mytype, MATSEQAIJ)) \
46  parallel_object_only(); } } while (0)
47 #else
48 #define semiparallel_only()
49 #endif
50 
51 
52 // Petsc include files.
53 #include <petscmat.h>
54 
55 
56 
57 namespace libMesh
58 {
59 
60 // Forward Declarations
61 template <typename T> class DenseMatrix;
62 
63 
73 template <typename T>
74 class PetscMatrix libmesh_final : public SparseMatrix<T>
75 {
76 public:
87  explicit
88  PetscMatrix (const Parallel::Communicator & comm_in
89  LIBMESH_CAN_DEFAULT_TO_COMMWORLD);
90 
98  explicit
99  PetscMatrix (Mat m,
100  const Parallel::Communicator & comm_in
101  LIBMESH_CAN_DEFAULT_TO_COMMWORLD);
102 
107  ~PetscMatrix ();
108 
109  virtual void init (const numeric_index_type m,
110  const numeric_index_type n,
111  const numeric_index_type m_l,
112  const numeric_index_type n_l,
113  const numeric_index_type nnz=30,
114  const numeric_index_type noz=10,
115  const numeric_index_type blocksize=1) libmesh_override;
116 
128  void init (const numeric_index_type m,
129  const numeric_index_type n,
130  const numeric_index_type m_l,
131  const numeric_index_type n_l,
132  const std::vector<numeric_index_type> & n_nz,
133  const std::vector<numeric_index_type> & n_oz,
134  const numeric_index_type blocksize=1);
135 
136  virtual void init () libmesh_override;
137 
143  void update_preallocation_and_zero();
144 
145  virtual void clear () libmesh_override;
146 
147  virtual void zero () libmesh_override;
148 
149  virtual void zero_rows (std::vector<numeric_index_type> & rows, T diag_value = 0.0) libmesh_override;
150 
151  virtual void close () libmesh_override;
152 
153  virtual void flush () libmesh_override;
154 
155  virtual numeric_index_type m () const libmesh_override;
156 
157  virtual numeric_index_type n () const libmesh_override;
158 
159  virtual numeric_index_type row_start () const libmesh_override;
160 
161  virtual numeric_index_type row_stop () const libmesh_override;
162 
163  virtual void set (const numeric_index_type i,
164  const numeric_index_type j,
165  const T value) libmesh_override;
166 
167  virtual void add (const numeric_index_type i,
168  const numeric_index_type j,
169  const T value) libmesh_override;
170 
171  virtual void add_matrix (const DenseMatrix<T> & dm,
172  const std::vector<numeric_index_type> & rows,
173  const std::vector<numeric_index_type> & cols) libmesh_override;
174 
175  virtual void add_matrix (const DenseMatrix<T> & dm,
176  const std::vector<numeric_index_type> & dof_indices) libmesh_override;
177 
178  virtual void add_block_matrix (const DenseMatrix<T> & dm,
179  const std::vector<numeric_index_type> & brows,
180  const std::vector<numeric_index_type> & bcols) libmesh_override;
181 
182  virtual void add_block_matrix (const DenseMatrix<T> & dm,
183  const std::vector<numeric_index_type> & dof_indices) libmesh_override
184  { this->add_block_matrix (dm, dof_indices, dof_indices); }
185 
199  virtual void add (const T a, SparseMatrix<T> & X) libmesh_override;
200 
201  virtual T operator () (const numeric_index_type i,
202  const numeric_index_type j) const libmesh_override;
203 
204  virtual Real l1_norm () const libmesh_override;
205 
206  virtual Real linfty_norm () const libmesh_override;
207 
208  virtual bool closed() const libmesh_override;
209 
216  virtual void print_personal(std::ostream & os=libMesh::out) const libmesh_override;
217 
218  virtual void print_matlab(const std::string & name = "") const libmesh_override;
219 
220  virtual void get_diagonal (NumericVector<T> & dest) const libmesh_override;
221 
222  virtual void get_transpose (SparseMatrix<T> & dest) const libmesh_override;
223 
228  void swap (PetscMatrix<T> &);
229 
238  Mat mat () { libmesh_assert (_mat); return _mat; }
239 
240 protected:
241 
252  virtual void _get_submatrix(SparseMatrix<T> & submatrix,
253  const std::vector<numeric_index_type> & rows,
254  const std::vector<numeric_index_type> & cols,
255  const bool reuse_submatrix) const libmesh_override;
256 
257 private:
258 
262  Mat _mat;
263 
269 };
270 
271 } // namespace libMesh
272 
273 #endif // #ifdef LIBMESH_HAVE_PETSC
274 #endif // LIBMESH_PETSC_MATRIX_H
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:39
bool closed()
Checks that the library has been closed.
Definition: libmesh.C:281
Mat _mat
PETSc matrix datatype to store values.
Definition: petsc_matrix.h:262
Numeric vector.
Definition: dof_map.h:66
The libMesh namespace provides an interface to certain functionality in the library.
const Number zero
.
Definition: libmesh.h:178
libmesh_assert(j)
Generic sparse matrix.
Definition: dof_map.h:65
dof_id_type numeric_index_type
Definition: id_types.h:92
void init(triangulateio &t)
Initializes the fields of t to NULL/0 as necessary.
bool _destroy_mat_on_exit
This boolean value should only be set to false for the constructor which takes a PETSc Mat object...
Definition: petsc_matrix.h:268
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
X_input swap(X_system)
OStreamProxy out
static const bool value
Definition: xdr_io.C:108
Defines a dense matrix for use in Finite Element-type computations.
Definition: dof_map.h:64