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

openmc-dev / openmc / 12736621629

12 Jan 2025 07:58PM UTC coverage: 84.911% (+0.04%) from 84.868%
12736621629

Pull #3235

github

web-flow
Merge a15dc3072 into d2edf0ce4
Pull Request #3235: Simulation of decay photons through the D1S method

276 of 287 new or added lines in 15 files covered. (96.17%)

3 existing lines in 2 files now uncovered.

50309 of 59249 relevant lines covered (84.91%)

34373326.96 hits per line

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

88.08
/src/nuclide.cpp
1
#include "openmc/nuclide.h"
2

3
#include "openmc/capi.h"
4
#include "openmc/container_util.h"
5
#include "openmc/cross_sections.h"
6
#include "openmc/endf.h"
7
#include "openmc/error.h"
8
#include "openmc/hdf5_interface.h"
9
#include "openmc/message_passing.h"
10
#include "openmc/photon.h"
11
#include "openmc/random_lcg.h"
12
#include "openmc/search.h"
13
#include "openmc/settings.h"
14
#include "openmc/simulation.h"
15
#include "openmc/string_utils.h"
16
#include "openmc/thermal.h"
17

18
#include <fmt/core.h>
19

20
#include "xtensor/xbuilder.hpp"
21
#include "xtensor/xview.hpp"
22

23
#include <algorithm> // for sort, min_element
24
#include <string>    // for to_string, stoi
25

