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

openmc-dev / openmc / 19076324204

04 Nov 2025 04:51PM UTC coverage: 82.008% (-3.1%) from 85.155%
19076324204

Pull #3252

github

web-flow
Merge b4a815914 into bd76fc056
Pull Request #3252: Adding vtkhdf option to write vtk data

16714 of 23236 branches covered (71.93%)

Branch coverage included in aggregate %.

61 of 66 new or added lines in 1 file covered. (92.42%)

3175 existing lines in 103 files now uncovered.

54243 of 63288 relevant lines covered (85.71%)

43026229.62 hits per line

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

77.84
/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_window_checkpoint_collision)
1,783,060,477!
35
    apply_weight_windows(p);
1,783,060,477✔
36

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

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

50
  if (model::materials[p.material()]->fissionable()) {
1,783,060,477✔
51
    if (settings::run_mode == RunMode::EIGENVALUE ||
1,777,796,823!
52
        (settings::run_mode == RunMode::FIXED_SOURCE &&
×
53
          settings::create_fission_neutrons)) {
54
      create_fission_sites(p);
1,777,796,823✔
55
    }
56
  }
57

58
  // If survival biasing is being used, the following subroutine adjusts the
59
  // weight of the particle. Otherwise, it checks to see if absorption occurs.
60
  if (p.macro_xs().absorption > 0.) {
1,783,060,477!
61
    absorption(p);
1,783,060,477✔
62
  }
63
  if (!p.alive())
1,783,060,477✔
64
    return;
113,644,773✔
65

66
  // Sample a scattering event to determine the energy of the exiting neutron
67
  scatter(p);
1,669,415,704✔
68

69
  // Play Russian roulette if survival biasing is turned on
70
  if (settings::survival_biasing) {
1,669,415,704✔
71
    // if survival normalization is applicable, use normalized weight cutoff and
72
    // normalized weight survive
73
    if (settings::survival_normalization) {
4,219,259!
74
      if (p.wgt() < settings::weight_cutoff * p.wgt_born()) {
×
75
        russian_roulette(p, settings::weight_survive * p.wgt_born());
×
76
      }
77
    } else if (p.wgt() < settings::weight_cutoff) {
4,219,259✔
78
      russian_roulette(p, settings::weight_survive);
142,340✔
79
    }
80
  }
81
}
82

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

© 2025 Coveralls, Inc