• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmc-dev / openmc / 13167860103

05 Feb 2025 10:35PM UTC coverage: 84.937% (+0.2%) from 84.729%
13167860103

Pull #3133

github

web-flow
Merge 715eed13d into 6e0f156d3
Pull Request #3133: Kinetics parameters using Iterated Fission Probability

312 of 322 new or added lines in 11 files covered. (96.89%)

2420 existing lines in 79 files now uncovered.

50463 of 59412 relevant lines covered (84.94%)

35029259.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.11
/src/physics.cpp
1
#include "openmc/physics.h"
2

3
#include "openmc/bank.h"
4
#include "openmc/bremsstrahlung.h"
5
#include "openmc/constants.h"
6
#include "openmc/distribution_multi.h"
7
#include "openmc/eigenvalue.h"
8
#include "openmc/endf.h"
9
#include "openmc/error.h"
10
#include "openmc/ifp.h"
11
#include "openmc/material.h"
12
#include "openmc/math_functions.h"
13
#include "openmc/message_passing.h"
14
#include "openmc/ncrystal_interface.h"
15
#include "openmc/nuclide.h"
16
#include "openmc/photon.h"
17
#include "openmc/physics_common.h"
18
#include "openmc/random_dist.h"
19
#include "openmc/random_lcg.h"
20
#include "openmc/reaction.h"
21
#include "openmc/search.h"
22
#include "openmc/secondary_uncorrelated.h"
23
#include "openmc/settings.h"
24
#include "openmc/simulation.h"
25
#include "openmc/string_utils.h"
26
#include "openmc/tallies/tally.h"
27
#include "openmc/thermal.h"
28
#include "openmc/weight_windows.h"
29

30
#include <fmt/core.h>
31

32
#include <algorithm> // for max, min, max_element
33
#include <cmath>     // for sqrt, exp, log, abs, copysign
34
#include <xtensor/xview.hpp>
35

