14#include <dune/common/exceptions.hh>
17#include <Eigen/SparseCore>
66template <
typename ST =
double>
115 virtual ~SolverBase() =
default;
122 virtual void solve(Eigen::VectorX<ScalarType>& x,
const Eigen::VectorX<ScalarType>&)
const = 0;
123 virtual void solve(Eigen::MatrixX2<ScalarType>& x,
const Eigen::MatrixX2<ScalarType>&)
const = 0;
124 virtual void solve(Eigen::MatrixX3<ScalarType>& x,
const Eigen::MatrixX3<ScalarType>&)
const = 0;
125 virtual void solve(Eigen::MatrixX<ScalarType>& x,
const Eigen::MatrixX<ScalarType>&)
const = 0;
128 template <
typename Solver>
129 struct SolverImpl :
public SolverBase
131 using SolverBase::analyzePattern;
134 if constexpr (
requires(Solver sol) { sol.analyzePattern(A); })
135 solver.analyzePattern(A);
139 if constexpr (
requires(Solver sol) { sol.factorize(A); })
146 if constexpr (
requires(Solver sol) { sol.compute(A); } && std::is_base_of_v<Eigen::SolverBase<Solver>, Solver>)
150 if constexpr (std::is_base_of_v<Eigen::SparseSolverBase<Solver>, Solver>)
153 DUNE_THROW(Dune::NotImplemented,
"This solver does not support solving with sparse matrices.");
157 if constexpr (std::is_base_of_v<Eigen::SolverBase<Solver>, Solver>)
160 DUNE_THROW(Dune::NotImplemented,
"This solver does not support solving with dense matrices.");
163 void solve(Eigen::VectorX<ScalarType>& x,
const Eigen::VectorX<ScalarType>& b)
const final { x = solver.solve(b); }
165 void solve(Eigen::MatrixX2<ScalarType>& x,
const Eigen::MatrixX2<ScalarType>& b)
const final {
169 void solve(Eigen::MatrixX3<ScalarType>& x,
const Eigen::MatrixX3<ScalarType>& b)
const final {
173 void solve(Eigen::MatrixX<ScalarType>& x,
const Eigen::MatrixX<ScalarType>& b)
const final { x = solver.solve(b); }
178 std::unique_ptr<SolverBase> solverimpl_;
188 template <
typename MatrixType>
189 requires std::is_same_v<MatrixType, DenseMatrixType> || std::is_same_v<MatrixType, SparseMatrixType>
200 template <
typename MatrixType>
201 requires std::is_same_v<MatrixType, DenseMatrixType> || std::is_same_v<MatrixType, SparseMatrixType>
203 solverimpl_->analyzePattern(A);
211 template <
typename MatrixType>
212 requires std::is_same_v<MatrixType, DenseMatrixType> || std::is_same_v<MatrixType, SparseMatrixType>
214 solverimpl_->factorize(A);
222 void solve(Eigen::VectorX<ScalarType>& x,
const Eigen::VectorX<ScalarType>& b) { solverimpl_->solve(x, b); }
229 void solve(Eigen::MatrixX3<ScalarType>& x,
const Eigen::MatrixX3<ScalarType>& b) { solverimpl_->solve(x, b); }
235 void solve(Eigen::MatrixX2<ScalarType>& x,
const Eigen::MatrixX2<ScalarType>& b) { solverimpl_->solve(x, b); }
242 void solve(Eigen::MatrixX<ScalarType>& x,
const Eigen::MatrixX<ScalarType>& b) { solverimpl_->solve(x, b); }
Definition: simpleassemblers.hh:22
MatrixTypeTag
Enumeration representing different matrix types (Dense or Sparse).
Definition: linearsolver.hh:55
SolverTypeTag
Enumeration representing different solver types.
Definition: linearsolver.hh:28
@ sd_CholmodSupernodalLLT
@ si_LeastSquaresConjugateGradient
@ d_CompleteOrthogonalDecomposition
A type-erased class which wraps most of the linear solvers available in Eigen.
Definition: linearsolver.hh:68
LinearSolverTemplate & compute(const MatrixType &A)
Compute the factorization of the matrix.
Definition: linearsolver.hh:190
LinearSolverTemplate(const SolverTypeTag &solverTypeTag)
Constructor for LinearSolverTemplate.
LinearSolverTemplate & operator=(LinearSolverTemplate &&other) noexcept=default
Move assignment operator.
LinearSolverTemplate(const LinearSolverTemplate &other)
Copy constructor.
Definition: linearsolver.hh:99
void solve(Eigen::MatrixX3< ScalarType > &x, const Eigen::MatrixX3< ScalarType > &b)
Solve the linear system for a n times 3 matrix.
Definition: linearsolver.hh:229
~LinearSolverTemplate()=default
Destructor for LinearSolverTemplate.
void solve(Eigen::MatrixX< ScalarType > &x, const Eigen::MatrixX< ScalarType > &b)
Solve the linear system for a n times n matrix.
Definition: linearsolver.hh:242
void analyzePattern(const MatrixType &A)
Analyze the pattern of the matrix.
Definition: linearsolver.hh:202
LinearSolverTemplate(LinearSolverTemplate &&other) noexcept=default
Move constructor.
Eigen::MatrixX< ScalarType > DenseMatrixType
Definition: linearsolver.hh:72
LinearSolverTemplate & operator=(const LinearSolverTemplate &other)
Copy assignment operator.
Definition: linearsolver.hh:90
ST ScalarType
Definition: linearsolver.hh:70
Eigen::SparseMatrix< ScalarType > SparseMatrixType
Definition: linearsolver.hh:71
void factorize(const MatrixType &A)
Factorize the matrix.
Definition: linearsolver.hh:213
void solve(Eigen::VectorX< ScalarType > &x, const Eigen::VectorX< ScalarType > &b)
Solve the linear system for a vector.
Definition: linearsolver.hh:222
void solve(Eigen::MatrixX2< ScalarType > &x, const Eigen::MatrixX2< ScalarType > &b)
Solve the linear system for a n times 2 matrix.
Definition: linearsolver.hh:235