Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-15 10:10:39

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