36
namespace openmc {
37

38
//==============================================================================
39
// Non-member functions
40
//==============================================================================
41

42
void collision(Particle& p)
815,258,586✔
43
{
44
  // Add to collision counter for particle
45
  ++(p.n_collision());
815,258,586✔
46

47
  // Sample reaction for the material the particle is in
48
  switch (p.type()) {
815,258,586✔
49
  case ParticleType::neutron:
747,709,355✔
50
    sample_neutron_reaction(p);
747,709,355✔
51
    break;
747,709,355✔
52
  case ParticleType::photon:
14,559,788✔
53
    sample_photon_reaction(p);
14,559,788✔
54
    break;
14,559,788✔
55
  case ParticleType::electron:
52,896,479✔
56
    sample_electron_reaction(p);
52,896,479✔
57
    break;
52,896,479✔
58
  case ParticleType::positron:
92,964✔
59
    sample_positron_reaction(p);
92,964✔
60
    break;
92,964✔
61
  }
62

63
  if (settings::weight_window_checkpoint_collision)
815,258,586✔
64
    apply_weight_windows(p);
815,258,586✔
65

66
  // Kill particle if energy falls below cutoff
67
  int type = static_cast<int>(p.type());
815,258,586✔
68
  if (p.E() < settings::energy_cutoff[type]) {
815,258,586✔
69
    p.wgt() = 0.0;
5,123,551✔
70
  }
71

72
  // Display information about collision
73
  if (settings::verbosity >= 10 || p.trace()) {
815,258,586✔
74
    std::string msg;
84✔
75
    if (p.event() == TallyEvent::KILL) {
84✔
76
      msg = fmt::format("    Killed. Energy = {} eV.", p.E());
×
77
    } else if (p.type() == ParticleType::neutron) {
84✔
78
      msg = fmt::format("    {} with {}. Energy = {} eV.",
238✔
79
        reaction_name(p.event_mt()), data::nuclides[p.event_nuclide()]->name_,
168✔
80
        p.E());
84✔
81
    } else if (p.type() == ParticleType::photon) {
×
82
      msg = fmt::format("    {} with {}. Energy = {} eV.",
×
83
        reaction_name(p.event_mt()),
×
84
        to_element(data::nuclides[p.event_nuclide()]->name_), p.E());
×
85
    } else {
86
      msg = fmt::format("    Disappeared. Energy = {} eV.", p.E());
×
87
    }
88
    write_message(msg, 1);
84✔
89
  }
84✔
90
}
815,258,586✔
91

92
void sample_neutron_reaction(Particle& p)
747,709,355✔
93
{
94
  // Sample a nuclide within the material
95
  int i_nuclide = sample_nuclide(p);
747,709,355✔
96

97
  // Save which nuclide particle had collision with
98
  p.event_nuclide() = i_nuclide;
747,709,355✔
99

100
  // Create fission bank sites. Note that while a fission reaction is sampled,
101
  // it never actually "happens", i.e. the weight of the particle does not
102
  // change when sampling fission sites. The following block handles all
103
  // absorption (including fission)
104

105
  const auto& nuc {data::nuclides[i_nuclide]};
747,709,355✔
106

107
  if (nuc->fissionable_ && p.neutron_xs(i_nuclide).fission > 0.0) {
747,709,355✔
108
    auto& rx = sample_fission(i_nuclide, p);
119,929,001✔
109
    if (settings::run_mode == RunMode::EIGENVALUE) {
119,929,001✔
110
      create_fission_sites(p, i_nuclide, rx);
119,325,754✔
111
    } else if (settings::run_mode == RunMode::FIXED_SOURCE &&
603,247✔
112
               settings::create_fission_neutrons) {
113
      create_fission_sites(p, i_nuclide, rx);
588,079✔
114

115
      // Make sure particle population doesn't grow out of control for
116
      // subcritical multiplication problems.
117
      if (p.secondary_bank().size() >= 10000) {
588,079✔
118
        fatal_error(
×
119
          "The secondary particle bank appears to be growing without "
120
          "bound. You are likely running a subcritical multiplication problem "
121
          "with k-effective close to or greater than one.");
122
      }
123
    }
124
  }
125

126
  // Create secondary photons
127
  if (settings::photon_transport) {
747,709,355✔
128
    sample_secondary_photons(p, i_nuclide);
9,848,112✔
129
  }
130

131
  // If survival biasing is being used, the following subroutine adjusts the
132
  // weight of the particle. Otherwise, it checks to see if absorption occurs
133

134
  if (p.neutron_xs(i_nuclide).absorption > 0.0) {
747,709,355✔
135
    absorption(p, i_nuclide);
747,705,061✔
136
  }
137
  if (!p.alive())
747,709,355✔
138
    return;
19,409,514✔
139

140
  // Sample a scattering reaction and determine the secondary energy of the
141
  // exiting neutron
142
  const auto& ncrystal_mat = model::materials[p.material()]->ncrystal_mat();
728,299,841✔
143
  if (ncrystal_mat && p.E() < NCRYSTAL_MAX_ENERGY) {
728,299,841✔
144
    ncrystal_mat.scatter(p);
14,438✔
145
  } else {
146
    scatter(p, i_nuclide);
728,285,403✔
147
  }
148

149
  // Advance URR seed stream 'N' times after energy changes
150
  if (p.E() != p.E_last()) {
728,299,841✔
151
    advance_prn_seed(data::nuclides.size(), &p.seeds(STREAM_URR_PTABLE));
728,087,169✔
152
  }
153

154
  // Play russian roulette if survival biasing is turned on
155
  if (settings::survival_biasing) {
728,299,841✔
156
    if (p.wgt() < settings::weight_cutoff) {
557,892✔
157
      russian_roulette(p, settings::weight_survive);
63,984✔
158
    }
159
  }
160
}
161

162
void create_fission_sites(Particle& p, int i_nuclide, const Reaction& rx)
119,913,833✔
163
{
164
  // If uniform fission source weighting is turned on, we increase or decrease
165
  // the expected number of fission sites produced
166
  double weight = settings::ufs_on ? ufs_get_weight(p) : 1.0;
119,913,833✔
167

168
  // Determine the expected number of neutrons produced
169
  double nu_t = p.wgt() / simulation::keff * weight *
119,913,833✔
170
                p.neutron_xs(i_nuclide).nu_fission /
119,913,833✔
171
                p.neutron_xs(i_nuclide).total;
119,913,833✔
172

173
  // Sample the number of neutrons produced
174
  int nu = static_cast<int>(nu_t);
119,913,833✔
175
  if (prn(p.current_seed()) <= (nu_t - nu))
119,913,833✔
176
    ++nu;
17,693,490✔
177

178
  // If no neutrons were produced then don't continue
179
  if (nu == 0)
119,913,833✔
180
    return;
97,951,950✔
181

182
  // Initialize the counter of delayed neutrons encountered for each delayed
183
  // group.
184
  double nu_d[MAX_DELAYED_GROUPS] = {0.};
21,961,883✔
185

186
  // Clear out particle's nu fission bank
187
  p.nu_bank().clear();
21,961,883✔
188

189
  p.fission() = true;
21,961,883✔
190

191
  // Determine whether to place fission sites into the shared fission bank
192
  // or the secondary particle bank.
193
  bool use_fission_bank = (settings::run_mode == RunMode::EIGENVALUE);
21,961,883✔
194

195
  // Counter for the number of fission sites successfully stored to the shared
196
  // fission bank or the secondary particle bank
197
  int n_sites_stored;
198

199
  for (n_sites_stored = 0; n_sites_stored < nu; n_sites_stored++) {
49,522,262✔
200
    // Initialize fission site object with particle data
201
    SourceSite site;
27,560,379✔
202
    site.r = p.r();
27,560,379✔
203
    site.particle = ParticleType::neutron;
27,560,379✔
204
    site.time = p.time();
27,560,379✔
205
    site.wgt = 1. / weight;
27,560,379✔
206
    site.parent_id = p.id();
27,560,379✔
207
    site.progeny_id = p.n_progeny()++;
27,560,379✔
208
    site.surf_id = 0;
27,560,379✔
209

210
    // Sample delayed group and angle/energy for fission reaction
211
    sample_fission_neutron(i_nuclide, rx, &site, p);
27,560,379✔
212

213
    // Store fission site in bank
214
    if (use_fission_bank) {
27,560,379✔
215
      int64_t idx = simulation::fission_bank.thread_safe_append(site);
27,357,026✔
216
      if (idx == -1) {
27,357,026✔
217
        warning(
×
218
          "The shared fission bank is full. Additional fission sites created "
219
          "in this generation will not be banked. Results may be "
220
          "non-deterministic.");
221

222
        // Decrement number of particle progeny as storage was unsuccessful.
223
        // This step is needed so that the sum of all progeny is equal to the
224
        // size of the shared fission bank.
225
        p.n_progeny()--;
×
226

227
        // Break out of loop as no more sites can be added to fission bank
228
        break;
×
229
      }
230
      // Iterated Fission Probability (IFP) method
231
      if (settings::ifp) {
27,357,026✔
232
        ifp(p, site, idx);
242,100✔
233
      }
234
    } else {
235
      p.secondary_bank().push_back(site);
203,353✔
236
    }
237

238
    // Set the delayed group on the particle as well
239
    p.delayed_group() = site.delayed_group;
27,560,379✔
240

241
    // Increment the number of neutrons born delayed
242
    if (p.delayed_group() > 0) {
27,560,379✔
243
      nu_d[p.delayed_group() - 1]++;
174,430✔
244
    }
245

246
    // Write fission particles to nuBank
247
    NuBank& nu_bank_entry = p.nu_bank().emplace_back();
27,560,379✔
248
    nu_bank_entry.wgt = site.wgt;
27,560,379✔
249
    nu_bank_entry.E = site.E;
27,560,379✔
250
    nu_bank_entry.delayed_group = site.delayed_group;
27,560,379✔
251
  }
252

253
  // If shared fission bank was full, and no fissions could be added,
254
  // set the particle fission flag to false.
255
  if (n_sites_stored == 0) {
21,961,883✔
UNCOV
256
    p.fission() = false;
×
257
    return;
×
258
  }
259

260
  // Set nu to the number of fission sites successfully stored. If the fission
261
  // bank was not found to be full then these values are already equivalent.
262
  nu = n_sites_stored;
21,961,883✔
263

264
  // Store the total weight banked for analog fission tallies
265
  p.n_bank() = nu;
21,961,883✔
266
  p.wgt_bank() = nu / weight;
21,961,883✔
267
  for (size_t d = 0; d < MAX_DELAYED_GROUPS; d++) {
197,656,947✔
268
    p.n_delayed_bank(d) = nu_d[d];
175,695,064✔
269
  }
270
}
271

272
void sample_photon_reaction(Particle& p)
14,559,788✔
273
{
274
  // Kill photon if below energy cutoff -- an extra check is made here because
275
  // photons with energy below the cutoff may have been produced by neutrons
276
  // reactions or atomic relaxation
277
  int photon = static_cast<int>(ParticleType::photon);
14,559,788✔
278
  if (p.E() < settings::energy_cutoff[photon]) {
14,559,788✔
UNCOV
279
    p.E() = 0.0;
×
280
    p.wgt() = 0.0;
×
281
    return;
×
282
  }
283

284
  // Sample element within material
285
  int i_element = sample_element(p);
14,559,788✔
286
  const auto& micro {p.photon_xs(i_element)};
14,559,788✔
287
  const auto& element {*data::elements[i_element]};
14,559,788✔
288

289
  // Calculate photon energy over electron rest mass equivalent
290
  double alpha = p.E() / MASS_ELECTRON_EV;
14,559,788✔
291

292
  // For tallying purposes, this routine might be called directly. In that
293
  // case, we need to sample a reaction via the cutoff variable
294
  double prob = 0.0;
14,559,788✔
295
  double cutoff = prn(p.current_seed()) * micro.total;
14,559,788✔
296

297
  // Coherent (Rayleigh) scattering
298
  prob += micro.coherent;
14,559,788✔
299
  if (prob > cutoff) {
14,559,788✔
300
    p.mu() = element.rayleigh_scatter(alpha, p.current_seed());
772,344✔
301
    p.u() = rotate_angle(p.u(), p.mu(), nullptr, p.current_seed());
772,344✔
302
    p.event() = TallyEvent::SCATTER;
772,344✔
303
    p.event_mt() = COHERENT;
772,344✔
304
    return;
772,344✔
305
  }
306

307
  // Incoherent (Compton) scattering
308
  prob += micro.incoherent;
13,787,444✔
309
  if (prob > cutoff) {
13,787,444✔
310
    double alpha_out;
311
    int i_shell;
312
    element.compton_scatter(
17,351,618✔
313
      alpha, true, &alpha_out, &p.mu(), &i_shell, p.current_seed());
8,675,809✔
314

315
    // Determine binding energy of shell. The binding energy is 0.0 if
316
    // doppler broadening is not used.
317
    double e_b;
318
    if (i_shell == -1) {
8,675,809✔
UNCOV
319
      e_b = 0.0;
×
320
    } else {
321
      e_b = element.binding_energy_[i_shell];
8,675,809✔
322
    }
323

324
    // Create Compton electron
325
    double phi = uniform_distribution(0., 2.0 * PI, p.current_seed());
8,675,809✔
326
    double E_electron = (alpha - alpha_out) * MASS_ELECTRON_EV - e_b;
8,675,809✔
327
    int electron = static_cast<int>(ParticleType::electron);
8,675,809✔
328
    if (E_electron >= settings::energy_cutoff[electron]) {
8,675,809✔
329
      double mu_electron = (alpha - alpha_out * p.mu()) /
8,597,086✔
330
                           std::sqrt(alpha * alpha + alpha_out * alpha_out -
17,194,172✔
331
                                     2.0 * alpha * alpha_out * p.mu());
8,597,086✔
332
      Direction u = rotate_angle(p.u(), mu_electron, &phi, p.current_seed());
8,597,086✔
333
      p.create_secondary(p.wgt(), u, E_electron, ParticleType::electron);
8,597,086✔
334
    }
335

336
    // TODO: Compton subshell data does not match atomic relaxation data
337
    // Allow electrons to fill orbital and produce auger electrons
338
    // and fluorescent photons
339
    if (i_shell >= 0) {
8,675,809✔
340
      element.atomic_relaxation(i_shell, p);
8,675,809✔
341
    }
342

343
    phi += PI;
8,675,809✔
344
    p.E() = alpha_out * MASS_ELECTRON_EV;
8,675,809✔
345
    p.u() = rotate_angle(p.u(), p.mu(), &phi, p.current_seed());
8,675,809✔
346
    p.event() = TallyEvent::SCATTER;
8,675,809✔
347
    p.event_mt() = INCOHERENT;
8,675,809✔
348
    return;
8,675,809✔
349
  }
350

351
  // Photoelectric effect
352
  double prob_after = prob + micro.photoelectric;
5,111,635✔
353

354
  if (prob_after > cutoff) {
5,111,635✔
355
    // Get grid index, interpolation factor, and bounding subshell
356
    // cross sections
357
    int i_grid = micro.index_grid;
5,018,671✔
358
    double f = micro.interp_factor;
5,018,671✔
359
    const auto& xs_lower = xt::row(element.cross_sections_, i_grid);
5,018,671✔
360
    const auto& xs_upper = xt::row(element.cross_sections_, i_grid + 1);
5,018,671✔
361

362
    for (int i_shell = 0; i_shell < element.shells_.size(); ++i_shell) {
25,736,311✔
363
      const auto& shell {element.shells_[i_shell]};
25,736,311✔
364

365
      // Check threshold of reaction
366
      if (xs_lower(i_shell) == 0)
25,736,311✔
367
        continue;
11,113,200✔
368

369
      //  Evaluation subshell photoionization cross section
370
      prob += std::exp(
14,623,111✔
371
        xs_lower(i_shell) + f * (xs_upper(i_shell) - xs_lower(i_shell)));
14,623,111✔
372

373
      if (prob > cutoff) {
14,623,111✔
374
        // Determine binding energy based on whether atomic relaxation data is
375
        // present (if not, use value from Compton profile data)
376
        double binding_energy = element.has_atomic_relaxation_
5,018,671✔
377
                                  ? shell.binding_energy
5,018,671✔
UNCOV
378
                                  : element.binding_energy_[i_shell];
×
379

380
        // Determine energy of secondary electron
381
        double E_electron = p.E() - binding_energy;
5,018,671✔
382

383
        // Sample mu using non-relativistic Sauter distribution.
384
        // See Eqns 3.19 and 3.20 in "Implementing a photon physics
385
        // model in Serpent 2" by Toni Kaltiaisenaho
386
        double mu;
387
        while (true) {
388
          double r = prn(p.current_seed());
7,539,357✔
389
          if (4.0 * (1.0 - r) * r >= prn(p.current_seed())) {
7,539,357✔
390
            double rel_vel =
391
              std::sqrt(E_electron * (E_electron + 2.0 * MASS_ELECTRON_EV)) /
5,018,671✔
392
              (E_electron + MASS_ELECTRON_EV);
5,018,671✔
393
            mu =
5,018,671✔
394
              (2.0 * r + rel_vel - 1.0) / (2.0 * rel_vel * r - rel_vel + 1.0);
5,018,671✔
395
            break;
5,018,671✔
396
          }
397
        }
2,520,686✔
398

399
        double phi = uniform_distribution(0., 2.0 * PI, p.current_seed());
5,018,671✔
400
        Direction u;
5,018,671✔
401
        u.x = mu;
5,018,671✔
402
        u.y = std::sqrt(1.0 - mu * mu) * std::cos(phi);
5,018,671✔
403
        u.z = std::sqrt(1.0 - mu * mu) * std::sin(phi);
5,018,671✔
404

405
        // Create secondary electron
406
        p.create_secondary(p.wgt(), u, E_electron, ParticleType::electron);
5,018,671✔
407

408
        // Allow electrons to fill orbital and produce auger electrons
409
        // and fluorescent photons
410
        element.atomic_relaxation(i_shell, p);
5,018,671✔
411
        p.event() = TallyEvent::ABSORB;
5,018,671✔
412
        p.event_mt() = 533 + shell.index_subshell;
5,018,671✔
413
        p.wgt() = 0.0;
5,018,671✔
414
        p.E() = 0.0;
5,018,671✔
415
        return;
5,018,671✔
416
      }
417
    }
418
  }
10,037,342✔
419
  prob = prob_after;
92,964✔
420

421
  // Pair production
422
  prob += micro.pair_production;
92,964✔
423
  if (prob > cutoff) {
92,964✔
424
    double E_electron, E_positron;
425
    double mu_electron, mu_positron;
426
    element.pair_production(alpha, &E_electron, &E_positron, &mu_electron,
92,964✔
427
      &mu_positron, p.current_seed());
428

429
    // Create secondary electron
430
    Direction u = rotate_angle(p.u(), mu_electron, nullptr, p.current_seed());
92,964✔
431
    p.create_secondary(p.wgt(), u, E_electron, ParticleType::electron);
92,964✔
432

433
    // Create secondary positron
434
    u = rotate_angle(p.u(), mu_positron, nullptr, p.current_seed());
92,964✔
435
    p.create_secondary(p.wgt(), u, E_positron, ParticleType::positron);
92,964✔
436

437
    p.event() = TallyEvent::ABSORB;
92,964✔
438
    p.event_mt() = PAIR_PROD;
92,964✔
439
    p.wgt() = 0.0;
92,964✔
440
    p.E() = 0.0;
92,964✔
441
  }
442
}
443

444
void sample_electron_reaction(Particle& p)
52,896,479✔
445
{
446
  // TODO: create reaction types
447

448
  if (settings::electron_treatment == ElectronTreatment::TTB) {
52,896,479✔
449
    double E_lost;
450
    thick_target_bremsstrahlung(p, &E_lost);
52,896,479✔
451
  }
452

453
  p.E() = 0.0;
52,896,479✔
454
  p.wgt() = 0.0;
52,896,479✔
455
  p.event() = TallyEvent::ABSORB;
52,896,479✔
456
}
52,896,479✔
457

458
void sample_positron_reaction(Particle& p)
92,964✔
459
{
460
  // TODO: create reaction types
461

462
  if (settings::electron_treatment == ElectronTreatment::TTB) {
92,964✔
463
    double E_lost;
464
    thick_target_bremsstrahlung(p, &E_lost);
92,964✔
465
  }
466

467
  // Sample angle isotropically
468
  Direction u = isotropic_direction(p.current_seed());
92,964✔
469

470
  // Create annihilation photon pair traveling in opposite directions
471
  p.create_secondary(p.wgt(), u, MASS_ELECTRON_EV, ParticleType::photon);
92,964✔
472
  p.create_secondary(p.wgt(), -u, MASS_ELECTRON_EV, ParticleType::photon);
92,964✔
473

474
  p.E() = 0.0;
92,964✔
475
  p.wgt() = 0.0;
92,964✔
476
  p.event() = TallyEvent::ABSORB;
92,964✔
477
}
92,964✔
478

479
int sample_nuclide(Particle& p)
747,709,355✔
480
{
481
  // Sample cumulative distribution function
482
  double cutoff = prn(p.current_seed()) * p.macro_xs().total;
747,709,355✔
483

484
  // Get pointers to nuclide/density arrays
485
  const auto& mat {model::materials[p.material()]};
747,709,355✔
486
  int n = mat->nuclide_.size();
747,709,355✔
487

488
  double prob = 0.0;
747,709,355✔
489
  for (int i = 0; i < n; ++i) {
1,696,871,869✔
490
    // Get atom density
491
    int i_nuclide = mat->nuclide_[i];
1,696,871,869✔
492
    double atom_density = mat->atom_density_[i];
1,696,871,869✔
493

494
    // Increment probability to compare to cutoff
495
    prob += atom_density * p.neutron_xs(i_nuclide).total;
1,696,871,869✔
496
    if (prob >= cutoff)
1,696,871,869✔
497
      return i_nuclide;
747,709,355✔
498
  }
499

500
  // If we reach here, no nuclide was sampled
UNCOV
501
  p.write_restart();
×
502
  throw std::runtime_error {"Did not sample any nuclide during collision."};
×
503
}
504

505
int sample_element(Particle& p)
14,559,788✔
506
{
507
  // Sample cumulative distribution function
508
  double cutoff = prn(p.current_seed()) * p.macro_xs().total;
14,559,788✔
509

510
  // Get pointers to elements, densities
511
  const auto& mat {model::materials[p.material()]};
14,559,788✔
512

513
  double prob = 0.0;
14,559,788✔
514
  for (int i = 0; i < mat->element_.size(); ++i) {
37,482,255✔
515
    // Find atom density
516
    int i_element = mat->element_[i];
37,482,255✔
517
    double atom_density = mat->atom_density_[i];
37,482,255✔
518

519
    // Determine microscopic cross section
520
    double sigma = atom_density * p.photon_xs(i_element).total;
37,482,255✔
521

522
    // Increment probability to compare to cutoff
523
    prob += sigma;
37,482,255✔
524
    if (prob > cutoff) {
37,482,255✔
525
      // Save which nuclide particle had collision with for tally purpose
526
      p.event_nuclide() = mat->nuclide_[i];
14,559,788✔
527

528
      return i_element;
14,559,788✔
529
    }
530
  }
531

532
  // If we made it here, no element was sampled
UNCOV
533
  p.write_restart();
×
534
  fatal_error("Did not sample any element during collision.");
×
535
}
536

537
Reaction& sample_fission(int i_nuclide, Particle& p)
119,929,001✔
538
{
539
  // Get pointer to nuclide
540
  const auto& nuc {data::nuclides[i_nuclide]};
119,929,001✔
541

542
  // If we're in the URR, by default use the first fission reaction. We also
543
  // default to the first reaction if we know that there are no partial fission
544
  // reactions
545
  if (p.neutron_xs(i_nuclide).use_ptable || !nuc->has_partial_fission_) {
119,929,001✔
546
    return *nuc->fission_rx_[0];
119,901,579✔
547
  }
548

549
  // Check to see if we are in a windowed multipole range.  WMP only supports
550
  // the first fission reaction.
551
  if (nuc->multipole_) {
27,422✔
UNCOV
552
    if (p.E() >= nuc->multipole_->E_min_ && p.E() <= nuc->multipole_->E_max_) {
×
553
      return *nuc->fission_rx_[0];
×
554
    }
555
  }
556

557
  // Get grid index and interpolation factor and sample fission cdf
558
  const auto& micro = p.neutron_xs(i_nuclide);
27,422✔
559
  double cutoff = prn(p.current_seed()) * p.neutron_xs(i_nuclide).fission;
27,422✔
560
  double prob = 0.0;
27,422✔
561

562
  // Loop through each partial fission reaction type
563
  for (auto& rx : nuc->fission_rx_) {
27,436✔
564
    // add to cumulative probability
565
    prob += rx->xs(micro);
27,436✔
566

567
    // Create fission bank sites if fission occurs
568
    if (prob > cutoff)
27,436✔
569
      return *rx;
27,422✔
570
  }
571

572
  // If we reached here, no reaction was sampled
UNCOV
573
  throw std::runtime_error {
×
574
    "No fission reaction was sampled for " + nuc->name_};
×
575
}
576

577
void sample_photon_product(
1,403,340✔
578
  int i_nuclide, Particle& p, int* i_rx, int* i_product)
579
{
580
  // Get grid index and interpolation factor and sample photon production cdf
581
  const auto& micro = p.neutron_xs(i_nuclide);
1,403,340✔
582
  double cutoff = prn(p.current_seed()) * micro.photon_prod;
1,403,340✔
583
  double prob = 0.0;
1,403,340✔
584

585
  // Loop through each reaction type
586
  const auto& nuc {data::nuclides[i_nuclide]};
1,403,340✔
587
  for (int i = 0; i < nuc->reactions_.size(); ++i) {
23,211,156✔
588
    // Evaluate neutron cross section
589
    const auto& rx = nuc->reactions_[i];
23,211,156✔
590
    double xs = rx->xs(micro);
23,211,156✔
591

592
    // if cross section is zero for this reaction, skip it
593
    if (xs == 0.0)
23,211,156✔
594
      continue;
7,180,140✔
595

596
    for (int j = 0; j < rx->products_.size(); ++j) {
76,049,580✔
597
      if (rx->products_[j].particle_ == ParticleType::photon) {
61,421,904✔
598
        // For fission, artificially increase the photon yield to account
599
        // for delayed photons
600
        double f = 1.0;
47,915,244✔
601
        if (settings::delayed_photon_scaling) {
47,915,244✔
602
          if (is_fission(rx->mt_)) {
47,915,244✔
603
            if (nuc->prompt_photons_ && nuc->delayed_photons_) {
586,632✔
604
              double energy_prompt = (*nuc->prompt_photons_)(p.E());
586,632✔
605
              double energy_delayed = (*nuc->delayed_photons_)(p.E());
586,632✔
606
              f = (energy_prompt + energy_delayed) / (energy_prompt);
586,632✔
607
            }
608
          }
609
        }
610

611
        // add to cumulative probability
612
        prob += f * (*rx->products_[j].yield_)(p.E()) * xs;
47,915,244✔
613

614
        *i_rx = i;
47,915,244✔
615
        *i_product = j;
47,915,244✔
616
        if (prob > cutoff)
47,915,244✔
617
          return;
1,403,340✔
618
      }
619
    }
620
  }
621
}
622

623
void absorption(Particle& p, int i_nuclide)
747,705,061✔
624
{
625
  if (settings::survival_biasing) {
747,705,061✔
626
    // Determine weight absorbed in survival biasing
627
    const double wgt_absorb = p.wgt() * p.neutron_xs(i_nuclide).absorption /
557,892✔
628
                              p.neutron_xs(i_nuclide).total;
557,892✔
629

630
    // Adjust weight of particle by probability of absorption
631
    p.wgt() -= wgt_absorb;
557,892✔
632

633
    // Score implicit absorption estimate of keff
634
    if (settings::run_mode == RunMode::EIGENVALUE) {
557,892✔
635
      p.keff_tally_absorption() += wgt_absorb *
557,892✔
636
                                   p.neutron_xs(i_nuclide).nu_fission /
557,892✔
637
                                   p.neutron_xs(i_nuclide).absorption;
557,892✔
638
    }
639
  } else {
640
    // See if disappearance reaction happens
641
    if (p.neutron_xs(i_nuclide).absorption >
747,147,169✔
642
        prn(p.current_seed()) * p.neutron_xs(i_nuclide).total) {
747,147,169✔
643
      // Score absorption estimate of keff
644
      if (settings::run_mode == RunMode::EIGENVALUE) {
19,409,514✔
645
        p.keff_tally_absorption() += p.wgt() *
34,067,520✔
646
                                     p.neutron_xs(i_nuclide).nu_fission /
17,033,760✔
647
                                     p.neutron_xs(i_nuclide).absorption;
17,033,760✔
648
      }
649

650
      p.wgt() = 0.0;
19,409,514✔
651
      p.event() = TallyEvent::ABSORB;
19,409,514✔
652
      p.event_mt() = N_DISAPPEAR;
19,409,514✔
653
    }
654
  }
655
}
747,705,061✔
656

657
void scatter(Particle& p, int i_nuclide)
728,285,403✔
658
{
659
  // copy incoming direction
660
  Direction u_old {p.u()};
728,285,403✔
661

662
  // Get pointer to nuclide and grid index/interpolation factor
663
  const auto& nuc {data::nuclides[i_nuclide]};
728,285,403✔
664
  const auto& micro {p.neutron_xs(i_nuclide)};
728,285,403✔
665
  int i_temp = micro.index_temp;
728,285,403✔
666

667
  // For tallying purposes, this routine might be called directly. In that
668
  // case, we need to sample a reaction via the cutoff variable
669
  double cutoff = prn(p.current_seed()) * (micro.total - micro.absorption);
728,285,403✔
670
  bool sampled = false;
728,285,403✔
671

672
  // Calculate elastic cross section if it wasn't precalculated
673
  if (micro.elastic == CACHE_INVALID) {
728,285,403✔
674
    nuc->calculate_elastic_xs(p);
606,985,326✔
675
  }
676

677
  double prob = micro.elastic - micro.thermal;
728,285,403✔
678
  if (prob > cutoff) {
728,285,403✔
679
    // =======================================================================
680
    // NON-S(A,B) ELASTIC SCATTERING
681

682
    // Determine temperature
683
    double kT = nuc->multipole_ ? p.sqrtkT() * p.sqrtkT() : nuc->kTs_[i_temp];
633,740,453✔
684

685
    // Perform collision physics for elastic scattering
686
    elastic_scatter(i_nuclide, *nuc->reactions_[0], kT, p);
633,740,453✔
687

688
    p.event_mt() = ELASTIC;
633,740,453✔
689
    sampled = true;
633,740,453✔
690
  }
691

692
  prob = micro.elastic;
728,285,403✔
693
  if (prob > cutoff && !sampled) {
728,285,403✔
694
    // =======================================================================
695
    // S(A,B) SCATTERING
696

697
    sab_scatter(i_nuclide, micro.index_sab, p);
77,804,644✔
698

699
    p.event_mt() = ELASTIC;
77,804,644✔
700
    sampled = true;
77,804,644✔
701
  }
702

703
  if (!sampled) {
728,285,403✔
704
    // =======================================================================
705
    // INELASTIC SCATTERING
706

707
    int n = nuc->index_inelastic_scatter_.size();
16,740,306✔
708
    int i = 0;
16,740,306✔
709
    for (int j = 0; j < n && prob < cutoff; ++j) {
289,480,652✔
710
      i = nuc->index_inelastic_scatter_[j];
272,740,346✔
711

712
      // add to cumulative probability
713
      prob += nuc->reactions_[i]->xs(micro);
272,740,346✔
714
    }
715

716
    // Perform collision physics for inelastic scattering
717
    const auto& rx {nuc->reactions_[i]};
16,740,306✔
718
    inelastic_scatter(*nuc, *rx, p);
16,740,306✔
719
    p.event_mt() = rx->mt_;
16,740,306✔
720
  }
721

722
  // Set event component
723
  p.event() = TallyEvent::SCATTER;
728,285,403✔
724

725
  // Sample new outgoing angle for isotropic-in-lab scattering
726
  const auto& mat {model::materials[p.material()]};
728,285,403✔
727
  if (!mat->p0_.empty()) {
728,285,403✔
728
    int i_nuc_mat = mat->mat_nuclide_index_[i_nuclide];
339,852✔
729
    if (mat->p0_[i_nuc_mat]) {
339,852✔
730
      // Sample isotropic-in-lab outgoing direction
731
      p.u() = isotropic_direction(p.current_seed());
339,852✔
732
      p.mu() = u_old.dot(p.u());
339,852✔
733
    }
734
  }
735
}
728,285,403✔
736

737
void elastic_scatter(int i_nuclide, const Reaction& rx, double kT, Particle& p)
633,740,453✔
738
{
739
  // get pointer to nuclide
740
  const auto& nuc {data::nuclides[i_nuclide]};
633,740,453✔
741

742
  double vel = std::sqrt(p.E());
633,740,453✔
743
  double awr = nuc->awr_;
633,740,453✔
744

745
  // Neutron velocity in LAB
746
  Direction v_n = vel * p.u();
633,740,453✔
747

748
  // Sample velocity of target nucleus
749
  Direction v_t {};
633,740,453✔
750
  if (!p.neutron_xs(i_nuclide).use_ptable) {
633,740,453✔
751
    v_t = sample_target_velocity(*nuc, p.E(), p.u(), v_n,
1,215,462,224✔
752
      p.neutron_xs(i_nuclide).elastic, kT, p.current_seed());
607,731,112✔
753
  }
754

755
  // Velocity of center-of-mass
756
  Direction v_cm = (v_n + awr * v_t) / (awr + 1.0);
633,740,453✔
757

758
  // Transform to CM frame
759
  v_n -= v_cm;
633,740,453✔
760

761
  // Find speed of neutron in CM
762
  vel = v_n.norm();
633,740,453✔
763

764
  // Sample scattering angle, checking if angle distribution is present (assume
765
  // isotropic otherwise)
766
  double mu_cm;
767
  auto& d = rx.products_[0].distribution_[0];
633,740,453✔
768
  auto d_ = dynamic_cast<UncorrelatedAngleEnergy*>(d.get());
633,740,453✔
769
  if (!d_->angle().empty()) {
633,740,453✔
770
    mu_cm = d_->angle().sample(p.E(), p.current_seed());
633,740,453✔
771
  } else {
UNCOV
772
    mu_cm = uniform_distribution(-1., 1., p.current_seed());
×
773
  }
774

775
  // Determine direction cosines in CM
776
  Direction u_cm = v_n / vel;
633,740,453✔
777

778
  // Rotate neutron velocity vector to new angle -- note that the speed of the
779
  // neutron in CM does not change in elastic scattering. However, the speed
780
  // will change when we convert back to LAB
781
  v_n = vel * rotate_angle(u_cm, mu_cm, nullptr, p.current_seed());
633,740,453✔
782

783
  // Transform back to LAB frame
784
  v_n += v_cm;
633,740,453✔
785

786
  p.E() = v_n.dot(v_n);
633,740,453✔
787
  vel = std::sqrt(p.E());
633,740,453✔
788

789
  // compute cosine of scattering angle in LAB frame by taking dot product of
790
  // neutron's pre- and post-collision angle
791
  p.mu() = p.u().dot(v_n) / vel;
633,740,453✔
792

793
  // Set energy and direction of particle in LAB frame
794
  p.u() = v_n / vel;
633,740,453✔
795

796
  // Because of floating-point roundoff, it may be possible for mu_lab to be
797
  // outside of the range [-1,1). In these cases, we just set mu_lab to exactly
798
  // -1 or 1
799
  if (std::abs(p.mu()) > 1.0)
633,740,453✔
UNCOV
800
    p.mu() = std::copysign(1.0, p.mu());
×
801
}
633,740,453✔
802

803
void sab_scatter(int i_nuclide, int i_sab, Particle& p)
77,804,644✔
804
{
805
  // Determine temperature index
806
  const auto& micro {p.neutron_xs(i_nuclide)};
77,804,644✔
807
  int i_temp = micro.index_temp_sab;
77,804,644✔
808

809
  // Sample energy and angle
810
  double E_out;
811
  data::thermal_scatt[i_sab]->data_[i_temp].sample(
155,609,288✔
812
    micro, p.E(), &E_out, &p.mu(), p.current_seed());
77,804,644✔
813

814
  // Set energy to outgoing, change direction of particle
815
  p.E() = E_out;
77,804,644✔
816
  p.u() = rotate_angle(p.u(), p.mu(), nullptr, p.current_seed());
77,804,644✔
817
}
77,804,644✔
818

819
Direction sample_target_velocity(const Nuclide& nuc, double E, Direction u,
607,731,112✔
820
  Direction v_neut, double xs_eff, double kT, uint64_t* seed)
821
{
822
  // check if nuclide is a resonant scatterer
823
  ResScatMethod sampling_method;
824
  if (nuc.resonant_) {
607,731,112✔
825

826
    // sampling method to use
827
    sampling_method = settings::res_scat_method;
90,600✔
828

829
    // upper resonance scattering energy bound (target is at rest above this E)
830
    if (E > settings::res_scat_energy_max) {
90,600✔
831
      return {};
44,196✔
832

833
      // lower resonance scattering energy bound (should be no resonances below)
834
    } else if (E < settings::res_scat_energy_min) {
46,404✔
835
      sampling_method = ResScatMethod::cxs;
26,052✔
836
    }
837

838
    // otherwise, use free gas model
839
  } else {
840
    if (E >= FREE_GAS_THRESHOLD * kT && nuc.awr_ > 1.0) {
607,640,512✔
841
      return {};
254,166,826✔
842
    } else {
843
      sampling_method = ResScatMethod::cxs;
353,473,686✔
844
    }
845
  }
846

847
  // use appropriate target velocity sampling method
848
  switch (sampling_method) {
353,520,090✔
849
  case ResScatMethod::cxs:
353,499,738✔
850

851
    // sample target velocity with the constant cross section (cxs) approx.
852
    return sample_cxs_target_velocity(nuc.awr_, E, u, kT, seed);
353,499,738✔
853

854
  case ResScatMethod::dbrc:
20,352✔
855
  case ResScatMethod::rvs: {
856
    double E_red = std::sqrt(nuc.awr_ * E / kT);
20,352✔
857
    double E_low = std::pow(std::max(0.0, E_red - 4.0), 2) * kT / nuc.awr_;
20,352✔
858
    double E_up = (E_red + 4.0) * (E_red + 4.0) * kT / nuc.awr_;
20,352✔
859

860
    // find lower and upper energy bound indices
861
    // lower index
862
    int i_E_low;
863
    if (E_low < nuc.energy_0K_.front()) {
20,352✔
UNCOV
864
      i_E_low = 0;
×
865
    } else if (E_low > nuc.energy_0K_.back()) {
20,352✔
UNCOV
866
      i_E_low = nuc.energy_0K_.size() - 2;
×
867
    } else {
868
      i_E_low =
20,352✔
869
        lower_bound_index(nuc.energy_0K_.begin(), nuc.energy_0K_.end(), E_low);
20,352✔
870
    }
871

872
    // upper index
873
    int i_E_up;
874
    if (E_up < nuc.energy_0K_.front()) {
20,352✔
UNCOV
875
      i_E_up = 0;
×
876
    } else if (E_up > nuc.energy_0K_.back()) {
20,352✔
UNCOV
877
      i_E_up = nuc.energy_0K_.size() - 2;
×
878
    } else {
879
      i_E_up =
20,352✔
880
        lower_bound_index(nuc.energy_0K_.begin(), nuc.energy_0K_.end(), E_up);
20,352✔
881
    }
882

883
    if (i_E_up == i_E_low) {
20,352✔
884
      // Handle degenerate case -- if the upper/lower bounds occur for the same
885
      // index, then using cxs is probably a good approximation
886
      return sample_cxs_target_velocity(nuc.awr_, E, u, kT, seed);
20,352✔
887
    }
888

889
    if (sampling_method == ResScatMethod::dbrc) {
16,860✔
890
      // interpolate xs since we're not exactly at the energy indices
UNCOV
891
      double xs_low = nuc.elastic_0K_[i_E_low];
×
892
      double m = (nuc.elastic_0K_[i_E_low + 1] - xs_low) /
×
893
                 (nuc.energy_0K_[i_E_low + 1] - nuc.energy_0K_[i_E_low]);
×
894
      xs_low += m * (E_low - nuc.energy_0K_[i_E_low]);
×
895
      double xs_up = nuc.elastic_0K_[i_E_up];
×
896
      m = (nuc.elastic_0K_[i_E_up + 1] - xs_up) /
×
897
          (nuc.energy_0K_[i_E_up + 1] - nuc.energy_0K_[i_E_up]);
×
898
      xs_up += m * (E_up - nuc.energy_0K_[i_E_up]);
×
899

900
      // get max 0K xs value over range of practical relative energies
UNCOV
901
      double xs_max = *std::max_element(
×
902
        &nuc.elastic_0K_[i_E_low + 1], &nuc.elastic_0K_[i_E_up + 1]);
×
903
      xs_max = std::max({xs_low, xs_max, xs_up});
×
904

905
      while (true) {
906
        double E_rel;
UNCOV
907
        Direction v_target;
×
908
        while (true) {
909
          // sample target velocity with the constant cross section (cxs)
910
          // approx.
UNCOV
911
          v_target = sample_cxs_target_velocity(nuc.awr_, E, u, kT, seed);
×
912
          Direction v_rel = v_neut - v_target;
×
913
          E_rel = v_rel.dot(v_rel);
×
914
          if (E_rel < E_up)
×
915
            break;
×
916
        }
917

918
        // perform Doppler broadening rejection correction (dbrc)
UNCOV
919
        double xs_0K = nuc.elastic_xs_0K(E_rel);
×
920
        double R = xs_0K / xs_max;
×
921
        if (prn(seed) < R)
×
922
          return v_target;
×
923
      }
924

925
    } else if (sampling_method == ResScatMethod::rvs) {
16,860✔
926
      // interpolate xs CDF since we're not exactly at the energy indices
927
      // cdf value at lower bound attainable energy
928
      double cdf_low = 0.0;
16,860✔
929
      if (E_low > nuc.energy_0K_.front()) {
16,860✔
930
        double m = (nuc.xs_cdf_[i_E_low + 1] - nuc.xs_cdf_[i_E_low]) /
16,860✔
931
                   (nuc.energy_0K_[i_E_low + 1] - nuc.energy_0K_[i_E_low]);
16,860✔
932
        cdf_low = nuc.xs_cdf_[i_E_low] + m * (E_low - nuc.energy_0K_[i_E_low]);
16,860✔
933
      }
934

935
      // cdf value at upper bound attainable energy
936
      double m = (nuc.xs_cdf_[i_E_up + 1] - nuc.xs_cdf_[i_E_up]) /
16,860✔
937
                 (nuc.energy_0K_[i_E_up + 1] - nuc.energy_0K_[i_E_up]);
16,860✔
938
      double cdf_up = nuc.xs_cdf_[i_E_up] + m * (E_up - nuc.energy_0K_[i_E_up]);
16,860✔
939

940
      while (true) {
941
        // directly sample Maxwellian
942
        double E_t = -kT * std::log(prn(seed));
177,324✔
943

944
        // sample a relative energy using the xs cdf
945
        double cdf_rel = cdf_low + prn(seed) * (cdf_up - cdf_low);
177,324✔
946
        int i_E_rel = lower_bound_index(nuc.xs_cdf_.begin() + i_E_low,
177,324✔
947
          nuc.xs_cdf_.begin() + i_E_up + 2, cdf_rel);
177,324✔
948
        double E_rel = nuc.energy_0K_[i_E_low + i_E_rel];
177,324✔
949
        double m = (nuc.xs_cdf_[i_E_low + i_E_rel + 1] -
177,324✔
950
                     nuc.xs_cdf_[i_E_low + i_E_rel]) /
177,324✔
951
                   (nuc.energy_0K_[i_E_low + i_E_rel + 1] -
177,324✔
952
                     nuc.energy_0K_[i_E_low + i_E_rel]);
177,324✔
953
        E_rel += (cdf_rel - nuc.xs_cdf_[i_E_low + i_E_rel]) / m;
177,324✔
954

955
        // perform rejection sampling on cosine between
956
        // neutron and target velocities
957
        double mu = (E_t + nuc.awr_ * (E - E_rel)) /
177,324✔
958
                    (2.0 * std::sqrt(nuc.awr_ * E * E_t));
177,324✔
959

960
        if (std::abs(mu) < 1.0) {
177,324✔
961
          // set and accept target velocity
962
          E_t /= nuc.awr_;
16,860✔
963
          return std::sqrt(E_t) * rotate_angle(u, mu, nullptr, seed);
16,860✔
964
        }
965
      }
160,464✔
966
    }
967
  } // case RVS, DBRC
968
  } // switch (sampling_method)
969

UNCOV
970
  UNREACHABLE();
×
971
}
972

973
Direction sample_cxs_target_velocity(
353,503,230✔
974
  double awr, double E, Direction u, double kT, uint64_t* seed)
975
{
976
  double beta_vn = std::sqrt(awr * E / kT);
353,503,230✔
977
  double alpha = 1.0 / (1.0 + std::sqrt(PI) * beta_vn / 2.0);
353,503,230✔
978

979
  double beta_vt_sq;
980
  double mu;
981
  while (true) {
982
    // Sample two random numbers
983
    double r1 = prn(seed);
419,318,632✔
984
    double r2 = prn(seed);
419,318,632✔
985

986
    if (prn(seed) < alpha) {
419,318,632✔
987
      // With probability alpha, we sample the distribution p(y) =
988
      // y*e^(-y). This can be done with sampling scheme C45 from the Monte
989
      // Carlo sampler
990

991
      beta_vt_sq = -std::log(r1 * r2);
107,022,748✔
992

993
    } else {
994
      // With probability 1-alpha, we sample the distribution p(y) = y^2 *
995
      // e^(-y^2). This can be done with sampling scheme C61 from the Monte
996
      // Carlo sampler
997

998
      double c = std::cos(PI / 2.0 * prn(seed));
312,295,884✔
999
      beta_vt_sq = -std::log(r1) - std::log(r2) * c * c;
312,295,884✔
1000
    }
1001

1002
    // Determine beta * vt
1003
    double beta_vt = std::sqrt(beta_vt_sq);
419,318,632✔
1004

1005
    // Sample cosine of angle between neutron and target velocity
1006
    mu = uniform_distribution(-1., 1., seed);
419,318,632✔
1007

1008
    // Determine rejection probability
1009
    double accept_prob =
1010
      std::sqrt(beta_vn * beta_vn + beta_vt_sq - 2 * beta_vn * beta_vt * mu) /
419,318,632✔
1011
      (beta_vn + beta_vt);
419,318,632✔
1012

1013
    // Perform rejection sampling on vt and mu
1014
    if (prn(seed) < accept_prob)
419,318,632✔
1015
      break;
353,503,230✔
1016
  }
65,815,402✔
1017

1018
  // Determine speed of target nucleus
1019
  double vt = std::sqrt(beta_vt_sq * kT / awr);
353,503,230✔
1020

1021
  // Determine velocity vector of target nucleus based on neutron's velocity
1022
  // and the sampled angle between them
1023
  return vt * rotate_angle(u, mu, nullptr, seed);
353,503,230✔
1024
}
1025

1026
void sample_fission_neutron(
27,560,379✔
1027
  int i_nuclide, const Reaction& rx, SourceSite* site, Particle& p)
1028
{
1029
  // Get attributes of particle
1030
  double E_in = p.E();
27,560,379✔
1031
  uint64_t* seed = p.current_seed();
27,560,379✔
1032

1033
  // Determine total nu, delayed nu, and delayed neutron fraction
1034
  const auto& nuc {data::nuclides[i_nuclide]};
27,560,379✔
1035
  double nu_t = nuc->nu(E_in, Nuclide::EmissionMode::total);
27,560,379✔
1036
  double nu_d = nuc->nu(E_in, Nuclide::EmissionMode::delayed);
27,560,379✔
1037
  double beta = nu_d / nu_t;
27,560,379✔
1038

1039
  if (prn(seed) < beta) {
27,560,379✔
1040
    // ====================================================================
1041
    // DELAYED NEUTRON SAMPLED
1042

1043
    // sampled delayed precursor group
1044
    double xi = prn(seed) * nu_d;
174,430✔
1045
    double prob = 0.0;
174,430✔
1046
    int group;
1047
    for (group = 1; group < nuc->n_precursor_; ++group) {
654,706✔
1048
      // determine delayed neutron precursor yield for group j
1049
      double yield = (*rx.products_[group].yield_)(E_in);
640,984✔
1050

1051
      // Check if this group is sampled
1052
      prob += yield;
640,984✔
1053
      if (xi < prob)
640,984✔
1054
        break;
160,708✔
1055
    }
1056

1057
    // if the sum of the probabilities is slightly less than one and the
1058
    // random number is greater, j will be greater than nuc %
1059
    // n_precursor -- check for this condition
1060
    group = std::min(group, nuc->n_precursor_);
174,430✔
1061

1062
    // set the delayed group for the particle born from fission
1063
    site->delayed_group = group;
174,430✔
1064

1065
  } else {
1066
    // ====================================================================
1067
    // PROMPT NEUTRON SAMPLED
1068

1069
    // set the delayed group for the particle born from fission to 0
1070
    site->delayed_group = 0;
27,385,949✔
1071
  }
1072

1073
  // sample from prompt neutron energy distribution
1074
  int n_sample = 0;
27,560,379✔
1075
  double mu;
1076
  while (true) {
1077
    rx.products_[site->delayed_group].sample(E_in, site->E, mu, seed);
27,560,379✔
1078

1079
    // resample if energy is greater than maximum neutron energy
1080
    constexpr int neutron = static_cast<int>(ParticleType::neutron);
27,560,379✔
1081
    if (site->E < data::energy_max[neutron])
27,560,379✔
1082
      break;
27,560,379✔
1083

1084
    // check for large number of resamples
UNCOV
1085
    ++n_sample;
×
1086
    if (n_sample == MAX_SAMPLE) {
×
1087
      // particle_write_restart(p)
UNCOV
1088
      fatal_error("Resampled energy distribution maximum number of times "
×
1089
                  "for nuclide " +
×
1090
                  nuc->name_);
×
1091
    }
1092
  }
1093

1094
  // Sample azimuthal angle uniformly in [0, 2*pi) and assign angle
1095
  site->u = rotate_angle(p.u(), mu, nullptr, seed);
27,560,379✔
1096
}
27,560,379✔
1097

1098
void inelastic_scatter(const Nuclide& nuc, const Reaction& rx, Particle& p)
16,740,306✔
1099
{
1100
  // copy energy of neutron
1101
  double E_in = p.E();
16,740,306✔
1102

1103
  // sample outgoing energy and scattering cosine
1104
  double E;
1105
  double mu;
1106
  rx.products_[0].sample(E_in, E, mu, p.current_seed());
16,740,306✔
1107

1108
  // if scattering system is in center-of-mass, transfer cosine of scattering
1109
  // angle and outgoing energy from CM to LAB
1110
  if (rx.scatter_in_cm_) {
16,740,306✔
1111
    double E_cm = E;
16,703,316✔
1112

1113
    // determine outgoing energy in lab
1114
    double A = nuc.awr_;
16,703,316✔
1115
    E = E_cm + (E_in + 2.0 * mu * (A + 1.0) * std::sqrt(E_in * E_cm)) /
16,703,316✔
1116
                 ((A + 1.0) * (A + 1.0));
16,703,316✔
1117

1118
    // determine outgoing angle in lab
1119
    mu = mu * std::sqrt(E_cm / E) + 1.0 / (A + 1.0) * std::sqrt(E_in / E);
16,703,316✔
1120
  }
1121

1122
  // Because of floating-point roundoff, it may be possible for mu to be
1123
  // outside of the range [-1,1). In these cases, we just set mu to exactly -1
1124
  // or 1
1125
  if (std::abs(mu) > 1.0)
16,740,306✔
UNCOV
1126
    mu = std::copysign(1.0, mu);
×
1127

1128
  // Set outgoing energy and scattering angle
1129
  p.E() = E;
16,740,306✔
1130
  p.mu() = mu;
16,740,306✔
1131

1132
  // change direction of particle
1133
  p.u() = rotate_angle(p.u(), mu, nullptr, p.current_seed());
16,740,306✔
1134

1135
  // evaluate yield
1136
  double yield = (*rx.products_[0].yield_)(E_in);
16,740,306✔
1137
  if (std::floor(yield) == yield && yield > 0) {
16,740,306✔
1138
    // If yield is integral, create exactly that many secondary particles
1139
    for (int i = 0; i < static_cast<int>(std::round(yield)) - 1; ++i) {
16,805,332✔
1140
      p.create_secondary(p.wgt(), p.u(), p.E(), ParticleType::neutron);
65,104✔
1141
    }
1142
  } else {
16,740,228✔
1143
    // Otherwise, change weight of particle based on yield
1144
    p.wgt() *= yield;
78✔
1145
  }
1146
}
16,740,306✔
1147

1148
void sample_secondary_photons(Particle& p, int i_nuclide)
9,848,112✔
1149
{
1150
  // Sample the number of photons produced
1151
  double y_t =
1152
    p.neutron_xs(i_nuclide).photon_prod / p.neutron_xs(i_nuclide).total;
9,848,112✔
1153
  int y = static_cast<int>(y_t);
9,848,112✔
1154
  if (prn(p.current_seed()) <= y_t - y)
9,848,112✔
1155
    ++y;
630,816✔
1156

1157
  // Sample each secondary photon
1158
  for (int i = 0; i < y; ++i) {
11,251,452✔
1159
    // Sample the reaction and product
1160
    int i_rx;
1161
    int i_product;
1162
    sample_photon_product(i_nuclide, p, &i_rx, &i_product);
1,403,340✔
1163

1164
    // Sample the outgoing energy and angle
1165
    auto& rx = data::nuclides[i_nuclide]->reactions_[i_rx];
1,403,340✔
1166
    double E;
1167
    double mu;
1168
    rx->products_[i_product].sample(p.E(), E, mu, p.current_seed());
1,403,340✔
1169

1170
    // Sample the new direction
1171
    Direction u = rotate_angle(p.u(), mu, nullptr, p.current_seed());
1,403,340✔
1172

1173
    // In a k-eigenvalue simulation, it's necessary to provide higher weight to
1174
    // secondary photons from non-fission reactions to properly balance energy
1175
    // release and deposition. See D. P. Griesheimer, S. J. Douglass, and M. H.
1176
    // Stedry, "Self-consistent energy normalization for quasistatic reactor
1177
    // calculations", Proc. PHYSOR, Cambridge, UK, Mar 29-Apr 2, 2020.
1178
    double wgt;
1179
    if (settings::run_mode == RunMode::EIGENVALUE && !is_fission(rx->mt_)) {
1,403,340✔
1180
      wgt = simulation::keff * p.wgt();
378,540✔
1181
    } else {
1182
      wgt = p.wgt();
1,024,800✔
1183
    }
1184

1185
    // Create the secondary photon
1186
    p.create_secondary(wgt, u, E, ParticleType::photon);
1,403,340✔
1187
  }
1188
}
9,848,112✔
1189

1190
} // namespace openmc
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc