Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:52:23

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/mat/MaterialTrackView.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <cmath>
0010 
0011 #include "corecel/Assert.hh"
0012 #include "corecel/Macros.hh"
0013 #include "corecel/Types.hh"
0014 #include "corecel/sys/ThreadId.hh"
0015 #include "celeritas/Quantities.hh"
0016 #include "celeritas/Types.hh"
0017 
0018 #include "MaterialData.hh"
0019 #include "MaterialView.hh"
0020 
0021 namespace celeritas
0022 {
0023 //---------------------------------------------------------------------------//
0024 /*!
0025  * Read/write view to the material properties of a single particle track.
0026  *
0027  * These functions should be used in each physics Process or Interactor or
0028  * anything else that needs to access particle properties. Assume that all
0029  * these functions are expensive: when using them as accessors, locally store
0030  * the results rather than calling the function repeatedly. If any of the
0031  * calculations prove to be hot spots we will experiment with cacheing some of
0032  * the variables.
0033  *
0034  * The element scratch space is "thread-private" data with a fixed size
0035  * *greater than or equal to* the number of elemental components in the current
0036  * material.
0037  */
0038 class MaterialTrackView
0039 {
0040   public:
0041     //!@{
0042     //! \name Type aliases
0043     using Initializer_t = MaterialTrackState;
0044     using MaterialParamsRef = NativeCRef<MaterialParamsData>;
0045     using MaterialStateRef = NativeRef<MaterialStateData>;
0046     //!@}
0047 
0048   public:
0049     // Construct from "static" parameters and "dynamic" state
0050     inline CELER_FUNCTION MaterialTrackView(MaterialParamsRef const& params,
0051                                             MaterialStateRef const& states,
0052                                             TrackSlotId tid);
0053 
0054     // Initialize the particle
0055     inline CELER_FUNCTION MaterialTrackView&
0056     operator=(Initializer_t const& other);
0057 
0058     //// DYNAMIC PROPERTIES (pure accessors, free) ////
0059 
0060     // Current material identifier
0061     CELER_FORCEINLINE_FUNCTION PhysMatId material_id() const;
0062 
0063     //// STATIC PROPERTIES ////
0064 
0065     // Get a view to material properties
0066     CELER_FORCEINLINE_FUNCTION MaterialView material_record() const;
0067 
0068     // Access scratch space with at least one real per element component
0069     inline CELER_FUNCTION Span<real_type> element_scratch();
0070 
0071   private:
0072     MaterialParamsRef const& params_;
0073     MaterialStateRef const& states_;
0074     TrackSlotId const track_slot_;
0075 
0076     CELER_FORCEINLINE_FUNCTION MaterialTrackState& state() const;
0077 };
0078 
0079 //---------------------------------------------------------------------------//
0080 // INLINE DEFINITIONS
0081 //---------------------------------------------------------------------------//
0082 /*!
0083  * Construct from dynamic and static particle properties.
0084  */
0085 CELER_FUNCTION
0086 MaterialTrackView::MaterialTrackView(MaterialParamsRef const& params,
0087                                      MaterialStateRef const& states,
0088                                      TrackSlotId tid)
0089     : params_(params), states_(states), track_slot_(tid)
0090 {
0091     CELER_EXPECT(tid < states.state.size());
0092 }
0093 
0094 //---------------------------------------------------------------------------//
0095 /*!
0096  * Initialize the particle.
0097  */
0098 CELER_FUNCTION MaterialTrackView&
0099 MaterialTrackView::operator=(Initializer_t const& other)
0100 {
0101     CELER_EXPECT(other.material_id < params_.materials.size());
0102     this->state() = other;
0103     return *this;
0104 }
0105 
0106 //---------------------------------------------------------------------------//
0107 /*!
0108  * Current material identifier.
0109  */
0110 CELER_FUNCTION PhysMatId MaterialTrackView::material_id() const
0111 {
0112     return this->state().material_id;
0113 }
0114 
0115 //---------------------------------------------------------------------------//
0116 /*!
0117  * Get material properties for the current material.
0118  */
0119 CELER_FUNCTION MaterialView MaterialTrackView::material_record() const
0120 {
0121     return MaterialView(params_, this->material_id());
0122 }
0123 
0124 //---------------------------------------------------------------------------//
0125 /*!
0126  * Access scratch space with at least one real per element component.
0127  */
0128 CELER_FUNCTION Span<real_type> MaterialTrackView::element_scratch()
0129 {
0130     auto offset = track_slot_.get() * params_.max_element_components;
0131     Span<real_type> all_scratch
0132         = states_.element_scratch[AllItems<real_type, MemSpace::native>{}];
0133     CELER_ENSURE(offset + params_.max_element_components <= all_scratch.size());
0134     return all_scratch.subspan(offset, params_.max_element_components);
0135 }
0136 
0137 //---------------------------------------------------------------------------//
0138 /*!
0139  * Access the thread-local state.
0140  */
0141 CELER_FUNCTION MaterialTrackState& MaterialTrackView::state() const
0142 {
0143     return states_.state[track_slot_];
0144 }
0145 
0146 //---------------------------------------------------------------------------//
0147 }  // namespace celeritas