| 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).           | 
| 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 "utils/simError.h" | 
| 44 | 
  | 
#include <cmath> | 
| 45 | 
+ | 
#include <cassert> | 
| 46 | 
  | 
#include <cstdio> | 
| 47 | 
  | 
#include <algorithm> | 
| 48 | 
  | 
 | 
| 60 | 
  | 
void CubicSpline::addPoints(const vector<RealType>& xps,  | 
| 61 | 
  | 
                            const vector<RealType>& yps) { | 
| 62 | 
  | 
   | 
| 63 | 
< | 
  if (xps.size() != yps.size()) { | 
| 64 | 
< | 
    printf( painCave.errMsg, | 
| 65 | 
< | 
            "CubicSpline::addPoints was passed vectors of different length!\n"); | 
| 66 | 
< | 
    painCave.severity = OPENMD_ERROR; | 
| 67 | 
< | 
    painCave.isFatal = 1; | 
| 68 | 
< | 
    simError();     | 
| 69 | 
< | 
  } | 
| 70 | 
< | 
 | 
| 71 | 
< | 
  for (int i = 0; i < xps.size(); i++)  | 
| 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 | 
  | 
 | 
| 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 | 
< | 
 | 
| 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]))/(data_[n-1].first - data_[n-4].first); | 
| 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 | 
  | 
   | 
| 160 | 
– | 
   | 
| 155 | 
  | 
  // Calculate the right hand side and store it in C. | 
| 156 | 
  | 
   | 
| 157 | 
  | 
  c[n-1] = 3.0 * (fpn - c[n-2]); | 
| 197 | 
  | 
  //   value of spline at t. | 
| 198 | 
  | 
   | 
| 199 | 
  | 
  if (!generated) generate(); | 
| 206 | 
– | 
  RealType dt; | 
| 200 | 
  | 
   | 
| 201 | 
< | 
  if ( t < data_[0].first || t > data_[n-1].first ) {     | 
| 202 | 
< | 
    sprintf( painCave.errMsg, | 
| 210 | 
< | 
             "CubicSpline::getValueAt was passed a value outside the range of the spline!\n"); | 
| 211 | 
< | 
    painCave.severity = OPENMD_ERROR; | 
| 212 | 
< | 
    painCave.isFatal = 1; | 
| 213 | 
< | 
    simError();     | 
| 214 | 
< | 
  } | 
| 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 | 
  | 
 | 
| 219 | 
– | 
  int j; | 
| 220 | 
– | 
 | 
| 207 | 
  | 
  if (isUniform) {     | 
| 208 | 
  | 
     | 
| 209 | 
  | 
    j = max(0, min(n-1, int((t - data_[0].first) * dx)));    | 
| 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])); | 
| 241 | 
< | 
   | 
| 226 | 
> | 
  return data_[j].second + dt*(b[j] + dt*(c[j] + dt*d[j]));   | 
| 227 | 
  | 
} | 
| 228 | 
  | 
 | 
| 229 | 
  | 
 | 
| 236 | 
  | 
  //   pair containing value of spline at t and first derivative at t | 
| 237 | 
  | 
 | 
| 238 | 
  | 
  if (!generated) generate(); | 
| 254 | 
– | 
  RealType dt; | 
| 239 | 
  | 
   | 
| 240 | 
< | 
  if ( t < data_.front().first || t > data_.back().first ) {     | 
| 241 | 
< | 
    sprintf( painCave.errMsg, | 
| 258 | 
< | 
             "CubicSpline::getValueAndDerivativeAt was passed a value outside the range of the spline!\n"); | 
| 259 | 
< | 
    painCave.severity = OPENMD_ERROR; | 
| 260 | 
< | 
    painCave.isFatal = 1; | 
| 261 | 
< | 
    simError();     | 
| 262 | 
< | 
  } | 
| 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 | 
  | 
 | 
| 267 | 
– | 
  int j; | 
| 268 | 
– | 
 | 
| 246 | 
  | 
  if (isUniform) {     | 
| 247 | 
  | 
     | 
| 248 | 
  | 
    j = max(0, min(n-1, int((t - data_[0].first) * dx)));    | 
| 263 | 
  | 
   | 
| 264 | 
  | 
  dt = t - data_[j].first; | 
| 265 | 
  | 
 | 
| 266 | 
< | 
  RealType yval = data_[j].second + dt*(b[j] + dt*(c[j] + dt*d[j])); | 
| 267 | 
< | 
  RealType dydx = b[j] + dt*(2.0 * c[j] + 3.0 * dt * d[j]); | 
| 266 | 
> | 
  yval = data_[j].second + dt*(b[j] + dt*(c[j] + dt*d[j])); | 
| 267 | 
> | 
  dydx = b[j] + dt*(2.0 * c[j] + 3.0 * dt * d[j]); | 
| 268 | 
  | 
   | 
| 269 | 
  | 
  return make_pair(yval, dydx); | 
| 270 | 
  | 
} |