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

openmc-dev / openmc / 22210404096

20 Feb 2026 03:44AM UTC coverage: 81.804% (+0.08%) from 81.721%
22210404096

Pull #3809

github

web-flow
Merge d39f3220e into 53ce1910f
Pull Request #3809: Implement tally filter for filtering by reaction

17328 of 24423 branches covered (70.95%)

Branch coverage included in aggregate %.

125 of 149 new or added lines in 11 files covered. (83.89%)

1322 existing lines in 33 files now uncovered.

57670 of 67257 relevant lines covered (85.75%)

45506622.43 hits per line

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

78.43
/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,
5,620✔
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;
5,620✔
32
  awr = in_awr;
5,620✔
33
  kTs = tensor::Tensor<double>(in_kTs.data(), in_kTs.size());
5,620✔
34
  fissionable = in_fissionable;
5,620✔
35
  scatter_format = in_scatter_format;
5,620✔
36
  xs.resize(in_kTs.size());
5,620✔
37
  is_isotropic = in_is_isotropic;
5,620✔
38
  n_pol = in_polar.size();
5,620✔
39
  n_azi = in_azimuthal.size();
5,620✔
40
  polar = in_polar;
5,620✔
41
  azimuthal = in_azimuthal;
5,620✔
42
}
5,620✔
43

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

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

55
  // Get the AWR
56
  double in_awr;
57
  if (attribute_exists(xs_id, "atomic_weight_ratio")) {
2,999✔
58
    read_attr_double(xs_id, "atomic_weight_ratio", &in_awr);
112✔
59
  } else {
60
    in_awr = MACROSCOPIC_AWR;
2,887✔
61
  }
62

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

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

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

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

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

102
  switch (settings::temperature_method) {
2,999!
103
  case TemperatureMethod::NEAREST:
2,831✔
104
    // Determine actual temperatures to read
105
    for (const auto& T : temperature) {
5,634✔
106
      // Determine the closest temperature value
107
      auto i_closest = tensor::abs(temps_available - T).argmin();
2,803✔
108

109
      double temp_actual = temps_available[i_closest];
2,803✔
110
      if (std::fabs(temp_actual - T) < settings::temperature_tolerance) {
2,803!
111
        if (std::find(temps_to_read.begin(), temps_to_read.end(),
2,803✔
112
              std::round(temp_actual)) == temps_to_read.end()) {
5,606!
113
          temps_to_read.push_back(std::round(temp_actual));
2,803✔
114
        }
115
      } else {
UNCOV
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.",
UNCOV
121
          in_name, std::round(T), concatenate(temps_available)));
×
122
      }
123
    }
124
    break;
2,831✔
125

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

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

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

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

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

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

191
  bool in_fissionable = false;
2,999✔
192
  if (attribute_exists(xs_id, "fissionable")) {
2,999!
193
    int int_fiss;
194
    read_attr_int(xs_id, "fissionable", &int_fiss);
2,999✔
195
    in_fissionable = int_fiss;
2,999✔
196
  } else {
UNCOV
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")) {
2,999!
202
    read_attr_int(xs_id, "order", &order_dim);
2,999✔
203
  } else {
UNCOV
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) {
2,999✔
213
    ++order_dim;
2,887✔
214
  }
215

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

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

247
  // Set the angular bins to use equally-spaced bins
248
  vector<double> in_polar(in_n_pol);
5,998✔
249
  double dangle = PI / in_n_pol;
2,999✔
250
  for (int p = 0; p < in_n_pol; p++) {
6,026✔
251
    in_polar[p] = (p + 0.5) * dangle;
3,027✔
252
  }
253
  vector<double> in_azimuthal(in_n_azi);
5,998✔
254
  dangle = 2. * PI / in_n_azi;
2,999✔
255
  for (int a = 0; a < in_n_azi; a++) {
6,026✔
256
    in_azimuthal[a] = (a + 0.5) * dangle - PI;
3,027✔
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,
2,999✔
261
    in_is_isotropic, in_polar, in_azimuthal);
262
}
2,999✔
263

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

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

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

282
  // Load the more specific XsData information
