File indexing completed on 2025-09-17 08:53:40
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/math/Algorithms.hh"
0018 #include "corecel/math/Quantity.hh"
0019
0020 #include "XsGridData.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 = RealQuantity<XsGridRecord::EnergyUnits>;
0050 using Values
0051 = Collection<real_type, Ownership::const_reference, MemSpace::native>;
0052
0053
0054 public:
0055
0056 inline CELER_FUNCTION
0057 InverseRangeCalculator(XsGridRecord const& grid, 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(XsGridRecord const& grid,
0079 Values const& values)
0080 : log_energy_(grid.lower.grid)
0081 , range_(grid.lower.value, values)
0082 , deriv_(values[grid.lower.derivative])
0083 {
0084 CELER_EXPECT(range_.size() == log_energy_.size());
0085 CELER_EXPECT(!grid.upper);
0086 }
0087
0088
0089
0090
0091
0092 CELER_FUNCTION auto
0093 InverseRangeCalculator::operator()(real_type range) const -> Energy
0094 {
0095 CELER_EXPECT(range >= 0 && range <= range_.back());
0096
0097 if (range < range_.front())
0098 {
0099
0100
0101 return Energy{std::exp(log_energy_.front())
0102 * ipow<2>(range / range_.front())};
0103 }
0104
0105
0106 if (CELER_UNLIKELY(range >= range_.back()))
0107 {
0108 CELER_ASSERT(range == range_.back());
0109 return Energy{std::exp(log_energy_.back())};
0110 }
0111
0112
0113 auto idx = range_.find(range);
0114 CELER_ASSERT(idx + 1 < log_energy_.size());
0115
0116 real_type result;
0117 if (deriv_.empty())
0118 {
0119
0120 result = LinearInterpolator<real_type>(
0121 {range_[idx], std::exp(log_energy_[idx])},
0122 {range_[idx + 1], std::exp(log_energy_[idx + 1])})(range);
0123 }
0124 else
0125 {
0126
0127 result = SplineInterpolator<real_type>(
0128 {range_[idx], std::exp(log_energy_[idx]), deriv_[idx]},
0129 {range_[idx + 1], std::exp(log_energy_[idx + 1]), deriv_[idx + 1]})(
0130 range);
0131 }
0132 return Energy{result};
0133 }
0134
0135
0136 }