libMesh
Loading...
Searching...
No Matches
sparse_matrix.h
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 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#include "libmesh/enum_solver_package.h"
33
34// C++ includes
35#include <cstddef>
36#include <iomanip>
37#include <vector>
38#include <memory>
39
40namespace libMesh
41{
42
43// forward declarations
44template <typename T> class SparseMatrix;
45template <typename T> class DenseMatrix;
46class DofMap;
47namespace SparsityPattern {
48 class Build;
49 class Graph;
50}
51template <typename T> class NumericVector;
52
53// This template helper function must be declared before it
54// can be defined below.
55template <typename T>
56std::ostream & operator << (std::ostream & os, const SparseMatrix<T> & m);
57
58
68template <typename T>
69class SparseMatrix : public ReferenceCountedObject<SparseMatrix<T>>,
70 public ParallelObject
71{
72public:
84 explicit
86
99 {
100 libmesh_not_implemented();
101 return *this;
102 }
103
109 SparseMatrix (const SparseMatrix &) = default;
111
116 virtual ~SparseMatrix() = default;
117
122 static std::unique_ptr<SparseMatrix<T>>
125 const MatrixBuildType matrix_build_type = MatrixBuildType::AUTOMATIC);
126
128
133 virtual bool initialized() const { return _is_initialized; }
134
141 void attach_dof_map (const DofMap & dof_map);
142
152
162 virtual bool need_full_sparsity_pattern() const
163 { return false; }
164
168 virtual bool require_sparsity_pattern() const { return !this->use_hash_table(); }
169
176
189 virtual void init (const numeric_index_type m,
190 const numeric_index_type n,
191 const numeric_index_type m_l,
192 const numeric_index_type n_l,
193 const numeric_index_type nnz=30,
194 const numeric_index_type noz=10,
195 const numeric_index_type blocksize=1) = 0;
196
201 virtual void init (ParallelType type = PARALLEL) = 0;
202
206 virtual void clear () = 0;
207
211 virtual void zero () = 0;
212
219 virtual std::unique_ptr<SparseMatrix<T>> zero_clone () const = 0;
220
226 virtual std::unique_ptr<SparseMatrix<T>> clone () const = 0;
227
231 virtual void zero_rows (std::vector<numeric_index_type> & rows, T diag_value = 0.0);
232
237 virtual void close () = 0;
238
244 virtual void flush () { close(); }
245
249 virtual numeric_index_type m () const = 0;
250
254 virtual numeric_index_type local_m () const { return row_stop() - row_start(); }
255
259 virtual numeric_index_type local_n () const { return col_stop() - col_start(); }
260
264 virtual numeric_index_type n () const = 0;
265
270 virtual numeric_index_type row_start () const = 0;
271
276 virtual numeric_index_type row_stop () const = 0;
277
282 virtual numeric_index_type col_start () const = 0;
283
288 virtual numeric_index_type col_stop () const = 0;
289
295 virtual void set (const numeric_index_type i,
296 const numeric_index_type j,
297 const T value) = 0;
298
304 virtual void add (const numeric_index_type i,
305 const numeric_index_type j,
306 const T value) = 0;
307
312 virtual void add_matrix (const DenseMatrix<T> & dm,
313 const std::vector<numeric_index_type> & rows,
314 const std::vector<numeric_index_type> & cols) = 0;
315
320 virtual void add_matrix (const DenseMatrix<T> & dm,
321 const std::vector<numeric_index_type> & dof_indices) = 0;
322
329 virtual void add_block_matrix (const DenseMatrix<T> & dm,
330 const std::vector<numeric_index_type> & brows,
331 const std::vector<numeric_index_type> & bcols);
332
337 virtual void add_block_matrix (const DenseMatrix<T> & dm,
338 const std::vector<numeric_index_type> & dof_indices)
339 { this->add_block_matrix (dm, dof_indices, dof_indices); }
340
344 virtual void add (const T a, const SparseMatrix<T> & X) = 0;
345
349 virtual void matrix_matrix_mult (SparseMatrix<T> & /*X*/, SparseMatrix<T> & /*Y*/, bool /*reuse*/)
350 { libmesh_not_implemented(); }
351
356 virtual void add_sparse_matrix (const SparseMatrix<T> & /*spm*/,
357 const std::map<numeric_index_type, numeric_index_type> & /*rows*/,
358 const std::map<numeric_index_type, numeric_index_type> & /*cols*/,
359 const T /*scalar*/)
360 { libmesh_not_implemented(); }
361
369 const numeric_index_type j) const = 0;
370
379 virtual Real l1_norm () const = 0;
380
391 virtual Real linfty_norm () const = 0;
392
396 Real l1_norm_diff (const SparseMatrix<T> & other_mat) const;
397
401 virtual bool closed() const = 0;
402
407 virtual std::size_t n_nonzeros() const;
408
413 void print(std::ostream & os=libMesh::out, const bool sparse=false) const;
414
442 friend std::ostream & operator << <>(std::ostream & os, const SparseMatrix<T> & m);
443
448 virtual void print_personal(std::ostream & os=libMesh::out) const = 0;
449
454 void print(const std::string & filename) const;
455
461 virtual void print_coreform_hdf5(const std::string & filename,
462 const std::string & groupname = "extraction") const;
463
469 virtual void print_matlab(const std::string & /*name*/ = "") const;
470
475 virtual void print_petsc_binary(const std::string & filename) const;
476
481 virtual void print_petsc_hdf5(const std::string & filename) const;
482
487 virtual void read(const std::string & filename);
488
498 virtual void read_coreform_hdf5(const std::string & filename,
499 const std::string & groupname = "extraction");
500
512 virtual void read_matlab(const std::string & filename);
513
518 virtual void read_petsc_binary(const std::string & filename);
519
524 virtual void read_petsc_hdf5(const std::string & filename);
525
534 virtual void create_submatrix(SparseMatrix<T> & submatrix,
535 const std::vector<numeric_index_type> & rows,
536 const std::vector<numeric_index_type> & cols) const
537 {
538 this->_get_submatrix(submatrix,
539 rows,
540 cols,
541 false); // false means DO NOT REUSE submatrix
542 }
543
552 virtual void create_submatrix_nosort(SparseMatrix<T> & /*submatrix*/,
553 const std::vector<numeric_index_type> & /*rows*/,
554 const std::vector<numeric_index_type> & /*cols*/) const
555 {
556 libmesh_not_implemented();
557 }
558
565 virtual void reinit_submatrix(SparseMatrix<T> & submatrix,
566 const std::vector<numeric_index_type> & rows,
567 const std::vector<numeric_index_type> & cols) const
568 {
569 this->_get_submatrix(submatrix,
570 rows,
571 cols,
572 true); // true means REUSE submatrix
573 }
574
579 void vector_mult (NumericVector<T> & dest,
580 const NumericVector<T> & arg) const;
581
587 const NumericVector<T> & arg) const;
588
592 virtual void get_diagonal (NumericVector<T> & dest) const = 0;
593
598 virtual void get_transpose (SparseMatrix<T> & dest) const = 0;
599
608 std::vector<numeric_index_type> & indices,
609 std::vector<T> & values) const = 0;
610
614 virtual void scale(const T scale);
615
619 virtual bool supports_hash_table() const { return false; }
620
627 void use_hash_table(bool use_hash);
628
634 bool use_hash_table() const { return _use_hash_table; }
635
644 virtual void restore_original_nonzero_pattern() { libmesh_not_implemented(); }
645
646protected:
654 virtual void _get_submatrix(SparseMatrix<T> & /*submatrix*/,
655 const std::vector<numeric_index_type> & /*rows*/,
656 const std::vector<numeric_index_type> & /*cols*/,
657 const bool /*reuse_submatrix*/) const
658 {
659 libmesh_not_implemented();
660 }
661
667
674
679
684};
685
686
687
688//-----------------------------------------------------------------------
689// SparseMatrix inline members
690template <typename T>
691void
693{
694 libmesh_error_msg_if(use_hash && !this->supports_hash_table(),
695 "This matrix class does not support hash table assembly");
696 this->_use_hash_table = use_hash;
697}
698
699// For SGI MIPSpro this implementation must occur after
700// the full specialization of the print() member.
701//
702// It's generally easier to define these friend functions in the header
703// file.
704template <typename T>
705std::ostream & operator << (std::ostream & os, const SparseMatrix<T> & m)
706{
707 m.print(os);
708 return os;
709}
710
711template <typename T>
712auto
714{
715 return mat.l1_norm();
716}
717
718template <typename T>
719auto
721{
722 return mat1.l1_norm_diff(mat2);
723}
724
725} // namespace libMesh
726
727
728#endif // LIBMESH_SPARSE_MATRIX_H
Defines a dense matrix for use in Finite Element-type computations.
This class handles the numbering of degrees of freedom on a mesh.
Definition dof_map.h:181
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
An object whose state is distributed along a set of processors.
const Parallel::Communicator & comm() const
This class implements reference counting.
Generic sparse matrix.
virtual Real l1_norm() const =0
virtual void update_sparsity_pattern(const SparsityPattern::Graph &)
Updates the matrix sparsity pattern.
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.
virtual void close()=0
Calls the SparseMatrix's internal assembly routines, ensuring that the values are consistent across p...
bool use_hash_table() const
Real l1_norm_diff(const SparseMatrix< T > &other_mat) const
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.
virtual numeric_index_type col_stop() const =0
virtual void print_petsc_hdf5(const std::string &filename) const
Write the contents of the matrix to a file in PETSc's HDF5 sparse matrix format.
bool _use_hash_table
Flag indicating whether the matrix is assembled using a hash table.
virtual void read_coreform_hdf5(const std::string &filename, const std::string &groupname="extraction")
Read the contents of the matrix from a file, with the HDF5 sparse matrix format used by CoreForm,...
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 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 bool require_sparsity_pattern() const
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 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.
void attach_dof_map(const DofMap &dof_map)
Set a pointer to the DofMap to use.
virtual void read_matlab(const std::string &filename)
Read the contents of the matrix from the Matlab-script sparse matrix format used by PETSc.
virtual void scale(const T scale)
Scales all elements of this matrix by scale.
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 void read_petsc_binary(const std::string &filename)
Read the contents of the matrix from a file in PETSc's binary sparse matrix format.
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 std::unique_ptr< SparseMatrix< T > > clone() const =0
virtual T operator()(const numeric_index_type i, const numeric_index_type j) const =0
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 initialized() const
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 numeric_index_type n() const =0
virtual SparseMatrix< T > & operator=(const SparseMatrix< T > &)
This looks like a copy assignment operator, but note that, unlike normal copy assignment operators,...
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 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,...
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 void add_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &dof_indices)=0
Same as add_matrix, but assumes the row and column maps are the same.
virtual void clear()=0
Restores the SparseMatrix<T> to a pristine state.
DofMap const * _dof_map
The DofMap object associated with this object.
virtual void read_petsc_hdf5(const std::string &filename)
Read the contents of the matrix from a file in PETSc's HDF5 sparse matrix format.
virtual numeric_index_type row_stop() const =0
virtual numeric_index_type m() const =0
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 void zero()=0
Set all entries to 0.
bool _is_initialized
Flag indicating whether or not the matrix has been initialized.
virtual void print_coreform_hdf5(const std::string &filename, const std::string &groupname="extraction") const
Print the contents of the matrix to a file, with the HDF5 sparse matrix format used by CoreForm,...
SparseMatrix(SparseMatrix &&)=default
These 3 special functions can be defaulted for this class, as it does not manage any memory itself.
virtual numeric_index_type local_m() const
Get the number of rows owned by this process.
virtual bool supports_hash_table() const
virtual void restore_original_nonzero_pattern()
Reset the memory storage of the matrix.
virtual numeric_index_type col_start() const =0
virtual void print_petsc_binary(const std::string &filename) const
Write the contents of the matrix to a file in PETSc's binary sparse matrix format.
virtual void init(ParallelType type=PARALLEL)=0
Initialize this matrix using the sparsity structure computed by dof_map.
virtual void get_transpose(SparseMatrix< T > &dest) const =0
Copies the transpose of the matrix into dest, which may be *this.
virtual std::unique_ptr< SparseMatrix< T > > zero_clone() const =0
virtual std::size_t n_nonzeros() const
virtual numeric_index_type local_n() const
Get the number of columns owned by this process.
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(const T a, const SparseMatrix< T > &X)=0
Compute for scalar a, matrix X.
SparsityPattern::Build const * _sp
The sparsity pattern associated with this object.
virtual bool need_full_sparsity_pattern() const
virtual void matrix_matrix_mult(SparseMatrix< T > &, SparseMatrix< T > &, bool)
Compute Y = A*X for matrix X.
virtual void read(const std::string &filename)
Read the contents of the matrix from a file, with the file format inferred from the extension of file...
virtual void flush()
For PETSc matrix , this function is similar to close but without shrinking memory.
virtual ~SparseMatrix()=default
While this class doesn't manage any memory, the derived class might and users may be deleting through...
virtual void set(const numeric_index_type i, const numeric_index_type j, const T value)=0
Set the element (i,j) to value.
virtual void get_diagonal(NumericVector< T > &dest) const =0
Copies the diagonal part of the matrix into dest.
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 numeric_index_type row_start() const =0
void use_hash_table(bool use_hash)
Sets whether to use hash table assembly.
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...
void attach_sparsity_pattern(const SparsityPattern::Build &sp)
Set a pointer to a sparsity pattern to use.
virtual bool closed() const =0
virtual void print_matlab(const std::string &="") const
Print the contents of the matrix in Matlab's sparse matrix format.
virtual Real linfty_norm() const =0
SparseMatrix(const SparseMatrix &)=default
virtual SolverPackage solver_package()=0
This helper class can be called on multiple threads to compute the sparsity pattern (or graph) of the...
The libMesh namespace provides an interface to certain functionality in the library.
auto l1_norm(const NumericVector< T > &vec)
ParallelType
Defines an enum for parallel data structure types.
SolverPackage default_solver_package()
Definition libmesh.C:1064
MatrixBuildType
Defines an enum for matrix build types.
std::ostream & operator<<(std::ostream &os, const OrderWrapper &order)
Overload stream operators.
Definition fe_type.h:182
OStreamProxy out
dof_id_type numeric_index_type
Definition id_types.h:99
SolverPackage
Defines an enum for various linear solver packages.
auto l1_norm_diff(const NumericVector< T > &vec1, const NumericVector< T > &vec2)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
static const bool value
Definition xdr_io.C:55