• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

openmc-dev / openmc / 21991279157

13 Feb 2026 02:53PM UTC coverage: 81.82% (-0.06%) from 81.875%
21991279157

Pull #3805

github

web-flow
Merge 0a7a80411 into bcb939520
Pull Request #3805: Remove xtensor and xtl Dependencies

17242 of 24268 branches covered (71.05%)

Branch coverage included in aggregate %.

977 of 1013 new or added lines in 39 files covered. (96.45%)

404 existing lines in 8 files now uncovered.

57420 of 66983 relevant lines covered (85.72%)

45458907.73 hits per line

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

78.31
/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/random_lcg.h"
15
#include "openmc/settings.h"
16
#include "openmc/string_utils.h"
17

18
namespace openmc {
19

20
//==============================================================================
21
// Mgxs base-class methods
22
//==============================================================================
23

24
void Mgxs::init(const std::string& in_name, double in_awr,
5,592✔
25
  const vector<double>& in_kTs, bool in_fissionable,
26
  AngleDistributionType in_scatter_format, bool in_is_isotropic,
27
  const vector<double>& in_polar, const vector<double>& in_azimuthal)
28
{
29
  // Set the metadata
30
  name = in_name;
5,592✔
31
  awr = in_awr;
5,592✔
32
  kTs = tensor::Tensor<double>(in_kTs.data(), in_kTs.size());
5,592✔
33
  fissionable = in_fissionable;
5,592✔
34
  scatter_format = in_scatter_format;
5,592✔
35
  xs.resize(in_kTs.size());
5,592✔
36
  is_isotropic = in_is_isotropic;
5,592✔
37
  n_pol = in_polar.size();
5,592✔
38
  n_azi = in_azimuthal.size();
5,592✔
39
  polar = in_polar;
5,592✔
40
  azimuthal = in_azimuthal;
5,592✔
41
}
5,592✔
42

43
//==============================================================================
44

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

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

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

75
    // convert eV to Kelvin
76
    temps_available[i] = std::round(temps_available[i] / K_BOLTZMANN);
3,993✔
77

78
    // Done with dset_names, so delete it
79
    delete[] dset_names[i];
3,993!
80
  }
81
  delete[] dset_names;
2,985!
82
  std::sort(temps_available.begin(), temps_available.end());
2,985✔
83

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

94
  switch (settings::temperature_method) {
2,985!
95
  case TemperatureMethod::NEAREST:
2,817✔
96
    // Determine actual temperatures to read
97
    for (const auto& T : temperature) {
5,606✔
98
      // Determine the closest temperature value
99
      auto i_closest = tensor::abs(temps_available - T).argmin();
2,789✔
100

101
      double temp_actual = temps_available[i_closest];
2,789✔
102
      if (std::fabs(temp_actual - T) < settings::temperature_tolerance) {
2,789!
103
        if (std::find(temps_to_read.begin(), temps_to_read.end(),
2,789✔
104
              std::round(temp_actual)) == temps_to_read.end()) {
5,578!
105
          temps_to_read.push_back(std::round(temp_actual));
2,789✔
106
        }
107
      } else {
108
        fatal_error(fmt::format(
×
109
          "MGXS library does not contain cross sections "
110
          "for {} at or near {} K. Available temperatures "
111
          "are {} K. Consider making use of openmc.Settings.temperature "
112
          "to specify how intermediate temperatures are treated.",
113
          in_name, std::round(T), concatenate(temps_available)));
×
114
      }
115
    }
116
    break;
2,817✔
117

118
  case TemperatureMethod::INTERPOLATION:
168✔
119
    for (int i = 0; i < temperature.size(); i++) {
336✔
120
      for (int j = 0; j < num_temps; j++) {
252!
121
        if (j == (num_temps - 1)) {
252!
122
          fatal_error("MGXS Library does not contain cross sections for " +
×
123
                      in_name + " at temperatures that bound " +
×
124
                      std::to_string(std::round(temperature[i])));
×
125
        }
126
        if ((temps_available[j] <= temperature[i]) &&
504!
127
            (temperature[i] < temps_available[j + 1])) {
252✔
128
          if (std::find(temps_to_read.begin(), temps_to_read.end(),
168✔
129
                temps_available[j]) == temps_to_read.end()) {
336!
130
            temps_to_read.push_back(temps_available[j]);
168✔
131
          }
132

133
          if (std::find(temps_to_read.begin(), temps_to_read.end(),
168✔
134
                temps_available[j + 1]) == temps_to_read.end()) {
336!
135
            temps_to_read.push_back(temps_available[j + 1]);
168✔
136
          }
137
          break;
168✔
138
        }
139
      }
140
    }
141
  }