283
  for (int t = 0; t < temps_to_read.size(); t++) {
6,138✔
284
    xs[t] = XsData(fissionable, final_scatter_format, n_pol, n_azi, num_groups,
6,278✔
285
      num_delayed_groups);
6,278✔
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,139✔
288
    hid_t xsdata_grp = open_group(xs_id, temp_str.c_str());
3,139✔
289

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

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

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

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

302
Mgxs::Mgxs(const std::string& in_name, const vector<double>& mat_kTs,
2,621✔
303
  const vector<Mgxs*>& micros, const vector<double>& atom_densities,
304
  int num_group, int num_delay)
2,621✔
305
  : num_groups(num_group), num_delayed_groups(num_delay)
2,621✔
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,621✔
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,621✔
313
  for (int m = 0; m < micros.size(); m++) {
5,578✔
314
    if (micros[m]->fissionable)
2,957✔
315
      in_fissionable = true;
1,046✔
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,621✔
320
  bool in_is_isotropic = micros[0]->is_isotropic;
2,621✔
321
  vector<double> in_polar = micros[0]->polar;
2,621✔
322
  vector<double> in_azimuthal = micros[0]->azimuthal;
2,621✔
323

324
  init(in_name, in_awr, mat_kTs, in_fissionable, in_scatter_format,
2,621✔
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,270✔
329
    xs[t] = XsData(in_fissionable, in_scatter_format, in_polar.size(),
5,298✔
330
      in_azimuthal.size(), num_groups, num_delayed_groups);
5,298✔
331

332
    // Find the right temperature index to use
333
    double temp_desired = mat_kTs[t];
2,649✔
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,649✔
338
    vector<double> micro_t_interp(micros.size(), 0.);
2,649✔
339
    for (int m = 0; m < micros.size(); m++) {
5,634✔
340
      switch (settings::temperature_method) {
2,985!
341
      case TemperatureMethod::NEAREST: {
2,817✔
342
        micro_t[m] = tensor::abs(micros[m]->kTs - temp_desired).argmin();
2,817✔
343
        auto temp_actual = micros[m]->kTs[micro_t[m]];
2,817✔
344

345
        if (std::abs(temp_actual - temp_desired) >=
2,817✔
346
            K_BOLTZMANN * settings::temperature_tolerance) {
2,817!
UNCOV
347
          fatal_error(fmt::format("MGXS Library does not contain cross section "
×
348
                                  "for {} at or near {} K.",
UNCOV
349
            name, std::round(temp_desired / K_BOLTZMANN)));
×
350
        }
351
      } break;
2,817✔
352
      case TemperatureMethod::INTERPOLATION:
168✔
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++) {
336✔
356
          if ((micros[m]->kTs[k] <= temp_desired) &&
336!
357
              (temp_desired < micros[m]->kTs[k + 1])) {
168!
358
            micro_t[m] = k;
168✔
359
            if (k == 0) {
168!
360
              micro_t_interp[m] = (temp_desired - micros[m]->kTs[k]) /
336✔
361
                                  (micros[m]->kTs[k + 1] - micros[m]->kTs[k]);
168✔
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,298✔
376
    vector<int> temp_indices;      // the temperature index for each Mgxs
5,298✔
377
    vector<Mgxs*> mgxs_to_combine; // The Mgxs to combine
5,298✔
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++) {
5,634✔
382
      if (settings::temperature_method == TemperatureMethod::NEAREST) {
2,985✔
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]);
2,817✔
387
        temp_indices.push_back(micro_t[m]);
2,817✔
388
        mgxs_to_combine.push_back(micros[m]);
2,817✔
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]);
168✔
394
        temp_indices.push_back(micro_t[m]);
168✔
395
        mgxs_to_combine.push_back(micros[m]);
168✔
396
        // The higher point
397
        interpolant.push_back((micro_t_interp[m]) * atom_densities[m]);
168✔
398
        temp_indices.push_back(micro_t[m] + 1);
168✔
399
        mgxs_to_combine.push_back(micros[m]);
168✔
400
      }
401
    }
402

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

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

410
void Mgxs::combine(const vector<Mgxs*>& micros, const vector<double>& scalars,
2,649✔
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,649✔
415
  for (int i = 0; i < micros.size(); i++) {
5,802✔
416
    those_xs[i] = &(micros[i]->xs[micro_ts[i]]);
3,153✔
417
  }
418

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

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

424
double Mgxs::get_xs(MgxsType xstype, int gin, const int* gout, const double* mu,
2,090,094,146✔
425
  const int* dg, int t, int a)
426
{
427
  XsData* xs_t = &xs[t];
2,090,094,146✔
428
  double val;
429
  switch (xstype) {
2,090,094,146!
430
  case MgxsType::TOTAL:
18,757,442✔
431
    val = xs_t->total(a, gin);
18,757,442✔
432
    break;
18,757,442✔
433
  case MgxsType::NU_FISSION:
9,270,580✔
434
    val = fissionable ? xs_t->nu_fission(a, gin) : 0.;
9,270,580✔
435
    break;
9,270,580✔
436
  case MgxsType::ABSORPTION:
5,296,100✔
437
    val = xs_t->absorption(a, gin);
5,296,100✔
438
    ;
439
    break;
5,296,100✔
440
  case MgxsType::FISSION:
10,154,980✔
441
    val = fissionable ? xs_t->fission(a, gin) : 0.;
10,154,980✔
442
    break;
10,154,980✔
443
  case MgxsType::KAPPA_FISSION:
9,492,220✔
444
    val = fissionable ? xs_t->kappa_fission(a, gin) : 0.;
9,492,220✔
445
    break;
9,492,220✔
446
  case MgxsType::NU_SCATTER:
17,010,274✔
447
  case MgxsType::SCATTER:
448
  case MgxsType::NU_SCATTER_FMU:
449
  case MgxsType::SCATTER_FMU:
450
    val = xs_t->scatter[a]->get_xs(xstype, gin, gout, mu);
17,010,274✔
451
    break;
17,010,274✔
452
  case MgxsType::PROMPT_NU_FISSION:
9,262,360✔
453
    val = fissionable ? xs_t->prompt_nu_fission(a, gin) : 0.;
9,262,360!
454
    break;
9,262,360✔
455
  case MgxsType::DELAYED_NU_FISSION:
64,836,520✔
456
    if (fissionable) {
64,836,520!
457
      if (dg != nullptr) {
64,836,520✔
458
        val = xs_t->delayed_nu_fission(a, *dg, gin);
55,574,160✔
459
      } else {
460
        val = 0.;
9,262,360✔
461
        for (int d = 0; d < xs_t->delayed_nu_fission.shape(1); d++) {
64,836,520✔
462
          val += xs_t->delayed_nu_fission(a, d, gin);
55,574,160✔
463
        }
464
      }
465
    } else {
UNCOV
466
      val = 0.;
×
467
    }
468
    break;
64,836,520✔
469
  case MgxsType::CHI_PROMPT:
8,220✔
470
    if (fissionable) {
8,220✔
471
      if (gout != nullptr) {
2,842!
472
        val = xs_t->chi_prompt(a, gin, *gout);
2,842✔
473
      } else {
474
        // provide an outgoing group-wise sum
UNCOV
475
        val = 0.;
×
UNCOV
476
        for (int g = 0; g < xs_t->chi_prompt.shape(2); g++) {
×
UNCOV
477
          val += xs_t->chi_prompt(a, gin, g);
×
478
        }
479
      }
480
    } else {
481
      val = 0.;
5,378✔
482
    }
483
    break;
8,220✔
UNCOV
484
  case MgxsType::CHI_DELAYED:
×
UNCOV
485
    if (fissionable) {
×
UNCOV
486
      if (gout != nullptr) {
×
UNCOV
487
        if (dg != nullptr) {
×
UNCOV
488
          val = xs_t->chi_delayed(a, *dg, gin, *gout);
×
489
        } else {
UNCOV
490
          val = xs_t->chi_delayed(a, 0, gin, *gout);
×
491
        }
492
      } else {
493
        if (dg != nullptr) {
×
UNCOV
494
          val = 0.;
×
UNCOV
495
          for (int g = 0; g < xs_t->delayed_nu_fission.shape(2); g++) {
×
UNCOV
496
            val += xs_t->delayed_nu_fission(a, *dg, gin, g);
×
497
          }
498
        } else {
UNCOV
499
          val = 0.;
×
500
          for (int g = 0; g < xs_t->delayed_nu_fission.shape(2); g++) {
×
501
            for (int d = 0; d < xs_t->delayed_nu_fission.shape(3); d++) {
×
502
              val += xs_t->delayed_nu_fission(a, d, gin, g);
×
503
            }
504
          }
505
        }
506
      }
507
    } else {
UNCOV
508
      val = 0.;
×
509
    }
510
    break;
×
511
  case MgxsType::INVERSE_VELOCITY:
1,890,428,360✔
512
    val = xs_t->inverse_velocity(a, gin);
1,890,428,360✔
513
    break;
1,890,428,360✔
514
  case MgxsType::DECAY_RATE:
55,577,090✔
515
    if (dg != nullptr) {
55,577,090!
516
      val = xs_t->decay_rate(a, *dg);
55,577,090✔
517
    } else {
518
      val = xs_t->decay_rate(a, 0);
×
519
    }
520
    break;
55,577,090✔
UNCOV
521
  default:
×
UNCOV
522
    val = 0.;
×
523
  }
524
  return val;
2,090,094,146✔
525
}
526

527
//==============================================================================
528

529
void Mgxs::sample_fission_energy(
100,990,850✔
530
  int gin, int& dg, int& gout, uint64_t* seed, int t, int a)
531
{
532
  XsData* xs_t = &xs[t];
100,990,850✔
533
  double nu_fission = xs_t->nu_fission(a, gin);
100,990,850✔
534

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

538
  // sample random numbers
539
  double xi_pd = prn(seed) * nu_fission;
100,990,850✔
540
  double xi_gout = prn(seed);
100,990,850✔
541

542
  // Select whether the neutron is prompt or delayed
543
  if (xi_pd <= prob_prompt) {
100,990,850✔
544
    // the neutron is prompt
545

546
    // set the delayed group for the particle to be -1, indicating prompt
547
    dg = -1;
100,989,480✔
548

549
    // sample the outgoing energy group
550
    double prob_gout = 0.;
100,989,480✔
551
    for (gout = 0; gout < num_groups; ++gout) {
101,036,500!
552
      prob_gout += xs_t->chi_prompt(a, gin, gout);
101,036,500✔
553
      if (xi_gout < prob_gout)
101,036,500✔
554
        break;
100,989,480✔
555
    }
556

557
  } else {
558
    // the neutron is delayed
559

560
    // get the delayed group
561
    for (dg = 0; dg < num_delayed_groups; ++dg) {
3,450!
562
      prob_prompt += xs_t->delayed_nu_fission(a, dg, gin);
3,450✔
563
      if (xi_pd < prob_prompt)
3,450✔
564
        break;
1,370✔
565
    }
566

567
    // adjust dg in case of round-off error
568
    dg = std::min(dg, num_delayed_groups - 1);
1,370✔
569

570
    // sample the outgoing energy group
571
    double prob_gout = 0.;
1,370✔
572
    for (gout = 0; gout < num_groups; ++gout) {
1,370!
573
      prob_gout += xs_t->chi_delayed(a, dg, gin, gout);
1,370✔
574
      if (xi_gout < prob_gout)
1,370!
575
        break;
1,370✔
576
    }
577
  }
578
}
100,990,850✔
579

580
//==============================================================================
581

582
void Mgxs::sample_scatter(
1,517,650,640✔
583
  int gin, int& gout, double& mu, double& wgt, uint64_t* seed, int t, int a)
584
{
585
  // Sample the data
586
  xs[t].scatter[a]->sample(gin, gout, mu, wgt, seed);
1,517,650,640✔
587
}
1,517,650,640✔
588

589
//==============================================================================
590

591
void Mgxs::calculate_xs(Particle& p)
1,876,306,740✔
592
{
593
  // If the material is different, then we need to do a full lookup
594
  if (p.material() != p.mg_xs_cache().material) {
1,876,306,740✔
595
    set_temperature_index(p);
101,161,459✔
596
    set_angle_index(p);
101,161,459✔
597
    p.mg_xs_cache().material = p.material();
101,161,459✔
598
  } else {
599
    // If material is the same, but temperature is different, need to
600
    // find the new temperature index
601
    if (p.sqrtkT() != p.mg_xs_cache().sqrtkT) {
1,775,145,281✔
602
      set_temperature_index(p);
2,024,841✔
603
    }
604
    // If the material is the same, but angle is different, need to
605
    // find the new angle index
606
    if (p.u_local() != p.mg_xs_cache().u) {
1,775,145,281!
607
      set_angle_index(p);
1,775,145,281✔
608
    }
609
  }
610
  int temperature = p.mg_xs_cache().t;
1,876,306,740✔
611
  int angle = p.mg_xs_cache().a;
1,876,306,740✔
612
  p.macro_xs().total = xs[temperature].total(angle, p.g()) * p.density_mult();
1,876,306,740✔
613
  p.macro_xs().absorption =
2,147,483,647✔
614
    xs[temperature].absorption(angle, p.g()) * p.density_mult();
1,876,306,740✔
615
  p.macro_xs().nu_fission =
1,876,306,740✔
616
    fissionable ? xs[temperature].nu_fission(angle, p.g()) * p.density_mult()
1,876,306,740✔
617
                : 0.;
618
}
1,876,306,740✔
619

620
//==============================================================================
621

UNCOV
622
bool Mgxs::equiv(const Mgxs& that)
×
623
{
624
  return (
UNCOV
625
    (num_delayed_groups == that.num_delayed_groups) &&
×
UNCOV
626
    (num_groups == that.num_groups) && (n_pol == that.n_pol) &&
×
UNCOV
627
    (n_azi == that.n_azi) &&
×
UNCOV
628
    (std::equal(polar.begin(), polar.end(), that.polar.begin())) &&
×
UNCOV
629
    (std::equal(azimuthal.begin(), azimuthal.end(), that.azimuthal.begin())) &&
×
UNCOV
630
    (scatter_format == that.scatter_format));
×
631
}
632

633
//==============================================================================
634

635
int Mgxs::get_temperature_index(double sqrtkT) const
114,575,613✔
636
{
637
  return tensor::abs(kTs - sqrtkT * sqrtkT).argmin();
114,575,613✔
638
}
639

640
//==============================================================================
641

642
void Mgxs::set_temperature_index(Particle& p)
103,186,300✔
643
{
644
  p.mg_xs_cache().t = get_temperature_index(p.sqrtkT());
103,186,300✔
645
  p.mg_xs_cache().sqrtkT = p.sqrtkT();
103,186,300✔
646
}
103,186,300✔
647

648
//==============================================================================
649

650
int Mgxs::get_angle_index(const Direction& u) const
1,940,734,430✔
651
{
652
  if (is_isotropic) {
1,940,734,430✔
653
    return 0;
1,940,694,210✔
654
  } else {
655
    // convert direction to polar and azimuthal angles
656
    double my_pol = std::acos(u.z);
40,220✔
657
    double my_azi = std::atan2(u.y, u.x);
40,220✔
658

659
    // Find the location, assuming equal-bin angles
660
    double delta_angle = PI / n_pol;
40,220✔
661
    int p = std::floor(my_pol / delta_angle);
40,220✔
662
    delta_angle = 2. * PI / n_azi;
40,220✔
663
    int a = std::floor((my_azi + PI) / delta_angle);
40,220✔
664

665
    return n_azi * p + a;
40,220✔
666
  }
667
}
668

669
//==============================================================================
670

671
void Mgxs::set_angle_index(Particle& p)
1,876,306,740✔
672
{
673
  // See if we need to find the new index
674
  if (!is_isotropic) {
1,876,306,740✔
675
    p.mg_xs_cache().a = get_angle_index(p.u_local());
20,110✔
676
    p.mg_xs_cache().u = p.u_local();
20,110✔
677
  }
678
}
1,876,306,740✔
679

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