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

openmc-dev / openmc / 22500302709

27 Feb 2026 07:16PM UTC coverage: 81.512% (-0.3%) from 81.826%
22500302709

Pull #3830

github

web-flow
Merge 25fbb4266 into b3788f11e
Pull Request #3830: Parallelize sampling external sources and threadsafe rejection counters

17488 of 25193 branches covered (69.42%)

Branch coverage included in aggregate %.

59 of 66 new or added lines in 6 files covered. (89.39%)

841 existing lines in 44 files now uncovered.

57726 of 67081 relevant lines covered (86.05%)

44920080.48 hits per line

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

82.04
/src/physics_mg.cpp
1
#include "openmc/physics_mg.h"
2

3
#include <stdexcept>
4

5
#include "openmc/tensor.h"
6
#include <fmt/core.h>
7

8
#include "openmc/bank.h"
9
#include "openmc/constants.h"
10
#include "openmc/eigenvalue.h"
11
#include "openmc/error.h"
12
#include "openmc/material.h"
13
#include "openmc/math_functions.h"
14
#include "openmc/message_passing.h"
15
#include "openmc/mgxs_interface.h"
16
#include "openmc/particle.h"
17
#include "openmc/physics_common.h"
18
#include "openmc/random_lcg.h"
19
#include "openmc/settings.h"
20
#include "openmc/simulation.h"
21
#include "openmc/tallies/tally.h"
22
#include "openmc/weight_windows.h"
23

