MARLEY (Model of Argon Reaction Low Energy Yields) v2.0.0
A Monte Carlo event generator for tens-of-MeV neutrino interactions
Loading...
Searching...
No Matches
marley::InterpolationGrid< FirstNumericType, SecondNumericType > Class Template Reference

One-dimensional function y(x) defined using a grid of ordered pairs (x,y) and an interpolation rule. More...

#include <InterpolationGrid.hh>

Public Types

enum class  ExtrapolationMethod { Zero , Endpoint , Continue , Throw }
 Method to use for computing y(x) when the x value lies beyond the grid boundaries. More...
 
using Grid = std::vector<OrderedPair>
 
using GridConstIterator = typename std::vector<OrderedPair>::const_iterator
 
enum class  InterpolationMethod {
  Constant = 1 , LinearLinear = 2 , LinearLog = 3 , LogLinear = 4 ,
  LogLog = 5
}
 Method to use for interpolating between (x,y) grid points. More...
 
using OrderedPair = std::pair<FirstNumericType, SecondNumericType>
 

Public Member Functions

 InterpolationGrid (const Grid &grid, InterpolationMethod interp_method=InterpolationMethod::LinearLinear, ExtrapolationMethod extrap_method=ExtrapolationMethod::Zero)
 Create an InterpolationGrid from a vector of ordered pairs.
 
 InterpolationGrid (const std::vector< FirstNumericType > &xs, const std::vector< SecondNumericType > &ys, InterpolationMethod interp_method=InterpolationMethod::LinearLinear, ExtrapolationMethod extrap_method=ExtrapolationMethod::Zero)
 Create an InterpolationGrid from vectors of x and y values.
 
 InterpolationGrid (InterpolationMethod interp_method=InterpolationMethod::LinearLinear, ExtrapolationMethod extrap_method=ExtrapolationMethod::Zero)
 Create an InterpolationGrid without any grid points.
 
OrderedPair & at (size_t j)
 Get a reference to the jth ordered pair from the grid.
 
const OrderedPair & back () const
 Returns a reference to the last ordered pair.
 
void clear ()
 Delete all ordered pairs from the grid.
 
ExtrapolationMethod extrapolation_method () const
 Get the ExtrapolationMethod used by this InterpolationGrid.
 
const OrderedPair & front () const
 Returns a reference to the first ordered pair.
 
std::function< SecondNumericType(FirstNumericType) > get_function ()
 Get a std::function object that represents y(x) for this InterpolationGrid.
 
void insert (FirstNumericType x, SecondNumericType y)
 Add a new ordered pair (x, y) to the grid.
 
SecondNumericType interpolate (FirstNumericType x) const
 Compute y(x) using the current InterpolationMethod.
 
InterpolationMethod interpolation_method () const
 Get the InterpolationMethod used by this InterpolationGrid.
 
GridConstIterator lower_bound (const GridConstIterator &begin, const GridConstIterator &end, FirstNumericType x) const
 Returns a const_iterator to the first element of the grid for which the x value is not less than (i.e. greater than or equal to) x.
 
void set_extrapolation_method (ExtrapolationMethod method)
 Set the ExtrapolationMethod to use.
 
void set_interpolation_method (InterpolationMethod method)
 Set the InterpolationMethod to use.
 
size_t size () const
 Get the number of ordered pairs on the grid.
 
GridConstIterator upper_bound (const GridConstIterator &begin, const GridConstIterator &end, FirstNumericType x) const
 Returns a const_iterator to the first element of the grid for which the x value is greater than x.
 

Detailed Description

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
class marley::InterpolationGrid< FirstNumericType, SecondNumericType >

One-dimensional function y(x) defined using a grid of ordered pairs (x,y) and an interpolation rule.

This class implements the one-dimensional interpolation rules described in the ENDF-6 formats manual for a grid of (x,y) pairs.

Template Parameters
FirstNumericTypetype for the x values
SecondNumericTypetype for the y values

Definition at line 37 of file InterpolationGrid.hh.

Member Typedef Documentation

◆ Grid

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
using marley::InterpolationGrid< FirstNumericType, SecondNumericType >::Grid = std::vector<OrderedPair>

Definition at line 42 of file InterpolationGrid.hh.

◆ GridConstIterator

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
using marley::InterpolationGrid< FirstNumericType, SecondNumericType >::GridConstIterator = typename std::vector<OrderedPair>::const_iterator

Definition at line 43 of file InterpolationGrid.hh.

◆ OrderedPair

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
using marley::InterpolationGrid< FirstNumericType, SecondNumericType >::OrderedPair = std::pair<FirstNumericType, SecondNumericType>

