4 #ifndef DUNE_ISTL_SOLVER_HH 5 #define DUNE_ISTL_SOLVER_HH 10 #include <dune/common/exceptions.hh> 11 #include <dune/common/shared_ptr.hh> 12 #include <dune/common/simd.hh> 90 template<
class X,
class Y>
103 typedef typename FieldTraits<field_type>::real_type
real_type;
139 #ifdef DUNE_ISTL_SUPPORT_OLD_CATEGORY_INTERFACE 141 DUNE_THROW(Dune::Exception,
"It is necessary to implement the category method in a derived classes, in the future this method will pure virtual.");
152 enum { iterationSpacing = 5 , normSpacing = 16 };
157 s << std::setw(iterationSpacing) <<
" Iter";
158 s << std::setw(normSpacing) <<
"Defect";
159 s << std::setw(normSpacing) <<
"Rate" << std::endl;
163 template <
typename CountType,
typename DataType>
165 const CountType& iter,
166 const DataType& norm,
167 const DataType& norm_old)
const 169 const DataType rate = norm/norm_old;
170 s << std::setw(iterationSpacing) << iter <<
" ";
171 s << std::setw(normSpacing) << norm <<
" ";
172 s << std::setw(normSpacing) << rate << std::endl;
176 template <
typename CountType,
typename DataType>
178 const CountType& iter,
179 const DataType& norm)
const 181 s << std::setw(iterationSpacing) << iter <<
" ";
182 s << std::setw(normSpacing) << norm << std::endl;
194 template<
class X,
class Y>
223 _op(stackobject_to_shared_ptr(op)),
224 _prec(stackobject_to_shared_ptr(prec)),
226 _reduction(reduction), _maxit(maxit), _verbose(verbose), _category(
SolverCategory::sequential)
255 scalar_real_type
reduction,
int maxit,
int verbose) :
256 _op(stackobject_to_shared_ptr(op)),
257 _prec(stackobject_to_shared_ptr(prec)),
258 _sp(stackobject_to_shared_ptr(sp)),
259 _reduction(reduction), _maxit(maxit), _verbose(verbose), _category(
SolverCategory::category(op))
262 DUNE_THROW(
InvalidSolverCategory,
"LinearOperator and Preconditioner must have the same SolverCategory!");
264 DUNE_THROW(
InvalidSolverCategory,
"LinearOperator and ScalarProduct must have the same SolverCategory!");
287 scalar_real_type saved_reduction = _reduction;
289 this->apply(x,b,res);
290 _reduction = saved_reduction;
300 std::shared_ptr<LinearOperator<X,Y>>
_op;
301 std::shared_ptr<Preconditioner<X,Y>>
_prec;
302 std::shared_ptr<ScalarProduct<X>>
_sp;
316 template <
typename ISTLLinearSolver,
typename BCRSMatrix>
323 static const bool is_direct_solver
334 template <
bool is_direct_solver,
typename Dummy =
void>
346 template <
typename Dummy>
352 solver.setMatrix(matrix);
Base class for all implementations of iterative solvers.
Definition: solver.hh:195
static void setMatrix(ISTLLinearSolver &solver, const BCRSMatrix &matrix)
Definition: solver.hh:349
X::field_type field_type
The field type of the operator.
Definition: solver.hh:100
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:254
Implementation that works together with iterative ISTL solvers, e.g. Dune::CGSolver or Dune::BiCGSTAB...
Definition: solver.hh:335
Definition: allocator.hh:7
double reduction
Reduction achieved: .
Definition: solver.hh:62
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:422
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
SimdScalar< real_type > scalar_real_type
scalar type underlying the field_type
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:103
Categories for the solvers.
Definition: solvercategory.hh:19
std::shared_ptr< ScalarProduct< X > > _sp
Definition: solver.hh:302
void printHeader(std::ostream &s) const
helper function for printing header of solver output
Definition: solver.hh:155
A linear operator.
Definition: operators.hh:64
Define base class for scalar product and norm.
void printOutput(std::ostream &s, const CountType &iter, const DataType &norm) const
helper function for printing solver output
Definition: solver.hh:177
std::shared_ptr< LinearOperator< X, Y > > _op
Definition: solver.hh:300
scalar_real_type _reduction
Definition: solver.hh:303
SolverCategory::Category _category
Definition: solver.hh:306
double elapsed
Elapsed time in seconds.
Definition: solver.hh:74
virtual ~InverseOperator()
Destructor.
Definition: solver.hh:148
Define general, extensible interface for operators. The available implementation wraps a matrix...
static void setMatrix(ISTLLinearSolver &, const BCRSMatrix &)
Definition: solver.hh:337
static void setMatrix(ISTLLinearSolver &solver, const BCRSMatrix &matrix)
Definition: solver.hh:320
bool converged
True if convergence criterion has been met.
Definition: solver.hh:65
Abstract base class for all solvers.
Definition: solver.hh:91
Definition: solvercategory.hh:52
InverseOperatorResult()
Default constructor.
Definition: solver.hh:43
Default implementation for the scalar case.
Definition: scalarproducts.hh:84
int _maxit
Definition: solver.hh:304
Category
Definition: solvercategory.hh:21
Definition: solvertype.hh:13
X domain_type
Type of the domain of the operator to be inverted.
Definition: solver.hh:94
void clear()
Resets all data.
Definition: solver.hh:49
Base class for scalar product and norm computation.
Definition: scalarproducts.hh:46
virtual void apply(X &x, X &b, double reduction, InverseOperatorResult &res)
Apply inverse operator with given reduction factor.
Definition: solver.hh:285
Y range_type
Type of the range of the operator to be inverted.
Definition: solver.hh:97
virtual SolverCategory::Category category() const
Category of the solver (see SolverCategory::Category)
Definition: solver.hh:294
Helper class for notifying a DUNE-ISTL linear solver about a change of the iteration matrix object in...
Definition: solver.hh:317
Statistics about the application of an inverse operator.
Definition: solver.hh:40
Category for sequential solvers.
Definition: solvercategory.hh:23
Base class for matrix free definition of preconditioners.
Definition: preconditioner.hh:30
int _verbose
Definition: solver.hh:305
int iterations
Number of iterations.
Definition: solver.hh:59
std::shared_ptr< Preconditioner< X, Y > > _prec
Definition: solver.hh:301
Templates characterizing the type of a solver.
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:164
double conv_rate
Convergence rate (average reduction per step)
Definition: solver.hh:68
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:222
double condition_estimate
Estimate of condition number.
Definition: solver.hh:71