NuTo
Numerics Tool
LineSearch.h
Go to the documentation of this file.
1 #pragma once
2 
3 namespace NuTo
4 {
5 namespace NewtonRaphson
6 {
7 
9 struct VoidInfo
10 {
11  template <typename... TAny>
12  void operator()(TAny&&...) const
13  {
14  }
15 };
16 
18 template <typename TInfo>
20 {
21 public:
24  constexpr LineSearchImplementation(TInfo info, int maxNumLineSearchSteps)
25  : mInfo(info)
26  , mMaxNumLineSearchStep(maxNumLineSearchSteps)
27  {
28  }
29 
38  template <typename TProblem, typename TX>
39  bool operator()(TProblem&& problem, TX* r, TX* x, TX dx) const
40  {
41  double alpha = 1.;
42  int lineSearchStep = 0;
43  const auto x0 = *x;
44  const auto previousNorm = problem.Norm(problem.Residual(*x));
45  while (lineSearchStep < mMaxNumLineSearchStep)
46  {
47  *x = x0 - alpha * dx;
48  *r = problem.Residual(*x);
49  const auto trialNorm = problem.Norm(*r);
50 
51  mInfo(lineSearchStep, alpha, trialNorm);
52 
53  if (trialNorm < problem.mTolerance)
54  return true;
55 
56  alpha *= 0.5;
57  lineSearchStep++;
58 
59  if (trialNorm < (1. - alpha) * previousNorm)
60  return false;
61  }
62  return false; // max steps reached
63  }
64 
65 private:
66  TInfo mInfo;
67  int mMaxNumLineSearchStep;
68 };
69 
71 template <typename TInfo = VoidInfo>
72 LineSearchImplementation<TInfo> LineSearch(TInfo info = VoidInfo(), int mMaxNumLineSearchStep = 6)
73 {
74  return LineSearchImplementation<TInfo>(info, mMaxNumLineSearchStep);
75 }
76 
80 {
81 public:
82  template <typename TProblem, typename TX>
83  bool operator()(TProblem&& problem, TX* r, TX* x, TX dx) const
84  {
85  *x -= dx;
86  *r = problem.Residual(*x);
87  return problem.Norm(*r) < problem.mTolerance;
88  }
89 };
90 
91 } /* NewtonRaphson */
92 } /* NuTo */
Performs the line search algorithm based on the results of a single newton iteration step...
Definition: LineSearch.h:19
constexpr LineSearchImplementation(TInfo info, int maxNumLineSearchSteps)
ctor
Definition: LineSearch.h:24
just a normal continuation of the newton scheme without using line search while keeping the interface...
Definition: LineSearch.h:79
takes any argument and does nothing...
Definition: LineSearch.h:9
float alpha
Definition: DamageBar.py:18
LineSearchImplementation< TInfo > LineSearch(TInfo info=VoidInfo(), int mMaxNumLineSearchStep=6)
convienient instantiation of the NuTo::LineSearchImplementation with template deduction ...
Definition: LineSearch.h:72
Definition: Exception.h:6
bool operator()(TProblem &&problem, TX *r, TX *x, TX dx) const
actual line search implementation
Definition: LineSearch.h:39
void operator()(TAny &&...) const
Definition: LineSearch.h:12
bool operator()(TProblem &&problem, TX *r, TX *x, TX dx) const
Definition: LineSearch.h:83