File indexing completed on 2025-09-17 08:53:33
0001
0002
0003
0004
0005
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
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 class EnergyLossUrbanDistribution
0047 {
0048 public:
0049
0050
0051 using FluctuationRef = NativeCRef<FluctuationData>;
0052 using Energy = units::MevEnergy;
0053 using Mass = units::MevMass;
0054
0055
0056 public:
0057
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
0067 explicit inline CELER_FUNCTION
0068 EnergyLossUrbanDistribution(EnergyLossHelper const& helper);
0069
0070
0071 template<class Generator>
0072 inline CELER_FUNCTION Energy operator()(Generator& rng);
0073
0074 private:
0075
0076
0077 using Real2 = Array<real_type, 2>;
0078
0079
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
0088
0089
0090 static CELER_CONSTEXPR_FUNCTION real_type rate() { return 0.56; }
0091
0092
0093 static CELER_CONSTEXPR_FUNCTION size_type max_collisions() { return 8; }
0094
0095
0096 static CELER_CONSTEXPR_FUNCTION real_type exc_thresh() { return 42; }
0097
0098
0099 static CELER_CONSTEXPR_FUNCTION real_type ionization_energy()
0100 {
0101 return value_as<Energy>(EnergyLossHelper::ionization_energy());
0102 }
0103
0104
0105 static CELER_CONSTEXPR_FUNCTION real_type fwhm_min_energy()
0106 {
0107 return 1e-3;
0108 }
0109
0110
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
0126
0127
0128
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
0144
0145
0146
0147
0148
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
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
0163
0164 auto const& mat = cur_mat.material_record();
0165 if (max_energy_ > value_as<Energy>(mat.mean_excitation_energy()))
0166 {
0167
0168
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
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
0193
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
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
0213
0214 xs_ion_ *= this->rate();
0215 }
0216 }
0217
0218
0219
0220
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
0236
0237 template<class Generator>
0238 CELER_FUNCTION auto
0239 EnergyLossUrbanDistribution::operator()(Generator& rng) -> Energy
0240 {
0241
0242
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
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
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
0269
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
0276
0277
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
0290 result += this->sample_fast_urban(mean, std::sqrt(variance), rng);
0291 }
0292 return result;
0293 }
0294
0295
0296
0297
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
0309
0310 real_type alpha = 1;
0311
0312
0313 real_type mean_num_coll = 0;
0314
0315 if (xs_ion_ > this->max_collisions())
0316 {
0317
0318
0319
0320
0321
0322 alpha = (xs_ion_ + this->max_collisions()) * energy_ratio
0323 / (this->max_collisions() * energy_ratio + xs_ion_);
0324
0325
0326 real_type const mean_loss_coll = alpha * std::log(alpha) / (alpha - 1);
0327
0328
0329 mean_num_coll = xs_ion_ * energy_ratio * (alpha - 1)
0330 / ((energy_ratio - 1) * alpha);
0331
0332
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
0338 result += this->sample_fast_urban(mean, stddev, rng);
0339 }
0340 if (xs_ion_ > 0 && energy_ratio > alpha)
0341 {
0342
0343
0344
0345
0346 PoissonDistribution<real_type> sample_num_ioni(xs_ion_ - mean_num_coll);
0347
0348
0349
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
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 }