12#ifndef DUMUX_LINEAR_PDE_SOLVER_HH
13#define DUMUX_LINEAR_PDE_SOLVER_HH
20#include <dune/common/timer.hh>
21#include <dune/common/exceptions.hh>
22#include <dune/common/parallel/mpicommunication.hh>
23#include <dune/common/parallel/mpihelper.hh>
24#include <dune/common/std/type_traits.hh>
25#include <dune/istl/bvector.hh>
26#include <dune/istl/multitypeblockvector.hh>
36#define DUMUX_SUPPRESS_LINEAR_SOLVER_ACCEPTS_MULTITYPEMATRIX_WARNING
38#undef DUMUX_SUPPRESS_LINEAR_SOLVER_ACCEPTS_MULTITYPEMATRIX_WARNING
44template <
class Solver,
class Matrix>
45using SetMatrixDetector =
decltype(std::declval<Solver>().setMatrix(std::declval<std::shared_ptr<Matrix>>()));
47template<
class Solver,
class Matrix>
49{
return Dune::Std::is_detected<SetMatrixDetector, Solver, Matrix>::value; }
66 class Comm = Dune::Communication<Dune::MPIHelper::MPICommunicator>>
70 using Scalar =
typename Assembler::Scalar;
71 using JacobianMatrix =
typename Assembler::JacobianMatrix;
72 using SolutionVector =
typename Assembler::SolutionVector;
73 using ResidualVector =
typename Assembler::ResidualType;
77 static constexpr bool assemblerExportsVariables = Detail::PDESolver::assemblerExportsVariables<Assembler>;
88 const Communication& comm = Dune::MPIHelper::getCommunication(),
115 Dune::Timer assembleTimer(
false);
116 Dune::Timer solveTimer(
false);
117 Dune::Timer updateTimer(
false);
119 if (verbose_ && enableDynamicOutput_)
120 std::cout <<
"Assemble: r(x^k) = dS/dt + div F - q; M = grad r"
128 assembleTimer.start();
130 this->
assembler().assembleResidual(vars);
132 this->
assembler().assembleJacobianAndResidual(vars);
133 assembleTimer.stop();
142 const char clearRemainingLine[] = { 0x1b,
'[',
'K', 0 };
144 if (verbose_ && enableDynamicOutput_)
145 std::cout <<
"\rSolve: M deltax^k = r"
146 << clearRemainingLine << std::flush;
152 ResidualVector deltaU = LinearAlgebraNativeBackend::zeros(Backend::size(Backend::dofs(vars)));
156 bool converged = solveLinearSystem_(deltaU);
165 if (verbose_ && enableDynamicOutput_)
166 std::cout <<
"\rUpdate: x^(k+1) = x^k - deltax^k"
167 << clearRemainingLine << std::flush;
171 auto uCurrent = Backend::dofs(vars);
172 Backend::axpy(-1.0, deltaU, uCurrent);
173 Backend::update(vars, uCurrent);
174 if constexpr (!assemblerExportsVariables)
175 this->
assembler().updateGridVariables(Backend::dofs(vars));
180 const auto elapsedTot = assembleTimer.elapsed() + solveTimer.elapsed() + updateTimer.elapsed();
181 if (enableDynamicOutput_)
183 std::cout <<
"Assemble/solve/update time: "
184 << assembleTimer.elapsed() <<
"(" << 100*assembleTimer.elapsed()/elapsedTot <<
"%)/"
185 << solveTimer.elapsed() <<
"(" << 100*solveTimer.elapsed()/elapsedTot <<
"%)/"
186 << updateTimer.elapsed() <<
"(" << 100*updateTimer.elapsed()/elapsedTot <<
"%)"
195 void report(std::ostream& sout = std::cout)
const
217 {
return verbose_ ; }
223 {
return paramGroup_; }
233 reuseMatrix_ = reuse;
235 if constexpr (Detail::LinearPDESolver::linearSolverHasSetMatrix<LinearSolver, JacobianMatrix>())
242 virtual bool solveLinearSystem_(ResidualVector& deltaU)
244 if constexpr (Detail::LinearPDESolver::linearSolverHasSetMatrix<LinearSolver, JacobianMatrix>())
251 if (reuseMatrix_ && comm_.size() > 1)
252 DUNE_THROW(Dune::NotImplemented,
253 "Reuse matrix for parallel runs with a solver that doesn't support the setMatrix interface"
272 template<
class V = Res
idualVector>
273 typename std::enable_if_t<!isMultiTypeBlockVector<V>(),
bool>
279 return ls.solve(A, x, b);
292 template<
class LS = LinearSolver,
class V = Res
idualVector>
293 typename std::enable_if_t<linearSolverAcceptsMultiTypeMatrix<LS>() &&
294 isMultiTypeBlockVector<V>(),
bool>
301 return ls.solve(A, x, b);
314 template<
class LS = LinearSolver,
class V = Res
idualVector>
315 [[deprecated(
"After 3.7 LinearPDESolver will no longer support conversion of multitype matrices for solvers that don't support this feature!")]]
316 typename std::enable_if_t<!linearSolverAcceptsMultiTypeMatrix<LS>() &&
317 isMultiTypeBlockVector<V>(),
bool>
329 const std::size_t numRows = M.N();
330 assert(numRows == M.M());
334 assert(bTmp.size() == numRows);
337 using VectorBlock =
typename Dune::FieldVector<Scalar, 1>;
338 using BlockVector =
typename Dune::BlockVector<VectorBlock>;
339 BlockVector y(numRows);
342 const bool converged = ls.solve(M, y, bTmp);
352 void initParams_(
const std::string& group =
"")
354 verbose_ = (Dune::MPIHelper::getCommunication().rank() == 0);
355 enableDynamicOutput_ = getParamFromGroup<bool>(group,
"LinearPDESolver.EnableDynamicOutput",
true);
362 bool enableDynamicOutput_;
365 std::string paramGroup_;
Definition: variablesbackend.hh:159
An implementation of a linear PDE solver.
Definition: linear/pdesolver.hh:68
LinearPDESolver(std::shared_ptr< Assembler > assembler, std::shared_ptr< LinearSolver > linearSolver, const std::string ¶mGroup)
The Constructor.
Definition: linear/pdesolver.hh:104
bool verbose() const
Returns true if the solver ought to be chatty.
Definition: linear/pdesolver.hh:216
void reuseMatrix(bool reuse=true)
Set whether the matrix should be reused.
Definition: linear/pdesolver.hh:231
void solve(Variables &vars) override
Solve a linear PDE system.
Definition: linear/pdesolver.hh:113
LinearPDESolver(std::shared_ptr< Assembler > assembler, std::shared_ptr< LinearSolver > linearSolver, const Communication &comm=Dune::MPIHelper::getCommunication(), const std::string ¶mGroup="")
The Constructor.
Definition: linear/pdesolver.hh:86
Comm Communication
Definition: linear/pdesolver.hh:81
const std::string & paramGroup() const
Returns the parameter group.
Definition: linear/pdesolver.hh:222
Scalar suggestTimeStepSize(Scalar oldTimeStep) const
Suggest a new time-step size based on the old time-step size.
Definition: linear/pdesolver.hh:202
void report(std::ostream &sout=std::cout) const
output statistics / report
Definition: linear/pdesolver.hh:195
void setVerbose(bool val)
Specifies if the solver ought to be chatty.
Definition: linear/pdesolver.hh:210
Base class for linear solvers.
Definition: solver.hh:27
static auto multiTypeToBCRSMatrix(const MultiTypeBlockMatrix &A)
Converts the matrix to a type the IterativeSolverBackend can handle.
Definition: matrixconverter.hh:46
Exception thrown if a fixable numerical problem occurs.
Definition: exceptions.hh:27
A high-level interface for a PDESolver.
Definition: common/pdesolver.hh:61
LinearSolver LinearSolver
Definition: common/pdesolver.hh:68
bool checkSizesOfSubMatrices(const Dune::MultiTypeBlockMatrix< FirstRow, Args... > &matrix) const
Helper function to assure the MultiTypeBlockMatrix's sub-blocks have the correct sizes.
Definition: common/pdesolver.hh:137
const LinearSolver & linearSolver() const
Access the linear solver.
Definition: common/pdesolver.hh:122
const Assembler & assembler() const
Access the assembler.
Definition: common/pdesolver.hh:110
Detail::PDESolver::AssemblerVariables< Assembler > Variables
export the type of variables that represent a numerical solution
Definition: common/pdesolver.hh:71
Manages the handling of time dependent problems.
Definition: common/timeloop.hh:56
static void retrieveValues(MultiTypeBlockVector &x, const BlockVector &y)
Copies the entries of a Dune::BlockVector to a Dune::MultiTypeBlockVector.
Definition: matrixconverter.hh:229
static auto multiTypeToBlockVector(const MultiTypeBlockVector &b)
Converts a Dune::MultiTypeBlockVector to a plain 1x1 Dune::BlockVector.
Definition: matrixconverter.hh:203
Defines a high-level interface for a PDESolver.
Manages the handling of time dependent problems.
Some exceptions thrown in DuMux
Trait checking if linear solvers accept Dune::MultiTypeBlockMatrix or we need to convert the matrix.
A helper class that converts a Dune::MultiTypeBlockMatrix into a plain Dune::BCRSMatrix.
Definition: linear/pdesolver.hh:42
decltype(std::declval< Solver >().setMatrix(std::declval< std::shared_ptr< Matrix > >())) SetMatrixDetector
Definition: linear/pdesolver.hh:45
static constexpr bool linearSolverHasSetMatrix()
Definition: linear/pdesolver.hh:48
Definition: common/pdesolver.hh:24
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
Backends for operations on different solution vector types or more generic variable classes to be use...
Type traits to be used with vector types.