Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-12 08:36:32

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/phys/PhysicsParams.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 #include <utility>
0011 #include <vector>
0012 
0013 #include "corecel/Assert.hh"
0014 #include "corecel/Types.hh"
0015 #include "corecel/cont/Range.hh"
0016 #include "corecel/cont/Span.hh"
0017 #include "corecel/data/CollectionMirror.hh"
0018 #include "corecel/data/ParamsDataInterface.hh"
0019 #include "corecel/grid/SplineDerivCalculator.hh"
0020 #include "celeritas/Quantities.hh"
0021 #include "celeritas/Types.hh"
0022 #include "celeritas/Units.hh"
0023 #include "celeritas/global/ActionInterface.hh"
0024 
0025 #include "Model.hh"
0026 #include "PhysicsData.hh"
0027 #include "PhysicsOptions.hh"
0028 #include "Process.hh"
0029 
0030 namespace celeritas
0031 {
0032 class ActionRegistry;
0033 class AtomicRelaxationParams;
0034 class MaterialParams;
0035 class ParticleParams;
0036 
0037 //---------------------------------------------------------------------------//
0038 /*!
0039  * Manage physics processes and models.
0040  *
0041  * The physics params takes a vector of processes and sets up the processes and
0042  * models. It constructs data and mappings of data:
0043  * - particle type and process to tabulated values of cross sections etc,
0044  * - particle type to applicable processes
0045  *
0046  * During construction it constructs models and their corresponding list of
0047  * \c ActionId values, as well as the tables of cross section data. Besides the
0048  * individual interaction kernels, the physics parameters manage additional
0049  * actions:
0050  * - "pre-step": calculate physics step limits
0051  * - "along-step": propagate, apply energy loss, multiple scatter
0052  * - "range": limit step by energy loss
0053  * - "discrete-select": sample a process for a discrete interaction, or reject
0054  *   due to integral cross sectionl
0055  * - "integral-rejected": do not apply a discrete interaction
0056  * - "failure": interactor failed to allocate secondaries
0057  */
0058 class PhysicsParams final : public ParamsDataInterface<PhysicsParamsData>
0059 {
0060   public:
0061     //!@{
0062     //! \name Type aliases
0063     using SPConstParticles = std::shared_ptr<ParticleParams const>;
0064     using SPConstMaterials = std::shared_ptr<MaterialParams const>;
0065     using SPConstProcess = std::shared_ptr<Process const>;
0066     using SPConstModel = std::shared_ptr<Model const>;
0067     using SPConstRelaxation = std::shared_ptr<AtomicRelaxationParams const>;
0068 
0069     using VecProcess = std::vector<SPConstProcess>;
0070     using SpanConstProcessId = Span<ProcessId const>;
0071     using ActionIdRange = Range<ActionId>;
0072     using Options = PhysicsOptions;
0073     //!@}
0074 
0075     //! Physics parameter construction arguments
0076     struct Input
0077     {
0078         SPConstParticles particles;
0079         SPConstMaterials materials;
0080         VecProcess processes;
0081         SPConstRelaxation relaxation;  //!< Optional atomic relaxation
0082         ActionRegistry* action_registry = nullptr;
0083 
0084         Options options;
0085     };
0086 
0087   public:
0088     // Construct with processes and helper classes
0089     explicit PhysicsParams(Input);
0090 
0091     //// HOST ACCESSORS ////
0092 
0093     //! Number of models
0094     ModelId::size_type num_models() const { return models_.size(); }
0095 
0096     //! Number of processes
0097     ProcessId::size_type num_processes() const { return processes_.size(); }
0098 
0099     // Number of particle types
0100     inline ParticleId::size_type num_particles() const;
0101 
0102     // Maximum number of processes that apply to any one particle
0103     inline ProcessId::size_type max_particle_processes() const;
0104 
0105     // Get a model
0106     inline SPConstModel const& model(ModelId) const;
0107 
0108     // Get a process
0109     inline SPConstProcess const& process(ProcessId) const;
0110 
0111     // Get the process for the given model
0112     inline ProcessId process_id(ModelId id) const;
0113 
0114     // Get the action IDs for all models
0115     inline ActionIdRange model_actions() const;
0116 
0117     // Get the processes that apply to a particular particle
0118     SpanConstProcessId processes(ParticleId) const;
0119 
0120     //! Access physics properties on the host
0121     HostRef const& host_ref() const final { return host_ref_; }
0122 
0123     //! Access physics properties on the device
0124     DeviceRef const& device_ref() const final { return device_ref_; }
0125 
0126   private:
0127     using BC = SplineDerivCalculator::BoundaryCondition;
0128     using SPAction = std::shared_ptr<StaticConcreteAction>;
0129     using VecModel = std::vector<std::pair<SPConstModel, ProcessId>>;
0130     using HostValue = celeritas::HostVal<PhysicsParamsData>;
0131     using DeviceValue = PhysicsParamsData<Ownership::value, MemSpace::device>;
0132 
0133     // Kernels/actions
0134     SPAction pre_step_action_;
0135     SPAction msc_action_;
0136     SPAction range_action_;
0137     SPAction discrete_action_;
0138     SPAction integral_rejection_action_;
0139     SPAction failure_action_;
0140     SPAction fixed_step_action_;
0141 
0142     // Host metadata/access
0143     VecProcess processes_;
0144     VecModel models_;
0145     SPConstRelaxation relaxation_;
0146 
0147     // Host/device storage and reference
0148     HostValue host_;
0149     HostRef host_ref_;
0150     DeviceValue device_;
0151     DeviceRef device_ref_;
0152 
0153   private:
0154     VecModel build_models(ActionRegistry*) const;
0155     void build_options(Options const&, HostValue*) const;
0156     void build_particle_options(ParticleOptions const&, ParticleScalars*) const;
0157     void build_ids(ParticleParams const&, HostValue*) const;
0158     void build_tables(Options const&, MaterialParams const&, HostValue*) const;
0159     void build_model_tables(MaterialParams const&, HostValue*) const;
0160     void build_hardwired();
0161 };
0162 
0163 //---------------------------------------------------------------------------//
0164 // INLINE DEFINITIONS
0165 //---------------------------------------------------------------------------//
0166 /*!
0167  * Number of particle types.
0168  */
0169 auto PhysicsParams::num_particles() const -> ParticleId::size_type
0170 {
0171     return this->host_ref().process_ids.size();
0172 }
0173 
0174 //---------------------------------------------------------------------------//
0175 /*!
0176  * Number of particle types.
0177  */
0178 auto PhysicsParams::max_particle_processes() const -> ProcessId::size_type
0179 {
0180     return this->host_ref().scalars.max_particle_processes;
0181 }
0182 
0183 //---------------------------------------------------------------------------//
0184 /*!
0185  * Get a model.
0186  */
0187 auto PhysicsParams::model(ModelId id) const -> SPConstModel const&
0188 {
0189     CELER_EXPECT(id < this->num_models());
0190     return models_[id.get()].first;
0191 }
0192 
0193 //---------------------------------------------------------------------------//
0194 /*!
0195  * Get a process.
0196  */
0197 auto PhysicsParams::process(ProcessId id) const -> SPConstProcess const&
0198 {
0199     CELER_EXPECT(id < this->num_processes());
0200     return processes_[id.get()];
0201 }
0202 
0203 //---------------------------------------------------------------------------//
0204 /*!
0205  * Get the process ID of the given model.
0206  */
0207 ProcessId PhysicsParams::process_id(ModelId id) const
0208 {
0209     CELER_EXPECT(id < this->num_models());
0210     return models_[id.get()].second;
0211 }
0212 
0213 //---------------------------------------------------------------------------//
0214 /*!
0215  * Get the action kernel IDs for all models.
0216  */
0217 auto PhysicsParams::model_actions() const -> ActionIdRange
0218 {
0219     auto offset = host_ref().scalars.model_to_action;
0220     return {ActionId{offset}, ActionId{offset + this->num_models()}};
0221 }
0222 
0223 //---------------------------------------------------------------------------//
0224 }  // namespace celeritas