3.5-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
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 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
25#ifndef DUMUX_NUMERIC_DIFFERENTIATION_HH
26#define DUMUX_NUMERIC_DIFFERENTIATION_HH
27
28#include <cmath>
29#include <limits>
30
31namespace Dumux {
32
38{
39public:
40
46 template<class Scalar>
47 static Scalar epsilon(const Scalar value, const Scalar baseEps = 1e-10)
48 {
49 assert(std::numeric_limits<Scalar>::epsilon()*1e4 < baseEps);
50 // the epsilon value used for the numeric differentiation is
51 // now scaled by the absolute value of the primary variable...
52 using std::abs;
53 return baseEps*(abs(value) + 1.0);
54 }
55
60 template<class Function, class Scalar, class FunctionEvalType>
61 static void partialDerivative(const Function& function, Scalar x0,
62 FunctionEvalType& derivative,
63 const FunctionEvalType& fx0,
64 const int numericDifferenceMethod = 1)
65 { partialDerivative(function, x0, derivative, fx0, epsilon(x0), numericDifferenceMethod); }
66
77 template<class Function, class Scalar, class FunctionEvalType>
78 static void partialDerivative(const Function& function, Scalar x0,
79 FunctionEvalType& derivative,
80 const FunctionEvalType& fx0,
81 const Scalar eps,
82 const int numericDifferenceMethod = 1)
83 {
84 Scalar delta = 0.0;
85 // we are using forward or central differences, i.e. we need to calculate f(x + \epsilon)
86 if (numericDifferenceMethod >= 0)
87 {
88 delta += eps;
89 // calculate the function evaluated with the deflected variable
90 derivative = function(x0 + eps);
91 }
92
93 // we are using backward differences,
94 // i.e. we don't need to calculate f(x + \epsilon)
95 // we can recycle the (possibly cached) f(x)
96 else derivative = fx0;
97
98 // we are using backward or central differences,
99 // i.e. we need to calculate f(x - \epsilon)
100 if (numericDifferenceMethod <= 0)
101 {
102 delta += eps;
103 // subtract the function evaluated with the deflected variable
104 derivative -= function(x0 - eps);
105 }
106
107 // we are using forward differences,
108 // i.e. we don't need to calculate f(x - \epsilon)
109 // we can recycle the (possibly cached) f(x)
110 else derivative -= fx0;
111
112 // divide difference in residuals by the magnitude of the
113 // deflections between the two function evaluation
114 derivative /= delta;
115 }
116};
117
118} // end namespace Dumux
119
120#endif
Definition: adapt.hh:29
A class for numeric differentiation with respect to a scalar parameter.
Definition: numericdifferentiation.hh:38
static Scalar epsilon(const Scalar value, const Scalar baseEps=1e-10)
Computes the epsilon used for numeric differentiation.
Definition: numericdifferentiation.hh:47
static void partialDerivative(const Function &function, Scalar x0, FunctionEvalType &derivative, const FunctionEvalType &fx0, const int numericDifferenceMethod=1)
Computes the derivative of a function with repect to a function parameter.
Definition: numericdifferentiation.hh:61
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 repect to a function parameter.
Definition: numericdifferentiation.hh:78