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

openmc-dev / openmc / 13375665600

17 Feb 2025 05:23PM UTC coverage: 84.967% (-0.005%) from 84.972%
13375665600

Pull #3304

github

web-flow
Merge 254c4c864 into a5b26de04
Pull Request #3304: Build NCrystal from Source for OpenMC

50234 of 59122 relevant lines covered (84.97%)

35412515.34 hits per line

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

84.33
/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)
1,315,842✔
23
{
24
  read_attribute(group, "Q_value", q_value_);
1,315,842✔
25
  read_attribute(group, "mt", mt_);
1,315,842✔
26
  int tmp;
27
  read_attribute(group, "center_of_mass", tmp);
1,315,842✔
28
  scatter_in_cm_ = (tmp == 1);
1,315,842✔
29

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

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

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

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

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

58
  // Read products
59
  for (const auto& name : group_names(group)) {
6,061,685✔
60
    if (name.rfind("product_", 0) == 0) {
4,745,843✔
61
      hid_t pgroup = open_group(group, name.c_str());
3,423,535✔
62
      products_.emplace_back(pgroup);
3,423,535✔
63
      close_group(pgroup);
3,423,535✔
64
    }
65
  }
1,315,842✔
66
}
1,315,842✔
67

68
double Reaction::xs(
810,350,588✔
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];
810,350,588✔
74
  return (i_grid < x.threshold)
810,350,588✔
75
           ? 0.0
810,350,588✔
76
           : (1.0 - interp_factor) * x.value[i_grid - x.threshold] +
473,513,689✔
77
               interp_factor * x.value[i_grid - x.threshold + 1];
810,350,588✔
78
}
79

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

85
double Reaction::collapse_rate(gsl::index i_temp,
816✔
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;
816✔
91
  int i_low = lower_bound_index(grid.cbegin(), grid.cend(), energy.front());
816✔
92

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

105
  double xs_flux_sum = 0.0;
816✔
106

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

112
    // Determine energy grid index corresponding to group high
113
    int i_high = i_low;
4,788✔
114
    while (grid[i_high + 1] < E_group_high && i_high + 1 < grid.size() - 1)
30,743,544✔
115
      ++i_high;
30,738,756✔
116

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

125
      double xs_l = xs[i_low - i_threshold];
30,743,544✔
126
      double xs_r = xs[i_low + 1 - i_threshold];
30,743,544✔
127

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

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

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

143
    i_low = i_high;
4,788✔
144

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

150
  return xs_flux_sum;
816✔
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_NONELASTIC, "(n,nonelastic)"},
179
  {N_LEVEL, "(n,level)"},
180
  {N_2ND, "(n,2nd)"},
181
  {N_2N, "(n,2n)"},
182
  {N_3N, "(n,3n)"},
183
  {N_FISSION, "(n,fission)"},
184
  {N_F, "(n,f)"},
185
  {N_NF, "(n,nf)"},
186
  {N_2NF, "(n,2nf)"},
187
  {N_NA, "(n,na)"},
188
  {N_N3A, "(n,n3a)"},
189
  {N_2NA, "(n,2na)"},
190
  {N_3NA, "(n,3na)"},
191
  {N_NP, "(n,np)"},
192
  {N_N2A, "(n,n2a)"},
193
  {N_2N2A, "(n,2n2a)"},
194
  {N_ND, "(n,nd)"},
195
  {N_NT, "(n,nt)"},
196
  {N_N3HE, "(n,n3He)"},
197
  {N_ND2A, "(n,nd2a)"},
198
  {N_NT2A, "(n,nt2a)"},
199
  {N_4N, "(n,4n)"},
200
  {N_3NF, "(n,3nf)"},
201
  {N_2NP, "(n,2np)"},
202
  {N_3NP, "(n,3np)"},
203
  {N_N2P, "(n,n2p)"},
204
  {N_NPA, "(n,npa)"},
205
  {N_NC, "(n,nc)"},
206
  {N_DISAPPEAR, "(n,disappear)"},
207
  {N_GAMMA, "(n,gamma)"},
208
  {N_P, "(n,p)"},
209
  {N_D, "(n,d)"},
210
  {N_T, "(n,t)"},
211
  {N_3HE, "(n,3He)"},
212
  {N_A, "(n,a)"},
213
  {N_2A, "(n,2a)"},
214
  {N_3A, "(n,3a)"},
215
  {N_2P, "(n,2p)"},
216
  {N_PA, "(n,pa)"},
217
  {N_T2A, "(n,t2a)"},
218
  {N_D2A, "(n,d2a)"},
219
  {N_PD, "(n,pd)"},
220
  {N_PT, "(n,pt)"},
221
  {N_DA, "(n,da)"},
222
  {N_5N, "(n,5n)"},
223
  {N_6N, "(n,6n)"},
224
  {N_2NT, "(n,2nt)"},
225
  {N_TA, "(n,ta)"},
226
  {N_4NP, "(n,4np)"},
227
  {N_3ND, "(n,3nd)"},
228
  {N_NDA, "(n,nda)"},
229
  {N_2NPA, "(n,2npa)"},
230
  {N_7N, "(n,7n)"},
231
  {N_8N, "(n,8n)"},
232
  {N_5NP, "(n,5np)"},
