version 3.10-dev
numericdifferentiation.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
13#ifndef DUMUX_NUMERIC_DIFFERENTIATION_HH
14#define DUMUX_NUMERIC_DIFFERENTIATION_HH
15
16#include <cmath>
17#include <cassert>
18#include <limits>
19
20namespace Dumux {
21
27{
28public:
29
35 template<class Scalar>
36 static Scalar epsilon(const Scalar value, const Scalar baseEps = 1e-10)
37 {
38 assert(std::numeric_limits<Scalar>::epsilon()*1e4 < baseEps);
39 // the epsilon value used for the numeric differentiation is
40 // now scaled by the absolute value of the primary variable...
41 using std::abs;
42 return baseEps*(abs(value) + 1.0);
43 }
44
49 template<class Function, class Scalar, class FunctionEvalType>
50 static void partialDerivative(const Function& function, Scalar x0,
51 FunctionEvalType& derivative,
52 const FunctionEvalType& fx0,
53 const int numericDifferenceMethod = 1)
54 { partialDerivative(function, x0, derivative, fx0, epsilon(x0), numericDifferenceMethod); }
55
66 template<class Function, class Scalar, class FunctionEvalType>
67 static void partialDerivative(const Function& function, Scalar x0,
68 FunctionEvalType& derivative,
69 const FunctionEvalType& fx0,
70 const Scalar eps,
71 const int numericDifferenceMethod = 1)
72 {
73 // Five-point stencil numeric difference,
74 // Abramowitz & Stegun, Table 25.2.
75 // The error is proportional to eps^4.
76 if (numericDifferenceMethod == 5)
77 {
78 derivative = function(x0 + eps);
79 derivative -= function(x0 - eps);
80 derivative *= 8.0;
81 derivative += function(x0 - 2*eps);
82 derivative -= function(x0 + 2*eps);
83 derivative /= 12*eps;
84 return;
85 }
86
87 // Forward, central, or backward differences
88 Scalar delta = 0.0;
89
90 // we are using forward or central differences, i.e. we need to calculate f(x + \epsilon)
91 if (numericDifferenceMethod >= 0)
92 {
93 delta += eps;
94 // calculate the function evaluated with the deflected variable
95 derivative = function(x0 + eps);
96 }
97
98 // we are using backward differences,
99 // i.e. we don't need to calculate f(x + \epsilon)
100 // we can recycle the (possibly cached) f(x)
101 else derivative = fx0;
102
103 // we are using backward or central differences,
104 // i.e. we need to calculate f(x - \epsilon)
105 if (numericDifferenceMethod <= 0)
106 {
107 delta += eps;
108 // subtract the function evaluated with the deflected variable
109 derivative -= function(x0 - eps);
110 }
111
112 // we are using forward differences,
113 // i.e. we don't need to calculate f(x - \epsilon)
114 // we can recycle the (possibly cached) f(x)
115 else derivative -= fx0;
116
117 // divide difference in residuals by the magnitude of the
118 // deflections between the two function evaluation
119 derivative /= delta;
120 }
121};
122
123} // end namespace Dumux
124
125#endif
A class for numeric differentiation with respect to a scalar parameter.
Definition: numericdifferentiation.hh:27
static Scalar epsilon(const Scalar value, const Scalar baseEps=1e-10)
Computes the epsilon used for numeric differentiation.
Definition: numericdifferentiation.hh:36
static void partialDerivative(const Function &function, Scalar x0, FunctionEvalType &derivative, const FunctionEvalType &fx0, const int numericDifferenceMethod=1)
Computes the derivative of a function with respect to a function parameter.
Definition: numericdifferentiation.hh:50
static void partialDerivative(const Function &function, Scalar x0, FunctionEvalType &derivative, const FunctionEvalType &fx0, const Scalar eps, const int numericDifferenceMethod=1)
Computes the derivative of a function with respect to a function parameter.
Definition: numericdifferentiation.hh:67
Definition: adapt.hh:17