Back to home page

EIC code displayed by LXR

 
 

    


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

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