Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/celeritas/phys/ImportedProcessAdapter.hh was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 "celeritas/Types.hh"
0020 #include "celeritas/grid/ValueGridBuilder.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 //! Small helper class to hopefully help a little with debugging errors
0034 class IPAContextException : public RichContextException
0035 {
0036   public:
0037     IPAContextException(ParticleId id, ImportProcessClass ipc, PhysMatId mid);
0038 
0039     //! This class type
0040     char const* type() const final { return "ImportProcessAdapterContext"; }
0041 
0042     // Save context to a JSON object
0043     void output(JsonPimpl*) const final {}
0044 
0045     //! Get an explanatory message
0046     char const* what() const noexcept final { return what_.c_str(); }
0047 
0048   private:
0049     std::string what_;
0050 };
0051 
0052 //---------------------------------------------------------------------------//
0053 /*!
0054  * Manage imported physics data.
0055  */
0056 class ImportedProcesses
0057 {
0058   public:
0059     //!@{
0060     //! \name Type aliases
0061     using ImportProcessId = OpaqueId<ImportProcess>;
0062     using key_type = std::pair<PDGNumber, ImportProcessClass>;
0063     using SPConstParticles = std::shared_ptr<ParticleParams const>;
0064     //!@}
0065 
0066   public:
0067     // Construct with imported data
0068     static std::shared_ptr<ImportedProcesses>
0069     from_import(ImportData const& data, SPConstParticles particle_params);
0070 
0071     // Construct with imported tables
0072     explicit ImportedProcesses(std::vector<ImportProcess> io);
0073 
0074     // Return physics tables for a particle type and process
0075     ImportProcessId find(key_type) const;
0076 
0077     // Get the table for the given process ID
0078     inline ImportProcess const& get(ImportProcessId id) const;
0079 
0080     // Number of imported processes
0081     inline ImportProcessId::size_type size() const;
0082 
0083   private:
0084     std::vector<ImportProcess> processes_;
0085     std::map<key_type, ImportProcessId> ids_;
0086 };
0087 
0088 //---------------------------------------------------------------------------//
0089 /*!
0090  * Construct step limits from imported physics data.
0091  */
0092 class ImportedProcessAdapter
0093 {
0094   public:
0095     //!@{
0096     //! \name Type aliases
0097     using SPConstImported = std::shared_ptr<ImportedProcesses const>;
0098     using SPConstParticles = std::shared_ptr<ParticleParams const>;
0099     using StepLimitBuilders = Process::StepLimitBuilders;
0100     using SpanConstPDG = Span<PDGNumber const>;
0101     //!@}
0102 
0103   public:
0104     // Construct from shared table data
0105     ImportedProcessAdapter(SPConstImported imported,
0106                            SPConstParticles const& particles,
0107                            ImportProcessClass process_class,
0108                            SpanConstPDG pdg_numbers);
0109 
0110     // Construct from shared table data
0111     ImportedProcessAdapter(SPConstImported imported,
0112                            SPConstParticles const& particles,
0113                            ImportProcessClass process_class,
0114                            std::initializer_list<PDGNumber> pdg_numbers);
0115 
0116     // Construct step limits from the given particle/material type
0117     StepLimitBuilders step_limits(Applicability const& applic) const;
0118 
0119     // Get the lambda table for the given particle ID
0120     inline ImportPhysicsTable const& get_lambda(ParticleId id) const;
0121 
0122     // Access the imported processes
0123     SPConstImported const& processes() const { return imported_; }
0124 
0125     // Whether the given model is present in the process
0126     inline bool has_model(PDGNumber, ImportModelClass) const;
0127 
0128     // Whether the process applies when the particle is stopped
0129     inline bool applies_at_rest() const;
0130 
0131   private:
0132     using ImportTableId = OpaqueId<ImportPhysicsTable>;
0133     using ImportProcessId = ImportedProcesses::ImportProcessId;
0134 
0135     struct ParticleProcessIds
0136     {
0137         ImportProcessId process;
0138         ImportTableId lambda;
0139         ImportTableId lambda_prim;
0140         ImportTableId dedx;
0141         ImportTableId range;
0142     };
0143 
0144     SPConstImported imported_;
0145     ImportProcessClass process_class_;
0146     std::map<ParticleId, ParticleProcessIds> ids_;
0147 };
0148 
0149 //---------------------------------------------------------------------------//
0150 // INLINE DEFINITIONS
0151 //---------------------------------------------------------------------------//
0152 /*!
0153  * Get the table for the given process ID.
0154  */
0155 ImportProcess const& ImportedProcesses::get(ImportProcessId id) const
0156 {
0157     CELER_EXPECT(id < this->size());
0158     return processes_[id.get()];
0159 }
0160 
0161 //---------------------------------------------------------------------------//
0162 /*!
0163  * Number of imported processes.
0164  */
0165 auto ImportedProcesses::size() const -> ImportProcessId::size_type
0166 {
0167     return processes_.size();
0168 }
0169 
0170 //---------------------------------------------------------------------------//
0171 /*!
0172  * Get cross sections for the given particle ID.
0173  *
0174  * This is currently used for loading MSC data for calculating mean free paths.
0175  */
0176 ImportPhysicsTable const&
0177 ImportedProcessAdapter::get_lambda(ParticleId id) const
0178 {
0179     auto iter = ids_.find(id);
0180     CELER_EXPECT(iter != ids_.end());
0181     ImportTableId tab = iter->second.lambda;
0182     CELER_ENSURE(tab);
0183     return imported_->get(iter->second.process).tables[tab.unchecked_get()];
0184 }
0185 
0186 //---------------------------------------------------------------------------//
0187 /*!
0188  * Whether the given model is present in the process.
0189  */
0190 bool ImportedProcessAdapter::has_model(PDGNumber pdg, ImportModelClass imc) const
0191 {
0192     auto const& models
0193         = imported_->get(imported_->find({pdg, process_class_})).models;
0194     return std::any_of(
0195         models.begin(), models.end(), [&imc](ImportModel const& m) {
0196             return m.model_class == imc;
0197         });
0198 }
0199 
0200 //---------------------------------------------------------------------------//
0201 /*!
0202  * Whether the process applies when the particle is stopped.
0203  */
0204 bool ImportedProcessAdapter::applies_at_rest() const
0205 {
0206     auto it = ids_.begin();
0207     bool result = imported_->get(it->second.process).applies_at_rest;
0208     while (++it != ids_.end())
0209     {
0210         CELER_VALIDATE(
0211             result == imported_->get(it->second.process).applies_at_rest,
0212             << "process '" << to_cstring(process_class_)
0213             << "' applies at rest for some particles but not others");
0214     }
0215     return result;
0216 }
0217 
0218 //---------------------------------------------------------------------------//
0219 }  // namespace celeritas