20 #ifndef LIBMESH_SPARSE_MATRIX_H 21 #define LIBMESH_SPARSE_MATRIX_H 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" 31 #include "libmesh/enum_matrix_build_type.h" 43 template <
typename T>
class SparseMatrix;
44 template <
typename T>
class DenseMatrix;
46 namespace SparsityPattern {
50 template <
typename T>
class NumericVector;
55 std::ostream & operator << (std::ostream & os, const SparseMatrix<T> & m);
68 class SparseMatrix :
public ReferenceCountedObject<SparseMatrix<T>>,
100 static std::unique_ptr<SparseMatrix<T>>
101 build(
const Parallel::Communicator &
comm,
177 virtual void clear () = 0;
182 virtual void zero () = 0;
190 virtual std::unique_ptr<SparseMatrix<T>>
zero_clone ()
const = 0;
197 virtual std::unique_ptr<SparseMatrix<T>>
clone ()
const = 0;
202 virtual void zero_rows (std::vector<numeric_index_type> & rows, T diag_value = 0.0);
208 virtual void close () = 0;
284 const std::vector<numeric_index_type> & rows,
285 const std::vector<numeric_index_type> & cols) = 0;
292 const std::vector<numeric_index_type> & dof_indices) = 0;
301 const std::vector<numeric_index_type> & brows,
302 const std::vector<numeric_index_type> & bcols);
309 const std::vector<numeric_index_type> & dof_indices)
321 { libmesh_not_implemented(); }
328 const std::map<numeric_index_type, numeric_index_type> & ,
329 const std::map<numeric_index_type, numeric_index_type> & ,
331 { libmesh_not_implemented(); }
367 virtual bool closed()
const = 0;
408 friend std::ostream & operator << <>(std::ostream & os,
const SparseMatrix<T> &
m);
421 virtual void print_matlab(
const std::string & =
"")
const;
447 virtual void read_matlab(
const std::string & filename);
470 const std::vector<numeric_index_type> & rows,
471 const std::vector<numeric_index_type> & cols)
const 488 const std::vector<numeric_index_type> & ,
489 const std::vector<numeric_index_type> & )
const 491 libmesh_not_implemented();
501 const std::vector<numeric_index_type> & rows,
502 const std::vector<numeric_index_type> & cols)
const 543 std::vector<numeric_index_type> & indices,
544 std::vector<T> & values)
const = 0;
556 const std::vector<numeric_index_type> & ,
557 const std::vector<numeric_index_type> & ,
560 libmesh_not_implemented();
592 template <
typename T>
593 std::ostream & operator << (std::ostream & os, const SparseMatrix<T> & m)
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.
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'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's HDF5 sparse matrix format. ...
SparseMatrix(const Parallel::Communicator &comm)
Constructor; initializes the matrix to be empty, without any structure, i.e.
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.
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
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...
SolverPackage default_solver_package()
virtual void read_matlab(const std::string &filename)
Read the contents of the matrix from Matlab's sparse matrix format.
This class handles the numbering of degrees of freedom on a mesh.
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
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'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.
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'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
virtual void print_matlab(const std::string &="") const
Print the contents of the matrix in Matlab'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.
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.
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'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.