Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-05 10:07:29

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/ImportedProcessAdapter.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <algorithm>
0010 #include <initializer_list>
0011 #include <map>
0012 #include <memory>
0013 #include <utility>
0014 #include <vector>
0015 
0016 #include "corecel/Assert.hh"
0017 #include "corecel/OpaqueId.hh"
0018 #include "corecel/cont/Span.hh"
0019 #include "corecel/io/EnumStringMapper.hh"
0020 #include "celeritas/Types.hh"
0021 #include "celeritas/io/ImportPhysicsTable.hh"
0022 #include "celeritas/io/ImportProcess.hh"
0023 
0024 #include "Applicability.hh"
0025 #include "PDGNumber.hh"
0026 #include "Process.hh"
0027 
0028 namespace celeritas
0029 {
0030 class ParticleParams;
0031 struct ImportData;
0032 
0033 //---------------------------------------------------------------------------//
0034 /*!
0035  * Manage imported physics data.
0036  */
0037 class ImportedProcesses
0038 {
0039   public:
0040     //!@{
0041     //! \name Type aliases
0042     using ImportProcessId = OpaqueId<ImportProcess>;
0043     using key_type = std::pair<PDGNumber, ImportProcessClass>;
0044     using SPConstParticles = std::shared_ptr<ParticleParams const>;
0045     //!@}
0046 
0047   public:
0048     // Construct with imported data
0049     static std::shared_ptr<ImportedProcesses>
0050     from_import(ImportData const& data, SPConstParticles particle_params);
0051 
0052     // Construct with imported tables
0053     explicit ImportedProcesses(std::vector<ImportProcess> io);
0054 
0055     // Return physics tables for a particle type and process
0056     ImportProcessId find(key_type) const;
0057 
0058     // Get the table for the given process ID
0059     inline ImportProcess const& get(ImportProcessId id) const;
0060 
0061     // Number of imported processes
0062     inline ImportProcessId::size_type size() const;
0063 
0064   private:
0065     std::vector<ImportProcess> processes_;
0066     std::map<key_type, ImportProcessId> ids_;
0067 };
0068 
0069 //---------------------------------------------------------------------------//
0070 /*!
0071  * Construct step limits from imported physics data.
0072  */
0073 class ImportedProcessAdapter
0074 {
0075   public:
0076     //!@{
0077     //! \name Type aliases
0078     using SPConstImported = std::shared_ptr<ImportedProcesses const>;
0079     using SPConstParticles = std::shared_ptr<ParticleParams const>;
0080     using XsGrid = Process::XsGrid;
0081     using EnergyLossGrid = Process::EnergyLossGrid;
0082     using SpanConstPDG = Span<PDGNumber const>;
0083     //!@}
0084 
0085   public:
0086     // Construct from shared table data
0087     ImportedProcessAdapter(SPConstImported imported,
0088                            SPConstParticles const& particles,
0089                            ImportProcessClass process_class,
0090                            SpanConstPDG pdg_numbers);
0091 
0092     // Construct from shared table data
0093     ImportedProcessAdapter(SPConstImported imported,
0094                            SPConstParticles const& particles,
0095                            ImportProcessClass process_class,
0096                            std::initializer_list<PDGNumber> pdg_numbers);
0097 
0098     // Get cross sections for the given particle/material type
0099     XsGrid macro_xs(Applicability const& applic) const;
0100 
0101     // Get energy loss for the given particle/material type
0102     EnergyLossGrid energy_loss(Applicability const& applic) const;
0103 
0104     // Access the imported processes
0105     SPConstImported const& processes() const { return imported_; }
0106 
0107     // Whether the given model is present in the process
0108     inline bool has_model(PDGNumber, ImportModelClass) const;
0109 
0110     // Whether the process applies when the particle is stopped
0111     inline bool applies_at_rest() const;
0112 
0113   private:
0114     using ImportProcessId = ImportedProcesses::ImportProcessId;
0115 
0116     SPConstImported imported_;
0117     ImportProcessClass process_class_;
0118     std::map<ParticleId, ImportProcessId> ids_;
0119 };
0120 
0121 //---------------------------------------------------------------------------//
0122 // INLINE DEFINITIONS
0123 //---------------------------------------------------------------------------//
0124 /*!
0125  * Get the table for the given process ID.
0126  */
0127 ImportProcess const& ImportedProcesses::get(ImportProcessId id) const
0128 {
0129     CELER_EXPECT(id < this->size());
0130     return processes_[id.get()];
0131 }
0132 
0133 //---------------------------------------------------------------------------//
0134 /*!
0135  * Number of imported processes.
0136  */
0137 auto ImportedProcesses::size() const -> ImportProcessId::size_type
0138 {
0139     return processes_.size();
0140 }
0141 
0142 //---------------------------------------------------------------------------//
0143 /*!
0144  * Whether the given model is present in the process.
0145  */
0146 bool ImportedProcessAdapter::has_model(PDGNumber pdg, ImportModelClass imc) const
0147 {
0148     auto const& models
0149         = imported_->get(imported_->find({pdg, process_class_})).models;
0150     return std::any_of(
0151         models.begin(), models.end(), [&imc](ImportModel const& m) {
0152             return m.model_class == imc;
0153         });
0154 }
0155 
0156 //---------------------------------------------------------------------------//
0157 /*!
0158  * Whether the process applies when the particle is stopped.
0159  */
0160 bool ImportedProcessAdapter::applies_at_rest() const
0161 {
0162     auto it = ids_.begin();
0163     bool result = imported_->get(it->second).applies_at_rest;
0164     while (++it != ids_.end())
0165     {
0166         CELER_VALIDATE(result == imported_->get(it->second).applies_at_rest,
0167                        << "process '" << process_class_
0168                        << "' applies at rest for some particles but not "
0169                           "others");
0170     }
0171     return result;
0172 }
0173 
0174 //---------------------------------------------------------------------------//
0175 }  // namespace celeritas