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

openmc-dev / openmc / 17448060282

03 Sep 2025 10:50PM UTC coverage: 85.19% (-0.02%) from 85.209%
17448060282

Pull #3546

github

web-flow
Merge d2b204976 into 591856472
Pull Request #3546: Add distributed cell density multipliers

148 of 187 new or added lines in 12 files covered. (79.14%)

159 existing lines in 8 files now uncovered.

53075 of 62302 relevant lines covered (85.19%)

40233512.28 hits per line

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

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

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

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

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

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

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

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

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

117
      double temp_actual = temps_available[i_closest];
2,640✔
118
      if (std::fabs(temp_actual - T) < settings::temperature_tolerance) {
2,640✔
119
        if (std::find(temps_to_read.begin(), temps_to_read.end(),
2,640✔
120
              std::round(temp_actual)) == temps_to_read.end()) {
5,280✔
121
          temps_to_read.push_back(std::round(temp_actual));
2,640✔
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.",
129
          in_name, std::round(T), concatenate(temps_available)));
×
130
      }
131
    }
132
    break;
2,691✔
133

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

310
Mgxs::Mgxs(const std::string& in_name, const vector<double>& mat_kTs,
2,436✔
311
  const vector<Mgxs*>& micros, const vector<double>& atom_densities,
312
  int num_group, int num_delay)
2,436✔
313
  : num_groups(num_group), num_delayed_groups(num_delay)
2,436✔
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,436✔
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,436✔
321
  for (int m = 0; m < micros.size(); m++) {
5,280✔
322
    if (micros[m]->fissionable)
2,844✔
323
      in_fissionable = true;
1,054✔
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,436✔
328
  bool in_is_isotropic = micros[0]->is_isotropic;
2,436✔
329
  vector<double> in_polar = micros[0]->polar;
2,436✔
330
  vector<double> in_azimuthal = micros[0]->azimuthal;
2,436✔
331

332
  init(in_name, in_awr, mat_kTs, in_fissionable, in_scatter_format,
2,436✔
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,889✔
337
    xs[t] = XsData(in_fissionable, in_scatter_format, in_polar.size(),
4,906✔
338
      in_azimuthal.size(), num_groups, num_delayed_groups);
4,906✔
339

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

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

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

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

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

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

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

432
double Mgxs::get_xs(MgxsType xstype, int gin, const int* gout, const double* mu,
2,147,483,647✔
433
  const int* dg, int t, int a)
434
{
435
  XsData* xs_t = &xs[t];
2,147,483,647✔
436
  double val;
437
  switch (xstype) {
2,147,483,647✔
438
  case MgxsType::TOTAL:
22,509,315✔
439
    val = xs_t->total(a, gin);
22,509,315✔
440
    break;
22,509,315✔
441
  case MgxsType::NU_FISSION:
11,115,481✔
442
    val = fissionable ? xs_t->nu_fission(a, gin) : 0.;
11,115,481✔
443
    break;
11,115,481✔
444
  case MgxsType::ABSORPTION:
6,348,972✔
445
    val = xs_t->absorption(a, gin);
6,348,972✔
446
    ;
447
    break;
6,348,972✔
448
  case MgxsType::FISSION:
12,176,017✔
449
    val = fissionable ? xs_t->fission(a, gin) : 0.;
12,176,017✔
450
    break;
12,176,017✔
451
  case MgxsType::KAPPA_FISSION:
11,373,144✔
452
    val = fissionable ? xs_t->kappa_fission(a, gin) : 0.;
11,373,144✔
453
    break;
11,373,144✔
454
  case MgxsType::NU_SCATTER:
20,278,181✔
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);
20,278,181✔
459
    break;
20,278,181✔
460
  case MgxsType::PROMPT_NU_FISSION:
11,108,184✔
461
    val = fissionable ? xs_t->prompt_nu_fission(a, gin) : 0.;
11,108,184✔
462
    break;
11,108,184✔
463
  case MgxsType::DELAYED_NU_FISSION:
77,757,288✔
464
    if (fissionable) {
77,757,288✔
465
      if (dg != nullptr) {
77,757,288✔
466
        val = xs_t->delayed_nu_fission(a, *dg, gin);
66,649,104✔
467
      } else {
468
        val = 0.;
11,108,184✔
469
        for (int d = 0; d < xs_t->delayed_nu_fission.shape()[1]; d++) {
77,757,288✔
470
          val += xs_t->delayed_nu_fission(a, d, gin);
66,649,104✔
471
        }
472
      }
473
    } else {
474
      val = 0.;
×
475
    }
476
    break;
77,757,288✔
477
  case MgxsType::CHI_PROMPT:
7,297✔
478
    if (fissionable) {
7,297✔
479
      if (gout != nullptr) {
2,380✔
480
        val = xs_t->chi_prompt(a, gin, *gout);
2,380✔
481
      } else {
482
        // provide an outgoing group-wise sum
483
        val = 0.;
×
484
        for (int g = 0; g < xs_t->chi_prompt.shape()[2]; g++) {
×
485
          val += xs_t->chi_prompt(a, gin, g);
×
486
        }
487
      }
488
    } else {
489
      val = 0.;
4,917✔
490
    }
491
    break;
7,297✔
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 {
498
          val = xs_t->chi_delayed(a, 0, gin, *gout);
×
499
        }
500
      } else {
501
        if (dg != nullptr) {
×
502
          val = 0.;
×
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 {
507
          val = 0.;
×
508
          for (int g = 0; g < xs_t->delayed_nu_fission.shape()[2]; g++) {
×
509
            for (int d = 0; d < xs_t->delayed_nu_fission.shape()[3]; d++) {
×
510
              val += xs_t->delayed_nu_fission(a, d, gin, g);
×
511
            }
512
          }
513
        }
514
      }
515
    } else {
516
      val = 0.;
×
517
    }
518
    break;
×
519
  case MgxsType::INVERSE_VELOCITY:
2,147,483,647✔
520
    val = xs_t->inverse_velocity(a, gin);
2,147,483,647✔
521
    break;
2,147,483,647✔
522
  case MgxsType::DECAY_RATE:
66,651,024✔
523
    if (dg != nullptr) {
66,651,024✔
524
      val = xs_t->decay_rate(a, *dg);
66,651,024✔
525
    } else {
526
      val = xs_t->decay_rate(a, 0);
×
527
    }
528
    break;
66,651,024✔
529
  default:
×
530
    val = 0.;
×
531
  }
532
  return val;
2,147,483,647✔
533
}
534

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

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

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

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

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

