Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-09 08:40:05

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/ParticleParams.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 #include <string>
0011 #include <unordered_map>
0012 #include <utility>
0013 #include <vector>
0014 
0015 #include "corecel/Assert.hh"
0016 #include "corecel/data/CollectionMirror.hh"
0017 #include "corecel/data/ParamsDataInterface.hh"
0018 #include "celeritas/Quantities.hh"
0019 #include "celeritas/Types.hh"
0020 
0021 #include "PDGNumber.hh"
0022 #include "ParticleData.hh"
0023 #include "ParticleView.hh"
0024 
0025 namespace celeritas
0026 {
0027 struct ImportData;
0028 struct ImportParticle;
0029 
0030 //---------------------------------------------------------------------------//
0031 /*!
0032  * Data management for Standard Model particle classifications.
0033  *
0034  * This class represents "per-problem" shared data about standard
0035  * model particles being used.
0036  *
0037  * The ParticleParams is constructed on the host with a vector that
0038  * combines metadata (used for debugging output and interfacing with physics
0039  * setup) and data (used for on-device transport). Each entry in the
0040  * construction is assigned a unique \c ParticleId used for runtime access. See
0041  * \c celeritas::PDGNumber for details on the PDG code used during
0042  * construction.
0043  */
0044 class ParticleParams final : public ParamsDataInterface<ParticleParamsData>
0045 {
0046   public:
0047     //! Define a particle's input data
0048     struct ParticleInput
0049     {
0050         std::string name;  //!< Particle name
0051         PDGNumber pdg_code;  //!< See "Review of Particle Physics"
0052         units::MevMass mass;  //!< Rest mass [MeV / c^2]
0053         units::ElementaryCharge charge;  //!< Charge in units of [e]
0054         real_type decay_constant{};  //!< Decay constant [1/time]
0055 
0056         // Conversion function
0057         static ParticleInput from_import(ImportParticle const&);
0058     };
0059 
0060     //! Input data to construct this class
0061     using Input = std::vector<ParticleInput>;
0062 
0063   public:
0064     // Construct with imported data
0065     static std::shared_ptr<ParticleParams> from_import(ImportData const& data);
0066 
0067     // Construct with a vector of particle definitions
0068     explicit ParticleParams(Input const& defs);
0069 
0070     //// HOST ACCESSORS ////
0071 
0072     //! Number of particle definitions
0073     ParticleId::size_type size() const { return md_.size(); }
0074 
0075     // Get particle name
0076     inline std::string const& id_to_label(ParticleId id) const;
0077 
0078     // Get PDG code
0079     inline PDGNumber id_to_pdg(ParticleId id) const;
0080 
0081     // Find the ID from a name
0082     inline ParticleId find(std::string const& name) const;
0083 
0084     // Find the ID from a PDG code
0085     inline ParticleId find(PDGNumber pdg_code) const;
0086 
0087     // Access particle properties on host
0088     ParticleView get(ParticleId id) const;
0089 
0090     //! Access material properties on the host
0091     HostRef const& host_ref() const final { return data_.host_ref(); }
0092 
0093     //! Access material properties on the device
0094     DeviceRef const& device_ref() const final { return data_.device_ref(); }
0095 
0096   private:
0097     // Saved copy of metadata
0098     std::vector<std::pair<std::string, PDGNumber>> md_;
0099 
0100     // Map particle names to registered IDs
0101     std::unordered_map<std::string, ParticleId> name_to_id_;
0102 
0103     // Map particle codes to registered IDs
0104     std::unordered_map<PDGNumber, ParticleId> pdg_to_id_;
0105 
0106     // Host/device storage and reference
0107     CollectionMirror<ParticleParamsData> data_;
0108 };
0109 
0110 //---------------------------------------------------------------------------//
0111 // INLINE DEFINITIONS
0112 //---------------------------------------------------------------------------//
0113 /*!
0114  * Get particle name.
0115  */
0116 std::string const& ParticleParams::id_to_label(ParticleId id) const
0117 {
0118     CELER_EXPECT(id < this->size());
0119     return md_[id.get()].first;
0120 }
0121 
0122 //---------------------------------------------------------------------------//
0123 /*!
0124  * Get PDG code for a particle ID.
0125  */
0126 PDGNumber ParticleParams::id_to_pdg(ParticleId id) const
0127 {
0128     CELER_EXPECT(id < this->size());
0129     return md_[id.get()].second;
0130 }
0131 
0132 //---------------------------------------------------------------------------//
0133 /*!
0134  * Find the ID from a name.
0135  */
0136 ParticleId ParticleParams::find(std::string const& name) const
0137 {
0138     auto iter = name_to_id_.find(name);
0139     if (iter == name_to_id_.end())
0140     {
0141         return ParticleId{};
0142     }
0143     return iter->second;
0144 }
0145 
0146 //---------------------------------------------------------------------------//
0147 /*!
0148  * Find the ID from a PDG code.
0149  *
0150  * \todo Multiple particles can share a "generic" PDG code so this should be a
0151  * multimap.
0152  */
0153 ParticleId ParticleParams::find(PDGNumber pdg_code) const
0154 {
0155     auto iter = pdg_to_id_.find(pdg_code);
0156     if (iter == pdg_to_id_.end())
0157     {
0158         return ParticleId{};
0159     }
0160     return iter->second;
0161 }
0162 
0163 //---------------------------------------------------------------------------//
0164 }  // namespace celeritas