Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:09:12

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     // Reset step limiter
0047     inline CELER_FUNCTION void reset_step_limit();
0048 
0049     // Reset step limiter to the given limit
0050     inline CELER_FUNCTION void reset_step_limit(StepLimit const& sl);
0051 
0052     // Limit the step by this distance and action
0053     inline CELER_FUNCTION bool step_limit(StepLimit const& sl);
0054 
0055     // Update limiting step
0056     inline CELER_FUNCTION void step_length(real_type length);
0057 
0058     // Force the limiting action to take
0059     inline CELER_FUNCTION void post_step_action(ActionId action);
0060 
0061     //// DYNAMIC PROPERTIES ////
0062 
0063     // Time elapsed in the lab frame since the start of the event
0064     inline CELER_FUNCTION real_type time() const;
0065 
0066     // Whether the track is alive or inactive or dying
0067     inline CELER_FUNCTION TrackStatus status() const;
0068 
0069     // Limiting step
0070     inline CELER_FUNCTION real_type step_length() const;
0071 
0072     // Access post-step action to take
0073     inline CELER_FUNCTION ActionId post_step_action() const;
0074 
0075   private:
0076     NativeRef<SimStateData> const& states_;
0077     TrackSlotId track_slot_;
0078 };
0079 
0080 //---------------------------------------------------------------------------//
0081 // INLINE DEFINITIONS
0082 //---------------------------------------------------------------------------//
0083 /*!
0084  * Construct from local data.
0085  */
0086 CELER_FUNCTION
0087 SimTrackView::SimTrackView(NativeRef<SimStateData> const& states,
0088                            TrackSlotId tid)
0089     : states_(states), track_slot_(tid)
0090 {
0091     CELER_EXPECT(track_slot_ < states_.size());
0092 }
0093 
0094 //---------------------------------------------------------------------------//
0095 /*!
0096  * Initialize the simulation state.
0097  */
0098 CELER_FUNCTION SimTrackView& SimTrackView::operator=(Initializer const& init)
0099 {
0100     states_.time[track_slot_] = init.time;
0101     states_.step_length[track_slot_] = {};
0102     states_.status[track_slot_] = TrackStatus::initializing;
0103     states_.post_step_action[track_slot_] = {};
0104     return *this;
0105 }
0106 
0107 //---------------------------------------------------------------------------//
0108 /*!
0109  * Set whether the track is active, dying, or inactive.
0110  */
0111 CELER_FUNCTION void SimTrackView::status(TrackStatus status)
0112 {
0113     CELER_EXPECT(status != TrackStatus::size_);
0114     states_.status[track_slot_] = status;
0115 }
0116 
0117 //---------------------------------------------------------------------------//
0118 /*!
0119  * Add the time change over the step.
0120  */
0121 CELER_FUNCTION void SimTrackView::add_time(real_type delta)
0122 {
0123     CELER_EXPECT(delta >= 0);
0124     states_.time[track_slot_] += delta;
0125 }
0126 
0127 //---------------------------------------------------------------------------//
0128 /*!
0129  * Reset step limiter at the beginning of a step.
0130  *
0131  * The action can be unset if and only if the step is infinite.
0132  */
0133 CELER_FUNCTION void SimTrackView::reset_step_limit(StepLimit const& sl)
0134 {
0135     CELER_EXPECT(sl.step >= 0);
0136     CELER_EXPECT(static_cast<bool>(sl.action)
0137                  != (sl.step == numeric_limits<real_type>::infinity()));
0138     states_.step_length[track_slot_] = sl.step;
0139     states_.post_step_action[track_slot_] = sl.action;
0140 }
0141 
0142 //---------------------------------------------------------------------------//
0143 /*!
0144  * Reset step limiter at the beginning of a step.
0145  */
0146 CELER_FUNCTION void SimTrackView::reset_step_limit()
0147 {
0148     StepLimit limit;
0149     limit.step = numeric_limits<real_type>::infinity();
0150     limit.action = {};
0151     this->reset_step_limit(limit);
0152 }
0153 
0154 //---------------------------------------------------------------------------//
0155 /*!
0156  * Force the limiting action to take.
0157  *
0158  * This is used by intermediate kernels (such as \c discrete_select_track )
0159  * that dispatch to another kernel action before the end of the step without
0160  * changing the step itself.
0161  */
0162 CELER_FUNCTION void SimTrackView::post_step_action(ActionId action)
0163 {
0164     CELER_ASSERT(action);
0165     states_.post_step_action[track_slot_] = action;
0166 }
0167 
0168 //---------------------------------------------------------------------------//
0169 /*!
0170  * Update the current limiting step.
0171  */
0172 CELER_FUNCTION void SimTrackView::step_length(real_type length)
0173 {
0174     CELER_EXPECT(length > 0);
0175     states_.step_length[track_slot_] = length;
0176 }
0177 
0178 //---------------------------------------------------------------------------//
0179 /*!
0180  * Limit the step by this distance and action.
0181  *
0182  * If the step limits are the same, the original action is retained.
0183  *
0184  * \return Whether the given limit is the new limit.
0185  */
0186 CELER_FUNCTION bool SimTrackView::step_limit(StepLimit const& sl)
0187 {
0188     CELER_ASSERT(sl.step >= 0);
0189 
0190     bool is_limiting = (sl.step < states_.step_length[track_slot_]);
0191     if (is_limiting)
0192     {
0193         states_.step_length[track_slot_] = sl.step;
0194         states_.post_step_action[track_slot_] = sl.action;
0195     }
0196     return is_limiting;
0197 }
0198 
0199 //---------------------------------------------------------------------------//
0200 // DYNAMIC PROPERTIES
0201 //---------------------------------------------------------------------------//
0202 /*!
0203  * Time elapsed in the lab frame since the start of the event [s].
0204  */
0205 CELER_FORCEINLINE_FUNCTION real_type SimTrackView::time() const
0206 {
0207     return states_.time[track_slot_];
0208 }
0209 
0210 //---------------------------------------------------------------------------//
0211 /*!
0212  * Whether the track is inactive, alive, or being killed.
0213  */
0214 CELER_FORCEINLINE_FUNCTION TrackStatus SimTrackView::status() const
0215 {
0216     return states_.status[track_slot_];
0217 }
0218 
0219 //---------------------------------------------------------------------------//
0220 /*!
0221  * Get the current limiting step.
0222  */
0223 CELER_FORCEINLINE_FUNCTION real_type SimTrackView::step_length() const
0224 {
0225     return states_.step_length[track_slot_];
0226 }
0227 
0228 //---------------------------------------------------------------------------//
0229 /*!
0230  * Access post-step action to take.
0231  */
0232 CELER_FORCEINLINE_FUNCTION ActionId SimTrackView::post_step_action() const
0233 {
0234     return states_.post_step_action[track_slot_];
0235 }
0236 
0237 //---------------------------------------------------------------------------//
0238 }  // namespace optical
0239 }  // namespace celeritas