OGS
LisMatrix.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
9#include "LisCheck.h"
10#include "LisWrapper.h"
13
14namespace MathLib
15{
16// Forward declarations.
17class LisVector;
18class LisMatrix;
19
20template <typename SPARSITY_PATTERN>
21struct SetMatrixSparsity<LisMatrix, SPARSITY_PATTERN>;
22
31{
32public:
34 enum class MatrixType : int
35 {
36 CRS = 1,
37 CCS = 2,
38 MSR = 3,
39 DIA = 4,
40 ELL = 5,
41 JDS = 6,
42 BSR = 7,
43 BSC = 8,
44 VBR = 9,
45 COO = 10,
46 DNS = 11
47 };
48
49 using IndexType = LIS_INT;
50
51public:
57 LisMatrix(std::size_t n_rows, MatrixType mat_type = MatrixType::CRS);
58
69 LisMatrix(std::size_t n_rows, int nonzero, IndexType* row_ptr,
70 IndexType* col_idx, double* data);
71
75 virtual ~LisMatrix();
76
78 std::size_t getNumberOfRows() const { return n_rows_; }
79
81 std::size_t getNumberOfColumns() const { return getNumberOfRows(); }
82
84 std::size_t getRangeBegin() const { return is_; }
85
87 std::size_t getRangeEnd() const { return ie_; }
88
90 void setZero();
91
93 int setValue(IndexType rowId, IndexType colId, double v);
94
96 int add(IndexType rowId, IndexType colId, double v);
97
99 void write(const std::string& filename) const;
100
102 LIS_MATRIX& getRawMatrix() { return AA_; }
103
106 template <class T_DENSE_MATRIX>
107 void add(std::vector<IndexType> const& row_pos,
108 const T_DENSE_MATRIX& sub_matrix,
109 double fkt = 1.0)
110 {
111 this->add(row_pos, row_pos, sub_matrix, fkt);
112 }
113
115 template <class T_DENSE_MATRIX>
116 void add(RowColumnIndices<IndexType> const& indices,
117 const T_DENSE_MATRIX& sub_matrix,
118 double fkt = 1.0)
119 {
120 this->add(indices.rows, indices.columns, sub_matrix, fkt);
121 }
122
123 template <class T_DENSE_MATRIX>
124 void add(std::vector<IndexType> const& row_pos,
125 std::vector<IndexType> const& col_pos,
126 const T_DENSE_MATRIX& sub_matrix, double fkt = 1.0);
127
130
132 bool isAssembled() const { return is_assembled_; }
133
134private:
135 std::size_t const n_rows_;
137 LIS_MATRIX AA_;
138 LIS_VECTOR diag_;
145
146 // friend function
147 friend bool finalizeMatrixAssembly(LisMatrix& mat);
148
149 template <typename MATRIX, typename SPARSITY_PATTERN>
150 friend struct SetMatrixSparsity;
151};
152
153template <class T_DENSE_MATRIX>
154void LisMatrix::add(std::vector<IndexType> const& row_pos,
155 std::vector<IndexType> const& col_pos,
156 const T_DENSE_MATRIX& sub_matrix, double fkt)
157{
158 auto const n_rows = row_pos.size();
159 auto const n_cols = col_pos.size();
160 for (auto i = decltype(n_rows){0}; i < n_rows; i++)
161 {
162 auto const row = row_pos[i];
163 for (auto j = decltype(n_cols){0}; j < n_cols; j++)
164 {
165 auto const col = col_pos[j];
166 add(row, col, fkt * sub_matrix(i, j));
167 }
168 }
169};
170
173
175template <typename SPARSITY_PATTERN>
176struct SetMatrixSparsity<LisMatrix, SPARSITY_PATTERN>
177{
178 void operator()(LisMatrix& matrix,
179 SPARSITY_PATTERN const& sparsity_pattern) const
180 {
181 auto const n_rows = matrix.getNumberOfRows();
182 std::vector<LisMatrix::IndexType> row_sizes;
183 row_sizes.reserve(n_rows);
184
185 // LIS needs 1 more entry, otherwise it starts reallocating arrays.
186 transform(cbegin(sparsity_pattern), cend(sparsity_pattern),
187 back_inserter(row_sizes), [](auto const i) { return i + 1; });
188
189 int ierr = lis_matrix_malloc(matrix.AA_, 0, row_sizes.data());
190 checkLisError(ierr);
191 }
192};
193
194} // namespace MathLib
LisMatrix is a wrapper class for matrix types of the linear iterative solvers library.
Definition LisMatrix.h:31
IndexType ie_
location where the partial matrix AA_ ends in global matrix.
Definition LisMatrix.h:143
LisMatrix(std::size_t n_rows, MatrixType mat_type=MatrixType::CRS)
Definition LisMatrix.cpp:15
std::size_t getRangeBegin() const
return a start index of the active data range
Definition LisMatrix.h:84
friend struct SetMatrixSparsity
Definition LisMatrix.h:150
std::size_t const n_rows_
Definition LisMatrix.h:135
std::size_t getNumberOfColumns() const
return the number of columns
Definition LisMatrix.h:81
LIS_VECTOR diag_
Definition LisMatrix.h:138
MatrixType const mat_type_
Definition LisMatrix.h:136
std::size_t getNumberOfRows() const
return the number of rows
Definition LisMatrix.h:78
int setValue(IndexType rowId, IndexType colId, double v)
set entry
Definition LisMatrix.cpp:81
std::size_t getRangeEnd() const
return an end index of the active data range
Definition LisMatrix.h:87
int add(IndexType rowId, IndexType colId, double v)
add value
Definition LisMatrix.cpp:92
void add(std::vector< IndexType > const &row_pos, const T_DENSE_MATRIX &sub_matrix, double fkt=1.0)
Definition LisMatrix.h:107
void setZero()
reset this matrix with keeping its original dimension
Definition LisMatrix.cpp:65
MatrixType getMatrixType() const
get this matrix type
Definition LisMatrix.h:129
friend bool finalizeMatrixAssembly(LisMatrix &mat)
finish assembly to make this matrix be ready for use
void write(const std::string &filename) const
printout this equation for debugging
MatrixType
Matrix type.
Definition LisMatrix.h:35
LIS_MATRIX & getRawMatrix()
return a raw Lis matrix object
Definition LisMatrix.h:102
void add(RowColumnIndices< IndexType > const &indices, const T_DENSE_MATRIX &sub_matrix, double fkt=1.0)
Add sub-matrix at positions given by indices.
Definition LisMatrix.h:116
virtual ~LisMatrix()
Definition LisMatrix.cpp:51
bool isAssembled() const
return if this matrix is already assembled or not
Definition LisMatrix.h:132
Lis vector wrapper class.
Definition LisVector.h:20
bool checkLisError(int err)
Definition LisCheck.h:20
static const double v
bool finalizeMatrixAssembly(MAT_T &)
void operator()(LisMatrix &matrix, SPARSITY_PATTERN const &sparsity_pattern) const
Definition LisMatrix.h:178