OGS
PETScMatrix.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <string>
7#include <vector>
8
10#include "PETScMatrixOption.h"
11
12typedef Mat PETSc_Mat;
13
14namespace MathLib
15{
16class PETScVector;
21{
22public:
23 using IndexType = PetscInt;
24
25public:
33 PETScMatrix(const PetscInt nrows,
34 const PETScMatrixOption& mat_op = PETScMatrixOption());
35
43 PETScMatrix(const PetscInt nrows, const PetscInt ncols,
44 const PETScMatrixOption& mat_op = PETScMatrixOption());
45
47 PETScMatrix(PETScMatrix const& A);
48
50
56 void finalizeAssembly(const MatAssemblyType asm_type = MAT_FINAL_ASSEMBLY)
57 {
58 MatAssemblyBegin(A_, asm_type);
59 MatAssemblyEnd(A_, asm_type);
60 }
61
63 PetscInt getNumberOfRows() const { return nrows_; }
65 PetscInt getNumberOfColumns() const { return ncols_; }
67 PetscInt getNumberOfLocalRows() const { return n_loc_rows_; }
69 PetscInt getNumberOfLocalColumns() const { return n_loc_cols_; }
71 PetscInt getRangeBegin() const { return start_rank_; }
73 PetscInt getRangeEnd() const { return end_rank_; }
75 Mat& getRawMatrix() { return A_; }
82 Mat const& getRawMatrix() const { return A_; }
84 void setZero() { MatZeroEntries(A_); }
94 void setRowsColumnsZero(std::vector<PetscInt> const& row_pos);
95
102 void set(const PetscInt i, const PetscInt j, const PetscScalar value)
103 {
104 MatSetValue(A_, i, j, value, INSERT_VALUES);
105 }
106
113 void add(const PetscInt i, const PetscInt j, const PetscScalar value)
114 {
115 MatSetValue(A_, i, j, value, ADD_VALUES);
116 }
117
134 template <class T_DENSE_MATRIX>
135 void add(RowColumnIndices<PetscInt> const& indices,
136 const T_DENSE_MATRIX& sub_matrix)
137 {
138 // Set global column indices to positive to allow all entries of columns
139 // to be added to the global matrix. For the ghost columns, only the
140 // off diagonal entries are added due to the negative indices of the
141 // corresponding rows.
142 std::vector<PetscInt> cols;
143 cols.reserve(indices.columns.size());
144 for (auto col : indices.columns)
145 {
146 // Ghost entries, and its original index is 0.
147 if (col == -ncols_)
148 {
149 cols.push_back(0);
150 }
151 else
152 {
153 cols.push_back(std::abs(col));
154 }
155 }
156
157 add(indices.rows, cols, sub_matrix);
158 }
159
168 template <class T_DENSE_MATRIX>
169 void add(std::vector<PetscInt> const& row_pos,
170 std::vector<PetscInt> const& col_pos,
171 const T_DENSE_MATRIX& sub_mat);
172
202 void viewer(const std::string& file_name,
203 const PetscViewerFormat vw_format = PETSC_VIEWER_ASCII_MATLAB);
204
205private:
206 void destroy()
207 {
208 if (A_ != nullptr)
209 {
210 MatDestroy(&A_);
211 }
212 A_ = nullptr;
213 }
214
216 Mat A_ = nullptr;
217
219 PetscInt nrows_;
220
222 PetscInt ncols_;
223
225 PetscInt n_loc_rows_;
226
228 PetscInt n_loc_cols_;
229
231 PetscInt start_rank_;
232
234 PetscInt end_rank_;
235
244 void create(const PetscInt d_nz, const PetscInt o_nz);
245
246 friend bool finalizeMatrixAssembly(PETScMatrix& mat,
247 const MatAssemblyType asm_type);
248};
249
250template <class T_DENSE_MATRIX>
251void PETScMatrix::add(std::vector<PetscInt> const& row_pos,
252 std::vector<PetscInt> const& col_pos,
253 const T_DENSE_MATRIX& sub_mat)
254{
255 const PetscInt nrows = static_cast<PetscInt>(row_pos.size());
256 const PetscInt ncols = static_cast<PetscInt>(col_pos.size());
257
258 MatSetValues(A_, nrows, &row_pos[0], ncols, &col_pos[0], sub_mat.data(),
259 ADD_VALUES);
260};
261
269 PETScMatrix& mat, const MatAssemblyType asm_type = MAT_FINAL_ASSEMBLY);
270
271} // namespace MathLib
Mat PETSc_Mat
Definition PETScMatrix.h:12
Wrapper class for PETSc matrix routines for matrix.
Definition PETScMatrix.h:21
PetscInt n_loc_cols_
Number of the local columns.
Mat const & getRawMatrix() const
Definition PETScMatrix.h:82
PetscInt getNumberOfLocalRows() const
Get the number of local rows.
Definition PETScMatrix.h:67
friend bool finalizeMatrixAssembly(PETScMatrix &mat, const MatAssemblyType asm_type)
General interface for the matrix assembly.
PETScMatrix & operator=(PETScMatrix const &A)
Mat & getRawMatrix()
Get matrix reference.
Definition PETScMatrix.h:75
PetscInt getNumberOfColumns() const
Get the number of columns.
Definition PETScMatrix.h:65
void add(RowColumnIndices< PetscInt > const &indices, const T_DENSE_MATRIX &sub_matrix)
Add sub-matrix at positions given by global indices, in which negative index indicates ghost entry.
void setZero()
Set all entries to zero.
Definition PETScMatrix.h:84
void create(const PetscInt d_nz, const PetscInt o_nz)
Create the matrix, configure memory allocation and set the related member data.
PetscInt getRangeEnd() const
Get the end global index of the rows in the same rank.
Definition PETScMatrix.h:73
void set(const PetscInt i, const PetscInt j, const PetscScalar value)
Set a single entry with a value.
PetscInt end_rank_
Ending index in a rank.
PetscInt getRangeBegin() const
Get the start global index of the rows of the same rank.
Definition PETScMatrix.h:71
void finalizeAssembly(const MatAssemblyType asm_type=MAT_FINAL_ASSEMBLY)
Perform MPI collection of assembled entries in buffer.
Definition PETScMatrix.h:56
PetscInt n_loc_rows_
Number of the local rows.
Mat A_
PETSc matrix.
PetscInt nrows_
Number of the global rows.
void setRowsColumnsZero(std::vector< PetscInt > const &row_pos)
Set the specified rows to zero except diagonal entries, i.e. , where This function must be called...
PetscInt ncols_
Number of the global columns.
void viewer(const std::string &file_name, const PetscViewerFormat vw_format=PETSC_VIEWER_ASCII_MATLAB)
void add(const PetscInt i, const PetscInt j, const PetscScalar value)
Add value to a single entry.
PetscInt getNumberOfRows() const
Get the number of rows.
Definition PETScMatrix.h:63
PetscInt start_rank_
Starting index in a rank.
PetscInt getNumberOfLocalColumns() const
Get the number of local columns.
Definition PETScMatrix.h:69
bool finalizeMatrixAssembly(MAT_T &)
This a struct data containing the configuration information to create a PETSc type matrix.