Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 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/optical/CoreTrackView.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include "celeritas/geo/GeoTrackView.hh"
0011 #include "celeritas/random/RngEngine.hh"
0012 
0013 #include "CoreTrackData.hh"
0014 #include "MaterialView.hh"
0015 #include "ParticleTrackView.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() const;
0054 
0055     // Return a material view (using an existing geo view
0056     inline CELER_FUNCTION MaterialView material(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 an RNG engine
0065     inline CELER_FUNCTION RngEngine rng() const;
0066 
0067     // Get the track's index among the states
0068     inline CELER_FUNCTION TrackSlotId track_slot_id() const;
0069 
0070     // Action ID for encountering a geometry boundary
0071     inline CELER_FUNCTION ActionId boundary_action() const;
0072 
0073     // Flag a track for deletion
0074     inline CELER_FUNCTION void apply_errored();
0075 
0076   private:
0077     ParamsRef const& params_;
0078     StateRef const& states_;
0079     TrackSlotId const track_slot_id_;
0080 };
0081 
0082 //---------------------------------------------------------------------------//
0083 // INLINE DEFINITIONS
0084 //---------------------------------------------------------------------------//
0085 /*!
0086  * Construct with comprehensive param/state data and track slot.
0087  *
0088  * For optical tracks, the value of the track slot is the same as the track ID.
0089  */
0090 CELER_FUNCTION
0091 CoreTrackView::CoreTrackView(ParamsRef const& params,
0092                              StateRef const& states,
0093                              TrackSlotId track_slot)
0094     : params_(params), states_(states), track_slot_id_(track_slot)
0095 {
0096     CELER_EXPECT(track_slot_id_ < states_.size());
0097 }
0098 
0099 //---------------------------------------------------------------------------//
0100 /*!
0101  * Initialize the track states.
0102  */
0103 CELER_FUNCTION CoreTrackView&
0104 CoreTrackView::operator=(TrackInitializer const& init)
0105 {
0106     // Initialiize the sim state
0107     this->sim() = SimTrackView::Initializer{init.time};
0108 
0109     // Initialize the geometry state
0110     auto geo = this->geometry();
0111     geo = GeoTrackInitializer{init.position, init.direction};
0112     if (CELER_UNLIKELY(geo.failed() || geo.is_outside()))
0113     {
0114 #if !CELER_DEVICE_COMPILE
0115         if (geo.is_outside())
0116         {
0117             // Print an error message if initialization was "successful" but
0118             // track is outside
0119             CELER_LOG_LOCAL(error) << "Track started outside the geometry";
0120         }
0121 #endif
0122         this->apply_errored();
0123         return *this;
0124     }
0125 
0126     // Initialize the particle state
0127     this->particle()
0128         = ParticleTrackView::Initializer{init.energy, init.polarization};
0129 
0130     //! \todo Add physics view and clear physics state
0131 
0132     return *this;
0133 }
0134 
0135 //---------------------------------------------------------------------------//
0136 /*!
0137  * Return a geometry view.
0138  */
0139 CELER_FUNCTION auto CoreTrackView::geometry() const -> GeoTrackView
0140 {
0141     return GeoTrackView{
0142         params_.geometry, states_.geometry, this->track_slot_id()};
0143 }
0144 
0145 //---------------------------------------------------------------------------//
0146 /*!
0147  * Return a material view.
0148  */
0149 CELER_FORCEINLINE_FUNCTION auto CoreTrackView::material() const -> MaterialView
0150 {
0151     return this->material(this->geometry());
0152 }
0153 
0154 //---------------------------------------------------------------------------//
0155 /*!
0156  * Return a material view using an existing geo track view.
0157  */
0158 CELER_FUNCTION auto
0159 CoreTrackView::material(GeoTrackView const& geo) const -> MaterialView
0160 {
0161     CELER_EXPECT(!geo.is_outside());
0162     return MaterialView{params_.material, geo.volume_id()};
0163 }
0164 
0165 //---------------------------------------------------------------------------//
0166 /*!
0167  * Return a particle view.
0168  */
0169 CELER_FUNCTION auto CoreTrackView::particle() const -> ParticleTrackView
0170 {
0171     return ParticleTrackView{states_.particle, this->track_slot_id()};
0172 }
0173 
0174 //---------------------------------------------------------------------------//
0175 /*!
0176  * Return the RNG engine.
0177  */
0178 CELER_FUNCTION auto CoreTrackView::rng() const -> RngEngine
0179 {
0180     return RngEngine{params_.rng, states_.rng, this->track_slot_id()};
0181 }
0182 
0183 //---------------------------------------------------------------------------//
0184 /*!
0185  * Return a simulation management view.
0186  */
0187 CELER_FUNCTION SimTrackView CoreTrackView::sim() const
0188 {
0189     return SimTrackView{states_.sim, this->track_slot_id()};
0190 }
0191 
0192 //---------------------------------------------------------------------------//
0193 /*!
0194  * Get the track's index among the states.
0195  */
0196 CELER_FORCEINLINE_FUNCTION TrackSlotId CoreTrackView::track_slot_id() const
0197 {
0198     return track_slot_id_;
0199 }
0200 
0201 //---------------------------------------------------------------------------//
0202 /*!
0203  * Get the action ID for encountering a geometry boundary.
0204  */
0205 CELER_FUNCTION ActionId CoreTrackView::boundary_action() const
0206 {
0207     return params_.scalars.boundary_action;
0208 }
0209 
0210 //---------------------------------------------------------------------------//
0211 /*!
0212  * Set the 'errored' flag and tracking cut post-step action.
0213  *
0214  * \pre This cannot be applied if the current action is *after* post-step. (You
0215  * can't guarantee for example that sensitive detectors will pick up the energy
0216  * deposition.)
0217  *
0218  * \todo Add a tracking cut action? Currently the track is simply killed.
0219  */
0220 CELER_FUNCTION void CoreTrackView::apply_errored()
0221 {
0222     auto sim = this->sim();
0223     CELER_EXPECT(is_track_valid(sim.status()));
0224     sim.status(TrackStatus::killed);
0225     sim.post_step_action({});
0226 }
0227 
0228 //---------------------------------------------------------------------------//
0229 }  // namespace optical
0230 }  // namespace celeritas