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

openmc-dev / openmc / 21887062786

10 Feb 2026 11:55PM UTC coverage: 81.899% (-0.1%) from 82.009%
21887062786

Pull #3493

github

web-flow
Merge 320b68711 into 3f20a5e22
Pull Request #3493: Implement vector fitting to replace external `vectfit` package

17361 of 24292 branches covered (71.47%)

Branch coverage included in aggregate %.

185 of 218 new or added lines in 3 files covered. (84.86%)

2770 existing lines in 69 files now uncovered.

56369 of 65733 relevant lines covered (85.75%)

51144917.16 hits per line

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

77.53
/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,620,964,070✔
27
{
28
  // Add to the collision counter for the particle
29
  p.n_collision()++;
1,620,964,070✔
30
  p.secondary_bank_index() = p.secondary_bank().size();
1,620,964,070✔
31

32
  // Sample the reaction type
33
  sample_reaction(p);
1,620,964,070✔
34

35
  if (settings::weight_window_checkpoint_collision)
1,620,964,070!
36
    apply_weight_windows(p);
1,620,964,070✔
37

38
  // Display information about collision
39
  if ((settings::verbosity >= 10) || p.trace()) {
1,620,964,070!
UNCOV
40
    write_message(fmt::format("    Energy Group = {}", p.g()), 1);
×
41
  }
42
}
1,620,964,070✔
43

44
void sample_reaction(Particle& p)
1,620,964,070✔
45
{
46
  // Create fission bank sites. Note that while a fission reaction is sampled,
47
  // it never actually "happens", i.e. the weight of the particle does not
48
  // change when sampling fission sites. The following block handles all
49
  // absorption (including fission)
50

51
  if (model::materials[p.material()]->fissionable()) {
1,620,964,070✔
52
    if (settings::run_mode == RunMode::EIGENVALUE ||
1,616,178,930!
UNCOV
53
        (settings::run_mode == RunMode::FIXED_SOURCE &&
×
54
          settings::create_fission_neutrons)) {
55
      create_fission_sites(p);
1,616,178,930✔
56
    }
57
  }
58

59
  // If survival biasing is being used, the following subroutine adjusts the
60
  // weight of the particle. Otherwise, it checks to see if absorption occurs.
61
  if (p.macro_xs().absorption > 0.) {
1,620,964,070!
62
    absorption(p);
1,620,964,070✔
63
  }
64
  if (!p.alive())
1,620,964,070✔
65
    return;
103,313,430✔
66

67
  // Sample a scattering event to determine the energy of the exiting neutron
68
  scatter(p);
1,517,650,640✔
69

70
  // Play Russian roulette if survival biasing is turned on
71
  if (settings::survival_biasing) {
1,517,650,640✔
72
    // if survival normalization is applicable, use normalized weight cutoff and
73
    // normalized weight survive
74
    if (settings::survival_normalization) {
3,835,690!
75
      if (p.wgt() < settings::weight_cutoff * p.wgt_born()) {
×
UNCOV
76
        russian_roulette(p, settings::weight_survive * p.wgt_born());
×
77
      }
78
    } else if (p.wgt() < settings::weight_cutoff) {
3,835,690✔
79
      russian_roulette(p, settings::weight_survive);
129,400✔
80
    }
81
  }
82
}
83

84
void scatter(Particle& p)
1,517,650,640✔
85
{
86
  data::mg.macro_xs_[p.material()].sample_scatter(p.g_last(), p.g(), p.mu(),
2,451,013,775✔
87
    p.wgt(), p.current_seed(), p.mg_xs_cache().t, p.mg_xs_cache().a);
1,517,650,640✔
88

89
  // Rotate the angle
90
  p.u() = rotate_angle(p.u(), p.mu(), nullptr, p.current_seed());
1,517,650,640✔
91

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

95
  // Set event component
96
  p.event() = TallyEvent::SCATTER;
1,517,650,640✔
97
}
1,517,650,640✔
98

99
void create_fission_sites(Particle& p)
1,616,178,930✔
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,616,178,930!
104

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

109
  // Sample the number of neutrons produced
110
  int nu = static_cast<int>(nu_t);
1,616,178,930✔
111
  if (prn(p.current_seed()) <= (nu_t - int(nu_t))) {
1,616,178,930✔
112
    nu++;
100,988,700✔
113
  }
114

115
  // If no neutrons were produced then don't continue
116
  if (nu == 0)
1,616,178,930✔
117
    return;
1,515,189,650✔
118

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

123
  // Clear out particle's nu fission bank
124
  p.nu_bank().clear();
100,989,280✔
125

126
  p.fission() = true;
100,989,280✔
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);
100,989,280✔
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;
135

136
  for (n_sites_stored = 0; n_sites_stored < nu; n_sites_stored++) {
201,980,130✔
137
    // Initialize fission site object with particle data
138
    SourceSite site;
100,990,850✔
139
    site.r = p.r();
100,990,850✔
140
    site.particle = ParticleType::neutron();
100,990,850✔
141
    site.time = p.time();
100,990,850✔
142
    site.wgt = 1. / weight;
100,990,850✔
143

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

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

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

160
    // Store the energy and delayed groups on the fission bank
161
    site.E = gout;
100,990,850✔
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;
100,990,850✔
166

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

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

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

185
    // Store fission site in bank
186
    if (use_fission_bank) {
100,990,180!
187
      int64_t idx = simulation::fission_bank.thread_safe_append(site);
100,990,180✔
188
      if (idx == -1) {
100,990,180!
UNCOV
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.
UNCOV
197
        p.n_progeny()--;
×
198

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

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

210
    // Increment the number of neutrons born delayed
211
    if (p.delayed_group() > 0) {
100,990,180✔
212
      nu_d[dg]++;
700✔
213
    }
214

215
    // Write fission particles to nuBank
216
    NuBank& nu_bank_entry = p.nu_bank().emplace_back();
100,990,180✔
217
    nu_bank_entry.wgt = site.wgt;
100,990,180✔
218
    nu_bank_entry.E = site.E;
100,990,180✔
219
    nu_bank_entry.delayed_group = site.delayed_group;
100,990,180✔
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) {
100,989,280!
UNCOV
225
    p.fission() = false;
×
UNCOV
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;
100,989,280✔
232

233
  // Store the total weight banked for analog fission tallies
234
  p.n_bank() = nu;
100,989,280✔
235
  p.wgt_bank() = nu / weight;
100,989,280✔
236
  for (size_t d = 0; d < MAX_DELAYED_GROUPS; d++) {
908,903,520✔
237
    p.n_delayed_bank(d) = nu_d[d];
807,914,240✔
238
  }
239
}
240

241
void absorption(Particle& p)
1,620,964,070✔
242
{
243
  if (settings::survival_biasing) {
1,620,964,070✔
244
    // Determine weight absorbed in survival biasing
245
    double wgt_absorb = p.wgt() * p.macro_xs().absorption / p.macro_xs().total;
3,835,690✔
246

247
    // Adjust weight of particle by the probability of absorption
248
    p.wgt() -= wgt_absorb;
3,835,690✔
249

250
    // Score implicit absorpion estimate of keff
251
    p.keff_tally_absorption() +=
3,835,690✔
252
      wgt_absorb * p.macro_xs().nu_fission / p.macro_xs().absorption;
3,835,690✔
253
  } else {
254
    if (p.macro_xs().absorption > prn(p.current_seed()) * p.macro_xs().total) {
1,617,128,380✔
255
      p.keff_tally_absorption() +=
206,626,860✔
256
        p.wgt() * p.macro_xs().nu_fission / p.macro_xs().absorption;
103,313,430✔
257
      p.wgt() = 0.0;
103,313,430✔
258
      p.event() = TallyEvent::ABSORB;
103,313,430✔
259
    }
260
  }
261
}
1,620,964,070✔
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