File indexing completed on 2026-11-26 10:24:14
0001
0002
0003
0004
0005
0006
0007 #pragma once
0008
0009 #include "corecel/Assert.hh"
0010 #include "corecel/data/HyperslabIndexer.hh"
0011 #include "corecel/grid/NonuniformGrid.hh"
0012 #include "corecel/math/Algorithms.hh"
0013 #include "orange/OrangeData.hh"
0014
0015 #include "detail/RaggedRightIndexer.hh"
0016 #include "detail/Types.hh"
0017 #include "detail/Utils.hh"
0018
0019 namespace celeritas
0020 {
0021
0022
0023
0024
0025 class RectArrayTracker
0026 {
0027 public:
0028
0029
0030 using ParamsRef = NativeCRef<OrangeParamsData>;
0031 using Initialization = detail::Initialization;
0032 using Intersection = detail::Intersection;
0033 using LocalState = detail::LocalState;
0034 using Grid = NonuniformGrid<real_type>;
0035 using VolumeIndexer = HyperslabIndexer<3>;
0036 using VolumeInverseIndexer = HyperslabInverseIndexer<3>;
0037 using SurfaceIndexer = detail::RaggedRightIndexer<3>;
0038 using SurfaceInverseIndexer = detail::RaggedRightInverseIndexer<3>;
0039 using Coords = Array<size_type, 3>;
0040
0041
0042 public:
0043
0044 inline CELER_FUNCTION RectArrayTracker(ParamsRef const& params,
0045 RectArrayId rid);
0046
0047
0048
0049
0050 CELER_FUNCTION LocalVolumeId::size_type num_volumes() const
0051 {
0052 return record_.daughters.size();
0053 }
0054
0055
0056 CELER_FUNCTION LocalSurfaceId::size_type num_surfaces() const
0057 {
0058 size_type num_surfs = 0;
0059 for (auto ax : range(Axis::size_))
0060 {
0061 num_surfs += record_.dims[to_int(ax)] + 1;
0062 }
0063 return num_surfs;
0064 }
0065
0066
0067 inline CELER_FUNCTION DaughterId daughter(LocalVolumeId vol) const;
0068
0069
0070 CELER_FUNCTION vol_level_uint local_vol_level(LocalVolumeId) const
0071 {
0072 CELER_ASSERT_UNREACHABLE();
0073 }
0074
0075
0076 inline CELER_FUNCTION LocalVolumeId local_parent(LocalVolumeId) const
0077 {
0078 CELER_ASSERT_UNREACHABLE();
0079 }
0080
0081
0082
0083
0084 inline CELER_FUNCTION Initialization initialize(
0085 LocalState const& state) const;
0086
0087
0088 inline CELER_FUNCTION Intersection intersect(LocalState const& state) const;
0089
0090
0091 inline CELER_FUNCTION Intersection intersect(LocalState const& state,
0092 real_type max_dist) const;
0093
0094
0095 inline CELER_FUNCTION Initialization cross_boundary(
0096 LocalState const& state) const;
0097
0098
0099 inline CELER_FUNCTION real_type safety(Real3 const& pos,
0100 LocalVolumeId vol) const;
0101
0102
0103 inline CELER_FUNCTION Real3 normal(Real3 const& pos,
0104 LocalSurfaceId surf) const;
0105
0106 private:
0107
0108 ParamsRef const& params_;
0109 RectArrayRecord const& record_;
0110
0111
0112
0113
0114 inline CELER_FUNCTION size_type find_surface_axis_idx(
0115 LocalSurfaceId s) const;
0116
0117
0118 inline CELER_FUNCTION Grid make_grid(Axis ax) const;
0119 };
0120
0121
0122
0123
0124
0125
0126
0127 CELER_FUNCTION RectArrayTracker::RectArrayTracker(ParamsRef const& params,
0128 RectArrayId rid)
0129 : params_(params), record_(params.rect_arrays[rid])
0130 {
0131 CELER_EXPECT(params_);
0132 }
0133
0134
0135
0136
0137
0138
0139
0140
0141 CELER_FUNCTION auto RectArrayTracker::initialize(LocalState const& state) const
0142 -> Initialization
0143 {
0144 CELER_EXPECT(params_);
0145 CELER_EXPECT(!state.surface && !state.volume);
0146
0147 Coords coords;
0148
0149 for (auto ax : range(Axis::size_))
0150 {
0151 auto const& pos = state.pos[to_int(ax)];
0152 auto grid = this->make_grid(ax);
0153
0154 if (pos < grid.front() || pos > grid.back())
0155 {
0156
0157 return {};
0158 }
0159 else
0160 {
0161 size_type index = grid.find(pos);
0162 if (grid[index] == pos)
0163 {
0164
0165 return {};
0166 }
0167 coords[to_int(ax)] = index;
0168 }
0169 }
0170
0171 VolumeIndexer to_index(record_.dims);
0172 return {id_cast<LocalVolumeId>(to_index(coords)), {}};
0173 }
0174
0175
0176
0177
0178
0179 CELER_FUNCTION auto RectArrayTracker::cross_boundary(
0180 LocalState const& state) const -> Initialization
0181 {
0182 CELER_EXPECT(state.surface && state.volume);
0183
0184
0185 VolumeIndexer to_index(record_.dims);
0186 VolumeInverseIndexer to_coords(record_.dims);
0187 auto coords = to_coords(state.volume.unchecked_get());
0188 auto ax_idx = this->find_surface_axis_idx(state.surface.id());
0189
0190
0191
0192 CELER_ASSERT(
0193 !(coords[ax_idx] == 0 && state.surface.sense() == Sense::inside)
0194 && !(coords[ax_idx] == record_.dims[ax_idx] - 1
0195 && state.surface.sense() == Sense::outside));
0196
0197
0198
0199 int inc = (state.surface.sense() == Sense::outside) ? 1 : -1;
0200
0201 coords[ax_idx] += inc;
0202 return {LocalVolumeId(to_index(coords)), state.surface};
0203 }
0204
0205
0206
0207
0208
0209
0210
0211
0212 CELER_FORCEINLINE_FUNCTION auto RectArrayTracker::intersect(
0213 LocalState const& state) const -> Intersection
0214 {
0215 return this->intersect(state, NumericLimits<real_type>::max());
0216 }
0217
0218
0219
0220
0221
0222 CELER_FUNCTION auto RectArrayTracker::intersect(
0223 LocalState const& state, real_type max_dist) const -> Intersection
0224 {
0225 CELER_EXPECT(max_dist > 0);
0226 CELER_EXPECT(state.volume);
0227
0228 auto coords
0229 = VolumeInverseIndexer{record_.dims}(state.volume.unchecked_get());
0230
0231 Intersection result{{}, max_dist};
0232 SurfaceIndexer to_index(record_.surface_indexer_data);
0233
0234 for (auto ax : range(Axis::size_))
0235 {
0236 auto dir = state.dir[to_int(ax)];
0237
0238
0239 if (dir == 0)
0240 {
0241 continue;
0242 }
0243
0244 size_type target_coord = coords[to_int(ax)] + static_cast<int>(dir > 0);
0245 real_type target_value = this->make_grid(ax)[target_coord];
0246
0247 real_type dist = (target_value - state.pos[to_int(ax)])
0248 / state.dir[to_int(ax)];
0249
0250 if (dist > 0 && detail::IsNotFurtherThan{result.distance}(dist))
0251 {
0252 result.distance = dist;
0253
0254 auto local_surface = LocalSurfaceId(
0255 to_index({static_cast<size_type>(to_int(ax)), target_coord}));
0256 result.surface
0257 = {local_surface, dir > 0 ? Sense::inside : Sense::outside};
0258 }
0259 }
0260
0261 return result;
0262 }
0263
0264
0265
0266
0267
0268
0269
0270
0271 CELER_FUNCTION real_type RectArrayTracker::safety(Real3 const& pos,
0272 LocalVolumeId vol_id) const
0273 {
0274 CELER_EXPECT(vol_id && vol_id.get() < this->num_volumes());
0275
0276 VolumeInverseIndexer to_coords(record_.dims);
0277 auto coords = to_coords(vol_id.unchecked_get());
0278
0279 real_type min_dist = numeric_limits<real_type>::infinity();
0280
0281 for (auto ax : range(Axis::size_))
0282 {
0283 auto grid = this->make_grid(ax);
0284 for (auto i : range(2))
0285 {
0286 auto target_coord = coords[to_int(ax)] + i;
0287 real_type target = grid[target_coord];
0288 min_dist = min(min_dist, std::fabs(pos[to_int(ax)] - target));
0289 }
0290 }
0291
0292 CELER_ENSURE(min_dist >= 0
0293 && min_dist < numeric_limits<real_type>::infinity());
0294 return min_dist;
0295 }
0296
0297
0298
0299
0300
0301 CELER_FUNCTION auto RectArrayTracker::normal(
0302 Real3 const&, LocalSurfaceId surf) const -> Real3
0303 {
0304 CELER_EXPECT(surf && surf.get() < this->num_surfaces());
0305 size_type ax = this->find_surface_axis_idx(surf);
0306
0307 Real3 normal{0, 0, 0};
0308 normal[ax] = 1;
0309
0310 return normal;
0311 }
0312
0313
0314
0315
0316
0317 CELER_FORCEINLINE_FUNCTION DaughterId RectArrayTracker::daughter(
0318 LocalVolumeId vol) const
0319 {
0320 CELER_EXPECT(vol && vol.get() < this->num_volumes());
0321 return record_.daughters[vol];
0322 }
0323
0324
0325
0326
0327
0328 CELER_FUNCTION size_type RectArrayTracker::find_surface_axis_idx(
0329 LocalSurfaceId s) const
0330 {
0331 SurfaceInverseIndexer to_axis(record_.surface_indexer_data);
0332 return to_axis(s.unchecked_get())[0];
0333 }
0334
0335
0336
0337
0338
0339 CELER_FUNCTION RectArrayTracker::Grid RectArrayTracker::make_grid(Axis ax) const
0340 {
0341 return Grid(record_.grid[to_int(ax)], params_.reals);
0342 }
0343
0344
0345 }