dune-pdelab  2.7-git
newtonterminate.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 #ifndef DUNE_PDELAB_SOLVER_NEWTONTERMINATE_HH
4 #define DUNE_PDELAB_SOLVER_NEWTONTERMINATE_HH
5 
6 namespace Dune::PDELab
7 {
9  {
10  public:
12  virtual ~TerminateInterface () {}
13 
14  virtual bool terminate() = 0;
15 
16  virtual void setParameters(const ParameterTree&) = 0;
17  };
18 
19 
20  template <typename Newton>
22  {
23  public:
24  using Real = typename Newton::Real;
25 
26  DefaultTerminate(Newton& newton) : _newton(newton) {}
27 
28  virtual bool terminate() override
29  {
30  if (_force_iteration && _newton.result().iterations == 0)
31  return false;
32  auto converged = _newton.result().defect < _newton.getAbsoluteLimit() || _newton.result().defect < _newton.result().first_defect * _newton.getReduction();
33  if (_newton.result().iterations >= _maxIterations && not _newton.result().converged)
34  DUNE_THROW(NewtonNotConverged,
35  "NewtonTerminate::terminate(): Maximum iteration count reached");
36  return converged;
37  }
38 
39  virtual void setParameters(const ParameterTree& parameterTree) override
40  {
41  _maxIterations = parameterTree.get<unsigned int>("max_iterations", _maxIterations);
42  _force_iteration = parameterTree.get<bool>("force_iteration", _force_iteration);
43  }
44 
45  private:
46  Newton& _newton;
47  unsigned int _maxIterations = 40;
48  bool _force_iteration = false;
49  };
50 }
51 
52 #endif
Definition: adaptivity.hh:29
Definition: newtonterminate.hh:9
virtual ~TerminateInterface()
Every abstract base class should have a virtual destructor.
Definition: newtonterminate.hh:12
virtual void setParameters(const ParameterTree &)=0
Definition: newtonterminate.hh:22
virtual bool terminate() override
Definition: newtonterminate.hh:28
DefaultTerminate(Newton &newton)
Definition: newtonterminate.hh:26
virtual void setParameters(const ParameterTree &parameterTree) override
Definition: newtonterminate.hh:39
typename Newton::Real Real
Definition: newtonterminate.hh:24