Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
vulpes::la::SparseMatrix Class Reference

#include <sparse_matrix.hpp>

Public Types

enum  Type {
  Plain , IntelMKL , AcceleratedSIMD , Vectorized ,
  Block
}

Public Member Functions

template<typename DofHandler>
 SparseMatrix (const DofHandler &dof_handler, Type type=Plain, PetscInt block_size=1, PetscInt aij_block_size=1)
 Construct a SparseMatrix based on a DofHandler's sparsity pattern.
 SparseMatrix (const SparseMatrix &other, MatDuplicateOption duplicate_options)
 Construct a SparseMatrix by duplicating another SparseMatrix.
 SparseMatrix (PetscInt loc_size, PetscInt glob_size, Type type=Plain)
 Construct a SparseMatrix with given local and global sizes.
SparseMatrix & operator= (const SparseMatrix &other)=delete
 SparseMatrix (const SparseMatrix &)=delete
 SparseMatrix (SparseMatrix &&)=delete
SparseMatrix & operator= (SparseMatrix &&)=delete
template<typename DofHandler>
void init (const DofHandler &dof_handler, Type type=Plain, PetscInt block_size=1, PetscInt aij_block_size=1)
 Initialize the SparseMatrix based on a DofHandler's sparsity pattern.
void compress ()
 Finalize the assembly of the matrix by doing intracommunication.
void zero ()
 Set all entries to zero, keeping the nonzero pattern.
void print () const
 Print the matrix to standard output.
void save (const std::string &filename, const std::string &matname="", PetscViewerFormat format=PETSC_VIEWER_ASCII_MATLAB) const
 Save the matrix to a file.
void saveBinary (const std::string &filename, const std::string &matname="") const
 Save the matrix to a file in binary format.
std::array< PetscInt, 2 > getSize () const
 Get the size of the matrix as an array [rows, cols].
PetscInt getBlockSize () const
 Get the block size of the matrix.
void copy (const la::SparseMatrix &src, MatStructure str=DIFFERENT_NONZERO_PATTERN)
 Copy the contents of another SparseMatrix into this one.
void insertValue (PetscScalar value, PetscInt i, PetscInt j, InsertMode mode=ADD_VALUES)
 Insert a single value into the matrix.
void insertValues (const PetscScalar *loc_mat, const PetscInt *idxs_row, const PetscInt *idxs_col, PetscInt n_loc, DataLayout orientation=ColumnMajor, InsertMode mode=ADD_VALUES)
 Insert a sub matrix into the matrix.
void insertValues (const Eigen::Ref< const Eigen::MatrixXd > &loc_mat, const datastructure::CrsMap< PetscInt, PetscInt >::RowView< false > &idxs_row_col, DataLayout orientation=ColumnMajor, InsertMode mode=ADD_VALUES)
 Insert a sub matrix into the matrix.
void insertValues (const Eigen::Ref< const Eigen::MatrixXd > &loc_mat, const datastructure::CrsMap< PetscInt, PetscInt >::RowView< false > &idxs_row, const datastructure::CrsMap< PetscInt, PetscInt >::RowView< false > &idxs_col, DataLayout orientation=ColumnMajor, InsertMode mode=ADD_VALUES)
 Insert a sub matrix into the matrix.
void insertValuesBlocked (const PetscScalar *loc_mat, const PetscInt *idxs_row, const PetscInt *idxs_col, PetscInt n_loc, DataLayout orientation=ColumnMajor, InsertMode mode=ADD_VALUES)
 Insert a blocked sub matrix into the matrix.
void insertValuesBlocked (const Eigen::Ref< const Eigen::MatrixXd > &loc_mat, const datastructure::CrsMap< PetscInt, PetscInt >::RowView< false > &idxs_row_col, DataLayout orientation=ColumnMajor, InsertMode mode=ADD_VALUES)
 Insert a blocked sub matrix into the matrix.
void insertValuesBlocked (const Eigen::Ref< const Eigen::MatrixXd > &loc_mat, const datastructure::CrsMap< PetscInt, PetscInt >::RowView< false > &idxs_row, const datastructure::CrsMap< PetscInt, PetscInt >::RowView< false > &idxs_col, DataLayout orientation=ColumnMajor, InsertMode mode=ADD_VALUES)
 Insert a blocked sub matrix into the matrix.
