Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-27 10:26:31

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/optical/gen/detail/ScintSpectrumInserter.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <variant>
0010 
0011 #include "corecel/Assert.hh"
0012 #include "corecel/Types.hh"
0013 #include "corecel/cont/Span.hh"
0014 #include "corecel/data/CollectionBuilder.hh"
0015 #include "corecel/data/DedupeCollectionBuilder.hh"
0016 #include "corecel/grid/VectorUtils.hh"
0017 #include "corecel/math/PdfUtils.hh"
0018 #include "celeritas/Types.hh"
0019 #include "celeritas/grid/NonuniformGridInserter.hh"
0020 #include "celeritas/inp/OpticalPhysics.hh"
0021 
0022 #include "../ScintillationData.hh"
0023 
0024 namespace celeritas
0025 {
0026 namespace detail
0027 {
0028 //---------------------------------------------------------------------------//
0029 /*!
0030  * Build scintillation spectrum data.
0031  */
0032 class ScintSpectrumInserter
0033 {
0034   public:
0035     //!@{
0036     //! \name Type aliases
0037     using Data = HostVal<ScintillationData>;
0038     using SpectrumId = OptMatId;
0039     //!@}
0040 
0041   public:
0042     // Construct with data to insert into
0043     explicit inline ScintSpectrumInserter(Data* data);
0044 
0045     // Add scintillation data for a single material
0046     SpectrumId operator()(inp::ScintillationMaterial const& mat);
0047 
0048     // Add empty scintillation data for a single material
0049     SpectrumId operator()();
0050 
0051   private:
0052     // Index and inserter types for nonuniform grids (use opaque ID for grids)
0053     CollectionBuilder<ScintSpectrumRecord, MemSpace::host, OptMatId> spectra_;
0054     DedupeCollectionBuilder<real_type> reals_;
0055     CollectionBuilder<ScintDistributionRecord> scint_records_;
0056     using GridId = OpaqueId<NonuniformGridRecord>;
0057     NonuniformGridInserter<GridId> insert_energy_cdf_;
0058     CollectionBuilder<NonuniformGridRecord> energy_cdfs_;
0059 
0060     struct SpectrumVisitor;
0061 };
0062 
0063 //! Add to spectrum distribution variant
0064 struct ScintSpectrumInserter::SpectrumVisitor
0065 {
0066     ScintDistributionRecord& scint;
0067     NonuniformGridInserter<GridId>& insert_energy_cdf;
0068     inp::SpectrumArgument spectrum_argument;
0069 
0070     void operator()(inp::NormalDistribution const& norm_dist);
0071     void operator()(inp::Grid const& grid);
0072 };
0073 
0074 //---------------------------------------------------------------------------//
0075 // INLINE DEFINITIONS
0076 //---------------------------------------------------------------------------//
0077 /*!
0078  * Construct with defaults.
0079  */
0080 ScintSpectrumInserter::ScintSpectrumInserter(Data* data)
0081     : spectra_{&data->spectra}
0082     , reals_{&data->reals}
0083     , scint_records_{&data->scint_records}
0084     , insert_energy_cdf_(&data->reals, &data->energy_cdfs)
0085     , energy_cdfs_(&data->energy_cdfs)
0086 {
0087     CELER_EXPECT(data);
0088 }
0089 
0090 //---------------------------------------------------------------------------//
0091 /*!
0092  * Add scintillation data for a single material.
0093  */
0094 auto ScintSpectrumInserter::operator()(inp::ScintillationMaterial const& mat)
0095     -> SpectrumId
0096 {
0097     CELER_EXPECT(mat);
0098 
0099     // Calculate total yield across all components
0100     double total_yield{0};
0101     for (auto const& comp : mat.components)
0102     {
0103         CELER_VALIDATE(comp.yield > 0,
0104                        << "invalid yield=" << comp.yield
0105                        << " for scintillation component (should be positive)");
0106         total_yield += comp.yield;
0107     }
0108 
0109     std::vector<double> yield_pdf;
0110     auto const begin_components = scint_records_.size_id();
0111     for (auto const& comp : mat.components)
0112     {
0113         CELER_VALIDATE(comp.rise_time >= 0,
0114                        << "invalid rise_time=" << comp.rise_time
0115                        << " (should be nonnegative)");
0116         CELER_VALIDATE(comp.fall_time > 0,
0117                        << "invalid fall_time=" << comp.fall_time
0118                        << " (should be positive)");
0119 
0120         ScintDistributionRecord scint;
0121         scint.rise_time = comp.rise_time;
0122         scint.fall_time = comp.fall_time;
0123 
0124         std::visit(
0125             SpectrumVisitor{scint, insert_energy_cdf_, comp.spectrum_argument},
0126             comp.spectrum_distribution);
0127 
0128         scint_records_.push_back(scint);
0129         yield_pdf.push_back(comp.yield);
0130     }
0131 
0132     // Normalize yield PDF by total yield
0133     for (auto& y : yield_pdf)
0134     {
0135         y /= total_yield;
0136     }
0137 
0138     ScintSpectrumRecord spectrum;
0139     spectrum.yield_per_energy = total_yield;
0140     spectrum.components = {begin_components, scint_records_.size_id()};
0141     spectrum.yield_pdf = reals_.insert_back(yield_pdf.begin(), yield_pdf.end());
0142     CELER_ASSERT(total_yield > 0);
0143     CELER_ASSERT(!spectrum.components.empty());
0144     CELER_ASSERT(spectrum.components.size() == spectrum.yield_pdf.size());
0145 
0146     CELER_ENSURE(spectrum.components.size() == mat.components.size());
0147     return spectra_.push_back(std::move(spectrum));
0148 }
0149 
0150 //---------------------------------------------------------------------------//
0151 /*!
0152  * Add an empty record for non-scintillating materials.
0153  */
0154 auto ScintSpectrumInserter::operator()() -> SpectrumId
0155 {
0156     return spectra_.push_back({});
0157 }
0158 
0159 //---------------------------------------------------------------------------//
0160 /*!
0161  * Add scintillation data for a single material.
0162  */
0163 void ScintSpectrumInserter::SpectrumVisitor::operator()(
0164     inp::NormalDistribution const& norm_dist)
0165 {
0166     // Gaussian distribution
0167     CELER_VALIDATE(spectrum_argument == inp::SpectrumArgument::wavelength,
0168                    << "normal distribution scintillation must use wavelength");
0169     CELER_VALIDATE(norm_dist.mean > 0,
0170                    << "invalid lambda_mean=" << norm_dist.mean
0171                    << " for scintillation component (should be positive)");
0172     CELER_VALIDATE(norm_dist.stddev > 0,
0173                    << "invalid lambda_sigma=" << norm_dist.stddev
0174                    << " (should be positive)");
0175     scint.lambda_mean = norm_dist.mean;
0176     scint.lambda_sigma = norm_dist.stddev;
0177 }
0178 
0179 void ScintSpectrumInserter::SpectrumVisitor::operator()(inp::Grid const& grid)
0180 {
0181     // Explicit grid
0182     CELER_VALIDATE(is_monotonic_increasing(make_span(grid.x)),
0183                    << "scintillation spectrum energy grid values are not "
0184                       "monotonically increasing");
0185 
0186     inp::Grid cdf_grid;
0187     cdf_grid.x = grid.x;
0188     cdf_grid.y.resize(grid.x.size());
0189 
0190     if (spectrum_argument != inp::SpectrumArgument::energy)
0191     {
0192         CELER_NOT_IMPLEMENTED("wavelength-based grid scintillation spectra");
0193     }
0194 
0195     // Energy-based spectrum: integrate to get CDF
0196     SegmentIntegrator integrate_emission{TrapezoidSegmentIntegrator{}};
0197     integrate_emission(
0198         make_span(grid.x), make_span(grid.y), make_span(cdf_grid.y));
0199     normalize_cdf(make_span(cdf_grid.y));
0200 
0201     scint.energy_cdf = insert_energy_cdf(cdf_grid);
0202 }
0203 
0204 //---------------------------------------------------------------------------//
0205 }  // namespace detail
0206 }  // namespace celeritas