Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-02 09:59:07

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file celeritas/grid/RangeCalculator.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <cmath>
0010 
0011 #include "corecel/data/Collection.hh"
0012 #include "corecel/grid/Interpolator.hh"
0013 #include "corecel/grid/SplineInterpolator.hh"
0014 #include "corecel/grid/UniformGrid.hh"
0015 #include "corecel/grid/UniformGridData.hh"
0016 #include "corecel/math/Quantity.hh"
0017 #include "celeritas/Quantities.hh"
0018 
0019 namespace celeritas
0020 {
0021 //---------------------------------------------------------------------------//
0022 /*!
0023  * Find and interpolate range on a uniform log grid.
0024  *
0025  * \code
0026     RangeCalculator calc_range(xs_grid, xs_params.reals);
0027     real_type range = calc_range(particle);
0028    \endcode
0029  *
0030  * Below the minimum tabulated energy, the range is scaled:
0031  * \f[
0032     r = r_\mathrm{min} \sqrt{\frac{E}{E_\mathrm{min}}}
0033  * \f]
0034  */
0035 class RangeCalculator
0036 {
0037   public:
0038     //!@{
0039     //! \name Type aliases
0040     using Energy = units::MevEnergy;
0041     using Values
0042         = Collection<real_type, Ownership::const_reference, MemSpace::native>;
0043     //!@}
0044 
0045   public:
0046     // Construct from state-independent data
0047     inline CELER_FUNCTION RangeCalculator(UniformGridRecord const& grid,
0048                                           Values const& values);
0049 
0050     // Find and interpolate from the energy
0051     inline CELER_FUNCTION real_type operator()(Energy energy) const;
0052 
0053   private:
0054     UniformGridRecord const& data_;
0055     Values const& reals_;
0056 
0057     CELER_FORCEINLINE_FUNCTION real_type get(size_type index) const;
0058 };
0059 
0060 //---------------------------------------------------------------------------//
0061 // INLINE DEFINITIONS
0062 //---------------------------------------------------------------------------//
0063 /*!
0064  * Construct from cross section data.
0065  *
0066  * Range tables should be uniform in energy, without extra scaling.
0067  */
0068 CELER_FUNCTION RangeCalculator::RangeCalculator(UniformGridRecord const& grid,
0069                                                 Values const& values)
0070     : data_(grid), reals_(values)
0071 {
0072     CELER_EXPECT(data_);
0073 }
0074 
0075 //---------------------------------------------------------------------------//
0076 /*!
0077  * Calculate the range.
0078  */
0079 CELER_FUNCTION real_type RangeCalculator::operator()(Energy energy) const
0080 {
0081     using namespace celeritas::literals;
0082 
0083     CELER_ASSERT(energy > zero_quantity());
0084     UniformGrid loge_grid(data_.grid);
0085     real_type const loge = std::log(energy.value());
0086 
0087     if (loge <= loge_grid.front())
0088     {
0089         real_type result = this->get(0);
0090         // Scale by sqrt(E/Emin) = exp(.5 (log E - log Emin))
0091         result *= std::exp(0.5_r * (loge - loge_grid.front()));
0092         return result;
0093     }
0094     else if (loge >= loge_grid.back())
0095     {
0096         // Clip to highest range value
0097         return this->get(loge_grid.size() - 1);
0098     }
0099 
0100     // Locate the energy bin
0101     auto idx = loge_grid.find(loge);
0102     CELER_ASSERT(idx + 1 < loge_grid.size());
0103 
0104     real_type result;
0105     if (data_.derivative.empty())
0106     {
0107         // Interpolate *linearly* on energy
0108         result = LinearInterpolator<real_type>(
0109             {std::exp(loge_grid[idx]), this->get(idx)},
0110             {std::exp(loge_grid[idx + 1]), this->get(idx + 1)})(
0111             value_as<Energy>(energy));
0112     }
0113     else
0114     {
0115         // Use cubic spline interpolation
0116         real_type lower_deriv = reals_[data_.derivative[idx]];
0117         real_type upper_deriv = reals_[data_.derivative[idx + 1]];
0118 
0119         result = SplineInterpolator<real_type>(
0120             {std::exp(loge_grid[idx]), this->get(idx), lower_deriv},
0121             {std::exp(loge_grid[idx + 1]), this->get(idx + 1), upper_deriv})(
0122             value_as<Energy>(energy));
0123     }
0124     return result;
0125 }
0126 
0127 //---------------------------------------------------------------------------//
0128 /*!
0129  * Get the raw range data at a particular index.
0130  */
0131 CELER_FUNCTION real_type RangeCalculator::get(size_type index) const
0132 {
0133     CELER_EXPECT(index < reals_.size());
0134     return reals_[data_.value[index]];
0135 }
0136 
0137 //---------------------------------------------------------------------------//
0138 }  // namespace celeritas