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::getCommunication().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::getCommunication().size() > 1)
154 initializeParameters_();
157 parallelHelper_ = std::make_unique<ParallelISTLHelper<LinearSolverTraits>>(gridView, dofMapper);
168 const typename LinearSolverTraits::DofMapper& dofMapper)
172 parallelHelper_ = std::make_unique<ParallelISTLHelper<LinearSolverTraits>>(gridView, dofMapper);
183 template<
class Matrix,
class Vector>
184 bool solve(Matrix& A, Vector& x, Vector& b)
187 solveSequentialOrParallel_(A, x, b);
189 solveSequential_(A, x, b);
192 return result_.converged;
195 const Dune::InverseOperatorResult&
result()
const
200 const std::string&
name()
const
207 void initializeParameters_()
210 checkMandatoryParameters_();
211 name_ = params_.get<std::string>(
"preconditioner.type") +
"-preconditioned " + params_.get<std::string>(
"type");
212 if (params_.get<
int>(
"verbose", 0) > 0)
213 std::cout <<
"Initialized linear solver of type: " << name_ << std::endl;
216 void checkMandatoryParameters_()
218 if (!params_.hasKey(
"type"))
219 DUNE_THROW(Dune::InvalidStateException,
"Solver factory needs \"LinearSolver.Type\" parameter to select the solver");
221 if (!params_.hasKey(
"preconditioner.type"))
222 DUNE_THROW(Dune::InvalidStateException,
"Solver factory needs \"LinearSolver.Preconditioner.Type\" parameter to select the preconditioner");
226 template<
class Matrix,
class Vector>
227 void solveSequentialOrParallel_(Matrix& A, Vector& x, Vector& b)
236 if (LinearSolverTraits::isNonOverlapping(parallelHelper_->gridView()))
238 using PTraits =
typename LinearSolverTraits::template ParallelNonoverlapping<Matrix, Vector>;
239 solveParallel_<PTraits>(A, x, b);
243 using PTraits =
typename LinearSolverTraits::template ParallelOverlapping<Matrix, Vector>;
244 solveParallel_<PTraits>(A, x, b);
248 solveSequential_(A, x, b);
252 solveSequential_(A, x, b);
256 template<
class ParallelTraits,
class Matrix,
class Vector>
257 void solveParallel_(Matrix& A, Vector& x, Vector& b)
259#if DUNE_VERSION_GTE(DUNE_ISTL,2,8)
260 using Comm =
typename ParallelTraits::Comm;
261 using LinearOperator =
typename ParallelTraits::LinearOperator;
262 using ScalarProduct =
typename ParallelTraits::ScalarProduct;
265 initSolverFactories<Matrix, LinearOperator>();
267 std::shared_ptr<Comm> comm;
268 std::shared_ptr<LinearOperator> linearOperator;
269 std::shared_ptr<ScalarProduct> scalarProduct;
270 prepareLinearAlgebraParallel<LinearSolverTraits, ParallelTraits>(A, b, comm, linearOperator, scalarProduct, *parallelHelper_);
273 auto solver = getSolverFromFactory_(linearOperator);
276 solver->apply(x, b, result_);
278 DUNE_THROW(Dune::NotImplemented,
"Parallel solvers only available for dune-istl > 2.7.0");
283 template<
class Matrix,
class Vector>
284 void solveSequential_(Matrix& A, Vector& x, Vector& b)
287 using Traits =
typename LinearSolverTraits::template Sequential<Matrix, Vector>;
288 using LinearOperator =
typename Traits::LinearOperator;
289 auto linearOperator = std::make_shared<LinearOperator>(A);
292 initSolverFactories<Matrix, LinearOperator>();
295 auto solver = getSolverFromFactory_(linearOperator);
298 solver->apply(x, b, result_);
301 template<
class LinearOperator>
302 auto getSolverFromFactory_(std::shared_ptr<LinearOperator>& fop)
304 try {
return Dune::getSolverFromFactory(fop, params_); }
305 catch(Dune::Exception& e)
307 std::cerr <<
"Could not create solver with factory" << std::endl;
308 std::cerr << e.what() << std::endl;
313 const std::string paramGroup_;
315 std::unique_ptr<ParallelISTLHelper<LinearSolverTraits>> parallelHelper_;
317 bool isParallel_ =
false;
320 Dune::InverseOperatorResult result_;
321 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 ...
Dumux preconditioners for iterative solvers.
Provides a helper class for nonoverlapping decomposition.
Generates a parameter tree required for the linear solvers and precondioners of the Dune ISTL.
Base class for linear solvers.
void initSolverFactories()
Initialize the solver factories for regular matrices or MultiTypeBlockMatrices.
Definition: istlsolverfactorybackend.hh:94
static constexpr bool canCommunicate
Definition: gridcapabilities.hh:63
Definition: common/pdesolver.hh:36
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:200
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:184
const Dune::InverseOperatorResult & result() const
Definition: istlsolverfactorybackend.hh:195
void updateAfterGridAdaption(const typename LinearSolverTraits::GridView &gridView, const typename LinearSolverTraits::DofMapper &dofMapper)
Update the solver after grid adaption.
Definition: istlsolverfactorybackend.hh:167
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:49
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