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

openmc-dev / openmc / 38723868691

15 Oct 2026 10:57AM UTC coverage: 81.413%. First build
38723868691

Pull #3675

github

web-flow
Merge 7b3825ee0 into 46c4c6075
Pull Request #3675: Extend level scattering to support incident photons

20116 of 29272 branches covered (68.72%)

Branch coverage included in aggregate %.

89 of 110 new or added lines in 6 files covered. (80.91%)

62768 of 72535 relevant lines covered (86.53%)

42665756.41 hits per line

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

79.41
/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 "openmc/tensor.h"
21

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

26
namespace openmc {
27

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

32
namespace data {
33
array<double, 4> energy_min {0.0, 0.0, 0.0, 0.0};
34
array<double, 4> energy_max {INFTY, INFTY, 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)
24,976 ✔
52
{
53
  // Set index of nuclide in global vector
54
  index_ = data::nuclides.size();
24,976 ✔
55

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

60
  read_attribute(group, "Z", Z_);
24,976 ✔
61
  read_attribute(group, "A", A_);
24,976 ✔
62
  read_attribute(group, "metastable", metastable_);
24,976 ✔
63
  if (attribute_exists(group, "excitation_energy")) {
24,976 !
64
    read_attribute(group, "excitation_energy", excitation_energy_);
×
65
  }
66
  read_attribute(group, "atomic_weight_ratio", awr_);
24,976 ✔
67

68
  if (settings::run_mode == RunMode::VOLUME) {
24,976 ✔
69
    // Determine whether nuclide is fissionable and then exit
70
    int mt;
2,145 ✔
71
    hid_t rxs_group = open_group(group, "reactions");
2,145 ✔
72
    for (auto name : group_names(rxs_group)) {
81,360 ✔
73
      if (starts_with(name, "reaction_")) {
79,728 !
74
        hid_t rx_group = open_group(rxs_group, name.c_str());
79,728 ✔
75
        read_attribute(rx_group, "mt", mt);
79,728 ✔
76
        if (is_fission(mt)) {
79,728 ✔
77
          fissionable_ = true;
513 ✔
78
          break;
513 ✔
79
        }
80
      }
81
    }
81,360 ✔
82
    return;
2,145 ✔
83
  }
84

85
  // Determine temperatures available
86
  hid_t kT_group = open_group(group, "kTs");
22,831 ✔
87
  auto dset_names = dataset_names(kT_group);
22,831 ✔
88
  vector<double> temps_available;
22,831 ✔
89
  for (const auto& name : dset_names) {
45,902 ✔
90
    double T;
23,071 ✔
91
    read_dataset(kT_group, name.c_str(), T);
23,071 ✔
92
    temps_available.push_back(std::round(T / K_BOLTZMANN));
23,071 ✔
93
  }
94
  std::sort(temps_available.begin(), temps_available.end());
22,831 ✔
95

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

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

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

128
    // Add corresponding temperatures to vector
129
    for (auto it = T_min_it; it != T_max_it; ++it) {
4,772 ✔
130
      temps_to_read.push_back(std::round(*it));
2,386 ✔
131
    }
132
  }
133

134
  switch (settings::temperature_method) {
22,831 !
135
  case TemperatureMethod::NEAREST:
22,743 ✔
136
    // Find nearest temperatures
137
    for (double T_desired : temperature) {
43,763 ✔
138

139
      // Determine closest temperature
140
      double min_delta_T = INFTY;
21,020 ✔
141
      double T_actual = 0.0;
21,020 ✔
142
      for (auto T : temps_available) {
42,104 ✔
143
        double delta_T = std::abs(T - T_desired);
21,084 ✔
144
        if (delta_T < min_delta_T) {
21,084 ✔
145
          T_actual = T;
21,044 ✔
146
          min_delta_T = delta_T;
21,044 ✔
147
        }
148
      }
149

150
      if (std::abs(T_actual - T_desired) < settings::temperature_tolerance) {
21,020 !
151
        if (!contains(temps_to_read, std::round(T_actual))) {
21,020 ✔
152
          temps_to_read.push_back(std::round(T_actual));
20,357 ✔
153

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

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

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

219
  // Sort temperatures to read
220
  std::sort(temps_to_read.begin(), temps_to_read.end());
22,831 ✔
221

222
  data::temperature_min =
22,831 ✔
223
    std::min(data::temperature_min, static_cast<double>(temps_to_read.front()));
22,831 ✔
224
  data::temperature_max =
22,831 ✔
225
    std::max(data::temperature_max, static_cast<double>(temps_to_read.back()));
22,831 ✔
226

227
  hid_t energy_group = open_group(group, "energy");
22,831 ✔
228
  for (const auto& T : temps_to_read) {
45,718 ✔
229
    std::string dset {std::to_string(T) + "K"};
22,887 ✔
230

231
    // Determine exact kT values
232
    double kT;
22,887 ✔
233
    read_dataset(kT_group, dset.c_str(), kT);
22,887 ✔
234
    kTs_.push_back(kT);
22,887 ✔
235

236
    // Read energy grid
237
    grid_.emplace_back();
22,887 ✔
238
    read_dataset(energy_group, dset.c_str(), grid_.back().energy);
22,887 ✔
239
  }
22,887 ✔
240
  close_group(kT_group);
22,831 ✔
241

242
  // Check for 0K energy grid
243
  if (object_exists(energy_group, "0K")) {
22,831 ✔
244
    read_dataset(energy_group, "0K", energy_0K_);
4,145 ✔
245
  }
246
  close_group(energy_group);
22,831 ✔
247

248
  // Read reactions
249
  hid_t rxs_group = open_group(group, "reactions");
22,831 ✔
250
  for (auto name : group_names(rxs_group)) {
1,096,563 ✔
251
    if (starts_with(name, "reaction_")) {
1,073,732 !
252
      hid_t rx_group = open_group(rxs_group, name.c_str());
1,073,732 ✔
253
      reactions_.push_back(
2,147,464 ✔
254
        make_unique<Reaction>(rx_group, temps_to_read, name_));
2,147,464 ✔
255

256
      // Check for 0K elastic scattering
257
      const auto& rx = reactions_.back();
1,073,732 ✔
258
      if (rx->mt_ == ELASTIC) {
1,073,732 ✔
259
        if (object_exists(rx_group, "0K")) {
22,831 ✔
260
          hid_t temp_group = open_group(rx_group, "0K");
4,145 ✔
261
          read_dataset(temp_group, "xs", elastic_0K_);
4,145 ✔
262
          close_group(temp_group);
4,145 ✔
263
        }
264
      }
265
      close_group(rx_group);
1,073,732 ✔
266

267
      // Determine reaction indices for inelastic scattering reactions
268
      if (is_inelastic_scatter(rx->mt_) && !rx->redundant_) {
1,073,732 !
269
        index_inelastic_scatter_.push_back(reactions_.size() - 1);
525,996 ✔
270
      }
271
    }
272
  }
1,096,563 ✔
273
  close_group(rxs_group);
22,831 ✔
274

275
  // Read unresolved resonance probability tables if present
276
  if (object_exists(group, "urr")) {
22,831 ✔
277
    urr_present_ = true;
10,399 ✔
278
    urr_data_.reserve(temps_to_read.size());
10,399 ✔
279

280
    for (int i = 0; i < temps_to_read.size(); i++) {
20,798 ✔
281
      // Get temperature as a string
282
      std::string temp_str {std::to_string(temps_to_read[i]) + "K"};
10,399 ✔
283

284
      // Read probability tables for i-th temperature
285
      hid_t urr_group = open_group(group, ("urr/" + temp_str).c_str());
10,399 ✔
286
      urr_data_.emplace_back(urr_group);
10,399 ✔
287
      close_group(urr_group);
10,399 ✔
288

289
      // Check for negative values
290
      if (urr_data_[i].has_negative() && mpi::master) {
10,399 !
291
        warning("Negative value(s) found on probability table for nuclide " +
×
292
                name_ + " at " + temp_str);
×
293
      }
294
    }
10,399 ✔
295

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

311
      if (urr_data_[0].inelastic_flag_ > 0) {
10,399 ✔
312
        for (int i = 0; i < reactions_.size(); i++) {
329,254 ✔
313
          if (reactions_[i]->mt_ == urr_data_[0].inelastic_flag_) {
323,278 ✔
314
            urr_inelastic_ = i;
5,976 ✔
315
          }
316
        }
317

318
        // Abort if no corresponding inelastic reaction was found
319
        if (urr_inelastic_ == C_NONE) {
5,976 !
320
          fatal_error("Could no find inelastic reaction specified on "
×
321
                      "unresolved resonance probability table.");
322
        }
323
      }
324
    }
325
  }
326

327
  // Check for total nu data
328
  if (object_exists(group, "total_nu")) {
22,831 ✔
329
    // Read total nu data
330
    hid_t nu_group = open_group(group, "total_nu");
5,758 ✔
331
    total_nu_ = read_function(nu_group, "yield");
5,758 ✔
332
    close_group(nu_group);
5,758 ✔
333
  }
334

335
  // Read fission energy release data if present
336
  if (object_exists(group, "fission_energy_release")) {
22,831 ✔
337
    hid_t fer_group = open_group(group, "fission_energy_release");
5,758 ✔
338
    fission_q_prompt_ = read_function(fer_group, "q_prompt");
5,758 ✔
339
    fission_q_recov_ = read_function(fer_group, "q_recoverable");
5,758 ✔
340

341
    // Read fission fragment and delayed beta energy release. This is needed for
342
    // energy normalization in k-eigenvalue calculations
343
    fragments_ = read_function(fer_group, "fragments");
5,758 ✔
344
    betas_ = read_function(fer_group, "betas");
5,758 ✔
345

346
    // We need prompt/delayed photon energy release for scaling fission photon
347
    // production
348
    prompt_photons_ = read_function(fer_group, "prompt_photons");
5,758 ✔
349
    delayed_photons_ = read_function(fer_group, "delayed_photons");
5,758 ✔
350
    close_group(fer_group);
5,758 ✔
351
  }
352

353
  this->create_derived(prompt_photons_.get(), delayed_photons_.get());
22,831 ✔
354
}
22,831 ✔
355

356
Nuclide::~Nuclide()
24,976 ✔
357
{
358
  data::nuclide_map.erase(name_);
24,976 ✔
359
}
24,976 ✔
360

361
void Nuclide::create_derived(
22,831 ✔
362
  const Function1D* prompt_photons, const Function1D* delayed_photons)
363
{
364
  for (const auto& grid : grid_) {
45,718 ✔
365
    // Allocate and initialize cross section
366
    xs_.push_back(tensor::zeros<double>({grid.energy.size(), 5}));
68,661 ✔
367
  }
368

369
  reaction_index_.fill(C_NONE);
22,831 ✔
370
  for (int i = 0; i < reactions_.size(); ++i) {
1,096,563 ✔
371
    const auto& rx {reactions_[i]};
1,073,732 ✔
372

373
    // Set entry in direct address table for reaction
374
    reaction_index_[rx->mt_] = i;
1,073,732 ✔
375

376
    for (int t = 0; t < kTs_.size(); ++t) {
2,147,632 ✔
377
      int j = rx->xs_[t].threshold;
1,073,900 ✔
378
      int n = rx->xs_[t].value.size();
1,073,900 ✔
379
      auto xs = tensor::Tensor<double>(
1,073,900 ✔
380
        rx->xs_[t].value.data(), rx->xs_[t].value.size());
1,073,900 ✔
381
      for (const auto& p : rx->products_) {
3,870,913 ✔
382
        if (p.particle_.is_photon()) {
2,797,013 ✔
383
          for (int k = 0; k < n; ++k) {
2,147,483,647 ✔
384
            double E = grid_[t].energy[k + j];
2,147,483,647 !
385

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

399
            xs_[t](j + k, XS_PHOTON_PROD) += f * xs[k] * (*p.yield_)(E);
2,147,483,647 ✔
400
          }
401
        }
402
      }
403

404
      // Skip redundant reactions
405
      if (rx->redundant_)
1,073,900 ✔
406
        continue;
144,597 ✔
407

408
      // Add contribution to total cross section
409
      xs_[t].slice(tensor::range(j, j + n), XS_TOTAL) += xs;
929,303 ✔
410

411
      // Add contribution to absorption cross section
412
      if (is_disappearance(rx->mt_)) {
929,303 ✔
413
        xs_[t].slice(tensor::range(j, j + n), XS_ABSORPTION) += xs;
739,942 ✔
414
      }
415

416
      if (is_fission(rx->mt_)) {
929,303 ✔
417
        fissionable_ = true;
10,449 ✔
418
        xs_[t].slice(tensor::range(j, j + n), XS_FISSION) += xs;
10,449 ✔
419
        xs_[t].slice(tensor::range(j, j + n), XS_ABSORPTION) += xs;
10,449 ✔
420

421
        // Keep track of fission reactions
422
        if (t == 0) {
10,449 ✔
423
          fission_rx_.push_back(rx.get());
10,393 ✔
424
          if (rx->mt_ == N_F)
10,393 ✔
425
            has_partial_fission_ = true;
1,505 ✔
426
        }
427
      }
428
    }
1,073,900 ✔
429
  }
430

431
  // Determine number of delayed neutron precursors
432
  if (fissionable_) {
22,831 ✔
433
    for (const auto& product : fission_rx_[0]->products_) {
50,407 ✔
434
      if (product.emission_mode_ == EmissionMode::delayed) {
44,529 ✔
435
        ++n_precursor_;
34,428 ✔
436
      }
437
    }
438
  }
439

440
  // Calculate nu-fission cross section
441
  for (int t = 0; t < kTs_.size(); ++t) {
45,718 ✔
442
    if (fissionable_) {
22,887 ✔
443
      int n = grid_[t].energy.size();
5,934 ✔
444
      for (int i = 0; i < n; ++i) {
453,764,399 ✔
445
        double E = grid_[t].energy[i];
453,758,465 ✔
446
        xs_[t](i, XS_NU_FISSION) =
453,758,465 ✔
447
          nu(E, EmissionMode::total) * xs_[t](i, XS_FISSION);
453,758,465 ✔
448
      }
449
    }
450
  }
451

452
  if (settings::res_scat_on) {
22,831 ✔
453
    // Determine if this nuclide should be treated as a resonant scatterer
454
    if (!settings::res_scat_nuclides.empty()) {
40 !
455
      // If resonant nuclides were specified, check the list explicitly
456
      for (const auto& name : settings::res_scat_nuclides) {
100 ✔
457
        if (name_ == name) {
90 ✔
458
          resonant_ = true;
30 ✔
459

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

475
    if (resonant_) {
40 ✔
476
      // Build CDF for 0K elastic scattering
477
      double xs_cdf_sum = 0.0;
30 ✔
478
      xs_cdf_.resize(energy_0K_.size());
30 ✔
479
      xs_cdf_[0] = 0.0;
30 ✔
480

481
      const auto& E = energy_0K_;
30 ✔
482
      auto& xs = elastic_0K_;
30 ✔
483
      for (int i = 0; i < E.size() - 1; ++i) {
5,570,360 ✔
484
        // Negative cross sections result in a CDF that is not monotonically
485
        // increasing. Set all negative xs values to zero.
486
        if (xs[i] < 0.0)
5,570,330 !
487
          xs[i] = 0.0;
×
488

489
        // build xs cdf
490
        xs_cdf_sum +=
11,140,660 ✔
491
          (std::sqrt(E[i]) * xs[i] + std::sqrt(E[i + 1]) * xs[i + 1]) / 2.0 *
5,570,330 ✔
492
          (E[i + 1] - E[i]);
5,570,330 ✔
493
        xs_cdf_[i + 1] = xs_cdf_sum;
5,570,330 ✔
494
      }
495
    }
496
  }
497
}
22,831 ✔
498

499
void Nuclide::init_grid()
25,101 ✔
500
{
501
  int neutron = ParticleType::neutron().transport_index();
25,101 ✔
502
  double E_min = data::energy_min[neutron];
25,101 ✔
503
  double E_max = data::energy_max[neutron];
25,101 ✔
504
  int M = settings::n_log_bins;
25,101 ✔
505

506
  // Determine equal-logarithmic energy spacing
507
  double spacing = std::log(E_max / E_min) / M;
25,101 ✔
508

509
  // Create equally log-spaced energy grid
510
  auto umesh = tensor::linspace(0.0, M * spacing, M + 1);
25,101 ✔
511

512
  for (auto& grid : grid_) {
50,258 ✔
513
    // Resize array for storing grid indices
514
    grid.grid_index.resize(M + 1);
25,157 ✔
515

516
    // Determine corresponding indices in nuclide grid to energies on
517
    // equal-logarithmic grid
518
    int j = 0;
519
    for (int k = 0; k <= M; ++k) {
201,666,314 ✔
520
      while (std::log(grid.energy[j + 1] / E_min) <= umesh(k)) {
806,593,952 ✔
521
        // Ensure that for isotopes where maxval(grid.energy) << E_max that
522
        // there are no out-of-bounds issues.
523
        if (j + 2 == grid.energy.size())
604,968,211 ✔
524
          break;
525
        ++j;
526
      }
527
      grid.grid_index[k] = j;
201,641,157 ✔
528
    }
529
  }
530
}
25,101 ✔
531

532
double Nuclide::nu(double E, EmissionMode mode, int group) const
817,903,826 ✔
533
{
534
  if (!fissionable_)
817,903,826 ✔
535
    return 0.0;
536

537
  switch (mode) {
796,614,410 !
538
  case EmissionMode::prompt:
5,548,680 ✔
539
    return (*fission_rx_[0]->products_[0].yield_)(E);
5,548,680 ✔
540
  case EmissionMode::delayed:
140,169,708 ✔
541
    if (n_precursor_ > 0 && settings::create_delayed_neutrons) {
140,169,708 !
542
      auto rx = fission_rx_[0];
138,813,076 ✔
543
      if (group >= 1 && group < rx->products_.size()) {
138,813,076 !
544
        // If delayed group specified, determine yield immediately
545
        return (*rx->products_[group].yield_)(E);
104,644,352 ✔
546
      } else {
547
        double nu {0.0};
548

549
        for (int i = 1; i < rx->products_.size(); ++i) {
272,453,131 ✔
550
          // Skip any non-neutron products
551
          const auto& product = rx->products_[i];
238,284,407 ✔
552
          if (!product.particle_.is_neutron())
238,284,407 ✔
553
            continue;
33,272,063 ✔
554

555
          // Evaluate yield
556
          if (product.emission_mode_ == EmissionMode::delayed) {
205,012,344 !
557
            nu += (*product.yield_)(E);
205,012,344 ✔
558
          }
559
        }
560
        return nu;
34,168,724 ✔
561
      }
562
    } else {
563
      return 0.0;
564
    }
565
  case EmissionMode::total:
650,896,022 ✔
566
    if (total_nu_ && settings::create_delayed_neutrons) {
650,896,022 !
567
      return (*total_nu_)(E);
649,634,702 ✔
568
    } else {
569
      return (*fission_rx_[0]->products_[0].yield_)(E);
1,261,320 ✔
570
    }
571
  }
572
  UNREACHABLE();
×
573
}
574

575
void Nuclide::calculate_elastic_xs(Particle& p) const
1,169,493,976 ✔
576
{
577
  // Get temperature index, grid index, and interpolation factor
578
  auto& micro {p.neutron_xs(index_)};
1,169,493,976 ✔
579
  int i_temp = micro.index_temp;
1,169,493,976 ✔
580
  int i_grid = micro.index_grid;
1,169,493,976 ✔
581
  double f = micro.interp_factor;
1,169,493,976 ✔
582

583
  if (i_temp >= 0) {
1,169,493,976 ✔
584
    const auto& xs = reactions_[0]->xs_[i_temp].value;
1,149,066,928 ✔
585
    micro.elastic = (1.0 - f) * xs[i_grid] + f * xs[i_grid + 1];
1,149,066,928 ✔
586
  }
587
}
1,169,493,976 ✔
588

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

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

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

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

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

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

623
  // Check to see if there is multipole data present at this energy
624
  bool use_mp = false;
2,147,483,647 ✔
625
  if (multipole_) {
2,147,483,647 ✔
626
    use_mp = (p.E() >= multipole_->E_min_ && p.E() <= multipole_->E_max_);
470,400,568 !
627
  }
628

629
  // Evaluate multipole or interpolate
630
  if (use_mp) {
322,833,504 ✔
631
    // Call multipole kernel
632
    double sig_s, sig_a, sig_f;
322,833,504 ✔
633
    std::tie(sig_s, sig_a, sig_f) = multipole_->evaluate(p.E(), p.sqrtkT());
322,833,504 ✔
634

635
    micro.total = sig_s + sig_a;
322,833,504 ✔
636
    micro.elastic = sig_s;
322,833,504 ✔
637
    micro.absorption = sig_a;
322,833,504 ✔
638
    micro.fission = sig_f;
322,833,504 ✔
639
    micro.nu_fission =
645,667,008 ✔
640
      fissionable_ ? sig_f * this->nu(p.E(), EmissionMode::total) : 0.0;
322,833,504 ✔
641

642
    if (simulation::need_depletion_rx) {
322,833,504 !
643
      // Only non-zero reaction is (n,gamma)
644
      micro.reaction[0] = sig_a - sig_f;
×
645

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

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

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

690
    case TemperatureMethod::INTERPOLATION:
1,103,352 ✔
691
      // If current kT outside of the bounds of available, snap to the bound
692
      if (kT < kTs_.front()) {
1,103,352 ✔
693
        i_temp = 0;
694
        break;
695
      }
696
      if (kT >= kTs_.back()) {
802,824 ✔
697
        i_temp = kTs_.size() - 1;
156,832 ✔
698
        break;
156,832 ✔
699
      }
700

701
      // Find temperatures that bound the actual temperature
702
      for (i_temp = 0; i_temp < kTs_.size() - 1; ++i_temp) {
645,992 !
703
        if (kTs_[i_temp] <= kT && kT < kTs_[i_temp + 1])
645,992 !
704
          break;
705
      }
706

707
      // Randomly sample between temperature i and i+1
708
      f = (kT - kTs_[i_temp]) / (kTs_[i_temp + 1] - kTs_[i_temp]);
645,992 ✔
709
      if (f > prn(p.current_seed()))
645,992 ✔
710
        ++i_temp;
299,544 ✔
711
      break;
712
    }
713

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

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

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

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

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

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

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

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

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

757
    if (fissionable_) {
2,147,483,647 ✔
758
      // Calculate microscopic nuclide total cross section
759
      micro.fission =
838,852,683 ✔
760
        (1.0 - f) * xs(i_grid, XS_FISSION) + f * xs(i_grid + 1, XS_FISSION);
838,852,683 ✔
761

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

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

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

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

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

797
          int threshold = rx->xs_[i_temp].threshold;
2,147,483,647 ✔
798
          if (i_grid >= threshold) {
2,147,483,647 ✔
799
            micro.reaction[j] = (1.0 - f) * rx_xs[i_grid - threshold] +
606,430,158 ✔
800
                                f * rx_xs[i_grid - threshold + 1];
606,430,158 ✔
801
          } else if (j >= 3) {
2,147,483,647 ✔
802
            // One can show that the the threshold for (n,(x+1)n) is always
803
            // higher than the threshold for (n,xn). Thus, if we are below
804
            // the threshold for, e.g., (n,2n), there is no reason to check
805
            // the threshold for (n,3n) and (n,4n).
806
            break;
807
          }
808
        }
809
      }
810
    }
811
  }
812

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

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

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

824
  if (i_sab >= 0)
2,147,483,647 ✔
825
    this->calculate_sab_xs(i_sab, sab_frac, p);
109,564,584 ✔
826

827
  // If the particle is in the unresolved resonance range and there are
828
  // probability tables, we need to determine cross sections from the table
829
  if (settings::urr_ptables_on && urr_present_ && !use_mp) {
2,147,483,647 ✔
830
    if (urr_data_[micro.index_temp].energy_in_bounds(p.E()))
1,972,255,200 ✔
831
      this->calculate_urr_xs(micro.index_temp, p);
317,876,256 ✔
832
  }
833

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

838
void Nuclide::calculate_sab_xs(int i_sab, double sab_frac, Particle& p)
109,564,584 ✔
839
{
840
  auto& micro {p.neutron_xs(index_)};
109,564,584 ✔
841

842
  // Set flag that S(a,b) treatment should be used for scattering
843
  micro.index_sab = i_sab;
109,564,584 ✔
844

845
  // Calculate the S(a,b) cross section
846
  int i_temp;
109,564,584 ✔
847
  double elastic;
109,564,584 ✔
848
  double inelastic;
109,564,584 ✔
849
  data::thermal_scatt[i_sab]->calculate_xs(
109,564,584 ✔
850
    p.E(), p.sqrtkT(), &i_temp, &elastic, &inelastic, p.current_seed());
109,564,584 ✔
851

852
  // Store the S(a,b) cross sections.
853
  micro.thermal = sab_frac * (elastic + inelastic);
109,564,584 ✔
854
  micro.thermal_elastic = sab_frac * elastic;
109,564,584 ✔
855

856
  // Calculate free atom elastic cross section
857
  this->calculate_elastic_xs(p);
109,564,584 ✔
858

859
  // Correct total and elastic cross sections
860
  micro.total = micro.total + micro.thermal - sab_frac * micro.elastic;
109,564,584 ✔
861
  micro.elastic = micro.thermal + (1.0 - sab_frac) * micro.elastic;
109,564,584 ✔
862

863
  // Save temperature index and thermal fraction
864
  micro.index_temp_sab = i_temp;
109,564,584 ✔
865
  micro.sab_frac = sab_frac;
109,564,584 ✔
866
}
109,564,584 ✔
867

868
void Nuclide::calculate_urr_xs(int i_temp, Particle& p) const
317,876,256 ✔
869
{
870
  auto& micro = p.neutron_xs(index_);
317,876,256 ✔
871
  micro.use_ptable = true;
317,876,256 ✔
872

873
  // Create a shorthand for the URR data
874
  const auto& urr = urr_data_[i_temp];
317,876,256 ✔
875

876
  // Determine the energy table
877
  int i_energy =
317,876,256 ✔
878
    lower_bound_index(urr.energy_.begin(), urr.energy_.end(), p.E());
317,876,256 ✔
879

880
  // Sample the probability table using the cumulative distribution
881

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

889
  // Warning: this assumes row-major order of cdf_values_
890
  int i_low = upper_bound_index(&urr.cdf_values_(i_energy, 0),
635,752,512 ✔
891
                &urr.cdf_values_(i_energy, 0) + urr.n_cdf(), r) +
635,752,512 !
892
              1;
317,876,256 ✔
893
  int i_up = upper_bound_index(&urr.cdf_values_(i_energy + 1, 0),
317,876,256 ✔
894
               &urr.cdf_values_(i_energy + 1, 0) + urr.n_cdf(), r) +
317,876,256 ✔
895
             1;
317,876,256 ✔
896

897
  // Determine elastic, fission, and capture cross sections from the
898
  // probability table
899
  double elastic = 0.;
317,876,256 ✔
900
  double fission = 0.;
317,876,256 ✔
901
  double capture = 0.;
317,876,256 ✔
902
  double f;
317,876,256 ✔
903
  if (urr.interp_ == Interpolation::lin_lin) {
317,876,256 ✔
904
    // Determine the interpolation factor on the table
905
    f = (p.E() - urr.energy_[i_energy]) /
172,593,080 ✔
906
        (urr.energy_[i_energy + 1] - urr.energy_[i_energy]);
172,593,080 ✔
907

908
    elastic = (1. - f) * urr.xs_values_(i_energy, i_low).elastic +
172,593,080 ✔
909
              f * urr.xs_values_(i_energy + 1, i_up).elastic;
172,593,080 ✔
910
    fission = (1. - f) * urr.xs_values_(i_energy, i_low).fission +
172,593,080 ✔
911
              f * urr.xs_values_(i_energy + 1, i_up).fission;
172,593,080 ✔
912
    capture = (1. - f) * urr.xs_values_(i_energy, i_low).n_gamma +
172,593,080 ✔
913
              f * urr.xs_values_(i_energy + 1, i_up).n_gamma;
172,593,080 ✔
914
  } else if (urr.interp_ == Interpolation::log_log) {
145,283,176 !
915
    // Determine interpolation factor on the table
916
    f = std::log(p.E() / urr.energy_[i_energy]) /
145,283,176 ✔
917
        std::log(urr.energy_[i_energy + 1] / urr.energy_[i_energy]);
145,283,176 ✔
918

919
    // Calculate the elastic cross section/factor
920
    if ((urr.xs_values_(i_energy, i_low).elastic > 0.) &&
145,283,176 !
921
        (urr.xs_values_(i_energy + 1, i_up).elastic > 0.)) {
145,283,176 !
922
      elastic =
145,283,176 ✔
923
        std::exp((1. - f) * std::log(urr.xs_values_(i_energy, i_low).elastic) +
145,283,176 ✔
924
                 f * std::log(urr.xs_values_(i_energy + 1, i_up).elastic));
145,283,176 ✔
925
    } else {
926
      elastic = 0.;
927
    }
928

929
    // Calculate the fission cross section/factor
930
    if ((urr.xs_values_(i_energy, i_low).fission > 0.) &&
145,283,176 ✔
931
        (urr.xs_values_(i_energy + 1, i_up).fission > 0.)) {
83,794,742 !
932
      fission =
83,794,742 ✔
933
        std::exp((1. - f) * std::log(urr.xs_values_(i_energy, i_low).fission) +
83,794,742 ✔
934
                 f * std::log(urr.xs_values_(i_energy + 1, i_up).fission));
83,794,742 ✔
935
    } else {
936
      fission = 0.;
937
    }
938

939
    // Calculate the capture cross section/factor
940
    if ((urr.xs_values_(i_energy, i_low).n_gamma > 0.) &&
145,283,176 !
941
        (urr.xs_values_(i_energy + 1, i_up).n_gamma > 0.)) {
145,283,176 !
942
      capture =
145,283,176 ✔
943
        std::exp((1. - f) * std::log(urr.xs_values_(i_energy, i_low).n_gamma) +
145,283,176 ✔
944
                 f * std::log(urr.xs_values_(i_energy + 1, i_up).n_gamma));
145,283,176 ✔
945
    } else {
946
      capture = 0.;
947
    }
948
  }
949

950
  // Determine the treatment of inelastic scattering
951
  double inelastic = 0.;
317,876,256 ✔
952
  if (urr.inelastic_flag_ != C_NONE) {
317,876,256 ✔
953
    // get interpolation factor
954
    f = micro.interp_factor;
166,652,885 ✔
955

956
    // Determine inelastic scattering cross section
957
    Reaction* rx = reactions_[urr_inelastic_].get();
166,652,885 ✔
958
    int xs_index = micro.index_grid - rx->xs_[i_temp].threshold;
166,652,885 ✔
959
    if (xs_index >= 0) {
166,652,885 ✔
960
      inelastic = (1. - f) * rx->xs_[i_temp].value[xs_index] +
79,928,654 ✔
961
                  f * rx->xs_[i_temp].value[xs_index + 1];
79,928,654 ✔
962
    }
963
  }
964

965
  // Multiply by smooth cross-section if needed
966
  if (urr.multiply_smooth_) {
317,876,256 ✔
967
    calculate_elastic_xs(p);
89,387,590 ✔
968
    elastic *= micro.elastic;
89,387,590 ✔
969
    capture *= (micro.absorption - micro.fission);
89,387,590 ✔
970
    fission *= micro.fission;
89,387,590 ✔
971
  }
972

973
  // Check for negative values
974
  if (elastic < 0.) {
317,876,256 !
975
    elastic = 0.;
×
976
  }
977
  if (fission < 0.) {
317,876,256 !
978
    fission = 0.;
×
979
  }
980
  if (capture < 0.) {
317,876,256 !
981
    capture = 0.;
×
982
  }
983

984
  // Set elastic, absorption, fission, total, and capture x/s. Note that the
985
  // total x/s is calculated as a sum of partials instead of the
986
  // table-provided value
987
  micro.elastic = elastic;
317,876,256 ✔
988
  micro.absorption = capture + fission;
317,876,256 ✔
989
  micro.fission = fission;
317,876,256 ✔
990
  micro.total = elastic + inelastic + capture + fission;
317,876,256 ✔
991
  if (simulation::need_depletion_rx) {
317,876,256 ✔
992
    micro.reaction[0] = capture;
154,109,921 ✔
993
  }
994

995
  // Determine nu-fission cross-section
996
  if (fissionable_) {
317,876,256 ✔
997
    micro.nu_fission = nu(p.E(), EmissionMode::total) * micro.fission;
151,007,121 ✔
998
  }
999
}
317,876,256 ✔
1000

1001
std::pair<int64_t, double> Nuclide::find_temperature(double T) const
1,576 ✔
1002
{
1003
  assert(T >= 0.0);
1,576 !
1004

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

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

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

1040
  assert(i_temp >= 0 && i_temp < n);
1,576 !
1041

1042
  return {i_temp, f};
1,576 ✔
1043
}
1044

1045
double Nuclide::collapse_rate(int MT, double temperature,
2,072 ✔
1046
  span<const double> energy, span<const double> flux) const
1047
{
1048
  assert(MT > 0);
2,072 !
1049
  assert(energy.size() > 0);
2,072 !
1050
  assert(energy.size() == flux.size() + 1);
2,072 !
1051

1052
  int i_rx = reaction_index_[MT];
2,072 ✔
1053
  if (i_rx < 0)
2,072 ✔
1054
    return 0.0;
1055
  const auto& rx = reactions_[i_rx];
1,576 ✔
1056

1057
  // Determine temperature index
1058
  int64_t i_temp;
1,576 ✔
1059
  double f;
1,576 ✔
1060
  std::tie(i_temp, f) = this->find_temperature(temperature);
1,576 ✔
1061

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

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

1078
//==============================================================================
1079
// Non-member functions
1080
//==============================================================================
1081

1082
void check_data_version(hid_t file_id)
27,211 ✔
1083
{
1084
  if (attribute_exists(file_id, "version")) {
27,211 !
1085
    vector<int> version;
27,211 ✔
1086
    read_attribute(file_id, "version", version);
27,211 ✔
1087
    // A newer minor version may use datasets or attributes this build does
1088
    // not know about, and a missing attribute is not diagnosed anywhere below
1089
    // -- read_attr does not check the HDF5 status -- so refuse it here rather
1090
    // than transport on whatever the uninitialised member happened to hold.
1091
    if (version[0] != HDF5_VERSION[0] || version[1] > HDF5_VERSION[1]) {
27,211 !
1092
      fatal_error("HDF5 data format uses version " +
×
1093
                  std::to_string(version[0]) + "." +
×
1094
                  std::to_string(version[1]) +
×
1095
                  " whereas your installation of "
1096
                  "OpenMC expects version " +
×
NEW
1097
                  std::to_string(HDF5_VERSION[0]) + "." +
×
NEW
1098
                  std::to_string(HDF5_VERSION[1]) + " or earlier.");
×
1099
    }
1100
  } else {
×
1101
    fatal_error("HDF5 data does not indicate a version. Your installation of "
×
1102
                "OpenMC expects version " +
×
1103
                std::to_string(HDF5_VERSION[0]) + ".x data.");
×
1104
  }
1105
}
27,211 ✔
1106

1107
extern "C" size_t nuclides_size()
16 ✔
1108
{
1109
  return data::nuclides.size();
16 ✔
1110
}
1111

1112
//==============================================================================
1113
// C API
1114
//==============================================================================
1115

1116
extern "C" int openmc_load_nuclide(const char* name, const double* temps, int n)
25,098 ✔
1117
{
1118
  if (data::nuclide_map.find(name) == data::nuclide_map.end() ||
73,608 ✔
1119
      data::nuclide_map.at(name) >= data::nuclides.size()) {
95,246 !
1120
    LibraryKey key {Library::Type::neutron, name};
25,010 ✔
1121
    const auto& it = data::library_map.find(key);
25,010 ✔
1122
    if (it == data::library_map.end()) {
25,010 ✔
1123
      set_errmsg(
34 ✔
1124
        "Nuclide '" + std::string {name} + "' is not present in library.");
68 ✔
1125
      return OPENMC_E_DATA;
34 ✔
1126
    }
1127

1128
    // Get filename for library containing nuclide
1129
    int idx = it->second;
24,976 ✔
1130
    const auto& filename = data::libraries[idx].path_;
24,976 ✔
1131
    write_message(6, "Reading {} from {}", name, filename);
24,976 ✔
1132

1133
    // Open file and make sure version is sufficient
1134
    hid_t file_id = file_open(filename, 'r');
24,976 ✔
1135
    check_data_version(file_id);
24,976 ✔
1136

1137
    // Read nuclide data from HDF5
1138
    hid_t group = open_group(file_id, name);
24,976 ✔
1139
    vector<double> temperature {temps, temps + n};
24,976 ✔
1140
    data::nuclides.push_back(make_unique<Nuclide>(group, temperature));
49,952 ✔
1141

1142
    close_group(group);
24,976 ✔
1143
    file_close(file_id);
24,976 ✔
1144

1145
    // Read multipole file into the appropriate entry on the nuclides array
1146
    int i_nuclide = data::nuclide_map.at(name);
49,952 ✔
1147
    if (settings::temperature_multipole)
24,976 ✔
1148
      read_multipole_data(i_nuclide);
852 ✔
1149

1150
    // Read elemental data, if necessary
1151
    if (settings::photon_transport) {
24,976 ✔
1152
      auto element = to_element(name);
2,196 ✔
1153
      if (data::element_map.find(element) == data::element_map.end() ||
4,392 !
1154
          data::element_map.at(element) >= data::elements.size()) {
2,196 ✔
1155
        // Read photon interaction data from HDF5 photon library
1156
        LibraryKey key {Library::Type::photon, element};
1,200 ✔
1157
        const auto& it = data::library_map.find(key);
1,200 !
1158
        if (it == data::library_map.end()) {
1,200 !
1159
          set_errmsg("Element '" + std::string {element} +
×
1160
                     "' is not present in library.");
1161
          return OPENMC_E_DATA;
×
1162
        }
1163

1164
        int idx = it->second;
1,200 ✔
1165
        const auto& filename = data::libraries[idx].path_;
1,200 ✔
1166
        write_message(6, "Reading {} from {} ", element, filename);
1,200 ✔
1167

1168
        // Open file and make sure version is sufficient
1169
        hid_t file_id = file_open(filename, 'r');
1,200 ✔
1170
        check_data_version(file_id);
1,200 ✔
1171

1172
        // Read element data from HDF5
1173
        hid_t group = open_group(file_id, element.c_str());
1,200 ✔
1174
        data::elements.push_back(make_unique<PhotonInteraction>(group));
2,400 ✔
1175

1176
        close_group(group);
1,200 ✔
1177
        file_close(file_id);
1,200 ✔
1178
      }
1,200 ✔
1179
    }
2,196 ✔
1180
  }
25,010 ✔
1181
  return 0;
1182
}
1183

1184
extern "C" int openmc_get_nuclide_index(const char* name, int* index)
1,032 ✔
1185
{
1186
  auto it = data::nuclide_map.find(name);
2,064 ✔
1187
  if (it == data::nuclide_map.end()) {
1,032 ✔
1188
    set_errmsg(
8 ✔
1189
      "No nuclide named '" + std::string {name} + "' has been loaded.");
16 ✔
1190
    return OPENMC_E_DATA;
8 ✔
1191
  }
1192
  *index = it->second;
1,024 ✔
1193
  return 0;
1,024 ✔
1194
}
1195

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

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

1216
  try {
2,072 ✔
1217
    *xs = data::nuclides[index]->collapse_rate(
4,144 ✔
1218
      MT, temperature, {energy, energy + n + 1}, {flux, flux + n});
2,072 ✔
1219
  } catch (const std::out_of_range& e) {
×
1220
    set_errmsg(e.what());
×
1221
    return OPENMC_E_OUT_OF_BOUNDS;
×
1222
  }
×
1223
  return 0;
2,072 ✔
1224
}
1225

1226
void nuclides_clear()
6,915 ✔
1227
{
1228
  data::nuclides.clear();
6,915 ✔
1229
  data::nuclide_map.clear();
6,915 ✔
1230
}
6,915 ✔
1231

1232
bool multipole_in_range(const Nuclide& nuc, double E)
714,032 ✔
1233
{
1234
  return E >= nuc.multipole_->E_min_ && E <= nuc.multipole_->E_max_;
714,032 !
1235
}
1236

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

© 2026 Coveralls, Inc