14#include <dune/common/exceptions.hh>
17#include <Eigen/SparseCore>
61 template <
typename ScalarType =
double>
107 virtual ~SolverBase() =
default;
114 virtual void solve(Eigen::VectorX<ScalarType>& x,
const Eigen::VectorX<ScalarType>&)
const = 0;
115 virtual void solve(Eigen::MatrixX2<ScalarType>& x,
const Eigen::MatrixX2<ScalarType>&)
const = 0;
116 virtual void solve(Eigen::MatrixX3<ScalarType>& x,
const Eigen::MatrixX3<ScalarType>&)
const = 0;
117 virtual void solve(Eigen::MatrixX<ScalarType>& x,
const Eigen::MatrixX<ScalarType>&)
const = 0;
120 template <
typename Solver>
121 struct SolverImpl :
public SolverBase {
122 using SolverBase::analyzePattern;
125 if constexpr (
requires(Solver sol) { sol.analyzePattern(A); }) solver.analyzePattern(A);
129 if constexpr (
requires(Solver sol) { sol.factorize(A); }) solver.factorize(A);
135 if constexpr (
requires(Solver sol) { sol.compute(A); } && std::is_base_of_v<Eigen::SolverBase<Solver>, Solver>)
139 if constexpr (std::is_base_of_v<Eigen::SparseSolverBase<Solver>, Solver>)
142 DUNE_THROW(Dune::NotImplemented,
"This solver does not support solving with sparse matrices.");
146 if constexpr (std::is_base_of_v<Eigen::SolverBase<Solver>, Solver>)
149 DUNE_THROW(Dune::NotImplemented,
"This solver does not support solving with dense matrices.");
152 void solve(Eigen::VectorX<ScalarType>& x,
const Eigen::VectorX<ScalarType>& b)
const final {
156 void solve(Eigen::MatrixX2<ScalarType>& x,
const Eigen::MatrixX2<ScalarType>& b)
const final {
160 void solve(Eigen::MatrixX3<ScalarType>& x,
const Eigen::MatrixX3<ScalarType>& b)
const final {
164 void solve(Eigen::MatrixX<ScalarType>& x,
const Eigen::MatrixX<ScalarType>& b)
const final {
171 std::unique_ptr<SolverBase> solverimpl;
181 template <
typename MatrixType>
182 requires std::is_same_v<MatrixType, DenseMatrixType> || std::is_same_v<MatrixType, SparseMatrixType>
193 template <
typename MatrixType>
194 requires std::is_same_v<MatrixType, DenseMatrixType> || std::is_same_v<MatrixType, SparseMatrixType>
195 inline void analyzePattern(
const MatrixType& A) { solverimpl->analyzePattern(A); }
202 template <
typename MatrixType>
203 requires std::is_same_v<MatrixType, DenseMatrixType> || std::is_same_v<MatrixType, SparseMatrixType>
204 inline void factorize(
const MatrixType& A) { solverimpl->factorize(A); }
211 void solve(Eigen::VectorX<ScalarType>& x,
const Eigen::VectorX<ScalarType>& b) { solverimpl->solve(x, b); }
218 void solve(Eigen::MatrixX3<ScalarType>& x,
const Eigen::MatrixX3<ScalarType>& b) { solverimpl->solve(x, b); }
224 void solve(Eigen::MatrixX2<ScalarType>& x,
const Eigen::MatrixX2<ScalarType>& b) { solverimpl->solve(x, b); }
231 void solve(Eigen::MatrixX<ScalarType>& x,
const Eigen::MatrixX<ScalarType>& b) { solverimpl->solve(x, b); }
Definition: simpleassemblers.hh:21
MatrixTypeTag
Enumeration representing different matrix types (Dense or Sparse).
Definition: linearsolver.hh:53
SolverTypeTag
Enumeration representing different solver types.
Definition: linearsolver.hh:27
@ sd_CholmodSupernodalLLT
@ si_LeastSquaresConjugateGradient
@ d_CompleteOrthogonalDecomposition
LinearSolverTemplate< double > LinearSolver
Definition: linearsolver.hh:234
A type-erased class which wraps most of the linear solvers available in Eigen.
Definition: linearsolver.hh:62
void solve(Eigen::MatrixX< ScalarType > &x, const Eigen::MatrixX< ScalarType > &b)
Solve the linear system for a n times n matrix.
Definition: linearsolver.hh:231
LinearSolverTemplate & operator=(LinearSolverTemplate &&other) noexcept=default
Move assignment operator.
void analyzePattern(const MatrixType &A)
Analyze the pattern of the matrix.
Definition: linearsolver.hh:195
LinearSolverTemplate & compute(const MatrixType &A)
Compute the factorization of the matrix.
Definition: linearsolver.hh:183
Eigen::SparseMatrix< ScalarType > SparseMatrixType
Definition: linearsolver.hh:64
LinearSolverTemplate(const SolverTypeTag &p_solverTypeTag)
Constructor for LinearSolverTemplate.
void solve(Eigen::VectorX< ScalarType > &x, const Eigen::VectorX< ScalarType > &b)
Solve the linear system for a vector.
Definition: linearsolver.hh:211
void solve(Eigen::MatrixX2< ScalarType > &x, const Eigen::MatrixX2< ScalarType > &b)
Solve the linear system for a n times 2 matrix.
Definition: linearsolver.hh:224
LinearSolverTemplate(const LinearSolverTemplate &other)
Copy constructor.
Definition: linearsolver.hh:92
Eigen::MatrixX< ScalarType > DenseMatrixType
Definition: linearsolver.hh:65
LinearSolverTemplate & operator=(const LinearSolverTemplate &other)
Copy assignment operator.
Definition: linearsolver.hh:83
LinearSolverTemplate(LinearSolverTemplate &&other) noexcept=default
Move constructor.
void factorize(const MatrixType &A)
Factorize the matrix.
Definition: linearsolver.hh:204
~LinearSolverTemplate()=default
Destructor for LinearSolverTemplate.
void solve(Eigen::MatrixX3< ScalarType > &x, const Eigen::MatrixX3< ScalarType > &b)
Solve the linear system for a n times 3 matrix.
Definition: linearsolver.hh:218