NuTo
Numerics Tool
NewtonRaphson.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Eigen/Sparse>
4 #include "nuto/base/Exception.h"
5 #include "nuto/math/LineSearch.h"
6 
7 namespace NuTo
8 {
9 namespace NewtonRaphson
10 {
11 
14 {
15 public:
16  NoConvergence(const std::string& caller = "", const std::string& message = "")
17  : Exception(caller, message)
18  {
19  }
20 };
21 
24 {
25  static double Solve(double dr, double r)
26  {
27  return r / dr;
28  }
29 };
30 
37 template <typename TR, typename TDR, typename TNorm, typename TTol, typename TInfo>
38 struct Problem
39 {
42  TNorm Norm;
43  TTol mTolerance;
44  TInfo Info;
45 };
46 
49 template <typename TR, typename TDR, typename TNorm, typename TTol, typename TInfo = VoidInfo>
50 auto DefineProblem(TR residual, TDR derivative, TNorm norm, TTol tolerance, TInfo info = VoidInfo())
51 {
52  return Problem<TR, TDR, TNorm, TTol, TInfo>({residual, derivative, norm, tolerance, info});
53 }
54 
62 template <typename TNonlinearProblem, typename TX, typename TSolver, typename TLineSearchAlgorithm = NoLineSearch>
63 auto Solve(TNonlinearProblem&& problem, TX&& x0, TSolver&& solver, int maxIterations = 20,
64  TLineSearchAlgorithm&& lineSearch = NoLineSearch(), int* numIterations = nullptr)
65 {
66  auto x = x0;
67  auto r = problem.Residual(x);
68 
69  int iteration = 0;
70  problem.Info(iteration, x, r);
71 
72  if (problem.Norm(r) < problem.mTolerance)
73  return x;
74 
75  while (iteration < maxIterations)
76  {
77  auto dr = problem.Derivative(x);
78  auto dx = solver.Solve(dr, r);
79 
80  ++iteration;
81 
82  if (lineSearch(problem, &r, &x, dx))
83  {
84  if (numIterations)
85  *numIterations = iteration;
86  problem.Info(iteration, x, r);
87  return x;
88  }
89  problem.Info(iteration, x, r);
90  }
91  if (numIterations)
92  *numIterations = iteration;
93  throw NoConvergence(__PRETTY_FUNCTION__, "No convergence after " + std::to_string(iteration) + " iterations.");
94 }
95 } /* NewtonRaphson */
96 } /* NuTo */
NoConvergence(const std::string &caller="", const std::string &message="")
Definition: NewtonRaphson.h:16
auto DefineProblem(TR residual, TDR derivative, TNorm norm, TTol tolerance, TInfo info=VoidInfo())
defines the problem, basically just to enable automatic template deduction.
Definition: NewtonRaphson.h:50
Base class for all exceptions thrown in NuTo.
Definition: Exception.h:9
auto Solve(TNonlinearProblem &&problem, TX &&x0, TSolver &&solver, int maxIterations=20, TLineSearchAlgorithm &&lineSearch=NoLineSearch(), int *numIterations=nullptr)
solves the Problem using the newton raphson iteration with linesearch
Definition: NewtonRaphson.h:63
TTol mTolerance
Definition: NewtonRaphson.h:43
constexpr double tolerance
Definition: NewtonRaphsonBenchmark.cpp:11
just a normal continuation of the newton scheme without using line search while keeping the interface...
Definition: LineSearch.h:79
solver
Definition: SparseDirectSolverMKLDSS.py:51
takes any argument and does nothing...
Definition: LineSearch.h:9
problem definition
Definition: NewtonRaphson.h:38
static double Solve(double dr, double r)
Definition: NewtonRaphson.h:25
TR Residual
Definition: NewtonRaphson.h:40
residual
Definition: Brick8NCoupling.py:135
Definition: Exception.h:6
TInfo Info
Definition: NewtonRaphson.h:44
TDR Derivative
Definition: NewtonRaphson.h:41
custom exception for the newton algorithm
Definition: NewtonRaphson.h:13
"Solver" for scalar values
Definition: NewtonRaphson.h:23
TNorm Norm
Definition: NewtonRaphson.h:42