26#ifndef VULPES_LA_SPARSE_MATRIX_HPP_
27#define VULPES_LA_SPARSE_MATRIX_HPP_
53 static_assert(std::is_same_v<double, PetscScalar>);
68 template <
typename DofHandler>
70 const DofHandler& dof_handler,
72 PetscInt block_size = 1,
73 PetscInt aij_block_size = 1) {
74 static_assert(std::is_same_v<typename DofHandler::index_t, PetscInt>);
75 init(dof_handler, type, block_size, aij_block_size);
102 template <
typename DofHandler>
104 const DofHandler& dof_handler,
106 PetscInt block_size = 1,
107 PetscInt aij_block_size = 1) {
109 block_size == 1 || type ==
Block, PETSC_COMM_WORLD,
110 PETSC_ERR_ARG_INCOMP,
111 "block size != 1 available only with block type matrices");
114 const PetscInt n_glob = dof_handler.ndofsGlobal();
115 PetscInt dof_begin = 0;
116 PetscInt dof_end = n_glob;
117 if constexpr (DofHandler::parallel) {
118 dof_begin = dof_handler.dofOffset()[
mpi::rank()];
119 dof_end = dof_handler.dofOffset()[
mpi::rank() + 1];
121 const PetscInt n_loc = dof_end - dof_begin;
126 if (aij_block_size != 1) {
130 aij_block_size < 1 || type ==
Block || n_loc % aij_block_size != 0;
131 MPI_Allreduce(MPI_IN_PLACE, &bad, 1, MPI_INT, MPI_LOR, PETSC_COMM_WORLD);
133 pcout <<
"ERROR: aij_block_size must be positive, divide the local "
134 <<
"number of rows on every rank and is not available with "
135 <<
"Block type matrices" << std::endl;
136 MPI_Abort(PETSC_COMM_WORLD, -1);
141 MatSetBlockSizes(
m_petsc_matrix, aij_block_size, aij_block_size));
145 if (DofHandler::parallel && (!dof_handler.hasRenumbering())) {
147 pcout <<
"WARNING: SparseMatrix without renumberMpiSubdomainWise() is "
148 "not preallocated, assembly will be slow"
152 std::vector<PetscInt> dnn(n_loc, 0), onn(n_loc, 0);
171 const std::string& filename,
172 const std::string& matname =
"",
173 PetscViewerFormat format = PETSC_VIEWER_ASCII_MATLAB)
const;
179 const std::string& filename,
const std::string& matname =
"")
const;
182 std::array<PetscInt, 2>
getSize()
const;
190 MatStructure str = DIFFERENT_NONZERO_PATTERN);
194 PetscScalar value, PetscInt i, PetscInt j, InsertMode mode = ADD_VALUES);
208 const PetscScalar* loc_mat,
209 const PetscInt* idxs_row,
210 const PetscInt* idxs_col,
213 InsertMode mode = ADD_VALUES);
225 const Eigen::Ref<const Eigen::MatrixXd>& loc_mat,
229 InsertMode mode = ADD_VALUES);
242 const Eigen::Ref<const Eigen::MatrixXd>& loc_mat,
246 InsertMode mode = ADD_VALUES);
261 const PetscScalar* loc_mat,
262 const PetscInt* idxs_row,
263 const PetscInt* idxs_col,
266 InsertMode mode = ADD_VALUES);
279 const Eigen::Ref<const Eigen::MatrixXd>& loc_mat,
283 InsertMode mode = ADD_VALUES);
297 const Eigen::Ref<const Eigen::MatrixXd>& loc_mat,
301 InsertMode mode = ADD_VALUES);
314 PetscReal
norm(NormType normType = NORM_FROBENIUS)
const;
327 MatReuse reuse = MAT_INITIAL_MATRIX,
328 PetscReal fill = PETSC_DETERMINE)
const;
332 MatReuse reuse = MAT_INITIAL_MATRIX,
333 PetscReal fill = PETSC_DETERMINE)
const;
337 MatReuse reuse = MAT_INITIAL_MATRIX,
338 PetscReal fill = PETSC_DETERMINE)
const;
344 MatStructure str = UNKNOWN_NONZERO_PATTERN);
347 void scale(PetscScalar value);
359 void pinDof(PetscInt dof_idx, PetscScalar value = 1.0);
378 double tolerance = 1e-12);
391 template <
typename DofHandler>
393 const DofHandler& dof_handler,
394 const PetscInt dof_begin,
395 const PetscInt dof_end,
396 std::vector<PetscInt>& dnn,
397 std::vector<PetscInt>& onn) {
398 for (
auto dof_id = dof_begin; dof_id < dof_end; ++dof_id) {
400 auto loc_dof_id = dof_id;
401 if (DofHandler::parallel) {
402 loc_dof_id = dof_handler.getCurrToRefNumbering().at(dof_id);
404 const auto sp_row = dof_handler.getSparsityPattern()[loc_dof_id];
405 for (
const auto glob_dof_nz : sp_row) {
406 if (dof_begin <= glob_dof_nz && glob_dof_nz < dof_end) {
407 dnn[dof_id - dof_begin]++;
409 onn[dof_id - dof_begin]++;
417 const std::vector<PetscInt>& dnn,
418 const std::vector<PetscInt>& onn,
419 PetscInt block_size);
Definition crs_map.hpp:82
Definition sparse_matrix.hpp:52
void transposeMatMult(const SparseMatrix &B, SparseMatrix &C, MatReuse reuse=MAT_INITIAL_MATRIX, PetscReal fill=PETSC_DETERMINE) const
Definition sparse_matrix.cpp:435
void scale(PetscScalar value)
Multiply all entries by a scalar, in place.
Definition sparse_matrix.cpp:454
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.
Definition sparse_matrix.cpp:480
void allocateMatrix(Type type, const std::vector< PetscInt > &dnn, const std::vector< PetscInt > &onn, PetscInt block_size)
Definition sparse_matrix.cpp:80
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.
Definition sparse_matrix.cpp:294
SparseMatrix & operator=(SparseMatrix &&)=delete
Eigen::MatrixXd toEigenDense() const
Definition sparse_matrix.cpp:335
Mat m_petsc_matrix
Definition sparse_matrix.hpp:421
void setNearNullSpace(const Vector &v)
Attach a near null space to the matrix, used by algebraic multigrid preconditioners (e....
Definition sparse_matrix.cpp:458
PetscReal norm(NormType normType=NORM_FROBENIUS) const
Get the norm of the matrix.
Definition sparse_matrix.cpp:177
void createMatrix(Type type)
Definition sparse_matrix.cpp:49
std::pair< PetscInt, PetscInt > getMatrixSizes() const
Get the local and global sizes of the matrix.
Definition sparse_matrix.cpp:364
SparseMatrix(const SparseMatrix &)=delete
Type
Definition sparse_matrix.hpp:56
@ Plain
Definition sparse_matrix.hpp:56
@ Vectorized
Definition sparse_matrix.hpp:56
@ Block
Definition sparse_matrix.hpp:56
@ IntelMKL
Definition sparse_matrix.hpp:56
@ AcceleratedSIMD
Definition sparse_matrix.hpp:56
PetscInt getBlockSize() const
Get the block size of the matrix.
Definition sparse_matrix.cpp:189
void compress()
Finalize the assembly of the matrix by doing intracommunication.
Definition sparse_matrix.cpp:118
const auto & getPetscMatrix() const
Get the underlying PETSc matrix.
Definition sparse_matrix.hpp:307
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.
Definition sparse_matrix.hpp:69
void matMult(const SparseMatrix &B, SparseMatrix &C, MatReuse reuse=MAT_INITIAL_MATRIX, PetscReal fill=PETSC_DETERMINE) const
Definition sparse_matrix.cpp:415
void copy(const la::SparseMatrix &src, MatStructure str=DIFFERENT_NONZERO_PATTERN)
Copy the contents of another SparseMatrix into this one.
Definition sparse_matrix.cpp:195
void addScaledMatrix(const SparseMatrix &other, PetscScalar scale, MatStructure str=UNKNOWN_NONZERO_PATTERN)
Definition sparse_matrix.cpp:445
void save(const std::string &filename, const std::string &matname="", PetscViewerFormat format=PETSC_VIEWER_ASCII_MATLAB) const
Save the matrix to a file.
Definition sparse_matrix.cpp:132
~SparseMatrix()
Definition sparse_matrix.cpp:516
std::array< PetscInt, 2 > getSize() const
Get the size of the matrix as an array [rows, cols].
Definition sparse_matrix.cpp:183
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.
Definition sparse_matrix.cpp:270
void print() const
Print the matrix to standard output.
Definition sparse_matrix.cpp:123
SparseMatrix & operator=(const SparseMatrix &other)=delete
void computePreallocationData(const DofHandler &dof_handler, const PetscInt dof_begin, const PetscInt dof_end, std::vector< PetscInt > &dnn, std::vector< PetscInt > &onn)
Definition sparse_matrix.hpp:392
void saveBinary(const std::string &filename, const std::string &matname="") const
Save the matrix to a file in binary format.
Definition sparse_matrix.cpp:159
void insertValue(PetscScalar value, PetscInt i, PetscInt j, InsertMode mode=ADD_VALUES)
Insert a single value into the matrix.
Definition sparse_matrix.cpp:203
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 col...
Definition sparse_matrix.cpp:472
void vmultTAdd(const Vector &in_mult, const Vector &in_add, Vector &out) const
Definition sparse_matrix.cpp:385
void vmultAdd(const Vector &in_mult, const Vector &in_add, Vector &out) const
Definition sparse_matrix.cpp:379
void vmult(const Vector &in, Vector &out) const
Definition sparse_matrix.cpp:371
void setMatrixSizes(const PetscInt n_loc, const PetscInt n_glob)
Definition sparse_matrix.cpp:75
SparseMatrix(SparseMatrix &&)=delete
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.
Definition sparse_matrix.hpp:103
auto & getPetscMatrix()
Get the underlying PETSc matrix (non-const).
Definition sparse_matrix.hpp:309
void matMultTranspose(const SparseMatrix &B, SparseMatrix &C, MatReuse reuse=MAT_INITIAL_MATRIX, PetscReal fill=PETSC_DETERMINE) const
Definition sparse_matrix.cpp:425
void zero()
Set all entries to zero, keeping the nonzero pattern.
Definition sparse_matrix.cpp:199
SparseMatrix(Mat existing_matrix)
Definition sparse_matrix.hpp:384
void vmultT(const Vector &in, Vector &out) const
Definition sparse_matrix.cpp:375
A wrapper around PETSc Vec for distributed vectors.
Definition vector.hpp:54
Definition boomer_amg.hpp:27
DataLayout
Definition sparse_matrix.hpp:50
@ ColumnMajor
Definition sparse_matrix.hpp:50
@ RowMajor
Definition sparse_matrix.hpp:50
static int rank()
Return the MPI rank in MPI_COMM_WORLD.
Definition mpi_wrappers.hpp:116
Definition cell_manager.hpp:38
vulpes::utils::pcout_t & pcout
Definition pcout.hpp:160
#define PetscCallAbortWorld(...)
Definition petsc_utils.hpp:32