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

openmc-dev / openmc / 14423552163

12 Apr 2025 09:31PM UTC coverage: 85.361% (+0.3%) from 85.044%
14423552163

Pull #3363

github

web-flow
Merge 383920d8a into c1a4d43da
Pull Request #3363: Figure of Merit implementation

27 of 30 new or added lines in 2 files covered. (90.0%)

101 existing lines in 8 files now uncovered.

52102 of 61037 relevant lines covered (85.36%)

37419346.52 hits per line

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

86.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 "xtensor/xadapt.hpp"
9
#include "xtensor/xmath.hpp"
10
#include "xtensor/xsort.hpp"
11
#include "xtensor/xview.hpp"
12
#include <fmt/core.h>
13

14
#include "openmc/error.h"
15
#include "openmc/math_functions.h"
16
#include "openmc/mgxs_interface.h"
17
#include "openmc/random_lcg.h"
18
#include "openmc/settings.h"
19
#include "openmc/string_utils.h"
20

21
namespace openmc {
22

23
//==============================================================================
24
// Mgxs base-class methods
25
//==============================================================================
26

27
void Mgxs::init(const std::string& in_name, double in_awr,
5,012✔
28
  const vector<double>& in_kTs, bool in_fissionable,
29
  AngleDistributionType in_scatter_format, bool in_is_isotropic,
30
  const vector<double>& in_polar, const vector<double>& in_azimuthal)
31
{
32
  // Set the metadata
33
  name = in_name;
5,012✔
34
  awr = in_awr;
5,012✔
35
  // TODO: Remove adapt when in_KTs is an xtensor
36
  kTs = xt::adapt(in_kTs);
5,012✔
37
  fissionable = in_fissionable;
5,012✔
38
  scatter_format = in_scatter_format;
5,012✔
39
  xs.resize(in_kTs.size());
5,012✔
40
  is_isotropic = in_is_isotropic;
5,012✔
41
  n_pol = in_polar.size();
5,012✔
42
  n_azi = in_azimuthal.size();
5,012✔
43
  polar = in_polar;
5,012✔
44
  azimuthal = in_azimuthal;
5,012✔
45
}
5,012✔
46

47
//==============================================================================
48

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

58
  // Get the AWR
59
  double in_awr;
60
  if (attribute_exists(xs_id, "atomic_weight_ratio")) {
2,722✔
61
    read_attr_double(xs_id, "atomic_weight_ratio", &in_awr);
128✔
62
  } else {
63
    in_awr = MACROSCOPIC_AWR;
2,594✔
64
  }
65

66
  // Determine the available temperatures
67
  hid_t kT_group = open_group(xs_id, "kTs");
2,722✔
68
  size_t num_temps = get_num_datasets(kT_group);
2,722✔
69
  char** dset_names = new char*[num_temps];
2,722✔
70
  for (int i = 0; i < num_temps; i++) {
6,420✔
71
    dset_names[i] = new char[151];
3,698✔
72
  }
73
  get_datasets(kT_group, dset_names);
2,722✔
74
  vector<size_t> shape = {num_temps};
2,722✔
75
  xt::xarray<double> temps_available(shape);
2,722✔
76
  for (int i = 0; i < num_temps; i++) {
6,420✔
77
    read_double(kT_group, dset_names[i], &temps_available[i], true);
3,698✔
78

79
    // convert eV to Kelvin
80
    temps_available[i] = std::round(temps_available[i] / K_BOLTZMANN);
3,698✔
81

82
    // Done with dset_names, so delete it
83
    delete[] dset_names[i];
3,698✔
84
  }
85
  delete[] dset_names;
2,722✔
86
  std::sort(temps_available.begin(), temps_available.end());
2,722✔
87

88
  // If only one temperature is available, lets just use nearest temperature
89
  // interpolation
90
  if ((num_temps == 1) &&
2,722✔
91
      (settings::temperature_method == TemperatureMethod::INTERPOLATION)) {
2,226✔
92
    warning("Cross sections for " + strtrim(name) + " are only available " +
×
93
            "at one temperature.  Reverting to the nearest temperature " +
×
94
            "method.");
95
    settings::temperature_method = TemperatureMethod::NEAREST;
×
96
  }
97

98
  switch (settings::temperature_method) {
2,722✔
99
  case TemperatureMethod::NEAREST:
2,530✔
100
    // Determine actual temperatures to read
101
    for (const auto& T : temperature) {
5,012✔
102
      // Determine the closest temperature value
103
      // NOTE: the below block could be replaced with the following line,
104
      // though this gives a runtime error if using LLVM 20 or newer,
105
      // likely due to a bug in xtensor.
106
      // auto i_closest = xt::argmin(xt::abs(temps_available - T))[0];
107
      double closest = std::numeric_limits<double>::max();
2,482✔
108
      int i_closest = 0;
2,482✔
109
      for (int i = 0; i < temps_available.size(); i++) {
5,572✔
110
        double diff = std::abs(temps_available[i] - T);
3,090✔
111
        if (diff < closest) {
3,090✔
112
          closest = diff;
2,786✔
113
          i_closest = i;
2,786✔
114
        }
115
      }
116

117
      double temp_actual = temps_available[i_closest];
2,482✔
118
      if (std::fabs(temp_actual - T) < settings::temperature_tolerance) {
2,482✔
119
        if (std::find(temps_to_read.begin(), temps_to_read.end(),
2,482✔
120
              std::round(temp_actual)) == temps_to_read.end()) {
4,964✔
121
          temps_to_read.push_back(std::round(temp_actual));
2,482✔
122
        }
123
      } else {
124
        fatal_error(fmt::format(
×
125
          "MGXS library does not contain cross sections "
126
          "for {} at or near {} K. Available temperatures "
127
          "are {} K. Consider making use of openmc.Settings.temperature "
128
          "to specify how intermediate temperatures are treated.",
UNCOV
129
          in_name, std::round(T), concatenate(temps_available)));
×
130
      }
131
    }
132
    break;
2,530✔
133

134
  case TemperatureMethod::INTERPOLATION:
192✔
135
    for (int i = 0; i < temperature.size(); i++) {
384✔
136
      for (int j = 0; j < num_temps; j++) {
288✔
137
        if (j == (num_temps - 1)) {
288✔
UNCOV
138
          fatal_error("MGXS Library does not contain cross sections for " +
×
UNCOV
139
                      in_name + " at temperatures that bound " +
×
UNCOV
140
                      std::to_string(std::round(temperature[i])));
×
141
        }
142
        if ((temps_available[j] <= temperature[i]) &&
576✔
143
            (temperature[i] < temps_available[j + 1])) {
288✔
144
          if (std::find(temps_to_read.begin(), temps_to_read.end(),
192✔
145
                temps_available[j]) == temps_to_read.end()) {
384✔
146
            temps_to_read.push_back(temps_available[j]);
192✔
147
          }
148

149
          if (std::find(temps_to_read.begin(), temps_to_read.end(),
192✔
150
                temps_available[j + 1]) == temps_to_read.end()) {
384✔
151
            temps_to_read.push_back(temps_available[j + 1]);
192✔
152
          }
153
          break;
192✔
154
        }
155
      }
156
    }
157
  }
158
  std::sort(temps_to_read.begin(), temps_to_read.end());
2,722✔
159

160
  // Get the library's temperatures
161
  int n_temperature = temps_to_read.size();
2,722✔
162
  vector<double> in_kTs(n_temperature);
5,444✔
163
  for (int i = 0; i < n_temperature; i++) {
5,588✔
164
    std::string temp_str(std::to_string(temps_to_read[i]) + "K");
2,866✔
165

166
    // read exact temperature value
167
    read_double(kT_group, temp_str.c_str(), &in_kTs[i], true);
2,866✔
168
  }
2,866✔
169
  close_group(kT_group);
2,722✔
170

171
  // Load the remaining metadata
172
  AngleDistributionType in_scatter_format;
173
  if (attribute_exists(xs_id, "scatter_format")) {
2,722✔
174
    std::string temp_str;
2,722✔
175
    read_attribute(xs_id, "scatter_format", temp_str);
2,722✔
176
    to_lower(strtrim(temp_str));
2,722✔
177
    if (temp_str.compare(0, 8, "legendre") == 0) {
2,722✔
178
      in_scatter_format = AngleDistributionType::LEGENDRE;
2,594✔
179
    } else if (temp_str.compare(0, 9, "histogram") == 0) {
128✔
180
      in_scatter_format = AngleDistributionType::HISTOGRAM;
64✔
181
    } else if (temp_str.compare(0, 7, "tabular") == 0) {
64✔
182
      in_scatter_format = AngleDistributionType::TABULAR;
64✔
183
    } else {
UNCOV
184
      fatal_error("Invalid scatter_format option!");
×
185
    }
186
  } else {
2,722✔
UNCOV
187
    in_scatter_format = AngleDistributionType::LEGENDRE;
×
188
  }
189

190
  if (attribute_exists(xs_id, "scatter_shape")) {
2,722✔
191
    std::string temp_str;
2,722✔
192
    read_attribute(xs_id, "scatter_shape", temp_str);
2,722✔
193
    to_lower(strtrim(temp_str));
2,722✔
194
    if (temp_str.compare(0, 14, "[g][g\'][order]") != 0) {
2,722✔
UNCOV
195
      fatal_error("Invalid scatter_shape option!");
×
196
    }
197
  }
2,722✔
198

199
  bool in_fissionable = false;
2,722✔
200
  if (attribute_exists(xs_id, "fissionable")) {
2,722✔
201
    int int_fiss;
202
    read_attr_int(xs_id, "fissionable", &int_fiss);
2,722✔
203
    in_fissionable = int_fiss;
2,722✔
204
  } else {
UNCOV
205
    fatal_error("Fissionable element must be set!");
×
206
  }
207

208
  // Get the library's value for the order
209
  if (attribute_exists(xs_id, "order")) {
2,722✔
210
    read_attr_int(xs_id, "order", &order_dim);
2,722✔
211
  } else {
UNCOV
212
    fatal_error("Order must be provided!");
×
213
  }
214

215
  // Store the dimensionality of the data in order_dim.
216
  // For Legendre data, we usually refer to it as Pn where n is the order.
217
  // However Pn has n+1 sets of points (since you need to count the P0
218
  // moment). Adjust for that. Histogram and Tabular formats dont need this
219
  // adjustment.
220
  if (in_scatter_format == AngleDistributionType::LEGENDRE) {
2,722✔
221
    ++order_dim;
2,594✔
222
  }
223

224
  // Get the angular information
225
  int in_n_pol;
226
  int in_n_azi;
227
  bool in_is_isotropic = true;
2,722✔
228
  if (attribute_exists(xs_id, "representation")) {
2,722✔
229
    std::string temp_str;
2,722✔
230
    read_attribute(xs_id, "representation", temp_str);
2,722✔
231
    to_lower(strtrim(temp_str));
2,722✔
232
    if (temp_str.compare(0, 5, "angle") == 0) {
2,722✔
233
      in_is_isotropic = false;
32✔
234
    } else if (temp_str.compare(0, 9, "isotropic") != 0) {
2,690✔
UNCOV
235
      fatal_error("Invalid Data Representation!");
×
236
    }
237
  }
2,722✔
238

239
  if (!in_is_isotropic) {
2,722✔
240
    if (attribute_exists(xs_id, "num_polar")) {
32✔
241
      read_attr_int(xs_id, "num_polar", &in_n_pol);
32✔
242
    } else {
UNCOV
243
      fatal_error("num_polar must be provided!");
×
244
    }
245
    if (attribute_exists(xs_id, "num_azimuthal")) {
32✔
246
      read_attr_int(xs_id, "num_azimuthal", &in_n_azi);
32✔
247
    } else {
UNCOV
248
      fatal_error("num_azimuthal must be provided!");
×
249
    }
250
  } else {
251
    in_n_pol = 1;
2,690✔
252
    in_n_azi = 1;
2,690✔
253
  }
254

255
  // Set the angular bins to use equally-spaced bins
256
  vector<double> in_polar(in_n_pol);
5,444✔
257
  double dangle = PI / in_n_pol;
2,722✔
258
  for (int p = 0; p < in_n_pol; p++) {
5,476✔
259
    in_polar[p] = (p + 0.5) * dangle;
2,754✔
260
  }
261
  vector<double> in_azimuthal(in_n_azi);
5,444✔
262
  dangle = 2. * PI / in_n_azi;
2,722✔
263
  for (int a = 0; a < in_n_azi; a++) {
5,476✔
264
    in_azimuthal[a] = (a + 0.5) * dangle - PI;
2,754✔
265
  }
266

267
  // Finally use this data to initialize the MGXS Object
268
  init(in_name, in_awr, in_kTs, in_fissionable, in_scatter_format,
2,722✔
269
    in_is_isotropic, in_polar, in_azimuthal);
270
}
2,722✔
271

