libMesh
sparse_matrix.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 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/reference_counted_object.h"
28 #include "libmesh/id_types.h"
29 #include "libmesh/parallel_object.h"
30 #include "libmesh/enum_parallel_type.h" // PARALLEL
31 #include "libmesh/enum_matrix_build_type.h" // AUTOMATIC
32 
33 // C++ includes
34 #include <cstddef>
35 #include <iomanip>
36 #include <vector>
37 #include <memory>
38 
39 namespace libMesh
40 {
41 
42 // forward declarations
43 template <typename T> class SparseMatrix;
44 template <typename T> class DenseMatrix;
45 class DofMap;
46 namespace SparsityPattern {
47  class Build;
48  class Graph;
49 }
50 template <typename T> class NumericVector;
51 
52 // This template helper function must be declared before it
53 // can be defined below.
54 template <typename T>
55 std::ostream & operator << (std::ostream & os, const SparseMatrix<T> & m);
56 
57 
67 template <typename T>
68 class SparseMatrix : public ReferenceCountedObject<SparseMatrix<T>>,
69  public ParallelObject
70 {
71 public:
83  explicit
84  SparseMatrix (const Parallel::Communicator & comm);
85 
90  SparseMatrix (SparseMatrix &&) = default;
91  SparseMatrix (const SparseMatrix &) = default;
92  SparseMatrix & operator= (const SparseMatrix &) = default;
93  SparseMatrix & operator= (SparseMatrix &&) = default;
94  virtual ~SparseMatrix () = default;
95 
100  static std::unique_ptr<SparseMatrix<T>>
101  build(const Parallel::Communicator & comm,
102  const SolverPackage solver_package = libMesh::default_solver_package(),
103  const MatrixBuildType matrix_build_type = MatrixBuildType::AUTOMATIC);
104 
109  virtual bool initialized() const { return _is_initialized; }
110 
117  void attach_dof_map (const DofMap & dof_map);
118 
128 
138  virtual bool need_full_sparsity_pattern() const
139  { return false; }
140 
147 
160  virtual void init (const numeric_index_type m,
161  const numeric_index_type n,
162  const numeric_index_type m_l,
163  const numeric_index_type n_l,
164  const numeric_index_type nnz=30,
165  const numeric_index_type noz=10,
166  const numeric_index_type blocksize=1) = 0;
167 
172  virtual void init (ParallelType type = PARALLEL) = 0;
173 
177  virtual void clear () = 0;
178 
182  virtual void zero () = 0;
183 
190  virtual std::unique_ptr<SparseMatrix<T>> zero_clone () const = 0;
191 
197  virtual std::unique_ptr<SparseMatrix<T>> clone () const = 0;
198 
202  virtual void zero_rows (std::vector<numeric_index_type> & rows, T diag_value = 0.0);
203 
208  virtual void close () = 0;
209 
215  virtual void flush () { close(); }
216 
220  virtual numeric_index_type m () const = 0;
221 
225  virtual numeric_index_type local_m () const { return row_stop() - row_start(); }
226 
230  virtual numeric_index_type local_n () const { return col_stop() - col_start(); }
231 
235  virtual numeric_index_type n () const = 0;
236 
241  virtual numeric_index_type row_start () const = 0;
242 
247  virtual numeric_index_type row_stop () const = 0;
248 
253  virtual numeric_index_type col_start () const = 0;
254 
259  virtual numeric_index_type col_stop () const = 0;
260 
266  virtual void set (const numeric_index_type i,
267  const numeric_index_type j,
268  const T value) = 0;
269 
275  virtual void add (const numeric_index_type i,
276  const numeric_index_type j,
277  const T value) = 0;
278 
283  virtual void add_matrix (const DenseMatrix<T> & dm,
284  const std::vector<numeric_index_type> & rows,
285  const std::vector<numeric_index_type> & cols) = 0;
286 
291  virtual void add_matrix (const DenseMatrix<T> & dm,
292  const std::vector<numeric_index_type> & dof_indices) = 0;
293 
300  virtual void add_block_matrix (const DenseMatrix<T> & dm,
301  const std::vector<numeric_index_type> & brows,
302  const std::vector<numeric_index_type> & bcols);
303 
308  virtual void add_block_matrix (const DenseMatrix<T> & dm,
309  const std::vector<numeric_index_type> & dof_indices)
310  { this->add_block_matrix (dm, dof_indices, dof_indices); }
311 
315  virtual void add (const T a, const SparseMatrix<T> & X) = 0;
316 
320  virtual void matrix_matrix_mult (SparseMatrix<T> & /*X*/, SparseMatrix<T> & /*Y*/, bool /*reuse*/)
321  { libmesh_not_implemented(); }
322 
327  virtual void add_sparse_matrix (const SparseMatrix<T> & /*spm*/,
328  const std::map<numeric_index_type, numeric_index_type> & /*rows*/,
329  const std::map<numeric_index_type, numeric_index_type> & /*cols*/,
330  const T /*scalar*/)
331  { libmesh_not_implemented(); }
332 
339  virtual T operator () (const numeric_index_type i,
340  const numeric_index_type j) const = 0;
341 
350  virtual Real l1_norm () const = 0;
351 
362  virtual Real linfty_norm () const = 0;
363 
367  virtual bool closed() const = 0;
368 
373  virtual std::size_t n_nonzeros() const;
374 
379  void print(std::ostream & os=libMesh::out, const bool sparse=false) const;
380 
408  friend std::ostream & operator << <>(std::ostream & os, const SparseMatrix<T> & m);
409 
414  virtual void print_personal(std::ostream & os=libMesh::out) const = 0;
415 
421  virtual void print_matlab(const std::string & /*name*/ = "") const;
422 
427  virtual void print_petsc_binary(const std::string & filename);
428 
433  virtual void print_petsc_hdf5(const std::string & filename);
434 
435 
447  virtual void read_matlab(const std::string & filename);
448 
453  virtual void read_petsc_binary(const std::string & filename);
454 
459  virtual void read_petsc_hdf5(const std::string & filename);
460 
469  virtual void create_submatrix(SparseMatrix<T> & submatrix,
470  const std::vector<numeric_index_type> & rows,
471  const std::vector<numeric_index_type> & cols) const
472  {
473  this->_get_submatrix(submatrix,
474  rows,
475  cols,
476  false); // false means DO NOT REUSE submatrix
477  }
478 
487  virtual void create_submatrix_nosort(SparseMatrix<T> & /*submatrix*/,
488  const std::vector<numeric_index_type> & /*rows*/,
489  const std::vector<numeric_index_type> & /*cols*/) const
490  {
491  libmesh_not_implemented();
492  }
493 
500  virtual void reinit_submatrix(SparseMatrix<T> & submatrix,
501  const std::vector<numeric_index_type> & rows,
502  const std::vector<numeric_index_type> & cols) const
503  {
504  this->_get_submatrix(submatrix,
505  rows,
506  cols,
507  true); // true means REUSE submatrix
508  }
509 
514  void vector_mult (NumericVector<T> & dest,
515  const NumericVector<T> & arg) const;
516 
521  void vector_mult_add (NumericVector<T> & dest,
522  const NumericVector<T> & arg) const;
523 
527  virtual void get_diagonal (NumericVector<T> & dest) const = 0;
528 
533  virtual void get_transpose (SparseMatrix<T> & dest) const = 0;
534 
542  virtual void get_row(numeric_index_type i,
543  std::vector<numeric_index_type> & indices,
544  std::vector<T> & values) const = 0;
545 
546 protected:
547 
555  virtual void _get_submatrix(SparseMatrix<T> & /*submatrix*/,
556  const std::vector<numeric_index_type> & /*rows*/,
557  const std::vector<numeric_index_type> & /*cols*/,
558  const bool /*reuse_submatrix*/) const
559  {
560  libmesh_not_implemented();
561  }
562 
567  DofMap const * _dof_map;
568 
575 
580 };
581 
582 
583 
584 //-----------------------------------------------------------------------
585 // SparseMatrix inline members
586 
587 // For SGI MIPSpro this implementation must occur after
588 // the full specialization of the print() member.
589 //
590 // It's generally easier to define these friend functions in the header
591 // file.
592 template <typename T>
593 std::ostream & operator << (std::ostream & os, const SparseMatrix<T> & m)
594 {
595  m.print(os);
596  return os;
597 }
598 
599 
600 } // namespace libMesh
601 
602 
603 #endif // LIBMESH_SPARSE_MATRIX_H
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:82
virtual bool initialized() const
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...
virtual std::size_t n_nonzeros() const
virtual void print_petsc_binary(const std::string &filename)
Write the contents of the matrix to a file in PETSc&#39;s binary sparse matrix format.
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...
virtual void read_petsc_hdf5(const std::string &filename)
Read the contents of the matrix from a file in PETSc&#39;s HDF5 sparse matrix format. ...
SparseMatrix(const Parallel::Communicator &comm)
Constructor; initializes the matrix to be empty, without any structure, i.e.
Definition: sparse_matrix.C:53
This helper class can be called on multiple threads to compute the sparsity pattern (or graph) of the...
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...
virtual ~SparseMatrix()=default
virtual numeric_index_type local_m() const
Get the number of rows owned by this process.
SparsityPattern::Build const * _sp
The sparsity pattern associated with this object.
void attach_sparsity_pattern(const SparsityPattern::Build &sp)
Set a pointer to a sparsity pattern to use.
Definition: sparse_matrix.C:73
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
Definition: vector_fe_ex5.C:43
const Parallel::Communicator & comm() const
virtual void matrix_matrix_mult(SparseMatrix< T > &, SparseMatrix< T > &, bool)
Compute Y = A*X for matrix X.
The libMesh namespace provides an interface to certain functionality in the library.
static std::unique_ptr< SparseMatrix< T > > build(const Parallel::Communicator &comm, const SolverPackage solver_package=libMesh::default_solver_package(), const MatrixBuildType matrix_build_type=MatrixBuildType::AUTOMATIC)
Builds a SparseMatrix<T> using the linear solver package specified by solver_package.
virtual numeric_index_type local_n() const
Get the number of columns owned by this process.
virtual numeric_index_type row_stop() const =0
virtual void clear()=0
Restores the SparseMatrix<T> to a pristine state.
virtual void add(const numeric_index_type i, const numeric_index_type j, const T value)=0
Add value to the element (i,j).
virtual void add_sparse_matrix(const SparseMatrix< T > &, const std::map< numeric_index_type, numeric_index_type > &, const std::map< numeric_index_type, numeric_index_type > &, const T)
Add scalar* spm to the rows and cols of this matrix (A): A(rows[i], cols[j]) += scalar * spm(i...
Generic sparse matrix.
Definition: vector_fe_ex5.C:45
SolverPackage default_solver_package()
Definition: libmesh.C:1050
virtual void read_matlab(const std::string &filename)
Read the contents of the matrix from Matlab&#39;s sparse matrix format.
This class handles the numbering of degrees of freedom on a mesh.
Definition: dof_map.h:169
virtual void flush()
For PETSc matrix , this function is similar to close but without shrinking memory.
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.
virtual void zero()=0
Set all entries to 0.
dof_id_type numeric_index_type
Definition: id_types.h:99
bool _is_initialized
Flag indicating whether or not the matrix has been initialized.
virtual std::unique_ptr< SparseMatrix< T > > zero_clone() const =0
virtual numeric_index_type m() const =0
virtual void read_petsc_binary(const std::string &filename)
Read the contents of the matrix from a file in PETSc&#39;s binary sparse matrix format.
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.
virtual numeric_index_type col_stop() const =0
virtual void get_diagonal(NumericVector< T > &dest) const =0
Copies the diagonal part of the matrix into dest.
virtual numeric_index_type col_start() const =0
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...
virtual void create_submatrix_nosort(SparseMatrix< T > &, const std::vector< numeric_index_type > &, const std::vector< numeric_index_type > &) const
Similar to the above function, this function creates a submatrix which is defined by the indices give...
virtual bool closed() const =0
void attach_dof_map(const DofMap &dof_map)
Set a pointer to the DofMap to use.
Definition: sparse_matrix.C:63
DofMap const * _dof_map
The DofMap object associated with this object.
virtual void get_row(numeric_index_type i, std::vector< numeric_index_type > &indices, std::vector< T > &values) const =0
Get a row from the matrix.
virtual bool need_full_sparsity_pattern() const
virtual void close()=0
Calls the SparseMatrix&#39;s internal assembly routines, ensuring that the values are consistent across p...
virtual void update_sparsity_pattern(const SparsityPattern::Graph &)
Updates the matrix sparsity pattern.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
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...
virtual numeric_index_type row_start() const =0
OStreamProxy out
virtual void print_matlab(const std::string &="") const
Print the contents of the matrix in Matlab&#39;s sparse matrix format.
virtual void get_transpose(SparseMatrix< T > &dest) const =0
Copies the transpose of the matrix into dest, which may be *this.
static const bool value
Definition: xdr_io.C:54
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.
virtual Real linfty_norm() const =0
SparseMatrix & operator=(const SparseMatrix &)=default
SolverPackage
Defines an enum for various linear solver packages.
virtual Real l1_norm() const =0
MatrixBuildType
Defines an enum for matrix build types.
virtual std::unique_ptr< SparseMatrix< T > > clone() const =0
Defines a dense matrix for use in Finite Element-type computations.
Definition: dof_map.h:65
virtual T operator()(const numeric_index_type i, const numeric_index_type j) const =0
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...
virtual numeric_index_type n() const =0
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.
virtual void print_petsc_hdf5(const std::string &filename)
Write the contents of the matrix to a file in PETSc&#39;s HDF5 sparse matrix format.
virtual void init(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 nnz=30, const numeric_index_type noz=10, const numeric_index_type blocksize=1)=0
Initialize SparseMatrix with the specified sizes.
ParallelType
Defines an enum for parallel data structure types.