FML
Loading...
Searching...
No Matches
Spline.cpp
Go to the documentation of this file.
1#include "Spline.h"
2
3#include <cstdlib>
4
5namespace FML {
6 namespace INTERPOLATION {
7 namespace SPLINE {
8
9 // An accelerator on its own cache line: the accelerators of the threads are written at every
10 // evaluation, and next to each other in memory (as gsl_interp_accel_alloc gives them) the threads
11 // slow each other down (false sharing; 2D evaluation ~20% slower with 2 threads than without
12 // accelerators). Freed with gsl_interp_accel_free (free)
13 static gsl_interp_accel * alloc_accel() {
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);
17 if (mem == nullptr)
18 throw std::bad_alloc();
19 auto * acc = static_cast<gsl_interp_accel *>(mem);
20 gsl_interp_accel_reset(acc);
21 return acc;
22 }
23
24#ifdef USE_OMP
25 const int nmax_threads = omp_get_max_threads();
26
27 // Get the accelerator for the current thread. The accelerators are only a
28 // lookup cache so if we cannot safely get a unique one (more threads than
29 // when the spline was created, or nested parallel regions where thread ids
30 // are not unique) we return nullptr which GSL handles with a binary search
31 //
32 // Inside an inactive (one-thread) region nested in an active one, e.g. a library
33 // '#pragma omp parallel for' called from a user's parallel loop, omp_get_thread_num() is 0
34 // for all the threads of the outer team, so we use the thread number in the
35 // (one) active region instead
36 static inline gsl_interp_accel * get_thread_accel(const std::vector<gsl_interp_accel *> & accs) {
37 const int level = omp_get_level();
38 if (level <= 1) {
39 const int id = omp_get_thread_num();
40 return id < int(accs.size()) ? accs[id] : nullptr;
41 }
42 if (omp_get_active_level() > 1)
43 return nullptr;
44 int id = 0;
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;
49 }
50#endif
51
52 // How to handle an error
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);
57 abort();
58#else
59 throw std::runtime_error(errormessage);
60#endif
61 }
62
63 // How to handle an error
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);
68 abort();
69#else
70 throw std::runtime_error(errormessage);
71#endif
72 }
73
74 //====================================================
75 // Constructors
76 //====================================================
77
78 GSLSpline::GSLSpline(std::string name) : GSLSpline() { this->name = name; }
79
81 double * y,
82 int nx,
83 std::string splinename,
84 const gsl_interp_type * interpoltype) {
85 create(x, y, nx, splinename, interpoltype);
86 }
87
89 const DVector & y,
90 std::string splinename,
91 const gsl_interp_type * interpoltype) {
92 // Check that dimensions match
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);
97 }
98
99 //====================================================
100 // Assignment constructor
101 //====================================================
103 if (this != &rhs) {
104 // We just create the spline from scratch instead of copying all the data
105 if (rhs.spline != nullptr) {
106 create(rhs.spline->x, rhs.spline->y, rhs.size_x, rhs.name, rhs.interpoltype_used);
107 } else {
108 free();
109 name = rhs.name;
110 }
111 out_of_bounds_warning = rhs.out_of_bounds_warning;
112 }
113 return *this;
114 }
115
116 //====================================================
117 // Copy constructor
118 //====================================================
120 // We just create the spline from scratch instead of copying all the data
121 out_of_bounds_warning = rhs.out_of_bounds_warning;
122 name = rhs.name;
123 if (rhs.spline != nullptr) {
124 create(rhs.spline->x, rhs.spline->y, rhs.size_x, rhs.name, rhs.interpoltype_used);
125 }
126 }
127
128 GSLSpline::operator bool() const { return (spline != nullptr); }
129
130 //====================================================
131 // Move constructor and assignment: take over the data
132 //====================================================
133 GSLSpline::GSLSpline(GSLSpline && rhs) noexcept { *this = std::move(rhs); }
134
136 if (this != &rhs) {
137 std::swap(spline, rhs.spline);
138#ifdef USE_OMP
139 std::swap(xaccs, rhs.xaccs);
140#else
141 std::swap(xacc, rhs.xacc);
142#endif
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);
151 rhs.free();
152 }
153 return *this;
154 }
155
156 //====================================================
157 // Create a GSL spline
158 //====================================================
159 void GSLSpline::create(const double * x,
160 const double * y,
161 int nx,
162 std::string splinename,
163 const gsl_interp_type * interpoltype) {
164 // Clean up if we already have a spline allocated
165 if (spline != nullptr) {
166 free();
167 }
168 name = splinename;
169
170 // Check that we have enough points (GSL would otherwise abort the program)
171 const int nmin = std::max(2, int(gsl_interp_type_min_size(interpoltype)));
172 if (nx < nmin) {
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);
177 }
178
179 // Check if array is increasing
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);
186 }
187 }
188
189 // If array is reversed then reverse it since GSL
190 // requires a monotonely increasing array
191 DVector xx, yy;
192 if (sign_one < 0) {
193 xx = DVector(nx);
194 yy = DVector(nx);
195 for (int i = 0; i < nx; i++) {
196 xx[i] = x[nx - 1 - i];
197 yy[i] = y[nx - 1 - i];
198 }
199 x = xx.data();
200 y = yy.data();
201 }
202
203 // Set class variables
204 xmin = x[0];
205 xmax = x[nx - 1];
206 dx_min = (x[1] - x[0]) / 2.0;
207 dx_max = (x[nx - 1] - x[nx - 2]) / 2.0;
208 size_x = nx;
209 name = splinename;
210 interpoltype_used = interpoltype;
211
212 // Make the spline
213 spline = gsl_spline_alloc(interpoltype, nx);
214 gsl_spline_init(spline, x, y, nx);
215
216 // Make accelerators (one per thread if OpenMP)
217 // If nthreads = 1 we are likely trying to create a spline
218 // inside a OMP region. In that case allocate as many as we have
219 // threads available on the system
220#ifdef USE_OMP
221 int nthreads = 1;
222#pragma omp parallel
223 {
224 int id = omp_get_thread_num();
225 if (id == 0)
226 nthreads = omp_get_num_threads();
227 }
228 if (nthreads == 1)
229 nthreads = nmax_threads;
230
231 // If we make a spline inside a nested openmp then
232 // the stuff above does not work
233 // nthreads = std::max(16, nthreads);
234 xaccs = std::vector<gsl_interp_accel *>(nthreads);
235 for (auto & xa : xaccs) {
236 xa = alloc_accel();
237 }
238#else
239 xacc = alloc_accel();
240#endif
241 }
242
244 const DVector & y,
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 +
250 "\n";
251 throw_error(errormessage);
252 }
253 create(x.data(), y.data(), int(x.size()), splinename, interpoltype);
254 }
255
256 //====================================================
257 // Evaluate the function
258 // NB: using closest points if out-of-bounds!
259 //====================================================
260
261 double GSLSpline::operator()(double x) const { return eval(x); }
262
263 double GSLSpline::eval(double x) const {
264 if (spline == nullptr) {
265 std::string errormessage = "[GSLSpline::eval] Spline " + name + " has not been created!\n";
266 throw_error(errormessage);
267 }
268
269 // If out of bounds show a warning and set x to boundary value
270 out_of_bounds_check(x);
271 x = std::max(x, xmin);
272 x = std::min(x, xmax);
273
274 // Return f, f' or f'' depending on value of deriv
275#ifdef USE_OMP
276 gsl_interp_accel * xacc_thread = get_thread_accel(xaccs);
277#else
278 gsl_interp_accel * xacc_thread = xacc;
279#endif
280 return gsl_spline_eval(spline, x, xacc_thread);
281 }
282
283 double GSLSpline::eval_deriv(double x, int deriv) const {
284 if (spline == nullptr) {
285 std::string errormessage = "[GSLSpline::eval_deriv] Spline " + name + " has not been created!\n";
286 throw_error(errormessage);
287 }
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);
292 }
293
294 // If out of bounds show a warning and set x to boundary value
295 out_of_bounds_check(x);
296 x = std::max(x, xmin);
297 x = std::min(x, xmax);
298
299 // Return f, f' or f'' depending on value of deriv
300#ifdef USE_OMP
301 gsl_interp_accel * xacc_thread = get_thread_accel(xaccs);
302#else
303 gsl_interp_accel * xacc_thread = xacc;
304#endif
305
306 double dydx = 0.0;
307 if (deriv == 0) {
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);
313 }
314 return dydx;
315 }
316
317 //====================================================
318 // Free up memory
319 //====================================================
321 if (spline != nullptr) {
322 // Reset class variables
323 xmin = xmax = 0.0;
324 size_x = 0;
325 dx_min = dx_max = 0.0;
326
327 // Free the spline
328 gsl_spline_free(spline);
329 spline = nullptr;
330
331 // Free accelerators
332#ifdef USE_OMP
333 for (auto & xa : xaccs) {
334 gsl_interp_accel_free(xa);
335 }
336 std::vector<gsl_interp_accel *>().swap(xaccs);
337#else
338 gsl_interp_accel_free(xacc);
339 xacc = nullptr;
340#endif
341 }
342 }
343
344 //====================================================
345 // Rest of the class methods
346 //====================================================
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";
352 }
353 }
354 }
355 double GSLSpline::deriv_x(double x) const { return eval_deriv(x, 1); }
356
357 double GSLSpline::integral(double a, double b) const {
358 if (spline == nullptr) {
359 std::string errormessage = "[GSLSpline::integral] Spline " + name + " has not been created!\n";
360 throw_error(errormessage);
361 }
362 if (a > b)
363 return -integral(b, a);
364 a = std::min(std::max(a, xmin), xmax);
365 b = std::min(std::max(b, xmin), xmax);
366#ifdef USE_OMP
367 gsl_interp_accel * xacc_thread = get_thread_accel(xaccs);
368#else
369 gsl_interp_accel * xacc_thread = xacc;
370#endif
371 return gsl_spline_eval_integ(spline, a, b, xacc_thread);
372 }
373 double GSLSpline::deriv_xx(double x) const { return eval_deriv(x, 2); }
374 std::pair<double, double> GSLSpline::get_xrange() const { return {xmin, xmax}; }
375 std::string GSLSpline::get_name() const { return name; }
376 void GSLSpline::set_out_of_bounds_warning(bool v) { out_of_bounds_warning = v; }
377
378 //====================================================
379 // Destructor
380 //====================================================
382
383 //====================================================
384 // Constructors
385 //====================================================
386
387 GSLSpline2D::GSLSpline2D(std::string name) : GSLSpline2D() { this->name = name; }
388
389 GSLSpline2D::GSLSpline2D(const double * x,
390 const double * y,
391 const double * z,
392 int nx,
393 int ny,
394 std::string splinename,
395 const gsl_interp2d_type * interpoltype) {
396 create(x, y, z, nx, ny, splinename, interpoltype);
397 }
398
400 const DVector & y,
401 const DVector & z,
402 std::string splinename,
403 const gsl_interp2d_type * interpoltype) {
404 create(x, y, z, splinename, interpoltype);
405 }
406
408 const DVector & y,
409 const DVector2D & z,
410 std::string splinename,
411 const gsl_interp2d_type * interpoltype) {
412
413 // Check that dimensions match
414 bool ok = (x.size() == z.size());
415 ok = ok and (z.size() > 0 ? z[0].size() == y.size() : true);
416 if (not ok)
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);
420 }
421
422 //====================================================
423 // Assignment constructor
424 //====================================================
426 if (this != &rhs) {
427 // We just create the spline from scratch instead of copying all the data
428 if (rhs.spline != nullptr) {
429 create(rhs.spline->xarr,
430 rhs.spline->yarr,
431 rhs.spline->zarr,
432 rhs.size_x,
433 rhs.size_y,
434 rhs.name,
435 rhs.interpoltype_used);
436 } else {
437 free();
438 name = rhs.name;
439 }
440 out_of_bounds_warning = rhs.out_of_bounds_warning;
441 }
442 return *this;
443 }
444
445 //====================================================
446 // Copy constructor
447 //====================================================
449 // We just create the spline from scratch instead of copying all the data
450 out_of_bounds_warning = rhs.out_of_bounds_warning;
451 name = rhs.name;
452 if (rhs.spline != nullptr) {
453 create(rhs.spline->xarr,
454 rhs.spline->yarr,
455 rhs.spline->zarr,
456 rhs.size_x,
457 rhs.size_y,
458 rhs.name,
459 rhs.interpoltype_used);
460 }
461 }
462
463 // For more easy evaluation
464 double GSLSpline2D::operator()(double x, double y) const { return eval(x, y); }
465
466 GSLSpline2D::operator bool() const { return (spline != nullptr); }
467
468 //====================================================
469 // Move constructor and assignment: take over the data
470 //====================================================
471 GSLSpline2D::GSLSpline2D(GSLSpline2D && rhs) noexcept { *this = std::move(rhs); }
472
474 if (this != &rhs) {
475 std::swap(spline, rhs.spline);
476#ifdef USE_OMP
477 std::swap(xaccs, rhs.xaccs);
478 std::swap(yaccs, rhs.yaccs);
479#else
480 std::swap(xacc, rhs.xacc);
481 std::swap(yacc, rhs.yacc);
482#endif
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);
496 rhs.free();
497 }
498 return *this;
499 }
500
501 //====================================================
502 // Create a GSL 2D spline
503 //====================================================
504 void GSLSpline2D::create(const double * x,
505 const double * y,
506 const double * z,
507 int nx,
508 int ny,
509 std::string splinename,
510 const gsl_interp2d_type * interpoltype) {
511 // Clean up if we already have a spline
512 if (spline != nullptr) {
513 free();
514 }
515 name = splinename;
516
517 // Check that we have enough points (GSL would otherwise abort the program)
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);
525 }
526
527 // Check that the arrays are monotone. GSL requires increasing arrays
528 // so if an array is decreasing we reverse it (and z accordingly) like for 1D splines
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");
534 }
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");
538 }
539 DVector xx, yy, zz;
540 if (sign_x < 0 || sign_y < 0) {
541 xx = DVector(nx);
542 yy = DVector(ny);
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];
553 }
554 }
555 x = xx.data();
556 y = yy.data();
557 z = zz.data();
558 }
559
560 // Set class variables
561 xmin = x[0];
562 xmax = x[nx - 1];
563 ymin = y[0];
564 ymax = y[ny - 1];
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;
569 size_x = nx;
570 size_y = ny;
571 name = splinename;
572 interpoltype_used = interpoltype;
573
574 // Create spline
575 spline = gsl_spline2d_alloc(interpoltype, nx, ny);
576 gsl_spline2d_init(spline, x, y, z, nx, ny);
577
578 // Make accelerators (one per thread if OpenMP)
579 // If nthreads = 1 we are likely trying to create a spline
580 // inside a OMP region. In that case allocate as many as we have
581 // threads available on the system
582#ifdef USE_OMP
583 int nthreads = 1;
584#pragma omp parallel
585 {
586 int id = omp_get_thread_num();
587 if (id == 0)
588 nthreads = omp_get_num_threads();
589 }
590 if (nthreads == 1)
591 nthreads = nmax_threads;
592
593 xaccs = std::vector<gsl_interp_accel *>(nthreads);
594 yaccs = std::vector<gsl_interp_accel *>(nthreads);
595 for (auto & xa : xaccs) {
596 xa = alloc_accel();
597 }
598 for (auto & ya : yaccs) {
599 ya = alloc_accel();
600 }
601#else
602 xacc = alloc_accel();
603 yacc = alloc_accel();
604#endif
605 }
606
608 const DVector & y,
609 const DVector & z,
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);
616 }
617 create(x.data(), y.data(), z.data(), int(x.size()), int(y.size()), splinename, interpoltype);
618 }
619
621 const DVector & y,
622 const DVector2D & z,
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);
632 }
633 for (int i = 0; i < nz_x; i++) {
634 int n = int(z[i].size());
635 if (n != nz_y) {
636 std::string errormessage =
637 "[GSLSpline2D::create] z array for spline " + name + " have wrong dimensions\n";
638 throw_error(errormessage);
639 }
640 }
641 DVector f(nz_x * nz_y);
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];
645 }
646 }
647 create(x.data(), y.data(), f.data(), int(x.size()), int(y.size()), splinename, interpoltype);
648 }
649
650 //====================================================
651 // Lookup a value from a GSL 2D spline
652 // Use closest points for out-of-bounds
653 //====================================================
654 double GSLSpline2D::eval(double x, double y) const {
655 if (spline == nullptr) {
656 std::string errormessage = "[GSLSpline2D::eval] Spline " + name + " has not been created!\n";
657 throw_error(errormessage);
658 }
659 // If out of bounds show a warning and set x,y to boundary value
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);
665
666#ifdef USE_OMP
667 gsl_interp_accel * xacc_thread = get_thread_accel(xaccs);
668 gsl_interp_accel * yacc_thread = get_thread_accel(yaccs);
669#else
670 gsl_interp_accel * xacc_thread = xacc;
671 gsl_interp_accel * yacc_thread = yacc;
672#endif
673 return gsl_spline2d_eval(spline, x, y, xacc_thread, yacc_thread);
674 }
675
676 double GSLSpline2D::eval_deriv(double x, double y, int derivx, int derivy) const {
677 // Map (dx,dy) => n = derivx + 3*derivy
678 // which gives 0 = f, 1 = f_x, 2 = f_xx, 3 = f_y, 4 = f_xy and (f_xyy), 6 =
679 // f_yy
680 const int n = derivx + 3 * derivy;
681
682 if (spline == nullptr) {
683 std::string errormessage = "[GSLSpline2D::eval_deriv] Spline " + name + " has not been created!\n";
684 throw_error(errormessage);
685 }
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);
692 }
693
694 // If out of bounds show a warning and set x,y to boundary value
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);
700
701#ifdef USE_OMP
702 gsl_interp_accel * xacc_thread = get_thread_accel(xaccs);
703 gsl_interp_accel * yacc_thread = get_thread_accel(yaccs);
704#else
705 gsl_interp_accel * xacc_thread = xacc;
706 gsl_interp_accel * yacc_thread = yacc;
707#endif
708
709 return derivfunc[n](spline, x, y, xacc_thread, yacc_thread);
710 }
711
712 //====================================================
713 // Free up memory
714 //====================================================
716 if (spline != nullptr) {
717 // Reset class variables
718 xmin = xmax = 0.0;
719 ymin = ymax = 0.0;
720 size_x = size_y = 0;
721 dx_min = dx_max = 0.0;
722 dy_min = dy_max = 0.0;
723
724 // Free the spline data
725 gsl_spline2d_free(spline);
726 spline = nullptr;
727
728 // Free accelerators
729#ifdef USE_OMP
730 for (auto & xa : xaccs) {
731 gsl_interp_accel_free(xa);
732 }
733 std::vector<gsl_interp_accel *>().swap(xaccs);
734 for (auto & ya : yaccs) {
735 gsl_interp_accel_free(ya);
736 }
737 std::vector<gsl_interp_accel *>().swap(yaccs);
738#else
739 gsl_interp_accel_free(xacc);
740 gsl_interp_accel_free(yacc);
741 xacc = nullptr;
742 yacc = nullptr;
743#endif
744 }
745 }
746
747 //====================================================
748 // Destructor
749 //====================================================
751
752 //====================================================
753 // Rest of the class methods
754 //====================================================
755
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";
761 }
762 if (y < ymin - dy_min || y > ymax + dy_max) {
763 std::cout << "Warning GSLSpline2D[" << name << "] ";
764 std::cout << "y = " << y << " is out of bounds (" << ymin << "," << ymax << ")\n";
765 }
766 }
767 }
768 double GSLSpline2D::deriv_x(double x, double y) const { return eval_deriv(x, y, 1, 0); }
769 double GSLSpline2D::deriv_xx(double x, double y) const { return eval_deriv(x, y, 2, 0); }
770 double GSLSpline2D::deriv_y(double x, double y) const { return eval_deriv(x, y, 0, 1); }
771 double GSLSpline2D::deriv_yy(double x, double y) const { return eval_deriv(x, y, 0, 2); }
772 double GSLSpline2D::deriv_xy(double x, double y) const { return eval_deriv(x, y, 1, 1); }
773 std::pair<double, double> GSLSpline2D::get_xrange() const { return {xmin, xmax}; }
774 std::pair<double, double> GSLSpline2D::get_yrange() const { return {ymin, ymax}; }
775 std::string GSLSpline2D::get_name() const { return name; }
776 void GSLSpline2D::set_out_of_bounds_warning(bool v) { out_of_bounds_warning = v; }
777 //====================================================
778 // UniformSpline: second derivatives from the tridiagonal system
779 // y2[i-1] + 4 y2[i] + y2[i+1] = 6 (y[i+1] - 2 y[i] + y[i-1]) / dx^2
780 // with the end-point y2 from one-sided 4-point finite differences
781 //====================================================
782 void UniformSpline::create(double x0_in, double dx, const DVector & y_in) {
783 if (y_in.size() < 4)
784 throw std::invalid_argument("UniformSpline::create: need at least 4 points");
785 if (!(dx > 0.0))
786 throw std::invalid_argument("UniformSpline::create: dx must be > 0");
787 x0 = x0_in;
788 inv_dx = 1.0 / dx;
789 dx2_6 = dx * dx / 6.0;
790 y = y_in;
791 const size_t n = y.size();
792 const double h2 = dx * dx;
793 y2.assign(n, 0.0);
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;
796
797 // Thomas algorithm for the interior points (sub/super-diagonal 1, diagonal 4)
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;
801 if (i == 1)
802 rhs -= y2[0];
803 if (i == n - 2)
804 rhs -= y2[n - 1];
805 const double m = 4.0 - (i > 1 ? cp[i - 1] : 0.0);
806 cp[i] = 1.0 / m;
807 dp[i] = (rhs - (i > 1 ? dp[i - 1] : 0.0)) / m;
808 }
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]);
811 if (i == 1)
812 break;
813 }
814 }
815
816 } // namespace SPLINE
817 } // namespace INTERPOLATION
818} // namespace FML
Spline2D spline
Definition BBN.cpp:17
double f
Definition Minimizer.cpp:28
std::string name
double deriv_xy(double x, double y) const
Get the value of the x,y-derivative of the spline.
Definition Spline.cpp:772
GSLSpline2D & operator=(const GSLSpline2D &rhs)
Definition Spline.cpp:425
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.
Definition Spline.cpp:504
double eval(double x, double y) const
Get the value of the spline (if out of bounds we use the closest value)
Definition Spline.cpp:654
double eval_deriv(double x, double y, int derivx, int derivy) const
General method to fetch derivatives.
Definition Spline.cpp:676
void free()
Free up memory associated with the spline.
Definition Spline.cpp:715
double operator()(double x, double y) const
Overload of the () operator for easy evaluation of the spline.
Definition Spline.cpp:464
double deriv_x(double x, double y) const
Get the value of the x-derivative of the spline.
Definition Spline.cpp:768
double deriv_yy(double x, double y) const
Get the value of the second y-derivative of the spline.
Definition Spline.cpp:771
std::pair< double, double > get_xrange() const
Get the x-range the spline was created on.
Definition Spline.cpp:773
double deriv_y(double x, double y) const
Get the value of the y-derivative of the spline.
Definition Spline.cpp:770
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)
Definition Spline.cpp:776
std::string get_name() const
Get the name of the spline.
Definition Spline.cpp:775
double deriv_xx(double x, double y) const
Get the value of the second x-derivative of the spline.
Definition Spline.cpp:769
std::pair< double, double > get_yrange() const
Get the y-range the spline was created on.
Definition Spline.cpp:774
This is a wrapper class for easy use of GSL splines It is (OpenMP) thread safe as long as splines are...
Definition Spline.h:106
double eval(double x) const
Get the value of the spline (if out of bounds we use the closest value)
Definition Spline.cpp:263
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.
Definition Spline.cpp:159
double eval_deriv(double x, int deriv) const
Definition Spline.cpp:283
double deriv_x(double x) const
Get the value of the first derivative of the spline.
Definition Spline.cpp:355
double operator()(double x) const
Overload of the () operator for easy evaluation of the spline.
Definition Spline.cpp:261
GSLSpline & operator=(const GSLSpline &rhs)
Definition Spline.cpp:102
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...
Definition Spline.cpp:357
std::string get_name() const
Get the name of the spline.
Definition Spline.cpp:375
void free()
Free up memory associated with the spline.
Definition Spline.cpp:320
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)
Definition Spline.cpp:376
double deriv_xx(double x) const
Get the value of the second derivative of the spline.
Definition Spline.cpp:373
std::pair< double, double > get_xrange() const
Get the range the spline was created on.
Definition Spline.cpp:374
void create(double x0, double dx, const DVector &y)
Definition Spline.cpp:782
static gsl_interp_accel * alloc_accel()
Definition Spline.cpp:13
std::vector< DVector > DVector2D
Definition Spline.h:39
static gsl_interp_accel * get_thread_accel(const std::vector< gsl_interp_accel * > &accs)
Definition Spline.cpp:36
std::vector< double > DVector
Definition Spline.h:38
return y
Definition Global.h:365