272
//==============================================================================
273

274
Mgxs::Mgxs(
2,722✔
275
  hid_t xs_id, const vector<double>& temperature, int num_group, int num_delay)
2,722✔
276
  : num_groups(num_group), num_delayed_groups(num_delay)
2,722✔
277
{
278
  // Call generic data gathering routine (will populate the metadata)
279
  int order_data;
280
  vector<int> temps_to_read;
2,722✔
281
  metadata_from_hdf5(xs_id, temperature, temps_to_read, order_data);
2,722✔
282

283
  // Set number of energy and delayed groups
284
  AngleDistributionType final_scatter_format = scatter_format;
2,722✔
285
  if (settings::legendre_to_tabular) {
2,722✔
286
    if (scatter_format == AngleDistributionType::LEGENDRE)
2,466✔
287
      final_scatter_format = AngleDistributionType::TABULAR;
2,370✔
288
  }
289

290
  // Load the more specific XsData information
291
  for (int t = 0; t < temps_to_read.size(); t++) {
5,588✔
292
    xs[t] = XsData(fissionable, final_scatter_format, n_pol, n_azi, num_groups,
5,732✔
293
      num_delayed_groups);
5,732✔
294
    // Get the temperature as a string and then open the HDF5 group
295
    std::string temp_str = std::to_string(temps_to_read[t]) + "K";
2,866✔
296
    hid_t xsdata_grp = open_group(xs_id, temp_str.c_str());
2,866✔
297

298
    xs[t].from_hdf5(xsdata_grp, fissionable, scatter_format,
2,866✔
299
      final_scatter_format, order_data, is_isotropic, n_pol, n_azi);
2,866✔
300
    close_group(xsdata_grp);
2,866✔
301

302
  } // end temperature loop
2,866✔
303

304
  // Make sure the scattering format is updated to the final case
305
  scatter_format = final_scatter_format;
2,722✔
306
}
2,722✔
307