Eigen::MatrixXd toEigenDense () const
const auto & getPetscMatrix () const
 Get the underlying PETSc matrix.
auto & getPetscMatrix ()
 Get the underlying PETSc matrix (non-const).
std::pair< PetscInt, PetscInt > getMatrixSizes () const
 Get the local and global sizes of the matrix.
PetscReal norm (NormType normType=NORM_FROBENIUS) const
 Get the norm of the matrix.
void vmult (const Vector &in, Vector &out) const
void vmultT (const Vector &in, Vector &out) const
void vmultAdd (const Vector &in_mult, const Vector &in_add, Vector &out) const
void vmultTAdd (const Vector &in_mult, const Vector &in_add, Vector &out) const
void matMult (const SparseMatrix &B, SparseMatrix &C, MatReuse reuse=MAT_INITIAL_MATRIX, PetscReal fill=PETSC_DETERMINE) const
void matMultTranspose (const SparseMatrix &B, SparseMatrix &C, MatReuse reuse=MAT_INITIAL_MATRIX, PetscReal fill=PETSC_DETERMINE) const
void transposeMatMult (const SparseMatrix &B, SparseMatrix &C, MatReuse reuse=MAT_INITIAL_MATRIX, PetscReal fill=PETSC_DETERMINE) const
void addScaledMatrix (const SparseMatrix &other, PetscScalar scale, MatStructure str=UNKNOWN_NONZERO_PATTERN)
void scale (PetscScalar value)
 Multiply all entries by a scalar, in place.
void pinDof (PetscInt dof_idx, PetscScalar value=1.0)
 Pin a degree of freedom to enforce a constraint This performs symmetric elimination: sets row and column of the specified DOF to zero, except the diagonal which is set to 1.0. This removes a DOF from the system and is useful for enforcing Dirichlet BCs or removing null space (e.g., constant modes in periodic problems). The corresponding RHS entry should be set to the desired value.
void setNearNullSpace (const Vector &v)
 Attach a near null space to the matrix, used by algebraic multigrid preconditioners (e.g. GAMG) to build the coarse spaces.
 ~SparseMatrix ()

Static Public Member Functions

static bool compareSparseMatrices (const SparseMatrix &mat1, const SparseMatrix &mat2, double tolerance=1e-12)
 Static function that compares two sparse matrices for equality within a given tolerance.

Protected Member Functions

 SparseMatrix (Mat existing_matrix)
void createMatrix (Type type)
void setMatrixSizes (const PetscInt n_loc, const PetscInt n_glob)
template<typename DofHandler>
void computePreallocationData (const DofHandler &dof_handler, const PetscInt dof_begin, const PetscInt dof_end, std::vector< PetscInt > &dnn, std::vector< PetscInt > &onn)
void allocateMatrix (Type type, const std::vector< PetscInt > &dnn, const std::vector< PetscInt > &onn, PetscInt block_size)

Protected Attributes

Mat m_petsc_matrix

Member Enumeration Documentation

◆ Type

Enumerator
Plain 
IntelMKL 
AcceleratedSIMD 
Vectorized 
Block 

Constructor & Destructor Documentation

◆ SparseMatrix() [1/6]

template<typename DofHandler>
vulpes::la::SparseMatrix::SparseMatrix ( const DofHandler & dof_handler,
Type type = Plain,
PetscInt block_size = 1,
PetscInt aij_block_size = 1 )
inlineexplicit

Construct a SparseMatrix based on a DofHandler's sparsity pattern.

Parameters
dof_handlerThe DofHandler providing the sparsity pattern
typeThe type of the matrix (Plain, Block, etc.)
block_sizeThe block size for Block type matrices
aij_block_sizeBlock size of the rows and columns of a non-Block matrix (e.g. the dofs of a DG cell): the storage stays AIJ, the blocks are used by GAMG (aggregation of whole blocks) and PBJACOBI

◆ SparseMatrix() [2/6]

vulpes::la::SparseMatrix::SparseMatrix ( const SparseMatrix & other,
MatDuplicateOption duplicate_options )

Construct a SparseMatrix by duplicating another SparseMatrix.

Parameters
otherThe SparseMatrix to duplicate
duplicate_optionsCopy options (sparsity pattern, values, etc.)

◆ SparseMatrix() [3/6]