142
  std::sort(temps_to_read.begin(), temps_to_read.end());
2,985✔
143

144
  // Get the library's temperatures
145
  int n_temperature = temps_to_read.size();
2,985✔
146
  vector<double> in_kTs(n_temperature);
5,970✔
147
  for (int i = 0; i < n_temperature; i++) {
6,110✔
148
    std::string temp_str(std::to_string(temps_to_read[i]) + "K");
3,125✔
149

150
    // read exact temperature value
151
    read_double(kT_group, temp_str.c_str(), &in_kTs[i], true);
3,125✔
152
  }
3,125✔
153
  close_group(kT_group);
2,985✔
154

155
  // Load the remaining metadata
156
  AngleDistributionType in_scatter_format;
157
  if (attribute_exists(xs_id, "scatter_format")) {
2,985!
158
    std::string temp_str;
2,985✔
159
    read_attribute(xs_id, "scatter_format", temp_str);
2,985✔
160
    to_lower(strtrim(temp_str));
2,985✔
161
    if (temp_str.compare(0, 8, "legendre") == 0) {
2,985✔
162
      in_scatter_format = AngleDistributionType::LEGENDRE;
2,873✔
163
    } else if (temp_str.compare(0, 9, "histogram") == 0) {
112✔
164
      in_scatter_format = AngleDistributionType::HISTOGRAM;
56✔
165
    } else if (temp_str.compare(0, 7, "tabular") == 0) {
56!
166
      in_scatter_format = AngleDistributionType::TABULAR;
56✔
167
    } else {
168
      fatal_error("Invalid scatter_format option!");
×
169
    }
170
  } else {
2,985✔
171
    in_scatter_format = AngleDistributionType::LEGENDRE;
×
172
  }
173

174
  if (attribute_exists(xs_id, "scatter_shape")) {
2,985!
175
    std::string temp_str;
2,985✔
176
    read_attribute(xs_id, "scatter_shape", temp_str);
2,985✔
177
    to_lower(strtrim(temp_str));
2,985✔
178
    if (temp_str.compare(0, 14, "[g][g\'][order]") != 0) {
2,985!
179
      fatal_error("Invalid scatter_shape option!");
×
180
    }
181
  }
2,985✔
182

183
  bool in_fissionable = false;
2,985✔
184
  if (attribute_exists(xs_id, "fissionable")) {
2,985!
185
    int int_fiss;
186
    read_attr_int(xs_id, "fissionable", &int_fiss);
2,985✔
187
    in_fissionable = int_fiss;
2,985✔
188
  } else {
189
    fatal_error("Fissionable element must be set!");
×
190
  }
191

192
  // Get the library's value for the order
193
  if (attribute_exists(xs_id, "order")) {
2,985!
194
    read_attr_int(xs_id, "order", &order_dim);
2,985✔
195
  } else {
196
    fatal_error("Order must be provided!");
×
197
  }
198

199
  // Store the dimensionality of the data in order_dim.
200
  // For Legendre data, we usually refer to it as Pn where n is the order.
201
  // However Pn has n+1 sets of points (since you need to count the P0
202
  // moment). Adjust for that. Histogram and Tabular formats dont need this
203
  // adjustment.
204
  if (in_scatter_format == AngleDistributionType::LEGENDRE) {
2,985✔
205
    ++order_dim;
2,873✔
206
  }
207

208
  // Get the angular information
209
  int in_n_pol;
210
  int in_n_azi;
211
  bool in_is_isotropic = true;
2,985✔
212
  if (attribute_exists(xs_id, "representation")) {
2,985!
213
    std::string temp_str;
2,985✔
214
    read_attribute(xs_id, "representation", temp_str);
2,985✔
215
    to_lower(strtrim(temp_str));
2,985✔
216
    if (temp_str.compare(0, 5, "angle") == 0) {
2,985✔
217
      in_is_isotropic = false;
28✔
218
    } else if (temp_str.compare(0, 9, "isotropic") != 0) {
2,957!
219
      fatal_error("Invalid Data Representation!");
×
220
    }
221
  }
2,985✔
222

223
  if (!in_is_isotropic) {
2,985✔
224
    if (attribute_exists(xs_id, "num_polar")) {
28!
225
      read_attr_int(xs_id, "num_polar", &in_n_pol);
28✔
226
    } else {
227
      fatal_error("num_polar must be provided!");
×
228
    }
229
    if (attribute_exists(xs_id, "num_azimuthal")) {
28!
230
      read_attr_int(xs_id, "num_azimuthal", &in_n_azi);
28✔
231
    } else {
232
      fatal_error("num_azimuthal must be provided!");
×
233
    }
234
  } else {
235
    in_n_pol = 1;
2,957✔
236
    in_n_azi = 1;
2,957✔
237
  }
238

239
  // Set the angular bins to use equally-spaced bins
240
  vector<double> in_polar(in_n_pol);
5,970✔
241
  double dangle = PI / in_n_pol;
2,985✔
242
  for (int p = 0; p < in_n_pol; p++) {
5,998✔
243
    in_polar[p] = (p + 0.5) * dangle;
3,013✔
244
  }
245
  vector<double> in_azimuthal(in_n_azi);
5,970✔
246
  dangle = 2. * PI / in_n_azi;
2,985✔
247
  for (int a = 0; a < in_n_azi; a++) {
5,998✔
248
    in_azimuthal[a] = (a + 0.5) * dangle - PI;
3,013✔
249
  }
250

251
  // Finally use this data to initialize the MGXS Object
252
  init(in_name, in_awr, in_kTs, in_fissionable, in_scatter_format,
2,985✔
253
    in_is_isotropic, in_polar, in_azimuthal);
254
}
2,985✔
255

256
//==============================================================================
257

258
Mgxs::Mgxs(
2,985✔
259
  hid_t xs_id, const vector<double>& temperature, int num_group, int num_delay)
2,985✔
260
  : num_groups(num_group), num_delayed_groups(num_delay)
2,985✔
261
{
262
  // Call generic data gathering routine (will populate the metadata)
263
  int order_data;
264
  vector<int> temps_to_read;
2,985✔
265
  metadata_from_hdf5(xs_id, temperature, temps_to_read, order_data);
2,985✔
266

267
  // Set number of energy and delayed groups
268
  AngleDistributionType final_scatter_format = scatter_format;
2,985✔
269
  if (settings::legendre_to_tabular) {
2,985✔
270
    if (scatter_format == AngleDistributionType::LEGENDRE)
2,761✔
271
      final_scatter_format = AngleDistributionType::TABULAR;
2,677✔
272
  }
273

274
  // Load the more specific XsData information
275
  for (int t = 0; t < temps_to_read.size(); t++) {
6,110✔
276
    xs[t] = XsData(fissionable, final_scatter_format, n_pol, n_azi, num_groups,
6,250✔
277
      num_delayed_groups);
6,250✔
278
    // Get the temperature as a string and then open the HDF5 group
279
    std::string temp_str = std::to_string(temps_to_read[t]) + "K";
3,125✔
280
    hid_t xsdata_grp = open_group(xs_id, temp_str.c_str());
3,125✔
281

282
    xs[t].from_hdf5(xsdata_grp, fissionable, scatter_format,
3,125✔
283
      final_scatter_format, order_data, is_isotropic, n_pol, n_azi);
3,125✔
284
    close_group(xsdata_grp);
3,125✔
285

286
  } // end temperature loop
3,125✔
287

288
  // Make sure the scattering format is updated to the final case
289
  scatter_format = final_scatter_format;
2,985✔
290
}
2,985✔
291

292
//==============================================================================
293

294
Mgxs::Mgxs(const std::string& in_name, const vector<double>& mat_kTs,
2,607✔
295
  const vector<Mgxs*>& micros, const vector<double>& atom_densities,
296
  int num_group, int num_delay)
2,607✔
297
  : num_groups(num_group), num_delayed_groups(num_delay)
2,607✔
298
{
299
  // Get the minimum data needed to initialize:
300
  // Dont need awr, but lets just initialize it anyways
301
  double in_awr = -1.;
2,607✔
302
  // start with the assumption it is not fissionable and set
303
  // the fissionable status if we learn differently
304
  bool in_fissionable = false;
2,607✔
305
  for (int m = 0; m < micros.size(); m++) {
5,550✔
306
    if (micros[m]->fissionable)
2,943✔
307
      in_fissionable = true;
1,046✔
308
  }
309
  // Force all of the following data to be the same; these will be verified
310
  // to be true later
311
  AngleDistributionType in_scatter_format = micros[0]->scatter_format;
2,607✔
312
  bool in_is_isotropic = micros[0]->is_isotropic;
2,607✔
313
  vector<double> in_polar = micros[0]->polar;
2,607✔
314
  vector<double> in_azimuthal = micros[0]->azimuthal;
2,607✔
315

316
  init(in_name, in_awr, mat_kTs, in_fissionable, in_scatter_format,
2,607✔
317
    in_is_isotropic, in_polar, in_azimuthal);
318

319
  // Create the xs data for each temperature
320
  for (int t = 0; t < mat_kTs.size(); t++) {
5,242✔
321
    xs[t] = XsData(in_fissionable, in_scatter_format, in_polar.size(),
5,270✔
322
      in_azimuthal.size(), num_groups, num_delayed_groups);
5,270✔
323

324
    // Find the right temperature index to use
325
    double temp_desired = mat_kTs[t];
2,635✔
326

327
    // Create the list of temperature indices and interpolation factors for
328
    // each microscopic data at the material temperature
329
    vector<int> micro_t(micros.size(), 0);
2,635✔
330
    vector<double> micro_t_interp(micros.size(), 0.);
2,635✔
331
    for (int m = 0; m < micros.size(); m++) {
5,606✔
332
      switch (settings::temperature_method) {
2,971!
333
      case TemperatureMethod::NEAREST: {
2,803✔
334
        micro_t[m] = tensor::abs(micros[m]->kTs - temp_desired).argmin();
2,803✔
335
        auto temp_actual = micros[m]->kTs[micro_t[m]];
2,803✔
336

337
        if (std::abs(temp_actual - temp_desired) >=
2,803✔
338
            K_BOLTZMANN * settings::temperature_tolerance) {
2,803!
339
          fatal_error(fmt::format("MGXS Library does not contain cross section "
×
340
                                  "for {} at or near {} K.",
341
            name, std::round(temp_desired / K_BOLTZMANN)));
×
342
        }
343
      } break;
2,803✔
344
      case TemperatureMethod::INTERPOLATION:
168✔
345
        // Get a list of bounding temperatures for each actual temperature
346
        // present in the model
347
        for (int k = 0; k < micros[m]->kTs.shape(0) - 1; k++) {
336✔
348
          if ((micros[m]->kTs[k] <= temp_desired) &&
336!
349
              (temp_desired < micros[m]->kTs[k + 1])) {
168!
350
            micro_t[m] = k;
168✔
351
            if (k == 0) {
168!
352
              micro_t_interp[m] = (temp_desired - micros[m]->kTs[k]) /
336✔
353
                                  (micros[m]->kTs[k + 1] - micros[m]->kTs[k]);
168✔
354
            } else {
355
              micro_t_interp[m] = 1.;
×
356
            }
357
          }
358
        }
359
      } // end switch
360
    }   // end microscopic temperature loop
361

362
    // Now combine the microscopic data at each relevant temperature
363
    // We will do this by treating the multiple temperatures of a nuclide as
364
    // a different nuclide. Mathematically this just means the temperature
365
    // interpolant is included in the number density.
366
    // These interpolants are contained within interpolant.
367
    vector<double> interpolant;    // the interpolant for the Mgxs
5,270✔
368
    vector<int> temp_indices;      // the temperature index for each Mgxs
5,270✔
369
    vector<Mgxs*> mgxs_to_combine; // The Mgxs to combine
5,270✔
370
    // Now go through and build the above vectors so that we can use them to
371
    // combine the data. We will step through each microscopic data and
372
    // add in its lower and upper temperature points
373
    for (int m = 0; m < micros.size(); m++) {
5,606✔
374
      if (settings::temperature_method == TemperatureMethod::NEAREST) {
2,971✔
375
        // Nearest interpolation only has one temperature point per isotope
376
        // and so we dont need to include a temperature interpolant in
377
        // the interpolant vector
378
        interpolant.push_back(atom_densities[m]);
2,803✔
379
        temp_indices.push_back(micro_t[m]);
2,803✔
380
        mgxs_to_combine.push_back(micros[m]);
2,803✔
381
      } else {
382
        // This will be an interpolation between two points so get both these
383
        // points
384
        // Start with the low point
385
        interpolant.push_back((1. - micro_t_interp[m]) * atom_densities[m]);
168✔
386
        temp_indices.push_back(micro_t[m]);
168✔
387
        mgxs_to_combine.push_back(micros[m]);
168✔
388
        // The higher point
389
        interpolant.push_back((micro_t_interp[m]) * atom_densities[m]);
168✔
390
        temp_indices.push_back(micro_t[m] + 1);
168✔
391
        mgxs_to_combine.push_back(micros[m]);
168✔
392
      }
393
    }
394

395
    // And finally, combine the data
396
    combine(mgxs_to_combine, interpolant, temp_indices, t);
2,635✔
397
  } // end temperature (t) loop
2,635✔
398
}
2,607✔
399

400
//==============================================================================
401

402
void Mgxs::combine(const vector<Mgxs*>& micros, const vector<double>& scalars,
2,635✔
403
  const vector<int>& micro_ts, int this_t)
404
{
405
  // Build the vector of pointers to the xs objects within micros
406
  vector<XsData*> those_xs(micros.size());
2,635✔
407
  for (int i = 0; i < micros.size(); i++) {
5,774✔
408
    those_xs[i] = &(micros[i]->xs[micro_ts[i]]);
3,139✔
409
  }
410

411
  xs[this_t].combine(those_xs, scalars);
2,635✔
412
}
2,635✔
413

414
//==============================================================================
415

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

519
//==============================================================================
520

521
void Mgxs::sample_fission_energy(
100,990,850✔
522
  int gin, int& dg, int& gout, uint64_t* seed, int t, int a)
523
{
524
  XsData* xs_t = &xs[t];
100,990,850✔
525
  double nu_fission = xs_t->nu_fission(a, gin);
100,990,850✔
526

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

530
  // sample random numbers
531
  double xi_pd = prn(seed) * nu_fission;
100,990,850✔
532
  double xi_gout = prn(seed);
100,990,850✔
533

534
  // Select whether the neutron is prompt or delayed
535
  if (xi_pd <= prob_prompt) {
100,990,850✔
536
    // the neutron is prompt
537

538
    // set the delayed group for the particle to be -1, indicating prompt
539
    dg = -1;
100,989,480✔
540

541
    // sample the outgoing energy group
542
    double prob_gout = 0.;
100,989,480✔
543
    for (gout = 0; gout < num_groups; ++gout) {
101,036,500!
544
      prob_gout += xs_t->chi_prompt(a, gin, gout);
101,036,500✔
545
      if (xi_gout < prob_gout)
101,036,500✔
546
        break;
100,989,480✔
547
    }
548

549
  } else {
550
    // the neutron is delayed
551

552
    // get the delayed group
553
    for (dg = 0; dg < num_delayed_groups; ++dg) {
3,450!
554
      prob_prompt += xs_t->delayed_nu_fission(a, dg, gin);
3,450✔
555
      if (xi_pd < prob_prompt)
3,450✔
556
        break;
1,370✔
557
    }
558

559
    // adjust dg in case of round-off error
560
    dg = std::min(dg, num_delayed_groups - 1);
1,370✔
561

562
    // sample the outgoing energy group
563
    double prob_gout = 0.;
1,370✔
564
    for (gout = 0; gout < num_groups; ++gout) {
1,370!
565
      prob_gout += xs_t->chi_delayed(a, dg, gin, gout);
1,370✔
566
      if (xi_gout < prob_gout)
1,370!
567
        break;
1,370✔
568
    }
569
  }
570
}
100,990,850✔
571

572
//==============================================================================
573

574
void Mgxs::sample_scatter(
1,517,650,640✔
575
  int gin, int& gout, double& mu, double& wgt, uint64_t* seed, int t, int a)
576
{
577
  // Sample the data
578
  xs[t].scatter[a]->sample(gin, gout, mu, wgt, seed);
1,517,650,640✔
579
}
1,517,650,640✔
580

581
//==============================================================================
582

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

612
//==============================================================================
613

614
bool Mgxs::equiv(const Mgxs& that)
×
615
{
616
  return (
617
    (num_delayed_groups == that.num_delayed_groups) &&
×
618
    (num_groups == that.num_groups) && (n_pol == that.n_pol) &&
×
619
    (n_azi == that.n_azi) &&
×
620
    (std::equal(polar.begin(), polar.end(), that.polar.begin())) &&
×
621
    (std::equal(azimuthal.begin(), azimuthal.end(), that.azimuthal.begin())) &&
×
622
    (scatter_format == that.scatter_format));
×
623
}
624

625
//==============================================================================
626

627
int Mgxs::get_temperature_index(double sqrtkT) const
114,575,513✔
628
{
629
  return tensor::abs(kTs - sqrtkT * sqrtkT).argmin();
114,575,513✔
630
}
631

632
//==============================================================================
633

634
void Mgxs::set_temperature_index(Particle& p)
103,186,300✔
635
{
636
  p.mg_xs_cache().t = get_temperature_index(p.sqrtkT());
103,186,300✔
637
  p.mg_xs_cache().sqrtkT = p.sqrtkT();
103,186,300✔
638
}
103,186,300✔
639

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

642
int Mgxs::get_angle_index(const Direction& u) const
1,940,734,430✔
643
{
644
  if (is_isotropic) {
1,940,734,430✔
645
    return 0;
1,940,694,210✔
646
  } else {
647
    // convert direction to polar and azimuthal angles
648
    double my_pol = std::acos(u.z);
40,220✔
649
    double my_azi = std::atan2(u.y, u.x);
40,220✔
650

651
    // Find the location, assuming equal-bin angles
652
    double delta_angle = PI / n_pol;
40,220✔
653
    int p = std::floor(my_pol / delta_angle);
40,220✔
654
    delta_angle = 2. * PI / n_azi;
40,220✔
655
    int a = std::floor((my_azi + PI) / delta_angle);
40,220✔
656

657
    return n_azi * p + a;
40,220✔
658
  }
659
}
660

661
//==============================================================================
662

663
void Mgxs::set_angle_index(Particle& p)
1,876,306,740✔
664
{
665
  // See if we need to find the new index
666
  if (!is_isotropic) {
1,876,306,740✔
667
    p.mg_xs_cache().a = get_angle_index(p.u_local());
20,110✔
668
    p.mg_xs_cache().u = p.u_local();
20,110✔
669
  }
670
}
1,876,306,740✔
671

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