OGS
EigenMatrix.h
Go to the documentation of this file.
1
11#pragma once
12
13#include <Eigen/Sparse>
14#include <cassert>
15#include <iosfwd>
16#include <string>
17
20
21namespace MathLib
22{
28class EigenMatrix final
29{
30public:
31 using RawMatrixType = Eigen::SparseMatrix<double, Eigen::RowMajor>;
32 using IndexType = RawMatrixType::Index;
33
34 // TODO The matrix constructor should take num_rows and num_cols as
35 // arguments
36 // that is left for a later refactoring.
37
41 explicit EigenMatrix(IndexType const n,
42 IndexType const n_nonzero_columns = 0)
43 : mat_(n, n)
44 {
45 if (n_nonzero_columns > 0)
46 {
47 mat_.reserve(Eigen::Matrix<IndexType, Eigen::Dynamic, 1>::Constant(
48 n, n_nonzero_columns));
49 }
50 }
51
53 IndexType getNumberOfRows() const { return mat_.rows(); }
54
56 IndexType getNumberOfColumns() const { return mat_.cols(); }
57
59 static constexpr IndexType getRangeBegin() { return 0; }
60
63
65 void setZero()
66 {
67 auto const N = mat_.nonZeros();
68 for (auto i = decltype(N){0}; i < N; i++)
69 {
70 mat_.valuePtr()[i] = 0;
71 }
72 // don't use mat_.setZero(). it makes a matrix uncompressed
73 }
74
77 int setValue(IndexType row, IndexType col, double val)
78 {
79 assert(row < (IndexType)getNumberOfRows() &&
81 mat_.coeffRef(row, col) = val;
82 return 0;
83 }
84
87 int add(IndexType row, IndexType col, double val)
88 {
89 mat_.coeffRef(row, col) += val;
90 return 0;
91 }
92
95 template <class T_DENSE_MATRIX>
96 void add(std::vector<IndexType> const& row_pos,
97 const T_DENSE_MATRIX& sub_matrix,
98 double fkt = 1.0)
99 {
100 this->add(row_pos, row_pos, sub_matrix, fkt);
101 }
102
105 template <class T_DENSE_MATRIX>
106 void add(RowColumnIndices<IndexType> const& indices,
107 const T_DENSE_MATRIX& sub_matrix,
108 double fkt = 1.0)
109 {
110 this->add(indices.rows, indices.columns, sub_matrix, fkt);
111 }
112
125 template <class T_DENSE_MATRIX>
126 void add(std::vector<IndexType> const& row_pos,
127 std::vector<IndexType> const& col_pos,
128 const T_DENSE_MATRIX& sub_matrix, double fkt = 1.0);
129
131 double get(IndexType row, IndexType col) const
132 {
133 return mat_.coeff(row, col);
134 }
135
137 static constexpr bool isAssembled() { return true; }
138
140 void write(const std::string& filename) const;
141
143 void write(std::ostream& os) const;
144
146 const RawMatrixType& getRawMatrix() const { return mat_; }
147
148protected:
150};
151
152template <class T_DENSE_MATRIX>
153void EigenMatrix::add(std::vector<IndexType> const& row_pos,
154 std::vector<IndexType> const& col_pos,
155 const T_DENSE_MATRIX& sub_matrix, double fkt)
156{
157 auto const n_rows = row_pos.size();
158 auto const n_cols = col_pos.size();
159 for (auto i = decltype(n_rows){0}; i < n_rows; i++)
160 {
161 auto const row = row_pos[i];
162 for (auto j = decltype(n_cols){0}; j < n_cols; j++)
163 {
164 auto const col = col_pos[j];
165 add(row, col, fkt * sub_matrix(i, j));
166 }
167 }
168};
169
171template <typename SPARSITY_PATTERN>
172struct SetMatrixSparsity<EigenMatrix, SPARSITY_PATTERN>
173{
177 SPARSITY_PATTERN const& sparsity_pattern) const
178 {
179 static_assert(EigenMatrix::RawMatrixType::IsRowMajor,
180 "Set matrix sparsity relies on the EigenMatrix to be in "
181 "row-major storage order.");
182
183 assert(matrix.getNumberOfRows() ==
184 static_cast<EigenMatrix::IndexType>(sparsity_pattern.size()));
185
186 matrix.getRawMatrix().reserve(sparsity_pattern);
187 }
188};
189
190} // end namespace MathLib
RawMatrixType::Index IndexType
Definition EigenMatrix.h:32
IndexType getNumberOfColumns() const
return the number of columns
Definition EigenMatrix.h:56
void add(std::vector< IndexType > const &row_pos, const T_DENSE_MATRIX &sub_matrix, double fkt=1.0)
Definition EigenMatrix.h:96
static constexpr IndexType getRangeBegin()
return a start index of the active data range
Definition EigenMatrix.h:59
void write(const std::string &filename) const
printout this matrix for debugging
const RawMatrixType & getRawMatrix() const
void add(RowColumnIndices< IndexType > const &indices, const T_DENSE_MATRIX &sub_matrix, double fkt=1.0)
void setZero()
reset data entries to zero.
Definition EigenMatrix.h:65
int add(IndexType row, IndexType col, double val)
Definition EigenMatrix.h:87
RawMatrixType mat_
IndexType getNumberOfRows() const
return the number of rows
Definition EigenMatrix.h:53
int setValue(IndexType row, IndexType col, double val)
Definition EigenMatrix.h:77
Eigen::SparseMatrix< double, Eigen::RowMajor > RawMatrixType
Definition EigenMatrix.h:31
double get(IndexType row, IndexType col) const
get value. This function returns zero if the element doesn't exist.
EigenMatrix(IndexType const n, IndexType const n_nonzero_columns=0)
Definition EigenMatrix.h:41
IndexType getRangeEnd() const
return an end index of the active data range
Definition EigenMatrix.h:62
RawMatrixType & getRawMatrix()
static constexpr bool isAssembled()
return always true, i.e. the matrix is always ready for use
void operator()(EigenMatrix &matrix, SPARSITY_PATTERN const &sparsity_pattern) const