Version: SMASH-2.0
interpolation2D.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 -
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
smash::InterpolateData2DSpline::first_x_
double first_x_
First x value.
Definition: interpolation2D.h:52
smash
Definition: action.h:24
smash::InterpolateData2DSpline::last_y_
double last_y_
Last y value.
Definition: interpolation2D.h:58
smash::InterpolateData2DSpline::InterpolateData2DSpline
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.
Definition: interpolation2D.cc:16
smash::InterpolateData2DSpline::last_x_
double last_x_
Last x value.
Definition: interpolation2D.h:54
smash::InterpolateData2DSpline::first_y_
double first_y_
First y value.
Definition: interpolation2D.h:56
smash::InterpolateData2DSpline::yacc_
gsl_interp_accel * yacc_
GSL iterator for interpolation lookupin y direction.
Definition: interpolation2D.h:63
interpolation2D.h
smash::InterpolateData2DSpline::~InterpolateData2DSpline
~InterpolateData2DSpline()
Destructor.
Definition: interpolation2D.cc:54
smash::InterpolateData2DSpline::spline_
gsl_spline2d * spline_
GSL spline in 2D.
Definition: interpolation2D.h:65
smash::InterpolateData2DSpline::operator()
double operator()(double xi, double yi) const
Calculate bicubic interpolation for given x and y.
Definition: interpolation2D.cc:60
smash::InterpolateData2DSpline::xacc_
gsl_interp_accel * xacc_
GSL iterator for interpolation lookups in x direction.
Definition: interpolation2D.h:61