Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-05 10:28:00

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/ParamsDataInterface.hh"
0017 #include "corecel/data/ParamsDataStore.hh"
0018 #include "celeritas/Quantities.hh"
0019 #include "celeritas/Types.hh"
0020 #include "celeritas/inp/Particle.hh"
0021 
0022 #include "PDGNumber.hh"
0023 #include "ParticleData.hh"
0024 #include "ParticleView.hh"
0025 
0026 namespace celeritas
0027 {
0028 struct ImportData;
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     //! Input data to construct this class
0048     using Input = std::vector<inp::Particle>;
0049 
0050   public:
0051     // Construct with imported data
0052     static std::shared_ptr<ParticleParams> from_import(ImportData const& data);
0053 
0054     // Construct with a vector of particle definitions
0055     explicit ParticleParams(Input const& defs);
0056 
0057     //// HOST ACCESSORS ////
0058 
0059     //! Number of particle definitions
0060     ParticleId::size_type size() const { return md_.size(); }
0061 
0062     // Get particle name
0063     inline std::string const& id_to_label(ParticleId id) const;
0064 
0065     // Get PDG code
0066     inline PDGNumber id_to_pdg(ParticleId id) const;
0067 
0068     // Find the ID from a name
0069     inline ParticleId find(std::string const& name) const;
0070 
0071     // Find the ID from a PDG code
0072     inline ParticleId find(PDGNumber pdg_code) const;
0073 
0074     // Access particle properties on host
0075     ParticleView get(ParticleId id) const;
0076 
0077     //! Access material properties on the host
0078     HostRef const& host_ref() const final { return data_.host_ref(); }
0079 
0080     //! Access material properties on the device
0081     DeviceRef const& device_ref() const final { return data_.device_ref(); }
0082 
0083   private:
0084     // Saved copy of metadata
0085     std::vector<std::pair<std::string, PDGNumber>> md_;
0086 
0087     // Map particle names to registered IDs
0088     std::unordered_map<std::string, ParticleId> name_to_id_;
0089 
0090     // Map particle codes to registered IDs
0091     std::unordered_map<PDGNumber, ParticleId> pdg_to_id_;
0092 
0093     // Host/device storage and reference
0094     ParamsDataStore<ParticleParamsData> data_;
0095 };
0096 
0097 //---------------------------------------------------------------------------//
0098 // INLINE DEFINITIONS
0099 //---------------------------------------------------------------------------//
0100 /*!
0101  * Get particle name.
0102  */
0103 std::string const& ParticleParams::id_to_label(ParticleId id) const
0104 {
0105     CELER_EXPECT(id < this->size());
0106     return md_[id.get()].first;
0107 }
0108 
0109 //---------------------------------------------------------------------------//
0110 /*!
0111  * Get PDG code for a particle ID.
0112  */
0113 PDGNumber ParticleParams::id_to_pdg(ParticleId id) const
0114 {
0115     CELER_EXPECT(id < this->size());
0116     return md_[id.get()].second;
0117 }
0118 
0119 //---------------------------------------------------------------------------//
0120 /*!
0121  * Find the ID from a name.
0122  */
0123 ParticleId ParticleParams::find(std::string const& name) const
0124 {
0125     auto iter = name_to_id_.find(name);
0126     if (iter == name_to_id_.end())
0127     {
0128         return ParticleId{};
0129     }
0130     return iter->second;
0131 }
0132 
0133 //---------------------------------------------------------------------------//
0134 /*!
0135  * Find the ID from a PDG code.
0136  *
0137  * \todo Multiple particles can share a "generic" PDG code so this should be a
0138  * multimap.
0139  */
0140 ParticleId ParticleParams::find(PDGNumber pdg_code) const
0141 {
0142     auto iter = pdg_to_id_.find(pdg_code);
0143     if (iter == pdg_to_id_.end())
0144     {
0145         return ParticleId{};
0146     }
0147     return iter->second;
0148 }
0149 
0150 //---------------------------------------------------------------------------//
0151 }  // namespace celeritas