OGS
MathLib::EigenMatrix Class Referencefinal

Detailed Description

Global matrix based on Eigen sparse matrix

The matrix will be dynamically allocated during construction.

Definition at line 27 of file EigenMatrix.h.

#include <EigenMatrix.h>

Public Types

using RawMatrixType = Eigen::SparseMatrix<double, Eigen::RowMajor>
 
using IndexType = RawMatrixType::Index
 

Public Member Functions

 EigenMatrix (IndexType const n, IndexType const n_nonzero_columns=0)
 
IndexType getNumberOfRows () const
 return the number of rows
 
IndexType getNumberOfColumns () const
 return the number of columns
 
IndexType getRangeEnd () const
 return an end index of the active data range
 
void setZero ()
 reset data entries to zero.
 
int setValue (IndexType row, IndexType col, double val)
 
int add (IndexType row, IndexType col, double val)
 
template<class T_DENSE_MATRIX >
void add (std::vector< IndexType > const &row_pos, const T_DENSE_MATRIX &sub_matrix, double fkt=1.0)
 
template<class T_DENSE_MATRIX >
void add (RowColumnIndices< IndexType > const &indices, const T_DENSE_MATRIX &sub_matrix, double fkt=1.0)
 
template<class T_DENSE_MATRIX >
void add (std::vector< IndexType > const &row_pos, std::vector< IndexType > const &col_pos, const T_DENSE_MATRIX &sub_matrix, double fkt=1.0)
 
double get (IndexType row, IndexType col) const
 get value. This function returns zero if the element doesn't exist.
 
void write (const std::string &filename) const
 printout this matrix for debugging
 
void write (std::ostream &os) const
 printout this matrix for debugging
 
RawMatrixTypegetRawMatrix ()
 
const RawMatrixTypegetRawMatrix () const
 

Static Public Member Functions

static constexpr IndexType getRangeBegin ()
 return a start index of the active data range
 
static constexpr bool isAssembled ()
 return always true, i.e. the matrix is always ready for use
 

Protected Attributes

RawMatrixType mat_
 

Member Typedef Documentation

◆ IndexType

using MathLib::EigenMatrix::IndexType = RawMatrixType::Index

Definition at line 31 of file EigenMatrix.h.

◆ RawMatrixType

using MathLib::EigenMatrix::RawMatrixType = Eigen::SparseMatrix<double, Eigen::RowMajor>

Definition at line 30 of file EigenMatrix.h.

Constructor & Destructor Documentation

◆ EigenMatrix()

MathLib::EigenMatrix::EigenMatrix ( IndexType const n,
IndexType const n_nonzero_columns = 0 )
inlineexplicit
Parameters
nthe number of rows (that is equal to the number of columns).
n_nonzero_columnsthe number of non-zero columns used for preallocation.

Definition at line 40 of file EigenMatrix.h.

42 : mat_(n, n)
43 {
44 if (n_nonzero_columns > 0)
45 {
46 mat_.reserve(Eigen::Matrix<IndexType, Eigen::Dynamic, 1>::Constant(
47 n, n_nonzero_columns));
48 }
49 }
RawMatrixType mat_

References mat_.

Member Function Documentation

◆ add() [1/4]

◆ add() [2/4]

template<class T_DENSE_MATRIX >
void MathLib::EigenMatrix::add ( RowColumnIndices< IndexType > const & indices,
const T_DENSE_MATRIX & sub_matrix,
double fkt = 1.0 )
inline

Add sub-matrix at positions given by indices. If the entry doesn't exist, this class inserts the value.

Definition at line 105 of file EigenMatrix.h.

108 {
109 this->add(indices.rows, indices.columns, sub_matrix, fkt);
110 }
int add(IndexType row, IndexType col, double val)
Definition EigenMatrix.h:86

References add(), MathLib::RowColumnIndices< IDX_TYPE >::columns, and MathLib::RowColumnIndices< IDX_TYPE >::rows.

◆ add() [3/4]

template<class T_DENSE_MATRIX >
void MathLib::EigenMatrix::add ( std::vector< IndexType > const & row_pos,
const T_DENSE_MATRIX & sub_matrix,
double fkt = 1.0 )
inline

Add sub-matrix at positions row_pos and same column positions as the given row positions. If the entry doesn't exist, the value is inserted.

Definition at line 95 of file EigenMatrix.h.

98 {
99 this->add(row_pos, row_pos, sub_matrix, fkt);
100 }

References add().

◆ add() [4/4]

template<class T_DENSE_MATRIX >
void MathLib::EigenMatrix::add ( std::vector< IndexType > const & row_pos,
std::vector< IndexType > const & col_pos,
const T_DENSE_MATRIX & sub_matrix,
double fkt = 1.0 )

Add sub-matrix at positions row_pos and col_pos. If the entries doesn't exist in the matrix, the values are inserted.