308
//==============================================================================
309

310
Mgxs::Mgxs(const std::string& in_name, const vector<double>& mat_kTs,
2,290✔
311
  const vector<Mgxs*>& micros, const vector<double>& atom_densities,
312
  int num_group, int num_delay)
2,290✔
313
  : num_groups(num_group), num_delayed_groups(num_delay)
2,290✔
314
{
315
  // Get the minimum data needed to initialize:
316
  // Dont need awr, but lets just initialize it anyways
317
  double in_awr = -1.;
2,290✔
318
  // start with the assumption it is not fissionable and set
319
  // the fissionable status if we learn differently
320
  bool in_fissionable = false;
2,290✔
321
  for (int m = 0; m < micros.size(); m++) {
4,964✔
322
    if (micros[m]->fissionable)
2,674✔
323
      in_fissionable = true;
992✔
324
  }
325
  // Force all of the following data to be the same; these will be verified
326
  // to be true later
327
  AngleDistributionType in_scatter_format = micros[0]->scatter_format;
2,290✔
328
  bool in_is_isotropic = micros[0]->is_isotropic;
2,290✔
329
  vector<double> in_polar = micros[0]->polar;
2,290✔
330
  vector<double> in_azimuthal = micros[0]->azimuthal;
2,290✔
331

332
  init(in_name, in_awr, mat_kTs, in_fissionable, in_scatter_format,
2,290✔
333
    in_is_isotropic, in_polar, in_azimuthal);
334

335
  // Create the xs data for each temperature
336
  for (int t = 0; t < mat_kTs.size(); t++) {
4,596✔
337
    xs[t] = XsData(in_fissionable, in_scatter_format, in_polar.size(),
4,612✔
338
      in_azimuthal.size(), num_groups, num_delayed_groups);
4,612✔
339

340
    // Find the right temperature index to use
341
    double temp_desired = mat_kTs[t];
2,306✔
342

343
    // Create the list of temperature indices and interpolation factors for
344
    // each microscopic data at the material temperature
345
    vector<int> micro_t(micros.size(), 0);
2,306✔
346
    vector<double> micro_t_interp(micros.size(), 0.);
2,306✔
347
    for (int m = 0; m < micros.size(); m++) {
4,996✔
348
      switch (settings::temperature_method) {
2,690✔
349
      case TemperatureMethod::NEAREST: {
2,498✔
350
        micro_t[m] = xt::argmin(xt::abs(micros[m]->kTs - temp_desired))[0];
2,498✔
351
        auto temp_actual = micros[m]->kTs[micro_t[m]];
2,498✔
352

353
        if (std::abs(temp_actual - temp_desired) >=
2,498✔
354
            K_BOLTZMANN * settings::temperature_tolerance) {
2,498✔
UNCOV
355
          fatal_error(fmt::format("MGXS Library does not contain cross section "
×
356
                                  "for {} at or near {} K.",
357
            name, std::round(temp_desired / K_BOLTZMANN)));
×
358
        }
359
      } break;
2,498✔
360
      case TemperatureMethod::INTERPOLATION:
192✔
361
        // Get a list of bounding temperatures for each actual temperature
362
        // present in the model
363
        for (int k = 0; k < micros[m]->kTs.shape()[0] - 1; k++) {
384✔
364
          if ((micros[m]->kTs[k] <= temp_desired) &&
384✔
365
              (temp_desired < micros[m]->kTs[k + 1])) {
192✔
366
            micro_t[m] = k;
192✔
367
            if (k == 0) {
192✔
368
              micro_t_interp[m] = (temp_desired - micros[m]->kTs[k]) /
384✔
369
                                  (micros[m]->kTs[k + 1] - micros[m]->kTs[k]);
192✔
370
            } else {
UNCOV
371
              micro_t_interp[m] = 1.;
×
372
            }
373
          }
374
        }
375
      } // end switch
376
    }   // end microscopic temperature loop
377

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

411
    // And finally, combine the data
412
    combine(mgxs_to_combine, interpolant, temp_indices, t);
2,306✔
413
  } // end temperature (t) loop
2,306✔
414
}
2,290✔
415

