File indexing completed on 2026-05-10 08:36:12
0001
0002
0003
0004
0005
0006
0007 #pragma once
0008
0009 #include <cmath>
0010
0011 #include "corecel/Assert.hh"
0012 #include "corecel/data/Collection.hh"
0013 #include "corecel/grid/Interpolator.hh"
0014 #include "corecel/grid/NonuniformGrid.hh"
0015 #include "corecel/grid/SplineInterpolator.hh"
0016 #include "corecel/grid/UniformGrid.hh"
0017 #include "corecel/grid/UniformGridData.hh"
0018 #include "corecel/math/Algorithms.hh"
0019 #include "corecel/math/Quantity.hh"
0020 #include "celeritas/Quantities.hh"
0021
0022 namespace celeritas
0023 {
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044 class InverseRangeCalculator
0045 {
0046 public:
0047
0048
0049 using Energy = units::MevEnergy;
0050 using Values
0051 = Collection<real_type, Ownership::const_reference, MemSpace::native>;
0052
0053
0054 public:
0055
0056 inline CELER_FUNCTION InverseRangeCalculator(UniformGridRecord const& grid,
0057 Values const& values);
0058
0059
0060 inline CELER_FUNCTION Energy operator()(real_type range) const;
0061
0062 private:
0063 UniformGrid log_energy_;
0064 NonuniformGrid<real_type> range_;
0065 Span<real_type const> deriv_;
0066 };
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077 CELER_FUNCTION
0078 InverseRangeCalculator::InverseRangeCalculator(UniformGridRecord const& grid,
0079 Values const& values)
0080 : log_energy_(grid.grid)
0081 , range_(grid.value, values)
0082 , deriv_(values[grid.derivative])
0083 {
0084 CELER_EXPECT(range_.size() == log_energy_.size());
0085 }
0086
0087
0088
0089
0090
0091 CELER_FUNCTION auto InverseRangeCalculator::operator()(real_type range) const
0092 -> Energy
0093 {
0094 CELER_EXPECT(range >= 0 && range <= range_.back());
0095
0096 if (range < range_.front())
0097 {
0098
0099
0100 return Energy{std::exp(log_energy_.front())
0101 * ipow<2>(range / range_.front())};
0102 }
0103
0104
0105 if (CELER_UNLIKELY(range >= range_.back()))
0106 {
0107 CELER_ASSERT(range == range_.back());
0108 return Energy{std::exp(log_energy_.back())};
0109 }
0110
0111
0112 auto idx = range_.find(range);
0113 CELER_ASSERT(idx + 1 < log_energy_.size());
0114
0115 real_type result;
0116 if (deriv_.empty())
0117 {
0118
0119 result = LinearInterpolator<real_type>(
0120 {range_[idx], std::exp(log_energy_[idx])},
0121 {range_[idx + 1], std::exp(log_energy_[idx + 1])})(range);
0122 }
0123 else
0124 {
0125
0126 result = SplineInterpolator<real_type>(
0127 {range_[idx], std::exp(log_energy_[idx]), deriv_[idx]},
0128 {range_[idx + 1], std::exp(log_energy_[idx + 1]), deriv_[idx + 1]})(
0129 range);
0130 }
0131 return Energy{result};
0132 }
0133
0134
0135 }