criptic v1
Cosmic Ray Interstellar Propagation Tool using Itô Calculus
Loading...
Searching...
No Matches
Interp1D.H
Go to the documentation of this file.
1
8#ifndef _INTERP1D_H_
9#define _INTERP1D_H_
10
11#include <cmath>
12#include <gsl/gsl_interp.h>
13#include "ErrCodes.H"
14#include "ThreadVec.H"
15#include "Types.H"
16
17namespace criptic {
18
30
42 class Interp1D {
43
44 public:
45
65 Interp1D(const double* x_, const double* y_, const size_t n_,
66 const extrapType extrapLo_ = extrapError,
67 const extrapType extrapHi_ = extrapError,
68 const bool xLog_ = false, const bool yLog_ = false,
69 const double yLo_ = 0.0, const double yHi_ = 0.0) :
70 x(x_),
71 y(y_),
72 n(n_),
73 extrapLo(extrapLo_),
74 extrapHi(extrapHi_),
75 xLog(xLog_),
76 yLog(yLog_),
77 yLo(yLo_),
78 yHi(yHi_)
79 {
80
81 // Allocate and initialize GSL objects
82 interp = gsl_interp_alloc(gsl_interp_steffen, n);
83 gsl_interp_init(interp, x, y, n);
84 for (IdxType i = 0; i < acc.size(); i++)
85 acc[i] = gsl_interp_accel_alloc();
86
87 // Store slopes
89 mLo = (y[1] - y[0]) / (x[1] - x[0]);
91 mHi = (y[n-1] - y[n-2]) / (x[n-1] - x[n-2]);
92 }
93
98 // Free memory
99 gsl_interp_free(interp);
100 for (IdxType i = 0; i < acc.size(); i++)
101 gsl_interp_accel_free(acc[i]);
102 }
103
108 double xMin() const { return xLog ? std::pow(10., x[0]) : x[0]; }
109
114 double xMax() const { return xLog ? std::pow(10., x[n-1]) : x[n-1]; }
115
122 double operator()(const Real& x_) const {
123
124 // Take log if requested, and catch case where taking the log
125 // and then its inverse causes a value of x that should be at
126 // the edge of the table to move slighlty off it; to handle
127 // this, if xLog is set, we check for near-equality between x1
128 // and the table limits, and coerce x1 back onto the table if it
129 // is off by less than some tolerance.
130 double x1 = xLog ? std::log10(x_) : x_;
131 if (xLog) {
132 if (x1 < x[0] && x[0] - x1 < 1.0e-10) x1 = x[0];
133 else if (!(x1 < x[n-1]) && x1 - x[n-1] < tol) x1 = x[n-1];
134 }
135
136 // Compute interpolation
137 double y1 = 0.0;
138 if (x1 < x[0]) {
139 // Off table low
140 switch (extrapLo) {
141 case extrapLinear: {
142 y1 = y[0] + mLo * (x1 - x[0]);
143 break;
144 }
145 case extrapFixedVal: {
146 return yLo;
147 }
148 case extrapConst: {
149 y1 = y[0];
150 break;
151 }
152 case extrapError: {
154 }
155 }
156 } else if (x1 > x[n-1]) {
157 // Off table high
158 switch (extrapHi) {
159 case extrapLinear: {
160 y1 = y[n-1] + mHi * (x1 - x[n-1]);
161 break;
162 }
163 case extrapFixedVal: {
164 return yHi;
165 }
166 case extrapConst: {
167 y1 = y[n-1];
168 break;
169 }
170 case extrapError: {
172 }
173 }
174 } else {
175 // Within table
176 y1 = gsl_interp_eval(interp, x, y, x1, acc());
177 }
178 if (yLog) {
179 return std::pow(10., y1);
180 } else {
181 return y1;
182 }
183 }
184
185 private:
186
187 const double *x;
188 const double *y;
189 const size_t n;
192 const bool xLog;
193 const bool yLog;
194 const double yLo;
195 const double yHi;
196 double mLo;
197 double mHi;
198 static constexpr double tol = 1.0e-8;
201 // GSL machinery
202 gsl_interp *interp;
205 };
206
207}
208
209#endif
210// _INTERP1D_H_
A file containing a set of enumerated error codes.
A class to hold objects where one copy is required per thread.
Basic integer and real types.
A class to handle interpolation on 1D tables.
Definition Interp1D.H:42
Interp1D(const double *x_, const double *y_, const size_t n_, const extrapType extrapLo_=extrapError, const extrapType extrapHi_=extrapError, const bool xLog_=false, const bool yLog_=false, const double yLo_=0.0, const double yHi_=0.0)
Constructor.
Definition Interp1D.H:65
double operator()(const Real &x_) const
Caclulate an interpolated value from the table.
Definition Interp1D.H:122
const double * x
Definition Interp1D.H:187
double xMin() const
Return lower limit of table.
Definition Interp1D.H:108
const double yLo
Definition Interp1D.H:194
const double * y
Definition Interp1D.H:188
~Interp1D()
Destructor.
Definition Interp1D.H:97
const double yHi
Definition Interp1D.H:195
gsl_interp * interp
Definition Interp1D.H:202
const bool xLog
Definition Interp1D.H:192
const bool yLog
Definition Interp1D.H:193
double mLo
Definition Interp1D.H:196
const size_t n
Definition Interp1D.H:189
static constexpr double tol
Definition Interp1D.H:198
ThreadVec< gsl_interp_accel * > acc
Definition Interp1D.H:203
double xMax() const
Return upper limit of table.
Definition Interp1D.H:114
const extrapType extrapLo
Definition Interp1D.H:190
double mHi
Definition Interp1D.H:197
const extrapType extrapHi
Definition Interp1D.H:191
A class to automate private thread copies of objects.
Definition ThreadVec.H:35
IdxType size() const
Return the number of distinct objects stored.
Definition ThreadVec.H:59
The primary namespace for criptic objects.
Definition AdvancePacket.H:25
extrapType
Enum describing different ways to handle extrapolation.
Definition Interp1D.H:22
@ extrapConst
Definition Interp1D.H:25
@ extrapLinear
Definition Interp1D.H:24
@ extrapError
Definition Interp1D.H:23
@ extrapFixedVal
Definition Interp1D.H:27
@ errBadXLoExtrapolation
Definition ErrCodes.H:28
@ errBadXHiExtrapolation
Definition ErrCodes.H:30
std::vector< Real >::size_type IdxType
Definition Types.H:45
double Real
Definition Types.H:38