416
//==============================================================================
417

418
void Mgxs::combine(const vector<Mgxs*>& micros, const vector<double>& scalars,
2,306✔
419
  const vector<int>& micro_ts, int this_t)
420
{
421
  // Build the vector of pointers to the xs objects within micros
422
  vector<XsData*> those_xs(micros.size());
2,306✔
423
  for (int i = 0; i < micros.size(); i++) {
5,188✔
424
    those_xs[i] = &(micros[i]->xs[micro_ts[i]]);
2,882✔
425
  }
426

427
  xs[this_t].combine(those_xs, scalars);
2,306✔
428
}
2,306✔
429

430
//==============================================================================
431

432
double Mgxs::get_xs(MgxsType xstype, int gin, const int* gout, const double* mu,
234,934,053✔
433
  const int* dg, int t, int a)
434
{
435
  XsData* xs_t = &xs[t];
234,934,053✔
436
  double val;
437
  switch (xstype) {
234,934,053✔
438
  case MgxsType::TOTAL:
20,633,712✔
439
    val = xs_t->total(a, gin);
20,633,712✔
440
    break;
20,633,712✔
441
  case MgxsType::NU_FISSION:
10,189,366✔
442
    val = fissionable ? xs_t->nu_fission(a, gin) : 0.;
10,189,366✔
443
    break;
10,189,366✔
444
  case MgxsType::ABSORPTION:
5,819,891✔
445
    val = xs_t->absorption(a, gin);
5,819,891✔
446
    ;
447
    break;
5,819,891✔
448
  case MgxsType::FISSION:
11,161,524✔
449
    val = fissionable ? xs_t->fission(a, gin) : 0.;
11,161,524✔
450
    break;
11,161,524✔
451
  case MgxsType::KAPPA_FISSION:
10,425,382✔
452
    val = fissionable ? xs_t->kappa_fission(a, gin) : 0.;
10,425,382✔
453
    break;
10,425,382✔
454
  case MgxsType::NU_SCATTER:
18,594,864✔
455
  case MgxsType::SCATTER:
456
  case MgxsType::NU_SCATTER_FMU:
457
  case MgxsType::SCATTER_FMU:
458
    val = xs_t->scatter[a]->get_xs(xstype, gin, gout, mu);
18,594,864✔
459
    break;
18,594,864✔
460
  case MgxsType::PROMPT_NU_FISSION:
10,182,502✔
461
    val = fissionable ? xs_t->prompt_nu_fission(a, gin) : 0.;
10,182,502✔
462
    break;
10,182,502✔
463
  case MgxsType::DELAYED_NU_FISSION:
71,277,514✔
464
    if (fissionable) {
71,277,514✔
465
      if (dg != nullptr) {
71,277,514✔
466
        val = xs_t->delayed_nu_fission(a, *dg, gin);
61,095,012✔
467
      } else {
468
        val = 0.;
10,182,502✔
469
        for (int d = 0; d < xs_t->delayed_nu_fission.shape()[1]; d++) {
71,277,514✔
470
          val += xs_t->delayed_nu_fission(a, d, gin);
61,095,012✔
471
        }
472
      }
473
    } else {
UNCOV
474
      val = 0.;
×
475
    }
476
    break;
71,277,514✔
477
  case MgxsType::CHI_PROMPT:
6,864✔
478
    if (fissionable) {
6,864✔
479
      if (gout != nullptr) {
2,240✔
480
        val = xs_t->chi_prompt(a, gin, *gout);
2,240✔
481
      } else {
482
        // provide an outgoing group-wise sum
UNCOV
483
        val = 0.;
×
484
        for (int g = 0; g < xs_t->chi_prompt.shape()[2]; g++) {
×
UNCOV
485
          val += xs_t->chi_prompt(a, gin, g);
×
486
        }
487
      }
488
    } else {
489
      val = 0.;
4,624✔
490
    }
491
    break;
6,864✔
UNCOV
492
  case MgxsType::CHI_DELAYED:
×
493
    if (fissionable) {
×
494
      if (gout != nullptr) {
×
495
        if (dg != nullptr) {
×
496
          val = xs_t->chi_delayed(a, *dg, gin, *gout);
×
497
        } else {
UNCOV
498
          val = xs_t->chi_delayed(a, 0, gin, *gout);
×
499
        }
500
      } else {
UNCOV
501
        if (dg != nullptr) {
×
502
          val = 0.;
×
UNCOV
503
          for (int g = 0; g < xs_t->delayed_nu_fission.shape()[2]; g++) {
×
504
            val += xs_t->delayed_nu_fission(a, *dg, gin, g);
×
505
          }
506
        } else {
UNCOV
507
          val = 0.;
×
UNCOV
508
          for (int g = 0; g < xs_t->delayed_nu_fission.shape()[2]; g++) {
×
UNCOV
509
            for (int d = 0; d < xs_t->delayed_nu_fission.shape()[3]; d++) {
×
UNCOV
510
              val += xs_t->delayed_nu_fission(a, d, gin, g);
×
511
            }
512
          }
513
        }
514
      }
515
    } else {
516
      val = 0.;
×
517
    }
UNCOV
518
    break;
×
519
  case MgxsType::INVERSE_VELOCITY:
15,545,662✔
520
    val = xs_t->inverse_velocity(a, gin);
15,545,662✔
521
    break;
15,545,662✔
522
  case MgxsType::DECAY_RATE:
61,096,772✔
523
    if (dg != nullptr) {
61,096,772✔
524
      val = xs_t->decay_rate(a, *dg);
61,096,772✔
525
    } else {
UNCOV
526
      val = xs_t->decay_rate(a, 0);
×
527
    }
528
    break;
61,096,772✔
UNCOV
529
  default:
×
UNCOV
530
    val = 0.;
×
531
  }
532
  return val;
234,934,053✔
533
}
534

