OGS
EigenMatrix.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 <Eigen/Sparse>
7#include <cassert>
8#include <iosfwd>
9#include <string>
10
13
14namespace MathLib
15{
21class EigenMatrix final
22{
23public:
24 using RawMatrixType = Eigen::SparseMatrix<double, Eigen::RowMajor>;
25 using IndexType = RawMatrixType::Index;
26
27 // TODO The matrix constructor should take num_rows and num_cols as
28 // arguments
29 // that is left for a later refactoring.
30
34 explicit EigenMatrix(IndexType const n,
35 IndexType const n_nonzero_columns = 0)
36 : mat_(n, n)
37 {
38 if (n_nonzero_columns > 0)
39 {
40 mat_.reserve(Eigen::Matrix<IndexType, Eigen::Dynamic, 1>::Constant(
41 n, n_nonzero_columns));
42 }
43 }
44
46 IndexType getNumberOfRows() const { return mat_.rows(); }
47
49 IndexType getNumberOfColumns() const { return mat_.cols(); }
50
52 static constexpr IndexType getRangeBegin() { return 0; }
53
56
58 void setZero()
59 {
60 auto const N = mat_.nonZeros();
61 for (auto i = decltype(N){0}; i < N; i++)
62 {
63 mat_.valuePtr()[i] = 0;
64 }
65 // don't use mat_.setZero(). it makes a matrix uncompressed
66 }
67
70 int setValue(IndexType row, IndexType col, double val)
71 {
72 assert(row < (IndexType)getNumberOfRows() &&
74 mat_.coeffRef(row, col) = val;
75 return 0;
76 }
77
80 int add(IndexType row, IndexType col, double val)
81 {
82 mat_.coeffRef(row, col) += val;
83 return 0;
84 }
85
88 template <class T_DENSE_MATRIX>
89 void add(std::vector<IndexType> const& row_pos,
90 const T_DENSE_MATRIX& sub_matrix,
91 double fkt = 1.0)
92 {
93 this->add(row_pos, row_pos, sub_matrix, fkt);
94 }
95
98 template <class T_DENSE_MATRIX>
99 void add(RowColumnIndices<IndexType> const& indices,
100 const T_DENSE_MATRIX& sub_matrix,
101 double fkt = 1.0)
102 {
103 this->add(indices.rows, indices.columns, sub_matrix, fkt);
104 }
105
118 template <class T_DENSE_MATRIX>
119 void add(std::vector<IndexType> const& row_pos,
120 std::vector<IndexType> const& col_pos,
121 const T_DENSE_MATRIX& sub_matrix, double fkt = 1.0);
122
124 double get(IndexType row, IndexType col) const
125 {
126 return mat_.coeff(row, col);
127 }
128
130 static constexpr bool isAssembled() { return true; }
131
133 void write(const std::string& filename) const;
134
136 void write(std::ostream& os) const;
137
139 const RawMatrixType& getRawMatrix() const { return mat_; }
140
141protected:
143};
144
145template <class T_DENSE_MATRIX>
146void EigenMatrix::add(std::vector<IndexType> const& row_pos,
147 std::vector<IndexType> const& col_pos,
148 const T_DENSE_MATRIX& sub_matrix, double fkt)
149{
150 auto const n_rows = row_pos.size();
151 auto const n_cols = col_pos.size();
152 for (auto i = decltype(n_rows){0}; i < n_rows; i++)
153 {
154 auto const row = row_pos[i];
155 for (auto j = decltype(n_cols){0}; j < n_cols; j++)
156 {
157 auto const col = col_pos[j];
158 add(row, col, fkt * sub_matrix(i, j));
159 }
160 }
161};
162
164template <typename SPARSITY_PATTERN>
165struct SetMatrixSparsity<EigenMatrix, SPARSITY_PATTERN>
166{
170 SPARSITY_PATTERN const& sparsity_pattern) const
171 {
172 static_assert(EigenMatrix::RawMatrixType::IsRowMajor,
173 "Set matrix sparsity relies on the EigenMatrix to be in "
174 "row-major storage order.");
175
176 assert(matrix.getNumberOfRows() ==
177 static_cast<EigenMatrix::IndexType>(sparsity_pattern.size()));
178
179 matrix.getRawMatrix().reserve(sparsity_pattern);
180 }
181};
182
183} // end namespace MathLib
RawMatrixType::Index IndexType
Definition EigenMatrix.h:25
IndexType getNumberOfColumns() const
return the number of columns
Definition EigenMatrix.h:49
void add(std::vector< IndexType > const &row_pos, const T_DENSE_MATRIX &sub_matrix, double fkt=1.0)
Definition EigenMatrix.h:89
static constexpr IndexType getRangeBegin()
return a start index of the active data range
Definition EigenMatrix.h:52
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)
Definition EigenMatrix.h:99
void setZero()
reset data entries to zero.
Definition EigenMatrix.h:58
int add(IndexType row, IndexType col, double val)
Definition EigenMatrix.h:80
RawMatrixType mat_
IndexType getNumberOfRows() const
return the number of rows
Definition EigenMatrix.h:46
int setValue(IndexType row, IndexType col, double val)
Definition EigenMatrix.h:70
Eigen::SparseMatrix< double, Eigen::RowMajor > RawMatrixType
Definition EigenMatrix.h:24
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:34
IndexType getRangeEnd() const
return an end index of the active data range
Definition EigenMatrix.h:55
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