| 1 | /* | 
| 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, 234107 (2008). | 
| 39 | * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 | * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). | 
| 41 | */ | 
| 42 |  | 
| 43 | #include "math/CubicSpline.hpp" | 
| 44 | #include <cmath> | 
| 45 | #include <cassert> | 
| 46 | #include <cstdio> | 
| 47 | #include <algorithm> | 
| 48 |  | 
| 49 | using namespace OpenMD; | 
| 50 | using namespace std; | 
| 51 |  | 
| 52 | CubicSpline::CubicSpline() : generated(false), isUniform(true) { | 
| 53 | data_.clear(); | 
| 54 | } | 
| 55 |  | 
| 56 | void CubicSpline::addPoint(const RealType xp, const RealType yp) { | 
| 57 | data_.push_back(make_pair(xp, yp)); | 
| 58 | } | 
| 59 |  | 
| 60 | void CubicSpline::addPoints(const vector<RealType>& xps, | 
| 61 | const vector<RealType>& yps) { | 
| 62 |  | 
| 63 | assert(xps.size() == yps.size()); | 
| 64 |  | 
| 65 | for (unsigned int i = 0; i < xps.size(); i++) | 
| 66 | data_.push_back(make_pair(xps[i], yps[i])); | 
| 67 | } | 
| 68 |  | 
| 69 | void CubicSpline::generate() { | 
| 70 | // Calculate coefficients defining a smooth cubic interpolatory spline. | 
| 71 | // | 
| 72 | // class values constructed: | 
| 73 | //   n   = number of data_ points. | 
| 74 | //   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 |  | 
| 81 | RealType fp1, fpn, h, p; | 
| 82 |  | 
| 83 | // make sure the sizes match | 
| 84 |  | 
| 85 | n = data_.size(); | 
| 86 | 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 | if ( (data_[i].first - data_[i-1].first ) <= 0.0 ) sorted = false; | 
| 96 | } | 
| 97 |  | 
| 98 | // sort if necessary | 
| 99 |  | 
| 100 | if (!sorted) sort(data_.begin(), data_.end()); | 
| 101 |  | 
| 102 | // Calculate coefficients for the tridiagonal system: store | 
| 103 | // sub-diagonal in B, diagonal in D, difference quotient in C. | 
| 104 |  | 
| 105 | b[0] = data_[1].first - data_[0].first; | 
| 106 | c[0] = (data_[1].second - data_[0].second) / b[0]; | 
| 107 |  | 
| 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 | // (data_[1].second - data_[0].second) / (data_[1].first - data_[0].first) | 
| 114 | // and the c and d coefficients would both be zero. | 
| 115 | b[0] = 0.0; | 
| 116 | 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 | b[1] = b[0]; | 
| 121 | c[1] = 0.0; | 
| 122 | d[1] = 0.0; | 
| 123 | dx = 1.0 / (data_[1].first - data_[0].first); | 
| 124 | 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 | b[i] = data_[i+1].first - data_[i].first; | 
| 133 | if ( fabs( b[i] - b[0] ) / b[0] > 1.0e-5) isUniform = false; | 
| 134 | c[i] = (data_[i+1].second - data_[i].second) / b[i]; | 
| 135 | 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 | // that interpolate the data_ nearest the end. | 
| 142 |  | 
| 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 | c[1] + c[0]) / (data_[3].first - data_[0].first); | 
| 147 |  | 
| 148 | fpn = c[n-2] + b[n-2]*(c[n-2] - c[n-3])/(b[n-3] + b[n-2]); | 
| 149 |  | 
| 150 | if (n > 3)  fpn = fpn + b[n-2] * | 
| 151 | (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 |  | 
| 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 | h = data_[i+1].first - data_[i].first; | 
| 179 | d[i] = (c[i+1] - c[i]) / (3.0 * h); | 
| 180 | b[i] = (data_[i+1].second - data_[i].second)/h - h * (c[i] + h * d[i]); | 
| 181 | } | 
| 182 |  | 
| 183 | b[n-1] = b[n-2] + h * (2.0 * c[n-2] + h * 3.0 * d[n-2]); | 
| 184 |  | 
| 185 | if (isUniform) dx = 1.0 / (data_[1].first - data_[0].first); | 
| 186 |  | 
| 187 | generated = true; | 
| 188 | return; | 
| 189 | } | 
| 190 |  | 
| 191 | RealType CubicSpline::getValueAt(const RealType& t) { | 
| 192 | // 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 | assert(t >= data_.front().first); | 
| 202 | assert(t <= data_.back().first); | 
| 203 |  | 
| 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 | } | 
| 222 |  | 
| 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 |  | 
| 229 |  | 
| 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 | assert(t >= data_.front().first); | 
| 241 | assert(t <= data_.back().first); | 
| 242 |  | 
| 243 | //  Find the interval ( x[j], x[j+1] ) that contains or is nearest | 
| 244 | //  to t. | 
| 245 |  | 
| 246 | if (isUniform) { | 
| 247 |  | 
| 248 | j = max(0, min(n-1, int((t - data_[0].first) * dx))); | 
| 249 |  | 
| 250 | } else { | 
| 251 |  | 
| 252 | j = n-1; | 
| 253 |  | 
| 254 | for (int i = 0; i < n; i++) { | 
| 255 | if ( t < data_[i].first ) { | 
| 256 | j = i-1; | 
| 257 | break; | 
| 258 | } | 
| 259 | } | 
| 260 | } | 
| 261 |  | 
| 262 | //  Evaluate the cubic polynomial. | 
| 263 |  | 
| 264 | dt = t - data_[j].first; | 
| 265 | v = data_[j].second + dt*(b[j] + dt*(c[j] + dt*d[j])); | 
| 266 | } | 
| 267 |  | 
| 268 |  | 
| 269 | pair<RealType, RealType> CubicSpline::getValueAndDerivativeAt(const RealType& t){ | 
| 270 | // 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 | assert(t >= data_.front().first); | 
| 280 | assert(t <= data_.back().first); | 
| 281 |  | 
| 282 | //  Find the interval ( x[j], x[j+1] ) that contains or is nearest | 
| 283 | //  to t. | 
| 284 |  | 
| 285 | if (isUniform) { | 
| 286 |  | 
| 287 | j = max(0, min(n-1, int((t - data_[0].first) * dx))); | 
| 288 |  | 
| 289 | } else { | 
| 290 |  | 
| 291 | j = n-1; | 
| 292 |  | 
| 293 | for (int i = 0; i < n; i++) { | 
| 294 | if ( t < data_[i].first ) { | 
| 295 | j = i-1; | 
| 296 | break; | 
| 297 | } | 
| 298 | } | 
| 299 | } | 
| 300 |  | 
| 301 | //  Evaluate the cubic polynomial. | 
| 302 |  | 
| 303 | dt = t - data_[j].first; | 
| 304 |  | 
| 305 | 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 | return make_pair(yval, dydx); | 
| 309 | } | 
| 310 |  | 
| 311 | pair<RealType, RealType> CubicSpline::getLimits(){ | 
| 312 | if (!generated) generate(); | 
| 313 | return make_pair( data_.front().first, data_.back().first ); | 
| 314 | } | 
| 315 |  | 
| 316 | 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 | assert(t >= data_.front().first); | 
| 326 | assert(t <= data_.back().first); | 
| 327 |  | 
| 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 | } |