libMesh
sparse_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 
19 
20 #ifndef LIBMESH_SPARSE_MATRIX_H
21 #define LIBMESH_SPARSE_MATRIX_H
22 
23 
24 // Local includes
25 #include "libmesh/libmesh.h"
26 #include "libmesh/libmesh_common.h"
27 #include "libmesh/id_types.h"
28 #include "libmesh/reference_counted_object.h"
29 #include "libmesh/parallel_object.h"
30 
31 // C++ includes
32 #include <cstddef>
33 #include <iomanip>
34 #include <vector>
35 #include <memory>
36 
37 namespace libMesh
38 {
39 
40 // forward declarations
41 template <typename T> class SparseMatrix;
42 template <typename T> class DenseMatrix;
43 class DofMap;
44 namespace SparsityPattern { class Graph; }
45 template <typename T> class NumericVector;
46 
47 // This template helper function must be declared before it
48 // can be defined below.
49 template <typename T>
50 std::ostream & operator << (std::ostream & os, const SparseMatrix<T> & m);
51 
52 
62 template <typename T>
63 class SparseMatrix : public ReferenceCountedObject<SparseMatrix<T>>,
64  public ParallelObject
65 {
66 public:
78  explicit
79  SparseMatrix (const Parallel::Communicator & comm);
80 
85  SparseMatrix (SparseMatrix &&) = default;
86  SparseMatrix (const SparseMatrix &) = default;
87  SparseMatrix & operator= (const SparseMatrix &) = default;
88  SparseMatrix & operator= (SparseMatrix &&) = default;
89  virtual ~SparseMatrix () = default;
90 
95  static std::unique_ptr<SparseMatrix<T>>
96  build(const Parallel::Communicator & comm,
97  const SolverPackage solver_package = libMesh::default_solver_package());
98 
103  virtual bool initialized() const { return _is_initialized; }
104 
108  void attach_dof_map (const DofMap & dof_map)
109  { _dof_map = &dof_map; }
110 
120  virtual bool need_full_sparsity_pattern() const
121  { return false; }
122 
129 
142  virtual void init (const numeric_index_type m,
143  const numeric_index_type n,
144  const numeric_index_type m_l,
145  const numeric_index_type n_l,
146  const numeric_index_type nnz=30,
147  const numeric_index_type noz=10,
148  const numeric_index_type blocksize=1) = 0;
149 
153  virtual void init () = 0;
154 
158  virtual void clear () = 0;
159 
163  virtual void zero () = 0;
164 
168  virtual void zero_rows (std::vector<numeric_index_type> & rows, T diag_value = 0.0);
169 
174  virtual void close () = 0;
175 
181  virtual void flush () { close(); }
182 
186  virtual numeric_index_type m () const = 0;
187 
191  virtual numeric_index_type local_m () const { return row_stop() - row_start(); }
192 
196  virtual numeric_index_type n () const = 0;
197 
202  virtual numeric_index_type row_start () const = 0;
203 
208  virtual numeric_index_type row_stop () const = 0;
209 
215  virtual void set (const numeric_index_type i,
216  const numeric_index_type j,
217  const T value) = 0;
218 
224  virtual void add (const numeric_index_type i,
225  const numeric_index_type j,
226  const T value) = 0;
227 
232  virtual void add_matrix (const DenseMatrix<T> & dm,
233  const std::vector<numeric_index_type> & rows,
234  const std::vector<numeric_index_type> & cols) = 0;
235 
240  virtual void add_matrix (const DenseMatrix<T> & dm,
241  const std::vector<numeric_index_type> & dof_indices) = 0;
242 
249  virtual void add_block_matrix (const DenseMatrix<T> & dm,
250  const std::vector<numeric_index_type> & brows,
251  const std::vector<numeric_index_type> & bcols);
252 
257  virtual void add_block_matrix (const DenseMatrix<T> & dm,
258  const std::vector<numeric_index_type> & dof_indices)
259  { this->add_block_matrix (dm, dof_indices, dof_indices); }
260 
264  virtual void add (const T a, const SparseMatrix<T> & X) = 0;
265 
272  virtual T operator () (const numeric_index_type i,
273  const numeric_index_type j) const = 0;
274 
283  virtual Real l1_norm () const = 0;
284 
295  virtual Real linfty_norm () const = 0;
296 
300  virtual bool closed() const = 0;
301 
306  void print(std::ostream & os=libMesh::out, const bool sparse=false) const;
307 
335  friend std::ostream & operator << <>(std::ostream & os, const SparseMatrix<T> & m);
336 
341  virtual void print_personal(std::ostream & os=libMesh::out) const = 0;
342 
348  virtual void print_matlab(const std::string & /*name*/ = "") const
349  {
350  libmesh_not_implemented();
351  }
352 
358  virtual void create_submatrix(SparseMatrix<T> & submatrix,
359  const std::vector<numeric_index_type> & rows,
360  const std::vector<numeric_index_type> & cols) const
361  {
362  this->_get_submatrix(submatrix,
363  rows,
364  cols,
365  false); // false means DO NOT REUSE submatrix
366  }
367 
374  virtual void reinit_submatrix(SparseMatrix<T> & submatrix,
375  const std::vector<numeric_index_type> & rows,
376  const std::vector<numeric_index_type> & cols) const
377  {
378  this->_get_submatrix(submatrix,
379  rows,
380  cols,
381  true); // true means REUSE submatrix
382  }
383 
388  void vector_mult (NumericVector<T> & dest,
389  const NumericVector<T> & arg) const;
390 
395  void vector_mult_add (NumericVector<T> & dest,
396  const NumericVector<T> & arg) const;
397 
401  virtual void get_diagonal (NumericVector<T> & dest) const = 0;
402 
407  virtual void get_transpose (SparseMatrix<T> & dest) const = 0;
408 
416  virtual void get_row(numeric_index_type /*i*/,
417  std::vector<numeric_index_type> & /*indices*/,
418  std::vector<T> & /*values*/) const
419  {
420  libmesh_not_implemented();
421  }
422 
423 protected:
424 
432  virtual void _get_submatrix(SparseMatrix<T> & /*submatrix*/,
433  const std::vector<numeric_index_type> & /*rows*/,
434  const std::vector<numeric_index_type> & /*cols*/,
435  const bool /*reuse_submatrix*/) const
436  {
437  libmesh_not_implemented();
438  }
439 
443  DofMap const * _dof_map;
444 
449 };
450 
451 
452 
453 //-----------------------------------------------------------------------
454 // SparseMatrix inline members
455 
456 // For SGI MIPSpro this implementation must occur after
457 // the full specialization of the print() member.
458 //
459 // It's generally easier to define these friend functions in the header
460 // file.
461 template <typename T>
462 std::ostream & operator << (std::ostream & os, const SparseMatrix<T> & m)
463 {
464  m.print(os);
465  return os;
466 }
467 
468 
469 } // namespace libMesh
470 
471 
472 #endif // LIBMESH_SPARSE_MATRIX_H
libMesh::SparseMatrix::SparseMatrix
SparseMatrix(const Parallel::Communicator &comm)
Constructor; initializes the matrix to be empty, without any structure, i.e.
Definition: sparse_matrix.C:45
libMesh::SparseMatrix::close
virtual void close()=0
Calls the SparseMatrix's internal assembly routines, ensuring that the values are consistent across p...
libMesh::SolverPackage
SolverPackage
Defines an enum for various linear solver packages.
Definition: enum_solver_package.h:34
libMesh::SparseMatrix::operator()
virtual T operator()(const numeric_index_type i, const numeric_index_type j) const =0
libMesh::SparseMatrix::get_row
virtual void get_row(numeric_index_type, std::vector< numeric_index_type > &, std::vector< T > &) const
Get a row from the matrix.
Definition: sparse_matrix.h:416
libMesh::SparseMatrix::vector_mult
void vector_mult(NumericVector< T > &dest, const NumericVector< T > &arg) const
Multiplies the matrix by the NumericVector arg and stores the result in NumericVector dest.
Definition: sparse_matrix.C:170
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::operator<<
std::ostream & operator<<(std::ostream &os, const OrderWrapper &order)
Overload stream operators.
Definition: fe_type.h:164
libMesh::SparseMatrix::initialized
virtual bool initialized() const
Definition: sparse_matrix.h:103
libMesh::SparseMatrix::get_diagonal
virtual void get_diagonal(NumericVector< T > &dest) const =0
Copies the diagonal part of the matrix into dest.
libMesh::ParallelObject::comm
const Parallel::Communicator & comm() const
Definition: parallel_object.h:94
libMesh::DenseMatrix
Defines a dense matrix for use in Finite Element-type computations.
Definition: dof_map.h:73
libMesh::SparseMatrix::print_matlab
virtual void print_matlab(const std::string &="") const
Print the contents of the matrix in Matlab's sparse matrix format.
Definition: sparse_matrix.h:348
libMesh::SparseMatrix::linfty_norm
virtual Real linfty_norm() const =0
libMesh::SparseMatrix::_dof_map
const DofMap * _dof_map
The DofMap object associated with this object.
Definition: sparse_matrix.h:443
libMesh::default_solver_package
SolverPackage default_solver_package()
Definition: libmesh.C:993
libMesh::SparseMatrix::clear
virtual void clear()=0
Restores the SparseMatrix<T> to a pristine state.
libMesh::SparseMatrix::add_block_matrix
virtual void add_block_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &dof_indices)
Same as add_block_matrix(), but assumes the row and column maps are the same.
Definition: sparse_matrix.h:257
libMesh::SparseMatrix
Generic sparse matrix.
Definition: vector_fe_ex5.C:45
libMesh::SparseMatrix::need_full_sparsity_pattern
virtual bool need_full_sparsity_pattern() const
Definition: sparse_matrix.h:120
libMesh::NumericVector
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
Definition: vector_fe_ex5.C:43
libMesh::SparseMatrix::zero_rows
virtual void zero_rows(std::vector< numeric_index_type > &rows, T diag_value=0.0)
Sets all row entries to 0 then puts diag_value in the diagonal entry.
Definition: sparse_matrix.C:191
libMesh::SparseMatrix::flush
virtual void flush()
For PETSc matrix , this function is similar to close but without shrinking memory.
Definition: sparse_matrix.h:181
libMesh::SparseMatrix::closed
virtual bool closed() const =0
libMesh::SparseMatrix::local_m
virtual numeric_index_type local_m() const
Get the number of rows owned by this process.
Definition: sparse_matrix.h:191
libMesh::SparseMatrix::vector_mult_add
void vector_mult_add(NumericVector< T > &dest, const NumericVector< T > &arg) const
Multiplies the matrix by the NumericVector arg and adds the result to the NumericVector dest.
Definition: sparse_matrix.C:180
libMesh::SparseMatrix::m
virtual numeric_index_type m() const =0
libMesh::SparseMatrix::~SparseMatrix
virtual ~SparseMatrix()=default
libMesh::SparseMatrix::add
virtual void add(const numeric_index_type i, const numeric_index_type j, const T value)=0
Add value to the element (i,j).
libMesh::numeric_index_type
dof_id_type numeric_index_type
Definition: id_types.h:99
libMesh::SparseMatrix::_get_submatrix
virtual void _get_submatrix(SparseMatrix< T > &, const std::vector< numeric_index_type > &, const std::vector< numeric_index_type > &, const bool) const
Protected implementation of the create_submatrix and reinit_submatrix routines.
Definition: sparse_matrix.h:432
libMesh::SparseMatrix::add_matrix
virtual void add_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols)=0
Add the full matrix dm to the SparseMatrix.
libMesh::SparseMatrix::update_sparsity_pattern
virtual void update_sparsity_pattern(const SparsityPattern::Graph &)
Updates the matrix sparsity pattern.
Definition: sparse_matrix.h:128
libMesh::SparseMatrix::build
static std::unique_ptr< SparseMatrix< T > > build(const Parallel::Communicator &comm, const SolverPackage solver_package=libMesh::default_solver_package())
Builds a SparseMatrix<T> using the linear solver package specified by solver_package.
Definition: sparse_matrix.C:130
libMesh::SparseMatrix::init
virtual void init()=0
Initialize this matrix using the sparsity structure computed by dof_map.
libMesh::SparseMatrix::operator=
SparseMatrix & operator=(const SparseMatrix &)=default
libMesh::SparseMatrix::row_stop
virtual numeric_index_type row_stop() const =0
value
static const bool value
Definition: xdr_io.C:56
libMesh::DofMap
This class handles the numbering of degrees of freedom on a mesh.
Definition: dof_map.h:176
libMesh::SparseMatrix::attach_dof_map
void attach_dof_map(const DofMap &dof_map)
Get a pointer to the DofMap to use.
Definition: sparse_matrix.h:108
libMesh::SparseMatrix::print_personal
virtual void print_personal(std::ostream &os=libMesh::out) const =0
Print the contents of the matrix to the screen in a package-personalized style, if available.
libMesh::SparseMatrix::l1_norm
virtual Real l1_norm() const =0
libMesh::SparseMatrix::create_submatrix
virtual void create_submatrix(SparseMatrix< T > &submatrix, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) const
This function creates a matrix called "submatrix" which is defined by the row and column indices give...
Definition: sparse_matrix.h:358
libMesh::SparseMatrix::set
virtual void set(const numeric_index_type i, const numeric_index_type j, const T value)=0
Set the element (i,j) to value.
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::SparseMatrix::get_transpose
virtual void get_transpose(SparseMatrix< T > &dest) const =0
Copies the transpose of the matrix into dest, which may be *this.
libMesh::SparseMatrix::print
void print(std::ostream &os=libMesh::out, const bool sparse=false) const
Print the contents of the matrix to the screen in a uniform style, regardless of matrix/solver packag...
Definition: sparse_matrix.C:200
libMesh::SparseMatrix::reinit_submatrix
virtual void reinit_submatrix(SparseMatrix< T > &submatrix, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) const
This function is similar to the one above, but it allows you to reuse the existing sparsity pattern o...
Definition: sparse_matrix.h:374
libMesh::out
OStreamProxy out
libMesh::SparsityPattern::Graph
Definition: sparsity_pattern.h:52
libMesh::SparseMatrix::zero
virtual void zero()=0
Set all entries to 0.
libMesh::SparseMatrix::_is_initialized
bool _is_initialized
Flag indicating whether or not the matrix has been initialized.
Definition: sparse_matrix.h:448
libMesh::SparseMatrix::add_block_matrix
virtual void add_block_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &brows, const std::vector< numeric_index_type > &bcols)
Add the full matrix dm to the SparseMatrix.
Definition: sparse_matrix.C:56
libMesh::SparseMatrix::row_start
virtual numeric_index_type row_start() const =0
libMesh::SparseMatrix::n
virtual numeric_index_type n() const =0