File indexing completed on 2025-09-17 08:54:05
0001
0002
0003
0004
0005
0006
0007 #pragma once
0008
0009 #include "corecel/Assert.hh"
0010 #include "corecel/Macros.hh"
0011 #include "corecel/Types.hh"
0012 #include "corecel/cont/Span.hh"
0013
0014 #include "../NonuniformGridData.hh"
0015 #include "../UniformGrid.hh"
0016 #include "../UniformGridData.hh"
0017
0018 namespace celeritas
0019 {
0020 namespace detail
0021 {
0022
0023
0024
0025
0026 class GridAccessor
0027 {
0028 public:
0029
0030
0031 using SpanConstReal = Span<real_type const>;
0032 using Values
0033 = Collection<real_type, Ownership::const_reference, MemSpace::native>;
0034
0035
0036 public:
0037
0038 virtual ~GridAccessor() = default;
0039
0040
0041 virtual real_type x(size_type index) const = 0;
0042
0043
0044 virtual real_type y(size_type index) const = 0;
0045
0046
0047 virtual size_type size() const = 0;
0048
0049
0050 inline real_type delta_x(size_type index) const;
0051
0052
0053 inline real_type delta_y(size_type index) const;
0054
0055
0056 inline real_type delta_slope(size_type index) const;
0057 };
0058
0059
0060
0061
0062
0063 class NonuniformGridAccessor : public GridAccessor
0064 {
0065 public:
0066
0067 inline NonuniformGridAccessor(SpanConstReal x_values,
0068 SpanConstReal y_values);
0069
0070
0071 inline NonuniformGridAccessor(NonuniformGridRecord const& grid,
0072 Values const& values);
0073
0074
0075 inline real_type x(size_type index) const final;
0076
0077
0078 inline real_type y(size_type index) const final;
0079
0080
0081 size_type size() const final { return x_values_.size(); }
0082
0083 private:
0084 SpanConstReal x_values_;
0085 SpanConstReal y_values_;
0086 };
0087
0088
0089
0090
0091
0092 class UniformGridAccessor : public GridAccessor
0093 {
0094 public:
0095
0096 inline UniformGridAccessor(UniformGridRecord const& grid,
0097 Values const& values);
0098
0099
0100 inline real_type x(size_type index) const final;
0101
0102
0103 inline real_type y(size_type index) const final;
0104
0105
0106 size_type size() const final { return loge_grid_.size(); }
0107
0108 private:
0109 UniformGridRecord const& data_;
0110 Values const& reals_;
0111 UniformGrid loge_grid_;
0112 };
0113
0114
0115
0116
0117
0118 class InverseGridAccessor : public GridAccessor
0119 {
0120 public:
0121
0122
0123 using Values
0124 = Collection<real_type, Ownership::const_reference, MemSpace::native>;
0125
0126
0127 public:
0128
0129 inline InverseGridAccessor(UniformGridRecord const& grid,
0130 Values const& values);
0131
0132
0133 inline real_type x(size_type index) const final;
0134
0135
0136 inline real_type y(size_type index) const final;
0137
0138
0139 size_type size() const final { return loge_grid_.size(); }
0140
0141 private:
0142 UniformGridRecord const& data_;
0143 Values const& reals_;
0144 UniformGrid loge_grid_;
0145 };
0146
0147
0148
0149
0150
0151
0152
0153 real_type GridAccessor::delta_x(size_type index) const
0154 {
0155 return this->x(index + 1) - this->x(index);
0156 }
0157
0158
0159
0160
0161
0162 real_type GridAccessor::delta_y(size_type index) const
0163 {
0164 return this->y(index + 1) - this->y(index);
0165 }
0166
0167
0168
0169
0170
0171
0172
0173
0174 real_type GridAccessor::delta_slope(size_type index) const
0175 {
0176 CELER_EXPECT(index > 0);
0177 return this->delta_y(index) / this->delta_x(index)
0178 - this->delta_y(index - 1) / this->delta_x(index - 1);
0179 }
0180
0181
0182
0183
0184
0185 NonuniformGridAccessor::NonuniformGridAccessor(SpanConstReal x_values,
0186 SpanConstReal y_values)
0187 : x_values_(x_values), y_values_(y_values)
0188 {
0189 CELER_EXPECT(x_values_.size() == y_values_.size());
0190 }
0191
0192
0193
0194
0195
0196 NonuniformGridAccessor::NonuniformGridAccessor(NonuniformGridRecord const& grid,
0197 Values const& values)
0198
0199 : NonuniformGridAccessor(values[grid.grid], values[grid.value])
0200 {
0201 }
0202
0203
0204
0205
0206
0207 real_type NonuniformGridAccessor::x(size_type index) const
0208 {
0209 CELER_EXPECT(index < this->size());
0210 return x_values_[index];
0211 }
0212
0213
0214
0215
0216
0217 real_type NonuniformGridAccessor::y(size_type index) const
0218 {
0219 CELER_EXPECT(index < this->size());
0220 return y_values_[index];
0221 }
0222
0223
0224
0225
0226
0227 UniformGridAccessor::UniformGridAccessor(UniformGridRecord const& grid,
0228 Values const& values)
0229 : data_(grid), reals_(values), loge_grid_(data_.grid)
0230 {
0231 CELER_EXPECT(data_);
0232 }
0233
0234
0235
0236
0237
0238 real_type UniformGridAccessor::x(size_type index) const
0239 {
0240 CELER_EXPECT(index < this->size());
0241 return std::exp(loge_grid_[index]);
0242 }
0243
0244
0245
0246
0247
0248 real_type UniformGridAccessor::y(size_type index) const
0249 {
0250 CELER_EXPECT(index < this->size());
0251 return reals_[data_.value[index]];
0252 }
0253
0254
0255
0256
0257
0258 InverseGridAccessor::InverseGridAccessor(UniformGridRecord const& grid,
0259 Values const& values)
0260 : data_(grid), reals_(values), loge_grid_(data_.grid)
0261 {
0262 CELER_EXPECT(data_);
0263 }
0264
0265
0266
0267
0268
0269 real_type InverseGridAccessor::x(size_type index) const
0270 {
0271 CELER_EXPECT(index < this->size());
0272 return reals_[data_.value[index]];
0273 }
0274
0275
0276
0277
0278
0279 real_type InverseGridAccessor::y(size_type index) const
0280 {
0281 CELER_EXPECT(index < this->size());
0282 return std::exp(loge_grid_[index]);
0283 }
0284
0285
0286 }
0287 }