Add cool quote
[alexxy/gromacs.git] / src / gromacs / tables / splineutil.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016,2017, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35
36 /*! \internal \file
37  * \brief
38  * Implements internal utility functions for spline tables
39  *
40  * \author Erik Lindahl <erik.lindahl@gmail.com>
41  * \ingroup module_tables
42  */
43 #include "gmxpre.h"
44
45 #include "splineutil.h"
46
47 #include <cmath>
48
49 #include <algorithm>
50 #include <functional>
51 #include <utility>
52 #include <vector>
53
54 #include "gromacs/utility/alignedallocator.h"
55 #include "gromacs/utility/arrayref.h"
56 #include "gromacs/utility/exceptions.h"
57 #include "gromacs/utility/real.h"
58 #include "gromacs/utility/stringutil.h"
59
60 namespace gmx
61 {
62
63 namespace internal
64 {
65
66 void
67 throwUnlessDerivativeIsConsistentWithFunction(const std::function<double(double)>  &function,
68                                               const std::function<double(double)>  &derivative,
69                                               const std::pair<real, real>          &range)
70 {
71     // Since the numerical derivative will evaluate extra points
72     // we shrink the interval slightly to avoid calling the function with values
73     // outside the range specified.
74     double                     h            = std::cbrt(GMX_DOUBLE_EPS); // ideal spacing
75     std::pair<double, double>  newRange(range.first + h, range.second - h);
76     const int                  points       = 1000;                      // arbitrary
77     double                     dx           = (newRange.second - newRange.first) / points;
78     bool                       isConsistent = true;
79     double                     minFail      = newRange.second;
80     double                     maxFail      = newRange.first;
81
82     for (double x = newRange.first; x <= newRange.second; x += dx)
83     {
84         double analyticalDerivative = derivative(x);
85         double numericalDerivative  = (function(x+h)-function(x-h))/(2*h);
86         double thirdDerivative      = (derivative(x+h)-2.0*derivative(x)+derivative(x-h))/(h*h);
87
88         // We make two types of errors in numerical calculation of the derivative:
89         // - The truncation error: eps * |f| / h
90         // - The rounding error: h * h * |f'''| / 6.0
91         double expectedErr = GMX_DOUBLE_EPS*std::abs(function(x))/h + h*h*std::abs(thirdDerivative)/6.0;
92
93         // To avoid triggering false errors because of compiler optimization or numerical issues
94         // in the function evalulation we allow an extra factor of 10 in the expected error
95         if (std::abs(analyticalDerivative-numericalDerivative) > 10.0*expectedErr)
96         {
97             isConsistent = false;
98             minFail      = std::min(minFail, x);
99             maxFail      = std::max(maxFail, x);
100         }
101     }
102
103     if (!isConsistent)
104     {
105         GMX_THROW(InconsistentInputError(formatString("Derivative inconsistent with analytical function in range [%d,%d]", minFail, maxFail)));
106     }
107 }
108
109
110 void
111 throwUnlessDerivativeIsConsistentWithFunction(ArrayRef<const double>        function,
112                                               ArrayRef<const double>        derivative,
113                                               double                        inputSpacing,
114                                               const std::pair<real, real>  &range)
115 {
116     std::size_t     firstIndex   = range.first / inputSpacing;
117     std::size_t     lastIndex    = range.second / inputSpacing;
118     bool            isConsistent = true;
119     std::size_t     minFail      = lastIndex;
120     std::size_t     maxFail      = firstIndex;
121
122     // The derivative will access one extra point before/after each point, so reduce interval
123     for (std::size_t i = firstIndex + 1; (i + 1) < lastIndex; i++)
124     {
125         double inputDerivative     = derivative[i];
126         double numericalDerivative = (function[i+1] - function[i-1]) / (2.0 * inputSpacing);
127         double thirdDerivative     = (derivative[i+1] - 2.0*derivative[i] + derivative[i-1])/(inputSpacing * inputSpacing);
128
129         // We make two types of errors in numerical calculation of the derivative:
130         // - The truncation error: eps * |f| / h
131         // - The rounding error: h * h * |f'''| / 6.0
132         double expectedErr = GMX_DOUBLE_EPS*std::abs(function[i])/inputSpacing + inputSpacing*inputSpacing*std::abs(thirdDerivative)/6.0;
133
134         // To avoid triggering false errors because of compiler optimization or numerical issues
135         // in the function evalulation we allow an extra factor of 10 in the expected error
136         if (std::abs(inputDerivative-numericalDerivative) > 10.0*expectedErr)
137         {
138             isConsistent = false;
139             minFail      = std::min(minFail, i);
140             maxFail      = std::max(maxFail, i);
141         }
142     }
143     if (!isConsistent)
144     {
145         GMX_THROW(InconsistentInputError(formatString("Derivative inconsistent with numerical vector for elements %d-%d", minFail+1, maxFail+1)));
146     }
147 }
148
149
150 /*! \brief Update minQuotient if the ratio of this function value and its second derivative is smaller
151  *
152  * This is a utility function used in the functions to find the smallest quotient
153  * in a range.
154  *
155  * \param[in]    previousPoint Value of function at x-h.
156  * \param[in]    thisPoint     Value of function at x.
157  * \param[in]    nextPoint     Value of function at x+h.
158  * \param[in]    spacing       Value of h.
159  * \param[inout] minQuotient   Current minimum of such quotients, updated if this quotient is smaller.
160  */
161 static void
162 updateMinQuotientOfFunctionAndSecondDerivative(double     previousPoint,
163                                                double     thisPoint,
164                                                double     nextPoint,
165                                                double     spacing,
166                                                double *   minQuotient)
167 {
168     double value             = std::abs( thisPoint );
169     double secondDerivative  = std::abs( (previousPoint - 2.0 * thisPoint + nextPoint) / (spacing * spacing ) );
170
171     // Make sure we do not divide by zero. This limit is arbitrary,
172     // but it doesnt matter since this point will have a very large value,
173     // and the whole routine is searching for the smallest value.
174     secondDerivative = std::max(secondDerivative, static_cast<double>(std::sqrt(GMX_REAL_MIN)));
175
176     *minQuotient = std::min(*minQuotient, value / secondDerivative);
177 }
178
179
180 real
181 findSmallestQuotientOfFunctionAndSecondDerivative(const std::function<double(double)>   &f,
182                                                   const std::pair<real, real>           &range)
183 {
184     // Since the numerical second derivative will evaluate extra points
185     // we shrink the interval slightly to avoid calling the function with values
186     // outside the range specified.
187     double                     h           = std::pow( GMX_DOUBLE_EPS, 0.25 );
188     std::pair<double, double>  newRange(range.first + h, range.second - h);
189     const int                  points      = 1000; // arbitrary
190     double                     dx          = (newRange.second - newRange.first) / points;
191     double                     minQuotient = GMX_REAL_MAX;
192
193     for (double x = newRange.first; x <= newRange.second; x += dx)
194     {
195         updateMinQuotientOfFunctionAndSecondDerivative(f(x-h), f(x), f(x+h), h, &minQuotient);
196     }
197     return static_cast<real>(minQuotient);
198 }
199
200
201
202
203
204
205 real
206 findSmallestQuotientOfFunctionAndSecondDerivative(ArrayRef<const double>        function,
207                                                   double                        inputSpacing,
208                                                   const std::pair<real, real>   &range)
209 {
210
211     std::size_t  firstIndex  = range.first  / inputSpacing;
212     std::size_t  lastIndex   = range.second / inputSpacing;
213     double       minQuotient = GMX_REAL_MAX;
214
215     for (std::size_t i = firstIndex + 1; (i + 1) < lastIndex; i++)
216     {
217         updateMinQuotientOfFunctionAndSecondDerivative(function[i-1], function[i], function[i+1], inputSpacing, &minQuotient);
218     }
219     return static_cast<real>(minQuotient);
220 }
221
222
223
224 /*! \brief Update minQuotient if the ratio of this function value and its third derivative is smaller
225  *
226  * This is a utility function used in the functions to find the smallest quotient
227  * in a range.
228  *
229  * \param[in]    previousPreviousPoint Value of function at x-2h.
230  * \param[in]    previousPoint         Value of function at x-h.
231  * \param[in]    thisPoint             Value of function at x.
232  * \param[in]    nextPoint             Value of function at x+h.
233  * \param[in]    nextNextPoint         Value of function at x+2h.
234  * \param[in]    spacing               Value of h.
235  * \param[inout] minQuotient   Current minimum of such quotients, updated if this quotient is smaller.
236  */
237 static void
238 updateMinQuotientOfFunctionAndThirdDerivative(double     previousPreviousPoint,
239                                               double     previousPoint,
240                                               double     thisPoint,
241                                               double     nextPoint,
242                                               double     nextNextPoint,
243                                               double     spacing,
244                                               double *   minQuotient)
245 {
246     double value            = std::abs( thisPoint );
247     double thirdDerivative  = std::abs((nextNextPoint - 2 * nextPoint + 2 * previousPoint - previousPreviousPoint) / (2 * spacing * spacing * spacing));
248
249     // Make sure we do not divide by zero. This limit is arbitrary,
250     // but it doesnt matter since this point will have a very large value,
251     // and the whole routine is searching for the smallest value.
252     thirdDerivative = std::max(thirdDerivative, static_cast<double>(std::sqrt(GMX_REAL_MIN)));
253
254     *minQuotient = std::min(*minQuotient, value / thirdDerivative);
255 }
256
257
258 real
259 findSmallestQuotientOfFunctionAndThirdDerivative(const std::function<double(double)>  &f,
260                                                  const std::pair<real, real>           &range)
261 {
262     // Since the numerical second derivative will evaluate extra points
263     // we shrink the interval slightly to avoid calling the function with values
264     // outside the range specified.
265     double                     h           = std::pow( GMX_DOUBLE_EPS, 0.2 ); // optimal spacing for 3rd derivative
266     std::pair<double, double>  newRange(range.first + 2*h, range.second - 2*h);
267     const int                  points      = 1000;                            // arbitrary
268     double                     dx          = (newRange.second - newRange.first) / points;
269     double                     minQuotient = GMX_REAL_MAX;
270
271     for (double x = newRange.first; x <= newRange.second; x += dx)
272     {
273         updateMinQuotientOfFunctionAndThirdDerivative(f(x-2*h), f(x-h), f(x), f(x+h), f(x+2*h), h, &minQuotient);
274     }
275     return static_cast<real>(minQuotient);
276 }
277
278
279 real
280 findSmallestQuotientOfFunctionAndThirdDerivative(ArrayRef<const double>        function,
281                                                  double                        inputSpacing,
282                                                  const std::pair<real, real>   &range)
283 {
284
285     std::size_t  firstIndex  = range.first  / inputSpacing;
286     std::size_t  lastIndex   = range.second / inputSpacing;
287     double       minQuotient = GMX_REAL_MAX;
288
289     for (std::size_t i = firstIndex + 2; (i + 2) < lastIndex; i++)
290     {
291         updateMinQuotientOfFunctionAndThirdDerivative(function[i-2], function[i-1], function[i], function[i+1], function[i+2], inputSpacing, &minQuotient);
292     }
293     return static_cast<real>(minQuotient);
294 }
295
296
297
298 std::vector<double>
299 vectorSecondDerivative(ArrayRef<const double> f, double spacing)
300 {
301     if (f.size() < 5)
302     {
303         GMX_THROW(APIError("Too few points in vector for 5-point differentiation"));
304     }
305
306     std::vector<double> d(f.size());
307     std::size_t         i;
308
309     // 5-point formula evaluated for points 0,1
310     i    = 0;
311     d[i] = (11 * f[i+4] - 56 * f[i+3] + 114 * f[i+2] - 104 * f[i+1] + 35 * f[i]) / ( 12 * spacing * spacing);
312     i    = 1;
313     d[i] = (-f[i+3] + 4 * f[i+2] + 6 * f[i+1] - 20 * f[i] + 11 * f[i-1]) / ( 12 * spacing * spacing);
314
315     for (std::size_t i = 2; i < d.size() - 2; i++)
316     {
317         // 5-point formula evaluated for central point (2)
318         d[i] = (-f[i+2] + 16 * f[i+1] - 30 * f[i] + 16 * f[i-1] - f[i-2]) / (12 * spacing * spacing);
319     }
320
321     // 5-point formula evaluated for points 3,4
322     i    = d.size() - 2;
323     d[i] = (11 * f[i+1] - 20 * f[i] + 6 * f[i-1] + 4 * f[i-2] - f[i-3]) / ( 12 * spacing * spacing);
324     i    = d.size() - 1;
325     d[i] = (35 * f[i] - 104 * f[i-1] + 114 * f[i-2] - 56 * f[i-3] + 11 * f[i-4]) / ( 12 * spacing * spacing);
326
327     return d;
328 }
329
330
331
332 }  // namespace internal
333
334 }  // namespace gmx