4#ifndef DUNE_ISTL_SOLVER_HH
5#define DUNE_ISTL_SOLVER_HH
12#include <dune/common/exceptions.hh>
13#include <dune/common/shared_ptr.hh>
14#include <dune/common/simd/io.hh>
15#include <dune/common/simd/simd.hh>
16#include <dune/common/parametertree.hh>
17#include <dune/common/timer.hh>
96 template<
class X,
class Y>
109 typedef typename FieldTraits<field_type>::real_type
real_type;
145#ifdef DUNE_ISTL_SUPPORT_OLD_CATEGORY_INTERFACE
147 DUNE_THROW(Dune::Exception,
"It is necessary to implement the category method in a derived classes, in the future this method will pure virtual.");
165 s << std::setw(
normSpacing) <<
"Rate" << std::endl;
169 template <
typename CountType,
typename DataType>
171 const CountType& iter,
172 const DataType& norm,
173 const DataType& norm_old)
const
175 const DataType rate = norm/norm_old;
177 s << std::setw(
normSpacing) << Simd::io(norm) <<
" ";
178 s << std::setw(
normSpacing) << Simd::io(rate) << std::endl;
182 template <
typename CountType,
typename DataType>
184 const CountType& iter,
185 const DataType& norm)
const
188 s << std::setw(
normSpacing) << Simd::io(norm) << std::endl;
200 template<
class X,
class Y>
229 _op(stackobject_to_shared_ptr(op)),
230 _prec(stackobject_to_shared_ptr(prec)),
262 _op(stackobject_to_shared_ptr(op)),
263 _prec(stackobject_to_shared_ptr(prec)),
264 _sp(stackobject_to_shared_ptr(sp)),
268 DUNE_THROW(
InvalidSolverCategory,
"LinearOperator and Preconditioner must have the same SolverCategory!");
270 DUNE_THROW(
InvalidSolverCategory,
"LinearOperator and ScalarProduct must have the same SolverCategory!");
291 configuration.
get<int>(
"maxit"),
292 configuration.
get<int>(
"verbose"))
314 configuration.
get<int>(
"maxit"),
315 configuration.
get<int>(
"verbose"))
349 DUNE_THROW(
InvalidSolverCategory,
"LinearOperator and Preconditioner must have the same SolverCategory!");
351 DUNE_THROW(
InvalidSolverCategory,
"LinearOperator and ScalarProduct must have the same SolverCategory!");
376 this->
apply(x,b,res);
387 std::string
name = className(*
this);
388 return name.substr(0,
name.find(
"<"));
408 template<
class CountType =
unsigned int>
419 std::cout <<
"=== " << parent.
name() << std::endl;
435 other._valid =
false;
454 if (!Simd::allTrue(isFinite(def)))
457 std::cout <<
"=== " <<
_parent.
name() <<
": abort due to infinite or NaN defect"
461 <<
" is infinite or NaN");
502 std::shared_ptr<LinearOperator<X,Y>>
_op;
503 std::shared_ptr<Preconditioner<X,Y>>
_prec;
504 std::shared_ptr<ScalarProduct<X>>
_sp;
518 template <
typename ISTLLinearSolver,
typename BCRSMatrix>
525 static const bool is_direct_solver
536 template <
bool is_direct_solver,
typename Dummy =
void>
548 template <
typename Dummy>
554 solver.setMatrix(matrix);
Define general, extensible interface for operators. The available implementation wraps a matrix.
Define base class for scalar product and norm.
Templates characterizing the type of a solver.
Definition: allocator.hh:9
PropertyMapTypeSelector< Amg::VertexVisitedTag, Amg::PropertiesGraph< G, Amg::VertexProperties, EP, VM, EM > >::Type get(const Amg::VertexVisitedTag &tag, Amg::PropertiesGraph< G, Amg::VertexProperties, EP, VM, EM > &graph)
Definition: dependency.hh:291
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:464
Thrown when a solver aborts due to some problem.
Definition: istlexception.hh:53
A linear operator.
Definition: operators.hh:65
Base class for matrix free definition of preconditioners.
Definition: preconditioner.hh:30
Base class for scalar product and norm computation.
Definition: scalarproducts.hh:50
Default implementation for the scalar case.
Definition: scalarproducts.hh:166
Statistics about the application of an inverse operator.
Definition: solver.hh:46
InverseOperatorResult()
Default constructor.
Definition: solver.hh:48
double condition_estimate
Estimate of condition number.
Definition: solver.hh:77
double elapsed
Elapsed time in seconds.
Definition: solver.hh:80
int iterations
Number of iterations.
Definition: solver.hh:65
double reduction
Reduction achieved: .
Definition: solver.hh:68
void clear()
Resets all data.
Definition: solver.hh:54
double conv_rate
Convergence rate (average reduction per step)
Definition: solver.hh:74
bool converged
True if convergence criterion has been met.
Definition: solver.hh:71
Abstract base class for all solvers.
Definition: solver.hh:97
void printHeader(std::ostream &s) const
helper function for printing header of solver output
Definition: solver.hh:161
virtual ~InverseOperator()
Destructor.
Definition: solver.hh:154
void printOutput(std::ostream &s, const CountType &iter, const DataType &norm) const
helper function for printing solver output
Definition: solver.hh:183
void printOutput(std::ostream &s, const CountType &iter, const DataType &norm, const DataType &norm_old) const
helper function for printing solver output
Definition: solver.hh:170
virtual void apply(X &x, Y &b, double reduction, InverseOperatorResult &res)=0
apply inverse operator, with given convergence criteria.
Simd::Scalar< real_type > scalar_real_type
scalar type underlying the field_type
Definition: solver.hh:112
Y range_type
Type of the range of the operator to be inverted.
Definition: solver.hh:103
@ normSpacing
Definition: solver.hh:158
@ iterationSpacing
Definition: solver.hh:158
X domain_type
Type of the domain of the operator to be inverted.
Definition: solver.hh:100
virtual void apply(X &x, Y &b, InverseOperatorResult &res)=0
Apply inverse operator,.
X::field_type field_type
The field type of the operator.
Definition: solver.hh:106
FieldTraits< field_type >::real_type real_type
The real type of the field type (is the same if using real numbers, but differs for std::complex)
Definition: solver.hh:109
virtual SolverCategory::Category category() const =0
Category of the solver (see SolverCategory::Category)
Base class for all implementations of iterative solvers.
Definition: solver.hh:201
IterativeSolver(std::shared_ptr< LinearOperator< X, Y > > op, std::shared_ptr< ScalarProduct< X > > sp, std::shared_ptr< Preconditioner< X, Y > > prec, scalar_real_type reduction, int maxit, int verbose)
General constructor to initialize an iterative solver.
Definition: solver.hh:338
IterativeSolver(LinearOperator< X, Y > &op, ScalarProduct< X > &sp, Preconditioner< X, Y > &prec, scalar_real_type reduction, int maxit, int verbose)
General constructor to initialize an iterative solver.
Definition: solver.hh:260
virtual void apply(X &x, X &b, double reduction, InverseOperatorResult &res)
Apply inverse operator with given reduction factor.
Definition: solver.hh:372
std::string name() const
Definition: solver.hh:386
std::shared_ptr< LinearOperator< X, Y > > _op
Definition: solver.hh:502
std::shared_ptr< ScalarProduct< X > > _sp
Definition: solver.hh:504
IterativeSolver(std::shared_ptr< LinearOperator< X, Y > > op, std::shared_ptr< Preconditioner< X, X > > prec, const ParameterTree &configuration)
Constructor.
Definition: solver.hh:288
int _maxit
Definition: solver.hh:506
IterativeSolver(LinearOperator< X, Y > &op, Preconditioner< X, Y > &prec, scalar_real_type reduction, int maxit, int verbose)
General constructor to initialize an iterative solver.
Definition: solver.hh:228
int _verbose
Definition: solver.hh:507
scalar_real_type _reduction
Definition: solver.hh:505
SolverCategory::Category _category
Definition: solver.hh:508
IterativeSolver(std::shared_ptr< LinearOperator< X, Y > > op, std::shared_ptr< ScalarProduct< X > > sp, std::shared_ptr< Preconditioner< X, X > > prec, const ParameterTree &configuration)
Constructor.
Definition: solver.hh:311
std::shared_ptr< Preconditioner< X, Y > > _prec
Definition: solver.hh:503
virtual SolverCategory::Category category() const
Category of the solver (see SolverCategory::Category)
Definition: solver.hh:381
Class for controlling iterative methods.
Definition: solver.hh:409
Iteration(const IterativeSolver &parent, InverseOperatorResult &res)
Definition: solver.hh:411
Iteration(Iteration &&other)
Definition: solver.hh:426
InverseOperatorResult & _res
Definition: solver.hh:496
const IterativeSolver & _parent
Definition: solver.hh:497
Timer _watch
Definition: solver.hh:495
Iteration(const Iteration &)=delete
CountType _i
Definition: solver.hh:494
real_type _def0
Definition: solver.hh:493
bool step(CountType i, real_type def)
registers the iteration step, checks for invalid defect norm and convergence.
Definition: solver.hh:453
void finalize()
Definition: solver.hh:478
real_type _def
Definition: solver.hh:493
~Iteration()
Definition: solver.hh:438
bool _valid
Definition: solver.hh:498
Helper class for notifying a DUNE-ISTL linear solver about a change of the iteration matrix object in...
Definition: solver.hh:520
static void setMatrix(ISTLLinearSolver &solver, const BCRSMatrix &matrix)
Definition: solver.hh:522
Implementation that works together with iterative ISTL solvers, e.g. Dune::CGSolver or Dune::BiCGSTAB...
Definition: solver.hh:538
static void setMatrix(ISTLLinearSolver &, const BCRSMatrix &)
Definition: solver.hh:539
static void setMatrix(ISTLLinearSolver &solver, const BCRSMatrix &matrix)
Definition: solver.hh:551
Categories for the solvers.
Definition: solvercategory.hh:20
Category
Definition: solvercategory.hh:21
@ sequential
Category for sequential solvers.
Definition: solvercategory.hh:23
static Category category(const OP &op, decltype(op.category()) *=nullptr)
Helperfunction to extract the solver category either from an enum, or from the newly introduced virtu...
Definition: solvercategory.hh:32
Definition: solvercategory.hh:52
Definition: solvertype.hh:14