Version: SMASH-3.1
interpolation2D.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020,2022
3  * SMASH Team
4  *
5  * GNU General Public License (GPLv3 or later)
6  */
7 
9 
10 #include <initializer_list>
11 #include <iostream>
12 #include <stdexcept>
13 
14 namespace smash {
15 
17  const std::vector<double>& y,
18  const std::vector<double>& z) {
19  const size_t M = x.size();
20  const size_t N = y.size();
21 
22  if (z.size() != N * M) {
23  throw std::runtime_error(
24  "Dimensions not suitable for 2D interpolation. DIM(z) != DIM(x) * "
25  "DIM(y).");
26  }
27 
28  if (M < 4 || N < 4) {
29  throw std::runtime_error(
30  "Need at least 4 data points in each dimension for bicubic spline "
31  "interpolation.");
32  }
33 
34  // Assign lower and upper bounds for constant extrapolation
35  first_x_ = x.front();
36  last_x_ = x.back();
37  first_y_ = y.front();
38  last_y_ = y.back();
39 
40  // cast vectors into arrays, as GSL functions can only handle arrays
41  const double* xa = &x[0];
42  const double* ya = &y[0];
43  const double* za = &z[0];
44 
45  // Create accelerator objects (interpolation lookups)
46  xacc_ = gsl_interp_accel_alloc();
47  yacc_ = gsl_interp_accel_alloc();
48 
49  // Initialize bicubic spline interpolation
50  spline_ = gsl_spline2d_alloc(gsl_interp2d_bicubic, M, N);
51  gsl_spline2d_init(spline_, xa, ya, za, M, N);
52 }
53 
55  gsl_spline2d_free(spline_);
56  gsl_interp_accel_free(xacc_);
57  gsl_interp_accel_free(yacc_);
58 }
59 
60 double InterpolateData2DSpline::operator()(double xi, double yi) const {
61  // constant extrapolation at the edges
62  xi = (xi < first_x_) ? first_x_ : xi;
63  xi = (xi > last_x_) ? last_x_ : xi;
64  yi = (yi < first_y_) ? first_y_ : yi;
65  yi = (yi > last_y_) ? last_y_ : yi;
66 
67  // bicubic spline interpolation
68  return gsl_spline2d_eval(spline_, xi, yi, xacc_, yacc_);
69 }
70 
71 } // namespace smash
double first_y_
First y value.
InterpolateData2DSpline(const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &z)
Interpolate function f given discrete samples f(x_i, y_i) = z_i.
double operator()(double xi, double yi) const
Calculate bicubic interpolation for given x and y.
gsl_interp_accel * xacc_
GSL iterator for interpolation lookups in x direction.
gsl_spline2d * spline_
GSL spline in 2D.
double first_x_
First x value.
gsl_interp_accel * yacc_
GSL iterator for interpolation lookupin y direction.
Definition: action.h:24