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

openmc-dev / openmc / 13443476914

20 Feb 2025 07:49PM UTC coverage: 85.022% (+0.05%) from 84.969%
13443476914

Pull #3140

github

web-flow
Merge 8ddcb065a into 7638661fa
Pull Request #3140: Add Versioning Support from `version.txt`

50634 of 59554 relevant lines covered (85.02%)

35434881.25 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 <cassert>
25
#include <string> // for to_string, stoi
26

27
namespace openmc {
28

29
//==============================================================================
30
// Global variables
31
//==============================================================================
32

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

42
//==============================================================================
43
// Nuclide implementation
44
//==============================================================================
45

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

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

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

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

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

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

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

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

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

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

132
  switch (settings::temperature_method) {
28,496✔
133
  case TemperatureMethod::NEAREST:
28,364✔
134
    // Find nearest temperatures
135
    for (double T_desired : temperature) {
55,172✔
136

137
      // Determine closest temperature
138
      double min_delta_T = INFTY;
26,808✔
139
      double T_actual = 0.0;
26,808✔
140
      for (auto T : temps_available) {
53,712✔
141
        double delta_T = std::abs(T - T_desired);
26,904✔
142
        if (delta_T < min_delta_T) {
26,904✔
143
          T_actual = T;
26,844✔
144
          min_delta_T = delta_T;
26,844✔
145
        }
146
      }
147

148
      if (std::abs(T_actual - T_desired) < settings::temperature_tolerance) {
26,808✔
149
        if (!contains(temps_to_read, std::round(T_actual))) {
26,808✔
150
          temps_to_read.push_back(std::round(T_actual));
26,699✔
151

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

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

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

217
  // Sort temperatures to read
218
  std::sort(temps_to_read.begin(), temps_to_read.end());
28,496✔
219

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

225
  hid_t energy_group = open_group(group, "energy");
28,496✔
226
  for (const auto& T : temps_to_read) {
57,076✔
227
    std::string dset {std::to_string(T) + "K"};
28,580✔
228

229
    // Determine exact kT values
230
    double kT;
231
    read_dataset(kT_group, dset.c_str(), kT);
28,580✔
232
    kTs_.push_back(kT);
28,580✔
233

234
    // Read energy grid
235
    grid_.emplace_back();
28,580✔
236
    read_dataset(energy_group, dset.c_str(), grid_.back().energy);
28,580✔
237
  }
28,580✔
238
  close_group(kT_group);
28,496✔
239

240
  // Check for 0K energy grid
241
  if (object_exists(energy_group, "0K")) {
28,496✔
242
    read_dataset(energy_group, "0K", energy_0K_);
5,372✔
243
  }
244
  close_group(energy_group);
28,496✔
245

246
  // Read reactions
247
  hid_t rxs_group = open_group(group, "reactions");
28,496✔
248
  for (auto name : group_names(rxs_group)) {
1,339,069✔
249
    if (starts_with(name, "reaction_")) {
1,310,573✔
250
      hid_t rx_group = open_group(rxs_group, name.c_str());
1,310,573✔
251
      reactions_.push_back(make_unique<Reaction>(rx_group, temps_to_read));
1,310,573✔
252

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

264
      // Determine reaction indices for inelastic scattering reactions
265
      if (is_inelastic_scatter(rx->mt_) && !rx->redundant_) {
1,310,573✔
266
        index_inelastic_scatter_.push_back(reactions_.size() - 1);
663,489✔
267
      }
268
    }
269
  }
1,339,069✔
270
  close_group(rxs_group);
28,496✔
271

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

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

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

286
      // Check for negative values
287
      if (urr_data_[i].has_negative() && mpi::master) {
13,690✔
288
        warning("Negative value(s) found on probability table for nuclide " +
×
289
                name_ + " at " + temp_str);
×
290
      }
291
    }
13,690✔
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,690✔
297
      // Make sure inelastic flags are consistent for different temperatures
298
      for (int i = 0; i < urr_data_.size() - 1; ++i) {
13,690✔
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,690✔
309
        for (int i = 0; i < reactions_.size(); i++) {
433,669✔
310
          if (reactions_[i]->mt_ == urr_data_[0].inelastic_flag_) {
425,864✔
311
            urr_inelastic_ = i;
7,805✔
312
          }
313
        }
314

315
        // Abort if no corresponding inelastic reaction was found
316
        if (urr_inelastic_ == C_NONE) {
7,805✔
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")) {
28,496✔
326
    // Read total nu data
327
    hid_t nu_group = open_group(group, "total_nu");
7,425✔
328
    total_nu_ = read_function(nu_group, "yield");
7,425✔
329
    close_group(nu_group);
7,425✔
330
  }
331

332
  // Read fission energy release data if present
333
  if (object_exists(group, "fission_energy_release")) {
28,496✔
334
    hid_t fer_group = open_group(group, "fission_energy_release");
7,425✔
335
    fission_q_prompt_ = read_function(fer_group, "q_prompt");
7,425✔
336
    fission_q_recov_ = read_function(fer_group, "q_recoverable");
7,425✔
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,425✔
341
    betas_ = read_function(fer_group, "betas");
7,425✔
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,425✔
346
    delayed_photons_ = read_function(fer_group, "delayed_photons");
7,425✔
347
    close_group(fer_group);
7,425✔
348
  }
349

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

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

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

367
  reaction_index_.fill(C_NONE);
28,496✔
368
  for (int i = 0; i < reactions_.size(); ++i) {
1,339,069✔
369
    const auto& rx {reactions_[i]};
1,310,573✔
370

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

374
    for (int t = 0; t < kTs_.size(); ++t) {
2,621,398✔
375
      int j = rx->xs_[t].threshold;
1,310,825✔
376
      int n = rx->xs_[t].value.size();
1,310,825✔
377
      auto xs = xt::adapt(rx->xs_[t].value);
1,310,825✔
378

379
      for (const auto& p : rx->products_) {
4,718,042✔
380
        if (p.particle_ == ParticleType::photon) {
3,407,217✔
381
          auto pprod = xt::view(xs_[t], xt::range(j, j + n), XS_PHOTON_PROD);
2,623,843✔
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 account
386
            // 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) {
595,705,737✔
391
                  double energy_prompt = (*prompt_photons)(E);
595,705,737✔
392
                  double energy_delayed = (*delayed_photons)(E);
595,705,737✔
393
                  f = (energy_prompt + energy_delayed) / (energy_prompt);
595,705,737✔
394
                }
395
              }
396
            }
397

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

403
      // Skip redundant reactions
404
      if (rx->redundant_)
1,310,825✔
405
        continue;
177,107✔
406

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

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

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

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

433
  // Determine number of delayed neutron precursors
434
  if (fissionable_) {
28,496✔
435
    for (const auto& product : fission_rx_[0]->products_) {
65,060✔
436
      if (product.emission_mode_ == EmissionMode::delayed) {
57,455✔
437
        ++n_precursor_;
44,346✔
438
      }
439
    }
440
  }
441

442
  // Calculate nu-fission cross section
443
  for (int t = 0; t < kTs_.size(); ++t) {
57,076✔
444
    if (fissionable_) {
28,580✔
445
      int n = grid_[t].energy.size();
7,689✔
446
      for (int i = 0; i < n; ++i) {
596,685,605✔
447
        double E = grid_[t].energy[i];
596,677,916✔
448
        xs_[t](i, XS_NU_FISSION) =
596,677,916✔
449
          nu(E, EmissionMode::total) * xs_[t](i, XS_FISSION);
596,677,916✔
450
      }
451
    }
452
  }
453

454
  if (settings::res_scat_on) {
28,496✔
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 are
473
      // resonant
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
}
28,496✔
500

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

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

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

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

518
    // Determine corresponding indices in nuclide grid to energies on
519
    // equal-logarithmic grid
520
    int j = 0;
30,527✔
521
    for (int k = 0; k <= M; ++k) {
244,889,054✔
522
      while (std::log(grid.energy[j + 1] / E_min) <= umesh(k)) {
1,012,607,795✔
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())
767,767,950✔
526
          break;
18,682✔
527
        ++j;
767,749,268✔
528
      }
529
      grid.grid_index[k] = j;
244,858,527✔
530
    }
531
  }
532
}
30,443✔
533

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

539
  switch (mode) {
904,843,910✔
540
  case EmissionMode::prompt:
8,198,016✔
541
    return (*fission_rx_[0]->products_[0].yield_)(E);
8,198,016✔
542
  case EmissionMode::delayed:
149,527,929✔
543
    if (n_precursor_ > 0 && settings::create_delayed_neutrons) {
149,527,929✔
544
      auto rx = fission_rx_[0];
147,494,697✔
545
      if (group >= 1 && group < rx->products_.size()) {
147,494,697✔
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,373,737✔
550

551
        for (int i = 1; i < rx->products_.size(); ++i) {
241,689,102✔
552
          // Skip any non-neutron products
553
          const auto& product = rx->products_[i];
211,315,365✔
554
          if (product.particle_ != ParticleType::neutron)
211,315,365✔
555
            continue;
29,072,943✔
556

557
          // Evaluate yield
558
          if (product.emission_mode_ == EmissionMode::delayed) {
182,242,422✔
559
            nu += (*product.yield_)(E);
182,242,422✔
560
          }
561
        }
562
        return nu;
30,373,737✔
563
      }
564
    } else {
565
      return 0.0;
2,033,232✔
566
    }
567
  case EmissionMode::total:
747,117,965✔
568
    if (total_nu_ && settings::create_delayed_neutrons) {
747,117,965✔
569
      return (*total_nu_)(E);
745,226,813✔
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
842,083,816✔
578
{
579
  // Get temperature index, grid index, and interpolation factor
580
  auto& micro {p.neutron_xs(index_)};
842,083,816✔
581
  int i_temp = micro.index_temp;
842,083,816✔
582
  int i_grid = micro.index_grid;
842,083,816✔
583
  double f = micro.interp_factor;
842,083,816✔
584

585
  if (i_temp >= 0) {
842,083,816✔
586
    const auto& xs = reactions_[0]->xs_[i_temp].value;
838,571,824✔
587
    micro.elastic = (1.0 - f) * xs[i_grid] + f * xs[i_grid + 1];
838,571,824✔
588
  }
589
}
842,083,816✔
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 =
721,932,147✔
762
        (1.0 - f) * xs(i_grid, XS_FISSION) + f * xs(i_grid + 1, XS_FISSION);
721,932,147✔
763

764
      // Calculate microscopic nuclide nu-fission cross section
765
      micro.nu_fission = (1.0 - f) * xs(i_grid, XS_NU_FISSION) +
721,932,147✔
766
                         f * xs(i_grid + 1, XS_NU_FISSION);
721,932,147✔
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 the
785
        // 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] =
967,505,594✔
795
              (1.0 - f) * rx_xs[i_grid] + f * rx_xs[i_grid + 1];
967,505,594✔
796
            continue;
967,505,594✔
797
          }
798

799
          int threshold = rx->xs_[i_temp].threshold;
2,036,330,442✔
800
          if (i_grid >= threshold) {
2,036,330,442✔
801
            micro.reaction[j] = (1.0 - f) * rx_xs[i_grid - threshold] +
311,800,718✔
802
                                f * rx_xs[i_grid - threshold + 1];
311,800,718✔
803
          } else if (j >= 3) {
1,724,529,724✔
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;
791,282,176✔
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 sab_scatter
823
  // and sab_elastic cross sections and correct the total and elastic cross
824
  // sections.
825

826
  if (i_sab >= 0)
2,147,483,647✔
827
    this->calculate_sab_xs(i_sab, sab_frac, p);
94,982,287✔
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,343,034,946✔
833
      this->calculate_urr_xs(micro.index_temp, p);
176,904,598✔
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)
94,982,287✔
841
{
842
  auto& micro {p.neutron_xs(index_)};
94,982,287✔
843

844
  // Set flag that S(a,b) treatment should be used for scattering
845
  micro.index_sab = i_sab;
94,982,287✔
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(
189,964,574✔
852
    p.E(), p.sqrtkT(), &i_temp, &elastic, &inelastic, p.current_seed());
94,982,287✔
853

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

858
  // Calculate free atom elastic cross section
859
  this->calculate_elastic_xs(p);
94,982,287✔
860

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

865
  // Save temperature index and thermal fraction
866
  micro.index_temp_sab = i_temp;
94,982,287✔
867
  micro.sab_frac = sab_frac;
94,982,287✔
868
}
94,982,287✔
869

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

875
  // Create a shorthand for the URR data
876
  const auto& urr = urr_data_[i_temp];
176,904,598✔
877

878
  // Determine the energy table
879
  int i_energy =
880
    lower_bound_index(urr.energy_.begin(), urr.energy_.end(), p.E());
176,904,598✔
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));
176,904,598✔
890

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

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

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

921
    // Calculate the elastic cross section/factor
922
    if ((urr.xs_values_(i_energy, i_low).elastic > 0.) &&
215,012,924✔
923
        (urr.xs_values_(i_energy + 1, i_up).elastic > 0.)) {
107,506,462✔
924
      elastic =
925
        std::exp((1. - f) * std::log(urr.xs_values_(i_energy, i_low).elastic) +
107,506,462✔
926
                 f * std::log(urr.xs_values_(i_energy + 1, i_up).elastic));
107,506,462✔
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.) &&
184,312,252✔
933
        (urr.xs_values_(i_energy + 1, i_up).fission > 0.)) {
76,805,790✔
934
      fission =
935
        std::exp((1. - f) * std::log(urr.xs_values_(i_energy, i_low).fission) +
76,805,790✔
936
                 f * std::log(urr.xs_values_(i_energy + 1, i_up).fission));
76,805,790✔
937
    } else {
938
      fission = 0.;
30,700,672✔
939
    }
940

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

952
  // Determine the treatment of inelastic scattering
953
  double inelastic = 0.;
176,904,598✔
954
  if (urr.inelastic_flag_ != C_NONE) {
176,904,598✔
955
    // get interpolation factor
956
    f = micro.interp_factor;
127,671,920✔
957

958
    // Determine inelastic scattering cross section
959
    Reaction* rx = reactions_[urr_inelastic_].get();
127,671,920✔
960
    int xs_index = micro.index_grid - rx->xs_[i_temp].threshold;
127,671,920✔
961
    if (xs_index >= 0) {
127,671,920✔
962
      inelastic = (1. - f) * rx->xs_[i_temp].value[xs_index] +
75,240,350✔
963
                  f * rx->xs_[i_temp].value[xs_index + 1];
75,240,350✔
964
    }
965
  }
966

967
  // Multiply by smooth cross-section if needed
968
  if (urr.multiply_smooth_) {
176,904,598✔
969
    calculate_elastic_xs(p);
82,462,508✔
970
    elastic *= micro.elastic;
82,462,508✔
971
    capture *= (micro.absorption - micro.fission);
82,462,508✔
972
    fission *= micro.fission;
82,462,508✔
973
  }
974

975
  // Check for negative values
976
  if (elastic < 0.) {
176,904,598✔
977
    elastic = 0.;
×
978
  }
979
  if (fission < 0.) {
176,904,598✔
980
    fission = 0.;
×
981
  }
982
  if (capture < 0.) {
176,904,598✔
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 table-provided
988
  // value
989
  micro.elastic = elastic;
176,904,598✔
990
  micro.absorption = capture + fission;
176,904,598✔
991
  micro.fission = fission;
176,904,598✔
992
  micro.total = elastic + inelastic + capture + fission;
176,904,598✔
993
  if (simulation::need_depletion_rx) {
176,904,598✔
994
    micro.reaction[0] = capture;
44,349,318✔
995
  }
996

997
  // Determine nu-fission cross-section
998
  if (fissionable_) {
176,904,598✔
999
    micro.nu_fission = nu(p.E(), EmissionMode::total) * micro.fission;
122,706,840✔
1000
  }
1001
}
176,904,598✔
1002

1003
std::pair<int64_t, double> Nuclide::find_temperature(double T) const
816✔
1004
{
1005
  assert(T >= 0.0);
680✔
1006

1007
  // Determine temperature index
1008
  int64_t i_temp = 0;
816✔
1009
  double f = 0.0;
816✔
1010
  double kT = K_BOLTZMANN * T;
816✔
1011
  int64_t n = kTs_.size();
816✔
1012
  switch (settings::temperature_method) {
816✔
1013
  case TemperatureMethod::NEAREST: {
816✔
1014
    double max_diff = INFTY;
816✔
1015
    for (int64_t 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
  assert(i_temp >= 0 && i_temp < n);
680✔
1043

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

1047
double Nuclide::collapse_rate(int MT, double temperature,
1,200✔
1048
  span<const double> energy, span<const double> flux) const
1049
{
1050
  assert(MT > 0);
1,000✔
1051
  assert(energy.size() > 0);
1,000✔
1052
  assert(energy.size() == flux.size() + 1);
1,000✔
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
  int64_t 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)
30,807✔
1085
{
1086
  if (attribute_exists(file_id, "version")) {
30,807✔
1087
    vector<int> version;
30,807✔
1088
    read_attribute(file_id, "version", version);
30,807✔
1089
    if (version[0] != HDF5_VERSION[0]) {
30,807✔
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 {
30,807✔
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
}
30,807✔
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,055✔
1114
{
1115
  if (data::nuclide_map.find(name) == data::nuclide_map.end() ||
85,699✔
1116
      data::nuclide_map.at(name) >= data::elements.size()) {
56,644✔
1117
    LibraryKey key {Library::Type::neutron, name};
29,055✔
1118
    const auto& it = data::library_map.find(key);
29,055✔
1119
    if (it == data::library_map.end()) {
29,055✔
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,031✔
1127
    const auto& filename = data::libraries[idx].path_;
29,031✔
1128
    write_message(6, "Reading {} from {}", name, filename);
29,031✔
1129

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

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

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

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

1147
    // Read elemental data, if necessary
1148
    if (settings::photon_transport) {
29,031✔
1149
      auto element = to_element(name);
2,658✔
1150
      if (data::element_map.find(element) == data::element_map.end() ||
2,658✔
1151
          data::element_map.at(element) >= data::elements.size()) {
1,329✔
1152
        // Read photon interaction data from HDF5 photon library
1153
        LibraryKey key {Library::Type::photon, element};
647✔
1154
        const auto& it = data::library_map.find(key);
647✔
1155
        if (it == data::library_map.end()) {
647✔
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;
647✔
1162
        const auto& filename = data::libraries[idx].path_;
647✔
1163
        write_message(6, "Reading {} from {} ", element, filename);
647✔
1164

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

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

1173
        close_group(group);
647✔
1174
        file_close(file_id);
647✔
1175
      }
647✔
1176
    }
1,329✔
1177
  }
29,055✔
1178
  return 0;
29,031✔
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()
7,047✔
1225
{
1226
  data::nuclides.clear();
7,047✔
1227
  data::nuclide_map.clear();
7,047✔
1228
}
7,047✔
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