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

openmc-dev / openmc / 23923926391

02 Apr 2026 09:58PM UTC coverage: 81.34% (+0.02%) from 81.324%
23923926391

Pull #3903

github

web-flow
Merge 733cdbfaf into d9b30bbbd
Pull Request #3903: Add from_bounding_box classmethod to structured mesh classes

17624 of 25463 branches covered (69.21%)

Branch coverage included in aggregate %.

33 of 40 new or added lines in 1 file covered. (82.5%)

69 existing lines in 3 files now uncovered.

58121 of 67659 relevant lines covered (85.9%)

44573311.74 hits per line

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

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

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

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

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

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

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

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

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

92
  // If only one temperature is available, lets just use nearest temperature
93
  // interpolation
94
  if ((num_temps == 1) &&
3,227✔
95
      (settings::temperature_method == TemperatureMethod::INTERPOLATION)) {
2,597!
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,227!
103
  case TemperatureMethod::NEAREST:
3,047✔
104
    // Determine actual temperatures to read
105
    for (const auto& T : temperature) {
6,064✔
106
      // Determine the closest temperature value
107
      auto i_closest = tensor::abs(temps_available - T).argmin();
6,034✔
108

109
      double temp_actual = temps_available[i_closest];
3,017!
110
      if (std::fabs(temp_actual - T) < settings::temperature_tolerance) {
3,017!
111
        if (std::find(temps_to_read.begin(), temps_to_read.end(),
3,017!
112
              std::round(temp_actual)) == temps_to_read.end()) {
3,017!
113
          temps_to_read.push_back(std::round(temp_actual));
3,017✔
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++) {
360✔
128
      for (int j = 0; j < num_temps; j++) {
270!
129
        if (j == (num_temps - 1)) {
270!
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]) &&
270!
135
            (temperature[i] < temps_available[j + 1])) {
270✔
136
          if (std::find(temps_to_read.begin(), temps_to_read.end(),
180!
137
                temps_available[j]) == temps_to_read.end()) {
180!
138
            temps_to_read.push_back(temps_available[j]);
180✔
139
          }
140

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

345
        if (std::abs(temp_actual - temp_desired) >=
3,032!
346
            K_BOLTZMANN * settings::temperature_tolerance) {
3,032!
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++) {
720!
356
          if ((micros[m]->kTs[k] <= temp_desired) &&
180!
357
              (temp_desired < micros[m]->kTs[k + 1])) {
180!
358
            micro_t[m] = k;
180!
359
            if (k == 0) {
180!
360
              micro_t_interp[m] = (temp_desired - micros[m]->kTs[k]) /
180✔
361
                                  (micros[m]->kTs[k + 1] - micros[m]->kTs[k]);
180✔
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,704✔
376
    vector<int> temp_indices;      // the temperature index for each Mgxs
2,852✔
377
    vector<Mgxs*> mgxs_to_combine; // The Mgxs to combine
2,852✔
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,064✔
382
      if (settings::temperature_method == TemperatureMethod::NEAREST) {
3,212✔
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,032✔
387
        temp_indices.push_back(micro_t[m]);
3,032✔
388
        mgxs_to_combine.push_back(micros[m]);
3,032✔
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]);
180✔
394
        temp_indices.push_back(micro_t[m]);
180✔
395
        mgxs_to_combine.push_back(micros[m]);
180✔
396
        // The higher point
397
        interpolant.push_back((micro_t_interp[m]) * atom_densities[m]);
180✔
398
        temp_indices.push_back(micro_t[m] + 1);
180✔
399
        mgxs_to_combine.push_back(micros[m]);
180✔
400
      }
401
    }
402

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

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

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

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

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

424
double Mgxs::get_xs(MgxsType xstype, int gin, const int* gout, const double* mu,
2,147,483,647✔
425
  const int* dg, int t, int a)
426
{
427
  XsData* xs_t = &xs[t];
2,147,483,647!
428
  double val;
2,147,483,647✔
429
  switch (xstype) {
2,147,483,647!
430
  case MgxsType::TOTAL:
20,632,955✔
431
    val = xs_t->total(a, gin);
20,632,955✔
432
    break;
20,632,955✔
433
  case MgxsType::NU_FISSION:
10,197,407✔
434
    val = fissionable ? xs_t->nu_fission(a, gin) : 0.;
10,197,407✔
435
    break;
436
  case MgxsType::ABSORPTION:
5,825,710✔
437
    val = xs_t->absorption(a, gin);
5,825,710✔
438
    ;
5,825,710✔
439
    break;
5,825,710✔
440
  case MgxsType::FISSION:
11,170,247✔
441
    val = fissionable ? xs_t->fission(a, gin) : 0.;
11,170,247✔
442
    break;
443
  case MgxsType::KAPPA_FISSION:
10,441,211✔
444
    val = fissionable ? xs_t->kappa_fission(a, gin) : 0.;
10,441,211✔
445
    break;
446
  case MgxsType::NU_SCATTER:
18,704,631✔
447
  case MgxsType::SCATTER:
18,704,631✔
448
  case MgxsType::NU_SCATTER_FMU:
18,704,631✔
449
  case MgxsType::SCATTER_FMU:
18,704,631✔
450
    val = xs_t->scatter[a]->get_xs(xstype, gin, gout, mu);
18,704,631✔
451
    break;
18,704,631✔
452
  case MgxsType::PROMPT_NU_FISSION:
10,188,596✔
453
    val = fissionable ? xs_t->prompt_nu_fission(a, gin) : 0.;
10,188,596!
454
    break;
455
  case MgxsType::DELAYED_NU_FISSION:
71,320,172✔
456
    if (fissionable) {
71,320,172!
457
      if (dg != nullptr) {
71,320,172✔
458
        val = xs_t->delayed_nu_fission(a, *dg, gin);
61,131,576✔
459
      } else {
460
        val = 0.;
461
        for (int d = 0; d < xs_t->delayed_nu_fission.shape(1); d++) {
142,640,344!
462
          val += xs_t->delayed_nu_fission(a, d, gin);
61,131,576✔
463
        }
464
      }
465
    } else {
466
      val = 0.;
467
    }
468
    break;
469
  case MgxsType::CHI_PROMPT:
8,811✔
470
    if (fissionable) {
8,811✔
471
      if (gout != nullptr) {
3,047!
472
        val = xs_t->chi_prompt(a, gin, *gout);
3,047✔
473
      } else {
474
        // provide an outgoing group-wise sum
475
        val = 0.;
476
        for (int g = 0; g < xs_t->chi_prompt.shape(2); g++) {
×
477
          val += xs_t->chi_prompt(a, gin, g);
×
478
        }
479
      }
480
    } else {
481
      val = 0.;
482
    }
483
    break;
484
  case MgxsType::CHI_DELAYED:
×
485
    if (fissionable) {
×
486
      if (gout != nullptr) {
×
487
        if (dg != nullptr) {
×
488
          val = xs_t->chi_delayed(a, *dg, gin, *gout);
×
489
        } else {
490
          val = xs_t->chi_delayed(a, 0, gin, *gout);
×
491
        }
492
      } else {
493
        if (dg != nullptr) {
×
494
          val = 0.;
495
          for (int g = 0; g < xs_t->delayed_nu_fission.shape(2); g++) {
×
496
            val += xs_t->delayed_nu_fission(a, *dg, gin, g);
×
497
          }
498
        } else {
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 {
508
      val = 0.;
509
    }
510
    break;
511
  case MgxsType::INVERSE_VELOCITY:
2,079,471,856✔
512
    val = xs_t->inverse_velocity(a, gin);
2,079,471,856✔
513
    if (!(val > 0))
2,079,471,856✔
514
      val = data::mg.default_inverse_velocity_[gin];
2,061,510,000✔
515
    break;
516
  case MgxsType::DECAY_RATE:
61,134,799✔
517
    if (dg != nullptr) {
61,134,799!
518
      val = xs_t->decay_rate(a, *dg);
61,134,799✔
519
    } else {
UNCOV
520
      val = xs_t->decay_rate(a, 0);
×
521
    }
522
    break;
523
  default:
524
    val = 0.;
525
  }
526
  return val;
2,147,483,647✔
527
}
528

529
//==============================================================================
530

531
void Mgxs::sample_fission_energy(
111,089,935✔
532
  int gin, int& dg, int& gout, uint64_t* seed, int t, int a)
533
{
534
  XsData* xs_t = &xs[t];
111,089,935✔
535
  double nu_fission = xs_t->nu_fission(a, gin);
111,089,935✔
536

537
  // Find the probability of having a prompt neutron
538
  double prob_prompt = xs_t->prompt_nu_fission(a, gin);
111,089,935✔
539

540
  // sample random numbers
541
  double xi_pd = prn(seed) * nu_fission;
111,089,935✔
542
  double xi_gout = prn(seed);
111,089,935✔
543

544
  // Select whether the neutron is prompt or delayed
545
  if (xi_pd <= prob_prompt) {
111,089,935✔
546
    // the neutron is prompt
547

548
    // set the delayed group for the particle to be -1, indicating prompt
549
    dg = -1;
111,088,428✔
550

551
    // sample the outgoing energy group
552
    double prob_gout = 0.;
111,088,428✔
553
    for (gout = 0; gout < num_groups; ++gout) {
111,140,150!
554
      prob_gout += xs_t->chi_prompt(a, gin, gout);
111,140,150✔
555
      if (xi_gout < prob_gout)
111,140,150✔
556
        break;
557
    }
558

559
  } else {
560
    // the neutron is delayed
561

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

569
    // adjust dg in case of round-off error
570
    dg = std::min(dg, num_delayed_groups - 1);
1,507!
571

572
    // sample the outgoing energy group
573
    double prob_gout = 0.;
1,507✔
574
    for (gout = 0; gout < num_groups; ++gout) {
1,507!
575
      prob_gout += xs_t->chi_delayed(a, dg, gin, gout);
1,507✔
576
      if (xi_gout < prob_gout)
1,507!
577
        break;
578
    }
579
  }
580
}
111,089,935✔
581

582
//==============================================================================
583

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

591
//==============================================================================
592

593
void Mgxs::calculate_xs(Particle& p)
2,063,937,744✔
594
{
595
  // If the material is different, then we need to do a full lookup
596
  if (p.material() != p.mg_xs_cache().material) {
2,063,937,744✔
597
    set_temperature_index(p);
112,278,953✔
598
    set_angle_index(p);
112,278,953✔
599
    p.mg_xs_cache().material = p.material();
112,278,953✔
600
  } else {
601
    // If material is the same, but temperature is different, need to
602
    // find the new temperature index
603
    if (p.sqrtkT() != p.mg_xs_cache().sqrtkT) {
1,951,658,791✔
604
      set_temperature_index(p);
2,227,056✔
605
    }
606
    // If the material is the same, but angle is different, need to
607
    // find the new angle index
608
    if (p.u_local() != p.mg_xs_cache().u) {
1,951,658,791✔
609
      set_angle_index(p);
1,951,658,791✔
610
    }
611
  }
612
  int temperature = p.mg_xs_cache().t;
2,063,937,744✔
613
  int angle = p.mg_xs_cache().a;
2,063,937,744✔
614
  p.macro_xs().total = xs[temperature].total(angle, p.g()) * p.density_mult();
2,063,937,744✔
615
  p.macro_xs().absorption =
2,063,937,744✔
616
    xs[temperature].absorption(angle, p.g()) * p.density_mult();
2,063,937,744✔
617
  p.macro_xs().nu_fission =
2,147,483,647✔
618
    fissionable ? xs[temperature].nu_fission(angle, p.g()) * p.density_mult()
2,063,937,744✔
619
                : 0.;
620
}
2,063,937,744✔
621

622
//==============================================================================
623

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

635
//==============================================================================
636

637
int Mgxs::get_temperature_index(double sqrtkT) const
127,034,251✔
638
{
639
  return tensor::abs(kTs - sqrtkT * sqrtkT).argmin();
381,102,753✔
640
}
641

642
//==============================================================================
643

644
void Mgxs::set_temperature_index(Particle& p)
114,506,009✔
645
{
646
  p.mg_xs_cache().t = get_temperature_index(p.sqrtkT());
114,506,009✔
647
  p.mg_xs_cache().sqrtkT = p.sqrtkT();
114,506,009✔
648
}
114,506,009✔
649

650
//==============================================================================
651

652
int Mgxs::get_angle_index(const Direction& u) const
2,134,808,533✔
653
{
654
  if (is_isotropic) {
2,134,808,533✔
655
    return 0;
656
  } else {
657
    // convert direction to polar and azimuthal angles
658
    double my_pol = std::acos(u.z);
44,242✔
659
    double my_azi = std::atan2(u.y, u.x);
44,242✔
660

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

667
    return n_azi * p + a;
44,242✔
668
  }
669
}
670

671
//==============================================================================
672

673
void Mgxs::set_angle_index(Particle& p)
2,063,937,744✔
674
{
675
  // See if we need to find the new index
676
  if (!is_isotropic) {
2,063,937,744✔
677
    p.mg_xs_cache().a = get_angle_index(p.u_local());
22,121✔
678
    p.mg_xs_cache().u = p.u_local();
22,121✔
679
  }
680
}
2,063,937,744✔
681

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