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 28 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 More...
 
IndexType getNumberOfColumns () const
 return the number of columns More...
 
IndexType getRangeEnd () const
 return an end index of the active data range More...
 
void setZero ()
 reset data entries to zero. More...
 
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. More...
 
void write (const std::string &filename) const
 printout this matrix for debugging More...
 
void write (std::ostream &os) const
 printout this matrix for debugging More...
 
RawMatrixTypegetRawMatrix ()
 
const RawMatrixTypegetRawMatrix () const
 

Static Public Member Functions

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

Protected Attributes

RawMatrixType mat_
 

Member Typedef Documentation

◆ IndexType

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

Definition at line 32 of file EigenMatrix.h.

◆ RawMatrixType

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

Definition at line 31 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 41 of file EigenMatrix.h.

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  }
RawMatrixType mat_
Definition: EigenMatrix.h:166

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 106 of file EigenMatrix.h.

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

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 96 of file EigenMatrix.h.

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

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 170 of file EigenMatrix.h.

173 {
174  auto const n_rows = row_pos.size();
175  auto const n_cols = col_pos.size();
176  for (auto i = decltype(n_rows){0}; i < n_rows; i++)
177  {
178  auto const row = row_pos[i];
179  for (auto j = decltype(n_cols){0}; j < n_cols; j++)
180  {
181  auto const col = col_pos[j];
182  add(row, col, fkt * sub_matrix(i, j));
183  }
184  }
185 };

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 131 of file EigenMatrix.h.

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

References mat_.

◆ getNumberOfColumns()

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

return the number of columns

Definition at line 56 of file EigenMatrix.h.

56 { return mat_.cols(); }

References mat_.

Referenced by setValue().

◆ getNumberOfRows()

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

return the number of rows

Definition at line 53 of file EigenMatrix.h.

53 { 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 59 of file EigenMatrix.h.

59 { return 0; }

◆ getRangeEnd()

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

return an end index of the active data range

Definition at line 62 of file EigenMatrix.h.

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

References getNumberOfRows().

◆ getRawMatrix() [1/2]

◆ getRawMatrix() [2/2]

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

Definition at line 163 of file EigenMatrix.h.

163 { 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 137 of file EigenMatrix.h.

137 { 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 77 of file EigenMatrix.h.

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

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

◆ setZero()

void MathLib::EigenMatrix::setZero ( )
inline

reset data entries to zero.

Definition at line 65 of file EigenMatrix.h.

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  }

References mat_.

◆ write() [1/2]

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

printout this matrix for debugging

Definition at line 140 of file EigenMatrix.h.

141  {
142  std::ofstream of(filename);
143  if (of)
144  {
145  write(of);
146  }
147  }
void write(const std::string &filename) const
printout this matrix for debugging
Definition: EigenMatrix.h:140

◆ write() [2/2]

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

printout this matrix for debugging

Definition at line 150 of file EigenMatrix.h.

151  {
152  for (int k = 0; k < mat_.outerSize(); ++k)
153  {
154  for (RawMatrixType::InnerIterator it(mat_, k); it; ++it)
155  {
156  os << it.row() << " " << it.col() << ": " << it.value() << "\n";
157  }
158  }
159  os << std::endl;
160  }

References mat_.

Member Data Documentation

◆ mat_

RawMatrixType MathLib::EigenMatrix::mat_
protected

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