DUNE PDELab (2.8)

terminate.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_PDELAB_SOLVER_TERMINATE_HH
4#define DUNE_PDELAB_SOLVER_TERMINATE_HH
5
6#include <dune/pdelab/solver/newtonerrors.hh>
7
8namespace Dune::PDELab
9{
10
11 class TerminateInterface
12 {
13 public:
15 virtual ~TerminateInterface () {}
16
17 virtual bool terminate() = 0;
18
19 virtual void setParameters(const ParameterTree&) = 0;
20
21 virtual void printParameters() const
22 {
23 std::cout << "NewtonMethod::_terminate->printParameters() is not implemented." << std::endl;
24 }
25};
26
27
28 template <typename Solver>
29 class DefaultTerminate : public TerminateInterface
30 {
31 public:
32 using Real = typename Solver::Real;
33
34 DefaultTerminate(Solver& solver) : _solver(solver) {}
35
36 virtual bool terminate() override
37 {
38 if (_force_iteration && _solver.result().iterations == 0)
39 return false;
40 auto converged = _solver.result().defect < _solver.getAbsoluteLimit() || _solver.result().defect < _solver.result().first_defect * _solver.getReduction();
41 _solver.result().converged = converged;
42 if (_solver.result().iterations >= _maxIterations && not _solver.result().converged)
43 DUNE_THROW(TerminateError,
44 "Terminate::terminate(): Maximum iteration count reached");
45 return converged;
46 }
47
48 virtual void setParameters(const ParameterTree& parameterTree) override
49 {
50 _maxIterations = parameterTree.get<unsigned int>("MaxIterations", _maxIterations);
51 _force_iteration = parameterTree.get<bool>("ForceIteration", _force_iteration);
52 }
53
54 virtual void printParameters() const override
55 {
56 std::cout << "Terminate.MaxIterations. " << _maxIterations << std::endl;
57 std::cout << "Terminate.ForceIteration " << _force_iteration << std::endl;
58 }
59
61 void setMaxIterations(const unsigned int maxIterations)
62 {
63 _maxIterations = maxIterations;
64 }
65
67 void setForceIteration(const bool forceIteration)
68 {
69 _force_iteration = forceIteration;
70 }
71
72 private:
73 Solver& _solver;
74 unsigned int _maxIterations = 40;
75 bool _force_iteration = false;
76 };
77}
78
79#endif
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 22, 23:30, 2024)