554
    // set the delayed group for the particle to be -1, indicating prompt
555
    dg = -1;
121,149,396✔
556

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

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

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

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

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

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

590
void Mgxs::sample_scatter(
1,819,766,472✔
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,819,766,472✔
595
}
1,819,766,472✔
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);
113,566,738✔
604
    set_angle_index(p);
113,566,738✔
605
    p.mg_xs_cache().material = p.material();
113,566,738✔
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,136,630,470✔
610
      set_temperature_index(p);
2,451,958✔
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,136,630,470✔
615
      set_angle_index(p);
2,136,630,470✔
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()) * p.rho_mult();
2,147,483,647✔
621
  p.macro_xs().absorption =
2,147,483,647✔
622
    xs[temperature].absorption(angle, p.g()) * p.rho_mult();
2,147,483,647✔
623
  p.macro_xs().nu_fission =
2,147,483,647✔
624
    fissionable ? xs[temperature].nu_fission(angle, p.g()) * p.rho_mult() : 0.;
2,147,483,647✔
625
}
2,147,483,647✔
626

627
//==============================================================================
628

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

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

642
int Mgxs::get_temperature_index(double sqrtkT) const
127,118,972✔
643
{
644
  return xt::argmin(xt::abs(kTs - sqrtkT * sqrtkT))[0];
127,118,972✔
645
}
646

647
//==============================================================================
648

649
void Mgxs::set_temperature_index(Particle& p)
116,018,696✔
650
{
651
  p.mg_xs_cache().t = get_temperature_index(p.sqrtkT());
116,018,696✔
652
  p.mg_xs_cache().sqrtkT = p.sqrtkT();
116,018,696✔
653
}
116,018,696✔
654

655
//==============================================================================
656

657
int Mgxs::get_angle_index(const Direction& u) const
2,147,483,647✔
658
{
659
  if (is_isotropic) {
2,147,483,647✔
660
    return 0;
2,147,483,647✔
661
  } else {
662
    // convert direction to polar and azimuthal angles
663
    double my_pol = std::acos(u.z);
48,408✔
664
    double my_azi = std::atan2(u.y, u.x);
48,408✔
665

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

672
    return n_azi * p + a;
48,408✔
673
  }
674
}
675

676
//==============================================================================
677

678
void Mgxs::set_angle_index(Particle& p)
2,147,483,647✔
679
{
680
  // See if we need to find the new index
681
  if (!is_isotropic) {
2,147,483,647✔
682
    p.mg_xs_cache().a = get_angle_index(p.u_local());
24,204✔
683
    p.mg_xs_cache().u = p.u_local();
24,204✔
684
  }
685
}
2,147,483,647✔
686

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