24
namespace openmc {
25

26
void collision_mg(Particle& p)
1,783,060,477✔
27
{
28
  // Add to the collision counter for the particle
29
  p.n_collision()++;
1,783,060,477✔
30
  p.secondary_bank_index() = p.secondary_bank().size();
1,783,060,477✔
31

32
  // Sample the reaction type
33
  sample_reaction(p);
1,783,060,477✔
34

35
  if (settings::weight_windows_on) {
1,783,060,477✔
36
    auto [ww_found, ww] = search_weight_window(p);
3,057,087✔
37
    if (!ww_found && p.type() == ParticleType::neutron()) {
3,057,087!
38
      // if the weight window is not valid, apply russian roulette
39
      // (regardless of weight window collision checkpoint setting)
40
      apply_russian_roulette(p);
×
41
    } else if (settings::weight_window_checkpoint_collision) {
3,057,087!
42
      // if collision checkpointing is on, apply weight window
43
      apply_weight_window(p, ww);
3,057,087✔
44
    }
45
  }
46

47
  // Display information about collision
48
  if ((settings::verbosity >= 10) || p.trace()) {
1,783,060,477!
UNCOV
49
    write_message(fmt::format("    Energy Group = {}", p.g()), 1);
×
50
  }
51
}
1,783,060,477✔
52

53
void sample_reaction(Particle& p)
1,783,060,477✔
54
{
55
  // Create fission bank sites. Note that while a fission reaction is sampled,
56
  // it never actually "happens", i.e. the weight of the particle does not
57
  // change when sampling fission sites. The following block handles all
58
  // absorption (including fission)
59

60
  if (model::materials[p.material()]->fissionable()) {
1,783,060,477✔
61
    if (settings::run_mode == RunMode::EIGENVALUE ||
1,777,796,823!
UNCOV
62
        (settings::run_mode == RunMode::FIXED_SOURCE &&
×
63
          settings::create_fission_neutrons)) {
64
      create_fission_sites(p);
1,777,796,823✔
65
    }
66
  }
67

68
  // If survival biasing is being used, the following subroutine adjusts the
69
  // weight of the particle. Otherwise, it checks to see if absorption occurs.
70
  if (p.macro_xs().absorption > 0.) {
1,783,060,477!
71
    absorption(p);
1,783,060,477✔
72
  }
73
  if (!p.alive())
1,783,060,477✔
74
    return;
75

76
  // Sample a scattering event to determine the energy of the exiting neutron
77
  scatter(p);
1,669,415,704✔
78

79
  // Play russian roulette if there are no weight windows
80
  if (!settings::weight_windows_on)
1,669,415,704✔
81
    apply_russian_roulette(p);
1,668,643,207✔
82
}
83

84
void scatter(Particle& p)
1,669,415,704✔
85
{
86
  data::mg.macro_xs_[p.material()].sample_scatter(p.g_last(), p.g(), p.mu(),
1,669,415,704✔
87
    p.wgt(), p.current_seed(), p.mg_xs_cache().t, p.mg_xs_cache().a);
1,669,415,704✔
88

89
  // Rotate the angle
90
  p.u() = rotate_angle(p.u(), p.mu(), nullptr, p.current_seed());
1,669,415,704✔
91

92
  // Update energy value for downstream compatability (in tallying)
93
  p.E() = data::mg.energy_bin_avg_[p.g()];
1,669,415,704✔
94

95
  // Set event component
96
  p.event() = TallyEvent::SCATTER;
1,669,415,704✔
97
}
1,669,415,704✔
98

99
void create_fission_sites(Particle& p)
1,777,796,823✔
100
{
101
  // If uniform fission source weighting is turned on, we increase or decrease
102
  // the expected number of fission sites produced
103
  double weight = settings::ufs_on ? ufs_get_weight(p) : 1.0;
1,777,796,823!
104

105
  // Determine the expected number of neutrons produced
106
  double nu_t = p.wgt() / simulation::keff * weight * p.macro_xs().nu_fission /
1,777,796,823✔
107
                p.macro_xs().total;
1,777,796,823✔
108

109
  // Sample the number of neutrons produced
110
  int nu = static_cast<int>(nu_t);
1,777,796,823✔
111
  if (prn(p.current_seed()) <= (nu_t - int(nu_t))) {
1,777,796,823✔
112
    nu++;
111,087,570✔
113
  }
114

115
  // If no neutrons were produced then don't continue
116
  if (nu == 0)
1,777,796,823✔
117
    return;
1,666,708,615✔
118

119
  // Initialize the counter of delayed neutrons encountered for each delayed
120
  // group.
121
  double nu_d[MAX_DELAYED_GROUPS] = {0.};
111,088,208✔
122

123
  // Clear out particle's nu fission bank
124
  p.nu_bank().clear();
111,088,208✔
125

126
  p.fission() = true;
111,088,208✔
127

128
  // Determine whether to place fission sites into the shared fission bank
129
  // or the secondary particle bank.
130
  bool use_fission_bank = (settings::run_mode == RunMode::EIGENVALUE);
111,088,208✔
131

132
  // Counter for the number of fission sites successfully stored to the shared
133
  // fission bank or the secondary particle bank
134
  int n_sites_stored;
111,088,208✔
135

136
  for (n_sites_stored = 0; n_sites_stored < nu; n_sites_stored++) {
222,178,143✔
137
    // Initialize fission site object with particle data
138
    SourceSite site;
111,089,935✔
139
    site.r = p.r();
111,089,935✔
140
    site.particle = ParticleType::neutron();
111,089,935✔
141
    site.time = p.time();
111,089,935✔
142
    site.wgt = 1. / weight;
111,089,935✔
143

144
    // Sample the cosine of the angle, assuming fission neutrons are emitted
145
    // isotropically
146
    double mu = 2. * prn(p.current_seed()) - 1.;
111,089,935✔
147

148
    // Sample the azimuthal angle uniformly in [0, 2.pi)
149
    double phi = 2. * PI * prn(p.current_seed());
111,089,935✔
150
    site.u.x = mu;
111,089,935✔
151
    site.u.y = std::sqrt(1. - mu * mu) * std::cos(phi);
111,089,935✔
152
    site.u.z = std::sqrt(1. - mu * mu) * std::sin(phi);
111,089,935✔
153

154
    // Sample secondary energy distribution for the fission reaction
155
    int dg;
111,089,935✔
156
    int gout;
111,089,935✔
157
    data::mg.macro_xs_[p.material()].sample_fission_energy(
111,089,935✔
158
      p.g(), dg, gout, p.current_seed(), p.mg_xs_cache().t, p.mg_xs_cache().a);
111,089,935✔
159

160
    // Store the energy and delayed groups on the fission bank
161
    site.E = gout;
111,089,935✔
162

163
    // We add 1 to the delayed_group bc in MG, -1 is prompt, but in the rest
164
    // of the code, 0 is prompt.
165
    site.delayed_group = dg + 1;
111,089,935✔
166

167
    // If delayed product production, sample time of emission
168
    if (dg != -1) {
111,089,935✔
169
      auto& macro_xs = data::mg.macro_xs_[p.material()];
1,507✔
170
      double decay_rate =
1,507✔
171
        macro_xs.get_xs(MgxsType::DECAY_RATE, 0, nullptr, nullptr, &dg, 0, 0);
1,507✔
172
      site.time -= std::log(prn(p.current_seed())) / decay_rate;
1,507✔
173

174
      // Reject site if it exceeds time cutoff
175
      double t_cutoff = settings::time_cutoff[site.particle.transport_index()];
1,507✔
176
      if (site.time > t_cutoff) {
1,507✔
177
        continue;
737✔
178
      }
179
    }
180

181
    // Set parent and progeny ID
182
    site.parent_id = p.id();
111,089,198✔
183
    site.progeny_id = p.n_progeny()++;
111,089,198✔
184

185
    // Store fission site in bank
186
    if (use_fission_bank) {
111,089,198!
187
      int64_t idx = simulation::fission_bank.thread_safe_append(site);
111,089,198✔
188
      if (idx == -1) {
111,089,198!
189
        warning(
×
190
          "The shared fission bank is full. Additional fission sites created "
191
          "in this generation will not be banked. Results may be "
192
          "non-deterministic.");
193

194
        // Decrement number of particle progeny as storage was unsuccessful.
195
        // This step is needed so that the sum of all progeny is equal to the
196
        // size of the shared fission bank.
197
        p.n_progeny()--;
×
198

199
        // Break out of loop as no more sites can be added to fission bank
200
        break;
×
201
      }
202
    } else {
203
      p.secondary_bank().push_back(site);
×
204
      p.n_secondaries()++;
×
205
    }
206

207
    // Set the delayed group on the particle as well
208
    p.delayed_group() = dg + 1;
111,089,198✔
209

210
    // Increment the number of neutrons born delayed
211
    if (p.delayed_group() > 0) {
111,089,198✔
212
      nu_d[dg]++;
770✔
213
    }
214

215
    // Write fission particles to nuBank
216
    NuBank& nu_bank_entry = p.nu_bank().emplace_back();
111,089,198✔
217
    nu_bank_entry.wgt = site.wgt;
111,089,198✔
218
    nu_bank_entry.E = site.E;
111,089,198✔
219
    nu_bank_entry.delayed_group = site.delayed_group;
111,089,198✔
220
  }
221

222
  // If shared fission bank was full, and no fissions could be added,
223
  // set the particle fission flag to false.
224
  if (n_sites_stored == 0) {
111,088,208!
225
    p.fission() = false;
×
226
    return;
×
227
  }
228

229
  // Set nu to the number of fission sites successfully stored. If the fission
230
  // bank was not found to be full then these values are already equivalent.
231
  nu = n_sites_stored;
111,088,208✔
232

233
  // Store the total weight banked for analog fission tallies
234
  p.n_bank() = nu;
111,088,208✔
235
  p.wgt_bank() = nu / weight;
111,088,208✔
236
  for (size_t d = 0; d < MAX_DELAYED_GROUPS; d++) {
999,793,872✔
237
    p.n_delayed_bank(d) = nu_d[d];
888,705,664✔
238
  }
239
}
240

241
void absorption(Particle& p)
1,783,060,477✔
242
{
243
  if (settings::survival_biasing) {
1,783,060,477✔
244
    // Determine weight absorbed in survival biasing
245
    double wgt_absorb = p.wgt() * p.macro_xs().absorption / p.macro_xs().total;
4,219,259✔
246

247
    // Adjust weight of particle by the probability of absorption
248
    p.wgt() -= wgt_absorb;
4,219,259✔
249

250
    // Score implicit absorpion estimate of keff
251
    p.keff_tally_absorption() +=
4,219,259✔
252
      wgt_absorb * p.macro_xs().nu_fission / p.macro_xs().absorption;
4,219,259✔
253
  } else {
254
    if (p.macro_xs().absorption > prn(p.current_seed()) * p.macro_xs().total) {
1,778,841,218✔
255
      p.keff_tally_absorption() +=
113,644,773✔
256
        p.wgt() * p.macro_xs().nu_fission / p.macro_xs().absorption;
113,644,773✔
257
      p.wgt() = 0.0;
113,644,773✔
258
      p.event() = TallyEvent::ABSORB;
113,644,773✔
259
    }
260
  }
261
}
1,783,060,477✔
262

263
} // 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