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

openmc-dev / openmc / 38478309103

13 Oct 2026 10:30PM UTC coverage: 81.445% (-0.03%) from 81.477%
38478309103

Pull #3682

github

web-flow
Merge 5814f63f9 into 3aec0edef
Pull Request #3682: Extend Kalbach Mann systematics to support incident photons

20131 of 29256 branches covered (68.81%)

Branch coverage included in aggregate %.

52 of 57 new or added lines in 4 files covered. (91.23%)

1495 existing lines in 26 files now uncovered.

62694 of 72439 relevant lines covered (86.55%)

42726717.55 hits per line

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

83.76
/src/reaction.cpp
1
#include "openmc/reaction.h"
2

3
#include <algorithm> // for remove_if
4
#include <string>
5
#include <unordered_map>
6
#include <utility> // for move
7

8
#include <fmt/core.h>
9

10
#include "openmc/chain.h"
11
#include "openmc/constants.h"
12
#include "openmc/endf.h"
13
#include "openmc/hdf5_interface.h"
14
#include "openmc/random_lcg.h"
15
#include "openmc/search.h"
16
#include "openmc/secondary_uncorrelated.h"
17
#include "openmc/settings.h"
18

19
namespace openmc {
20

21
//==============================================================================
22
// Reaction implementation
23
//==============================================================================
24

25
Reaction::Reaction(
1,073,732 ✔
26
  hid_t group, const vector<int>& temperatures, std::string name)
1,073,732 ✔
27
{
28
  if (attribute_exists(group, "q_reaction")) {
1,073,732 ✔
29
    read_attribute(group, "q_reaction", q_reaction_);
360 ✔
30
  } else {
31
    read_attribute(group, "Q_value", q_reaction_);
1,073,372 ✔
32
  }
33
  if (attribute_exists(group, "q_mass_difference")) {
1,073,732 !
UNCOV
34
    read_attribute(group, "q_mass_difference", q_mass_difference_);
×
35
  }
36
  if (attribute_exists(group, "breakup_flag")) {
1,073,732 !
UNCOV
37
    read_attribute(group, "breakup_flag", breakup_flag_);
×
38
  }
39
  read_attribute(group, "mt", mt_);
1,073,732 ✔
40
  int tmp;
1,073,732 ✔
41
  read_attribute(group, "center_of_mass", tmp);
1,073,732 ✔
42
  scatter_in_cm_ = (tmp == 1);
1,073,732 ✔
43

44
  // Checks if redudant attribute exists before loading
45
  // (for compatibiltiy with legacy .h5 libraries)
46
  if (attribute_exists(group, "redundant")) {
1,073,732 !
47
    read_attribute(group, "redundant", tmp);
1,073,732 ✔
48
    redundant_ = (tmp == 1);
1,073,732 ✔
49
  } else {
UNCOV
50
    redundant_ = false;
×
51
  }
52

53
  // Read cross section and threshold_idx data
54
  for (auto t : temperatures) {
2,147,632 ✔
55
    // Get group corresponding to temperature
56
    hid_t temp_group = open_group(group, fmt::format("{}K", t).c_str());
1,073,900 ✔
57
    hid_t dset = open_dataset(temp_group, "xs");
1,073,900 ✔
58

59
    // Get threshold index
60
    TemperatureXS xs;
1,073,900 ✔
61
    read_attribute(dset, "threshold_idx", xs.threshold);
1,073,900 ✔
62

63
    // Read cross section values
64
    read_dataset(dset, xs.value);
1,073,900 ✔
65
    close_dataset(dset);
1,073,900 ✔
66
    close_group(temp_group);
1,073,900 ✔
67

68
    // create new entry in xs vector
69
    xs_.push_back(std::move(xs));
1,073,900 ✔
70
  }
1,073,900 ✔
71

72
  // Read products
73
  for (const auto& name : group_names(group)) {
4,955,086 ✔
74
    if (name.rfind("product_", 0) == 0) {
3,881,354 ✔
75
      hid_t pgroup = open_group(group, name.c_str());
2,802,757 ✔
76
      products_.emplace_back(pgroup);
2,802,757 ✔
77
      close_group(pgroup);
2,802,757 ✔
78
    }
79
  }
1,073,732 ✔
80

81
  if (settings::use_decay_photons) {
1,073,732 ✔
82
    // Remove photon products for D1S method
83
    products_.erase(std::remove_if(products_.begin(), products_.end(),
904 ✔
84
                      [](const auto& p) { return p.particle_.is_photon(); }),
6,304 !
85
      products_.end());
904 ✔
86

87
    // Determine product for D1S method
88
    auto nuclide_it = data::chain_nuclide_map.find(name);
904 ✔
89
    if (nuclide_it != data::chain_nuclide_map.end()) {
904 !
90
      const auto& chain_nuc = data::chain_nuclides[nuclide_it->second];
904 ✔
91
      const auto& rx_products = chain_nuc->reaction_products();
904 ✔
92
      auto product_it = rx_products.find(mt_);
904 ✔
93
      if (product_it != rx_products.end()) {
904 ✔
94
        auto decay_products = product_it->second;
160 ✔
95
        for (const auto& decay_product : decay_products) {
320 ✔
96
          auto product_it = data::chain_nuclide_map.find(decay_product.name);
160 ✔
97
          if (product_it != data::chain_nuclide_map.end()) {
160 !
98
            const auto& product_nuc = data::chain_nuclides[product_it->second];
160 ✔
99
            if (product_nuc->photon_energy()) {
160 ✔
100
              products_.emplace_back(decay_product);
96 ✔
101
            }
102
          }
103
        }
104
      }
160 ✔
105
    }
106
  }
107
}
1,073,732 ✔
108

109
double Reaction::xs(int64_t i_temp, int64_t i_grid, double interp_factor) const
652,749,297 ✔
110
{
111
  // If energy is below threshold, return 0. Otherwise interpolate between
112
  // nearest grid points
113
  const auto& x = xs_[i_temp];
652,749,297 ✔
114
  return (i_grid < x.threshold)
652,749,297 ✔
115
           ? 0.0
652,749,297 ✔
116
           : (1.0 - interp_factor) * x.value[i_grid - x.threshold] +
350,818,300 ✔
117
               interp_factor * x.value[i_grid - x.threshold + 1];
350,818,300 ✔
118
}
119

120
double Reaction::xs(const NuclideMicroXS& micro) const
652,749,297 ✔
121
{
122
  return this->xs(micro.index_temp, micro.index_grid, micro.interp_factor);
652,749,297 ✔
123
}
124

125
double Reaction::collapse_rate(int64_t i_temp, span<const double> energy,
1,576 ✔
126
  span<const double> flux, const vector<double>& grid) const
127
{
128
  // Find index corresponding to first energy
129
  const auto& xs = xs_[i_temp].value;
1,576 ✔
130
  int i_low = lower_bound_index(grid.cbegin(), grid.cend(), energy.front());
1,576 ✔
131

132
  // Check for threshold and adjust starting point if necessary
133
  int j_start = 0;
1,576 ✔
134
  int i_threshold = xs_[i_temp].threshold;
1,576 ✔
135
  if (i_low < i_threshold) {
1,576 ✔
136
    i_low = i_threshold;
15,496 ✔
137
    while (energy[j_start + 1] < grid[i_low]) {
15,496 ✔
138
      ++j_start;
14,296 ✔
139
      if (j_start + 1 == energy.size())
14,296 !
140
        return 0.0;
141
    }
142
  }
143

144
  double xs_flux_sum = 0.0;
145

146
  for (int j = j_start; j < flux.size(); ++j) {
31,920 ✔
147
    double E_group_low = energy[j];
30,344 ✔
148
    double E_group_high = energy[j + 1];
30,344 ✔
149
    double flux_per_eV = flux[j] / (E_group_high - E_group_low);
30,344 ✔
150

151
    // Determine energy grid index corresponding to group high
152
    int i_high = i_low;
30,344 ✔
153
    while (grid[i_high + 1] < E_group_high && i_high + 1 < grid.size() - 1)
30,236,608 ✔
154
      ++i_high;
155

156
    // Loop over energy grid points within [E_group_low, E_group_high]
157
    for (; i_low <= i_high; ++i_low) {
30,266,952 ✔
158
      // Determine bounding grid energies and cross sections
159
      double E_l = grid[i_low];
30,236,608 !
160
      double E_r = grid[i_low + 1];
30,236,608 ✔
161
      if (E_l == E_r)
30,236,608 !
UNCOV
162
        continue;
×
163

164
      double xs_l = xs[i_low - i_threshold];
30,236,608 ✔
165
      double xs_r = xs[i_low + 1 - i_threshold];
30,236,608 ✔
166

167
      // Determine actual energies
168
      double E_low = std::max(E_group_low, E_l);
30,236,608 ✔
169
      double E_high = std::min(E_group_high, E_r);
30,236,608 ✔
170

171
      // Determine average cross section across segment
172
      double m = (xs_r - xs_l) / (E_r - E_l);
30,236,608 ✔
173
      double xs_low = xs_l + m * (E_low - E_l);
30,236,608 ✔
174
      double xs_high = xs_l + m * (E_high - E_l);
30,236,608 ✔
175
      double xs_avg = 0.5 * (xs_low + xs_high);
30,236,608 ✔
176

177
      // Add contribution from segment
178
      double dE = (E_high - E_low);
30,236,608 ✔
179
      xs_flux_sum += flux_per_eV * xs_avg * dE;
30,236,608 ✔
180
    }
181

182
    i_low = i_high;
30,344 ✔
183

184
    // Check for end of energy grid
185
    if (i_low + 1 == grid.size())
30,344 !
186
      break;
187
  }
188

189
  return xs_flux_sum;
190
}
191

192
//==============================================================================
193
// Non-member functions
194
//==============================================================================
195

196
std::unordered_map<int, std::string> REACTION_NAME_MAP {
197
  {SCORE_FLUX, "flux"},
198
  {SCORE_TOTAL, "total"},
199
  {SCORE_SCATTER, "scatter"},
200
  {SCORE_NU_SCATTER, "nu-scatter"},
201
  {SCORE_ABSORPTION, "absorption"},
202
  {SCORE_FISSION, "fission"},
203
  {SCORE_NU_FISSION, "nu-fission"},
204
  {SCORE_DECAY_RATE, "decay-rate"},
205
  {SCORE_DELAYED_NU_FISSION, "delayed-nu-fission"},
206
  {SCORE_PROMPT_NU_FISSION, "prompt-nu-fission"},
207
  {SCORE_KAPPA_FISSION, "kappa-fission"},
208
  {SCORE_CURRENT, "current"},
209
  {SCORE_EVENTS, "events"},
210
  {SCORE_INVERSE_VELOCITY, "inverse-velocity"},
211
  {SCORE_FISS_Q_PROMPT, "fission-q-prompt"},
212
  {SCORE_FISS_Q_RECOV, "fission-q-recoverable"},
213
  {SCORE_PULSE_HEIGHT, "pulse-height"},
214
  {SCORE_IFP_TIME_NUM, "ifp-time-numerator"},
215
  {SCORE_IFP_BETA_NUM, "ifp-beta-numerator"},
216
  {SCORE_IFP_DENOM, "ifp-denominator"},
217
  // Normal ENDF-based reactions
218
  {TOTAL_XS, "(n,total)"},
219
  {ELASTIC, "(n,elastic)"},
220
  {N_NONELASTIC, "(n,nonelastic)"},
221
  {N_LEVEL, "(n,level)"},
222
  {N_2ND, "(n,2nd)"},
223
  {N_2N, "(n,2n)"},
224
  {N_3N, "(n,3n)"},
225
  {N_FISSION, "(n,fission)"},
226
  {N_F, "(n,f)"},
227
  {N_NF, "(n,nf)"},
228
  {N_2NF, "(n,2nf)"},
229
  {N_NA, "(n,na)"},
230
  {N_N3A, "(n,n3a)"},
231
  {N_2NA, "(n,2na)"},
232
  {N_3NA, "(n,3na)"},
233
  {N_NP, "(n,np)"},
234
  {N_N2A, "(n,n2a)"},
235
  {N_2N2A, "(n,2n2a)"},
236
  {N_ND, "(n,nd)"},
237
  {N_NT, "(n,nt)"},
238
  {N_N3HE, "(n,n3He)"},
239
  {N_ND2A, "(n,nd2a)"},
240
  {N_NT2A, "(n,nt2a)"},
241
  {N_4N, "(n,4n)"},
242
  {N_3NF, "(n,3nf)"},
243
  {N_2NP, "(n,2np)"},
244
  {N_3NP, "(n,3np)"},
245
  {N_N2P, "(n,n2p)"},
246
  {N_NPA, "(n,npa)"},
247
  {N_NC, "(n,nc)"},
248
  {N_DISAPPEAR, "(n,disappear)"},
249
  {N_GAMMA, "(n,gamma)"},
250
  {N_P, "(n,p)"},
251
  {N_D, "(n,d)"},
252
  {N_T, "(n,t)"},
253
  {N_3HE, "(n,3He)"},
254
  {N_A, "(n,a)"},
255
  {N_2A, "(n,2a)"},
256
  {N_3A, "(n,3a)"},
257
  {N_2P, "(n,2p)"},
258
  {N_PA, "(n,pa)"},
259
  {N_T2A, "(n,t2a)"},
260
  {N_D2A, "(n,d2a)"},
261
  {N_PD, "(n,pd)"},
262
  {N_PT, "(n,pt)"},
263
  {N_DA, "(n,da)"},
264
  {N_5N, "(n,5n)"},
265
  {N_6N, "(n,6n)"},
266
  {N_2NT, "(n,2nt)"},
267
  {N_TA, "(n,ta)"},
268
  {N_4NP, "(n,4np)"},
269
  {N_3ND, "(n,3nd)"},
270
  {N_NDA, "(n,nda)"},
271
  {N_2NPA, "(n,2npa)"},
272
  {N_7N, "(n,7n)"},
273
  {N_8N, "(n,8n)"},
274
  {N_5NP, "(n,5np)"},
275
  {N_6NP, "(n,6np)"},
276
  {N_7NP, "(n,7np)"},
277
  {N_4NA, "(n,4na)"},
278
  {N_5NA, "(n,5na)"},
279
  {N_6NA, "(n,6na)"},
280
  {N_7NA, "(n,7na)"},
281
  {N_4ND, "(n,4nd)"},
282
  {N_5ND, "(n,5nd)"},
283
  {N_6ND, "(n,6nd)"},
284
  {N_3NT, "(n,3nt)"},
285
  {N_4NT, "(n,4nt)"},
286
  {N_5NT, "(n,5nt)"},
287
  {N_6NT, "(n,6nt)"},
288
  {N_2N3HE, "(n,2n3He)"},
289
  {N_3N3HE, "(n,3n3He)"},
290
  {N_4N3HE, "(n,4n3He)"},
291
  {N_3N2P, "(n,3n2p)"},
292
  {N_3N2A, "(n,3n2a)"},
293
  {N_3NPA, "(n,3npa)"},
294
  {N_DT, "(n,dt)"},
295
  {N_NPD, "(n,npd)"},
296
  {N_NPT, "(n,npt)"},
297
  {N_NDT, "(n,ndt)"},
298
  {N_NP3HE, "(n,np3He)"},
299
  {N_ND3HE, "(n,nd3He)"},
300
  {N_NT3HE, "(n,nt3He)"},
301
  {N_NTA, "(n,nta)"},
302
  {N_2N2P, "(n,2n2p)"},
303
  {N_P3HE, "(n,p3He)"},
304
  {N_D3HE, "(n,d3He)"},
305
  {N_3HEA, "(n,3Hea)"},
306
  {N_4N2P, "(n,4n2p)"},
307
  {N_4N2A, "(n,4n2a)"},
308
  {N_4NPA, "(n,4npa)"},
309
  {N_3P, "(n,3p)"},
310
  {N_N3P, "(n,n3p)"},
311
  {N_3N2PA, "(n,3n2pa)"},
312
  {N_5N2P, "(n,5n2p)"},
313
  {201, "(n,Xn)"},
314
  {202, "(n,Xgamma)"},
315
  {N_XP, "(n,Xp)"},
316
  {N_XD, "(n,Xd)"},
317
  {N_XT, "(n,Xt)"},
318
  {N_X3HE, "(n,X3He)"},
319
  {N_XA, "(n,Xa)"},
320
  {HEATING, "heating"},
321
  {DAMAGE_ENERGY, "damage-energy"},
322
  {PHOTON_TOTAL, "photon-total"},
323
  {COHERENT, "coherent-scatter"},
324
  {INCOHERENT, "incoherent-scatter"},
325
  {PAIR_PROD_ELEC, "pair-production-electron"},
326
  {PAIR_PROD, "pair-production"},
327
  {PAIR_PROD_NUC, "pair-production-nuclear"},
328
  {PHOTOELECTRIC, "photoelectric"},
329
  {N_PC, "(n,pc)"},
330
  {N_DC, "(n,dc)"},
331
  {N_TC, "(n,tc)"},
332
  {N_3HEC, "(n,3Hec)"},
333
  {N_AC, "(n,ac)"},
334
  {N_2NC, "(n,2nc)"},
335
  {HEATING_LOCAL, "heating-local"},
336
};
337

338
std::unordered_map<std::string, int> REACTION_TYPE_MAP;
339

340
void initialize_maps()
5,184 ✔
341
{
342
  // Add level reactions to name map
343
  for (int level = 0; level <= 48; ++level) {
259,200 ✔
344
    if (level >= 1 && level <= 40) {
254,016 ✔
345
      REACTION_NAME_MAP[50 + level] = fmt::format("(n,n{})", level);
207,360 ✔
346
    }
347
    REACTION_NAME_MAP[600 + level] = fmt::format("(n,p{})", level);
254,016 ✔
348
    REACTION_NAME_MAP[650 + level] = fmt::format("(n,d{})", level);
254,016 ✔
349
    REACTION_NAME_MAP[700 + level] = fmt::format("(n,t{})", level);
254,016 ✔
350
    REACTION_NAME_MAP[750 + level] = fmt::format("(n,3He{})", level);
254,016 ✔
351
    REACTION_NAME_MAP[800 + level] = fmt::format("(n,a{})", level);
254,016 ✔
352
    if (level <= 15) {
254,016 ✔
353
      REACTION_NAME_MAP[875 + level] = fmt::format("(n,2n{})", level);
82,944 ✔
354
    }
355
  }
356

357
  // Create photoelectric subshells
358
  for (int mt = 534; mt <= 572; ++mt) {
207,360 ✔
359
    REACTION_NAME_MAP[mt] =
404,352 ✔
360
      fmt::format("photoelectric-{}", SUBSHELLS[mt - 534]);
379,587 ✔
361
  }
362

363
  // Invert name map to create type map
364
  for (const auto& kv : REACTION_NAME_MAP) {
2,483,136 ✔
365
    REACTION_TYPE_MAP[kv.second] = kv.first;
2,477,952 ✔
366
  }
367

368
  // Alternate names
369
  REACTION_TYPE_MAP["elastic"] = ELASTIC;
10,368 ✔
370
  REACTION_TYPE_MAP["n2n"] = N_2N;
10,368 ✔
371
  REACTION_TYPE_MAP["n3n"] = N_3N;
10,368 ✔
372
  REACTION_TYPE_MAP["n4n"] = N_4N;
10,368 ✔
373
  REACTION_TYPE_MAP["H1-production"] = N_XP;
10,368 ✔
374
  REACTION_TYPE_MAP["H2-production"] = N_XD;
10,368 ✔
375
  REACTION_TYPE_MAP["H3-production"] = N_XT;
10,368 ✔
376
  REACTION_TYPE_MAP["He3-production"] = N_X3HE;
10,368 ✔
377
  REACTION_TYPE_MAP["He4-production"] = N_XA;
10,368 ✔
378
}
5,184 ✔
379

380
std::string reaction_name(int mt)
45,285 ✔
381
{
382
  // Initialize remainder of name map and all of type map
383
  if (REACTION_TYPE_MAP.empty())
45,285 !
UNCOV
384
    initialize_maps();
×
385

386
  // Get reaction name from map
387
  auto it = REACTION_NAME_MAP.find(mt);
45,285 !
388
  if (it != REACTION_NAME_MAP.end()) {
45,285 !
389
    return it->second;
45,285 ✔
390
  } else {
UNCOV
391
    return fmt::format("MT={}", mt);
×
392
  }
393
}
394

395
int reaction_tally_mt(std::string name)
27,428 ✔
396
{
397
  // All "total" scores should map to the special SCORE_TOTAL
398
  if (name == "total" || name == "(n,total)" || name == "photon-total")
77,934 !
399
    return SCORE_TOTAL;
2,175 ✔
400

401
  // All fission scores should map to the special SCORE_FISSION
402
  if (name == "fission" || name == "(n,fission)")
48,844 !
403
    return SCORE_FISSION;
1,662 ✔
404

405
  // Delegate everything else to reaction_mt()
406
  return reaction_mt(name);
23,591 ✔
407
}
408

409
int reaction_mt(const std::string& name)
51,239 ✔
410
{
411
  // Initialize maps if needed
412
  if (REACTION_TYPE_MAP.empty())
51,239 ✔
413
    initialize_maps();
5,184 ✔
414

415
  // Look up directly in type map (no score indirection)
416
  auto it = REACTION_TYPE_MAP.find(name);
51,239 ✔
417
  if (it != REACTION_TYPE_MAP.end()) {
51,239 ✔
418
    int mt = it->second;
51,219 ✔
419
    return mt;
51,219 ✔
420
  }
421

422
  // Assume the given string is an MT number
423
  int MT = 0;
20 ✔
424
  try {
20 ✔
425
    MT = std::stoi(name);
20 ✔
UNCOV
426
  } catch (const std::invalid_argument& ex) {
×
UNCOV
427
    throw std::invalid_argument("Unknown reaction name \"" + name + "\".");
×
UNCOV
428
  }
×
429
  if (MT < 1)
20 !
UNCOV
430
    throw std::invalid_argument("Unknown reaction name \"" + name + "\".");
×
431
  return MT;
432
}
433

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

© 2026 Coveralls, Inc