Definition at line 41 of file InterpolationGrid.hh.

Member Enumeration Documentation

◆ ExtrapolationMethod

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
enum class marley::InterpolationGrid::ExtrapolationMethod
strong

Method to use for computing y(x) when the x value lies beyond the grid boundaries.

The table below describes how y(x) is calculated using each of the allowed ExtrapolationMethod settings. In the notation used below, the lowest x value is called \(x_\text{left}\), the greatest x value is called \(x_\text{right}\), and their corresponding y values are called \(y_\text{left}\) and \(y_\text{right}\), respectively. The ExtrapolationMethod is only relevant when \(x < x_\text{left}\) or \(x > x_\text{right}\).

Extrapolation Methods
MethodDescription
Zero\(y = 0\)
Endpoint\(y = \begin{cases} y_\text{left} & x < x_\text{left} \\ y_\text{right} & x > x_\text{right} \end{cases}\)
ContinueUse the usual InterpolationMethod formula with
\(y = \begin{cases} y_1 = y_\text{left} \text{ and } y_2 = y_{\text{left}+1} & x < x_\text{left} \\ y_1 = y_{\text{right}-1} \text{ and } y_2 = y_\text{right} & x > x_\text{right} \end{cases}\)
where \(y_{\text{left}+1}\) is the y value corresponding to the second-lowest x value, and \(y_{\text{right}-1}\) is the y value corresponding to the second-highest x value.

Definition at line 96 of file InterpolationGrid.hh.