Parameters
row_posa vector of row position indices. The vector size should equal to the number of rows in the given sub-matrix.
col_posa vector of column position indices. The vector size should equal to the number of columns in the given sub-matrix.
sub_matrixa sub-matrix to be added
fkta scaling factor applied to all entries in the sub-matrix

Definition at line 152 of file EigenMatrix.h.

155{
156 auto const n_rows = row_pos.size();
157 auto const n_cols = col_pos.size();
158 for (auto i = decltype(n_rows){0}; i < n_rows; i++)
159 {
160 auto const row = row_pos[i];
161 for (auto j = decltype(n_cols){0}; j < n_cols; j++)
162 {
163 auto const col = col_pos[j];
164 add(row, col, fkt * sub_matrix(i, j));
165 }
166 }
167};

References add().

◆ get()

double MathLib::EigenMatrix::get ( IndexType row,
IndexType col ) const
inline

get value. This function returns zero if the element doesn't exist.

Definition at line 130 of file EigenMatrix.h.

131 {
132 return mat_.coeff(row, col);
133 }

References mat_.

◆ getNumberOfColumns()

IndexType MathLib::EigenMatrix::getNumberOfColumns ( ) const
inline

return the number of columns

Definition at line 55 of file EigenMatrix.h.

55{ return mat_.cols(); }

References mat_.

Referenced by setValue().

◆ getNumberOfRows()

IndexType MathLib::EigenMatrix::getNumberOfRows ( ) const
inline

return the number of rows

Definition at line 52 of file EigenMatrix.h.

52{ return mat_.rows(); }

References mat_.

Referenced by getRangeEnd(), MathLib::SetMatrixSparsity< EigenMatrix, SPARSITY_PATTERN >::operator()(), setValue(), and MathLib::EigenLisLinearSolver::solve().

◆ getRangeBegin()

static constexpr IndexType MathLib::EigenMatrix::getRangeBegin ( )
inlinestaticconstexpr

return a start index of the active data range

Definition at line 58 of file EigenMatrix.h.

58{ return 0; }

◆ getRangeEnd()

IndexType MathLib::EigenMatrix::getRangeEnd ( ) const
inline

return an end index of the active data range

Definition at line 61 of file EigenMatrix.h.

61{ return getNumberOfRows(); }
IndexType getNumberOfRows() const
return the number of rows
Definition EigenMatrix.h:52

References getNumberOfRows().

◆ getRawMatrix() [1/2]

◆ getRawMatrix() [2/2]

const RawMatrixType & MathLib::EigenMatrix::getRawMatrix ( ) const
inline

Definition at line 145 of file EigenMatrix.h.

145{ return mat_; }

References mat_.

◆ isAssembled()

static constexpr bool MathLib::EigenMatrix::isAssembled ( )
inlinestaticconstexpr

return always true, i.e. the matrix is always ready for use

Definition at line 136 of file EigenMatrix.h.

136{ return true; }

◆ setValue()

int MathLib::EigenMatrix::setValue ( IndexType row,
IndexType col,
double val )
inline

set a value to the given entry. If the entry doesn't exist, this class dynamically allocates it.

Definition at line 76 of file EigenMatrix.h.

77 {
78 assert(row < (IndexType)getNumberOfRows() &&
80 mat_.coeffRef(row, col) = val;
81 return 0;
82 }
RawMatrixType::Index IndexType
Definition EigenMatrix.h:31
IndexType getNumberOfColumns() const
return the number of columns
Definition EigenMatrix.h:55

References getNumberOfColumns(), getNumberOfRows(), and mat_.

◆ setZero()

void MathLib::EigenMatrix::setZero ( )
inline

reset data entries to zero.

Definition at line 64 of file EigenMatrix.h.

65 {
66 auto const N = mat_.nonZeros();
67 for (auto i = decltype(N){0}; i < N; i++)
68 {
69 mat_.valuePtr()[i] = 0;
70 }
71 // don't use mat_.setZero(). it makes a matrix uncompressed
72 }

References mat_.

◆ write() [1/2]

void MathLib::EigenMatrix::write ( const std::string & filename) const

printout this matrix for debugging

Definition at line 19 of file EigenMatrix.cpp.

20{
21 std::ofstream of(filename);
22 if (of)
23 {
24 write(of);
25 }
26}
void write(const std::string &filename) const
printout this matrix for debugging

References write().

Referenced by write().

◆ write() [2/2]

void MathLib::EigenMatrix::write ( std::ostream & os) const

printout this matrix for debugging

Definition at line 29 of file EigenMatrix.cpp.

30{
31 for (int k = 0; k < mat_.outerSize(); ++k)
32 {
33 for (RawMatrixType::InnerIterator it(mat_, k); it; ++it)
34 {
35 os << it.row() << " " << it.col() << " " << it.value() << "\n";
36 }
37 }
38 os << std::endl;
39}

References mat_.

Member Data Documentation

◆ mat_

RawMatrixType MathLib::EigenMatrix::mat_
protected

The documentation for this class was generated from the following files: