6 namespace INTERPOLATION {
14 constexpr size_t line = 64;
15 static_assert(
sizeof(gsl_interp_accel) <= line,
"gsl_interp_accel larger than a cache line");
16 void * mem = std::aligned_alloc(line, line);
18 throw std::bad_alloc();
19 auto * acc =
static_cast<gsl_interp_accel *
>(mem);
20 gsl_interp_accel_reset(acc);
36 static inline gsl_interp_accel *
get_thread_accel(
const std::vector<gsl_interp_accel *> & accs) {
37 const int level = omp_get_level();
39 const int id = omp_get_thread_num();
40 return id < int(accs.size()) ? accs[id] :
nullptr;
42 if (omp_get_active_level() > 1)
45 for (
int l = 1; l <= level; l++)
46 if (omp_get_team_size(l) > 1)
47 id = omp_get_ancestor_thread_num(l);
48 return id < int(accs.size()) ? accs[id] :
nullptr;
53 void GSLSpline::throw_error(std::string errormessage)
const {
54#if defined(USE_MPI) && !defined(SPLINE_THROW_ON_ERROR)
55 std::cout << errormessage << std::flush;
56 MPI_Abort(MPI_COMM_WORLD, 1);
59 throw std::runtime_error(errormessage);
64 void GSLSpline2D::throw_error(std::string errormessage)
const {
65#if defined(USE_MPI) && !defined(SPLINE_THROW_ON_ERROR)
66 std::cout << errormessage << std::flush;
67 MPI_Abort(MPI_COMM_WORLD, 1);
70 throw std::runtime_error(errormessage);
83 std::string splinename,
84 const gsl_interp_type * interpoltype) {
85 create(x,
y, nx, splinename, interpoltype);
90 std::string splinename,
91 const gsl_interp_type * interpoltype) {
93 if (x.size() !=
y.size())
94 throw std::runtime_error(
95 "GSLSpline::create The input vectors x, y don't have incompatible lengths!");
96 create(x,
y, splinename, interpoltype);
105 if (rhs.spline !=
nullptr) {
106 create(rhs.spline->x, rhs.spline->y, rhs.size_x, rhs.name, rhs.interpoltype_used);
111 out_of_bounds_warning = rhs.out_of_bounds_warning;
121 out_of_bounds_warning = rhs.out_of_bounds_warning;
123 if (rhs.spline !=
nullptr) {
124 create(rhs.spline->x, rhs.spline->y, rhs.size_x, rhs.name, rhs.interpoltype_used);
128 GSLSpline::operator bool()
const {
return (
spline !=
nullptr); }
137 std::swap(
spline, rhs.spline);
139 std::swap(xaccs, rhs.xaccs);
141 std::swap(xacc, rhs.xacc);
143 std::swap(interpoltype_used, rhs.interpoltype_used);
144 std::swap(size_x, rhs.size_x);
145 std::swap(xmin, rhs.xmin);
146 std::swap(xmax, rhs.xmax);
147 std::swap(dx_min, rhs.dx_min);
148 std::swap(dx_max, rhs.dx_max);
149 std::swap(
name, rhs.name);
150 std::swap(out_of_bounds_warning, rhs.out_of_bounds_warning);
162 std::string splinename,
163 const gsl_interp_type * interpoltype) {
165 if (spline !=
nullptr) {
171 const int nmin = std::max(2,
int(gsl_interp_type_min_size(interpoltype)));
173 std::string errormessage =
"[GSLSpline::create] spline " + name +
" needs at least " +
174 std::to_string(nmin) +
" points for interpolation type " +
175 std::string(interpoltype->name) +
", got " + std::to_string(nx) +
"\n";
176 throw_error(errormessage);
180 int sign_one = x[1] > x[0] ? 1.0 : -1.0;
181 for (
int i = 1; i < nx; i++) {
182 if (!((x[i] - x[i - 1]) * sign_one > 0)) {
183 std::string errormessage =
184 "[GSLSpline::create] x-array for spline " + name +
" is not monotone\n";
185 throw_error(errormessage);
195 for (
int i = 0; i < nx; i++) {
196 xx[i] = x[nx - 1 - i];
197 yy[i] =
y[nx - 1 - i];
206 dx_min = (x[1] - x[0]) / 2.0;
207 dx_max = (x[nx - 1] - x[nx - 2]) / 2.0;
210 interpoltype_used = interpoltype;
213 spline = gsl_spline_alloc(interpoltype, nx);
214 gsl_spline_init(spline, x,
y, nx);
224 int id = omp_get_thread_num();
226 nthreads = omp_get_num_threads();
234 xaccs = std::vector<gsl_interp_accel *>(nthreads);
235 for (
auto & xa : xaccs) {
245 std::string splinename,
246 const gsl_interp_type * interpoltype) {
247 if (x.size() !=
y.size()) {
248 std::string errormessage =
249 "[GSLSpline::create] x and y array must have the same number of elements for spline " + name +
251 throw_error(errormessage);
253 create(x.data(),
y.data(),
int(x.size()), splinename, interpoltype);
264 if (spline ==
nullptr) {
265 std::string errormessage =
"[GSLSpline::eval] Spline " + name +
" has not been created!\n";
266 throw_error(errormessage);
270 out_of_bounds_check(x);
271 x = std::max(x, xmin);
272 x = std::min(x, xmax);
278 gsl_interp_accel * xacc_thread = xacc;
280 return gsl_spline_eval(spline, x, xacc_thread);
284 if (spline ==
nullptr) {
285 std::string errormessage =
"[GSLSpline::eval_deriv] Spline " + name +
" has not been created!\n";
286 throw_error(errormessage);
288 if (deriv < 0 || deriv > 2) {
289 std::string errormessage =
"[GSLSpline::eval_deriv] Got deriv = " + std::to_string(deriv) +
290 " for spline " + name +
" Expected 0 = f, 1 = f' or 2 = f''\n";
291 throw_error(errormessage);
295 out_of_bounds_check(x);
296 x = std::max(x, xmin);
297 x = std::min(x, xmax);
303 gsl_interp_accel * xacc_thread = xacc;
308 dydx = gsl_spline_eval(spline, x, xacc_thread);
309 }
else if (deriv == 1) {
310 dydx = gsl_spline_eval_deriv(spline, x, xacc_thread);
311 }
else if (deriv == 2) {
312 dydx = gsl_spline_eval_deriv2(spline, x, xacc_thread);
321 if (spline !=
nullptr) {
325 dx_min = dx_max = 0.0;
328 gsl_spline_free(spline);
333 for (
auto & xa : xaccs) {
334 gsl_interp_accel_free(xa);
336 std::vector<gsl_interp_accel *>().swap(xaccs);
338 gsl_interp_accel_free(xacc);
347 void GSLSpline::out_of_bounds_check(
double x)
const {
348 if (out_of_bounds_warning) {
349 if (x < xmin - dx_min || x > xmax + dx_max) {
350 std::cout <<
"Warning GSLSpline[" << name <<
"] ";
351 std::cout <<
"x = " << x <<
" is out of bounds (" << xmin <<
"," << xmax <<
")\n";
358 if (spline ==
nullptr) {
359 std::string errormessage =
"[GSLSpline::integral] Spline " + name +
" has not been created!\n";
360 throw_error(errormessage);
364 a = std::min(std::max(a, xmin), xmax);
365 b = std::min(std::max(b, xmin), xmax);
369 gsl_interp_accel * xacc_thread = xacc;
371 return gsl_spline_eval_integ(spline, a, b, xacc_thread);
394 std::string splinename,
395 const gsl_interp2d_type * interpoltype) {
396 create(x,
y, z, nx, ny, splinename, interpoltype);
402 std::string splinename,
403 const gsl_interp2d_type * interpoltype) {
404 create(x,
y, z, splinename, interpoltype);
410 std::string splinename,
411 const gsl_interp2d_type * interpoltype) {
414 bool ok = (x.size() == z.size());
415 ok = ok and (z.size() > 0 ? z[0].size() ==
y.size() :
true);
417 throw std::runtime_error(
418 "GSLSpline2D::create The input vectors x, y, z don't have incompatible lengths!");
419 create(x,
y, z, splinename, interpoltype);
428 if (rhs.spline !=
nullptr) {
435 rhs.interpoltype_used);
440 out_of_bounds_warning = rhs.out_of_bounds_warning;
450 out_of_bounds_warning = rhs.out_of_bounds_warning;
452 if (rhs.spline !=
nullptr) {
459 rhs.interpoltype_used);
466 GSLSpline2D::operator bool()
const {
return (
spline !=
nullptr); }
475 std::swap(
spline, rhs.spline);
477 std::swap(xaccs, rhs.xaccs);
478 std::swap(yaccs, rhs.yaccs);
480 std::swap(xacc, rhs.xacc);
481 std::swap(yacc, rhs.yacc);
483 std::swap(interpoltype_used, rhs.interpoltype_used);
484 std::swap(size_x, rhs.size_x);
485 std::swap(size_y, rhs.size_y);
486 std::swap(xmin, rhs.xmin);
487 std::swap(xmax, rhs.xmax);
488 std::swap(ymin, rhs.ymin);
489 std::swap(ymax, rhs.ymax);
490 std::swap(dx_min, rhs.dx_min);
491 std::swap(dx_max, rhs.dx_max);
492 std::swap(dy_min, rhs.dy_min);
493 std::swap(dy_max, rhs.dy_max);
494 std::swap(
name, rhs.name);
495 std::swap(out_of_bounds_warning, rhs.out_of_bounds_warning);
509 std::string splinename,
510 const gsl_interp2d_type * interpoltype) {
512 if (spline !=
nullptr) {
518 const int nmin = std::max(2,
int(gsl_interp2d_type_min_size(interpoltype)));
519 if (nx < nmin || ny < nmin) {
520 std::string errormessage =
521 "[GSLSpline2D::create] spline " + name +
" needs at least " + std::to_string(nmin) +
522 " points in each direction for interpolation type " + std::string(interpoltype->name) +
523 ", got (" + std::to_string(nx) +
"," + std::to_string(ny) +
")\n";
524 throw_error(errormessage);
529 const int sign_x = x[1] > x[0] ? 1 : -1;
530 const int sign_y =
y[1] >
y[0] ? 1 : -1;
531 for (
int i = 1; i < nx; i++) {
532 if (!((x[i] - x[i - 1]) * sign_x > 0))
533 throw_error(
"[GSLSpline2D::create] x-array for spline " + name +
" is not monotone\n");
535 for (
int i = 1; i < ny; i++) {
536 if (!((
y[i] -
y[i - 1]) * sign_y > 0))
537 throw_error(
"[GSLSpline2D::create] y-array for spline " + name +
" is not monotone\n");
540 if (sign_x < 0 || sign_y < 0) {
543 zz =
DVector(
size_t(nx) *
size_t(ny));
544 for (
int ix = 0; ix < nx; ix++)
545 xx[ix] = x[sign_x < 0 ? nx - 1 - ix : ix];
546 for (
int iy = 0; iy < ny; iy++)
547 yy[iy] =
y[sign_y < 0 ? ny - 1 - iy : iy];
548 for (
int iy = 0; iy < ny; iy++) {
549 const int iy_old = sign_y < 0 ? ny - 1 - iy : iy;
550 for (
int ix = 0; ix < nx; ix++) {
551 const int ix_old = sign_x < 0 ? nx - 1 - ix : ix;
552 zz[ix + size_t(nx) * iy] = z[ix_old + size_t(nx) * iy_old];
565 dx_min = (x[1] - x[0]) / 2.0;
566 dx_max = (x[nx - 1] - x[nx - 2]) / 2.0;
567 dy_min = (
y[1] -
y[0]) / 2.0;
568 dy_max = (
y[ny - 1] -
y[ny - 2]) / 2.0;
572 interpoltype_used = interpoltype;
575 spline = gsl_spline2d_alloc(interpoltype, nx, ny);
576 gsl_spline2d_init(spline, x,
y, z, nx, ny);
586 int id = omp_get_thread_num();
588 nthreads = omp_get_num_threads();
593 xaccs = std::vector<gsl_interp_accel *>(nthreads);
594 yaccs = std::vector<gsl_interp_accel *>(nthreads);
595 for (
auto & xa : xaccs) {
598 for (
auto & ya : yaccs) {
610 std::string splinename,
611 const gsl_interp2d_type * interpoltype) {
612 if (x.size() *
y.size() != z.size()) {
613 std::string errormessage =
"[GSLSpline2D::create] z array for spline " + name +
614 " have wrong number of elements nx*ny != nz\n";
615 throw_error(errormessage);
617 create(x.data(),
y.data(), z.data(),
int(x.size()),
int(
y.size()), splinename, interpoltype);
623 std::string splinename,
624 const gsl_interp2d_type * interpoltype) {
625 int nz_x = int(z.size());
626 int nz_y = nz_x == 0 ? 0 : int(z[0].size());
627 int nxy = int(x.size() *
y.size());
628 if (nz_x * nz_y != nxy) {
629 std::string errormessage =
630 "[GSLSpline2D::create] z array for spline " + name +
" have wrong dimensions\n";
631 throw_error(errormessage);
633 for (
int i = 0; i < nz_x; i++) {
634 int n = int(z[i].size());
636 std::string errormessage =
637 "[GSLSpline2D::create] z array for spline " + name +
" have wrong dimensions\n";
638 throw_error(errormessage);
642 for (
int iy = 0; iy < nz_y; iy++) {
643 for (
int ix = 0; ix < nz_x; ix++) {
644 f[ix + nz_x * iy] = z[ix][iy];
647 create(x.data(),
y.data(),
f.data(),
int(x.size()),
int(
y.size()), splinename, interpoltype);
655 if (spline ==
nullptr) {
656 std::string errormessage =
"[GSLSpline2D::eval] Spline " + name +
" has not been created!\n";
657 throw_error(errormessage);
660 out_of_bounds_check(x,
y);
661 x = std::max(xmin, x);
662 x = std::min(xmax, x);
663 y = std::max(ymin,
y);
664 y = std::min(ymax,
y);
670 gsl_interp_accel * xacc_thread = xacc;
671 gsl_interp_accel * yacc_thread = yacc;
673 return gsl_spline2d_eval(spline, x,
y, xacc_thread, yacc_thread);
680 const int n = derivx + 3 * derivy;
682 if (spline ==
nullptr) {
683 std::string errormessage =
"[GSLSpline2D::eval_deriv] Spline " + name +
" has not been created!\n";
684 throw_error(errormessage);
686 if (derivx < 0 || derivy < 0 || derivx + derivy > 2 || n >=
int(derivfunc.size()) ||
687 derivfunc[n] ==
nullptr) {
688 std::string errormessage =
689 "[GSLSpline2D::eval_deriv] (" + std::to_string(derivx) +
"," + std::to_string(derivy) +
") " +
690 "Expected (0,0) = f, (1,0) = f_x, (0,1), f_y, (2,0) = f_xx, (0,2) = f_yy or (1,1) = f_xy!\n";
691 throw_error(errormessage);
695 out_of_bounds_check(x,
y);
696 x = std::max(xmin, x);
697 x = std::min(xmax, x);
698 y = std::max(ymin,
y);
699 y = std::min(ymax,
y);
705 gsl_interp_accel * xacc_thread = xacc;
706 gsl_interp_accel * yacc_thread = yacc;
709 return derivfunc[n](spline, x,
y, xacc_thread, yacc_thread);
716 if (spline !=
nullptr) {
721 dx_min = dx_max = 0.0;
722 dy_min = dy_max = 0.0;
725 gsl_spline2d_free(spline);
730 for (
auto & xa : xaccs) {
731 gsl_interp_accel_free(xa);
733 std::vector<gsl_interp_accel *>().swap(xaccs);
734 for (
auto & ya : yaccs) {
735 gsl_interp_accel_free(ya);
737 std::vector<gsl_interp_accel *>().swap(yaccs);
739 gsl_interp_accel_free(xacc);
740 gsl_interp_accel_free(yacc);
756 void GSLSpline2D::out_of_bounds_check(
double x,
double y)
const {
757 if (out_of_bounds_warning) {
758 if (x < xmin - dx_min || x > xmax + dx_max) {
759 std::cout <<
"Warning GSLSpline2D[" << name <<
"] ";
760 std::cout <<
"x = " << x <<
" is out of bounds (" << xmin <<
"," << xmax <<
")\n";
763 std::cout <<
"Warning GSLSpline2D[" << name <<
"] ";
764 std::cout <<
"y = " <<
y <<
" is out of bounds (" << ymin <<
"," << ymax <<
")\n";
784 throw std::invalid_argument(
"UniformSpline::create: need at least 4 points");
786 throw std::invalid_argument(
"UniformSpline::create: dx must be > 0");
789 dx2_6 = dx * dx / 6.0;
791 const size_t n = y.size();
792 const double h2 = dx * dx;
794 y2[0] = (2.0 * y[0] - 5.0 * y[1] + 4.0 * y[2] - y[3]) / h2;
795 y2[n - 1] = (2.0 * y[n - 1] - 5.0 * y[n - 2] + 4.0 * y[n - 3] - y[n - 4]) / h2;
798 DVector cp(n, 0.0), dp(n, 0.0);
799 for (
size_t i = 1; i < n - 1; i++) {
800 double rhs = 6.0 * (y[i + 1] - 2.0 * y[i] + y[i - 1]) / h2;
805 const double m = 4.0 - (i > 1 ? cp[i - 1] : 0.0);
807 dp[i] = (rhs - (i > 1 ? dp[i - 1] : 0.0)) / m;
809 for (
size_t i = n - 2; i >= 1; i--) {
810 y2[i] = dp[i] - cp[i] * (i == n - 2 ? 0.0 : y2[i + 1]);
double deriv_xy(double x, double y) const
Get the value of the x,y-derivative of the spline.
GSLSpline2D & operator=(const GSLSpline2D &rhs)
void create(const double *x, const double *y, const double *z, int nx, int ny, std::string splinename="NoName", const gsl_interp2d_type *interpoltype=SPLINE_FIDUCIAL_INTERPOL_TYPE_2D)
Create a spline from pointers to x, y and z. z must have nx * ny elements.
double eval(double x, double y) const
Get the value of the spline (if out of bounds we use the closest value)
double eval_deriv(double x, double y, int derivx, int derivy) const
General method to fetch derivatives.
void free()
Free up memory associated with the spline.
double operator()(double x, double y) const
Overload of the () operator for easy evaluation of the spline.
double deriv_x(double x, double y) const
Get the value of the x-derivative of the spline.
double deriv_yy(double x, double y) const
Get the value of the second y-derivative of the spline.
std::pair< double, double > get_xrange() const
Get the x-range the spline was created on.
double deriv_y(double x, double y) const
Get the value of the y-derivative of the spline.
void set_out_of_bounds_warning(bool v)
Turn on/off warnings if we try to evaluate out of bounds (we use closest value in that case)
std::string get_name() const
Get the name of the spline.
double deriv_xx(double x, double y) const
Get the value of the second x-derivative of the spline.
std::pair< double, double > get_yrange() const
Get the y-range the spline was created on.
This is a wrapper class for easy use of GSL splines It is (OpenMP) thread safe as long as splines are...
double eval(double x) const
Get the value of the spline (if out of bounds we use the closest value)
void create(const double *x, const double *y, int nx, std::string splinename="NoName", const gsl_interp_type *interpoltype=SPLINE_FIDUCIAL_INTERPOL_TYPE)
Create a spline from pointers to x and y. Both must have nx elements.
double eval_deriv(double x, int deriv) const
double deriv_x(double x) const
Get the value of the first derivative of the spline.
double operator()(double x) const
Overload of the () operator for easy evaluation of the spline.
GSLSpline & operator=(const GSLSpline &rhs)
double integral(double a, double b) const
Get the integral of the spline from a to b (a > b gives minus the integral from b to a; a and b are c...
std::string get_name() const
Get the name of the spline.
void free()
Free up memory associated with the spline.
void set_out_of_bounds_warning(bool v)
Turn on/off warnings if we try to evaluate out of bounds (we use closest value in that case)
double deriv_xx(double x) const
Get the value of the second derivative of the spline.
std::pair< double, double > get_xrange() const
Get the range the spline was created on.
static gsl_interp_accel * alloc_accel()
std::vector< DVector > DVector2D
static gsl_interp_accel * get_thread_accel(const std::vector< gsl_interp_accel * > &accs)
std::vector< double > DVector