Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-13 08:33:35

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/CoreTrackView.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/random/engine/RngEngine.hh"
0010 #include "celeritas/geo/GeoTrackView.hh"
0011 
0012 #include "CoreTrackData.hh"
0013 #include "MaterialView.hh"
0014 #include "ParticleTrackView.hh"
0015 #include "PhysicsTrackView.hh"
0016 #include "SimTrackView.hh"
0017 #include "TrackInitializer.hh"
0018 #include "surface/VolumeSurfaceSelector.hh"
0019 
0020 #if !CELER_DEVICE_COMPILE
0021 #    include "corecel/io/Logger.hh"
0022 #endif
0023 
0024 namespace celeritas
0025 {
0026 namespace optical
0027 {
0028 //---------------------------------------------------------------------------//
0029 /*!
0030  * Access all core properties of a physics track.
0031  */
0032 class CoreTrackView
0033 {
0034   public:
0035     //!@{
0036     //! \name Type aliases
0037     using ParamsRef = NativeCRef<CoreParamsData>;
0038     using StateRef = NativeRef<CoreStateData>;
0039     //!@}
0040 
0041   public:
0042     // Construct directly from a track slot ID
0043     inline CELER_FUNCTION CoreTrackView(ParamsRef const& params,
0044                                         StateRef const& states,
0045                                         TrackSlotId slot);
0046 
0047     // Initialize the track states
0048     inline CELER_FUNCTION CoreTrackView& operator=(TrackInitializer const&);
0049 
0050     // Return a geometry view
0051     inline CELER_FUNCTION GeoTrackView geometry() const;
0052 
0053     // Return a material view
0054     inline CELER_FUNCTION MaterialView material_record() const;
0055 
0056     // Return a material view (using an existing geo view
0057     inline CELER_FUNCTION MaterialView material_record(GeoTrackView const&) const;
0058 
0059     // Return a simulation management view
0060     inline CELER_FUNCTION SimTrackView sim() const;
0061 
0062     // Return a particle view
0063     inline CELER_FUNCTION ParticleTrackView particle() const;
0064 
0065     // Return a physics view
0066     inline CELER_FUNCTION PhysicsTrackView physics() const;
0067 
0068     // Return a surface selector for the current volume
0069     inline CELER_FUNCTION VolumeSurfaceSelector surface_selector() const;
0070 
0071     // Return an RNG engine
0072     inline CELER_FUNCTION RngEngine rng() const;
0073 
0074     // Get the track's index among the states
0075     inline CELER_FUNCTION TrackSlotId track_slot_id() const;
0076 
0077     // Action ID for encountering a geometry boundary
0078     inline CELER_FUNCTION ActionId boundary_action() const;
0079 
0080     // Flag a track for deletion
0081     inline CELER_FUNCTION void apply_errored();
0082 
0083   private:
0084     ParamsRef const& params_;
0085     StateRef const& states_;
0086     TrackSlotId const track_slot_id_;
0087 };
0088 
0089 //---------------------------------------------------------------------------//
0090 // INLINE DEFINITIONS
0091 //---------------------------------------------------------------------------//
0092 /*!
0093  * Construct with comprehensive param/state data and track slot.
0094  *
0095  * For optical tracks, the value of the track slot is the same as the track ID.
0096  */
0097 CELER_FUNCTION
0098 CoreTrackView::CoreTrackView(ParamsRef const& params,
0099                              StateRef const& states,
0100                              TrackSlotId track_slot)
0101     : params_(params), states_(states), track_slot_id_(track_slot)
0102 {
0103     CELER_EXPECT(track_slot_id_ < states_.size());
0104 }
0105 
0106 //---------------------------------------------------------------------------//
0107 /*!
0108  * Initialize the track states.
0109  */
0110 CELER_FUNCTION CoreTrackView&
0111 CoreTrackView::operator=(TrackInitializer const& init)
0112 {
0113     // Initialiize the sim state
0114     this->sim() = SimTrackView::Initializer{init.time};
0115 
0116     // Initialize the geometry state
0117     auto geo = this->geometry();
0118     geo = GeoTrackInitializer{init.position, init.direction, {}};
0119     if (CELER_UNLIKELY(geo.failed() || geo.is_outside()))
0120     {
0121 #if !CELER_DEVICE_COMPILE
0122         if (geo.is_outside())
0123         {
0124             // Print an error message if initialization was "successful" but
0125             // track is outside
0126             CELER_LOG_LOCAL(error) << "Track started outside the geometry";
0127         }
0128 #endif
0129         this->apply_errored();
0130         return *this;
0131     }
0132 
0133     // Initialize the particle state
0134     this->particle()
0135         = ParticleTrackView::Initializer{init.energy, init.polarization};
0136 
0137     // Initialize the physics state
0138     this->physics() = PhysicsTrackView::Initializer{};
0139 
0140     return *this;
0141 }
0142 
0143 //---------------------------------------------------------------------------//
0144 /*!
0145  * Return a geometry view.
0146  */
0147 CELER_FUNCTION auto CoreTrackView::geometry() const -> GeoTrackView
0148 {
0149     return GeoTrackView{
0150         params_.geometry, states_.geometry, this->track_slot_id()};
0151 }
0152 
0153 //---------------------------------------------------------------------------//
0154 /*!
0155  * Return a material view.
0156  */
0157 CELER_FORCEINLINE_FUNCTION auto CoreTrackView::material_record() const
0158     -> MaterialView
0159 {
0160     return this->material_record(this->geometry());
0161 }
0162 
0163 //---------------------------------------------------------------------------//
0164 /*!
0165  * Return a material view using an existing geo track view.
0166  */
0167 CELER_FUNCTION auto
0168 CoreTrackView::material_record(GeoTrackView const& geo) const -> MaterialView
0169 {
0170     CELER_EXPECT(!geo.is_outside());
0171     return MaterialView{params_.material, geo.volume_id()};
0172 }
0173 
0174 //---------------------------------------------------------------------------//
0175 /*!
0176  * Return a particle view.
0177  */
0178 CELER_FUNCTION auto CoreTrackView::particle() const -> ParticleTrackView
0179 {
0180     return ParticleTrackView{states_.particle, this->track_slot_id()};
0181 }
0182 
0183 //---------------------------------------------------------------------------//
0184 /*!
0185  * Return a physics view.
0186  */
0187 CELER_FUNCTION auto CoreTrackView::physics() const -> PhysicsTrackView
0188 {
0189     OptMatId mat_id = this->material_record().material_id();
0190     CELER_ASSERT(mat_id);
0191     return PhysicsTrackView{
0192         params_.physics, states_.physics, mat_id, this->track_slot_id()};
0193 }
0194 
0195 //---------------------------------------------------------------------------//
0196 /*!
0197  * Return a surface selector for the current volume.
0198  */
0199 CELER_FUNCTION auto CoreTrackView::surface_selector() const
0200     -> VolumeSurfaceSelector
0201 {
0202     return VolumeSurfaceSelector{params_.surface, this->geometry()};
0203 }
0204 
0205 //---------------------------------------------------------------------------//
0206 /*!
0207  * Return the RNG engine.
0208  */
0209 CELER_FUNCTION auto CoreTrackView::rng() const -> RngEngine
0210 {
0211     return RngEngine{params_.rng, states_.rng, this->track_slot_id()};
0212 }
0213 
0214 //---------------------------------------------------------------------------//
0215 /*!
0216  * Return a simulation management view.
0217  */
0218 CELER_FUNCTION SimTrackView CoreTrackView::sim() const
0219 {
0220     return SimTrackView{states_.sim, this->track_slot_id()};
0221 }
0222 
0223 //---------------------------------------------------------------------------//
0224 /*!
0225  * Get the track's index among the states.
0226  */
0227 CELER_FORCEINLINE_FUNCTION TrackSlotId CoreTrackView::track_slot_id() const
0228 {
0229     return track_slot_id_;
0230 }
0231 
0232 //---------------------------------------------------------------------------//
0233 /*!
0234  * Get the action ID for encountering a geometry boundary.
0235  */
0236 CELER_FUNCTION ActionId CoreTrackView::boundary_action() const
0237 {
0238     return params_.scalars.boundary_action;
0239 }
0240 
0241 //---------------------------------------------------------------------------//
0242 /*!
0243  * Set the 'errored' flag and tracking cut post-step action.
0244  *
0245  * \pre This cannot be applied if the current action is *after* post-step. (You
0246  * can't guarantee for example that sensitive detectors will pick up the energy
0247  * deposition.)
0248  *
0249  * \todo Add a tracking cut action? Currently the track is simply killed.
0250  */
0251 CELER_FUNCTION void CoreTrackView::apply_errored()
0252 {
0253     auto sim = this->sim();
0254     CELER_EXPECT(is_track_valid(sim.status()));
0255     sim.status(TrackStatus::errored);
0256     sim.post_step_action(params_.scalars.tracking_cut_action);
0257 }
0258 
0259 //---------------------------------------------------------------------------//
0260 }  // namespace optical
0261 }  // namespace celeritas