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

openmc-dev / openmc / 22500302709

27 Feb 2026 07:16PM UTC coverage: 81.512% (-0.3%) from 81.826%
22500302709

Pull #3830

github

web-flow
Merge 25fbb4266 into b3788f11e
Pull Request #3830: Parallelize sampling external sources and threadsafe rejection counters

17488 of 25193 branches covered (69.42%)

Branch coverage included in aggregate %.

59 of 66 new or added lines in 6 files covered. (89.39%)

841 existing lines in 44 files now uncovered.

57726 of 67081 relevant lines covered (86.05%)

44920080.48 hits per line

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

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

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

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

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

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

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

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

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

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

109
      double temp_actual = temps_available[i_closest];
3,006!
110
      if (std::fabs(temp_actual - T) < settings::temperature_tolerance) {
3,006!
111
        if (std::find(temps_to_read.begin(), temps_to_read.end(),
3,006!
112
              std::round(temp_actual)) == temps_to_read.end()) {
3,006!
113
          temps_to_read.push_back(std::round(temp_actual));
3,006✔
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,216✔
151

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

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

163
  // Load the remaining metadata
164
  AngleDistributionType in_scatter_format;
3,216✔
165
  if (attribute_exists(xs_id, "scatter_format")) {
3,216!
166
    std::string temp_str;
3,216✔
167
    read_attribute(xs_id, "scatter_format", temp_str);
3,216✔
168
    to_lower(strtrim(temp_str));
3,216✔
169
    if (temp_str.compare(0, 8, "legendre") == 0) {
3,216✔
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,216✔
179
    in_scatter_format = AngleDistributionType::LEGENDRE;
180
  }
181

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

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

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

231
  if (!in_is_isotropic) {
3,216✔
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,186✔
244
    in_n_azi = 1;
3,186✔
245
  }
246

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

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

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

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

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

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

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

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

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

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

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

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

345
        if (std::abs(temp_actual - temp_desired) >=
3,021!
346
            K_BOLTZMANN * settings::temperature_tolerance) {
3,021!
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,682✔
376
    vector<int> temp_indices;      // the temperature index for each Mgxs
2,841✔
377
    vector<Mgxs*> mgxs_to_combine; // The Mgxs to combine
2,841✔
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,042✔
382
      if (settings::temperature_method == TemperatureMethod::NEAREST) {
3,201✔
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,021✔
387
        temp_indices.push_back(micro_t[m]);
3,021✔
388
        mgxs_to_combine.push_back(micros[m]);
3,021✔
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,841✔
405
  } // end temperature (t) loop
2,841✔
406
}
2,811✔
407

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

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

419
  xs[this_t].combine(those_xs, scalars);
2,841✔
420
}
2,841✔
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,196✔
512
    val = xs_t->inverse_velocity(a, gin);
2,079,471,196✔
513
    break;
2,079,471,196✔
514
  case MgxsType::DECAY_RATE:
61,134,799✔
515
    if (dg != nullptr) {
61,134,799!
516
      val = xs_t->decay_rate(a, *dg);
61,134,799✔
517
    } else {
518
      val = xs_t->decay_rate(a, 0);
×
519
    }
520
    break;
521
  default:
522
    val = 0.;
523
  }
524
  return val;
2,147,483,647✔
525
}
526

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

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

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

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

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

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

549
    // sample the outgoing energy group
550
    double prob_gout = 0.;
111,088,428✔
551
    for (gout = 0; gout < num_groups; ++gout) {
111,140,150!
552
      prob_gout += xs_t->chi_prompt(a, gin, gout);
111,140,150✔
553
      if (xi_gout < prob_gout)
111,140,150✔
554
        break;
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,795!
562
      prob_prompt += xs_t->delayed_nu_fission(a, dg, gin);
3,795✔
563
      if (xi_pd < prob_prompt)
3,795✔
564
        break;
565
    }
566

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

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

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

582
void Mgxs::sample_scatter(
1,669,415,704✔
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,669,415,704✔
587
}
1,669,415,704✔
588

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

591
void Mgxs::calculate_xs(Particle& p)
2,063,937,414✔
592
{
593
  // If the material is different, then we need to do a full lookup
594
  if (p.material() != p.mg_xs_cache().material) {
2,063,937,414✔
595
    set_temperature_index(p);
112,278,623✔
596
    set_angle_index(p);
112,278,623✔
597
    p.mg_xs_cache().material = p.material();
112,278,623✔
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,951,658,791✔
602
      set_temperature_index(p);
2,227,056✔
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,951,658,791✔
607
      set_angle_index(p);
1,951,658,791✔
608
    }
609
  }
610
  int temperature = p.mg_xs_cache().t;
2,063,937,414✔
611
  int angle = p.mg_xs_cache().a;
2,063,937,414✔
612
  p.macro_xs().total = xs[temperature].total(angle, p.g()) * p.density_mult();
2,063,937,414✔
613
  p.macro_xs().absorption =
2,063,937,414✔
614
    xs[temperature].absorption(angle, p.g()) * p.density_mult();
2,063,937,414✔
615
  p.macro_xs().nu_fission =
2,147,483,647✔
616
    fissionable ? xs[temperature].nu_fission(angle, p.g()) * p.density_mult()
2,063,937,414✔
617
                : 0.;
618
}
2,063,937,414✔
619

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

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

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

635
int Mgxs::get_temperature_index(double sqrtkT) const
127,033,921✔
636
{
637
  return tensor::abs(kTs - sqrtkT * sqrtkT).argmin();
381,101,763✔
638
}
639

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

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

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

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

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

665
    return n_azi * p + a;
44,242✔
666
  }
667
}
668

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

671
void Mgxs::set_angle_index(Particle& p)
2,063,937,414✔
672
{
673
  // See if we need to find the new index
674
  if (!is_isotropic) {
2,063,937,414✔
675
    p.mg_xs_cache().a = get_angle_index(p.u_local());
22,121✔
676
    p.mg_xs_cache().u = p.u_local();
22,121✔
677
  }
678
}
2,063,937,414✔
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