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

openmc-dev / openmc / 23560024828

25 Mar 2026 07:28PM UTC coverage: 80.336% (-1.0%) from 81.326%
23560024828

Pull #3702

github

web-flow
Merge 0f768b2d1 into 6cd39073b
Pull Request #3702: Random Ray Kinetic Simulation Mode

17625 of 25263 branches covered (69.77%)

Branch coverage included in aggregate %.

926 of 1957 new or added lines in 21 files covered. (47.32%)

461 existing lines in 28 files now uncovered.

57995 of 68867 relevant lines covered (84.21%)

50654337.8 hits per line

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

80.23
/src/mgxs.cpp
1
#include "openmc/mgxs.h"
2

3
#include <algorithm>
4
#include <cmath>
5
#include <cstdlib>
6
#include <sstream>
7

8
#include "openmc/tensor.h"
9
#include <fmt/core.h>
10

11
#include "openmc/error.h"
12
#include "openmc/math_functions.h"
13
#include "openmc/mgxs_interface.h"
14
#include "openmc/nuclide.h"
15
#include "openmc/random_lcg.h"
16
#include "openmc/settings.h"
17
#include "openmc/string_utils.h"
18

19
namespace openmc {
20

21
//==============================================================================
22
// Mgxs base-class methods
23
//==============================================================================
24

25
void Mgxs::init(const std::string& in_name, double in_awr,
6,089✔
26
  const vector<double>& in_kTs, bool in_fissionable,
27
  AngleDistributionType in_scatter_format, bool in_is_isotropic,
28
  const vector<double>& in_polar, const vector<double>& in_azimuthal)
29
{
30
  // Set the metadata
31
  name = in_name;
6,089✔
32
  awr = in_awr;
6,089✔
33
  kTs = tensor::Tensor<double>(in_kTs.data(), in_kTs.size());
6,089✔
34
  fissionable = in_fissionable;
6,089✔
35
  scatter_format = in_scatter_format;
6,089✔
36
  xs.resize(in_kTs.size());
6,089✔
37
  is_isotropic = in_is_isotropic;
6,089✔
38
  n_pol = in_polar.size();
6,089✔
39
  n_azi = in_azimuthal.size();
6,089✔
40
  polar = in_polar;
6,089✔
41
  azimuthal = in_azimuthal;
6,089✔
42
}
6,089✔
43

44
//==============================================================================
45

46
void Mgxs::metadata_from_hdf5(hid_t xs_id, const vector<double>& temperature,
3,207✔
47
  vector<int>& temps_to_read, int& order_dim)
48
{
49
  // get name
50
  std::string in_name;
3,207✔
51
  get_name(xs_id, in_name);
3,207✔
52
  // remove the leading '/'
53
  in_name = in_name.substr(1);
3,207✔
54

55
  // Get the AWR
56
  double in_awr;
3,207✔
57
  if (attribute_exists(xs_id, "atomic_weight_ratio")) {
3,207✔
58
    read_attr_double(xs_id, "atomic_weight_ratio", &in_awr);
104✔
59
  } else {
60
    in_awr = MACROSCOPIC_AWR;
3,103✔
61
  }
62

63
  // Determine the available temperatures
64
  hid_t kT_group = open_group(xs_id, "kTs");
3,207✔
65
  size_t num_temps = get_num_datasets(kT_group);
3,207✔
66
  char** dset_names = new char*[num_temps];
3,207!
67
  for (int i = 0; i < num_temps; i++) {
7,350✔
68
    dset_names[i] = new char[151];
4,143✔
69
  }
70
  get_datasets(kT_group, dset_names);
3,207✔
71
  vector<size_t> shape = {num_temps};
3,207✔
72
  tensor::Tensor<double> temps_available(shape);
3,207✔
73
  for (int i = 0; i < num_temps; i++) {
7,350✔
74
    read_double(kT_group, dset_names[i], &temps_available[i], true);
4,143✔
75

76
    // convert eV to Kelvin
77
    temps_available[i] = std::round(temps_available[i] / K_BOLTZMANN);
4,143!
78

79
    // Done with dset_names, so delete it
80
    delete[] dset_names[i];
4,143!
81
  }
82
  delete[] dset_names;
3,207✔
83
  std::sort(temps_available.begin(), temps_available.end());
3,207✔
84

85
  // Set the global upper and lower interpolation bounds to avoid errors
86
  // involving C-API functions.
87
  data::temperature_min =
3,207✔
88
    std::min(data::temperature_min, temps_available.front());
3,207✔
89
  data::temperature_max =
3,207✔
90
    std::max(data::temperature_max, temps_available.back());
3,207✔
91

92
  // If only one temperature is available, lets just use nearest temperature
93
  // interpolation
94
  if ((num_temps == 1) &&
3,207✔
95
      (settings::temperature_method == TemperatureMethod::INTERPOLATION)) {
2,661!
96
    warning("Cross sections for " + strtrim(name) + " are only available " +
×
97
            "at one temperature.  Reverting to the nearest temperature " +
×
98
            "method.");
99
    settings::temperature_method = TemperatureMethod::NEAREST;
×
100
  }
101

102
  switch (settings::temperature_method) {
3,207!
103
  case TemperatureMethod::NEAREST:
3,051✔
104
    // Determine actual temperatures to read
105
    for (const auto& T : temperature) {
6,076✔
106
      // Determine the closest temperature value
107
      auto i_closest = tensor::abs(temps_available - T).argmin();
6,050✔
108

109
      double temp_actual = temps_available[i_closest];
3,025!
110
      if (std::fabs(temp_actual - T) < settings::temperature_tolerance) {
3,025!
111
        if (std::find(temps_to_read.begin(), temps_to_read.end(),
3,025!
112
              std::round(temp_actual)) == temps_to_read.end()) {
3,025!
113
          temps_to_read.push_back(std::round(temp_actual));
3,025✔
114
        }
115
      } else {
116
        fatal_error(fmt::format(
×
117
          "MGXS library does not contain cross sections "
118
          "for {} at or near {} K. Available temperatures "
119
          "are {} K. Consider making use of openmc.Settings.temperature "
120
          "to specify how intermediate temperatures are treated.",
121
          in_name, std::round(T), concatenate(temps_available)));
×
122
      }
123
    }
124
    break;
125

126
  case TemperatureMethod::INTERPOLATION:
127
    for (int i = 0; i < temperature.size(); i++) {
312✔
128
      for (int j = 0; j < num_temps; j++) {
234!
129
        if (j == (num_temps - 1)) {
234!
130
          fatal_error("MGXS Library does not contain cross sections for " +
×
131
                      in_name + " at temperatures that bound " +
×
132
                      std::to_string(std::round(temperature[i])));
×
133
        }
134
        if ((temps_available[j] <= temperature[i]) &&
234!
135
            (temperature[i] < temps_available[j + 1])) {
234✔
136
          if (std::find(temps_to_read.begin(), temps_to_read.end(),
156!
137
                temps_available[j]) == temps_to_read.end()) {
156!
138
            temps_to_read.push_back(temps_available[j]);
156✔
139
          }
140

141
          if (std::find(temps_to_read.begin(), temps_to_read.end(),
156!
142
                temps_available[j + 1]) == temps_to_read.end()) {
156!
143
            temps_to_read.push_back(temps_available[j + 1]);
156✔
144
          }
145
          break;
146
        }
147
      }
148
    }
149
  }
150
  std::sort(temps_to_read.begin(), temps_to_read.end());
3,207✔
151

152
  // Get the library's temperatures
153
  int n_temperature = temps_to_read.size();
3,207✔
154
  vector<double> in_kTs(n_temperature);
6,414✔
155
  for (int i = 0; i < n_temperature; i++) {
6,544✔
156
    std::string temp_str(std::to_string(temps_to_read[i]) + "K");
3,337✔
157

158
    // read exact temperature value
159
    read_double(kT_group, temp_str.c_str(), &in_kTs[i], true);
3,337✔
160
  }
3,337✔
161
  close_group(kT_group);
3,207✔
162

163
  // Load the remaining metadata
164
  AngleDistributionType in_scatter_format;
3,207✔
165
  if (attribute_exists(xs_id, "scatter_format")) {
3,207!
166
    std::string temp_str;
3,207✔
167
    read_attribute(xs_id, "scatter_format", temp_str);
3,207✔
168
    to_lower(strtrim(temp_str));
3,207✔
169
    if (temp_str.compare(0, 8, "legendre") == 0) {
3,207✔
170
      in_scatter_format = AngleDistributionType::LEGENDRE;
171
    } else if (temp_str.compare(0, 9, "histogram") == 0) {
104✔
172
      in_scatter_format = AngleDistributionType::HISTOGRAM;
173
    } else if (temp_str.compare(0, 7, "tabular") == 0) {
52!
174
      in_scatter_format = AngleDistributionType::TABULAR;
175
    } else {
176
      fatal_error("Invalid scatter_format option!");
×
177
    }
178
  } else {
3,207✔
179
    in_scatter_format = AngleDistributionType::LEGENDRE;
180
  }
181

182
  if (attribute_exists(xs_id, "scatter_shape")) {
3,207!
183
    std::string temp_str;
3,207✔
184
    read_attribute(xs_id, "scatter_shape", temp_str);
3,207✔
185
    to_lower(strtrim(temp_str));
3,207✔
186
    if (temp_str.compare(0, 14, "[g][g\'][order]") != 0) {
3,207!
187
      fatal_error("Invalid scatter_shape option!");
×
188
    }
189
  }
3,207✔
190

191
  bool in_fissionable = false;
3,207✔
192
  if (attribute_exists(xs_id, "fissionable")) {
3,207!
193
    int int_fiss;
3,207✔
194
    read_attr_int(xs_id, "fissionable", &int_fiss);
3,207✔
195
    in_fissionable = int_fiss;
3,207✔
196
  } else {
197
    fatal_error("Fissionable element must be set!");
×
198
  }
199

200
  // Get the library's value for the order
201
  if (attribute_exists(xs_id, "order")) {
3,207!
202
    read_attr_int(xs_id, "order", &order_dim);
3,207✔
203
  } else {
204
    fatal_error("Order must be provided!");
×
205
  }
206

207
  // Store the dimensionality of the data in order_dim.
208
  // For Legendre data, we usually refer to it as Pn where n is the order.
209
  // However Pn has n+1 sets of points (since you need to count the P0
210
  // moment). Adjust for that. Histogram and Tabular formats dont need this
211
  // adjustment.
212
  if (in_scatter_format == AngleDistributionType::LEGENDRE) {
3,207✔
213
    ++order_dim;
3,103✔
214
  }
215

216
  // Get the angular information
217
  int in_n_pol;
3,207✔
218
  int in_n_azi;
3,207✔
219
  bool in_is_isotropic = true;
3,207✔
220
  if (attribute_exists(xs_id, "representation")) {
3,207!
221
    std::string temp_str;
3,207✔
222
    read_attribute(xs_id, "representation", temp_str);
3,207✔
223
    to_lower(strtrim(temp_str));
3,207✔
224
    if (temp_str.compare(0, 5, "angle") == 0) {
3,207✔
225
      in_is_isotropic = false;
226
    } else if (temp_str.compare(0, 9, "isotropic") != 0) {
3,181!
227
      fatal_error("Invalid Data Representation!");
×
228
    }
229
  }
×
230

231
  if (!in_is_isotropic) {
3,207✔
232
    if (attribute_exists(xs_id, "num_polar")) {
26!
233
      read_attr_int(xs_id, "num_polar", &in_n_pol);
26✔
234
    } else {
235
      fatal_error("num_polar must be provided!");
×
236
    }
237
    if (attribute_exists(xs_id, "num_azimuthal")) {
26!
238
      read_attr_int(xs_id, "num_azimuthal", &in_n_azi);
26✔
239
    } else {
240
      fatal_error("num_azimuthal must be provided!");
×
241
    }
242
  } else {
243
    in_n_pol = 1;
3,181✔
244
    in_n_azi = 1;
3,181✔
245
  }
246

247
  // Set the angular bins to use equally-spaced bins
248
  vector<double> in_polar(in_n_pol);
6,414✔
249
  double dangle = PI / in_n_pol;
3,207✔
250
  for (int p = 0; p < in_n_pol; p++) {
6,440✔
251
    in_polar[p] = (p + 0.5) * dangle;
3,233✔
252
  }
253
  vector<double> in_azimuthal(in_n_azi);
6,414✔
254
  dangle = 2. * PI / in_n_azi;
3,207✔
255
  for (int a = 0; a < in_n_azi; a++) {
6,440✔
256
    in_azimuthal[a] = (a + 0.5) * dangle - PI;
3,233✔
257
  }
258

259
  // Finally use this data to initialize the MGXS Object
260
  init(in_name, in_awr, in_kTs, in_fissionable, in_scatter_format,
3,207✔
261
    in_is_isotropic, in_polar, in_azimuthal);
262
}
3,207✔
263

264
//==============================================================================
265

266
Mgxs::Mgxs(
3,207✔
267
  hid_t xs_id, const vector<double>& temperature, int num_group, int num_delay)
3,207✔
268
  : num_groups(num_group), num_delayed_groups(num_delay)
3,207✔
269
{
270
  // Call generic data gathering routine (will populate the metadata)
271
  int order_data;
3,207✔
272
  vector<int> temps_to_read;
3,207✔
273
  metadata_from_hdf5(xs_id, temperature, temps_to_read, order_data);
3,207✔
274

275
  // Set number of energy and delayed groups
276
  AngleDistributionType final_scatter_format = scatter_format;
3,207✔
277
  if (settings::legendre_to_tabular) {
3,207✔
278
    if (scatter_format == AngleDistributionType::LEGENDRE)
2,999✔
279
      final_scatter_format = AngleDistributionType::TABULAR;
2,921✔
280
  }
281

282
  // Load the more specific XsData information
283
  for (int t = 0; t < temps_to_read.size(); t++) {
6,544✔
284
    xs[t] = XsData(fissionable, final_scatter_format, n_pol, n_azi, num_groups,
3,337✔
285
      num_delayed_groups);
3,337✔
286
    // Get the temperature as a string and then open the HDF5 group
287
    std::string temp_str = std::to_string(temps_to_read[t]) + "K";
3,337✔
288
    hid_t xsdata_grp = open_group(xs_id, temp_str.c_str());
3,337✔
289

290
    xs[t].from_hdf5(xsdata_grp, fissionable, scatter_format,
3,337✔
291
      final_scatter_format, order_data, is_isotropic, n_pol, n_azi);
3,337✔
292
    close_group(xsdata_grp);
3,337✔
293

294
  } // end temperature loop
3,337✔
295

296
  // Make sure the scattering format is updated to the final case
297
  scatter_format = final_scatter_format;
3,207✔
298
}
3,207✔
299

300
//==============================================================================
301

302
Mgxs::Mgxs(const std::string& in_name, const vector<double>& mat_kTs,
2,882✔
303
  const vector<Mgxs*>& micros, const vector<double>& atom_densities,
304
  int num_group, int num_delay)
2,882✔
305
  : num_groups(num_group), num_delayed_groups(num_delay)
2,882✔
306
{
307
  // Get the minimum data needed to initialize:
308
  // Dont need awr, but lets just initialize it anyways
309
  double in_awr = -1.;
2,882✔
310
  // start with the assumption it is not fissionable and set
311
  // the fissionable status if we learn differently
312
  bool in_fissionable = false;
2,882✔
313
  for (int m = 0; m < micros.size(); m++) {
6,076✔
314
    if (micros[m]->fissionable)
3,194✔
315
      in_fissionable = true;
1,128✔
316
  }
317
  // Force all of the following data to be the same; these will be verified
318
  // to be true later
319
  AngleDistributionType in_scatter_format = micros[0]->scatter_format;
2,882✔
320
  bool in_is_isotropic = micros[0]->is_isotropic;
2,882✔
321
  vector<double> in_polar = micros[0]->polar;
2,882✔
322
  vector<double> in_azimuthal = micros[0]->azimuthal;
2,882✔
323

324
  init(in_name, in_awr, mat_kTs, in_fissionable, in_scatter_format,
2,882✔
325
    in_is_isotropic, in_polar, in_azimuthal);
326

327
  // Create the xs data for each temperature
328
  for (int t = 0; t < mat_kTs.size(); t++) {
5,790✔
329
    xs[t] = XsData(in_fissionable, in_scatter_format, in_polar.size(),
2,908✔
330
      in_azimuthal.size(), num_groups, num_delayed_groups);
2,908✔
331

332
    // Find the right temperature index to use
333
    double temp_desired = mat_kTs[t];
2,908✔
334

335
    // Create the list of temperature indices and interpolation factors for
336
    // each microscopic data at the material temperature
337
    vector<int> micro_t(micros.size(), 0);
2,908✔
338
    vector<double> micro_t_interp(micros.size(), 0.);
2,908✔
339
    for (int m = 0; m < micros.size(); m++) {
6,128✔
340
      switch (settings::temperature_method) {
3,220!
341
      case TemperatureMethod::NEAREST: {
3,064✔
342
        micro_t[m] = tensor::abs(micros[m]->kTs - temp_desired).argmin();
6,128✔
343
        auto temp_actual = micros[m]->kTs[micro_t[m]];
3,064!
344

345
        if (std::abs(temp_actual - temp_desired) >=
3,064!
346
            K_BOLTZMANN * settings::temperature_tolerance) {
3,064!
347
          fatal_error(fmt::format("MGXS Library does not contain cross section "
×
348
                                  "for {} at or near {} K.",
349
            name, std::round(temp_desired / K_BOLTZMANN)));
×
350
        }
351
      } break;
352
      case TemperatureMethod::INTERPOLATION:
353
        // Get a list of bounding temperatures for each actual temperature
354
        // present in the model
355
        for (int k = 0; k < micros[m]->kTs.shape(0) - 1; k++) {
624!
356
          if ((micros[m]->kTs[k] <= temp_desired) &&
156!
357
              (temp_desired < micros[m]->kTs[k + 1])) {
156!
358
            micro_t[m] = k;
156!
359
            if (k == 0) {
156!
360
              micro_t_interp[m] = (temp_desired - micros[m]->kTs[k]) /
156✔
361
                                  (micros[m]->kTs[k + 1] - micros[m]->kTs[k]);
156✔
362
            } else {
363
              micro_t_interp[m] = 1.;
×
364
            }
365
          }
366
        }
367
      } // end switch
368
    }   // end microscopic temperature loop
369

370
    // Now combine the microscopic data at each relevant temperature
371
    // We will do this by treating the multiple temperatures of a nuclide as
372
    // a different nuclide. Mathematically this just means the temperature
373
    // interpolant is included in the number density.
374
    // These interpolants are contained within interpolant.
375
    vector<double> interpolant;    // the interpolant for the Mgxs
5,816✔
376
    vector<int> temp_indices;      // the temperature index for each Mgxs
2,908✔
377
    vector<Mgxs*> mgxs_to_combine; // The Mgxs to combine
2,908✔
378
    // Now go through and build the above vectors so that we can use them to
379
    // combine the data. We will step through each microscopic data and
380
    // add in its lower and upper temperature points
381
    for (int m = 0; m < micros.size(); m++) {
6,128✔
382
      if (settings::temperature_method == TemperatureMethod::NEAREST) {
3,220✔
383
        // Nearest interpolation only has one temperature point per isotope
384
        // and so we dont need to include a temperature interpolant in
385
        // the interpolant vector
386
        interpolant.push_back(atom_densities[m]);
3,064✔
387
        temp_indices.push_back(micro_t[m]);
3,064✔
388
        mgxs_to_combine.push_back(micros[m]);
3,064✔
389
      } else {
390
        // This will be an interpolation between two points so get both these
391
        // points
392
        // Start with the low point
393
        interpolant.push_back((1. - micro_t_interp[m]) * atom_densities[m]);
156✔
394
        temp_indices.push_back(micro_t[m]);
156✔
395
        mgxs_to_combine.push_back(micros[m]);
156✔
396
        // The higher point
397
        interpolant.push_back((micro_t_interp[m]) * atom_densities[m]);
156✔
398
        temp_indices.push_back(micro_t[m] + 1);
156✔
399
        mgxs_to_combine.push_back(micros[m]);
156✔
400
      }
401
    }
402

403
    // And finally, combine the data
404
    combine(mgxs_to_combine, interpolant, temp_indices, t);
2,908✔
405
  } // end temperature (t) loop
2,908✔
406
}
2,882✔
407

408
//==============================================================================
409

410
void Mgxs::combine(const vector<Mgxs*>& micros, const vector<double>& scalars,
2,908✔
411
  const vector<int>& micro_ts, int this_t)
412
{
413
  // Build the vector of pointers to the xs objects within micros
414
  vector<XsData*> those_xs(micros.size());
2,908✔
415
  for (int i = 0; i < micros.size(); i++) {
6,284✔
416
    those_xs[i] = &(micros[i]->xs[micro_ts[i]]);
3,376✔
417
  }
418

419
  xs[this_t].combine(those_xs, scalars);
2,908✔
420
}
2,908✔
421

422
//==============================================================================
423

424
double Mgxs::get_xs(MgxsType xstype, int gin, const int* gout, const double* mu,
2,090,612,648✔
425
  const int* dg, int t, int a)
426
{
427
  XsData* xs_t = &xs[t];
2,090,612,648!
428
  double val;
2,090,612,648✔
429
  std::string mgxs_type;
2,090,612,648!
430
  switch (xstype) {
2,090,612,648!
431
  case MgxsType::TOTAL:
18,768,959✔
432
    val = xs_t->total(a, gin);
18,768,959✔
433
    mgxs_type = "total";
18,768,959✔
434
    break;
435
  case MgxsType::NU_FISSION:
9,276,839✔
436
    val = fissionable ? xs_t->nu_fission(a, gin) : 0.;
9,276,839✔
437
    mgxs_type = "nu-fission";
9,276,839✔
438
    break;
439
  case MgxsType::ABSORPTION:
5,296,100✔
440
    val = xs_t->absorption(a, gin);
5,296,100✔
441
    mgxs_type = "absorption";
5,296,100✔
442
    break;
443
  case MgxsType::FISSION:
10,161,239✔
444
    val = fissionable ? xs_t->fission(a, gin) : 0.;
10,161,239✔
445
    mgxs_type = "fission";
10,161,239✔
446
    break;
447
  case MgxsType::KAPPA_FISSION:
9,498,479✔
448
    val = fissionable ? xs_t->kappa_fission(a, gin) : 0.;
9,498,479✔
449
    mgxs_type = "kappa-fission";
9,498,479✔
450
    break;
451
  case MgxsType::NU_SCATTER:
17,383,101✔
452
  case MgxsType::SCATTER:
17,383,101✔
453
  case MgxsType::NU_SCATTER_FMU:
17,383,101✔
454
  case MgxsType::SCATTER_FMU:
17,383,101✔
455
    val = xs_t->scatter[a]->get_xs(xstype, gin, gout, mu);
17,383,101✔
456
    mgxs_type = "scatter_data";
17,383,101✔
457
    break;
458
  case MgxsType::PROMPT_NU_FISSION:
9,269,198✔
459
    val = fissionable ? xs_t->prompt_nu_fission(a, gin) : 0.;
9,269,198✔
460
    mgxs_type = "prompt-nu-fission";
9,269,198✔
461
    break;
462
  case MgxsType::DELAYED_NU_FISSION:
64,879,368✔
463
    if (fissionable) {
64,879,368✔
464
      if (dg != nullptr) {
64,851,288✔
465
        val = xs_t->delayed_nu_fission(a, *dg, gin);
55,588,928✔
466
      } else {
467
        val = 0.;
468
        for (int d = 0; d < xs_t->delayed_nu_fission.shape(1); d++) {
129,673,040!
469
          val += xs_t->delayed_nu_fission(a, d, gin);
55,574,160✔
470
        }
471
      }
472
    } else {
473
      val = 0.;
474
    }
475
    mgxs_type = "delayed-nu-fission";
64,879,368✔
476
    break;
477
  case MgxsType::CHI:
14,479✔
478
    if (fissionable) {
14,479✔
479
      if (gout != nullptr) {
4,984!
480
        val = xs_t->chi(a, gin, *gout);
4,984✔
481
      } else {
482
        // provide an outgoing group-wise sum
483
        val = 0.;
NEW
484
        for (int g = 0; g < xs_t->chi.shape()[2]; g++) {
×
NEW
485
          val += xs_t->chi(a, gin, g);
×
486
        }
487
      }
488
    } else {
489
      val = 0.;
490
    }
491
    mgxs_type = "chi";
14,479✔
492
    break;
493
  case MgxsType::CHI_PROMPT:
6,838✔
494
    if (fissionable) {
6,838✔
495
      if (gout != nullptr) {
2,340!
496
        val = xs_t->chi_prompt(a, gin, *gout);
2,340✔
497
      } else {
498
        // provide an outgoing group-wise sum
499
        val = 0.;
500
        for (int g = 0; g < xs_t->chi_prompt.shape(2); g++) {
×
501
          val += xs_t->chi_prompt(a, gin, g);
×
502
        }
503
      }
504
    } else {
505
      val = 0.;
506
    }
507
    mgxs_type = "chi-prompt";
6,838✔
508
    break;
509
  case MgxsType::CHI_DELAYED:
42,848✔
510
    if (fissionable) {
42,848✔
511
      if (gout != nullptr) {
14,768!
512
        if (dg != nullptr) {
14,768!
513
          val = xs_t->chi_delayed(a, *dg, gin, *gout);
14,768✔
514
        } else {
515
          val = xs_t->chi_delayed(a, 0, gin, *gout);
×
516
        }
517
      } else {
518
        if (dg != nullptr) {
×
519
          val = 0.;
520
          for (int g = 0; g < xs_t->delayed_nu_fission.shape(2); g++) {
×
521
            val += xs_t->delayed_nu_fission(a, *dg, gin, g);
×
522
          }
523
        } else {
524
          val = 0.;
525
          for (int g = 0; g < xs_t->delayed_nu_fission.shape(2); g++) {
×
526
            for (int d = 0; d < xs_t->delayed_nu_fission.shape(3); d++) {
×
527
              val += xs_t->delayed_nu_fission(a, d, gin, g);
×
528
            }
529
          }
530
        }
531
      }
532
    } else {
533
      val = 0.;
534
    }
535
    mgxs_type = "chi-delayed";
42,848✔
536
    break;
537
  case MgxsType::INVERSE_VELOCITY:
1,890,435,198✔
538
    val = xs_t->inverse_velocity(a, gin);
1,890,435,198✔
539
    mgxs_type = "inverse-velocity";
1,890,435,198✔
540
    break;
541
  case MgxsType::DECAY_RATE:
55,580,002✔
542
    if (dg != nullptr) {
55,580,002!
543
      val = xs_t->decay_rate(a, *dg);
55,580,002✔
544
    } else {
545
      val = xs_t->decay_rate(a, 0);
×
546
    }
547
    mgxs_type = "decay-rate";
55,580,002✔
548
    break;
549
  default:
550
    val = 0.;
551
  }
552
  // TODO: need to add more robust handling of zero-values cross sections that
553
  // produce NANs on normalization
554
  if (val != val) {
2,090,612,648✔
555
    warning(
1,404✔
556
      fmt::format("The {} cross section for this material has a nan present "
1,728✔
557
                  "(possibly due to a division by zero). Setting to zero...",
558
        mgxs_type));
559
    val = 0;
1,404✔
560
  }
561
  return val;
2,090,612,648✔
562
}
2,090,612,648✔
563

564
//==============================================================================
565

566
void Mgxs::sample_fission_energy(
100,990,850✔
567
  int gin, int& dg, int& gout, uint64_t* seed, int t, int a)
568
{
569
  XsData* xs_t = &xs[t];
100,990,850✔
570
  double nu_fission = xs_t->nu_fission(a, gin);
100,990,850✔
571

572
  // Find the probability of having a prompt neutron
573
  double prob_prompt = xs_t->prompt_nu_fission(a, gin);
100,990,850✔
574

575
  // sample random numbers
576
  double xi_pd = prn(seed) * nu_fission;
100,990,850✔
577
  double xi_gout = prn(seed);
100,990,850✔
578

579
  // Select whether the neutron is prompt or delayed
580
  if (xi_pd <= prob_prompt) {
100,990,850✔
581
    // the neutron is prompt
582

583
    // set the delayed group for the particle to be -1, indicating prompt
584
    dg = -1;
100,989,480✔
585

586
    // sample the outgoing energy group
587
    double prob_gout = 0.;
100,989,480✔
588
    for (gout = 0; gout < num_groups; ++gout) {
101,036,500!
589
      prob_gout += xs_t->chi_prompt(a, gin, gout);
101,036,500✔
590
      if (xi_gout < prob_gout)
101,036,500✔
591
        break;
592
    }
593

594
  } else {
595
    // the neutron is delayed
596

597
    // get the delayed group
598
    for (dg = 0; dg < num_delayed_groups; ++dg) {
3,450!
599
      prob_prompt += xs_t->delayed_nu_fission(a, dg, gin);
3,450✔
600
      if (xi_pd < prob_prompt)
3,450✔
601
        break;
602
    }
603

604
    // adjust dg in case of round-off error
605
    dg = std::min(dg, num_delayed_groups - 1);
1,370!
606

607
    // sample the outgoing energy group
608
    double prob_gout = 0.;
1,370✔
609
    for (gout = 0; gout < num_groups; ++gout) {
1,370!
610
      prob_gout += xs_t->chi_delayed(a, dg, gin, gout);
1,370✔
611
      if (xi_gout < prob_gout)
1,370!
612
        break;
613
    }
614
  }
615
}
100,990,850✔
616

617
//==============================================================================
618

619
void Mgxs::sample_scatter(
1,517,650,640✔
620
  int gin, int& gout, double& mu, double& wgt, uint64_t* seed, int t, int a)
621
{
622
  // Sample the data
623
  xs[t].scatter[a]->sample(gin, gout, mu, wgt, seed);
1,517,650,640✔
624
}
1,517,650,640✔
625

626
//==============================================================================
627

628
void Mgxs::calculate_xs(Particle& p)
1,876,306,740✔
629
{
630
  // If the material is different, then we need to do a full lookup
631
  if (p.material() != p.mg_xs_cache().material) {
1,876,306,740✔
632
    set_temperature_index(p);
101,161,459✔
633
    set_angle_index(p);
101,161,459✔
634
    p.mg_xs_cache().material = p.material();
101,161,459✔
635
  } else {
636
    // If material is the same, but temperature is different, need to
637
    // find the new temperature index
638
    if (p.sqrtkT() != p.mg_xs_cache().sqrtkT) {
1,775,145,281✔
639
      set_temperature_index(p);
2,024,841✔
640
    }
641
    // If the material is the same, but angle is different, need to
642
    // find the new angle index
643
    if (p.u_local() != p.mg_xs_cache().u) {
1,775,145,281✔
644
      set_angle_index(p);
1,775,145,281✔
645
    }
646
  }
647
  int temperature = p.mg_xs_cache().t;
1,876,306,740✔
648
  int angle = p.mg_xs_cache().a;
1,876,306,740✔
649
  p.macro_xs().total = xs[temperature].total(angle, p.g()) * p.density_mult();
1,876,306,740✔
650
  p.macro_xs().absorption =
1,876,306,740✔
651
    xs[temperature].absorption(angle, p.g()) * p.density_mult();
1,876,306,740✔
652
  p.macro_xs().nu_fission =
2,147,483,647✔
653
    fissionable ? xs[temperature].nu_fission(angle, p.g()) * p.density_mult()
1,876,306,740✔
654
                : 0.;
655
}
1,876,306,740✔
656

657
//==============================================================================
658

659
bool Mgxs::equiv(const Mgxs& that)
×
660
{
661
  return (
×
662
    (num_delayed_groups == that.num_delayed_groups) &&
×
663
    (num_groups == that.num_groups) && (n_pol == that.n_pol) &&
×
664
    (n_azi == that.n_azi) &&
×
665
    (std::equal(polar.begin(), polar.end(), that.polar.begin())) &&
×
666
    (std::equal(azimuthal.begin(), azimuthal.end(), that.azimuthal.begin())) &&
×
667
    (scatter_format == that.scatter_format));
×
668
}
669

670
//==============================================================================
671

672
int Mgxs::get_temperature_index(double sqrtkT) const
114,638,010✔
673
{
674
  return tensor::abs(kTs - sqrtkT * sqrtkT).argmin();
343,914,030✔
675
}
676

677
//==============================================================================
678

679
void Mgxs::set_temperature_index(Particle& p)
103,186,300✔
680
{
681
  p.mg_xs_cache().t = get_temperature_index(p.sqrtkT());
103,186,300✔
682
  p.mg_xs_cache().sqrtkT = p.sqrtkT();
103,186,300✔
683
}
103,186,300✔
684

685
//==============================================================================
686

687
int Mgxs::get_angle_index(const Direction& u) const
1,940,734,430✔
688
{
689
  if (is_isotropic) {
1,940,734,430✔
690
    return 0;
691
  } else {
692
    // convert direction to polar and azimuthal angles
693
    double my_pol = std::acos(u.z);
40,220✔
694
    double my_azi = std::atan2(u.y, u.x);
40,220✔
695

696
    // Find the location, assuming equal-bin angles
697
    double delta_angle = PI / n_pol;
40,220✔
698
    int p = std::floor(my_pol / delta_angle);
40,220✔
699
    delta_angle = 2. * PI / n_azi;
40,220✔
700
    int a = std::floor((my_azi + PI) / delta_angle);
40,220✔
701

702
    return n_azi * p + a;
40,220✔
703
  }
704
}
705

706
//==============================================================================
707

708
void Mgxs::set_angle_index(Particle& p)
1,876,306,740✔
709
{
710
  // See if we need to find the new index
711
  if (!is_isotropic) {
1,876,306,740✔
712
    p.mg_xs_cache().a = get_angle_index(p.u_local());
20,110✔
713
    p.mg_xs_cache().u = p.u_local();
20,110✔
714
  }
715
}
1,876,306,740✔
716

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