Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:53:44

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/PhysicsTrackView.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include "corecel/Macros.hh"
0011 #include "celeritas/Quantities.hh"
0012 #include "celeritas/Types.hh"
0013 #include "celeritas/grid/NonuniformGridCalculator.hh"
0014 
0015 #include "PhysicsData.hh"
0016 #include "Types.hh"
0017 
0018 namespace celeritas
0019 {
0020 namespace optical
0021 {
0022 //---------------------------------------------------------------------------//
0023 /*!
0024  * Optical physics data for a track.
0025  *
0026  * The physics track data provides an interface for data and operations common
0027  * to most optical models.
0028  */
0029 class PhysicsTrackView
0030 {
0031   public:
0032     //!@{
0033     //! \name Type aliases
0034     using PhysicsParamsRef = NativeCRef<PhysicsParamsData>;
0035     using PhysicsStateRef = NativeRef<PhysicsStateData>;
0036     using Energy = units::MevEnergy;
0037     //!@}
0038 
0039     //! Data for initializing a physics track
0040     struct Initializer
0041     {
0042     };
0043 
0044   public:
0045     // Construct from params, state, and material ID for a given track
0046     inline CELER_FUNCTION PhysicsTrackView(PhysicsParamsRef const&,
0047                                            PhysicsStateRef const&,
0048                                            OptMatId,
0049                                            TrackSlotId);
0050 
0051     // Initialize the physics for the track
0052     inline CELER_FUNCTION PhysicsTrackView& operator=(Initializer const&);
0053 
0054     //// Discrete interaction mean free path ////
0055 
0056     // Reset the currently calculated MFP
0057     inline CELER_FUNCTION void reset_interaction_mfp();
0058 
0059     // Change the interaction MFP
0060     inline CELER_FUNCTION void interaction_mfp(real_type);
0061 
0062     // Get the current MFP
0063     inline CELER_FUNCTION real_type interaction_mfp() const;
0064 
0065     // Whether there's a currently calculated MFP
0066     inline CELER_FUNCTION bool has_interaction_mfp() const;
0067 
0068     //// Step-Local (non-persistent) track data ////
0069 
0070     // Set total cross section of the step
0071     inline CELER_FUNCTION void macro_xs(real_type xs);
0072 
0073     // Retrieve total cross section of the step
0074     inline CELER_FUNCTION real_type macro_xs() const;
0075 
0076     //// Model-Action mappings ////
0077 
0078     // Number of optical models
0079     inline CELER_FUNCTION ModelId::size_type num_models() const;
0080 
0081     // Map a model ID to an action ID
0082     inline CELER_FUNCTION ActionId model_to_action(ModelId) const;
0083 
0084     // Map an action ID to a model ID
0085     inline CELER_FUNCTION ModelId action_to_model(ActionId) const;
0086 
0087     // ID of the discrete action
0088     inline CELER_FUNCTION ActionId discrete_action() const;
0089 
0090     //// Mean free path grids ////
0091 
0092     // Get MFP grid ID for the given model
0093     inline CELER_FUNCTION ValueGridId mfp_grid(ModelId) const;
0094 
0095     // Calculate the MFP for the given model and energy
0096     inline CELER_FUNCTION real_type calc_mfp(ModelId, Energy) const;
0097 
0098   private:
0099     PhysicsParamsRef const& params_;
0100     PhysicsStateRef const& states_;
0101     OptMatId const opt_material_;
0102     TrackSlotId const track_id_;
0103 };
0104 
0105 //---------------------------------------------------------------------------//
0106 // INLINE FUNCTIONS
0107 //---------------------------------------------------------------------------//
0108 /*!
0109  * Construct from params, state, and material ID for a given track.
0110  */
0111 CELER_FUNCTION
0112 PhysicsTrackView::PhysicsTrackView(PhysicsParamsRef const& params,
0113                                    PhysicsStateRef const& states,
0114                                    OptMatId opt_mat,
0115                                    TrackSlotId track_id)
0116     : params_(params)
0117     , states_(states)
0118     , opt_material_(opt_mat)
0119     , track_id_(track_id)
0120 {
0121     CELER_EXPECT(track_id_ < states_.size());
0122     CELER_EXPECT(opt_mat < params_.scalars.num_materials);
0123 }
0124 
0125 //---------------------------------------------------------------------------//
0126 /*!
0127  * Initialize the physics for the given track.
0128  */
0129 CELER_FUNCTION PhysicsTrackView& PhysicsTrackView::operator=(Initializer const&)
0130 {
0131     this->reset_interaction_mfp();
0132     return *this;
0133 }
0134 
0135 //---------------------------------------------------------------------------//
0136 /*!
0137  * Reset the currently calculated interaction MFP.
0138  */
0139 CELER_FUNCTION void PhysicsTrackView::reset_interaction_mfp()
0140 {
0141     states_.interaction_mfp[track_id_] = 0;
0142 }
0143 
0144 //---------------------------------------------------------------------------//
0145 /*!
0146  * Retrieve the interaction mean free path.
0147  */
0148 CELER_FUNCTION void PhysicsTrackView::interaction_mfp(real_type mfp)
0149 {
0150     states_.interaction_mfp[track_id_] = mfp;
0151 }
0152 
0153 //---------------------------------------------------------------------------//
0154 /*!
0155  * Retrieve the interaction mean free path.
0156  */
0157 CELER_FUNCTION real_type PhysicsTrackView::interaction_mfp() const
0158 {
0159     return states_.interaction_mfp[track_id_];
0160 }
0161 
0162 //---------------------------------------------------------------------------//
0163 /*!
0164  * Whether there's a calculated interaction MFP.
0165  */
0166 CELER_FUNCTION bool PhysicsTrackView::has_interaction_mfp() const
0167 {
0168     return states_.interaction_mfp[track_id_] > 0;
0169 }
0170 
0171 //---------------------------------------------------------------------------//
0172 /*!
0173  * Retrieve the number of optical models.
0174  */
0175 CELER_FUNCTION ModelId::size_type PhysicsTrackView::num_models() const
0176 {
0177     return params_.scalars.num_models;
0178 }
0179 
0180 //---------------------------------------------------------------------------//
0181 /*!
0182  * Convert a model ID to an action ID.
0183  */
0184 CELER_FUNCTION ActionId PhysicsTrackView::model_to_action(ModelId mid) const
0185 {
0186     CELER_EXPECT(mid < this->num_models());
0187     return ActionId{mid.get() + params_.scalars.model_to_action};
0188 }
0189 
0190 //---------------------------------------------------------------------------//
0191 /*!
0192  * Convert an action ID to a model ID.
0193  */
0194 CELER_FUNCTION ModelId PhysicsTrackView::action_to_model(ActionId aid) const
0195 {
0196     if (!aid)
0197         return ModelId{};
0198 
0199     // Rely on unsigned rollover if action ID is less than the first model
0200     ModelId::size_type result = aid.unchecked_get()
0201                                 - params_.scalars.model_to_action;
0202 
0203     if (result >= this->num_models())
0204         return ModelId{};
0205 
0206     return ModelId{result};
0207 }
0208 
0209 //---------------------------------------------------------------------------//
0210 /*!
0211  * Get the action ID for the discrete action.
0212  */
0213 CELER_FUNCTION ActionId PhysicsTrackView::discrete_action() const
0214 {
0215     return params_.scalars.discrete_action();
0216 }
0217 
0218 //---------------------------------------------------------------------------//
0219 /*!
0220  * Get the MFP grid ID for the given model.
0221  *
0222  * The grid corresponds to the optical material this track view was
0223  * constructed with.
0224  */
0225 CELER_FUNCTION ValueGridId PhysicsTrackView::mfp_grid(ModelId model) const
0226 {
0227     CELER_EXPECT(model < this->num_models());
0228 
0229     ValueGridId grid_id{opt_material_.get()
0230                         + model.get() * params_.scalars.num_materials};
0231 
0232     CELER_ENSURE(grid_id < params_.grids.size());
0233     return grid_id;
0234 }
0235 
0236 //---------------------------------------------------------------------------//
0237 /*!
0238  * Calculate the MFP for the given model and energy.
0239  *
0240  * Energy is interpolated using \c NonuniformGridCalculator for the model's
0241  * MFP grid.
0242  */
0243 CELER_FUNCTION real_type PhysicsTrackView::calc_mfp(ModelId model,
0244                                                     Energy energy) const
0245 {
0246     NonuniformGridCalculator calc{params_.grids[this->mfp_grid(model)], params_.reals};
0247     real_type result = calc(value_as<Energy>(energy));
0248     CELER_ENSURE(result > 0);
0249     return result;
0250 }
0251 
0252 //---------------------------------------------------------------------------//
0253 /*!
0254  * Retrieve total cross section for this step.
0255  */
0256 CELER_FUNCTION real_type PhysicsTrackView::macro_xs() const
0257 {
0258     return states_.macro_xs[track_id_];
0259 }
0260 
0261 //---------------------------------------------------------------------------//
0262 /*!
0263  * Set total cross section for this step.
0264  */
0265 CELER_FUNCTION void PhysicsTrackView::macro_xs(real_type xs)
0266 {
0267     states_.macro_xs[track_id_] = xs;
0268 }
0269 
0270 //---------------------------------------------------------------------------//
0271 }  // namespace optical
0272 }  // namespace celeritas