ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/math/CubicSpline.cpp
Revision: 1479
Committed: Mon Jul 26 19:00:48 2010 UTC (14 years, 9 months ago) by gezelter
Original Path: branches/development/src/math/CubicSpline.cpp
File size: 8685 byte(s)
Log Message:
Added EAM.  Still segfaults but compiles.

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

Properties

Name Value
svn:eol-style native