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

openmc-dev / openmc / 21458205742

28 Jan 2026 10:40PM UTC coverage: 81.974% (-0.02%) from 81.994%
21458205742

Pull #3751

github

web-flow
Merge a14249281 into 008d58460
Pull Request #3751: Resolve conflict with weight windows and global russian roulette

17239 of 24006 branches covered (71.81%)

Branch coverage included in aggregate %.

87 of 100 new or added lines in 5 files covered. (87.0%)

94 existing lines in 2 files now uncovered.

55702 of 64975 relevant lines covered (85.73%)

44017310.75 hits per line

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

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

3
#include <stdexcept>
4

5
#include "xtensor/xarray.hpp"
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

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

34
  if (settings::weight_windows_on) {
1,783,060,477✔
35
    auto ww = search_weight_window(p);
3,057,087✔
36
    if (ww.is_valid()) {
3,057,087!
37
      if (settings::weight_window_checkpoint_collision)
3,057,087!
38
        apply_weight_window(p, ww);
3,057,087✔
39
    } else {
NEW
40
      apply_russian_roulette(p);
×
41
    }
42
  }
43

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

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

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

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

73
  // Sample a scattering event to determine the energy of the exiting neutron
74
  scatter(p);
1,669,415,704✔
75

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

81
void scatter(Particle& p)
1,669,415,704✔
82
{
83
  data::mg.macro_xs_[p.material()].sample_scatter(p.g_last(), p.g(), p.mu(),
2,147,483,647✔
84
    p.wgt(), p.current_seed(), p.mg_xs_cache().t, p.mg_xs_cache().a);
1,669,415,704✔
85

86
  // Rotate the angle
87
  p.u() = rotate_angle(p.u(), p.mu(), nullptr, p.current_seed());
1,669,415,704✔
88

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

92
  // Set event component
93
  p.event() = TallyEvent::SCATTER;
1,669,415,704✔
94
}
1,669,415,704✔
95

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

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

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

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

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

120
  // Clear out particle's nu fission bank
121
  p.nu_bank().clear();
111,088,208✔
122

123
  p.fission() = true;
111,088,208✔
124

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

129
  // Counter for the number of fission sites successfully stored to the shared
130
  // fission bank or the secondary particle bank
131
  int n_sites_stored;
132

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

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

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

151
    // Sample secondary energy distribution for the fission reaction
152
    int dg;
153
    int gout;
154
    data::mg.macro_xs_[p.material()].sample_fission_energy(
333,269,805✔
155
      p.g(), dg, gout, p.current_seed(), p.mg_xs_cache().t, p.mg_xs_cache().a);
222,179,870✔
156

157
    // Store the energy and delayed groups on the fission bank
158
    site.E = gout;
111,089,935✔
159

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

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

171
      // Reject site if it exceeds time cutoff
172
      double t_cutoff = settings::time_cutoff[static_cast<int>(site.particle)];
1,507✔
173
      if (site.time > t_cutoff) {
1,507✔
174
        continue;
737✔
175
      }
176
    }
177

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

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

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

196
        // Break out of loop as no more sites can be added to fission bank
197
        break;
×
198
      }
199
    } else {
200
      p.secondary_bank().push_back(site);
×
201
    }
202

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

206
    // Increment the number of neutrons born delayed
207
    if (p.delayed_group() > 0) {
111,089,198✔
208
      nu_d[dg]++;
770✔
209
    }
210

211
    // Write fission particles to nuBank
212
    NuBank& nu_bank_entry = p.nu_bank().emplace_back();
111,089,198✔
213
    nu_bank_entry.wgt = site.wgt;
111,089,198✔
214
    nu_bank_entry.E = site.E;
111,089,198✔
215
    nu_bank_entry.delayed_group = site.delayed_group;
111,089,198✔
216
  }
217

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

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

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

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

243
    // Adjust weight of particle by the probability of absorption
244
    p.wgt() -= wgt_absorb;
4,219,259✔
245

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

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