ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/math/CubicSpline.cpp
Revision: 1536
Committed: Wed Jan 5 14:49:05 2011 UTC (14 years, 3 months ago) by gezelter
File size: 8747 byte(s)
Log Message:
compiles, builds and runs, but is very slow

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     * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008).
39     * [4] Vardeman & Gezelter, in progress (2009).
40     */
41    
42     #include "math/CubicSpline.hpp"
43     #include "utils/simError.h"
44     #include <cmath>
45     #include <algorithm>
46    
47     using namespace OpenMD;
48     using namespace std;
49    
50 gezelter 1536 CubicSpline::CubicSpline() : generated(false), isUniform(true) {
51     data_.clear();
52     }
53 gezelter 1475
54 gezelter 1536 void CubicSpline::addPoint(const RealType xp, const RealType yp) {
55     data_.push_back(make_pair(xp, yp));
56 gezelter 1475 }
57    
58     void CubicSpline::addPoints(const vector<RealType>& xps,
59     const vector<RealType>& yps) {
60    
61     if (xps.size() != yps.size()) {
62     printf( painCave.errMsg,
63     "CubicSpline::addPoints was passed vectors of different length!\n");
64     painCave.severity = OPENMD_ERROR;
65     painCave.isFatal = 1;
66     simError();
67     }
68    
69     for (int i = 0; i < xps.size(); i++)
70 gezelter 1536 data_.push_back(make_pair(xps[i], yps[i]));
71 gezelter 1475 }
72    
73     void CubicSpline::generate() {
74     // Calculate coefficients defining a smooth cubic interpolatory spline.
75     //
76     // class values constructed:
77 gezelter 1536 // n = number of data_ points.
78 gezelter 1475 // x = vector of independent variable values
79     // y = vector of dependent variable values
80     // b = vector of S'(x[i]) values.
81     // c = vector of S"(x[i])/2 values.
82     // d = vector of S'''(x[i]+)/6 values (i < n).
83     // Local variables:
84 gezelter 1536
85 gezelter 1475 RealType fp1, fpn, h, p;
86    
87     // make sure the sizes match
88    
89 gezelter 1536 n = data_.size();
90 gezelter 1475 b.resize(n);
91     c.resize(n);
92     d.resize(n);
93    
94     // make sure we are monotonically increasing in x:
95    
96     bool sorted = true;
97    
98     for (int i = 1; i < n; i++) {
99 gezelter 1536 if ( (data_[i].first - data_[i-1].first ) <= 0.0 ) sorted = false;
100 gezelter 1475 }
101    
102     // sort if necessary
103    
104 gezelter 1536 if (!sorted) sort(data_.begin(), data_.end());
105 gezelter 1475
106     // Calculate coefficients for the tridiagonal system: store
107     // sub-diagonal in B, diagonal in D, difference quotient in C.
108    
109 gezelter 1536 b[0] = data_[1].first - data_[0].first;
110     c[0] = (data_[1].second - data_[0].second) / b[0];
111 gezelter 1475
112     if (n == 2) {
113    
114     // Assume the derivatives at both endpoints are zero. Another
115     // assumption could be made to have a linear interpolant between
116     // the two points. In that case, the b coefficients below would be
117 gezelter 1536 // (data_[1].second - data_[0].second) / (data_[1].first - data_[0].first)
118 gezelter 1475 // and the c and d coefficients would both be zero.
119     b[0] = 0.0;
120 gezelter 1536 c[0] = -3.0 * pow((data_[1].second - data_[0].second) /
121     (data_[1].first-data_[0].first), 2);
122     d[0] = -2.0 * pow((data_[1].second - data_[0].second) /
123     (data_[1].first-data_[0].first), 3);
124 gezelter 1475 b[1] = b[0];
125     c[1] = 0.0;
126     d[1] = 0.0;
127 gezelter 1536 dx = 1.0 / (data_[1].first - data_[0].first);
128 gezelter 1475 isUniform = true;
129     generated = true;
130     return;
131     }
132    
133     d[0] = 2.0 * b[0];
134    
135     for (int i = 1; i < n-1; i++) {
136 gezelter 1536 b[i] = data_[i+1].first - data_[i].first;
137 gezelter 1475 if ( fabs( b[i] - b[0] ) / b[0] > 1.0e-5) isUniform = false;
138 gezelter 1536 c[i] = (data_[i+1].second - data_[i].second) / b[i];
139 gezelter 1475 d[i] = 2.0 * (b[i] + b[i-1]);
140     }
141    
142     d[n-1] = 2.0 * b[n-2];
143    
144     // Calculate estimates for the end slopes using polynomials
145 gezelter 1536 // that interpolate the data_ nearest the end.
146 gezelter 1475
147     fp1 = c[0] - b[0]*(c[1] - c[0])/(b[0] + b[1]);
148     if (n > 3) fp1 = fp1 + b[0]*((b[0] + b[1]) * (c[2] - c[1]) /
149     (b[1] + b[2]) -
150 gezelter 1536 c[1] + c[0]) / (data_[3].first - data_[0].first);
151 gezelter 1475
152     fpn = c[n-2] + b[n-2]*(c[n-2] - c[n-3])/(b[n-3] + b[n-2]);
153    
154     if (n > 3) fpn = fpn + b[n-2] *
155     (c[n-2] - c[n-3] - (b[n-3] + b[n-2]) *
156 gezelter 1536 (c[n-3] - c[n-4])/(b[n-3] + b[n-4]))/(data_[n-1].first - data_[n-4].first);
157 gezelter 1475
158    
159     // Calculate the right hand side and store it in C.
160    
161     c[n-1] = 3.0 * (fpn - c[n-2]);
162     for (int i = n-2; i > 0; i--)
163     c[i] = 3.0 * (c[i] - c[i-1]);
164     c[0] = 3.0 * (c[0] - fp1);
165    
166     // Solve the tridiagonal system.
167    
168     for (int k = 1; k < n; k++) {
169     p = b[k-1] / d[k-1];
170     d[k] = d[k] - p*b[k-1];
171     c[k] = c[k] - p*c[k-1];
172     }
173    
174     c[n-1] = c[n-1] / d[n-1];
175    
176     for (int k = n-2; k >= 0; k--)
177     c[k] = (c[k] - b[k] * c[k+1]) / d[k];
178    
179     // Calculate the coefficients defining the spline.
180    
181     for (int i = 0; i < n-1; i++) {
182 gezelter 1536 h = data_[i+1].first - data_[i].first;
183 gezelter 1475 d[i] = (c[i+1] - c[i]) / (3.0 * h);
184 gezelter 1536 b[i] = (data_[i+1].second - data_[i].second)/h - h * (c[i] + h * d[i]);
185 gezelter 1475 }
186    
187     b[n-1] = b[n-2] + h * (2.0 * c[n-2] + h * 3.0 * d[n-2]);
188    
189 gezelter 1536 if (isUniform) dx = 1.0 / (data_[1].first - data_[0].first);
190 gezelter 1475
191     generated = true;
192     return;
193     }
194    
195     RealType CubicSpline::getValueAt(RealType t) {
196     // Evaluate the spline at t using coefficients
197     //
198     // Input parameters
199     // t = point where spline is to be evaluated.
200     // Output:
201     // value of spline at t.
202    
203     if (!generated) generate();
204     RealType dt;
205    
206 gezelter 1536 if ( t < data_[0].first || t > data_[n-1].first ) {
207 gezelter 1475 sprintf( painCave.errMsg,
208     "CubicSpline::getValueAt was passed a value outside the range of the spline!\n");
209     painCave.severity = OPENMD_ERROR;
210     painCave.isFatal = 1;
211     simError();
212     }
213    
214     // Find the interval ( x[j], x[j+1] ) that contains or is nearest
215     // to t.
216    
217     int j;
218    
219     if (isUniform) {
220    
221 gezelter 1536 j = max(0, min(n-1, int((t - data_[0].first) * dx)));
222 gezelter 1475
223     } else {
224    
225     j = n-1;
226    
227     for (int i = 0; i < n; i++) {
228 gezelter 1536 if ( t < data_[i].first ) {
229 gezelter 1475 j = i-1;
230     break;
231     }
232     }
233     }
234    
235     // Evaluate the cubic polynomial.
236    
237 gezelter 1536 dt = t - data_[j].first;
238     return data_[j].second + dt*(b[j] + dt*(c[j] + dt*d[j]));
239 gezelter 1475
240     }
241    
242    
243     pair<RealType, RealType> CubicSpline::getValueAndDerivativeAt(RealType t) {
244     // Evaluate the spline and first derivative at t using coefficients
245     //
246     // Input parameters
247     // t = point where spline is to be evaluated.
248     // Output:
249     // pair containing value of spline at t and first derivative at t
250    
251     if (!generated) generate();
252     RealType dt;
253    
254 gezelter 1536 if ( t < data_.front().first || t > data_.back().first ) {
255 gezelter 1475 sprintf( painCave.errMsg,
256     "CubicSpline::getValueAndDerivativeAt was passed a value outside the range of the spline!\n");
257     painCave.severity = OPENMD_ERROR;
258     painCave.isFatal = 1;
259     simError();
260     }
261    
262     // Find the interval ( x[j], x[j+1] ) that contains or is nearest
263     // to t.
264    
265     int j;
266    
267     if (isUniform) {
268    
269 gezelter 1536 j = max(0, min(n-1, int((t - data_[0].first) * dx)));
270 gezelter 1475
271     } else {
272    
273     j = n-1;
274    
275     for (int i = 0; i < n; i++) {
276 gezelter 1536 if ( t < data_[i].first ) {
277 gezelter 1475 j = i-1;
278     break;
279     }
280     }
281     }
282    
283     // Evaluate the cubic polynomial.
284    
285 gezelter 1536 dt = t - data_[j].first;
286 gezelter 1475
287 gezelter 1536 RealType yval = data_[j].second + dt*(b[j] + dt*(c[j] + dt*d[j]));
288 gezelter 1475 RealType dydx = b[j] + dt*(2.0 * c[j] + 3.0 * dt * d[j]);
289    
290     return make_pair(yval, dydx);
291     }

Properties

Name Value
svn:eol-style native