97 { Zero, Endpoint,

◆ InterpolationMethod

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
enum class marley::InterpolationGrid::InterpolationMethod
strong

Method to use for interpolating between (x,y) grid points.

For \(x_1 \leq x < x_2\), the table below describes how the corresponding y value is calculated using each of the allowed InterpolationMethod settings.

Interpolation Methods
MethodDescription
Constant\(y = y_1\)
LinearLinear\(y = y_1 + \frac{y_2 - y_1}{x_2 - x_1}(x - x_1) \)
LinearLog\(y = \exp\left[\ln(y_1) + \frac{\ln(y_2) - \ln(y_1)}{x_2 - x_1}(x - x_1)\right] \)
LogLinear\(y = y_1 + \frac{y_2 - y_1}{\ln[x_2] - \ln[x_1]}[\ln(x) - \ln(x_1)] \)
LogLog\(y = \exp\left[\ln(y_1) + \frac{\ln(y_2) - \ln(y_1)}{\ln(x_2) - \ln(x_1)}(\ln[x] - \ln[x_1])\right] \)

Definition at line 63 of file InterpolationGrid.hh.

64 { Constant = 1,
65 LinearLinear = 2, LinearLog = 3, LogLinear = 4,

Constructor & Destructor Documentation

◆ InterpolationGrid() [1/3]

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
marley::InterpolationGrid< FirstNumericType, SecondNumericType >::InterpolationGrid ( InterpolationMethod interp_method = InterpolationMethod::LinearLinear,
ExtrapolationMethod extrap_method = ExtrapolationMethod::Zero )
inline

Create an InterpolationGrid without any grid points.

Definition at line 100 of file InterpolationGrid.hh.

103 : interpolation_method_( interp_method ),
104 extrapolation_method_( extrap_method )
105 {
One-dimensional function y(x) defined using a grid of ordered pairs (x,y) and an interpolation rule.

◆ InterpolationGrid() [2/3]

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
marley::InterpolationGrid< FirstNumericType, SecondNumericType >::InterpolationGrid ( const Grid & grid,
InterpolationMethod interp_method = InterpolationMethod::LinearLinear,
ExtrapolationMethod extrap_method = ExtrapolationMethod::Zero )
inline

Create an InterpolationGrid from a vector of ordered pairs.

Todo
Add error checks for the supplied grid

Definition at line 108 of file InterpolationGrid.hh.

112 : interpolation_method_( interp_method ),
113 extrapolation_method_( extrap_method ), ordered_pairs_( grid )
114 {

◆ InterpolationGrid() [3/3]

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
marley::InterpolationGrid< FirstNumericType, SecondNumericType >::InterpolationGrid ( const std::vector< FirstNumericType > & xs,
const std::vector< SecondNumericType > & ys,
InterpolationMethod interp_method = InterpolationMethod::LinearLinear,
ExtrapolationMethod extrap_method = ExtrapolationMethod::Zero )
inline

Create an InterpolationGrid from vectors of x and y values.

Definition at line 118 of file InterpolationGrid.hh.

123 : interpolation_method_( interp_method ),
124 extrapolation_method_( extrap_method )
125 {
126 if ( xs.size() != ys.size() ) throw marley::Error(
127 std::string("Vectors of x and y values passed to the constructor")
128 + " of marley::InterpolationGrid have unequal sizes." );
129
131 for ( size_t j = 0; j < xs.size(); ++j ) {
132 double new_x = xs.at( j );
133 if ( new_x <= old_x ) throw marley::Error( "The grid point x-values"
134 " defined for a marley::InterpolationGrid object are not strictly"
135 " increasing" );
136 ordered_pairs_.push_back( OrderedPair( xs.at(j), ys.at(j) ) );
137 }
size_t size() const
Get the number of ordered pairs on the grid.
OrderedPair & at(size_t j)
Get a reference to the jth ordered pair from the grid.

Member Function Documentation

◆ at()

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
OrderedPair & marley::InterpolationGrid< FirstNumericType, SecondNumericType >::at ( size_t j)
inline

Get a reference to the jth ordered pair from the grid.

Definition at line 152 of file InterpolationGrid.hh.

◆ back()

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
const OrderedPair & marley::InterpolationGrid< FirstNumericType, SecondNumericType >::back ( ) const
inline

Returns a reference to the last ordered pair.

Definition at line 187 of file InterpolationGrid.hh.

◆ clear()

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
void marley::InterpolationGrid< FirstNumericType, SecondNumericType >::clear ( )
inline

Delete all ordered pairs from the grid.

Definition at line 149 of file InterpolationGrid.hh.

◆ extrapolation_method()

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
ExtrapolationMethod marley::InterpolationGrid< FirstNumericType, SecondNumericType >::extrapolation_method ( ) const
inline

Get the ExtrapolationMethod used by this InterpolationGrid.

Definition at line 198 of file InterpolationGrid.hh.

◆ front()

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
const OrderedPair & marley::InterpolationGrid< FirstNumericType, SecondNumericType >::front ( ) const
inline

Returns a reference to the first ordered pair.

Definition at line 184 of file InterpolationGrid.hh.

◆ get_function()

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
std::function< SecondNumericType(FirstNumericType) > marley::InterpolationGrid< FirstNumericType, SecondNumericType >::get_function ( )
inline

Get a std::function object that represents y(x) for this InterpolationGrid.

Definition at line 156 of file InterpolationGrid.hh.

158 {
159 return [ this ]( FirstNumericType x )
160 -> SecondNumericType { return this->interpolate( x ); };
SecondNumericType interpolate(FirstNumericType x) const
Compute y(x) using the current InterpolationMethod.

◆ insert()

template<typename FirstNumericType, typename SecondNumericType>
void marley::InterpolationGrid< FirstNumericType, SecondNumericType >::insert ( FirstNumericType x,
SecondNumericType y )

Add a new ordered pair (x, y) to the grid.

Definition at line 343 of file InterpolationGrid.hh.

345 {
346 // Figure out where this grid point should go in the grid. Use
347 // std::upper_bound so that entries with the same x value (discontinuities)
348 // are inserted in the order that they are passed to
349 // InterpolationGrid::insert.
350 GridConstIterator insert_point = upper_bound( ordered_pairs_.begin(),
351 ordered_pairs_.end(), x );
352
353 // Insert the new grid point
354 ordered_pairs_.insert( insert_point, OrderedPair(x, y) );
355 }
GridConstIterator upper_bound(const GridConstIterator &begin, const GridConstIterator &end, FirstNumericType x) const
Returns a const_iterator to the first element of the grid for which the x value is greater than x.

References upper_bound().

◆ interpolate()

template<typename FirstNumericType, typename SecondNumericType>
SecondNumericType marley::InterpolationGrid< FirstNumericType, SecondNumericType >::interpolate ( FirstNumericType x) const

Compute y(x) using the current InterpolationMethod.

Definition at line 269 of file InterpolationGrid.hh.

270 {
271 // Find grid points just below [(x1, y1)] and just above [(x2, y2)]
273 // Includes a call to check_grid()
274 bool extrapolate = find_bin_limits( x, lower_point, upper_point );
275
276 // If the requested x value is outside of the grid, use the correct method
277 // for dealing with this situation based on the value of
278 if ( extrapolate ) {
279 if ( extrapolation_method_ == ExtrapolationMethod::Zero )
280 return static_cast< SecondNumericType >( 0. );
281 else if ( extrapolation_method_ == ExtrapolationMethod::Endpoint ) {
282 if ( lower_point->first > x ) return lower_point->second;
283 else return upper_point->second;
284 }
285 else if ( extrapolation_method_ == ExtrapolationMethod::Throw ) {
286 throw marley::Error( "x = " + std::to_string( x )
287 + " lies outside of the current interpolation grid object"
288 " (which extends from x_min = "
289 + std::to_string( ordered_pairs_.front().first )
290 + " and x_max = " + std::to_string( ordered_pairs_.back().first )
291 + ") and extrapolation is disabled." );
292 }
293 }
294
295 // Either the requested point falls within the grid or
296 // extrapolation_method == ExtrapolationMethod::Continue. If the "continue"
297 // method for extrapolation is selected, then we continue to use the same
298 // interpolation technique formula outside of the grid (for
299 // InterpolationMethod::LinearLinear, this is linear-linear extrapolation).
300
301 // If the constant interpolation method is selected, then use the *lower
302 // bound* of each bin as the interpolated value. If we're extrapolating,
303 // on the right, use the upper bound.
304 if ( interpolation_method_ == InterpolationMethod::Constant ) {
305 if ( !extrapolate ) return lower_point->second;
306 else if ( lower_point->first > x ) return lower_point->second;
307 else return upper_point->second;
308 }
309
310 // We'll use an interpolation formula for all other methods, so
311 // get the grid point x and y values for later use.
316
317 bool log_x = false, log_y = false;
319 if ( interpolation_method_ == InterpolationMethod::LinearLog )
320 log_x = true;
321 else if ( interpolation_method_ == InterpolationMethod::LogLinear )
322 log_y = true;
323 else if ( interpolation_method_ == InterpolationMethod::LogLog ) {
324 log_x = true;
325 log_y = true;
326 }
327 if ( log_x ) {
328 x1 = std::log( x1 );
329 x2 = std::log( x2 );
330 x_to_use = std::log( x );
331 }
332 if ( log_y ) {
333 y1 = std::log( y1 );
334 y2 = std::log( y2 );
335 }
336
337 FirstNumericType y_interp = y1 + ( (y2 - y1)/(x2 - x1) )*( x_to_use - x1 );
338 if ( log_y ) y_interp = std::exp( y_interp );
339 return y_interp;
340 }

References interpolate().

Referenced by marley::InterpolationMethod< double >::get_function(), and interpolate().

◆ interpolation_method()

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
InterpolationMethod marley::InterpolationGrid< FirstNumericType, SecondNumericType >::interpolation_method ( ) const
inline

Get the InterpolationMethod used by this InterpolationGrid.

Definition at line 190 of file InterpolationGrid.hh.

◆ lower_bound()

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
GridConstIterator marley::InterpolationGrid< FirstNumericType, SecondNumericType >::lower_bound ( const GridConstIterator & begin,
const GridConstIterator & end,
FirstNumericType x ) const
inline

Returns a const_iterator to the first element of the grid for which the x value is not less than (i.e. greater than or equal to) x.

Definition at line 164 of file InterpolationGrid.hh.

167 {
168 return std::lower_bound( begin, end, x,
169 []( const OrderedPair& pair, const FirstNumericType& f )
170 -> bool { return pair.first < f; } );

◆ set_extrapolation_method()

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
void marley::InterpolationGrid< FirstNumericType, SecondNumericType >::set_extrapolation_method ( ExtrapolationMethod method)
inline

Set the ExtrapolationMethod to use.

Definition at line 202 of file InterpolationGrid.hh.

◆ set_interpolation_method()

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
void marley::InterpolationGrid< FirstNumericType, SecondNumericType >::set_interpolation_method ( InterpolationMethod method)
inline

Set the InterpolationMethod to use.

Definition at line 194 of file InterpolationGrid.hh.

◆ size()

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
size_t marley::InterpolationGrid< FirstNumericType, SecondNumericType >::size ( ) const
inline

Get the number of ordered pairs on the grid.

Definition at line 146 of file InterpolationGrid.hh.

◆ upper_bound()

template<typename FirstNumericType, typename SecondNumericType = FirstNumericType>
GridConstIterator marley::InterpolationGrid< FirstNumericType, SecondNumericType >::upper_bound ( const GridConstIterator & begin,
const GridConstIterator & end,
FirstNumericType x ) const
inline

Returns a const_iterator to the first element of the grid for which the x value is greater than x.

Definition at line 174 of file InterpolationGrid.hh.

178 {
179 return std::upper_bound( begin, end, x,
180 []( const FirstNumericType& f, const OrderedPair& pair )
181 -> bool { return f < pair.first; } );

Referenced by insert().


The documentation for this class was generated from the following file: