Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-26 10:24:14

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 orange/univ/RectArrayTracker.hh
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  * Track a particle within an axes-aligned rectilinear grid.
0024  */
0025 class RectArrayTracker
0026 {
0027   public:
0028     //!@{
0029     //! \name Type aliases
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     // Construct with parameters (unit definitions and this one's ID)
0044     inline CELER_FUNCTION RectArrayTracker(ParamsRef const& params,
0045                                            RectArrayId rid);
0046 
0047     //// ACCESSORS ////
0048 
0049     //! Number of local volumes
0050     CELER_FUNCTION LocalVolumeId::size_type num_volumes() const
0051     {
0052         return record_.daughters.size();
0053     }
0054 
0055     //! Number of local surfaces
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     // DaughterId of universe embedded in a given volume
0067     inline CELER_FUNCTION DaughterId daughter(LocalVolumeId vol) const;
0068 
0069     // Conversion of G4 to RectArray not yet implemented
0070     CELER_FUNCTION vol_level_uint local_vol_level(LocalVolumeId) const
0071     {
0072         CELER_ASSERT_UNREACHABLE();
0073     }
0074 
0075     // Canonical parent volume placement in the local universe, if any
0076     inline CELER_FUNCTION LocalVolumeId local_parent(LocalVolumeId) const
0077     {
0078         CELER_ASSERT_UNREACHABLE();
0079     }
0080 
0081     ////// OPERATIONS ////
0082 
0083     // Find the local volume from a position
0084     inline CELER_FUNCTION Initialization initialize(
0085         LocalState const& state) const;
0086 
0087     // DEPRECATED: search for intersection without limit
0088     inline CELER_FUNCTION Intersection intersect(LocalState const& state) const;
0089 
0090     // Calculate distance-to-intercept for the next surface, with max distance
0091     inline CELER_FUNCTION Intersection intersect(LocalState const& state,
0092                                                  real_type max_dist) const;
0093 
0094     // Find the local volume given a post-crossing state
0095     inline CELER_FUNCTION Initialization cross_boundary(
0096         LocalState const& state) const;
0097 
0098     // Calculate closest distance to a surface in any direction
0099     inline CELER_FUNCTION real_type safety(Real3 const& pos,
0100                                            LocalVolumeId vol) const;
0101 
0102     // Calculate the local surface normal
0103     inline CELER_FUNCTION Real3 normal(Real3 const& pos,
0104                                        LocalSurfaceId surf) const;
0105 
0106   private:
0107     //// DATA ////
0108     ParamsRef const& params_;
0109     RectArrayRecord const& record_;
0110 
0111     //// METHODS ////
0112 
0113     // Find the index of axis (x/y/z) we are about to cross
0114     inline CELER_FUNCTION size_type find_surface_axis_idx(
0115         LocalSurfaceId s) const;
0116 
0117     // Create Grid object for a given axis
0118     inline CELER_FUNCTION Grid make_grid(Axis ax) const;
0119 };
0120 
0121 //---------------------------------------------------------------------------//
0122 // INLINE DEFINITIONS
0123 //---------------------------------------------------------------------------//
0124 /*!
0125  * Construct with reference to persistent parameter data.
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  * Find the local volume from a position.
0137  *
0138  * To avoid edge cases and inconsistent logical/physical states, it is
0139  * prohibited to initialize from an arbitrary point directly onto a surface.
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             // Outside the rect array
0157             return {};
0158         }
0159         else
0160         {
0161             size_type index = grid.find(pos);
0162             if (grid[index] == pos)
0163             {
0164                 // Initialization exactly on an edge is prohibited
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  * Find the local volume given a post-crossing state.
0178  */
0179 CELER_FUNCTION auto RectArrayTracker::cross_boundary(
0180     LocalState const& state) const -> Initialization
0181 {
0182     CELER_EXPECT(state.surface && state.volume);
0183 
0184     // Find the coords of the current volume
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     // Due to surface deduplication, crossing out of rect arrays is not
0191     // possible
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     // Value for incrementing the axial coordinate upon crossing
0198     // NOTE: that surface sense is currently the POST crossing value
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  * Search for an intersection without a distance limit.
0208  *
0209  * \deprecated Provide a physically reasonable upper bound to the distance
0210  * to reduce search cost and avoid a redundant method.
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  * Calculate distance-to-intercept for the next surface, with max distance.
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         // Ignore any stationary axis
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  * Calculate nearest distance to a surface in any direction.
0267  *
0268  * On an axis-aligned rectlinear grid the minimum distance to any surface is
0269  * always always occurs along a line parallel to an axis.
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  * Calculate the local surface normal.
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  * DaughterId of universe embedded in a given volume.
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  * Find the index of axis (x/y/z) we are about to cross:
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  * Create Grid object for a given axis.
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 }  // namespace celeritas