ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/math/CubicSpline.cpp
Revision: 1928
Committed: Sat Aug 17 13:03:17 2013 UTC (11 years, 8 months ago) by gezelter
File size: 10044 byte(s)
Log Message:
Fixed a few bugs in EAM and Electrostatic.

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

Properties

Name Value
svn:eol-style native