Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:54:43

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/global/CoreTrackView.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/random/engine/RngEngine.hh"
0010 #include "corecel/sys/ThreadId.hh"
0011 #include "celeritas/geo/GeoMaterialView.hh"
0012 #include "celeritas/geo/GeoTrackView.hh"
0013 #include "celeritas/mat/MaterialTrackView.hh"
0014 #include "celeritas/phys/CutoffView.hh"
0015 #include "celeritas/phys/ParticleTrackView.hh"
0016 #include "celeritas/phys/PhysicsStepView.hh"
0017 #include "celeritas/phys/PhysicsTrackView.hh"
0018 #include "celeritas/track/SimTrackView.hh"
0019 
0020 #include "CoreTrackData.hh"
0021 
0022 namespace celeritas
0023 {
0024 //---------------------------------------------------------------------------//
0025 /*!
0026  * Helper class to create views from core track data.
0027  *
0028  * TODO: const correctness? (Maybe have to wait till C++23's "deducing this"?)
0029  */
0030 class CoreTrackView
0031 {
0032   public:
0033     //!@{
0034     //! \name Type aliases
0035     using ParamsRef = NativeCRef<CoreParamsData>;
0036     using StateRef = NativeRef<CoreStateData>;
0037     //!@}
0038 
0039   public:
0040     // Construct with comprehensive param/state data and thread
0041     inline CELER_FUNCTION CoreTrackView(ParamsRef const& params,
0042                                         StateRef const& states,
0043                                         ThreadId thread);
0044 
0045     // Construct directly from a track slot ID
0046     inline CELER_FUNCTION CoreTrackView(ParamsRef const& params,
0047                                         StateRef const& states,
0048                                         TrackSlotId slot);
0049 
0050     // Return a simulation management view
0051     inline CELER_FUNCTION SimTrackView sim() const;
0052 
0053     // Return a geometry view
0054     inline CELER_FUNCTION GeoTrackView geometry() const;
0055 
0056     // Return a geometry-material view
0057     inline CELER_FUNCTION GeoMaterialView geo_material() const;
0058 
0059     // Return a material view
0060     inline CELER_FUNCTION MaterialTrackView material() const;
0061 
0062     // Return a particle view
0063     inline CELER_FUNCTION ParticleTrackView particle() const;
0064 
0065     // Return a particle view of another particle type
0066     inline CELER_FUNCTION ParticleView particle_record(ParticleId) const;
0067 
0068     // Return a cutoff view
0069     inline CELER_FUNCTION CutoffView cutoff() const;
0070 
0071     // Return a physics view
0072     inline CELER_FUNCTION PhysicsTrackView physics() const;
0073 
0074     // Return a view to temporary physics data
0075     inline CELER_FUNCTION PhysicsStepView physics_step() const;
0076 
0077     // Return an RNG engine
0078     inline CELER_FUNCTION RngEngine rng() const;
0079 
0080     // Get the index of the current thread in the current kernel
0081     inline CELER_FUNCTION ThreadId thread_id() const;
0082 
0083     // Get the track's index among the states
0084     inline CELER_FUNCTION TrackSlotId track_slot_id() const;
0085 
0086     // Action ID for encountering a geometry boundary
0087     inline CELER_FUNCTION ActionId boundary_action() const;
0088 
0089     // Action ID for some other propagation limit (e.g. field stepping)
0090     inline CELER_FUNCTION ActionId propagation_limit_action() const;
0091 
0092     // Action ID for being abandoned while looping
0093     inline CELER_FUNCTION ActionId tracking_cut_action() const;
0094 
0095     // HACK: return scalars (maybe have a struct for all actions?)
0096     inline CELER_FUNCTION CoreScalars const& core_scalars() const;
0097 
0098     // clang-format off
0099     // DEPRECATED: remove in v0.7
0100     [[deprecated]] CELER_FUNCTION SimTrackView make_sim_view() const { return this->sim(); }
0101     [[deprecated]] CELER_FUNCTION GeoTrackView make_geo_view() const { return this->geometry(); }
0102     [[deprecated]] CELER_FUNCTION GeoMaterialView make_geo_material_view() const { return this->geo_material(); }
0103     [[deprecated]] CELER_FUNCTION MaterialTrackView make_material_view() const { return this->material(); }
0104     [[deprecated]] CELER_FUNCTION ParticleTrackView make_particle_view() const { return this->particle(); }
0105     [[deprecated]] CELER_FUNCTION ParticleView make_particle_view(ParticleId pid) const { return this->particle_record(pid); }
0106     [[deprecated]] CELER_FUNCTION CutoffView make_cutoff_view() const { return this->cutoff(); }
0107     [[deprecated]] CELER_FUNCTION PhysicsTrackView make_physics_view() const { return this->physics(); }
0108     [[deprecated]] CELER_FUNCTION PhysicsStepView make_physics_step_view() const { return this->physics_step(); }
0109     [[deprecated]] CELER_FUNCTION RngEngine make_rng_engine() const { return this->rng(); }
0110     // clang-format on
0111 
0112     //// MUTATORS ////
0113 
0114     // Set the 'errored' flag and tracking cut post-step action
0115     inline CELER_FUNCTION void apply_errored();
0116 
0117   private:
0118     StateRef const& states_;
0119     ParamsRef const& params_;
0120     ThreadId const thread_id_;
0121     TrackSlotId track_slot_id_;
0122 };
0123 
0124 //---------------------------------------------------------------------------//
0125 // INLINE DEFINITIONS
0126 //---------------------------------------------------------------------------//
0127 /*!
0128  * Construct with comprehensive param/state data and thread.
0129  */
0130 CELER_FUNCTION
0131 CoreTrackView::CoreTrackView(ParamsRef const& params,
0132                              StateRef const& states,
0133                              ThreadId thread)
0134     : states_(states), params_(params), thread_id_(thread)
0135 {
0136     CELER_EXPECT(states_.track_slots.empty()
0137                  || thread_id_ < states_.track_slots.size());
0138     track_slot_id_ = TrackSlotId{states_.track_slots.empty()
0139                                      ? thread_id_.get()
0140                                      : states_.track_slots[thread_id_]};
0141     CELER_ENSURE(track_slot_id_ < states_.size());
0142 }
0143 
0144 //---------------------------------------------------------------------------//
0145 /*!
0146  * Construct with comprehensive param/state data and track slot.
0147  *
0148  * This signature is used for creating a view of a \em second track in a kernel
0149  * for initialization.
0150  */
0151 CELER_FUNCTION
0152 CoreTrackView::CoreTrackView(ParamsRef const& params,
0153                              StateRef const& states,
0154                              TrackSlotId track_slot)
0155     : states_(states), params_(params), track_slot_id_(track_slot)
0156 {
0157     CELER_EXPECT(track_slot_id_ < states_.size());
0158 }
0159 
0160 //---------------------------------------------------------------------------//
0161 /*!
0162  * Return a simulation management view.
0163  */
0164 CELER_FUNCTION SimTrackView CoreTrackView::sim() const
0165 {
0166     return SimTrackView{params_.sim, states_.sim, this->track_slot_id()};
0167 }
0168 
0169 //---------------------------------------------------------------------------//
0170 /*!
0171  * Return a geometry view.
0172  */
0173 CELER_FUNCTION auto CoreTrackView::geometry() const -> GeoTrackView
0174 {
0175     return GeoTrackView{
0176         params_.geometry, states_.geometry, this->track_slot_id()};
0177 }
0178 
0179 //---------------------------------------------------------------------------//
0180 /*!
0181  * Return a geometry-material view.
0182  */
0183 CELER_FUNCTION auto CoreTrackView::geo_material() const -> GeoMaterialView
0184 {
0185     return GeoMaterialView{params_.geo_mats};
0186 }
0187 
0188 //---------------------------------------------------------------------------//
0189 /*!
0190  * Return a material view.
0191  */
0192 CELER_FUNCTION auto CoreTrackView::material() const -> MaterialTrackView
0193 {
0194     return MaterialTrackView{
0195         params_.materials, states_.materials, this->track_slot_id()};
0196 }
0197 
0198 //---------------------------------------------------------------------------//
0199 /*!
0200  * Return a particle view.
0201  */
0202 CELER_FUNCTION auto CoreTrackView::particle() const -> ParticleTrackView
0203 {
0204     return ParticleTrackView{
0205         params_.particles, states_.particles, this->track_slot_id()};
0206 }
0207 
0208 //---------------------------------------------------------------------------//
0209 /*!
0210  * Return a particle view of another particle type.
0211  */
0212 CELER_FUNCTION auto
0213 CoreTrackView::particle_record(ParticleId pid) const -> ParticleView
0214 {
0215     return ParticleView{params_.particles, pid};
0216 }
0217 
0218 //---------------------------------------------------------------------------//
0219 /*!
0220  * Return a cutoff view.
0221  */
0222 CELER_FUNCTION auto CoreTrackView::cutoff() const -> CutoffView
0223 {
0224     PhysMatId mat_id = this->material().material_id();
0225     CELER_ASSERT(mat_id);
0226     return CutoffView{params_.cutoffs, mat_id};
0227 }
0228 
0229 //---------------------------------------------------------------------------//
0230 /*!
0231  * Return a physics view.
0232  */
0233 CELER_FUNCTION auto CoreTrackView::physics() const -> PhysicsTrackView
0234 {
0235     PhysMatId mat_id = this->material().material_id();
0236     CELER_ASSERT(mat_id);
0237     auto par = this->particle();
0238     return PhysicsTrackView{
0239         params_.physics, states_.physics, par, mat_id, this->track_slot_id()};
0240 }
0241 
0242 //---------------------------------------------------------------------------//
0243 /*!
0244  * Return a physics view.
0245  */
0246 CELER_FUNCTION auto CoreTrackView::physics_step() const -> PhysicsStepView
0247 {
0248     return PhysicsStepView{
0249         params_.physics, states_.physics, this->track_slot_id()};
0250 }
0251 
0252 //---------------------------------------------------------------------------//
0253 /*!
0254  * Return the RNG engine.
0255  */
0256 CELER_FUNCTION auto CoreTrackView::rng() const -> RngEngine
0257 {
0258     return RngEngine{params_.rng, states_.rng, this->track_slot_id()};
0259 }
0260 
0261 //---------------------------------------------------------------------------//
0262 /*!
0263  * Get the index of the current thread in the current kernel.
0264  *
0265  * \warning If the kernel calling this function is not applied to \em all
0266  * tracks, then comparing against a particular thread ID (e.g. zero for a
0267  * once-per-kernel initialization) may result in an error.
0268  *
0269  * \pre The thread ID is only set if the class is initialized with the thread
0270  * ID (e.g. from \c TrackExecutor ), which is not the case in track
0271  * initialization (where the "core track" is constructed from a vacancy).
0272  */
0273 CELER_FORCEINLINE_FUNCTION ThreadId CoreTrackView::thread_id() const
0274 {
0275     CELER_EXPECT(thread_id_);
0276     return thread_id_;
0277 }
0278 
0279 //---------------------------------------------------------------------------//
0280 /*!
0281  * Get the track's index among the states.
0282  */
0283 CELER_FORCEINLINE_FUNCTION TrackSlotId CoreTrackView::track_slot_id() const
0284 {
0285     return track_slot_id_;
0286 }
0287 
0288 //---------------------------------------------------------------------------//
0289 /*!
0290  * Get the action ID for encountering a geometry boundary.
0291  */
0292 CELER_FUNCTION ActionId CoreTrackView::boundary_action() const
0293 {
0294     return params_.scalars.boundary_action;
0295 }
0296 
0297 //---------------------------------------------------------------------------//
0298 /*!
0299  * Get the action ID for having to pause the step during propagation.
0300  *
0301  * This could be from an internal limiter (number of substeps during field
0302  * propagation) or from having to "bump" the track position for some reason
0303  * (geometry issue). The volume *must not* change as a result of the
0304  * propagation, and this should be an extremely rare case.
0305  */
0306 CELER_FUNCTION ActionId CoreTrackView::propagation_limit_action() const
0307 {
0308     return params_.scalars.propagation_limit_action;
0309 }
0310 
0311 //---------------------------------------------------------------------------//
0312 /*!
0313  * Get the action ID for killing a track prematurely.
0314  *
0315  * This \em unphysical local energy deposition can happen due to:
0316  * - Initialization in an invalid region
0317  * - Looping in a magnetic field
0318  * - A tracking error due to an invalid user geometry or a bug
0319  * - User tracking cuts
0320  */
0321 CELER_FUNCTION ActionId CoreTrackView::tracking_cut_action() const
0322 {
0323     return params_.scalars.tracking_cut_action;
0324 }
0325 
0326 //---------------------------------------------------------------------------//
0327 /*!
0328  * Get access to all the core scalars.
0329  *
0330  * TODO: maybe have a struct for all actions to simplify the class? (Action
0331  * view?)
0332  */
0333 CELER_FUNCTION CoreScalars const& CoreTrackView::core_scalars() const
0334 {
0335     return params_.scalars;
0336 }
0337 
0338 //---------------------------------------------------------------------------//
0339 /*!
0340  * Set the 'errored' flag and tracking cut post-step action.
0341  *
0342  * \pre This cannot be applied if the current action is *after* post-step. (You
0343  * can't guarantee for example that sensitive detectors will pick up the energy
0344  * deposition.)
0345  */
0346 CELER_FUNCTION void CoreTrackView::apply_errored()
0347 {
0348     auto sim = this->sim();
0349     CELER_EXPECT(is_track_valid(sim.status()));
0350     sim.status(TrackStatus::errored);
0351     sim.along_step_action({});
0352     sim.post_step_action(this->tracking_cut_action());
0353 }
0354 
0355 //---------------------------------------------------------------------------//
0356 }  // namespace celeritas