535
//==============================================================================
536

537
void Mgxs::sample_fission_energy(
111,055,153✔
538
  int gin, int& dg, int& gout, uint64_t* seed, int t, int a)
539
{
540
  XsData* xs_t = &xs[t];
111,055,153✔
541
  double nu_fission = xs_t->nu_fission(a, gin);
111,055,153✔
542

543
  // Find the probability of having a prompt neutron
544
  double prob_prompt = xs_t->prompt_nu_fission(a, gin);
111,055,153✔
545

546
  // sample random numbers
547
  double xi_pd = prn(seed) * nu_fission;
111,055,153✔
548
  double xi_gout = prn(seed);
111,055,153✔
549

550
  // Select whether the neutron is prompt or delayed
551
  if (xi_pd <= prob_prompt) {
111,055,153✔
552
    // the neutron is prompt
553

554
    // set the delayed group for the particle to be -1, indicating prompt
555
    dg = -1;
111,053,613✔
556

557
    // sample the outgoing energy group
558
    double prob_gout = 0.;
111,053,613✔
559
    for (gout = 0; gout < num_groups; ++gout) {
111,103,047✔
560
      prob_gout += xs_t->chi_prompt(a, gin, gout);
111,103,047✔
561
      if (xi_gout < prob_gout)
111,103,047✔
562
        break;
111,053,613✔
563
    }
564

565
  } else {
566
    // the neutron is delayed
567

568
    // get the delayed group
569
    for (dg = 0; dg < num_delayed_groups; ++dg) {
3,905✔
570
      prob_prompt += xs_t->delayed_nu_fission(a, dg, gin);
3,905✔
571
      if (xi_pd < prob_prompt)
3,905✔
572
        break;
1,540✔
573
    }
574

575
    // adjust dg in case of round-off error
576
    dg = std::min(dg, num_delayed_groups - 1);
1,540✔
577

578
    // sample the outgoing energy group
579
    double prob_gout = 0.;
1,540✔
580
    for (gout = 0; gout < num_groups; ++gout) {
1,540✔
581
      prob_gout += xs_t->chi_delayed(a, dg, gin, gout);
1,540✔
582
      if (xi_gout < prob_gout)
1,540✔
583
        break;
1,540✔
584
    }
585
  }
586
}
111,055,153✔
587