vulpes::la::SparseMatrix::SparseMatrix ( PetscInt loc_size,
PetscInt glob_size,
Type type = Plain )

Construct a SparseMatrix with given local and global sizes.

Parameters
loc_sizeThe local number of rows
glob_sizeThe global number of rows
typeThe type of the matrix (Plain, Block, etc.)

◆ SparseMatrix() [4/6]

vulpes::la::SparseMatrix::SparseMatrix ( const SparseMatrix & )
delete

◆ SparseMatrix() [5/6]

vulpes::la::SparseMatrix::SparseMatrix ( SparseMatrix && )
delete

◆ ~SparseMatrix()

vulpes::la::SparseMatrix::~SparseMatrix ( )

◆ SparseMatrix() [6/6]

vulpes::la::SparseMatrix::SparseMatrix ( Mat existing_matrix)
inlineexplicitprotected

Member Function Documentation

◆ addScaledMatrix()

void vulpes::la::SparseMatrix::addScaledMatrix ( const SparseMatrix & other,
PetscScalar scale,
MatStructure str = UNKNOWN_NONZERO_PATTERN )

◆ allocateMatrix()

void vulpes::la::SparseMatrix::allocateMatrix ( Type type,
const std::vector< PetscInt > & dnn,
const std::vector< PetscInt > & onn,
PetscInt block_size )
protected

◆ compareSparseMatrices()

bool vulpes::la::SparseMatrix::compareSparseMatrices ( const SparseMatrix & mat1,
const SparseMatrix & mat2,
double tolerance = 1e-12 )
static

Static function that compares two sparse matrices for equality within a given tolerance.

Parameters
mat1First matrix
mat2Second matrix
toleranceTolerance for comparison
Returns
True if matrices are equal within the given tolerance, false otherwise

◆ compress()

void vulpes::la::SparseMatrix::compress ( )

Finalize the assembly of the matrix by doing intracommunication.

◆ computePreallocationData()

template<typename DofHandler>
void vulpes::la::SparseMatrix::computePreallocationData ( const DofHandler & dof_handler,
const PetscInt dof_begin,
const PetscInt dof_end,
std::vector< PetscInt > & dnn,
std::vector< PetscInt > & onn )
inlineprotected

◆ copy()

void vulpes::la::SparseMatrix::copy ( const la::SparseMatrix & src,
MatStructure str = DIFFERENT_NONZERO_PATTERN )

Copy the contents of another SparseMatrix into this one.

◆ createMatrix()

void vulpes::la::SparseMatrix::createMatrix ( Type type)
protected

◆ getBlockSize()

PetscInt vulpes::la::SparseMatrix::getBlockSize ( ) const

Get the block size of the matrix.

◆ getMatrixSizes()

std::pair< PetscInt, PetscInt > vulpes::la::SparseMatrix::getMatrixSizes ( ) const

Get the local and global sizes of the matrix.

◆ getPetscMatrix() [1/2]

auto & vulpes::la::SparseMatrix::getPetscMatrix ( )
inline

Get the underlying PETSc matrix (non-const).

◆ getPetscMatrix() [2/2]

const auto & vulpes::la::SparseMatrix::getPetscMatrix ( ) const
inline

Get the underlying PETSc matrix.

◆ getSize()

std::array< PetscInt, 2 > vulpes::la::SparseMatrix::getSize ( ) const

Get the size of the matrix as an array [rows, cols].

◆ init()

template<typename DofHandler>
void vulpes::la::SparseMatrix::init ( const DofHandler & dof_handler,
Type type = Plain,
PetscInt block_size = 1,
PetscInt aij_block_size = 1 )
inline

Initialize the SparseMatrix based on a DofHandler's sparsity pattern.

Parameters
dof_handlerThe DofHandler providing the sparsity pattern
typeThe type of the matrix (Plain, Block, etc.)
block_sizeThe block size for Block type matrices
aij_block_sizeBlock size of the rows and columns of a non-Block matrix, see the constructor

◆ insertValue()

void vulpes::la::SparseMatrix::insertValue ( PetscScalar value,
PetscInt i,
PetscInt j,
InsertMode mode = ADD_VALUES )

Insert a single value into the matrix.

◆ insertValues() [1/3]

