Dune Core Modules (2.6.0)

solver.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3
4#ifndef DUNE_ISTL_SOLVER_HH
5#define DUNE_ISTL_SOLVER_HH
6
7#include <iomanip>
8#include <ostream>
9
12#include <dune/common/simd.hh>
13
14#include "solvertype.hh"
15#include "preconditioner.hh"
16#include "operators.hh"
17#include "scalarproducts.hh"
18
19namespace Dune
20{
41 {
44 {
45 clear();
46 }
47
49 void clear ()
50 {
51 iterations = 0;
52 reduction = 0;
53 converged = false;
54 conv_rate = 1;
55 elapsed = 0;
56 }
57
60
62 double reduction;
63
66
68 double conv_rate;
69
71 double condition_estimate = -1;
72
74 double elapsed;
75 };
76
77
78 //=====================================================================
90 template<class X, class Y>
92 public:
94 typedef X domain_type;
95
97 typedef Y range_type;
98
100 typedef typename X::field_type field_type;
101
103 typedef typename FieldTraits<field_type>::real_type real_type;
104
106 typedef SimdScalar<real_type> scalar_real_type;
107
120 virtual void apply (X& x, Y& b, InverseOperatorResult& res) = 0;
121
135 virtual void apply (X& x, Y& b, double reduction, InverseOperatorResult& res) = 0;
136
139#ifdef DUNE_ISTL_SUPPORT_OLD_CATEGORY_INTERFACE
140 {
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.");
142 }
143#else
144 = 0;
145#endif
146
148 virtual ~InverseOperator () {}
149
150 protected:
151 // spacing values
152 enum { iterationSpacing = 5 , normSpacing = 16 };
153
155 void printHeader(std::ostream& s) const
156 {
157 s << std::setw(iterationSpacing) << " Iter";
158 s << std::setw(normSpacing) << "Defect";
159 s << std::setw(normSpacing) << "Rate" << std::endl;
160 }
161
163 template <typename CountType, typename DataType>
164 void printOutput(std::ostream& s,
165 const CountType& iter,
166 const DataType& norm,
167 const DataType& norm_old) const
168 {
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;
173 }
174
176 template <typename CountType, typename DataType>
177 void printOutput(std::ostream& s,
178 const CountType& iter,
179 const DataType& norm) const
180 {
181 s << std::setw(iterationSpacing) << iter << " ";
182 s << std::setw(normSpacing) << norm << std::endl;
183 }
184 };
185
194 template<class X, class Y>
195 class IterativeSolver : public InverseOperator<X,Y>{
196 public:
200 using typename InverseOperator<X,Y>::real_type;
202
222 IterativeSolver (LinearOperator<X,Y>& op, Preconditioner<X,Y>& prec, scalar_real_type reduction, int maxit, int verbose) :
224 _prec(stackobject_to_shared_ptr(prec)),
225 _sp(new SeqScalarProduct<X>),
226 _reduction(reduction), _maxit(maxit), _verbose(verbose), _category(SolverCategory::sequential)
227 {
229 DUNE_THROW(InvalidSolverCategory, "LinearOperator has to be sequential!");
231 DUNE_THROW(InvalidSolverCategory, "Preconditioner has to be sequential!");
232 }
233
255 scalar_real_type reduction, int maxit, int verbose) :
257 _prec(stackobject_to_shared_ptr(prec)),
259 _reduction(reduction), _maxit(maxit), _verbose(verbose), _category(SolverCategory::category(op))
260 {
262 DUNE_THROW(InvalidSolverCategory, "LinearOperator and Preconditioner must have the same SolverCategory!");
264 DUNE_THROW(InvalidSolverCategory, "LinearOperator and ScalarProduct must have the same SolverCategory!");
265 }
266
267 // #warning actually we want to have this as the default and just implement the second one
268 // //! \copydoc InverseOperator::apply(X&,Y&,InverseOperatorResult&)
269 // virtual void apply (X& x, Y& b, InverseOperatorResult& res)
270 // {
271 // apply(x,b,_reduction,res);
272 // }
273
274#ifndef DOXYGEN
275 // make sure the three-argument apply from the base class does not get shadowed
276 // by the redefined four-argument version below
277 using InverseOperator<X,Y>::apply;
278#endif
279
285 virtual void apply (X& x, X& b, double reduction, InverseOperatorResult& res)
286 {
287 scalar_real_type saved_reduction = _reduction;
288 _reduction = reduction;
289 this->apply(x,b,res);
290 _reduction = saved_reduction;
291 }
292
295 {
296 return _category;
297 }
298
299 protected:
300 std::shared_ptr<LinearOperator<X,Y>> _op;
301 std::shared_ptr<Preconditioner<X,Y>> _prec;
302 std::shared_ptr<ScalarProduct<X>> _sp;
303 scalar_real_type _reduction;
304 int _maxit;
305 int _verbose;
306 SolverCategory::Category _category;
307 };
308
316 template <typename ISTLLinearSolver, typename BCRSMatrix>
318 {
319 public:
320 static void setMatrix (ISTLLinearSolver& solver,
321 const BCRSMatrix& matrix)
322 {
323 static const bool is_direct_solver
324 = Dune::IsDirectSolver<ISTLLinearSolver>::value;
327 }
328
329 protected:
334 template <bool is_direct_solver, typename Dummy = void>
336 {
337 static void setMatrix (ISTLLinearSolver&,
338 const BCRSMatrix&)
339 {}
340 };
341
346 template <typename Dummy>
347 struct Implementation<true,Dummy>
348 {
349 static void setMatrix (ISTLLinearSolver& solver,
350 const BCRSMatrix& matrix)
351 {
352 solver.setMatrix(matrix);
353 }
354 };
355 };
356
360}
361
362#endif
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:423
Base class for Dune-Exceptions.
Definition: exceptions.hh:94
Abstract base class for all solvers.
Definition: solver.hh:91
void printHeader(std::ostream &s) const
helper function for printing header of solver output
Definition: solver.hh:155
virtual ~InverseOperator()
Destructor.
Definition: solver.hh:148
void printOutput(std::ostream &s, const CountType &iter, const DataType &norm) const
helper function for printing solver output
Definition: solver.hh:177
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
virtual void apply(X &x, Y &b, double reduction, InverseOperatorResult &res)=0
apply inverse operator, with given convergence criteria.
Y range_type
Type of the range of the operator to be inverted.
Definition: solver.hh:97
SimdScalar< real_type > scalar_real_type
scalar type underlying the field_type
Definition: solver.hh:106
X domain_type
Type of the domain of the operator to be inverted.
Definition: solver.hh:94
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:100
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
virtual SolverCategory::Category category() const =0
Category of the solver (see SolverCategory::Category)
Base class for all implementations of iterative solvers.
Definition: solver.hh:195
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
virtual void apply(X &x, X &b, double reduction, InverseOperatorResult &res)
Apply inverse operator with given reduction factor.
Definition: solver.hh:285
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
virtual SolverCategory::Category category() const
Category of the solver (see SolverCategory::Category)
Definition: solver.hh:294
A linear operator.
Definition: operators.hh:64
Base class for matrix free definition of preconditioners.
Definition: preconditioner.hh:30
Base class for scalar product and norm computation.
Definition: scalarproducts.hh:46
Default implementation for the scalar case.
Definition: scalarproducts.hh:85
Helper class for notifying a DUNE-ISTL linear solver about a change of the iteration matrix object in...
Definition: solver.hh:318
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Dune namespace.
Definition: alignedallocator.hh:10
shared_ptr< T > stackobject_to_shared_ptr(T &t)
Create a shared_ptr for a stack-allocated object.
Definition: shared_ptr.hh:75
Define general, extensible interface for operators. The available implementation wraps a matrix.
Define base class for scalar product and norm.
This file implements the class shared_ptr (a reference counting pointer), for those systems that don'...
Templates characterizing the type of a solver.
Statistics about the application of an inverse operator.
Definition: solver.hh:41
InverseOperatorResult()
Default constructor.
Definition: solver.hh:43
double condition_estimate
Estimate of condition number.
Definition: solver.hh:71
double elapsed
Elapsed time in seconds.
Definition: solver.hh:74
int iterations
Number of iterations.
Definition: solver.hh:59
double reduction
Reduction achieved: .
Definition: solver.hh:62
void clear()
Resets all data.
Definition: solver.hh:49
double conv_rate
Convergence rate (average reduction per step)
Definition: solver.hh:68
bool converged
True if convergence criterion has been met.
Definition: solver.hh:65
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
Implementation that works together with iterative ISTL solvers, e.g. Dune::CGSolver or Dune::BiCGSTAB...
Definition: solver.hh:336
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 13, 23:29, 2024)