OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
AbstractSpline.cpp
Go to the documentation of this file.
1//
2// Cubic Spline Interpolation to replace GSL spline
3//
4// Copyright (c) 2023, Paul Scherrer Institute, Villigen PSI, Switzerland
5// All rights reserved
6//
7// This file is part of OPAL.
8//
9// OPAL is free software: you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 3 of the License, or
12// (at your option) any later version.
13//
14// You should have received a copy of the GNU General License
15// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
16//
17
18#include "AbstractSpline.h"
19#include <algorithm>
20#include <stdexcept>
21
22void AbstractSpline::init(const std::vector<double>& x, const std::vector<double>& y) {
23 // Validate and save the parameters
24 if (x.size() != y.size()) {
25 throw std::invalid_argument("LinearSpline: x and y must be the same size");
26 }
27 if (x.size() < 2) {
28 throw std::invalid_argument("LinearSpline: need at least 2 points");
29 }
30 for (size_t i = 1; i < x.size(); ++i) {
31 if (x[i] <= x[i - 1]) {
32 throw std::invalid_argument("LinearSpline: x must be strictly increasing");
33 }
34 }
35 x_ = x;
36 y_ = y;
37}
38
39size_t AbstractSpline::findInterval(const double x, size_t& intervalCache) const {
40 if (intervalCache < x_.size() - 1 && x >= x_[intervalCache] && x < x_[intervalCache + 1]) {
41 // x is in the cached interval
42 } else {
43 // Find the new interval
44 const auto it = std::ranges::lower_bound(x_, x);
45 intervalCache = it == x_.begin() ? 0 : std::distance(x_.begin(), it) - 1;
46 }
47 return intervalCache;
48}
virtual void init(const std::vector< double > &x, const std::vector< double > &y)
Initialize from tabulated data (natural spline).
size_t findInterval(double x, size_t &intervalCache) const
Return the interval index for the given x-coordinate, using the supplied cached value if possible.
std::vector< double > x_
std::vector< double > y_