void vulpes::la::SparseMatrix::insertValues ( const Eigen::Ref< const Eigen::MatrixXd > & loc_mat,
const datastructure::CrsMap< PetscInt, PetscInt >::RowView< false > & idxs_row,
const datastructure::CrsMap< PetscInt, PetscInt >::RowView< false > & idxs_col,
DataLayout orientation = ColumnMajor,
InsertMode mode = ADD_VALUES )

Insert a sub matrix into the matrix.

Parameters
loc_matEigen reference to the local matrix values
idxs_rowRow indices
idxs_colColumn indices
orientationData layout of the local matrix (ColumnMajor or RowMajor)
modeInsertion mode (ADD_VALUES or INSERT_VALUES)

◆ insertValues() [2/3]

void vulpes::la::SparseMatrix::insertValues ( const Eigen::Ref< const Eigen::MatrixXd > & loc_mat,
const datastructure::CrsMap< PetscInt, PetscInt >::RowView< false > & idxs_row_col,
DataLayout orientation = ColumnMajor,
InsertMode mode = ADD_VALUES )

Insert a sub matrix into the matrix.

Parameters
loc_matEigen reference to the local matrix values
idxs_row_colRow and column indices (same for both)
orientationData layout of the local matrix (ColumnMajor or RowMajor)
modeInsertion mode (ADD_VALUES or INSERT_VALUES)

◆ insertValues() [3/3]

void vulpes::la::SparseMatrix::insertValues ( const PetscScalar * loc_mat,
const PetscInt * idxs_row,
const PetscInt * idxs_col,
PetscInt n_loc,
DataLayout orientation = ColumnMajor,
InsertMode mode = ADD_VALUES )

Insert a sub matrix into the matrix.

Parameters
loc_matPointer to the local matrix values
idxs_rowPointer to the row indices
idxs_colPointer to the column indices
n_locNumber of local rows/columns
orientationData layout of the local matrix (ColumnMajor or RowMajor)
modeInsertion mode (ADD_VALUES or INSERT_VALUES)

◆ insertValuesBlocked() [1/3]

void vulpes::la::SparseMatrix::insertValuesBlocked ( const Eigen::Ref< const Eigen::MatrixXd > & loc_mat,
const datastructure::CrsMap< PetscInt, PetscInt >::RowView< false > & idxs_row,
const datastructure::CrsMap< PetscInt, PetscInt >::RowView< false > & idxs_col,
DataLayout orientation = ColumnMajor,
InsertMode mode = ADD_VALUES )

Insert a blocked sub matrix into the matrix.

Parameters
loc_matEigen reference to the local matrix values (it must have size [block_size * n_loc, block_size * n_loc])
idxs_rowRow indices
idxs_colColumn indices
orientationData layout of the local matrix (ColumnMajor or RowMajor)
modeInsertion mode (ADD_VALUES or INSERT_VALUES)

◆ insertValuesBlocked() [2/3]

void vulpes::la::SparseMatrix::insertValuesBlocked ( const Eigen::Ref< const Eigen::MatrixXd > & loc_mat,
const datastructure::CrsMap< PetscInt, PetscInt >::RowView< false > & idxs_row_col,
DataLayout orientation = ColumnMajor,
InsertMode mode = ADD_VALUES )

Insert a blocked sub matrix into the matrix.

Parameters
loc_matEigen reference to the local matrix values (it must have size [block_size * n_loc, block_size * n_loc])
idxs_row_colRow and column indices (same for both)
orientationData layout of the local matrix (ColumnMajor or RowMajor)
modeInsertion mode (ADD_VALUES or INSERT_VALUES)

◆ insertValuesBlocked() [3/3]

void vulpes::la::SparseMatrix::insertValuesBlocked ( const PetscScalar * loc_mat,
const PetscInt * idxs_row,
const PetscInt * idxs_col,
PetscInt n_loc,
DataLayout orientation = ColumnMajor,
InsertMode mode = ADD_VALUES )

Insert a blocked sub matrix into the matrix.

Parameters
loc_matPointer to the local matrix values (it must have size [block_size * n_loc, block_size * n_loc])
idxs_rowPointer to the row indices
idxs_colPointer to the column indices
n_locNumber of local rows/columns (in blocks)
orientationData layout of the local matrix (ColumnMajor or RowMajor)
modeInsertion mode (ADD_VALUES or INSERT_VALUES)

◆ matMult()

