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

openmc-dev / openmc / 13183515203

06 Feb 2025 04:36PM UTC coverage: 82.601% (-2.3%) from 84.867%
13183515203

Pull #3087

github

web-flow
Merge d68c72d5e into 6e0f156d3
Pull Request #3087: wheel building with scikit build core

107123 of 129687 relevant lines covered (82.6%)

12608333.34 hits per line

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

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

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

7
#include <fmt/core.h>
8

9
#include "openmc/constants.h"
10
#include "openmc/endf.h"
11
#include "openmc/hdf5_interface.h"
12
#include "openmc/random_lcg.h"
13
#include "openmc/search.h"
14
#include "openmc/secondary_uncorrelated.h"
15

16
namespace openmc {
17

18
//==============================================================================
19
// Reaction implementation
20
//==============================================================================
21

22
Reaction::Reaction(hid_t group, const vector<int>& temperatures)
775,621✔
23
{
24
  read_attribute(group, "Q_value", q_value_);
775,621✔
25
  read_attribute(group, "mt", mt_);
775,621✔
26
  int tmp;
27
  read_attribute(group, "center_of_mass", tmp);
775,621✔
28
  scatter_in_cm_ = (tmp == 1);
775,621✔
29

30
  // Checks if redudant attribute exists before loading
31
  // (for compatibiltiy with legacy .h5 libraries)
32
  if (attribute_exists(group, "redundant")) {
775,621✔
33
    read_attribute(group, "redundant", tmp);
775,621✔
34
    redundant_ = (tmp == 1);
775,621✔
35
  } else {
36
    redundant_ = false;
×
37
  }
38

39
  // Read cross section and threshold_idx data
40
  for (auto t : temperatures) {
1,551,494✔
41
    // Get group corresponding to temperature
42
    hid_t temp_group = open_group(group, fmt::format("{}K", t).c_str());
775,873✔
43
    hid_t dset = open_dataset(temp_group, "xs");
775,873✔
44

45
    // Get threshold index
46
    TemperatureXS xs;
775,873✔
47
    read_attribute(dset, "threshold_idx", xs.threshold);
775,873✔
48

49
    // Read cross section values
50
    read_dataset(dset, xs.value);
775,873✔
51
    close_dataset(dset);
775,873✔
52
    close_group(temp_group);
775,873✔
53

54
    // create new entry in xs vector
55
    xs_.push_back(std::move(xs));
775,873✔
56
  }
775,873✔
57

58
  // Read products
59
  for (const auto& name : group_names(group)) {
3,513,083✔
60
    if (name.rfind("product_", 0) == 0) {
2,737,462✔
61
      hid_t pgroup = open_group(group, name.c_str());
1,957,302✔
62
      products_.emplace_back(pgroup);
1,957,302✔
63
      close_group(pgroup);
1,957,302✔
64
    }
65
  }
775,621✔
66
}
775,621✔
67

68
double Reaction::xs(
584,648,244✔
69
  gsl::index i_temp, gsl::index i_grid, double interp_factor) const
70
{
71
  // If energy is below threshold, return 0. Otherwise interpolate between
72
  // nearest grid points
73
  const auto& x = xs_[i_temp];
584,648,244✔
74
  return (i_grid < x.threshold)
584,648,244✔
75
           ? 0.0
584,648,244✔
76
           : (1.0 - interp_factor) * x.value[i_grid - x.threshold] +
355,298,785✔
77
               interp_factor * x.value[i_grid - x.threshold + 1];
584,648,244✔
78
}
79

80
double Reaction::xs(const NuclideMicroXS& micro) const
584,648,244✔
81
{
82
  return this->xs(micro.index_temp, micro.index_grid, micro.interp_factor);
584,648,244✔
83
}
84

85
double Reaction::collapse_rate(gsl::index i_temp,
×
86
  gsl::span<const double> energy, gsl::span<const double> flux,
87
  const vector<double>& grid) const
88
{
89
  // Find index corresponding to first energy
90
  const auto& xs = xs_[i_temp].value;
×
91
  int i_low = lower_bound_index(grid.cbegin(), grid.cend(), energy.front());
×
92

93
  // Check for threshold and adjust starting point if necessary
94
  int j_start = 0;
×
95
  int i_threshold = xs_[i_temp].threshold;
×
96
  if (i_low < i_threshold) {
×
97
    i_low = i_threshold;
×
98
    while (energy[j_start + 1] < grid[i_low]) {
×
99
      ++j_start;
×
100
      if (j_start + 1 == energy.size())
×
101
        return 0.0;
×
102
    }
103
  }
104

105
  double xs_flux_sum = 0.0;
×
106

107
  for (int j = j_start; j < flux.size(); ++j) {
×
108
    double E_group_low = energy[j];
×
109
    double E_group_high = energy[j + 1];
×
110
    double flux_per_eV = flux[j] / (E_group_high - E_group_low);
×
111

112
    // Determine energy grid index corresponding to group high
113
    int i_high = i_low;
×
114
    while (grid[i_high + 1] < E_group_high && i_high + 1 < grid.size() - 1)
×
115
      ++i_high;
×
116

117
    // Loop over energy grid points within [E_group_low, E_group_high]
118
    for (; i_low <= i_high; ++i_low) {
×
119
      // Determine bounding grid energies and cross sections
120
      double E_l = grid[i_low];
×
121
      double E_r = grid[i_low + 1];
×
122
      if (E_l == E_r)
×
123
        continue;
×
124

125
      double xs_l = xs[i_low - i_threshold];
×
126
      double xs_r = xs[i_low + 1 - i_threshold];
×
127

128
      // Determine actual energies
129
      double E_low = std::max(E_group_low, E_l);
×
130
      double E_high = std::min(E_group_high, E_r);
×
131

132
      // Determine average cross section across segment
133
      double m = (xs_r - xs_l) / (E_r - E_l);
×
134
      double xs_low = xs_l + m * (E_low - E_l);
×
135
      double xs_high = xs_l + m * (E_high - E_l);
×
136
      double xs_avg = 0.5 * (xs_low + xs_high);
×
137

138
      // Add contribution from segment
139
      double dE = (E_high - E_low);
×
140
      xs_flux_sum += flux_per_eV * xs_avg * dE;
×
141
    }
142

143
    i_low = i_high;
×
144

145
    // Check for end of energy grid
146
    if (i_low + 1 == grid.size())
×
147
      break;
×
148
  }
149

150
  return xs_flux_sum;
×
151
}
152

153
//==============================================================================
154
// Non-member functions
155
//==============================================================================
156

157
std::unordered_map<int, std::string> REACTION_NAME_MAP {
158
  {SCORE_FLUX, "flux"},
159
  {SCORE_TOTAL, "total"},
160
  {SCORE_SCATTER, "scatter"},
161
  {SCORE_NU_SCATTER, "nu-scatter"},
162
  {SCORE_ABSORPTION, "absorption"},
163
  {SCORE_FISSION, "fission"},
164
  {SCORE_NU_FISSION, "nu-fission"},
165
  {SCORE_DECAY_RATE, "decay-rate"},
166
  {SCORE_DELAYED_NU_FISSION, "delayed-nu-fission"},
167
  {SCORE_PROMPT_NU_FISSION, "prompt-nu-fission"},
168
  {SCORE_KAPPA_FISSION, "kappa-fission"},
169
  {SCORE_CURRENT, "current"},
170
  {SCORE_EVENTS, "events"},
171
  {SCORE_INVERSE_VELOCITY, "inverse-velocity"},
172
  {SCORE_FISS_Q_PROMPT, "fission-q-prompt"},
173
  {SCORE_FISS_Q_RECOV, "fission-q-recoverable"},
174
  {SCORE_PULSE_HEIGHT, "pulse-height"},
175
  // Normal ENDF-based reactions
176
  {TOTAL_XS, "(n,total)"},
177
  {ELASTIC, "(n,elastic)"},
178
  {N_LEVEL, "(n,level)"},
179
  {N_2ND, "(n,2nd)"},
180
  {N_2N, "(n,2n)"},
181
  {N_3N, "(n,3n)"},
182
  {N_FISSION, "(n,fission)"},
183
  {N_F, "(n,f)"},
184
  {N_NF, "(n,nf)"},
185
  {N_2NF, "(n,2nf)"},
186
  {N_NA, "(n,na)"},
187
  {N_N3A, "(n,n3a)"},
188
  {N_2NA, "(n,2na)"},
189
  {N_3NA, "(n,3na)"},
190
  {N_NP, "(n,np)"},
191
  {N_N2A, "(n,n2a)"},
192
  {N_2N2A, "(n,2n2a)"},
193
  {N_ND, "(n,nd)"},
194
  {N_NT, "(n,nt)"},
195
  {N_N3HE, "(n,n3He)"},
196
  {N_ND2A, "(n,nd2a)"},
197
  {N_NT2A, "(n,nt2a)"},
198
  {N_4N, "(n,4n)"},
199
  {N_3NF, "(n,3nf)"},
200
  {N_2NP, "(n,2np)"},
201
  {N_3NP, "(n,3np)"},
202
  {N_N2P, "(n,n2p)"},
203
  {N_NPA, "(n,npa)"},
204
  {N_NC, "(n,nc)"},
205
  {N_DISAPPEAR, "(n,disappear)"},
206
  {N_GAMMA, "(n,gamma)"},
207
  {N_P, "(n,p)"},
208
  {N_D, "(n,d)"},
209
  {N_T, "(n,t)"},
210
  {N_3HE, "(n,3He)"},
211
  {N_A, "(n,a)"},
212
  {N_2A, "(n,2a)"},
213
  {N_3A, "(n,3a)"},
214
  {N_2P, "(n,2p)"},
215
  {N_PA, "(n,pa)"},
216
  {N_T2A, "(n,t2a)"},
217
  {N_D2A, "(n,d2a)"},
218
  {N_PD, "(n,pd)"},
219
  {N_PT, "(n,pt)"},
220
  {N_DA, "(n,da)"},
221
  {N_5N, "(n,5n)"},
222
  {N_6N, "(n,6n)"},
223
  {N_2NT, "(n,2nt)"},
224
  {N_TA, "(n,ta)"},
225
  {N_4NP, "(n,4np)"},
226
  {N_3ND, "(n,3nd)"},
227
  {N_NDA, "(n,nda)"},
228
  {N_2NPA, "(n,2npa)"},
229
  {N_7N, "(n,7n)"},
230
  {N_8N, "(n,8n)"},
231
  {N_5NP, "(n,5np)"},
232
  {N_6NP, "(n,6np)"},
233
  {N_7NP, "(n,7np)"},
234
  {N_4NA, "(n,4na)"},
235
  {N_5NA, "(n,5na)"},
236
  {N_6NA, "(n,6na)"},
237
  {N_7NA, "(n,7na)"},
238
  {N_4ND, "(n,4nd)"},
239
  {N_5ND, "(n,5nd)"},
240
  {N_6ND, "(n,6nd)"},
241
  {N_3NT, "(n,3nt)"},
242
  {N_4NT, "(n,4nt)"},
243
  {N_5NT, "(n,5nt)"},
244
  {N_6NT, "(n,6nt)"},
245
  {N_2N3HE, "(n,2n3He)"},
246
  {N_3N3HE, "(n,3n3He)"},
247
  {N_4N3HE, "(n,4n3He)"},
248
  {N_3N2P, "(n,3n2p)"},
249
  {N_3N2A, "(n,3n2a)"},
250
  {N_3NPA, "(n,3npa)"},
251
  {N_DT, "(n,dt)"},
252
  {N_NPD, "(n,npd)"},
253
  {N_NPT, "(n,npt)"},
254
  {N_NDT, "(n,ndt)"},
255
  {N_NP3HE, "(n,np3He)"},
256
  {N_ND3HE, "(n,nd3He)"},
257
  {N_NT3HE, "(n,nt3He)"},
258
  {N_NTA, "(n,nta)"},
259
  {N_2N2P, "(n,2n2p)"},
260
  {N_P3HE, "(n,p3He)"},
261
  {N_D3HE, "(n,d3He)"},
262
  {N_3HEA, "(n,3Hea)"},
263
  {N_4N2P, "(n,4n2p)"},
264
  {N_4N2A, "(n,4n2a)"},
265
  {N_4NPA, "(n,4npa)"},
266
  {N_3P, "(n,3p)"},
267
  {N_N3P, "(n,n3p)"},
268
  {N_3N2PA, "(n,3n2pa)"},
269
  {N_5N2P, "(n,5n2p)"},
270
  {201, "(n,Xn)"},
271
  {202, "(n,Xgamma)"},
272
  {N_XP, "(n,Xp)"},
273
  {N_XD, "(n,Xd)"},
274
  {N_XT, "(n,Xt)"},
275
  {N_X3HE, "(n,X3He)"},
276
  {N_XA, "(n,Xa)"},
277
  {HEATING, "heating"},
278
  {DAMAGE_ENERGY, "damage-energy"},
279
  {COHERENT, "coherent-scatter"},
280
  {INCOHERENT, "incoherent-scatter"},
281
  {PAIR_PROD_ELEC, "pair-production-electron"},
282
  {PAIR_PROD, "pair-production"},
283
  {PAIR_PROD_NUC, "pair-production-nuclear"},
284
  {PHOTOELECTRIC, "photoelectric"},
285
  {N_PC, "(n,pc)"},
286
  {N_DC, "(n,dc)"},
287
  {N_TC, "(n,tc)"},
288
  {N_3HEC, "(n,3Hec)"},
289
  {N_AC, "(n,ac)"},
290
  {N_2NC, "(n,2nc)"},
291
  {HEATING_LOCAL, "heating-local"},
292
};
293

294
std::unordered_map<std::string, int> REACTION_TYPE_MAP;
295

296
void initialize_maps()
3,031✔
297
{
298
  // Add level reactions to name map
299
  for (int level = 0; level <= 48; ++level) {
151,550✔
300
    if (level >= 1 && level <= 40) {
148,519✔
301
      REACTION_NAME_MAP[50 + level] = fmt::format("(n,n{})", level);
121,240✔
302
    }
303
    REACTION_NAME_MAP[600 + level] = fmt::format("(n,p{})", level);
148,519✔
304
    REACTION_NAME_MAP[650 + level] = fmt::format("(n,d{})", level);
148,519✔
305
    REACTION_NAME_MAP[700 + level] = fmt::format("(n,t{})", level);
148,519✔
306
    REACTION_NAME_MAP[750 + level] = fmt::format("(n,3He{})", level);
148,519✔
307
    REACTION_NAME_MAP[800 + level] = fmt::format("(n,a{})", level);
148,519✔
308
    if (level <= 15) {
148,519✔
309
      REACTION_NAME_MAP[875 + level] = fmt::format("(n,2n{})", level);
48,496✔
310
    }
311
  }
312

313
  // Create photoelectric subshells
314
  for (int mt = 534; mt <= 572; ++mt) {
121,240✔
315
    REACTION_NAME_MAP[mt] =
138,762✔
316
      fmt::format("photoelectric, {} subshell", SUBSHELLS[mt - 534]);
334,074✔
317
  }
318

319
  // Invert name map to create type map
320
  for (const auto& kv : REACTION_NAME_MAP) {
1,436,694✔
321
    REACTION_TYPE_MAP[kv.second] = kv.first;
1,433,663✔
322
  }
323
}
3,031✔
324

325
std::string reaction_name(int mt)
54,293✔
326
{
327
  // Initialize remainder of name map and all of type map
328
  if (REACTION_TYPE_MAP.empty())
54,293✔
329
    initialize_maps();
36✔
330

331
  // Get reaction name from map
332
  auto it = REACTION_NAME_MAP.find(mt);
54,293✔
333
  if (it != REACTION_NAME_MAP.end()) {
54,293✔
334
    return it->second;
54,293✔
335
  } else {
336
    return fmt::format("MT={}", mt);
×
337
  }
338
}
339

340
int reaction_type(std::string name)
33,330✔
341
{
342
  // Initialize remainder of name map and all of type map
343
  if (REACTION_TYPE_MAP.empty())
33,330✔
344
    initialize_maps();
2,995✔
345

346
  // (n,total) exists in REACTION_TYPE_MAP for MT=1, but we need this to return
347
  // the special SCORE_TOTAL score
348
  if (name == "(n,total)")
33,330✔
349
    return SCORE_TOTAL;
×
350

351
  // Check if type map has an entry for this reaction name
352
  auto it = REACTION_TYPE_MAP.find(name);
33,330✔
353
  if (it != REACTION_TYPE_MAP.end()) {
33,330✔
354
    return it->second;
33,078✔
355
  }
356

357
  // Alternate names for several reactions
358
  if (name == "elastic") {
252✔
359
    return ELASTIC;
143✔
360
  } else if (name == "n2n") {
109✔
361
    return N_2N;
×
362
  } else if (name == "n3n") {
109✔
363
    return N_3N;
×
364
  } else if (name == "n4n") {
109✔
365
    return N_4N;
×
366
  } else if (name == "H1-production") {
109✔
367
    return N_XP;
17✔
368
  } else if (name == "H2-production") {
92✔
369
    return N_XD;
17✔
370
  } else if (name == "H3-production") {
75✔
371
    return N_XT;
17✔
372
  } else if (name == "He3-production") {
58✔
373
    return N_X3HE;
41✔
374
  } else if (name == "He4-production") {
17✔
375
    return N_XA;
17✔
376
  }
377

378
  // Assume the given string is a reaction MT number.  Make sure it's a natural
379
  // number then return.
380
  int MT = 0;
×
381
  try {
382
    MT = std::stoi(name);
×
383
  } catch (const std::invalid_argument& ex) {
×
384
    throw std::invalid_argument(
×
385
      "Invalid tally score \"" + name +
×
386
      "\". See the docs "
387
      "for details: "
388
      "https://docs.openmc.org/en/stable/usersguide/tallies.html#scores");
×
389
  }
×
390
  if (MT < 1)
×
391
    throw std::invalid_argument(
×
392
      "Invalid tally score \"" + name +
×
393
      "\". See the docs "
394
      "for details: "
395
      "https://docs.openmc.org/en/stable/usersguide/tallies.html#scores");
×
396
  return MT;
×
397
}
398

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