Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-31 08:58:48

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