588
//==============================================================================
589

590
void Mgxs::sample_scatter(
1,669,113,996✔
591
  int gin, int& gout, double& mu, double& wgt, uint64_t* seed, int t, int a)
592
{
593
  // Sample the data
594
  xs[t].scatter[a]->sample(gin, gout, mu, wgt, seed);
1,669,113,996✔
595
}
1,669,113,996✔
596

597
//==============================================================================
598

599
void Mgxs::calculate_xs(Particle& p)
2,079,768,108✔
600
{
601
  // If the material is different, then we need to do a full lookup
602
  if (p.material() != p.mg_xs_cache().material) {
2,079,768,108✔
603
    set_temperature_index(p);
113,584,850✔
604
    set_angle_index(p);
113,584,850✔
605
    p.mg_xs_cache().material = p.material();
113,584,850✔
606
  } else {
607
    // If material is the same, but temperature is different, need to
608
    // find the new temperature index
609
    if (p.sqrtkT() != p.mg_xs_cache().sqrtkT) {
1,966,183,258✔
610
      set_temperature_index(p);
2,245,339✔
611
    }
612
    // If the material is the same, but angle is different, need to
613
    // find the new angle index
614
    if (p.u_local() != p.mg_xs_cache().u) {
1,966,183,258✔
615
      set_angle_index(p);
1,966,183,258✔
616
    }
617
  }
618
  int temperature = p.mg_xs_cache().t;
2,079,768,108✔
619
  int angle = p.mg_xs_cache().a;
2,079,768,108✔
620
  p.macro_xs().total = xs[temperature].total(angle, p.g());
2,079,768,108✔
621
  p.macro_xs().absorption = xs[temperature].absorption(angle, p.g());
2,079,768,108✔
622
  p.macro_xs().nu_fission =
2,079,768,108✔
623
    fissionable ? xs[temperature].nu_fission(angle, p.g()) : 0.;
2,079,768,108✔
624
}
2,079,768,108✔
625

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

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

