Skip to content
Snippets Groups Projects
Commit e7c53352 authored by Dmitri Naumov's avatar Dmitri Naumov
Browse files

[MaL] Eigen; clang-format.

parent 6349f2ee
No related branches found
No related tags found
No related merge requests found
......@@ -18,7 +18,6 @@
#include <Eigen/PardisoSupport>
#endif
#ifdef USE_EIGEN_UNSUPPORTED
#include <unsupported/Eigen/src/IterativeSolvers/GMRES.h>
#include <unsupported/Eigen/src/IterativeSolvers/Scaling.h>
......
......@@ -22,7 +22,6 @@ class ConfigTree;
namespace MathLib
{
class EigenMatrix;
class EigenVector;
......@@ -40,7 +39,7 @@ public:
* LisOption struct.
*/
EigenLinearSolver(const std::string& solver_name,
BaseLib::ConfigTree const*const option);
BaseLib::ConfigTree const* const option);
~EigenLinearSolver();
......@@ -59,7 +58,7 @@ public:
*/
EigenOption& getOption() { return option_; }
bool solve(EigenMatrix &A, EigenVector& b, EigenVector &x);
bool solve(EigenMatrix& A, EigenVector& b, EigenVector& x);
protected:
EigenOption option_;
......
......@@ -10,9 +10,9 @@
#pragma once
#include <Eigen/Core>
#include <cassert>
#include <vector>
#include <Eigen/Core>
namespace MathLib
{
......@@ -204,8 +204,7 @@ inline Eigen::Map<const Eigen::VectorXd> toVector(
* This is a convenience method which makes the specification of dynamically
* allocated Eigen vectors as return type easier.
*/
inline Eigen::Map<Eigen::VectorXd> toVector(
std::vector<double>& data)
inline Eigen::Map<Eigen::VectorXd> toVector(std::vector<double>& data)
{
return {data.data(), static_cast<Eigen::VectorXd::Index>(data.size())};
}
......
......@@ -10,18 +10,16 @@
#pragma once
#include <Eigen/Sparse>
#include <fstream>
#include <string>
#include <Eigen/Sparse>
#include "EigenVector.h"
#include "MathLib/LinAlg/RowColumnIndices.h"
#include "MathLib/LinAlg/SetMatrixSparsity.h"
#include "EigenVector.h"
namespace MathLib
{
/**
* Global matrix based on Eigen sparse matrix
*
......@@ -33,7 +31,8 @@ public:
using RawMatrixType = Eigen::SparseMatrix<double, Eigen::RowMajor>;
using IndexType = RawMatrixType::Index;
// TODO The matrix constructor should take num_rows and num_cols as arguments
// TODO The matrix constructor should take num_rows and num_cols as
// arguments
// that is left for a later refactoring.
/**
* constructor
......@@ -60,7 +59,7 @@ public:
static constexpr IndexType getRangeBegin() { return 0; }
/// return an end index of the active data range
IndexType getRangeEnd() const { return getNumberOfRows(); }
IndexType getRangeEnd() const { return getNumberOfRows(); }
/// reset data entries to zero.
void setZero()
......@@ -77,7 +76,8 @@ public:
/// dynamically allocates it.
int setValue(IndexType row, IndexType col, double val)
{
assert(row < (IndexType) getNumberOfRows() && col < (IndexType) getNumberOfColumns());
assert(row < (IndexType)getNumberOfRows() &&
col < (IndexType)getNumberOfColumns());
mat_.coeffRef(row, col) = val;
return 0;
}
......@@ -92,36 +92,40 @@ public:
/// Add sub-matrix at positions \c row_pos and same column positions as the
/// given row positions. If the entry doesn't exist, the value is inserted.
template<class T_DENSE_MATRIX>
template <class T_DENSE_MATRIX>
void add(std::vector<IndexType> const& row_pos,
const T_DENSE_MATRIX &sub_matrix,
double fkt = 1.0)
const T_DENSE_MATRIX& sub_matrix,
double fkt = 1.0)
{
this->add(row_pos, row_pos, sub_matrix, fkt);
}
/// Add sub-matrix at positions given by \c indices. If the entry doesn't exist,
/// this class inserts the value.
template<class T_DENSE_MATRIX>
/// Add sub-matrix at positions given by \c indices. If the entry doesn't
/// exist, this class inserts the value.
template <class T_DENSE_MATRIX>
void add(RowColumnIndices<IndexType> const& indices,
const T_DENSE_MATRIX &sub_matrix,
double fkt = 1.0)
const T_DENSE_MATRIX& sub_matrix,
double fkt = 1.0)
{
this->add(indices.rows, indices.columns, sub_matrix, fkt);
}
/// Add sub-matrix at positions \c row_pos and \c col_pos. If the entries doesn't
/// exist in the matrix, the values are inserted.
/// @param row_pos a vector of row position indices. The vector size should
/// Add sub-matrix at positions \c row_pos and \c col_pos. If the entries
/// doesn't exist in the matrix, the values are inserted.
/// @param row_pos a vector of row position indices. The vector size
/// should
/// equal to the number of rows in the given sub-matrix.
/// @param col_pos a vector of column position indices. The vector size should
/// equal to the number of columns in the given sub-matrix.
/// @param col_pos a vector of column position indices. The vector size
/// should
/// equal to the number of columns in the given
/// sub-matrix.
/// @param sub_matrix a sub-matrix to be added
/// @param fkt a scaling factor applied to all entries in the sub-matrix
/// @param fkt a scaling factor applied to all entries in the
/// sub-matrix
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);
std::vector<IndexType> const& col_pos,
const T_DENSE_MATRIX& sub_matrix, double fkt = 1.0);
/// get value. This function returns zero if the element doesn't exist.
double get(IndexType row, IndexType col) const
......@@ -133,7 +137,7 @@ public:
static constexpr bool isAssembled() { return true; }
/// printout this matrix for debugging
void write(const std::string &filename) const
void write(const std::string& filename) const
{
std::ofstream of(filename);
if (of)
......@@ -143,7 +147,7 @@ public:
}
/// printout this matrix for debugging
void write(std::ostream &os) const
void write(std::ostream& os) const
{
for (int k = 0; k < mat_.outerSize(); ++k)
{
......@@ -169,9 +173,11 @@ void EigenMatrix::add(std::vector<IndexType> const& row_pos,
{
auto const n_rows = row_pos.size();
auto const n_cols = col_pos.size();
for (auto i = decltype(n_rows){0}; i < n_rows; i++) {
for (auto i = decltype(n_rows){0}; i < n_rows; i++)
{
auto const row = row_pos[i];
for (auto j = decltype(n_cols){0}; j < n_cols; j++) {
for (auto j = decltype(n_cols){0}; j < n_cols; j++)
{
auto const col = col_pos[j];
add(row, col, fkt * sub_matrix(i, j));
}
......@@ -182,21 +188,20 @@ void EigenMatrix::add(std::vector<IndexType> const& row_pos,
template <typename SPARSITY_PATTERN>
struct SetMatrixSparsity<EigenMatrix, SPARSITY_PATTERN>
{
/// \note This operator relies on row-major storage order of the underlying
/// eigen matrix i.e. of the RawMatrixType.
void operator()(EigenMatrix& matrix,
SPARSITY_PATTERN const& sparsity_pattern) const
{
static_assert(EigenMatrix::RawMatrixType::IsRowMajor,
"Set matrix sparsity relies on the EigenMatrix to be in "
"row-major storage order.");
/// \note This operator relies on row-major storage order of the underlying
/// eigen matrix i.e. of the RawMatrixType.
void operator()(EigenMatrix& matrix,
SPARSITY_PATTERN const& sparsity_pattern) const
{
static_assert(EigenMatrix::RawMatrixType::IsRowMajor,
"Set matrix sparsity relies on the EigenMatrix to be in "
"row-major storage order.");
assert(matrix.getNumberOfRows()
== static_cast<EigenMatrix::IndexType>(sparsity_pattern.size()));
assert(matrix.getNumberOfRows() ==
static_cast<EigenMatrix::IndexType>(sparsity_pattern.size()));
matrix.getRawMatrix().reserve(sparsity_pattern);
}
matrix.getRawMatrix().reserve(sparsity_pattern);
}
};
} // end namespace MathLib
} // end namespace MathLib
......@@ -14,7 +14,6 @@
namespace MathLib
{
/// \brief Option for Eigen sparse solver
struct EigenOption final
{
......@@ -63,23 +62,21 @@ struct EigenOption final
/// @return a linear solver type
/// If there is no solver type matched with the given name, INVALID
/// is returned.
static SolverType getSolverType(const std::string &solver_name);
static SolverType getSolverType(const std::string& solver_name);
/// return a preconditioner type from the name
///
/// @param precon_name
/// @return a preconditioner type
/// If there is no preconditioner type matched with the given name, NONE
/// is returned.
static PreconType getPreconType(const std::string &precon_name);
/// If there is no preconditioner type matched with the given name,
/// NONE is returned.
static PreconType getPreconType(const std::string& precon_name);
/// return a linear solver name from the solver type
static std::string getSolverName(SolverType const solver_type);
/// return a preconditioner name from the preconditioner type
static std::string getPreconName(PreconType const precon_type);
};
} // namespace MathLib
......@@ -13,7 +13,7 @@
#include <vector>
#include "BaseLib/Error.h"
#include "EigenMatrix.h" // for EigenMatrix::IndexType
#include "EigenMatrix.h" // for EigenMatrix::IndexType
namespace MathLib
{
......@@ -30,8 +30,9 @@ class EigenVector;
* entries to enforce some conditions, value ignored in the current
* implementation
*/
void applyKnownSolution(EigenMatrix &A, EigenVector &b, EigenVector &/*x*/,
const std::vector<EigenMatrix::IndexType> &vec_knownX_id,
const std::vector<double> &vec_knownX_x, double penalty_scaling = 1e+10);
void applyKnownSolution(
EigenMatrix& A, EigenVector& b, EigenVector& /*x*/,
const std::vector<EigenMatrix::IndexType>& vec_knownX_id,
const std::vector<double>& vec_knownX_x, double penalty_scaling = 1e+10);
} // namespace MathLib
......@@ -21,7 +21,6 @@
namespace MathLib
{
/// Global vector based on Eigen vector
class EigenVector final
{
......@@ -81,11 +80,12 @@ public:
void add(IndexType rowId, double v) { vec_[rowId] += v; }
/// add entries
template<class T_SUBVEC>
void add(const std::vector<IndexType> &pos, const T_SUBVEC &sub_vec)
template <class T_SUBVEC>
void add(const std::vector<IndexType>& pos, const T_SUBVEC& sub_vec)
{
auto const length = pos.size();
for (std::size_t i=0; i<length; ++i) {
for (std::size_t i = 0; i < length; ++i)
{
add(pos[i], sub_vec[i]);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment