ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/math/CubicSpline.cpp
Revision: 2058
Committed: Tue Mar 3 15:35:45 2015 UTC (10 years, 2 months ago) by gezelter
File size: 9343 byte(s)
Log Message:
Replaced std::iota with explicit code

File Contents

# User Rev Content
1 gezelter 1475 /*
2     * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3     *
4     * The University of Notre Dame grants you ("Licensee") a
5     * non-exclusive, royalty free, license to use, modify and
6     * redistribute this software in source and binary code form, provided
7     * that the following conditions are met:
8     *
9     * 1. Redistributions of source code must retain the above copyright
10     * notice, this list of conditions and the following disclaimer.
11     *
12     * 2. Redistributions in binary form must reproduce the above copyright
13     * notice, this list of conditions and the following disclaimer in the
14     * documentation and/or other materials provided with the
15     * distribution.
16     *
17     * This software is provided "AS IS," without a warranty of any
18     * kind. All express or implied conditions, representations and
19     * warranties, including any implied warranty of merchantability,
20     * fitness for a particular purpose or non-infringement, are hereby
21     * excluded. The University of Notre Dame and its licensors shall not
22     * be liable for any damages suffered by licensee as a result of
23     * using, modifying or distributing the software or its
24     * derivatives. In no event will the University of Notre Dame or its
25     * licensors be liable for any lost revenue, profit or data, or for
26     * direct, indirect, special, consequential, incidental or punitive
27     * damages, however caused and regardless of the theory of liability,
28     * arising out of the use of or inability to use software, even if the
29     * University of Notre Dame has been advised of the possibility of
30     * such damages.
31     *
32     * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
33     * research, please cite the appropriate papers when you publish your
34     * work. Good starting points are:
35     *
36     * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
37     * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
38 gezelter 1879 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
39 gezelter 1665 * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010).
40     * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41 gezelter 1475 */
42    
43     #include "math/CubicSpline.hpp"
44     #include <cmath>
45 gezelter 1879 #include <cassert>
46 gezelter 1618 #include <cstdio>
47 gezelter 1475 #include <algorithm>
48 gezelter 2057 #include <numeric>
49 gezelter 1475
50     using namespace OpenMD;
51     using namespace std;
52    
53 gezelter 1536 CubicSpline::CubicSpline() : generated(false), isUniform(true) {
54 gezelter 2057 x_.clear();
55     y_.clear();
56 gezelter 1536 }
57 gezelter 1475
58 gezelter 1536 void CubicSpline::addPoint(const RealType xp, const RealType yp) {
59 gezelter 2057 x_.push_back(xp);
60     y_.push_back(yp);
61 gezelter 1475 }
62    
63     void CubicSpline::addPoints(const vector<RealType>& xps,
64     const vector<RealType>& yps) {
65    
66 gezelter 1879 assert(xps.size() == yps.size());
67    
68 gezelter 2057 for (unsigned int i = 0; i < xps.size(); i++){
69     x_.push_back(xps[i]);
70     y_.push_back(yps[i]);
71     }
72 gezelter 1475 }
73    
74     void CubicSpline::generate() {
75     // Calculate coefficients defining a smooth cubic interpolatory spline.
76     //
77     // class values constructed:
78 gezelter 1536 // n = number of data_ points.
79 gezelter 2057 // x_ = vector of independent variable values
80     // y_ = vector of dependent variable values
81     // b = vector of S'(x_[i]) values.
82     // c = vector of S"(x_[i])/2 values.
83     // d = vector of S'''(x_[i]+)/6 values (i < n).
84 gezelter 1475 // Local variables:
85 gezelter 1536
86 gezelter 1475 RealType fp1, fpn, h, p;
87    
88     // make sure the sizes match
89    
90 gezelter 2057 n = x_.size();
91 gezelter 1475 b.resize(n);
92     c.resize(n);
93     d.resize(n);
94    
95     // make sure we are monotonically increasing in x:
96    
97     bool sorted = true;
98    
99     for (int i = 1; i < n; i++) {
100 gezelter 2057 if ( (x_[i] - x_[i-1] ) <= 0.0 ) sorted = false;
101 gezelter 1475 }
102    
103     // sort if necessary
104    
105 gezelter 2057 if (!sorted) {
106     vector<int> p = sort_permutation(x_);
107     x_ = apply_permutation(x_, p);
108     y_ = apply_permutation(y_, p);
109     }
110 gezelter 1475
111 gezelter 2057
112 gezelter 1475 // Calculate coefficients for the tridiagonal system: store
113     // sub-diagonal in B, diagonal in D, difference quotient in C.
114    
115 gezelter 2057 b[0] = x_[1] - x_[0];
116     c[0] = (y_[1] - y_[0]) / b[0];
117 gezelter 1475
118     if (n == 2) {
119    
120     // Assume the derivatives at both endpoints are zero. Another
121     // assumption could be made to have a linear interpolant between
122     // the two points. In that case, the b coefficients below would be
123 gezelter 2057 // (y_[1] - y_[0]) / (x_[1] - x_[0])
124 gezelter 1475 // and the c and d coefficients would both be zero.
125     b[0] = 0.0;
126 gezelter 2057 c[0] = -3.0 * pow((y_[1] - y_[0]) / (x_[1] - x_[0]), 2);
127     d[0] = -2.0 * pow((y_[1] - y_[0]) / (x_[1] - x_[0]), 3);
128 gezelter 1475 b[1] = b[0];
129     c[1] = 0.0;
130     d[1] = 0.0;
131 gezelter 2057 dx = 1.0 / (x_[1] - x_[0]);
132 gezelter 1475 isUniform = true;
133     generated = true;
134     return;
135     }
136    
137     d[0] = 2.0 * b[0];
138    
139     for (int i = 1; i < n-1; i++) {
140 gezelter 2057 b[i] = x_[i+1] - x_[i];
141 gezelter 1475 if ( fabs( b[i] - b[0] ) / b[0] > 1.0e-5) isUniform = false;
142 gezelter 2057 c[i] = (y_[i+1] - y_[i]) / b[i];
143 gezelter 1475 d[i] = 2.0 * (b[i] + b[i-1]);
144     }
145    
146     d[n-1] = 2.0 * b[n-2];
147    
148     // Calculate estimates for the end slopes using polynomials
149 gezelter 1536 // that interpolate the data_ nearest the end.
150 gezelter 1475
151     fp1 = c[0] - b[0]*(c[1] - c[0])/(b[0] + b[1]);
152     if (n > 3) fp1 = fp1 + b[0]*((b[0] + b[1]) * (c[2] - c[1]) /
153     (b[1] + b[2]) -
154 gezelter 2057 c[1] + c[0]) / (x_[3] - x_[0]);
155 gezelter 1475
156     fpn = c[n-2] + b[n-2]*(c[n-2] - c[n-3])/(b[n-3] + b[n-2]);
157 gezelter 1879
158 gezelter 1475 if (n > 3) fpn = fpn + b[n-2] *
159 gezelter 1879 (c[n-2] - c[n-3] - (b[n-3] + b[n-2]) *
160     (c[n-3] - c[n-4])/(b[n-3] + b[n-4])) /
161 gezelter 2057 (x_[n-1] - x_[n-4]);
162 gezelter 1475
163     // Calculate the right hand side and store it in C.
164    
165     c[n-1] = 3.0 * (fpn - c[n-2]);
166     for (int i = n-2; i > 0; i--)
167     c[i] = 3.0 * (c[i] - c[i-1]);
168     c[0] = 3.0 * (c[0] - fp1);
169    
170     // Solve the tridiagonal system.
171    
172     for (int k = 1; k < n; k++) {
173     p = b[k-1] / d[k-1];
174     d[k] = d[k] - p*b[k-1];
175     c[k] = c[k] - p*c[k-1];
176     }
177    
178     c[n-1] = c[n-1] / d[n-1];
179    
180     for (int k = n-2; k >= 0; k--)
181     c[k] = (c[k] - b[k] * c[k+1]) / d[k];
182    
183     // Calculate the coefficients defining the spline.
184    
185     for (int i = 0; i < n-1; i++) {
186 gezelter 2057 h = x_[i+1] - x_[i];
187 gezelter 1475 d[i] = (c[i+1] - c[i]) / (3.0 * h);
188 gezelter 2057 b[i] = (y_[i+1] - y_[i])/h - h * (c[i] + h * d[i]);
189 gezelter 1475 }
190    
191     b[n-1] = b[n-2] + h * (2.0 * c[n-2] + h * 3.0 * d[n-2]);
192    
193 gezelter 2057 if (isUniform) dx = 1.0 / (x_[1] - x_[0]);
194 gezelter 1475
195     generated = true;
196     return;
197     }
198    
199 gezelter 1879 RealType CubicSpline::getValueAt(const RealType& t) {
200 gezelter 1475 // Evaluate the spline at t using coefficients
201     //
202     // Input parameters
203     // t = point where spline is to be evaluated.
204     // Output:
205     // value of spline at t.
206    
207     if (!generated) generate();
208    
209 gezelter 2057 assert(t >= x_.front());
210     assert(t <= x_.back());
211 gezelter 1879
212     // Find the interval ( x[j], x[j+1] ) that contains or is nearest
213     // to t.
214    
215     if (isUniform) {
216    
217 gezelter 2057 j = max(0, min(n-1, int((t - x_[0]) * dx)));
218 gezelter 1879
219     } else {
220    
221     j = n-1;
222    
223     for (int i = 0; i < n; i++) {
224 gezelter 2057 if ( t < x_[i] ) {
225 gezelter 1879 j = i-1;
226     break;
227     }
228     }
229 gezelter 1475 }
230 gezelter 1879
231     // Evaluate the cubic polynomial.
232    
233 gezelter 2057 dt = t - x_[j];
234     return y_[j] + dt*(b[j] + dt*(c[j] + dt*d[j]));
235 gezelter 1879 }
236 gezelter 1475
237 gezelter 1879
238     void CubicSpline::getValueAt(const RealType& t, RealType& v) {
239     // Evaluate the spline at t using coefficients
240     //
241     // Input parameters
242     // t = point where spline is to be evaluated.
243     // Output:
244     // value of spline at t.
245    
246     if (!generated) generate();
247    
248 gezelter 2057 assert(t >= x_.front());
249     assert(t <= x_.back());
250 gezelter 1879
251 gezelter 1475 // Find the interval ( x[j], x[j+1] ) that contains or is nearest
252     // to t.
253    
254     if (isUniform) {
255    
256 gezelter 2057 j = max(0, min(n-1, int((t - x_[0]) * dx)));
257 gezelter 1475
258     } else {
259    
260     j = n-1;
261    
262     for (int i = 0; i < n; i++) {
263 gezelter 2057 if ( t < x_[i] ) {
264 gezelter 1475 j = i-1;
265     break;
266     }
267     }
268     }
269    
270     // Evaluate the cubic polynomial.
271    
272 gezelter 2057 dt = t - x_[j];
273     v = y_[j] + dt*(b[j] + dt*(c[j] + dt*d[j]));
274 gezelter 1475 }
275    
276 gezelter 1928 pair<RealType, RealType> CubicSpline::getLimits(){
277     if (!generated) generate();
278 gezelter 2057 return make_pair( x_.front(), x_.back() );
279 gezelter 1928 }
280    
281 gezelter 1879 void CubicSpline::getValueAndDerivativeAt(const RealType& t, RealType& v,
282     RealType &dv) {
283     // Evaluate the spline and first derivative at t using coefficients
284     //
285     // Input parameters
286     // t = point where spline is to be evaluated.
287    
288     if (!generated) generate();
289    
290 gezelter 2057 assert(t >= x_.front());
291     assert(t <= x_.back());
292 gezelter 1879
293     // Find the interval ( x[j], x[j+1] ) that contains or is nearest
294     // to t.
295    
296     if (isUniform) {
297    
298 gezelter 2057 j = max(0, min(n-1, int((t - x_[0]) * dx)));
299 gezelter 1879
300     } else {
301    
302     j = n-1;
303    
304     for (int i = 0; i < n; i++) {
305 gezelter 2057 if ( t < x_[i] ) {
306 gezelter 1879 j = i-1;
307     break;
308     }
309     }
310     }
311    
312     // Evaluate the cubic polynomial.
313    
314 gezelter 2057 dt = t - x_[j];
315 gezelter 1879
316 gezelter 2057 v = y_[j] + dt*(b[j] + dt*(c[j] + dt*d[j]));
317 gezelter 1879 dv = b[j] + dt*(2.0 * c[j] + 3.0 * dt * d[j]);
318     }
319 gezelter 2057
320     std::vector<int> CubicSpline::sort_permutation(std::vector<RealType>& v) {
321     std::vector<int> p(v.size());
322 gezelter 2058
323     // 6 lines to replace std::iota(p.begin(), p.end(), 0);
324     int value = 0;
325     std::vector<int>::iterator i;
326     for (i = p.begin(); i != p.end(); ++i) {
327     (*i) = value;
328     ++value;
329     }
330    
331 gezelter 2057 std::sort(p.begin(), p.end(), OpenMD::Comparator(v) );
332     return p;
333     }
334    
335     std::vector<RealType> CubicSpline::apply_permutation(std::vector<RealType> const& v,
336     std::vector<int> const& p) {
337     int n = p.size();
338     std::vector<double> sorted_vec(n);
339     for (int i = 0; i < n; ++i) {
340     sorted_vec[i] = v[p[i]];
341     }
342     return sorted_vec;
343     }

Properties

Name Value
svn:eol-style native