639
//==============================================================================
640

641
int Mgxs::get_temperature_index(double sqrtkT) const
126,005,442✔
642
{
643
  return xt::argmin(xt::abs(kTs - sqrtkT * sqrtkT))[0];
126,005,442✔
644
}
645

646
//==============================================================================
647

648
void Mgxs::set_temperature_index(Particle& p)
115,830,189✔
649
{
650
  p.mg_xs_cache().t = get_temperature_index(p.sqrtkT());
115,830,189✔
651
  p.mg_xs_cache().sqrtkT = p.sqrtkT();
115,830,189✔
652
}
115,830,189✔
653

654
//==============================================================================
655

656
int Mgxs::get_angle_index(const Direction& u) const
89,030,579✔
657
{
658
  if (is_isotropic) {
89,030,579✔
659
    return 0;
89,008,392✔
660
  } else {
661
    // convert direction to polar and azimuthal angles
662
    double my_pol = std::acos(u.z);
22,187✔
663
    double my_azi = std::atan2(u.y, u.x);
22,187✔
664

665
    // Find the location, assuming equal-bin angles
666
    double delta_angle = PI / n_pol;
22,187✔
667
    int p = std::floor(my_pol / delta_angle);
22,187✔
668
    delta_angle = 2. * PI / n_azi;
22,187✔
669
    int a = std::floor((my_azi + PI) / delta_angle);
22,187✔
670

671
    return n_azi * p + a;
22,187✔
672
  }
673
}
674

675
//==============================================================================
676

677
void Mgxs::set_angle_index(Particle& p)
2,079,768,108✔
678
{
679
  // See if we need to find the new index
680
  if (!is_isotropic) {
2,079,768,108✔
681
    p.mg_xs_cache().a = get_angle_index(p.u_local());
22,187✔
682
    p.mg_xs_cache().u = p.u_local();
22,187✔
683
  }
684
}
2,079,768,108✔
685

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