Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-04 10:44:46

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 celeritas/optical/action/TrackSlotExecutor.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Assert.hh"
0010 #include "corecel/Types.hh"
0011 #include "corecel/sys/ThreadId.hh"
0012 #include "celeritas/optical/CoreTrackData.hh"
0013 #include "celeritas/optical/CoreTrackView.hh"
0014 #include "celeritas/optical/SimTrackView.hh"
0015 #include "celeritas/track/TrackFunctors.hh"
0016 
0017 namespace celeritas
0018 {
0019 namespace optical
0020 {
0021 //---------------------------------------------------------------------------//
0022 /*!
0023  * Transform a thread or track slot ID into a core track view.
0024  *
0025  * This class can be used to call a functor that applies to \c
0026  * optical::CoreTrackView using a \c TrackSlotId, so that the tracks can be
0027  * easily looped over as a group on CPU or GPU.
0028  *
0029  * To facilitate kernel launches, the class can also directly map \c ThreadId
0030  * to \c TrackSlotId, which will have the same numerical value because optical
0031  * photons do not implement sorting.
0032  */
0033 template<class T>
0034 class TrackSlotExecutor
0035 {
0036   public:
0037     //!@{
0038     //! \name Type aliases
0039     using ParamsPtr = CoreParamsPtr<MemSpace::native>;
0040     using StatePtr = CoreStatePtr<MemSpace::native>;
0041     using Applier = T;
0042     //!@}
0043 
0044   public:
0045     //! Construct with core data and executor
0046     CELER_FUNCTION TrackSlotExecutor(
0047         ParamsPtr params, StatePtr state, T&& execute_track)
0048         : params_{params}
0049         , state_{state}
0050         , execute_track_{celeritas::forward<T>(execute_track)}
0051     {
0052     }
0053 
0054     //! Call the underlying function based on the index in the state array
0055     CELER_FUNCTION void operator()(TrackSlotId ts)
0056     {
0057         CELER_EXPECT(ts < state_->size());
0058         CoreTrackView track(*params_, *state_, ts);
0059         return execute_track_(track);
0060     }
0061 
0062     //! Call the underlying function using the thread index
0063     CELER_FORCEINLINE_FUNCTION void operator()(ThreadId thread)
0064     {
0065         // For optical photons, thread index maps exactly to
0066         return (*this)(TrackSlotId{thread.unchecked_get()});
0067     }
0068 
0069   private:
0070     ParamsPtr const params_;
0071     StatePtr const state_;
0072     T execute_track_;
0073 };
0074 
0075 //---------------------------------------------------------------------------//
0076 /*!
0077  * Launch the track only when a certain condition applies.
0078  *
0079  * The condition \c C must have the signature \code
0080  * (SimTrackView const&) -> bool
0081   \endcode
0082  *
0083  * see \c make_active_track_executor for an example where this is used to apply
0084  * only to active (or killed) tracks.
0085  */
0086 template<class C, class T>
0087 class ConditionalTrackSlotExecutor
0088 {
0089   public:
0090     //!@{
0091     //! \name Type aliases
0092     using ParamsPtr = CoreParamsPtr<MemSpace::native>;
0093     using StatePtr = CoreStatePtr<MemSpace::native>;
0094     using Applier = T;
0095     //!@}
0096 
0097   public:
0098     //! Construct with condition and operator
0099     CELER_FUNCTION ConditionalTrackSlotExecutor(
0100         ParamsPtr params, StatePtr state, C&& applies, T&& execute_track)
0101         : params_{params}
0102         , state_{state}
0103         , applies_{celeritas::forward<C>(applies)}
0104         , execute_track_{celeritas::forward<T>(execute_track)}
0105     {
0106     }
0107 
0108     //! Launch the given thread if the track meets the condition
0109     CELER_FUNCTION void operator()(TrackSlotId ts)
0110     {
0111         CELER_EXPECT(ts < state_->size());
0112         CoreTrackView track(*params_, *state_, ts);
0113         if (!applies_(track))
0114         {
0115             return;
0116         }
0117 
0118         return execute_track_(track);
0119     }
0120 
0121     //! Call the underlying function using the thread index
0122     CELER_FORCEINLINE_FUNCTION void operator()(ThreadId thread)
0123     {
0124         // For optical photons, thread index maps exactly to
0125         return (*this)(TrackSlotId{thread.unchecked_get()});
0126     }
0127 
0128   private:
0129     ParamsPtr const params_;
0130     StatePtr const state_;
0131     C applies_;
0132     T execute_track_;
0133 };
0134 
0135 //---------------------------------------------------------------------------//
0136 // CONDITIONALS
0137 //---------------------------------------------------------------------------//
0138 /*!
0139  * Apply only to active tracks undergoing volumetric actions (not crossing a
0140  * boundary).
0141  */
0142 struct AppliesValidVolumetric
0143 {
0144     CELER_FUNCTION bool operator()(CoreTrackView const& track) const
0145     {
0146         return AppliesValid{}(track)
0147                && !track.surface_physics().is_crossing_boundary();
0148     }
0149 };
0150 
0151 //---------------------------------------------------------------------------//
0152 /*!
0153  * Whether the track is undergoing a surface crossing and it matches the given
0154  * step and model.
0155  *
0156  * After the interaction step, a track may be on a surface but exiting so it
0157  * does not have a valid next surface model. The surface stepping action is
0158  * instead used, since tracks exiting the surface will have the post-boundary
0159  * action set instead.
0160  *
0161  * For the interaction surface physics step, it should only be executed when
0162  * the surface reflectivity action is set to interact.
0163  */
0164 struct IsSurfaceModelEqual
0165 {
0166     SurfacePhysicsOrder step;
0167     SurfaceModelId model;
0168 
0169     //! Whether the surface model should be executed for the track
0170     CELER_FUNCTION bool operator()(CoreTrackView const& track) const
0171     {
0172         auto s_phys = track.surface_physics();
0173         return IsStepActionEqual{s_phys.scalars().surface_stepping_action}(
0174                    track)
0175                && s_phys.interface(step).surface_model_id() == model
0176                && (step != SurfacePhysicsOrder::interaction
0177                    || s_phys.reflectivity_action()
0178                           == ReflectivityAction::interact);
0179     }
0180 };
0181 
0182 //---------------------------------------------------------------------------//
0183 // DEDUCTION GUIDES
0184 //---------------------------------------------------------------------------//
0185 template<class T>
0186 CELER_CTAD_FUNCTION TrackSlotExecutor(
0187     CoreParamsPtr<MemSpace::native>, CoreStatePtr<MemSpace::native>, T&&)
0188     -> TrackSlotExecutor<T>;
0189 
0190 template<class C, class T>
0191 CELER_CTAD_FUNCTION ConditionalTrackSlotExecutor(
0192     CoreParamsPtr<MemSpace::native>, CoreStatePtr<MemSpace::native>, C&&, T&&)
0193     -> ConditionalTrackSlotExecutor<C, T>;
0194 
0195 //---------------------------------------------------------------------------//
0196 // FREE FUNCTIONS
0197 //---------------------------------------------------------------------------//
0198 /*!
0199  * Return a track executor that only applies to active, non-errored tracks.
0200  */
0201 template<class T>
0202 inline CELER_FUNCTION decltype(auto) make_active_thread_executor(
0203     CoreParamsPtr<MemSpace::native> params,
0204     CoreStatePtr<MemSpace::native> const& state,
0205     T&& apply_track)
0206 {
0207     return ConditionalTrackSlotExecutor{
0208         params, state, AppliesValid{}, celeritas::forward<T>(apply_track)};
0209 }
0210 
0211 //---------------------------------------------------------------------------//
0212 /*!
0213  * Return a track executor that only applies if the action ID matches.
0214  *
0215  * \note This should generally only be used for post-step actions and other
0216  * cases where the IDs \em explicitly are set. Many explicit actions apply to
0217  * all threads, active or not.
0218  */
0219 template<class T>
0220 inline CELER_FUNCTION decltype(auto) make_action_thread_executor(
0221     CoreParamsPtr<MemSpace::native> params,
0222     CoreStatePtr<MemSpace::native> state,
0223     ActionId action,
0224     T&& apply_track)
0225 {
0226     CELER_EXPECT(action);
0227     return ConditionalTrackSlotExecutor{params,
0228                                         state,
0229                                         IsStepActionEqual{action},
0230                                         celeritas::forward<T>(apply_track)};
0231 }
0232 
0233 //---------------------------------------------------------------------------//
0234 /*!
0235  * Return a track executor that only applies to active tracks undergoing
0236  * volumetric actions (not traversing a boundary).
0237  */
0238 template<class T>
0239 inline CELER_FUNCTION decltype(auto) make_active_volumetric_thread_executor(
0240     CoreParamsPtr<MemSpace::native> params,
0241     CoreStatePtr<MemSpace::native> const& state,
0242     T&& apply_track)
0243 {
0244     return ConditionalTrackSlotExecutor{params,
0245                                         state,
0246                                         AppliesValidVolumetric{},
0247                                         celeritas::forward<T>(apply_track)};
0248 }
0249 
0250 //---------------------------------------------------------------------------//
0251 /*!
0252  * Construct a track slot executor for a given surface step and model.
0253  *
0254  * The executor will launch kernels only on tracks which are undergoing a
0255  * boundary crossing.
0256  */
0257 template<class T>
0258 inline CELER_FUNCTION decltype(auto) make_surface_physics_executor(
0259     CoreParamsPtr<MemSpace::native> params,
0260     CoreStatePtr<MemSpace::native> const& state,
0261     SurfacePhysicsOrder step,
0262     SurfaceModelId model,
0263     T&& apply_track)
0264 {
0265     CELER_EXPECT(step != SurfacePhysicsOrder::size_);
0266     CELER_EXPECT(model);
0267     return ConditionalTrackSlotExecutor{params,
0268                                         state,
0269                                         IsSurfaceModelEqual{step, model},
0270                                         celeritas::forward<T>(apply_track)};
0271 }
0272 
0273 //---------------------------------------------------------------------------//
0274 }  // namespace optical
0275 }  // namespace celeritas