OGS
LisMatrix.h
Go to the documentation of this file.
1
15#pragma once
16
17#include <string>
18#include <vector>
19
20#include "LisCheck.h"
21#include "LisWrapper.h"
24
25namespace MathLib
26{
27// Forward declarations.
28class LisVector;
29class LisMatrix;
30
31template <typename SPARSITY_PATTERN>
32struct SetMatrixSparsity<LisMatrix, SPARSITY_PATTERN>;
33
42{
43public:
45 enum class MatrixType : int
46 {
47 CRS = 1,
48 CCS = 2,
49 MSR = 3,
50 DIA = 4,
51 ELL = 5,
52 JDS = 6,
53 BSR = 7,
54 BSC = 8,
55 VBR = 9,
56 COO = 10,
57 DNS = 11
58 };
59
60 using IndexType = LIS_INT;
61
62public:
68 LisMatrix(std::size_t n_rows, MatrixType mat_type = MatrixType::CRS);
69
80 LisMatrix(std::size_t n_rows, int nonzero, IndexType* row_ptr,
81 IndexType* col_idx, double* data);
82
86 virtual ~LisMatrix();
87
89 std::size_t getNumberOfRows() const { return n_rows_; }
90
92 std::size_t getNumberOfColumns() const { return getNumberOfRows(); }
93
95 std::size_t getRangeBegin() const { return is_; }
96
98 std::size_t getRangeEnd() const { return ie_; }
99
101 void setZero();
102
104 int setValue(IndexType rowId, IndexType colId, double v);
105
107 int add(IndexType rowId, IndexType colId, double v);
108
110 void write(const std::string& filename) const;
111
113 LIS_MATRIX& getRawMatrix() { return AA_; }
114
117 template <class T_DENSE_MATRIX>
118 void add(std::vector<IndexType> const& row_pos,
119 const T_DENSE_MATRIX& sub_matrix,
120 double fkt = 1.0)
121 {
122 this->add(row_pos, row_pos, sub_matrix, fkt);
123 }
124
126 template <class T_DENSE_MATRIX>
127 void add(RowColumnIndices<IndexType> const& indices,
128 const T_DENSE_MATRIX& sub_matrix,
129 double fkt = 1.0)
130 {
131 this->add(indices.rows, indices.columns, sub_matrix, fkt);
132 }
133
134 template <class T_DENSE_MATRIX>
135 void add(std::vector<IndexType> const& row_pos,
136 std::vector<IndexType> const& col_pos,
137 const T_DENSE_MATRIX& sub_matrix, double fkt = 1.0);
138
141
143 bool isAssembled() const { return is_assembled_; }
144
145private:
146 std::size_t const n_rows_;
148 LIS_MATRIX AA_;
149 LIS_VECTOR diag_;
156
157 // friend function
158 friend bool finalizeMatrixAssembly(LisMatrix& mat);
159
160 template <typename MATRIX, typename SPARSITY_PATTERN>
161 friend struct SetMatrixSparsity;
162};
163
164template <class T_DENSE_MATRIX>
165void LisMatrix::add(std::vector<IndexType> const& row_pos,
166 std::vector<IndexType> const& col_pos,
167 const T_DENSE_MATRIX& sub_matrix, double fkt)
168{
169 auto const n_rows = row_pos.size();
170 auto const n_cols = col_pos.size();
171 for (auto i = decltype(n_rows){0}; i < n_rows; i++)
172 {
173 auto const row = row_pos[i];
174 for (auto j = decltype(n_cols){0}; j < n_cols; j++)
175 {
176 auto const col = col_pos[j];
177 add(row, col, fkt * sub_matrix(i, j));
178 }
179 }
180};
181
184
186template <typename SPARSITY_PATTERN>
187struct SetMatrixSparsity<LisMatrix, SPARSITY_PATTERN>
188{
189 void operator()(LisMatrix& matrix,
190 SPARSITY_PATTERN const& sparsity_pattern) const
191 {
192 auto const n_rows = matrix.getNumberOfRows();
193 std::vector<LisMatrix::IndexType> row_sizes;
194 row_sizes.reserve(n_rows);
195
196 // LIS needs 1 more entry, otherwise it starts reallocating arrays.
197 transform(cbegin(sparsity_pattern), cend(sparsity_pattern),
198 back_inserter(row_sizes), [](auto const i) { return i + 1; });
199
200 int ierr = lis_matrix_malloc(matrix.AA_, 0, row_sizes.data());
201 checkLisError(ierr);
202 }
203};
204
205} // namespace MathLib
LisMatrix is a wrapper class for matrix types of the linear iterative solvers library.
Definition LisMatrix.h:42
IndexType ie_
location where the partial matrix AA_ ends in global matrix.
Definition LisMatrix.h:154
LisMatrix(std::size_t n_rows, MatrixType mat_type=MatrixType::CRS)
Definition LisMatrix.cpp:26
std::size_t getRangeBegin() const
return a start index of the active data range
Definition LisMatrix.h:95
std::size_t const n_rows_
Definition LisMatrix.h:146
std::size_t getNumberOfColumns() const
return the number of columns
Definition LisMatrix.h:92
LIS_VECTOR diag_
Definition LisMatrix.h:149
MatrixType const mat_type_
Definition LisMatrix.h:147
std::size_t getNumberOfRows() const
return the number of rows
Definition LisMatrix.h:89
int setValue(IndexType rowId, IndexType colId, double v)
set entry
Definition LisMatrix.cpp:92
std::size_t getRangeEnd() const
return an end index of the active data range
Definition LisMatrix.h:98
int add(IndexType rowId, IndexType colId, double v)
add value
void add(std::vector< IndexType > const &row_pos, const T_DENSE_MATRIX &sub_matrix, double fkt=1.0)
Definition LisMatrix.h:118
void setZero()
reset this matrix with keeping its original dimension
Definition LisMatrix.cpp:76
MatrixType getMatrixType() const
get this matrix type
Definition LisMatrix.h:140
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:46
LIS_MATRIX & getRawMatrix()
return a raw Lis matrix object
Definition LisMatrix.h:113
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:127
virtual ~LisMatrix()
Definition LisMatrix.cpp:62
bool isAssembled() const
return if this matrix is already assembled or not
Definition LisMatrix.h:143
bool checkLisError(int err)
Definition LisCheck.h:30
static const double v
bool finalizeMatrixAssembly(MAT_T &)
void operator()(LisMatrix &matrix, SPARSITY_PATTERN const &sparsity_pattern) const
Definition LisMatrix.h:189