void vulpes::la::SparseMatrix::matMult ( const SparseMatrix & B,
SparseMatrix & C,
MatReuse reuse = MAT_INITIAL_MATRIX,
PetscReal fill = PETSC_DETERMINE ) const

◆ matMultTranspose()

void vulpes::la::SparseMatrix::matMultTranspose ( const SparseMatrix & B,
SparseMatrix & C,
MatReuse reuse = MAT_INITIAL_MATRIX,
PetscReal fill = PETSC_DETERMINE ) const

◆ norm()

PetscReal vulpes::la::SparseMatrix::norm ( NormType normType = NORM_FROBENIUS) const

Get the norm of the matrix.

◆ operator=() [1/2]

SparseMatrix & vulpes::la::SparseMatrix::operator= ( const SparseMatrix & other)
delete

◆ operator=() [2/2]

SparseMatrix & vulpes::la::SparseMatrix::operator= ( SparseMatrix && )
delete

◆ pinDof()

void vulpes::la::SparseMatrix::pinDof ( PetscInt dof_idx,
PetscScalar value = 1.0 )

Pin a degree of freedom to enforce a constraint This performs symmetric elimination: sets row and column of the specified DOF to zero, except the diagonal which is set to 1.0. This removes a DOF from the system and is useful for enforcing Dirichlet BCs or removing null space (e.g., constant modes in periodic problems). The corresponding RHS entry should be set to the desired value.

Note
Before calling this method, the matrix object must be in compressed state, e.g. by means of SparseMatrix::compress().
Parameters
dof_idxThe global index of the DOF to pin
valueThe diagonal value to set (default 1.0)

◆ print()

void vulpes::la::SparseMatrix::print ( ) const

Print the matrix to standard output.

◆ save()

void vulpes::la::SparseMatrix::save ( const std::string & filename,
const std::string & matname = "",
PetscViewerFormat format = PETSC_VIEWER_ASCII_MATLAB ) const

Save the matrix to a file.

Parameters
filenameThe name of the file
matnameThe name of the matrix inside the file
formatThe format to use for saving

◆ saveBinary()

void vulpes::la::SparseMatrix::saveBinary ( const std::string & filename,
const std::string & matname = "" ) const

Save the matrix to a file in binary format.

Parameters
filenameThe name of the file
matnameThe name of the matrix inside the file

◆ scale()

void vulpes::la::SparseMatrix::scale ( PetscScalar value)

Multiply all entries by a scalar, in place.

◆ setMatrixSizes()

void vulpes::la::SparseMatrix::setMatrixSizes ( const PetscInt n_loc,
const PetscInt n_glob )
protected

◆ setNearNullSpace()

void vulpes::la::SparseMatrix::setNearNullSpace ( const Vector & v)

Attach a near null space to the matrix, used by algebraic multigrid preconditioners (e.g. GAMG) to build the coarse spaces.

Parameters
vVector spanning the near null space (e.g. the coefficients of the constant function for the Laplacian); it is copied and normalized
Note
Call it before LinearSolver::setOperators()

◆ toEigenDense()

Eigen::MatrixXd vulpes::la::SparseMatrix::toEigenDense ( ) const

Convert the sparse matrix to a dense Eigen matrix

Note
In parallel only the rows owned by this rank are filled.

◆ transposeMatMult()

void vulpes::la::SparseMatrix::transposeMatMult ( const SparseMatrix & B,
SparseMatrix & C,
MatReuse reuse = MAT_INITIAL_MATRIX,
PetscReal fill = PETSC_DETERMINE ) const

◆ vmult()

void vulpes::la::SparseMatrix::vmult ( const Vector & in,
Vector & out ) const

◆ vmultAdd()

void vulpes::la::SparseMatrix::vmultAdd ( const Vector & in_mult,
const Vector & in_add,
Vector & out ) const

◆ vmultT()

void vulpes::la::SparseMatrix::vmultT ( const Vector & in,
Vector & out ) const

◆ vmultTAdd()

void vulpes::la::SparseMatrix::vmultTAdd ( const Vector & in_mult,
const Vector & in_add,
Vector & out ) const

◆ zero()

void vulpes::la::SparseMatrix::zero ( )

Set all entries to zero, keeping the nonzero pattern.

Member Data Documentation

◆ m_petsc_matrix

Mat vulpes::la::SparseMatrix::m_petsc_matrix
protected

The documentation for this class was generated from the following files: