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

openmc-dev / openmc / 15371300071

01 Jun 2025 05:04AM UTC coverage: 85.143% (+0.3%) from 84.827%
15371300071

Pull #3176

github

web-flow
Merge 4f739184a into cb95c784b
Pull Request #3176: MeshFilter rotation - solution to issue #3166

86 of 99 new or added lines in 4 files covered. (86.87%)

3707 existing lines in 117 files now uncovered.

52212 of 61323 relevant lines covered (85.14%)

42831974.38 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,
6,890✔
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;
6,890✔
34
  awr = in_awr;
6,890✔
35
  // TODO: Remove adapt when in_KTs is an xtensor
36
  kTs = xt::adapt(in_kTs);
6,890✔
37
  fissionable = in_fissionable;
6,890✔
38
  scatter_format = in_scatter_format;
6,890✔
39
  xs.resize(in_kTs.size());
6,890✔
40
  is_isotropic = in_is_isotropic;
6,890✔
41
  n_pol = in_polar.size();
6,890✔
42
  n_azi = in_azimuthal.size();
6,890✔
43
  polar = in_polar;
6,890✔
44
  azimuthal = in_azimuthal;
6,890✔
45
}
6,890✔
46

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

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

58
  // Get the AWR
59
  double in_awr;
60
  if (attribute_exists(xs_id, "atomic_weight_ratio")) {
3,742✔
61
    read_attr_double(xs_id, "atomic_weight_ratio", &in_awr);
176✔
62
  } else {
63
    in_awr = MACROSCOPIC_AWR;
3,566✔
64
  }
65

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

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

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

88
  // If only one temperature is available, lets just use nearest temperature
89
  // interpolation
90
  if ((num_temps == 1) &&
3,742✔
91
      (settings::temperature_method == TemperatureMethod::INTERPOLATION)) {
3,060✔
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) {
3,742✔
99
  case TemperatureMethod::NEAREST:
3,478✔
100
    // Determine actual temperatures to read
101
    for (const auto& T : temperature) {
6,890✔
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();
3,412✔
108
      int i_closest = 0;
3,412✔
109
      for (int i = 0; i < temps_available.size(); i++) {
7,660✔
110
        double diff = std::abs(temps_available[i] - T);
4,248✔
111
        if (diff < closest) {
4,248✔
112
          closest = diff;
3,830✔
113
          i_closest = i;
3,830✔
114
        }
115
      }
116

117
      double temp_actual = temps_available[i_closest];
3,412✔
118
      if (std::fabs(temp_actual - T) < settings::temperature_tolerance) {
3,412✔
119
        if (std::find(temps_to_read.begin(), temps_to_read.end(),
3,412✔
120
              std::round(temp_actual)) == temps_to_read.end()) {
6,824✔
121
          temps_to_read.push_back(std::round(temp_actual));
3,412✔
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;
3,478✔
133

134
  case TemperatureMethod::INTERPOLATION:
264✔
135
    for (int i = 0; i < temperature.size(); i++) {
528✔
136
      for (int j = 0; j < num_temps; j++) {
396✔
137
        if (j == (num_temps - 1)) {
396✔
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]) &&
792✔
143
            (temperature[i] < temps_available[j + 1])) {
396✔
144
          if (std::find(temps_to_read.begin(), temps_to_read.end(),
264✔
145
                temps_available[j]) == temps_to_read.end()) {
528✔
146
            temps_to_read.push_back(temps_available[j]);
264✔
147
          }
148

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

160
  // Get the library's temperatures
161
  int n_temperature = temps_to_read.size();
3,742✔
162
  vector<double> in_kTs(n_temperature);
7,484✔
163
  for (int i = 0; i < n_temperature; i++) {
7,682✔
164
    std::string temp_str(std::to_string(temps_to_read[i]) + "K");
3,940✔
165

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

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

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

199
  bool in_fissionable = false;
3,742✔
200
  if (attribute_exists(xs_id, "fissionable")) {
3,742✔
201
    int int_fiss;
202
    read_attr_int(xs_id, "fissionable", &int_fiss);
3,742✔
203
    in_fissionable = int_fiss;
3,742✔
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")) {
3,742✔
210
    read_attr_int(xs_id, "order", &order_dim);
3,742✔
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) {
3,742✔
221
    ++order_dim;
3,566✔
222
  }
223

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

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

255
  // Set the angular bins to use equally-spaced bins
256
  vector<double> in_polar(in_n_pol);
7,484✔
257
  double dangle = PI / in_n_pol;
3,742✔
258
  for (int p = 0; p < in_n_pol; p++) {
7,528✔
259
    in_polar[p] = (p + 0.5) * dangle;
3,786✔
260
  }
261
  vector<double> in_azimuthal(in_n_azi);
7,484✔
262
  dangle = 2. * PI / in_n_azi;
3,742✔
263
  for (int a = 0; a < in_n_azi; a++) {
7,528✔
264
    in_azimuthal[a] = (a + 0.5) * dangle - PI;
3,786✔
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,
3,742✔
269
    in_is_isotropic, in_polar, in_azimuthal);
270
}
3,742✔
271

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

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

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

290
  // Load the more specific XsData information
291
  for (int t = 0; t < temps_to_read.size(); t++) {
7,682✔
292
    xs[t] = XsData(fissionable, final_scatter_format, n_pol, n_azi, num_groups,
7,880✔
293
      num_delayed_groups);
7,880✔
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";
3,940✔
296
    hid_t xsdata_grp = open_group(xs_id, temp_str.c_str());
3,940✔
297

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

302
  } // end temperature loop
3,940✔
303

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

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

310
Mgxs::Mgxs(const std::string& in_name, const vector<double>& mat_kTs,
3,148✔
311
  const vector<Mgxs*>& micros, const vector<double>& atom_densities,
312
  int num_group, int num_delay)
3,148✔
313
  : num_groups(num_group), num_delayed_groups(num_delay)
3,148✔
314
{
315
  // Get the minimum data needed to initialize:
316
  // Dont need awr, but lets just initialize it anyways
317
  double in_awr = -1.;
3,148✔
318
  // start with the assumption it is not fissionable and set
319
  // the fissionable status if we learn differently
320
  bool in_fissionable = false;
3,148✔
321
  for (int m = 0; m < micros.size(); m++) {
6,824✔
322
    if (micros[m]->fissionable)
3,676✔
323
      in_fissionable = true;
1,364✔
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;
3,148✔
328
  bool in_is_isotropic = micros[0]->is_isotropic;
3,148✔
329
  vector<double> in_polar = micros[0]->polar;
3,148✔
330
  vector<double> in_azimuthal = micros[0]->azimuthal;
3,148✔
331

332
  init(in_name, in_awr, mat_kTs, in_fissionable, in_scatter_format,
3,148✔
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++) {
6,318✔
337
    xs[t] = XsData(in_fissionable, in_scatter_format, in_polar.size(),
6,340✔
338
      in_azimuthal.size(), num_groups, num_delayed_groups);
6,340✔
339

340
    // Find the right temperature index to use
341
    double temp_desired = mat_kTs[t];
3,170✔
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);
3,170✔
346
    vector<double> micro_t_interp(micros.size(), 0.);
3,170✔
347
    for (int m = 0; m < micros.size(); m++) {
6,868✔
348
      switch (settings::temperature_method) {
3,698✔
349
      case TemperatureMethod::NEAREST: {
3,434✔
350
        micro_t[m] = xt::argmin(xt::abs(micros[m]->kTs - temp_desired))[0];
3,434✔
351
        auto temp_actual = micros[m]->kTs[micro_t[m]];
3,434✔
352

353
        if (std::abs(temp_actual - temp_desired) >=
3,434✔
354
            K_BOLTZMANN * settings::temperature_tolerance) {
3,434✔
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;
3,434✔
360
      case TemperatureMethod::INTERPOLATION:
264✔
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++) {
528✔
364
          if ((micros[m]->kTs[k] <= temp_desired) &&
528✔
365
              (temp_desired < micros[m]->kTs[k + 1])) {
264✔
366
            micro_t[m] = k;
264✔
367
            if (k == 0) {
264✔
368
              micro_t_interp[m] = (temp_desired - micros[m]->kTs[k]) /
528✔
369
                                  (micros[m]->kTs[k + 1] - micros[m]->kTs[k]);
264✔
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
6,340✔
384
    vector<int> temp_indices;      // the temperature index for each Mgxs
6,340✔
385
    vector<Mgxs*> mgxs_to_combine; // The Mgxs to combine
6,340✔
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++) {
6,868✔
390
      if (settings::temperature_method == TemperatureMethod::NEAREST) {
3,698✔
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]);
3,434✔
395
        temp_indices.push_back(micro_t[m]);
3,434✔
396
        mgxs_to_combine.push_back(micros[m]);
3,434✔
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]);
264✔
402
        temp_indices.push_back(micro_t[m]);
264✔
403
        mgxs_to_combine.push_back(micros[m]);
264✔
404
        // The higher point
405
        interpolant.push_back((micro_t_interp[m]) * atom_densities[m]);
264✔
406
        temp_indices.push_back(micro_t[m] + 1);
264✔
407
        mgxs_to_combine.push_back(micros[m]);
264✔
408
      }
409
    }
410

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

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

418
void Mgxs::combine(const vector<Mgxs*>& micros, const vector<double>& scalars,
3,170✔
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());
3,170✔
423
  for (int i = 0; i < micros.size(); i++) {
7,132✔
424
    those_xs[i] = &(micros[i]->xs[micro_ts[i]]);
3,962✔
425
  }
426

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

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

432
double Mgxs::get_xs(MgxsType xstype, int gin, const int* gout, const double* mu,
320,367,783✔
433
  const int* dg, int t, int a)
434
{
435
  XsData* xs_t = &xs[t];
320,367,783✔
436
  double val;
437
  switch (xstype) {
320,367,783✔
438
  case MgxsType::TOTAL:
28,136,958✔
439
    val = xs_t->total(a, gin);
28,136,958✔
440
    break;
28,136,958✔
441
  case MgxsType::NU_FISSION:
13,894,668✔
442
    val = fissionable ? xs_t->nu_fission(a, gin) : 0.;
13,894,668✔
443
    break;
13,894,668✔
444
  case MgxsType::ABSORPTION:
7,936,215✔
445
    val = xs_t->absorption(a, gin);
7,936,215✔
446
    ;
447
    break;
7,936,215✔
448
  case MgxsType::FISSION:
15,220,338✔
449
    val = fissionable ? xs_t->fission(a, gin) : 0.;
15,220,338✔
450
    break;
15,220,338✔
451
  case MgxsType::KAPPA_FISSION:
14,216,430✔
452
    val = fissionable ? xs_t->kappa_fission(a, gin) : 0.;
14,216,430✔
453
    break;
14,216,430✔
454
  case MgxsType::NU_SCATTER:
25,359,486✔
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);
25,359,486✔
459
    break;
25,359,486✔
460
  case MgxsType::PROMPT_NU_FISSION:
13,885,230✔
461
    val = fissionable ? xs_t->prompt_nu_fission(a, gin) : 0.;
13,885,230✔
462
    break;
13,885,230✔
463
  case MgxsType::DELAYED_NU_FISSION:
97,196,610✔
464
    if (fissionable) {
97,196,610✔
465
      if (dg != nullptr) {
97,196,610✔
466
        val = xs_t->delayed_nu_fission(a, *dg, gin);
83,311,380✔
467
      } else {
468
        val = 0.;
13,885,230✔
469
        for (int d = 0; d < xs_t->delayed_nu_fission.shape()[1]; d++) {
97,196,610✔
470
          val += xs_t->delayed_nu_fission(a, d, gin);
83,311,380✔
471
        }
472
      }
473
    } else {
UNCOV
474
      val = 0.;
×
475
    }
476
    break;
97,196,610✔
477
  case MgxsType::CHI_PROMPT:
9,438✔
478
    if (fissionable) {
9,438✔
479
      if (gout != nullptr) {
3,080✔
480
        val = xs_t->chi_prompt(a, gin, *gout);
3,080✔
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.;
6,358✔
490
    }
491
    break;
9,438✔
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:
21,198,630✔
520
    val = xs_t->inverse_velocity(a, gin);
21,198,630✔
521
    break;
21,198,630✔
522
  case MgxsType::DECAY_RATE:
83,313,780✔
523
    if (dg != nullptr) {
83,313,780✔
524
      val = xs_t->decay_rate(a, *dg);
83,313,780✔
525
    } else {
UNCOV
526
      val = xs_t->decay_rate(a, 0);
×
527
    }
528
    break;
83,313,780✔
UNCOV
529
  default:
×
UNCOV
530
    val = 0.;
×
531
  }
532
  return val;
320,367,783✔
533
}
534

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

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

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

546
  // sample random numbers
547
  double xi_pd = prn(seed) * nu_fission;
151,438,845✔
548
  double xi_gout = prn(seed);
151,438,845✔
549

550
  // Select whether the neutron is prompt or delayed
551
  if (xi_pd <= prob_prompt) {
151,438,845✔
552
    // the neutron is prompt
553

554
    // set the delayed group for the particle to be -1, indicating prompt
555
    dg = -1;
151,436,745✔
556

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

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

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

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

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

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

590
void Mgxs::sample_scatter(
2,147,483,647✔
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);
2,147,483,647✔
595
}
2,147,483,647✔
596

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

599
void Mgxs::calculate_xs(Particle& p)
2,147,483,647✔
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,147,483,647✔
603
    set_temperature_index(p);
158,528,582✔
604
    set_angle_index(p);
158,528,582✔
605
    p.mg_xs_cache().material = p.material();
158,528,582✔
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) {
2,147,483,647✔
610
      set_temperature_index(p);
3,060,827✔
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) {
2,147,483,647✔
615
      set_angle_index(p);
2,147,483,647✔
616
    }
617
  }
618
  int temperature = p.mg_xs_cache().t;
2,147,483,647✔
619
  int angle = p.mg_xs_cache().a;
2,147,483,647✔
620
  p.macro_xs().total = xs[temperature].total(angle, p.g());
2,147,483,647✔
621
  p.macro_xs().absorption = xs[temperature].absorption(angle, p.g());
2,147,483,647✔
622
  p.macro_xs().nu_fission =
2,147,483,647✔
623
    fissionable ? xs[temperature].nu_fission(angle, p.g()) : 0.;
2,147,483,647✔
624
}
2,147,483,647✔
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
175,464,754✔
642
{
643
  return xt::argmin(xt::abs(kTs - sqrtkT * sqrtkT))[0];
175,464,754✔
644
}
645

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

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

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

656
int Mgxs::get_angle_index(const Direction& u) const
121,405,335✔
657
{
658
  if (is_isotropic) {
121,405,335✔
659
    return 0;
121,375,080✔
660
  } else {
661
    // convert direction to polar and azimuthal angles
662
    double my_pol = std::acos(u.z);
30,255✔
663
    double my_azi = std::atan2(u.y, u.x);
30,255✔
664

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

671
    return n_azi * p + a;
30,255✔
672
  }
673
}
674

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

677
void Mgxs::set_angle_index(Particle& p)
2,147,483,647✔
678
{
679
  // See if we need to find the new index
680
  if (!is_isotropic) {
2,147,483,647✔
681
    p.mg_xs_cache().a = get_angle_index(p.u_local());
30,255✔
682
    p.mg_xs_cache().u = p.u_local();
30,255✔
683
  }
684
}
2,147,483,647✔
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