233
  {N_6NP, "(n,6np)"},
234
  {N_7NP, "(n,7np)"},
235
  {N_4NA, "(n,4na)"},
236
  {N_5NA, "(n,5na)"},
237
  {N_6NA, "(n,6na)"},
238
  {N_7NA, "(n,7na)"},
239
  {N_4ND, "(n,4nd)"},
240
  {N_5ND, "(n,5nd)"},
241
  {N_6ND, "(n,6nd)"},
242
  {N_3NT, "(n,3nt)"},
243
  {N_4NT, "(n,4nt)"},
244
  {N_5NT, "(n,5nt)"},
245
  {N_6NT, "(n,6nt)"},
246
  {N_2N3HE, "(n,2n3He)"},
247
  {N_3N3HE, "(n,3n3He)"},
248
  {N_4N3HE, "(n,4n3He)"},
249
  {N_3N2P, "(n,3n2p)"},
250
  {N_3N2A, "(n,3n2a)"},
251
  {N_3NPA, "(n,3npa)"},
252
  {N_DT, "(n,dt)"},
253
  {N_NPD, "(n,npd)"},
254
  {N_NPT, "(n,npt)"},
255
  {N_NDT, "(n,ndt)"},
256
  {N_NP3HE, "(n,np3He)"},
257
  {N_ND3HE, "(n,nd3He)"},
258
  {N_NT3HE, "(n,nt3He)"},
259
  {N_NTA, "(n,nta)"},
260
  {N_2N2P, "(n,2n2p)"},
261
  {N_P3HE, "(n,p3He)"},
262
  {N_D3HE, "(n,d3He)"},
263
  {N_3HEA, "(n,3Hea)"},
264
  {N_4N2P, "(n,4n2p)"},
265
  {N_4N2A, "(n,4n2a)"},
266
  {N_4NPA, "(n,4npa)"},
267
  {N_3P, "(n,3p)"},
268
  {N_N3P, "(n,n3p)"},
269
  {N_3N2PA, "(n,3n2pa)"},
270
  {N_5N2P, "(n,5n2p)"},
271
  {201, "(n,Xn)"},
272
  {202, "(n,Xgamma)"},
273
  {N_XP, "(n,Xp)"},
274
  {N_XD, "(n,Xd)"},
275
  {N_XT, "(n,Xt)"},
276
  {N_X3HE, "(n,X3He)"},
277
  {N_XA, "(n,Xa)"},
278
  {HEATING, "heating"},
279
  {DAMAGE_ENERGY, "damage-energy"},
280
  {COHERENT, "coherent-scatter"},
281
  {INCOHERENT, "incoherent-scatter"},
282
  {PAIR_PROD_ELEC, "pair-production-electron"},
283
  {PAIR_PROD, "pair-production"},
284
  {PAIR_PROD_NUC, "pair-production-nuclear"},
285
  {PHOTOELECTRIC, "photoelectric"},
286
  {N_PC, "(n,pc)"},
287
  {N_DC, "(n,dc)"},
288
  {N_TC, "(n,tc)"},
289
  {N_3HEC, "(n,3Hec)"},
290
  {N_AC, "(n,ac)"},
291
  {N_2NC, "(n,2nc)"},
292
  {HEATING_LOCAL, "heating-local"},
293
};
294

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

297
void initialize_maps()
3,058✔
298
{
299
  // Add level reactions to name map
300
  for (int level = 0; level <= 48; ++level) {
152,900✔
301
    if (level >= 1 && level <= 40) {
149,842✔
302
      REACTION_NAME_MAP[50 + level] = fmt::format("(n,n{})", level);
122,320✔
303
    }
304
    REACTION_NAME_MAP[600 + level] = fmt::format("(n,p{})", level);
149,842✔
305
    REACTION_NAME_MAP[650 + level] = fmt::format("(n,d{})", level);
149,842✔
306
    REACTION_NAME_MAP[700 + level] = fmt::format("(n,t{})", level);
149,842✔
307
    REACTION_NAME_MAP[750 + level] = fmt::format("(n,3He{})", level);
149,842✔
308
    REACTION_NAME_MAP[800 + level] = fmt::format("(n,a{})", level);
149,842✔
309
    if (level <= 15) {
149,842✔
310
      REACTION_NAME_MAP[875 + level] = fmt::format("(n,2n{})", level);
48,928✔
311
    }
312
  }
313

314
  // Create photoelectric subshells
315
  for (int mt = 534; mt <= 572; ++mt) {
122,320✔
316
    REACTION_NAME_MAP[mt] =
140,010✔
317
      fmt::format("photoelectric, {} subshell", SUBSHELLS[mt - 534]);
337,038✔
318
  }
319

320
  // Invert name map to create type map
321
  for (const auto& kv : REACTION_NAME_MAP) {
1,452,550✔
322
    REACTION_TYPE_MAP[kv.second] = kv.first;
1,449,492✔
323
  }
324
}
3,058✔
325

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

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

341
int reaction_type(std::string name)
37,462✔
342
{
343
  // Initialize remainder of name map and all of type map
344
  if (REACTION_TYPE_MAP.empty())
37,462✔
345
    initialize_maps();
3,022✔
346

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

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

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

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

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