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