Back to home page

EIC code displayed by LXR

 
 

    


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

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