Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:31:23

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