Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:53:33

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/em/distribution/EnergyLossUrbanDistribution.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <cmath>
0010 
0011 #include "corecel/Assert.hh"
0012 #include "corecel/Macros.hh"
0013 #include "corecel/math/Algorithms.hh"
0014 #include "corecel/random/distribution/PoissonDistribution.hh"
0015 #include "corecel/random/distribution/UniformRealDistribution.hh"
0016 #include "celeritas/em/data/FluctuationData.hh"
0017 
0018 #include "EnergyLossGaussianDistribution.hh"
0019 #include "EnergyLossHelper.hh"
0020 
0021 namespace celeritas
0022 {
0023 //---------------------------------------------------------------------------//
0024 /*!
0025  * Sample from the Urban model of energy loss fluctuations in thin layers.
0026  *
0027  * The Urban model is used to compute the energy loss fluctuation when \f$
0028  * \kappa \f$ is small. It assumes atoms have only two energy levels with
0029  * binding energies \f$ E_1 \f$ and \f$ E_2 \f$ and that the particle-atom
0030  * interaction will either be an excitation with energy loss \f$ E_1 \f$ or \f$
0031  * E_2 \f$ or an ionization with energy loss proportional to \f$ 1 / E^2 \f$.
0032  * The number of collisions for each interaction type has a Poisson
0033  * distribution with mean proportional to the interaction cross section. The
0034  * energy loss in a step will be the sum of the energy loss contributions from
0035  * excitation and ionization.
0036  *
0037  * When the number of ionizations is larger than a given threshold, a fast
0038  * sampling method can be used instead. The possible energy loss interval is
0039  * divided into two parts: a lower range in which the number of collisions is
0040  * large and the energy loss can be sampled from a Gaussian distribution, and
0041  * an upper range in which the energy loss is sampled for each collision.
0042  *
0043  * See section 7.3.2 of the Geant4 Physics Reference Manual and GEANT3 PHYS332
0044  * section 2.4 for details.
0045  */
0046 class EnergyLossUrbanDistribution
0047 {
0048   public:
0049     //!@{
0050     //! \name Type aliases
0051     using FluctuationRef = NativeCRef<FluctuationData>;
0052     using Energy = units::MevEnergy;
0053     using Mass = units::MevMass;
0054     //!@}
0055 
0056   public:
0057     // Construct from particle properties
0058     inline CELER_FUNCTION
0059     EnergyLossUrbanDistribution(FluctuationRef const& shared,
0060                                 MaterialTrackView const& cur_mat,
0061                                 Energy unscaled_mean_loss,
0062                                 Energy max_energy,
0063                                 Mass two_mebsgs,
0064                                 real_type beta_sq);
0065 
0066     // Construct from helper-calculated data
0067     explicit inline CELER_FUNCTION
0068     EnergyLossUrbanDistribution(EnergyLossHelper const& helper);
0069 
0070     // Sample energy loss according to the distribution
0071     template<class Generator>
0072     inline CELER_FUNCTION Energy operator()(Generator& rng);
0073 
0074   private:
0075     //// TYPES ////
0076 
0077     using Real2 = Array<real_type, 2>;
0078 
0079     //// DATA ////
0080 
0081     real_type max_energy_;
0082     real_type loss_scaling_;
0083     Real2 binding_energy_;
0084     Real2 xs_exc_;
0085     real_type xs_ion_;
0086 
0087     //// CONSTANTS ////
0088 
0089     //! Relative contribution of ionization to energy loss
0090     static CELER_CONSTEXPR_FUNCTION real_type rate() { return 0.56; }
0091 
0092     //! Number of collisions above which to use faster sampling from Gaussian
0093     static CELER_CONSTEXPR_FUNCTION size_type max_collisions() { return 8; }
0094 
0095     //! Threshold number of excitations used in width correction
0096     static CELER_CONSTEXPR_FUNCTION real_type exc_thresh() { return 42; }
0097 
0098     //! Ionization energy [MeV]
0099     static CELER_CONSTEXPR_FUNCTION real_type ionization_energy()
0100     {
0101         return value_as<Energy>(EnergyLossHelper::ionization_energy());
0102     }
0103 
0104     //! Energy point below which FWHM scaling doesn't change [MeV]
0105     static CELER_CONSTEXPR_FUNCTION real_type fwhm_min_energy()
0106     {
0107         return 1e-3;  // 1 keV
0108     }
0109 
0110     //// HELPER FUNCTIONS ////
0111 
0112     template<class Engine>
0113     CELER_FUNCTION real_type sample_excitation_loss(Engine& rng);
0114 
0115     template<class Engine>
0116     CELER_FUNCTION real_type sample_ionization_loss(Engine& rng);
0117 
0118     template<class Engine>
0119     static CELER_FUNCTION real_type sample_fast_urban(real_type mean,
0120                                                       real_type stddev,
0121                                                       Engine& rng);
0122 };
0123 
0124 //---------------------------------------------------------------------------//
0125 // INLINE DEFINITIONS
0126 //---------------------------------------------------------------------------//
0127 /*!
0128  * Construct from distribution parameters.
0129  */
0130 CELER_FUNCTION EnergyLossUrbanDistribution::EnergyLossUrbanDistribution(
0131     FluctuationRef const& shared,
0132     MaterialTrackView const& cur_mat,
0133     Energy unscaled_mean_loss,
0134     Energy max_energy,
0135     Mass two_mebsgs,
0136     real_type beta_sq)
0137     : max_energy_(max_energy.value())
0138 {
0139     CELER_EXPECT(unscaled_mean_loss > zero_quantity());
0140     CELER_EXPECT(two_mebsgs > zero_quantity());
0141     CELER_EXPECT(beta_sq > 0);
0142 
0143     // Width correction: the FWHM of the energy loss distribution in thin
0144     // layers is in most cases too small; this width correction is used to get
0145     // more accurate FWHM values while keeping the mean loss the same by
0146     // rescaling the energy levels and number of excitations. The width
0147     // correction algorithm is discussed (though not in much detail) in PRM
0148     // section 7.3.3
0149     loss_scaling_
0150         = real_type(0.5)
0151               * min(this->fwhm_min_energy() / max_energy_, real_type(1))
0152           + real_type(1);
0153     real_type const mean_loss = unscaled_mean_loss.value() / loss_scaling_;
0154 
0155     // Material-dependent data
0156     CELER_ASSERT(cur_mat.material_id() < shared.urban.size());
0157     UrbanFluctuationParameters const& params
0158         = shared.urban[cur_mat.material_id()];
0159     binding_energy_ = params.binding_energy;
0160     xs_exc_ = {0, 0};
0161 
0162     // Calculate the excitation macroscopic cross sections and apply the width
0163     // correction
0164     auto const& mat = cur_mat.material_record();
0165     if (max_energy_ > value_as<Energy>(mat.mean_excitation_energy()))
0166     {
0167         // Common term in the numerator and denominator of PRM Eq. 7.10
0168         // two_mebsgs = 2 * m_e c^2 * beta^2 * gamma^2
0169         real_type const w = std::log(value_as<units::MevMass>(two_mebsgs))
0170                             - beta_sq;
0171         real_type const w_0
0172             = value_as<units::LogMevEnergy>(mat.log_mean_excitation_energy());
0173         if (w > w_0)
0174         {
0175             if (w > params.log_binding_energy[1])
0176             {
0177                 real_type const c = mean_loss * (1 - this->rate()) / (w - w_0);
0178                 for (int i : range(2))
0179                 {
0180                     // Excitation macroscopic cross section (PRM Eq. 7.10)
0181                     xs_exc_[i] = c * params.oscillator_strength[i]
0182                                  * (w - params.log_binding_energy[i])
0183                                  / params.binding_energy[i];
0184                 }
0185             }
0186             else
0187             {
0188                 xs_exc_[0] = mean_loss * (1 - this->rate())
0189                              / params.binding_energy[0];
0190             }
0191 
0192             // Scale the binding energy and macroscopic cross section (i.e.,
0193             // the mean number of excitations)
0194             real_type scaling = 4;
0195             if (xs_exc_[0] < this->exc_thresh())
0196             {
0197                 scaling = real_type(0.5)
0198                           + (scaling - real_type(0.5))
0199                                 * std::sqrt(xs_exc_[0] / this->exc_thresh());
0200             }
0201             binding_energy_[0] *= scaling;
0202             xs_exc_[0] /= scaling;
0203         }
0204     }
0205 
0206     // Calculate the ionization macroscopic cross section (PRM Eq. 7.11)
0207     constexpr real_type e_0 = EnergyLossUrbanDistribution::ionization_energy();
0208     xs_ion_ = mean_loss * (max_energy_ - e_0)
0209               / (max_energy_ * e_0 * std::log(max_energy_ / e_0));
0210     if (xs_exc_[0] + xs_exc_[1] > 0)
0211     {
0212         // The contribution from excitation is nonzero, so scale the ionization
0213         // cross section accordingly
0214         xs_ion_ *= this->rate();
0215     }
0216 }
0217 
0218 //---------------------------------------------------------------------------//
0219 /*!
0220  * Construct from helper-calculated data.
0221  */
0222 CELER_FUNCTION EnergyLossUrbanDistribution::EnergyLossUrbanDistribution(
0223     EnergyLossHelper const& helper)
0224     : EnergyLossUrbanDistribution(helper.shared(),
0225                                   helper.material(),
0226                                   helper.mean_loss(),
0227                                   helper.max_energy(),
0228                                   helper.two_mebsgs(),
0229                                   helper.beta_sq())
0230 {
0231 }
0232 
0233 //---------------------------------------------------------------------------//
0234 /*!
0235  * Sample energy loss according to the distribution.
0236  */
0237 template<class Generator>
0238 CELER_FUNCTION auto
0239 EnergyLossUrbanDistribution::operator()(Generator& rng) -> Energy
0240 {
0241     // Calculate actual energy loss from the loss contributions from excitation
0242     // and ionization
0243     real_type result = this->sample_excitation_loss(rng)
0244                        + this->sample_ionization_loss(rng);
0245 
0246     CELER_ENSURE(result >= 0);
0247     return Energy{loss_scaling_ * result};
0248 }
0249 
0250 //---------------------------------------------------------------------------//
0251 /*!
0252  * Calculate the energy loss contribution from excitation for the Urban model.
0253  */
0254 template<class Engine>
0255 CELER_FUNCTION real_type
0256 EnergyLossUrbanDistribution::sample_excitation_loss(Engine& rng)
0257 {
0258     real_type result = 0;
0259 
0260     // Mean and variance for fast sampling from Gaussian
0261     real_type mean = 0;
0262     real_type variance = 0;
0263 
0264     for (int i : range(2))
0265     {
0266         if (xs_exc_[i] > this->max_collisions())
0267         {
0268             // When the number of collisions is large, use faster approach
0269             // of sampling from a Gaussian
0270             mean += xs_exc_[i] * binding_energy_[i];
0271             variance += xs_exc_[i] * ipow<2>(binding_energy_[i]);
0272         }
0273         else if (xs_exc_[i] > 0)
0274         {
0275             // The loss due to excitation is \f$ \Delta E_{exc} = n_1 E_1 + n_2
0276             // E_2 \f$, where the number of collisions \f$ n_i \f$ is sampled
0277             // from a Poisson distribution with mean \f$ \Sigma_i \f$
0278             unsigned int n = PoissonDistribution<real_type>(xs_exc_[i])(rng);
0279             if (n > 0)
0280             {
0281                 UniformRealDistribution<real_type> sample_fraction(n - 1,
0282                                                                    n + 1);
0283                 result += sample_fraction(rng) * binding_energy_[i];
0284             }
0285         }
0286     }
0287     if (variance > 0)
0288     {
0289         // Sample excitation energy loss contribution from a Gaussian
0290         result += this->sample_fast_urban(mean, std::sqrt(variance), rng);
0291     }
0292     return result;
0293 }
0294 
0295 //---------------------------------------------------------------------------//
0296 /*!
0297  * Calculate the energy loss contribution from ionization for the Urban model.
0298  */
0299 template<class Engine>
0300 CELER_FUNCTION real_type
0301 EnergyLossUrbanDistribution::sample_ionization_loss(Engine& rng)
0302 {
0303     real_type result = 0;
0304 
0305     constexpr real_type e_0 = EnergyLossUrbanDistribution::ionization_energy();
0306     real_type const energy_ratio = max_energy_ / e_0;
0307 
0308     // Parameter that determines the upper limit of the energy interval in
0309     // which the fast simulation is used
0310     real_type alpha = 1;
0311 
0312     // Mean number of collisions in the fast simulation interval
0313     real_type mean_num_coll = 0;
0314 
0315     if (xs_ion_ > this->max_collisions())
0316     {
0317         // When the number of collisions is large, fast sampling from a
0318         // Gaussian is used in the lower portion of the energy loss interval.
0319         // See PHYS332 section 2.4: Fast simulation for \f$ n_3 \ge 16 \f$
0320 
0321         // Calculate the maximum value of \f$ \alpha \f$ (Eq. 25)
0322         alpha = (xs_ion_ + this->max_collisions()) * energy_ratio
0323                 / (this->max_collisions() * energy_ratio + xs_ion_);
0324 
0325         // Mean energy loss for a single collision of this type (Eq. 14)
0326         real_type const mean_loss_coll = alpha * std::log(alpha) / (alpha - 1);
0327 
0328         // Mean number of collisions of this type (Eq. 16)
0329         mean_num_coll = xs_ion_ * energy_ratio * (alpha - 1)
0330                         / ((energy_ratio - 1) * alpha);
0331 
0332         // Mean and standard deviation of the total energy loss (Eqs. 18-19)
0333         real_type const mean = mean_num_coll * mean_loss_coll * e_0;
0334         real_type const stddev
0335             = e_0 * std::sqrt(xs_ion_ * (alpha - ipow<2>(mean_loss_coll)));
0336 
0337         // Sample energy loss from a Gaussian distribution
0338         result += this->sample_fast_urban(mean, stddev, rng);
0339     }
0340     if (xs_ion_ > 0 && energy_ratio > alpha)
0341     {
0342         // Sample number of ionizations from a Poisson distribution with mean
0343         // \f$ n_3 - n_A \f$, where \f$ n_3 \f$ is the number of ionizations
0344         // and \f$ n_A \f$ is the number of ionizations in the energy interval
0345         // in which the fast sampling from a Gaussian is used
0346         PoissonDistribution<real_type> sample_num_ioni(xs_ion_ - mean_num_coll);
0347 
0348         // Add the contribution from ionizations in the energy interval in
0349         // which the energy loss is sampled for each collision (Eq. 20)
0350         UniformRealDistribution<real_type> sample_fraction(
0351             alpha / energy_ratio, 1);
0352         for (auto n = sample_num_ioni(rng); n > 0; --n)
0353         {
0354             result += alpha * e_0 / sample_fraction(rng);
0355         }
0356     }
0357     return result;
0358 }
0359 
0360 //---------------------------------------------------------------------------//
0361 /*!
0362  * Fast sampling of energy loss in thin absorbers from a Gaussian distribution.
0363  */
0364 template<class Engine>
0365 CELER_FUNCTION real_type EnergyLossUrbanDistribution::sample_fast_urban(
0366     real_type mean, real_type stddev, Engine& rng)
0367 {
0368     if (stddev <= 4 * mean)
0369     {
0370         EnergyLossGaussianDistribution sample_eloss(Energy{mean},
0371                                                     Energy{stddev});
0372         return value_as<Energy>(sample_eloss(rng));
0373     }
0374     else
0375     {
0376         UniformRealDistribution<real_type> sample(0, 2 * mean);
0377         return sample(rng);
0378     }
0379 }
0380 
0381 //---------------------------------------------------------------------------//
0382 }  // namespace celeritas