26
namespace openmc {
27

28
//==============================================================================
29
// Global variables
30
//==============================================================================
31

32
namespace data {
33
array<double, 2> energy_min {0.0, 0.0};
34
array<double, 2> energy_max {INFTY, INFTY};
35
double temperature_min {INFTY};
36
double temperature_max {0.0};
37
std::unordered_map<std::string, int> nuclide_map;
38
vector<unique_ptr<Nuclide>> nuclides;
39
} // namespace data
40

41
//==============================================================================
42
// Nuclide implementation
43
//==============================================================================
44

45
int Nuclide::XS_TOTAL {0};
46
int Nuclide::XS_ABSORPTION {1};
47
int Nuclide::XS_FISSION {2};
48
int Nuclide::XS_NU_FISSION {3};
49
int Nuclide::XS_PHOTON_PROD {4};
50

51
Nuclide::Nuclide(hid_t group, const vector<double>& temperature)
29,558✔
52
{
53
  // Set index of nuclide in global vector
54
  index_ = data::nuclides.size();
29,558✔
55

56
  // Get name of nuclide from group, removing leading '/'
57
  name_ = object_name(group).substr(1);
29,558✔
58
  data::nuclide_map[name_] = index_;
29,558✔
59

60
  read_attribute(group, "Z", Z_);
29,558✔
61
  read_attribute(group, "A", A_);
29,558✔
62
  read_attribute(group, "metastable", metastable_);
29,558✔
63
  read_attribute(group, "atomic_weight_ratio", awr_);
29,558✔
64

65
  if (settings::run_mode == RunMode::VOLUME) {
29,558✔
66
    // Determine whether nuclide is fissionable and then exit
67
    int mt;
68
    hid_t rxs_group = open_group(group, "reactions");
535✔
69
    for (auto name : group_names(rxs_group)) {
19,353✔
70
      if (starts_with(name, "reaction_")) {
18,997✔
71
        hid_t rx_group = open_group(rxs_group, name.c_str());
18,997✔
72
        read_attribute(rx_group, "mt", mt);
18,997✔
73
        if (is_fission(mt)) {
18,997✔
74
          fissionable_ = true;
179✔
75
          break;
179✔
76
        }
77
      }
78
    }
19,353✔
79
    return;
535✔
80
  }
81

82
  // Determine temperatures available
83
  hid_t kT_group = open_group(group, "kTs");
29,023✔
84
  auto dset_names = dataset_names(kT_group);
29,023✔
85
  vector<double> temps_available;
29,023✔
86
  for (const auto& name : dset_names) {
58,406✔
87
    double T;
88
    read_dataset(kT_group, name.c_str(), T);
29,383✔
89
    temps_available.push_back(std::round(T / K_BOLTZMANN));
29,383✔
90
  }
91
  std::sort(temps_available.begin(), temps_available.end());
29,023✔
92

93
  // If only one temperature is available, revert to nearest temperature
94
  if (temps_available.size() == 1 &&
57,866✔
95
      settings::temperature_method == TemperatureMethod::INTERPOLATION) {
28,843✔
96
    if (mpi::master) {
×
97
      warning("Cross sections for " + name_ +
×
98
              " are only available at one "
99
              "temperature. Reverting to nearest temperature method.");
100
    }
101
    settings::temperature_method = TemperatureMethod::NEAREST;
×
102
  }
103

104
  // Determine actual temperatures to read -- start by checking whether a
105
  // temperature range was given (indicated by T_max > 0), in which case all
106
  // temperatures in the range are loaded irrespective of what temperatures
107
  // actually appear in the model
108
  vector<int> temps_to_read;
29,023✔
109
  int n = temperature.size();
29,023✔
110
  double T_min = n > 0 ? settings::temperature_range[0] : 0.0;
29,023✔
111
  double T_max = n > 0 ? settings::temperature_range[1] : INFTY;
29,023✔
112
  if (T_max > 0.0) {
29,023✔
113
    // Determine first available temperature below or equal to T_min
114
    auto T_min_it =
115
      std::upper_bound(temps_available.begin(), temps_available.end(), T_min);
1,753✔
116
    if (T_min_it != temps_available.begin())
1,753✔
117
      --T_min_it;
×
118

119
    // Determine first available temperature above or equal to T_max
120
    auto T_max_it =
121
      std::lower_bound(temps_available.begin(), temps_available.end(), T_max);
1,753✔
122
    if (T_max_it != temps_available.end())
1,753✔
123
      ++T_max_it;
×
124

125
    // Add corresponding temperatures to vector
126
    for (auto it = T_min_it; it != T_max_it; ++it) {
3,506✔
127
      temps_to_read.push_back(std::round(*it));
1,753✔
128
    }
129
  }
130

131
  switch (settings::temperature_method) {
29,023✔
132
  case TemperatureMethod::NEAREST:
28,891✔
133
    // Find nearest temperatures
134
    for (double T_desired : temperature) {
56,137✔
135

136
      // Determine closest temperature
137
      double min_delta_T = INFTY;
27,246✔
138
      double T_actual = 0.0;
27,246✔
139
      for (auto T : temps_available) {
54,588✔
140
        double delta_T = std::abs(T - T_desired);
27,342✔
141
        if (delta_T < min_delta_T) {
27,342✔
142
          T_actual = T;
27,282✔
143
          min_delta_T = delta_T;
27,282✔
144
        }
145
      }
146

147
      if (std::abs(T_actual - T_desired) < settings::temperature_tolerance) {
27,246✔
148
        if (!contains(temps_to_read, std::round(T_actual))) {
27,246✔
149
          temps_to_read.push_back(std::round(T_actual));
27,138✔
150

151
          // Write warning for resonance scattering data if 0K is not available
152
          if (std::abs(T_actual - T_desired) > 0 && T_desired == 0 &&
27,138✔
153
              mpi::master) {
154
            warning(name_ +
×
155
                    " does not contain 0K data needed for resonance "
156
                    "scattering options selected. Using data at " +
×
157
                    std::to_string(T_actual) + " K instead.");
×
158
          }
159
        }
160
      } else {
161
        fatal_error(fmt::format(
×
162
          "Nuclear data library does not contain cross sections "
163
          "for {}  at or near {} K. Available temperatures "
164
          "are {} K. Consider making use of openmc.Settings.temperature "
165
          "to specify how intermediate temperatures are treated.",
166
          name_, std::to_string(T_desired), concatenate(temps_available)));
×
167
      }
168
    }
169
    break;
28,891✔
170

171
  case TemperatureMethod::INTERPOLATION:
132✔
172
    // If temperature interpolation or multipole is selected, get a list of
173
    // bounding temperatures for each actual temperature present in the model
174
    for (double T_desired : temperature) {
276✔
175
      bool found_pair = false;
144✔
176
      for (int j = 0; j < temps_available.size() - 1; ++j) {
432✔
177
        if (temps_available[j] <= T_desired &&
468✔
178
            T_desired < temps_available[j + 1]) {
180✔
179
          int T_j = temps_available[j];
84✔
180
          int T_j1 = temps_available[j + 1];
84✔
181
          if (!contains(temps_to_read, T_j)) {
84✔
182
            temps_to_read.push_back(T_j);
84✔
183
          }
184
          if (!contains(temps_to_read, T_j1)) {
84✔
185
            temps_to_read.push_back(T_j1);
72✔
186
          }
187
          found_pair = true;
84✔
188
        }
189
      }
190

191
      if (!found_pair) {
144✔
192
        // If no pairs found, check if the desired temperature falls just
193
        // outside of data
194
        if (std::abs(T_desired - temps_available.front()) <=
60✔
195
            settings::temperature_tolerance) {
196
          if (!contains(temps_to_read, temps_available.front())) {
36✔
197
            temps_to_read.push_back(temps_available.front());
36✔
198
          }
199
          continue;
36✔
200
        }
201
        if (std::abs(T_desired - temps_available.back()) <=
24✔
202
            settings::temperature_tolerance) {
203
          if (!contains(temps_to_read, temps_available.back())) {
24✔
204
            temps_to_read.push_back(temps_available.back());
24✔
205
          }
206
          continue;
24✔
207
        }
208
        fatal_error(
×
209
          "Nuclear data library does not contain cross sections for " + name_ +
×
210
          " at temperatures that bound " + std::to_string(T_desired) + " K.");
×
211
      }
212
    }
213
    break;
132✔
214
  }
215

216
  // Sort temperatures to read
217
  std::sort(temps_to_read.begin(), temps_to_read.end());
29,023✔
218

219
  data::temperature_min =
29,023✔
220
    std::min(data::temperature_min, static_cast<double>(temps_to_read.front()));
29,023✔
221
  data::temperature_max =
29,023✔
222
    std::max(data::temperature_max, static_cast<double>(temps_to_read.back()));
29,023✔
223

224
  hid_t energy_group = open_group(group, "energy");
29,023✔
225
  for (const auto& T : temps_to_read) {
58,130✔
226
    std::string dset {std::to_string(T) + "K"};
29,107✔
227

228
    // Determine exact kT values
229
    double kT;
230
    read_dataset(kT_group, dset.c_str(), kT);
29,107✔
231
    kTs_.push_back(kT);
29,107✔
232

233
    // Read energy grid
234
    grid_.emplace_back();
29,107✔
235
    read_dataset(energy_group, dset.c_str(), grid_.back().energy);
29,107✔
236
  }
29,107✔
237
  close_group(kT_group);
29,023✔
238

239
  // Check for 0K energy grid
240
  if (object_exists(energy_group, "0K")) {
29,023✔
241
    read_dataset(energy_group, "0K", energy_0K_);
5,452✔
242
  }
243
  close_group(energy_group);
29,023✔
244

245
  // Read reactions
246
  hid_t rxs_group = open_group(group, "reactions");
29,023✔
247
  for (auto name : group_names(rxs_group)) {
1,361,539✔
248
    if (starts_with(name, "reaction_")) {
1,332,516✔
249
      hid_t rx_group = open_group(rxs_group, name.c_str());
1,332,516✔
250
      reactions_.push_back(
1,332,516✔
251
        make_unique<Reaction>(rx_group, temps_to_read, name_));
2,665,032✔
252

253
      // Check for 0K elastic scattering
254
      const auto& rx = reactions_.back();
1,332,516✔
255
      if (rx->mt_ == ELASTIC) {
1,332,516✔
256
        if (object_exists(rx_group, "0K")) {
29,023✔
257
          hid_t temp_group = open_group(rx_group, "0K");
5,452✔
258
          read_dataset(temp_group, "xs", elastic_0K_);
5,452✔
259
          close_group(temp_group);
5,452✔
260
        }
261
      }
262
      close_group(rx_group);
1,332,516✔
263

264
      // Determine reaction indices for inelastic scattering reactions
265
      if (is_inelastic_scatter(rx->mt_) && !rx->redundant_) {
1,332,516✔
266
        index_inelastic_scatter_.push_back(reactions_.size() - 1);
675,404✔
267
      }
268
    }
269
  }
1,361,539✔
270
  close_group(rxs_group);
29,023✔
271

272
  // Read unresolved resonance probability tables if present
273
  if (object_exists(group, "urr")) {
29,023✔
274
    urr_present_ = true;
13,964✔
275
    urr_data_.reserve(temps_to_read.size());
13,964✔
276

277
    for (int i = 0; i < temps_to_read.size(); i++) {
27,928✔
278
      // Get temperature as a string
279
      std::string temp_str {std::to_string(temps_to_read[i]) + "K"};
13,964✔
280

281
      // Read probability tables for i-th temperature
282
      hid_t urr_group = open_group(group, ("urr/" + temp_str).c_str());
13,964✔
283
      urr_data_.emplace_back(urr_group);
13,964✔
284
      close_group(urr_group);
13,964✔
285

286
      // Check for negative values
287
      if (urr_data_[i].has_negative() && mpi::master) {
13,964✔
288
        warning("Negative value(s) found on probability table for nuclide " +
×
289
                name_ + " at " + temp_str);
×
290
      }
291
    }
13,964✔
292

293
    // If the inelastic competition flag indicates that the inelastic cross
294
    // section should be determined from a normal reaction cross section, we
295
    // need to get the index of the reaction.
296
    if (temps_to_read.size() > 0) {
13,964✔
297
      // Make sure inelastic flags are consistent for different temperatures
298
      for (int i = 0; i < urr_data_.size() - 1; ++i) {
13,964✔
299
        if (urr_data_[i].inelastic_flag_ != urr_data_[i + 1].inelastic_flag_) {
×
300
          fatal_error(fmt::format(
×
301
            "URR inelastic flag is not consistent for "
302
            "multiple temperatures in nuclide {}. This most likely indicates "
303
            "a problem in how the data was processed.",
304
            name_));
×
305
        }
306
      }
307

308
      if (urr_data_[0].inelastic_flag_ > 0) {
13,964✔
309
        for (int i = 0; i < reactions_.size(); i++) {
441,825✔
310
          if (reactions_[i]->mt_ == urr_data_[0].inelastic_flag_) {
433,880✔
311
            urr_inelastic_ = i;
7,945✔
312
          }
313
        }
314

315
        // Abort if no corresponding inelastic reaction was found
316
        if (urr_inelastic_ == C_NONE) {
7,945✔
317
          fatal_error("Could no find inelastic reaction specified on "
×
318
                      "unresolved resonance probability table.");
319
        }
320
      }
321
    }
322
  }
323

324
  // Check for total nu data
325
  if (object_exists(group, "total_nu")) {
29,023✔
326
    // Read total nu data
327
    hid_t nu_group = open_group(group, "total_nu");
7,542✔
328
    total_nu_ = read_function(nu_group, "yield");
7,542✔
329
    close_group(nu_group);
7,542✔
330
  }
331

332
  // Read fission energy release data if present
333
  if (object_exists(group, "fission_energy_release")) {
29,023✔
334
    hid_t fer_group = open_group(group, "fission_energy_release");
7,542✔
335
    fission_q_prompt_ = read_function(fer_group, "q_prompt");
7,542✔
336
    fission_q_recov_ = read_function(fer_group, "q_recoverable");
7,542✔
337

338
    // Read fission fragment and delayed beta energy release. This is needed for
339
    // energy normalization in k-eigenvalue calculations
340
    fragments_ = read_function(fer_group, "fragments");
7,542✔
341
    betas_ = read_function(fer_group, "betas");
7,542✔
342

343
    // We need prompt/delayed photon energy release for scaling fission photon
344
    // production
345
    prompt_photons_ = read_function(fer_group, "prompt_photons");
7,542✔
346
    delayed_photons_ = read_function(fer_group, "delayed_photons");
7,542✔
347
    close_group(fer_group);
7,542✔
348
  }
349

350
  this->create_derived(prompt_photons_.get(), delayed_photons_.get());
29,023✔
351
}
29,023✔
352

353
Nuclide::~Nuclide()
29,558✔
354
{
355
  data::nuclide_map.erase(name_);
29,558✔
356
}
29,558✔
357

358
void Nuclide::create_derived(
29,023✔
359
  const Function1D* prompt_photons, const Function1D* delayed_photons)
360
{
361
  for (const auto& grid : grid_) {
58,130✔
362
    // Allocate and initialize cross section
363
    array<size_t, 2> shape {grid.energy.size(), 5};
29,107✔
364
    xs_.emplace_back(shape, 0.0);
29,107✔
365
  }
366

367
  reaction_index_.fill(C_NONE);
29,023✔
368
  for (int i = 0; i < reactions_.size(); ++i) {
1,361,539✔
369
    const auto& rx {reactions_[i]};
1,332,516✔
370

371
    // Set entry in direct address table for reaction
372
    reaction_index_[rx->mt_] = i;
1,332,516✔
373

374
    for (int t = 0; t < kTs_.size(); ++t) {
2,665,284✔
375
      int j = rx->xs_[t].threshold;
1,332,768✔
376
      int n = rx->xs_[t].value.size();
1,332,768✔
377
      auto xs = xt::adapt(rx->xs_[t].value);
1,332,768✔
378
      auto pprod = xt::view(xs_[t], xt::range(j, j + n), XS_PHOTON_PROD);
1,332,768✔
379

380
      for (const auto& p : rx->products_) {
4,797,557✔
381
        if (p.particle_ == ParticleType::photon) {
3,464,789✔
382
          for (int k = 0; k < n; ++k) {
2,147,483,647✔
383
            double E = grid_[t].energy[k + j];
2,147,483,647✔
384

385
            // For fission, artificially increase the photon yield to
386
            // account for delayed photons
387
            double f = 1.0;
2,147,483,647✔
388
            if (settings::delayed_photon_scaling) {
2,147,483,647✔
389
              if (is_fission(rx->mt_)) {
2,147,483,647✔
390
                if (prompt_photons && delayed_photons) {
606,018,699✔
391
                  double energy_prompt = (*prompt_photons)(E);
606,018,699✔
392
                  double energy_delayed = (*delayed_photons)(E);
606,018,699✔
393
                  f = (energy_prompt + energy_delayed) / (energy_prompt);
606,018,699✔
394
                }
395
              }
396
            }
397

398
            pprod[k] += f * xs[k] * (*p.yield_)(E);
2,147,483,647✔
399
          }
400
        }
401
      }
402

403
      // Skip redundant reactions
404
      if (rx->redundant_)
1,332,768✔
405
        continue;
180,214✔
406

407
      // Add contribution to total cross section
408
      auto total = xt::view(xs_[t], xt::range(j, j + n), XS_TOTAL);
1,152,554✔
409
      total += xs;
1,152,554✔
410

411
      // Add contribution to absorption cross section
412
      auto absorption = xt::view(xs_[t], xt::range(j, j + n), XS_ABSORPTION);
1,152,554✔
413
      if (is_disappearance(rx->mt_)) {
1,152,554✔
414
        absorption += xs;
434,513✔
415
      }
416

417
      if (is_fission(rx->mt_)) {
1,152,554✔
418
        fissionable_ = true;
13,530✔
419
        auto fission = xt::view(xs_[t], xt::range(j, j + n), XS_FISSION);
13,530✔
420
        fission += xs;
13,530✔
421
        absorption += xs;
13,530✔
422

423
        // Keep track of fission reactions
424
        if (t == 0) {
13,530✔
425
          fission_rx_.push_back(rx.get());
13,446✔
426
          if (rx->mt_ == N_F)
13,446✔
427
            has_partial_fission_ = true;
1,908✔
428
        }
429
      }
13,530✔
430
    }
1,512,982✔
431
  }
432

433
  // Determine number of delayed neutron precursors
434
  if (fissionable_) {
29,023✔
435
    for (const auto& product : fission_rx_[0]->products_) {
66,075✔
436
      if (product.emission_mode_ == EmissionMode::delayed) {
58,353✔
437
        ++n_precursor_;
45,048✔
438
      }
439
    }
440
  }
441

442
  // Calculate nu-fission cross section
443
  for (int t = 0; t < kTs_.size(); ++t) {
58,130✔
444
    if (fissionable_) {
29,107✔
445
      int n = grid_[t].energy.size();
7,806✔
446
      for (int i = 0; i < n; ++i) {
606,998,684✔
447
        double E = grid_[t].energy[i];
606,990,878✔
448
        xs_[t](i, XS_NU_FISSION) =
606,990,878✔
449
          nu(E, EmissionMode::total) * xs_[t](i, XS_FISSION);
606,990,878✔
450
      }
451
    }
452
  }
453

454
  if (settings::res_scat_on) {
29,023✔
455
    // Determine if this nuclide should be treated as a resonant scatterer
456
    if (!settings::res_scat_nuclides.empty()) {
68✔
457
      // If resonant nuclides were specified, check the list explicitly
458
      for (const auto& name : settings::res_scat_nuclides) {
170✔
459
        if (name_ == name) {
153✔
460
          resonant_ = true;
51✔
461

462
          // Make sure nuclide has 0K data
463
          if (energy_0K_.empty()) {
51✔
464
            fatal_error("Cannot treat " + name_ +
×
465
                        " as a resonant scatterer "
466
                        "because 0 K elastic scattering data is not present.");
467
          }
468
          break;
51✔
469
        }
470
      }
471
    } else {
472
      // Otherwise, assume that any that have 0 K elastic scattering data
473
      // are resonant
UNCOV
474
      resonant_ = !energy_0K_.empty();
×
475
    }
476

477
    if (resonant_) {
68✔
478
      // Build CDF for 0K elastic scattering
479
      double xs_cdf_sum = 0.0;
51✔
480
      xs_cdf_.resize(energy_0K_.size());
51✔
481
      xs_cdf_[0] = 0.0;
51✔
482

483
      const auto& E = energy_0K_;
51✔
484
      auto& xs = elastic_0K_;
51✔
485
      for (int i = 0; i < E.size() - 1; ++i) {
9,469,612✔
486
        // Negative cross sections result in a CDF that is not monotonically
487
        // increasing. Set all negative xs values to zero.
488
        if (xs[i] < 0.0)
9,469,561✔
489
          xs[i] = 0.0;
×
490

491
        // build xs cdf
492
        xs_cdf_sum +=
9,469,561✔
493
          (std::sqrt(E[i]) * xs[i] + std::sqrt(E[i + 1]) * xs[i + 1]) / 2.0 *
9,469,561✔
494
          (E[i + 1] - E[i]);
9,469,561✔
495
        xs_cdf_[i + 1] = xs_cdf_sum;
9,469,561✔
496
      }
497
    }
498
  }
499
}
29,023✔
500

501
void Nuclide::init_grid()
30,890✔
502
{
503
  int neutron = static_cast<int>(ParticleType::neutron);
30,890✔
504
  double E_min = data::energy_min[neutron];
30,890✔
505
  double E_max = data::energy_max[neutron];
30,890✔
506
  int M = settings::n_log_bins;
30,890✔
507

508
  // Determine equal-logarithmic energy spacing
509
  double spacing = std::log(E_max / E_min) / M;
30,890✔
510

511
  // Create equally log-spaced energy grid
512
  auto umesh = xt::linspace(0.0, M * spacing, M + 1);
30,890✔
513

514
  for (auto& grid : grid_) {
61,864✔
515
    // Resize array for storing grid indices
516
    grid.grid_index.resize(M + 1);
30,974✔
517

518
    // Determine corresponding indices in nuclide grid to energies on
519
    // equal-logarithmic grid
520
    int j = 0;
30,974✔
521
    for (int k = 0; k <= M; ++k) {
248,465,948✔
522
      while (std::log(grid.energy[j + 1] / E_min) <= umesh(k)) {
1,029,916,367✔
523
        // Ensure that for isotopes where maxval(grid.energy) << E_max that
524
        // there are no out-of-bounds issues.
525
        if (j + 2 == grid.energy.size())
781,500,360✔
526
          break;
18,967✔
527
        ++j;
781,481,393✔
528
      }
529
      grid.grid_index[k] = j;
248,434,974✔
530
    }
531
  }
532
}
30,890✔
533

534
double Nuclide::nu(double E, EmissionMode mode, int group) const
953,147,671✔
535
{
536
  if (!fissionable_)
953,147,671✔
537
    return 0.0;
31,816,896✔
538

539
  switch (mode) {
921,330,775✔
540
  case EmissionMode::prompt:
8,198,016✔
541
    return (*fission_rx_[0]->products_[0].yield_)(E);
8,198,016✔
542
  case EmissionMode::delayed:
150,098,913✔
543
    if (n_precursor_ > 0 && settings::create_delayed_neutrons) {
150,098,913✔
544
      auto rx = fission_rx_[0];
148,065,681✔
545
      if (group >= 1 && group < rx->products_.size()) {
148,065,681✔
546
        // If delayed group specified, determine yield immediately
547
        return (*rx->products_[group].yield_)(E);
117,120,960✔
548
      } else {
549
        double nu {0.0};
30,944,721✔
550

551
        for (int i = 1; i < rx->products_.size(); ++i) {
246,256,964✔
552
          // Skip any non-neutron products
553
          const auto& product = rx->products_[i];
215,312,243✔
554
          if (product.particle_ != ParticleType::neutron)
215,312,243✔
555
            continue;
29,643,917✔
556

557
          // Evaluate yield
558
          if (product.emission_mode_ == EmissionMode::delayed) {
185,668,326✔
559
            nu += (*product.yield_)(E);
185,668,326✔
560
          }
561
        }
562
        return nu;
30,944,721✔
563
      }
564
    } else {
565
      return 0.0;
2,033,232✔
566
    }
567
  case EmissionMode::total:
763,033,846✔
568
    if (total_nu_ && settings::create_delayed_neutrons) {
763,033,846✔
569
      return (*total_nu_)(E);
761,142,694✔
570
    } else {
571
      return (*fission_rx_[0]->products_[0].yield_)(E);
1,891,152✔
572
    }
573
  }
574
  UNREACHABLE();
×
575
}
576

577
void Nuclide::calculate_elastic_xs(Particle& p) const
856,242,021✔
578
{
579
  // Get temperature index, grid index, and interpolation factor
580
  auto& micro {p.neutron_xs(index_)};
856,242,021✔
581
  int i_temp = micro.index_temp;
856,242,021✔
582
  int i_grid = micro.index_grid;
856,242,021✔
583
  double f = micro.interp_factor;
856,242,021✔
584

585
  if (i_temp >= 0) {
856,242,021✔
586
    const auto& xs = reactions_[0]->xs_[i_temp].value;
852,730,029✔
587
    micro.elastic = (1.0 - f) * xs[i_grid] + f * xs[i_grid + 1];
852,730,029✔
588
  }
589
}
856,242,021✔
590

591
double Nuclide::elastic_xs_0K(double E) const
×
592
{
593
  // Determine index on nuclide energy grid
594
  int i_grid;
595
  if (E < energy_0K_.front()) {
×
596
    i_grid = 0;
×
597
  } else if (E > energy_0K_.back()) {
×
598
    i_grid = energy_0K_.size() - 2;
×
599
  } else {
600
    i_grid = lower_bound_index(energy_0K_.begin(), energy_0K_.end(), E);
×
601
  }
602

603
  // check for rare case where two energy points are the same
604
  if (energy_0K_[i_grid] == energy_0K_[i_grid + 1])
×
605
    ++i_grid;
×
606

607
  // calculate interpolation factor
608
  double f =
609
    (E - energy_0K_[i_grid]) / (energy_0K_[i_grid + 1] - energy_0K_[i_grid]);
×
610

611
  // Calculate microscopic nuclide elastic cross section
612
  return (1.0 - f) * elastic_0K_[i_grid] + f * elastic_0K_[i_grid + 1];
×
613
}
614

615
void Nuclide::calculate_xs(
2,147,483,647✔
616
  int i_sab, int i_log_union, double sab_frac, Particle& p)
617
{
618
  auto& micro {p.neutron_xs(index_)};
2,147,483,647✔
619

620
  // Initialize cached cross sections to zero
621
  micro.elastic = CACHE_INVALID;
2,147,483,647✔
622
  micro.thermal = 0.0;
2,147,483,647✔
623
  micro.thermal_elastic = 0.0;
2,147,483,647✔
624

625
  // Check to see if there is multipole data present at this energy
626
  bool use_mp = false;
2,147,483,647✔
627
  if (multipole_) {
2,147,483,647✔
628
    use_mp = (p.E() >= multipole_->E_min_ && p.E() <= multipole_->E_max_);
9,948,480✔
629
  }
630

631
  // Evaluate multipole or interpolate
632
  if (use_mp) {
2,147,483,647✔
633
    // Call multipole kernel
634
    double sig_s, sig_a, sig_f;
635
    std::tie(sig_s, sig_a, sig_f) = multipole_->evaluate(p.E(), p.sqrtkT());
9,579,864✔
636

637
    micro.total = sig_s + sig_a;
9,579,864✔
638
    micro.elastic = sig_s;
9,579,864✔
639
    micro.absorption = sig_a;
9,579,864✔
640
    micro.fission = sig_f;
9,579,864✔
641
    micro.nu_fission =
9,579,864✔
642
      fissionable_ ? sig_f * this->nu(p.E(), EmissionMode::total) : 0.0;
9,579,864✔
643

644
    if (simulation::need_depletion_rx) {
9,579,864✔
645
      // Only non-zero reaction is (n,gamma)
646
      micro.reaction[0] = sig_a - sig_f;
×
647

648
      // Set all other reaction cross sections to zero
649
      for (int i = 1; i < DEPLETION_RX.size(); ++i) {
×
650
        micro.reaction[i] = 0.0;
×
651
      }
652
    }
653

654
    /*
655
     * index_temp, index_grid, and interp_factor are used only in the
656
     * following places:
657
     *   1. physics.cpp - scatter - For inelastic scatter.
658
     *   2. physics.cpp - sample_fission - For partial fissions.
659
     *   3. tallies/tally_scoring.cpp - score_general -
660
     *        For tallying on MTxxx reactions.
661
     *   4. nuclide.cpp - calculate_urr_xs - For unresolved purposes.
662
     * It is worth noting that none of these occur in the resolved resonance
663
     * range, so the value here does not matter.  index_temp is set to -1 to
664
     * force a segfault in case a developer messes up and tries to use it with
665
     * multipole.
666
     *
667
     * However, a segfault is not necessarily guaranteed with an out-of-bounds
668
     * access, so this technique should be replaced by something more robust
669
     * in the future.
670
     */
671
    micro.index_temp = -1;
9,579,864✔
672
    micro.index_grid = -1;
9,579,864✔
673
    micro.interp_factor = 0.0;
9,579,864✔
674

675
  } else {
676
    // Find the appropriate temperature index.
677
    double kT = p.sqrtkT() * p.sqrtkT();
2,147,483,647✔
678
    double f;
679
    int i_temp = -1;
2,147,483,647✔
680
    switch (settings::temperature_method) {
2,147,483,647✔
681
    case TemperatureMethod::NEAREST: {
2,147,483,647✔
682
      double max_diff = INFTY;
2,147,483,647✔
683
      for (int t = 0; t < kTs_.size(); ++t) {
2,147,483,647✔
684
        double diff = std::abs(kTs_[t] - kT);
2,147,483,647✔
685
        if (diff < max_diff) {
2,147,483,647✔
686
          i_temp = t;
2,147,483,647✔
687
          max_diff = diff;
2,147,483,647✔
688
        }
689
      }
690
    } break;
2,147,483,647✔
691

692
    case TemperatureMethod::INTERPOLATION:
1,654,860✔
693
      // If current kT outside of the bounds of available, snap to the bound
694
      if (kT < kTs_.front()) {
1,654,860✔
695
        i_temp = 0;
450,792✔
696
        break;
450,792✔
697
      }
698
      if (kT > kTs_.back()) {
1,204,068✔
699
        i_temp = kTs_.size() - 1;
236,976✔
700
        break;
236,976✔
701
      }
702

703
      // Find temperatures that bound the actual temperature
704
      for (i_temp = 0; i_temp < kTs_.size() - 1; ++i_temp) {
967,092✔
705
        if (kTs_[i_temp] <= kT && kT < kTs_[i_temp + 1])
967,092✔
706
          break;
967,092✔
707
      }
708

709
      // Randomly sample between temperature i and i+1
710
      f = (kT - kTs_[i_temp]) / (kTs_[i_temp + 1] - kTs_[i_temp]);
967,092✔
711
      if (f > prn(p.current_seed()))
967,092✔
712
        ++i_temp;
449,316✔
713
      break;
967,092✔
714
    }
715

716
    // Determine the energy grid index using a logarithmic mapping to
717
    // reduce the energy range over which a binary search needs to be
718
    // performed
719

720
    const auto& grid {grid_[i_temp]};
2,147,483,647✔
721
    const auto& xs {xs_[i_temp]};
2,147,483,647✔
722

723
    int i_grid;
724
    if (p.E() < grid.energy.front()) {
2,147,483,647✔
725
      i_grid = 0;
884✔
726
    } else if (p.E() > grid.energy.back()) {
2,147,483,647✔
727
      i_grid = grid.energy.size() - 2;
×
728
    } else {
729
      // Determine bounding indices based on which equal log-spaced
730
      // interval the energy is in
731
      int i_low = grid.grid_index[i_log_union];
2,147,483,647✔
732
      int i_high = grid.grid_index[i_log_union + 1] + 1;
2,147,483,647✔
733

734
      // Perform binary search over reduced range
735
      i_grid = i_low + lower_bound_index(
2,147,483,647✔
736
                         &grid.energy[i_low], &grid.energy[i_high], p.E());
2,147,483,647✔
737
    }
738

739
    // check for rare case where two energy points are the same
740
    if (grid.energy[i_grid] == grid.energy[i_grid + 1])
2,147,483,647✔
741
      ++i_grid;
×
742

743
    // calculate interpolation factor
744
    f = (p.E() - grid.energy[i_grid]) /
2,147,483,647✔
745
        (grid.energy[i_grid + 1] - grid.energy[i_grid]);
2,147,483,647✔
746

747
    micro.index_temp = i_temp;
2,147,483,647✔
748
    micro.index_grid = i_grid;
2,147,483,647✔
749
    micro.interp_factor = f;
2,147,483,647✔
750

751
    // Calculate microscopic nuclide total cross section
752
    micro.total =
2,147,483,647✔
753
      (1.0 - f) * xs(i_grid, XS_TOTAL) + f * xs(i_grid + 1, XS_TOTAL);
2,147,483,647✔
754

755
    // Calculate microscopic nuclide absorption cross section
756
    micro.absorption =
2,147,483,647✔
757
      (1.0 - f) * xs(i_grid, XS_ABSORPTION) + f * xs(i_grid + 1, XS_ABSORPTION);
2,147,483,647✔
758

759
    if (fissionable_) {
2,147,483,647✔
760
      // Calculate microscopic nuclide total cross section
761
      micro.fission =
745,977,936✔
762
        (1.0 - f) * xs(i_grid, XS_FISSION) + f * xs(i_grid + 1, XS_FISSION);
745,977,936✔
763

764
      // Calculate microscopic nuclide nu-fission cross section
765
      micro.nu_fission = (1.0 - f) * xs(i_grid, XS_NU_FISSION) +
745,977,936✔
766
                         f * xs(i_grid + 1, XS_NU_FISSION);
745,977,936✔
767
    } else {
768
      micro.fission = 0.0;
2,147,483,647✔
769
      micro.nu_fission = 0.0;
2,147,483,647✔
770
    }
771

772
    // Calculate microscopic nuclide photon production cross section
773
    micro.photon_prod = (1.0 - f) * xs(i_grid, XS_PHOTON_PROD) +
2,147,483,647✔
774
                        f * xs(i_grid + 1, XS_PHOTON_PROD);
2,147,483,647✔
775

776
    // Depletion-related reactions
777
    if (simulation::need_depletion_rx) {
2,147,483,647✔
778
      // Initialize all reaction cross sections to zero
779
      for (double& xs_i : micro.reaction) {
2,147,483,647✔
780
        xs_i = 0.0;
2,147,483,647✔
781
      }
782

783
      for (int j = 0; j < DEPLETION_RX.size(); ++j) {
2,147,483,647✔
784
        // If reaction is present and energy is greater than threshold, set
785
        // the reaction xs appropriately
786
        int i_rx = reaction_index_[DEPLETION_RX[j]];
2,147,483,647✔
787
        if (i_rx >= 0) {
2,147,483,647✔
788
          const auto& rx = reactions_[i_rx];
2,147,483,647✔
789
          const auto& rx_xs = rx->xs_[i_temp].value;
2,147,483,647✔
790

791
          // Physics says that (n,gamma) is not a threshold reaction, so we
792
          // don't need to specifically check its threshold index
793
          if (j == 0) {
2,147,483,647✔
794
            micro.reaction[0] =
972,509,436✔
795
              (1.0 - f) * rx_xs[i_grid] + f * rx_xs[i_grid + 1];
972,509,436✔
796
            continue;
972,509,436✔
797
          }
798

799
          int threshold = rx->xs_[i_temp].threshold;
2,047,253,322✔
800
          if (i_grid >= threshold) {
2,047,253,322✔
801
            micro.reaction[j] = (1.0 - f) * rx_xs[i_grid - threshold] +
313,015,920✔
802
                                f * rx_xs[i_grid - threshold + 1];
313,015,920✔
803
          } else if (j >= 3) {
1,734,237,402✔
804
            // One can show that the the threshold for (n,(x+1)n) is always
805
            // higher than the threshold for (n,xn). Thus, if we are below
806
            // the threshold for, e.g., (n,2n), there is no reason to check
807
            // the threshold for (n,3n) and (n,4n).
808
            break;
795,956,475✔
809
          }
810
        }
811
      }
812
    }
813
  }
814

815
  // Initialize sab treatment to false
816
  micro.index_sab = C_NONE;
2,147,483,647✔
817
  micro.sab_frac = 0.0;
2,147,483,647✔
818

819
  // Initialize URR probability table treatment to false
820
  micro.use_ptable = false;
2,147,483,647✔
821

822
  // If there is S(a,b) data for this nuclide, we need to set the
823
  // sab_scatter and sab_elastic cross sections and correct the total and
824
  // elastic cross sections.
825

826
  if (i_sab >= 0)
2,147,483,647✔
827
    this->calculate_sab_xs(i_sab, sab_frac, p);
95,905,675✔
828

829
  // If the particle is in the unresolved resonance range and there are
830
  // probability tables, we need to determine cross sections from the table
831
  if (settings::urr_ptables_on && urr_present_ && !use_mp) {
2,147,483,647✔
832
    if (urr_data_[micro.index_temp].energy_in_bounds(p.E()))
1,381,699,691✔
833
      this->calculate_urr_xs(micro.index_temp, p);
183,948,613✔
834
  }
835

836
  micro.last_E = p.E();
2,147,483,647✔
837
  micro.last_sqrtkT = p.sqrtkT();
2,147,483,647✔
838
}
2,147,483,647✔
839

840
void Nuclide::calculate_sab_xs(int i_sab, double sab_frac, Particle& p)
95,905,675✔
841
{
842
  auto& micro {p.neutron_xs(index_)};
95,905,675✔
843

844
  // Set flag that S(a,b) treatment should be used for scattering
845
  micro.index_sab = i_sab;
95,905,675✔
846

847
  // Calculate the S(a,b) cross section
848
  int i_temp;
849
  double elastic;
850
  double inelastic;
851
  data::thermal_scatt[i_sab]->calculate_xs(
191,811,350✔
852
    p.E(), p.sqrtkT(), &i_temp, &elastic, &inelastic, p.current_seed());
95,905,675✔
853

854
  // Store the S(a,b) cross sections.
855
  micro.thermal = sab_frac * (elastic + inelastic);
95,905,675✔
856
  micro.thermal_elastic = sab_frac * elastic;
95,905,675✔
857

858
  // Calculate free atom elastic cross section
859
  this->calculate_elastic_xs(p);
95,905,675✔
860

861
  // Correct total and elastic cross sections
862
  micro.total = micro.total + micro.thermal - sab_frac * micro.elastic;
95,905,675✔
863
  micro.elastic = micro.thermal + (1.0 - sab_frac) * micro.elastic;
95,905,675✔
864

865
  // Save temperature index and thermal fraction
866
  micro.index_temp_sab = i_temp;
95,905,675✔
867
  micro.sab_frac = sab_frac;
95,905,675✔
868
}
95,905,675✔
869

870
void Nuclide::calculate_urr_xs(int i_temp, Particle& p) const
183,948,613✔
871
{
872
  auto& micro = p.neutron_xs(index_);
183,948,613✔
873
  micro.use_ptable = true;
183,948,613✔
874

875
  // Create a shorthand for the URR data
876
  const auto& urr = urr_data_[i_temp];
183,948,613✔
877

878
  // Determine the energy table
879
  int i_energy =
880
    lower_bound_index(urr.energy_.begin(), urr.energy_.end(), p.E());
183,948,613✔
881

882
  // Sample the probability table using the cumulative distribution
883

884
  // Random numbers for the xs calculation are sampled from a separate stream.
885
  // This guarantees the randomness and, at the same time, makes sure we
886
  // reuse random numbers for the same nuclide at different temperatures,
887
  // therefore preserving correlation of temperature in probability tables.
888
  double r =
889
    future_prn(static_cast<int64_t>(index_), p.seeds(STREAM_URR_PTABLE));
183,948,613✔
890

891
  // Warning: this assumes row-major order of cdf_values_
892
  int i_low = upper_bound_index(&urr.cdf_values_(i_energy, 0),
183,948,613✔
893
                &urr.cdf_values_(i_energy, 0) + urr.n_cdf(), r) +
183,948,613✔
894
              1;
183,948,613✔
895
  int i_up = upper_bound_index(&urr.cdf_values_(i_energy + 1, 0),
183,948,613✔
896
               &urr.cdf_values_(i_energy + 1, 0) + urr.n_cdf(), r) +
183,948,613✔
897
             1;
183,948,613✔
898

899
  // Determine elastic, fission, and capture cross sections from the
900
  // probability table
901
  double elastic = 0.;
183,948,613✔
902
  double fission = 0.;
183,948,613✔
903
  double capture = 0.;
183,948,613✔
904
  double f;
905
  if (urr.interp_ == Interpolation::lin_lin) {
183,948,613✔
906
    // Determine the interpolation factor on the table
907
    f = (p.E() - urr.energy_[i_energy]) /
70,999,532✔
908
        (urr.energy_[i_energy + 1] - urr.energy_[i_energy]);
70,999,532✔
909

910
    elastic = (1. - f) * urr.xs_values_(i_energy, i_low).elastic +
70,999,532✔
911
              f * urr.xs_values_(i_energy + 1, i_up).elastic;
70,999,532✔
912
    fission = (1. - f) * urr.xs_values_(i_energy, i_low).fission +
70,999,532✔
913
              f * urr.xs_values_(i_energy + 1, i_up).fission;
70,999,532✔
914
    capture = (1. - f) * urr.xs_values_(i_energy, i_low).n_gamma +
70,999,532✔
915
              f * urr.xs_values_(i_energy + 1, i_up).n_gamma;
70,999,532✔
916
  } else if (urr.interp_ == Interpolation::log_log) {
112,949,081✔
917
    // Determine interpolation factor on the table
918
    f = std::log(p.E() / urr.energy_[i_energy]) /
112,949,081✔
919
        std::log(urr.energy_[i_energy + 1] / urr.energy_[i_energy]);
112,949,081✔
920

921
    // Calculate the elastic cross section/factor
922
    if ((urr.xs_values_(i_energy, i_low).elastic > 0.) &&
225,898,162✔
923
        (urr.xs_values_(i_energy + 1, i_up).elastic > 0.)) {
112,949,081✔
924
      elastic =
925
        std::exp((1. - f) * std::log(urr.xs_values_(i_energy, i_low).elastic) +
112,949,081✔
926
                 f * std::log(urr.xs_values_(i_energy + 1, i_up).elastic));
112,949,081✔
927
    } else {
928
      elastic = 0.;
×
929
    }
930

931
    // Calculate the fission cross section/factor
932
    if ((urr.xs_values_(i_energy, i_low).fission > 0.) &&
193,550,957✔
933
        (urr.xs_values_(i_energy + 1, i_up).fission > 0.)) {
80,601,876✔
934
      fission =
935
        std::exp((1. - f) * std::log(urr.xs_values_(i_energy, i_low).fission) +
80,601,876✔
936
                 f * std::log(urr.xs_values_(i_energy + 1, i_up).fission));
80,601,876✔
937
    } else {
938
      fission = 0.;
32,347,205✔
939
    }
940

941
    // Calculate the capture cross section/factor
942
    if ((urr.xs_values_(i_energy, i_low).n_gamma > 0.) &&
225,898,162✔
943
        (urr.xs_values_(i_energy + 1, i_up).n_gamma > 0.)) {
112,949,081✔
944
      capture =
945
        std::exp((1. - f) * std::log(urr.xs_values_(i_energy, i_low).n_gamma) +
112,949,081✔
946
                 f * std::log(urr.xs_values_(i_energy + 1, i_up).n_gamma));
112,949,081✔
947
    } else {
948
      capture = 0.;
×
949
    }
950
  }
951

952
  // Determine the treatment of inelastic scattering
953
  double inelastic = 0.;
183,948,613✔
954
  if (urr.inelastic_flag_ != C_NONE) {
183,948,613✔
955
    // get interpolation factor
956
    f = micro.interp_factor;
133,011,518✔
957

958
    // Determine inelastic scattering cross section
959
    Reaction* rx = reactions_[urr_inelastic_].get();
133,011,518✔
960
    int xs_index = micro.index_grid - rx->xs_[i_temp].threshold;
133,011,518✔
961
    if (xs_index >= 0) {
133,011,518✔
962
      inelastic = (1. - f) * rx->xs_[i_temp].value[xs_index] +
78,931,549✔
963
                  f * rx->xs_[i_temp].value[xs_index + 1];
78,931,549✔
964
    }
965
  }
966

967
  // Multiply by smooth cross-section if needed
968
  if (urr.multiply_smooth_) {
183,948,613✔
969
    calculate_elastic_xs(p);
86,919,541✔
970
    elastic *= micro.elastic;
86,919,541✔
971
    capture *= (micro.absorption - micro.fission);
86,919,541✔
972
    fission *= micro.fission;
86,919,541✔
973
  }
974

975
  // Check for negative values
976
  if (elastic < 0.) {
183,948,613✔
977
    elastic = 0.;
×
978
  }
979
  if (fission < 0.) {
183,948,613✔
980
    fission = 0.;
×
981
  }
982
  if (capture < 0.) {
183,948,613✔
983
    capture = 0.;
×
984
  }
985

986
  // Set elastic, absorption, fission, total, and capture x/s. Note that the
987
  // total x/s is calculated as a sum of partials instead of the
988
  // table-provided value
989
  micro.elastic = elastic;
183,948,613✔
990
  micro.absorption = capture + fission;
183,948,613✔
991
  micro.fission = fission;
183,948,613✔
992
  micro.total = elastic + inelastic + capture + fission;
183,948,613✔
993
  if (simulation::need_depletion_rx) {
183,948,613✔
994
    micro.reaction[0] = capture;
45,163,179✔
995
  }
996

997
  // Determine nu-fission cross-section
998
  if (fissionable_) {
183,948,613✔
999
    micro.nu_fission = nu(p.E(), EmissionMode::total) * micro.fission;
127,738,775✔
1000
  }
1001
}
183,948,613✔
1002

1003
std::pair<gsl::index, double> Nuclide::find_temperature(double T) const
816✔
1004
{
1005
  Expects(T >= 0.0);
816✔
1006

1007
  // Determine temperature index
1008
  gsl::index i_temp = 0;
816✔
1009
  double f = 0.0;
816✔
1010
  double kT = K_BOLTZMANN * T;
816✔
1011
  gsl::index n = kTs_.size();
816✔
1012
  switch (settings::temperature_method) {
816✔
1013
  case TemperatureMethod::NEAREST: {
816✔
1014
    double max_diff = INFTY;
816✔
1015
    for (gsl::index t = 0; t < n; ++t) {
1,632✔
1016
      double diff = std::abs(kTs_[t] - kT);
816✔
1017
      if (diff < max_diff) {
816✔
1018
        i_temp = t;
816✔
1019
        max_diff = diff;
816✔
1020
      }
1021
    }
1022
  } break;
816✔
1023

1024
  case TemperatureMethod::INTERPOLATION:
×
1025
    // If current kT outside of the bounds of available, snap to the bound
1026
    if (kT < kTs_.front()) {
×
1027
      i_temp = 0;
×
1028
      break;
×
1029
    }
1030
    if (kT > kTs_.back()) {
×
1031
      i_temp = kTs_.size() - 1;
×
1032
      break;
×
1033
    }
1034
    // Find temperatures that bound the actual temperature
1035
    while (kTs_[i_temp + 1] < kT && i_temp + 1 < n - 1)
×
1036
      ++i_temp;
×
1037

1038
    // Determine interpolation factor
1039
    f = (kT - kTs_[i_temp]) / (kTs_[i_temp + 1] - kTs_[i_temp]);
×
1040
  }
1041

1042
  Ensures(i_temp >= 0 && i_temp < n);
816✔
1043

1044
  return {i_temp, f};
816✔
1045
}
1046

1047
double Nuclide::collapse_rate(int MT, double temperature,
1,200✔
1048
  gsl::span<const double> energy, gsl::span<const double> flux) const
1049
{
1050
  Expects(MT > 0);
1,200✔
1051
  Expects(energy.size() > 0);
1,200✔
1052
  Expects(energy.size() == flux.size() + 1);
1,200✔
1053

1054
  int i_rx = reaction_index_[MT];
1,200✔
1055
  if (i_rx < 0)
1,200✔
1056
    return 0.0;
384✔
1057
  const auto& rx = reactions_[i_rx];
816✔
1058

1059
  // Determine temperature index
1060
  gsl::index i_temp;
1061
  double f;
1062
  std::tie(i_temp, f) = this->find_temperature(temperature);
816✔
1063

1064
  // Get reaction rate at lower temperature
1065
  const auto& grid_low = grid_[i_temp].energy;
816✔
1066
  double rr_low = rx->collapse_rate(i_temp, energy, flux, grid_low);
816✔
1067

1068
  if (f > 0.0) {
816✔
1069
    // Interpolate between reaction rate at lower and higher temperature
1070
    const auto& grid_high = grid_[i_temp + 1].energy;
×
1071
    double rr_high = rx->collapse_rate(i_temp + 1, energy, flux, grid_high);
×
1072
    return rr_low + f * (rr_high - rr_low);
×
1073
  } else {
1074
    // If interpolation factor is zero, return reaction rate at lower
1075
    // temperature
1076
    return rr_low;
816✔
1077
  }
1078
}
1079

1080
//==============================================================================
1081
// Non-member functions
1082
//==============================================================================
1083

1084
void check_data_version(hid_t file_id)
31,330✔
1085
{
1086
  if (attribute_exists(file_id, "version")) {
31,330✔
1087
    vector<int> version;
31,330✔
1088
    read_attribute(file_id, "version", version);
31,330✔
1089
    if (version[0] != HDF5_VERSION[0]) {
31,330✔
1090
      fatal_error("HDF5 data format uses version " +
×
1091
                  std::to_string(version[0]) + "." +
×
1092
                  std::to_string(version[1]) +
×
1093
                  " whereas your installation of "
1094
                  "OpenMC expects version " +
×
1095
                  std::to_string(HDF5_VERSION[0]) + ".x data.");
×
1096
    }
1097
  } else {
31,330✔
1098
    fatal_error("HDF5 data does not indicate a version. Your installation of "
×
1099
                "OpenMC expects version " +
×
1100
                std::to_string(HDF5_VERSION[0]) + ".x data.");
×
1101
  }
1102
}
31,330✔
1103

1104
extern "C" size_t nuclides_size()
24✔
1105
{
1106
  return data::nuclides.size();
24✔
1107
}
1108

1109
//==============================================================================
1110
// C API
1111
//==============================================================================
1112

1113
extern "C" int openmc_load_nuclide(const char* name, const double* temps, int n)
29,582✔
1114
{
1115
  if (data::nuclide_map.find(name) == data::nuclide_map.end() ||
87,192✔
1116
      data::nuclide_map.at(name) >= data::elements.size()) {
57,610✔
1117
    LibraryKey key {Library::Type::neutron, name};
29,582✔
1118
    const auto& it = data::library_map.find(key);
29,582✔
1119
    if (it == data::library_map.end()) {
29,582✔
1120
      set_errmsg(
24✔
1121
        "Nuclide '" + std::string {name} + "' is not present in library.");
48✔
1122
      return OPENMC_E_DATA;
24✔
1123
    }
1124

1125
    // Get filename for library containing nuclide
1126
    int idx = it->second;
29,558✔
1127
    const auto& filename = data::libraries[idx].path_;
29,558✔
1128
    write_message(6, "Reading {} from {}", name, filename);
29,558✔
1129

1130
    // Open file and make sure version is sufficient
1131
    hid_t file_id = file_open(filename, 'r');
29,558✔
1132
    check_data_version(file_id);
29,558✔
1133

1134
    // Read nuclide data from HDF5
1135
    hid_t group = open_group(file_id, name);
29,558✔
1136
    vector<double> temperature {temps, temps + n};
29,558✔
1137
    data::nuclides.push_back(make_unique<Nuclide>(group, temperature));
29,558✔
1138

1139
    close_group(group);
29,558✔
1140
    file_close(file_id);
29,558✔
1141

1142
    // Read multipole file into the appropriate entry on the nuclides array
1143
    int i_nuclide = data::nuclide_map.at(name);
29,558✔
1144
    if (settings::temperature_multipole)
29,558✔
1145
      read_multipole_data(i_nuclide);
442✔
1146

1147
    // Read elemental data, if necessary
1148
    if (settings::photon_transport) {
29,558✔
1149
      auto element = to_element(name);
2,730✔
1150
      if (data::element_map.find(element) == data::element_map.end() ||
2,730✔
1151
          data::element_map.at(element) >= data::elements.size()) {
1,365✔
1152
        // Read photon interaction data from HDF5 photon library
1153
        LibraryKey key {Library::Type::photon, element};
635✔
1154
        const auto& it = data::library_map.find(key);
635✔
1155
        if (it == data::library_map.end()) {
635✔
1156
          set_errmsg("Element '" + std::string {element} +
×
1157
                     "' is not present in library.");
1158
          return OPENMC_E_DATA;
×
1159
        }
1160

1161
        int idx = it->second;
635✔
1162
        const auto& filename = data::libraries[idx].path_;
635✔
1163
        write_message(6, "Reading {} from {} ", element, filename);
635✔
1164

1165
        // Open file and make sure version is sufficient
1166
        hid_t file_id = file_open(filename, 'r');
635✔
1167
        check_data_version(file_id);
635✔
1168

1169
        // Read element data from HDF5
1170
        hid_t group = open_group(file_id, element.c_str());
635✔
1171
        data::elements.push_back(make_unique<PhotonInteraction>(group));
635✔
1172

1173
        close_group(group);
635✔
1174
        file_close(file_id);
635✔
1175
      }
635✔
1176
    }
1,365✔
1177
  }
29,582✔
1178
  return 0;
29,558✔
1179
}
1180

1181
extern "C" int openmc_get_nuclide_index(const char* name, int* index)
936✔
1182
{
1183
  auto it = data::nuclide_map.find(name);
936✔
1184
  if (it == data::nuclide_map.end()) {
936✔
1185
    set_errmsg(
12✔
1186
      "No nuclide named '" + std::string {name} + "' has been loaded.");
24✔
1187
    return OPENMC_E_DATA;
12✔
1188
  }
1189
  *index = it->second;
924✔
1190
  return 0;
924✔
1191
}
1192

1193
extern "C" int openmc_nuclide_name(int index, const char** name)
1,620✔
1194
{
1195
  if (index >= 0 && index < data::nuclides.size()) {
1,620✔
1196
    *name = data::nuclides[index]->name_.data();
1,620✔
1197
    return 0;
1,620✔
1198
  } else {
1199
    set_errmsg("Index in nuclides vector is out of bounds.");
×
1200
    return OPENMC_E_OUT_OF_BOUNDS;
×
1201
  }
1202
}
1203

1204
extern "C" int openmc_nuclide_collapse_rate(int index, int MT,
1,200✔
1205
  double temperature, const double* energy, const double* flux, int n,
1206
  double* xs)
1207
{
1208
  if (index < 0 || index >= data::nuclides.size()) {
1,200✔
1209
    set_errmsg("Index in nuclides vector is out of bounds.");
×
1210
    return OPENMC_E_OUT_OF_BOUNDS;
×
1211
  }
1212

1213
  try {
1214
    *xs = data::nuclides[index]->collapse_rate(
2,400✔
1215
      MT, temperature, {energy, energy + n + 1}, {flux, flux + n});
1,200✔
1216
  } catch (const std::out_of_range& e) {
×
1217
    fmt::print("Caught error\n");
×
1218
    set_errmsg(e.what());
×
1219
    return OPENMC_E_OUT_OF_BOUNDS;
×
1220
  }
×
1221
  return 0;
1,200✔
1222
}
1223

1224
void nuclides_clear()
6,986✔
1225
{
1226
  data::nuclides.clear();
6,986✔
1227
  data::nuclide_map.clear();
6,986✔
1228
}
6,986✔
1229

1230
bool multipole_in_range(const Nuclide& nuc, double E)
1,065,300✔
1231
{
1232
  return E >= nuc.multipole_->E_min_ && E <= nuc.multipole_->E_max_;
1,065,300✔
1233
}
1234

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