version 3.8
doubleexpintegrator.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// SPDX-FileCopyrightInfo: Copyright John D. Cook
7// SPDX-License-Identifier: BSD-2-Clause
8// Modified after the original version at
9// https://www.codeproject.com/Articles/31550/Fast-Numerical-Integration licensed under
10// BSD-2-Clause. All Changes are licensed under GPL-3.0-or-later.
11//
12/*****************************************************************************
13 * This version is modified after the original version by John D. Cook *
14 * see https://www.codeproject.com/ *
15 * Articles/31550/Fast-Numerical-Integration *
16 * which is licensed under BSD-2-clause, which reads as follows: *
17 * Copyright John D. Cook *
18 * *
19 * Redistribution and use in source and binary forms, with or without *
20 * modification, are permitted provided that the following *
21 * conditions are met: *
22 * 1. Redistributions of source code must retain the above *
23 * copyright notice, this list of conditions *
24 and the following disclaimer. *
25 * 2. Redistributions in binary form must reproduce the above *
26 * copyright notice, this list of conditions *
27 * and the following disclaimer *
28 * in the documentation and/or other materials *
29 * provided with the distribution. *
30 * *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
34 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
35 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, *
36 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE *
38 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
39 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, *
40 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE *
41 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, *
42 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
43 * *
44 *****************************************************************************/
50#ifndef DUMUX_COMMON_DOUBLEEXP_INTEGRATOR_HH
51#define DUMUX_COMMON_DOUBLEEXP_INTEGRATOR_HH
52
53#include <cmath>
54#include <limits>
55#include <type_traits>
56
58
59namespace Dumux {
60
64template<class Scalar>
66{
67public:
78 template<class Function,
79 typename std::enable_if_t<std::is_invocable_r_v<Scalar, Function, Scalar>>...>
80 static Scalar integrate(const Function& f,
81 const Scalar a,
82 const Scalar b,
83 const Scalar targetAbsoluteError,
84 int& numFunctionEvaluations,
85 Scalar& errorEstimate)
86 {
87 // Apply the linear change of variables x = ct + d
88 // $$\int_a^b f(x) dx = c \int_{-1}^1 f( ct + d ) dt$$
89 // c = (b-a)/2, d = (a+b)/2
90
91 const Scalar c = 0.5*(b - a);
92 const Scalar d = 0.5*(a + b);
93 return integrateCore_(f, c, d, targetAbsoluteError, numFunctionEvaluations, errorEstimate,
95 }
96
107 template<class Function,
108 typename std::enable_if_t<std::is_invocable_r_v<Scalar, Function, Scalar>>...>
109 static Scalar integrate(const Function& f,
110 const Scalar a,
111 const Scalar b,
112 const Scalar targetAbsoluteError)
113 {
114 int numFunctionEvaluations;
115 Scalar errorEstimate;
116 return integrate(f, a, b, targetAbsoluteError, numFunctionEvaluations, errorEstimate);
117 }
118
119private:
120 // Integrate f(cx + d) with the given integration constants
121 template<class Function>
122 static Scalar integrateCore_(const Function& f,
123 const Scalar c, // slope of change of variables
124 const Scalar d, // intercept of change of variables
125 Scalar targetAbsoluteError,
126 int& numFunctionEvaluations,
127 Scalar& errorEstimate,
128 const double* abcissas,
129 const double* weights)
130 {
131 targetAbsoluteError /= c;
132
133 // Offsets to where each level's integration constants start.
134 // The last element is not a beginning but an end.
135 static const int offsets[] = {1, 4, 7, 13, 25, 49, 97, 193};
136 static const int numLevels = sizeof(offsets)/sizeof(int) - 1;
137
138 Scalar newContribution = 0.0;
139 Scalar integral = 0.0;
140 Scalar h = 1.0;
141 errorEstimate = std::numeric_limits<Scalar>::max();
142 Scalar previousDelta, currentDelta = std::numeric_limits<Scalar>::max();
143
144 integral = f(c*abcissas[0] + d) * weights[0];
145 int i;
146 for (i = offsets[0]; i != offsets[1]; ++i)
147 integral += weights[i]*(f(c*abcissas[i] + d) + f(-c*abcissas[i] + d));
148
149 for (int level = 1; level != numLevels; ++level)
150 {
151 h *= 0.5;
152 newContribution = 0.0;
153 for (i = offsets[level]; i != offsets[level+1]; ++i)
154 newContribution += weights[i]*(f(c*abcissas[i] + d) + f(-c*abcissas[i] + d));
155 newContribution *= h;
156
157 // difference in consecutive integral estimates
158 previousDelta = currentDelta;
159 using std::abs;
160 currentDelta = abs(0.5*integral - newContribution);
161 integral = 0.5*integral + newContribution;
162
163 // Once convergence kicks in, error is approximately squared at each step.
164 // Determine whether we're in the convergent region by looking at the trend in the error.
165 if (level == 1)
166 continue; // previousDelta meaningless, so cannot check convergence.
167
168 // Exact comparison with zero is harmless here. Could possibly be replaced with
169 // a small positive upper limit on the size of currentDelta, but determining
170 // that upper limit would be difficult. At worse, the loop is executed more
171 // times than necessary. But no infinite loop can result since there is
172 // an upper bound on the loop variable.
173 if (currentDelta == 0.0)
174 break;
175
176 using std::log;
177 const Scalar rate = log( currentDelta )/log( previousDelta ); // previousDelta != 0 or would have been kicked out previously
178
179 if (rate > 1.9 && rate < 2.1)
180 {
181 // If convergence theory applied perfectly, r would be 2 in the convergence region.
182 // r close to 2 is good enough. We expect the difference between this integral estimate
183 // and the next one to be roughly delta^2.
184 errorEstimate = currentDelta*currentDelta;
185 }
186 else
187 {
188 // Not in the convergence region. Assume only that error is decreasing.
189 errorEstimate = currentDelta;
190 }
191
192 if (errorEstimate < 0.1*targetAbsoluteError)
193 break;
194 }
195
196 numFunctionEvaluations = 2*i - 1;
197 errorEstimate *= c;
198 return c*integral;
199 }
200};
201
202} // end namespace Dumux
203
204#endif
Numerical integration in one dimension using the double exponential method of M. Mori.
Definition: doubleexpintegrator.hh:66
static Scalar integrate(const Function &f, const Scalar a, const Scalar b, const Scalar targetAbsoluteError, int &numFunctionEvaluations, Scalar &errorEstimate)
Integrate an analytic function over a finite interval.
Definition: doubleexpintegrator.hh:80
static Scalar integrate(const Function &f, const Scalar a, const Scalar b, const Scalar targetAbsoluteError)
Integrate an analytic function over a finite interval.
Definition: doubleexpintegrator.hh:109
Exponential integration abcissas.
Definition: adapt.hh:17
constexpr double doubleExponentialIntegrationWeights[]
Definition: doubleexpintegrationconstants.hh:290
constexpr double doubleExponentialIntegrationAbcissas[]
Definition: doubleexpintegrationconstants.hh:86