26#ifndef DUMUX_LINEAR_ISTL_SOLVERFACTORYBACKEND_HH
27#define DUMUX_LINEAR_ISTL_SOLVERFACTORYBACKEND_HH
31#include <dune/common/version.hh>
32#include <dune/common/parallel/mpihelper.hh>
33#include <dune/common/parametertree.hh>
38#include <dune/istl/preconditioners.hh>
39#include <dune/istl/paamg/amg.hh>
43#include <dune/istl/solvers.hh>
44#include <dune/istl/solverfactory.hh>
65template<
class LinearOperator>
66int initSolverFactoriesForMultiTypeBlockMatrix()
68 using M =
typename LinearOperator::matrix_type;
69 using X =
typename LinearOperator::range_type;
70 using Y =
typename LinearOperator::domain_type;
71 using TL = Dune::TypeList<M,X,Y>;
72 auto& dsfac = Dune::DirectSolverFactory<M,X,Y>::instance();
73 Dune::addRegistryToFactory<TL>(dsfac, Dumux::MultiTypeBlockMatrixDirectSolverTag{});
74#if DUNE_VERSION_GTE(DUNE_ISTL,2,8)
75 auto& pfac = Dune::PreconditionerFactory<LinearOperator,X,Y>::instance();
77 auto& pfac = Dune::PreconditionerFactory<M,X,Y>::instance();
79 Dune::addRegistryToFactory<TL>(pfac, Dumux::MultiTypeBlockMatrixPreconditionerTag{});
80 using TLS = Dune::TypeList<X,Y>;
81 auto& isfac = Dune::IterativeSolverFactory<X,Y>::instance();
82 return Dune::addRegistryToFactory<TLS>(isfac, Dune::IterativeSolverTag{});
93template<
class Matrix,
class LinearOperator>
97 initSolverFactoriesForMultiTypeBlockMatrix<LinearOperator>();
99#if DUNE_VERSION_GTE(DUNE_ISTL,2,8)
100 Dune::initSolverFactories<LinearOperator>();
103 using X =
typename LinearOperator::range_type;
104 using Y =
typename LinearOperator::domain_type;
105 Dune::initSolverFactories<Matrix, X, Y>();
117template <
class LinearSolverTraits>
129 , isParallel_(
Dune::MPIHelper::getCollectiveCommunication().size() > 1)
132 DUNE_THROW(Dune::InvalidStateException,
"Using sequential constructor for parallel run. Use signature with gridView and dofMapper!");
135 initializeParameters_();
146 const typename LinearSolverTraits::DofMapper& dofMapper,
150 , isParallel_(
Dune::MPIHelper::getCollectiveCommunication().size() > 1)
154 initializeParameters_();
157 parallelHelper_ = std::make_unique<ParallelISTLHelper<LinearSolverTraits>>(gridView, dofMapper);
168 template<
class Matrix,
class Vector>
169 bool solve(Matrix& A, Vector& x, Vector& b)
172 solveSequentialOrParallel_(A, x, b);
174 solveSequential_(A, x, b);
177 return result_.converged;
180 const Dune::InverseOperatorResult&
result()
const
185 const std::string&
name()
const
192 void initializeParameters_()
195 checkMandatoryParameters_();
196 name_ = params_.get<std::string>(
"preconditioner.type") +
"-preconditioned " + params_.get<std::string>(
"type");
197 if (params_.get<
int>(
"verbose", 0) > 0)
198 std::cout <<
"Initialized linear solver of type: " << name_ << std::endl;
201 void checkMandatoryParameters_()
203 if (!params_.hasKey(
"type"))
204 DUNE_THROW(Dune::InvalidStateException,
"Solver factory needs \"LinearSolver.Type\" parameter to select the solver");
206 if (!params_.hasKey(
"preconditioner.type"))
207 DUNE_THROW(Dune::InvalidStateException,
"Solver factory needs \"LinearSolver.Preconditioner.Type\" parameter to select the preconditioner");
211 template<
class Matrix,
class Vector>
212 void solveSequentialOrParallel_(Matrix& A, Vector& x, Vector& b)
217 if constexpr (LinearSolverTraits::canCommunicate && !isMultiTypeBlockMatrix<Matrix>::value)
221 if (LinearSolverTraits::isNonOverlapping(parallelHelper_->gridView()))
223 using PTraits =
typename LinearSolverTraits::template ParallelNonoverlapping<Matrix, Vector>;
224 solveParallel_<PTraits>(A, x, b);
228 using PTraits =
typename LinearSolverTraits::template ParallelOverlapping<Matrix, Vector>;
229 solveParallel_<PTraits>(A, x, b);
233 solveSequential_(A, x, b);
237 solveSequential_(A, x, b);
241 template<
class ParallelTraits,
class Matrix,
class Vector>
242 void solveParallel_(Matrix& A, Vector& x, Vector& b)
244#if DUNE_VERSION_GTE(DUNE_ISTL,2,8)
245 using Comm =
typename ParallelTraits::Comm;
246 using LinearOperator =
typename ParallelTraits::LinearOperator;
247 using ScalarProduct =
typename ParallelTraits::ScalarProduct;
250 initSolverFactories<Matrix, LinearOperator>();
252 std::shared_ptr<Comm> comm;
253 std::shared_ptr<LinearOperator> linearOperator;
254 std::shared_ptr<ScalarProduct> scalarProduct;
255 prepareLinearAlgebraParallel<LinearSolverTraits, ParallelTraits>(A, b, comm, linearOperator, scalarProduct, *parallelHelper_);
258 auto solver = getSolverFromFactory_(linearOperator);
261 solver->apply(x, b, result_);
263 DUNE_THROW(Dune::NotImplemented,
"Parallel solvers only available for dune-istl > 2.7.0");
268 template<
class Matrix,
class Vector>
269 void solveSequential_(Matrix& A, Vector& x, Vector& b)
272 using Traits =
typename LinearSolverTraits::template Sequential<Matrix, Vector>;
273 using LinearOperator =
typename Traits::LinearOperator;
274 auto linearOperator = std::make_shared<LinearOperator>(A);
277 initSolverFactories<Matrix, LinearOperator>();
280 auto solver = getSolverFromFactory_(linearOperator);
283 solver->apply(x, b, result_);
286 template<
class LinearOperator>
287 auto getSolverFromFactory_(std::shared_ptr<LinearOperator>& fop)
289 try {
return Dune::getSolverFromFactory(fop, params_); }
290 catch(Dune::Exception& e)
292 std::cerr <<
"Could not create solver with factory" << std::endl;
293 std::cerr << e.what() << std::endl;
298 const std::string paramGroup_;
300 std::unique_ptr<ParallelISTLHelper<LinearSolverTraits>> parallelHelper_;
302 bool isParallel_ =
false;
305 Dune::InverseOperatorResult result_;
306 Dune::ParameterTree params_;
Type traits to be used with matrix types.
The specialized Dumux macro and tag for the ISTL registry to choose the solver and preconditioner at ...
Generates a parameter tree required for the linear solvers and precondioners of the Dune ISTL.
Provides a helper class for nonoverlapping decomposition.
Dumux preconditioners for iterative solvers.
Base class for linear solvers.
void initSolverFactories()
Initialize the solver factories for regular matrices or MultiTypeBlockMatrices.
Definition: istlsolverfactorybackend.hh:94
Definition: common/pdesolver.hh:35
Helper type to determine whether a given type is a Dune::MultiTypeBlockMatrix.
Definition: matrix.hh:49
A linear solver using the dune-istl solver factory to choose the solver and preconditioner at runtime...
Definition: istlsolverfactorybackend.hh:119
const std::string & name() const
Definition: istlsolverfactorybackend.hh:185
IstlSolverFactoryBackend(const typename LinearSolverTraits::GridView &gridView, const typename LinearSolverTraits::DofMapper &dofMapper, const std::string ¶mGroup="")
Construct the backend for parallel or sequential runs.
Definition: istlsolverfactorybackend.hh:145
IstlSolverFactoryBackend(const std::string ¶mGroup="")
Construct the backend for the sequential case only.
Definition: istlsolverfactorybackend.hh:127
bool solve(Matrix &A, Vector &x, Vector &b)
Solve a linear system.
Definition: istlsolverfactorybackend.hh:169
const Dune::InverseOperatorResult & result() const
Definition: istlsolverfactorybackend.hh:180
static Dune::ParameterTree createParameterTree(const std::string ¶mGroup="")
Create a tree containing parameters required for the linear solvers and precondioners of the Dune IST...
Definition: linearsolverparameters.hh:46
Base class for linear solvers.
Definition: solver.hh:37
const std::string & paramGroup() const
the parameter group for getting parameter from the parameter tree
Definition: solver.hh:79