Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-19 08:08:15

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/SimTrackView.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Macros.hh"
0010 #include "corecel/Types.hh"
0011 #include "celeritas/Types.hh"
0012 
0013 #include "SimData.hh"
0014 
0015 namespace celeritas
0016 {
0017 namespace optical
0018 {
0019 //---------------------------------------------------------------------------//
0020 /*!
0021  * Simulation properties for a single track.
0022  */
0023 class SimTrackView
0024 {
0025   public:
0026     //! Data for initializing the simulation state
0027     struct Initializer
0028     {
0029         real_type time{};
0030     };
0031 
0032   public:
0033     // Construct from local data
0034     inline CELER_FUNCTION
0035     SimTrackView(NativeRef<SimStateData> const&, TrackSlotId);
0036 
0037     // Initialize the sim state
0038     inline CELER_FUNCTION SimTrackView& operator=(Initializer const&);
0039 
0040     // Set whether the track is alive
0041     inline CELER_FUNCTION void status(TrackStatus);
0042 
0043     // Add the time change over the step
0044     inline CELER_FUNCTION void add_time(real_type delta);
0045 
0046     // Increment the total number of steps
0047     inline CELER_FUNCTION void increment_num_steps();
0048 
0049     // Reset step limiter
0050     inline CELER_FUNCTION void reset_step_limit();
0051 
0052     // Reset step limiter to the given limit
0053     inline CELER_FUNCTION void reset_step_limit(StepLimit const& sl);
0054 
0055     // Limit the step by this distance and action
0056     inline CELER_FUNCTION bool step_limit(StepLimit const& sl);
0057 
0058     // Update limiting step
0059     inline CELER_FUNCTION void step_length(real_type length);
0060 
0061     // Force the limiting action to take
0062     inline CELER_FUNCTION void post_step_action(ActionId action);
0063 
0064     //// DYNAMIC PROPERTIES ////
0065 
0066     // Total number of steps taken by the track
0067     inline CELER_FUNCTION size_type num_steps() const;
0068 
0069     // Time elapsed in the lab frame since the start of the event
0070     inline CELER_FUNCTION real_type time() const;
0071 
0072     // Whether the track is alive or inactive or dying
0073     inline CELER_FUNCTION TrackStatus status() const;
0074 
0075     // Limiting step
0076     inline CELER_FUNCTION real_type step_length() const;
0077 
0078     // Access post-step action to take
0079     inline CELER_FUNCTION ActionId post_step_action() const;
0080 
0081   private:
0082     NativeRef<SimStateData> const& states_;
0083     TrackSlotId track_slot_;
0084 };
0085 
0086 //---------------------------------------------------------------------------//
0087 // INLINE DEFINITIONS
0088 //---------------------------------------------------------------------------//
0089 /*!
0090  * Construct from local data.
0091  */
0092 CELER_FUNCTION
0093 SimTrackView::SimTrackView(NativeRef<SimStateData> const& states,
0094                            TrackSlotId tid)
0095     : states_(states), track_slot_(tid)
0096 {
0097     CELER_EXPECT(track_slot_ < states_.size());
0098 }
0099 
0100 //---------------------------------------------------------------------------//
0101 /*!
0102  * Initialize the simulation state.
0103  */
0104 CELER_FUNCTION SimTrackView& SimTrackView::operator=(Initializer const& init)
0105 {
0106     states_.time[track_slot_] = init.time;
0107     states_.step_length[track_slot_] = {};
0108     states_.status[track_slot_] = TrackStatus::initializing;
0109     states_.post_step_action[track_slot_] = {};
0110     states_.num_steps[track_slot_] = 0;
0111     return *this;
0112 }
0113 
0114 //---------------------------------------------------------------------------//
0115 /*!
0116  * Set whether the track is active, dying, or inactive.
0117  */
0118 CELER_FUNCTION void SimTrackView::status(TrackStatus status)
0119 {
0120     CELER_EXPECT(status != TrackStatus::size_);
0121     states_.status[track_slot_] = status;
0122 }
0123 
0124 //---------------------------------------------------------------------------//
0125 /*!
0126  * Increment the total number of steps.
0127  */
0128 CELER_FORCEINLINE_FUNCTION void SimTrackView::increment_num_steps()
0129 {
0130     ++states_.num_steps[track_slot_];
0131 }
0132 
0133 //---------------------------------------------------------------------------//
0134 /*!
0135  * Add the time change over the step.
0136  */
0137 CELER_FUNCTION void SimTrackView::add_time(real_type delta)
0138 {
0139     CELER_EXPECT(delta >= 0);
0140     states_.time[track_slot_] += delta;
0141 }
0142 
0143 //---------------------------------------------------------------------------//
0144 /*!
0145  * Reset step limiter at the beginning of a step.
0146  *
0147  * The action can be unset if and only if the step is infinite.
0148  */
0149 CELER_FUNCTION void SimTrackView::reset_step_limit(StepLimit const& sl)
0150 {
0151     CELER_EXPECT(sl.step >= 0);
0152     CELER_EXPECT(static_cast<bool>(sl.action)
0153                  != (sl.step == numeric_limits<real_type>::infinity()));
0154     states_.step_length[track_slot_] = sl.step;
0155     states_.post_step_action[track_slot_] = sl.action;
0156 }
0157 
0158 //---------------------------------------------------------------------------//
0159 /*!
0160  * Reset step limiter at the beginning of a step.
0161  */
0162 CELER_FUNCTION void SimTrackView::reset_step_limit()
0163 {
0164     StepLimit limit;
0165     limit.step = numeric_limits<real_type>::infinity();
0166     limit.action = {};
0167     this->reset_step_limit(limit);
0168 }
0169 
0170 //---------------------------------------------------------------------------//
0171 /*!
0172  * Force the limiting action to take.
0173  *
0174  * This is used by intermediate kernels (such as \c discrete_select_track )
0175  * that dispatch to another kernel action before the end of the step without
0176  * changing the step itself.
0177  */
0178 CELER_FUNCTION void SimTrackView::post_step_action(ActionId action)
0179 {
0180     CELER_ASSERT(action);
0181     states_.post_step_action[track_slot_] = action;
0182 }
0183 
0184 //---------------------------------------------------------------------------//
0185 /*!
0186  * Update the current limiting step.
0187  */
0188 CELER_FUNCTION void SimTrackView::step_length(real_type length)
0189 {
0190     CELER_EXPECT(length > 0);
0191     states_.step_length[track_slot_] = length;
0192 }
0193 
0194 //---------------------------------------------------------------------------//
0195 /*!
0196  * Limit the step by this distance and action.
0197  *
0198  * If the step limits are the same, the original action is retained.
0199  *
0200  * \return Whether the given limit is the new limit.
0201  */
0202 CELER_FUNCTION bool SimTrackView::step_limit(StepLimit const& sl)
0203 {
0204     CELER_ASSERT(sl.step >= 0);
0205 
0206     bool is_limiting = (sl.step < states_.step_length[track_slot_]);
0207     if (is_limiting)
0208     {
0209         states_.step_length[track_slot_] = sl.step;
0210         states_.post_step_action[track_slot_] = sl.action;
0211     }
0212     return is_limiting;
0213 }
0214 
0215 //---------------------------------------------------------------------------//
0216 // DYNAMIC PROPERTIES
0217 //---------------------------------------------------------------------------//
0218 /*!
0219  * Total number of steps taken by the track.
0220  */
0221 CELER_FORCEINLINE_FUNCTION size_type SimTrackView::num_steps() const
0222 {
0223     return states_.num_steps[track_slot_];
0224 }
0225 
0226 //---------------------------------------------------------------------------//
0227 /*!
0228  * Time elapsed in the lab frame since the start of the event [s].
0229  */
0230 CELER_FORCEINLINE_FUNCTION real_type SimTrackView::time() const
0231 {
0232     return states_.time[track_slot_];
0233 }
0234 
0235 //---------------------------------------------------------------------------//
0236 /*!
0237  * Whether the track is inactive, alive, or being killed.
0238  */
0239 CELER_FORCEINLINE_FUNCTION TrackStatus SimTrackView::status() const
0240 {
0241     return states_.status[track_slot_];
0242 }
0243 
0244 //---------------------------------------------------------------------------//
0245 /*!
0246  * Get the current limiting step.
0247  */
0248 CELER_FORCEINLINE_FUNCTION real_type SimTrackView::step_length() const
0249 {
0250     return states_.step_length[track_slot_];
0251 }
0252 
0253 //---------------------------------------------------------------------------//
0254 /*!
0255  * Access post-step action to take.
0256  */
0257 CELER_FORCEINLINE_FUNCTION ActionId SimTrackView::post_step_action() const
0258 {
0259     return states_.post_step_action[track_slot_];
0260 }
0261 
0262 //---------------------------------------------------------------------------//
0263 }  // namespace optical
0264 }  // namespace celeritas