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

openmc-dev / openmc / 19102841639

05 Nov 2025 01:01PM UTC coverage: 82.019% (-3.1%) from 85.155%
19102841639

Pull #3252

github

web-flow
Merge 3c71decac into bd76fc056
Pull Request #3252: Adding vtkhdf option to write vtk data

16722 of 23233 branches covered (71.98%)

Branch coverage included in aggregate %.

61 of 66 new or added lines in 1 file covered. (92.42%)

3177 existing lines in 103 files now uncovered.

54247 of 63294 relevant lines covered (85.71%)

42990146.99 hits per line

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

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

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

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

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

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

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

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

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

117
      double temp_actual = temps_available[i_closest];
2,531✔
118
      if (std::fabs(temp_actual - T) < settings::temperature_tolerance) {
2,531!
119
        if (std::find(temps_to_read.begin(), temps_to_read.end(),
2,531✔
120
              std::round(temp_actual)) == temps_to_read.end()) {
5,062!
121
          temps_to_read.push_back(std::round(temp_actual));
2,531✔
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,579✔
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!
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]) &&
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,771✔
159

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

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

171
  // Load the remaining metadata
172
  AngleDistributionType in_scatter_format;
173
  if (attribute_exists(xs_id, "scatter_format")) {
2,771!
174
    std::string temp_str;
2,771✔
175
    read_attribute(xs_id, "scatter_format", temp_str);
2,771✔
176
    to_lower(strtrim(temp_str));
2,771✔
177
    if (temp_str.compare(0, 8, "legendre") == 0) {
2,771✔
178
      in_scatter_format = AngleDistributionType::LEGENDRE;
2,643✔
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 {
184
      fatal_error("Invalid scatter_format option!");
×
185
    }
186
  } else {
2,771✔
187
    in_scatter_format = AngleDistributionType::LEGENDRE;
×
188
  }
189

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

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

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

239
  if (!in_is_isotropic) {
2,771✔
240
    if (attribute_exists(xs_id, "num_polar")) {
32!
241
      read_attr_int(xs_id, "num_polar", &in_n_pol);
32✔
242
    } else {
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 {
248
      fatal_error("num_azimuthal must be provided!");
×
249
    }
250
  } else {
251
    in_n_pol = 1;
2,739✔
252
    in_n_azi = 1;
2,739✔
253
  }
254

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

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

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

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

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

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

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

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

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

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

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

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

353
        if (std::abs(temp_actual - temp_desired) >=
2,547✔
354
            K_BOLTZMANN * settings::temperature_tolerance) {
2,547!
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,547✔
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 {
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,710✔
384
    vector<int> temp_indices;      // the temperature index for each Mgxs
4,710✔
385
    vector<Mgxs*> mgxs_to_combine; // The Mgxs to combine
4,710✔
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,094✔
390
      if (settings::temperature_method == TemperatureMethod::NEAREST) {
2,739✔
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,547✔
395
        temp_indices.push_back(micro_t[m]);
2,547✔
396
        mgxs_to_combine.push_back(micros[m]);
2,547✔
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,355✔
413
  } // end temperature (t) loop
2,355✔
414
}
2,339✔
415

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

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

427
  xs[this_t].combine(those_xs, scalars);
2,355✔
428
}
2,355✔
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:
20,628,684✔
439
    val = xs_t->total(a, gin);
20,628,684✔
440
    break;
20,628,684✔
441
  case MgxsType::NU_FISSION:
10,195,094✔
442
    val = fissionable ? xs_t->nu_fission(a, gin) : 0.;
10,195,094✔
443
    break;
10,195,094✔
444
  case MgxsType::ABSORPTION:
5,825,710✔
445
    val = xs_t->absorption(a, gin);
5,825,710✔
446
    ;
447
    break;
5,825,710✔
448
  case MgxsType::FISSION:
11,167,934✔
449
    val = fissionable ? xs_t->fission(a, gin) : 0.;
11,167,934✔
450
    break;
11,167,934✔
451
  case MgxsType::KAPPA_FISSION:
10,432,400✔
452
    val = fissionable ? xs_t->kappa_fission(a, gin) : 0.;
10,432,400!
453
    break;
10,432,400✔
454
  case MgxsType::NU_SCATTER:
18,702,804✔
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,702,804✔
459
    break;
18,702,804✔
460
  case MgxsType::PROMPT_NU_FISSION:
10,188,596✔
461
    val = fissionable ? xs_t->prompt_nu_fission(a, gin) : 0.;
10,188,596!
462
    break;
10,188,596✔
463
  case MgxsType::DELAYED_NU_FISSION:
71,320,172✔
464
    if (fissionable) {
71,320,172!
465
      if (dg != nullptr) {
71,320,172✔
466
        val = xs_t->delayed_nu_fission(a, *dg, gin);
61,131,576✔
467
      } else {
468
        val = 0.;
10,188,596✔
469
        for (int d = 0; d < xs_t->delayed_nu_fission.shape()[1]; d++) {
71,320,172✔
470
          val += xs_t->delayed_nu_fission(a, d, gin);
61,131,576✔
471
        }
472
      }
473
    } else {
474
      val = 0.;
×
475
    }
476
    break;
71,320,172✔
477
  case MgxsType::CHI_PROMPT:
6,498✔
478
    if (fissionable) {
6,498✔
479
      if (gout != nullptr) {
2,128!
480
        val = xs_t->chi_prompt(a, gin, *gout);
2,128✔
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,370✔
490
    }
491
    break;
6,498✔
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,079,471,196✔
520
    val = xs_t->inverse_velocity(a, gin);
2,079,471,196✔
521
    break;
2,079,471,196✔
522
  case MgxsType::DECAY_RATE:
61,134,799✔
523
    if (dg != nullptr) {
61,134,799!
524
      val = xs_t->decay_rate(a, *dg);
61,134,799✔
525
    } else {
526
      val = xs_t->decay_rate(a, 0);
×
527
    }
528
    break;
61,134,799✔
529
  default:
×
530
    val = 0.;
×
531
  }
532
  return val;
2,147,483,647✔
533
}
534

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

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

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

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

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

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

557
    // sample the outgoing energy group
558
    double prob_gout = 0.;
111,088,428✔
559
    for (gout = 0; gout < num_groups; ++gout) {
111,140,150!
560
      prob_gout += xs_t->chi_prompt(a, gin, gout);
111,140,150✔
561
      if (xi_gout < prob_gout)
111,140,150✔
562
        break;
111,088,428✔
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,795!
570
      prob_prompt += xs_t->delayed_nu_fission(a, dg, gin);
3,795✔
571
      if (xi_pd < prob_prompt)
3,795✔
572
        break;
1,507✔
573
    }
574

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

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

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

590
void Mgxs::sample_scatter(
1,669,415,704✔
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,415,704✔
595
}
1,669,415,704✔
596

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

599
void Mgxs::calculate_xs(Particle& p)
2,063,937,414✔
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,063,937,414✔
603
    set_temperature_index(p);
112,278,623✔
604
    set_angle_index(p);
112,278,623✔
605
    p.mg_xs_cache().material = p.material();
112,278,623✔
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,951,658,791✔
610
      set_temperature_index(p);
2,227,056✔
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,951,658,791!
615
      set_angle_index(p);
1,951,658,791✔
616
    }
617
  }
618
  int temperature = p.mg_xs_cache().t;
2,063,937,414✔
619
  int angle = p.mg_xs_cache().a;
2,063,937,414✔
620
  p.macro_xs().total = xs[temperature].total(angle, p.g()) * p.density_mult();
2,063,937,414✔
621
  p.macro_xs().absorption =
2,147,483,647✔
622
    xs[temperature].absorption(angle, p.g()) * p.density_mult();
2,063,937,414✔
623
  p.macro_xs().nu_fission =
2,063,937,414✔
624
    fissionable ? xs[temperature].nu_fission(angle, p.g()) * p.density_mult()
2,063,937,414✔
625
                : 0.;
626
}
2,063,937,414✔
627

628
//==============================================================================
629

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

641
//==============================================================================
642

643
int Mgxs::get_temperature_index(double sqrtkT) const
124,689,094✔
644
{
645
  return xt::argmin(xt::abs(kTs - sqrtkT * sqrtkT))[0];
124,689,094✔
646
}
647

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

650
void Mgxs::set_temperature_index(Particle& p)
114,505,679✔
651
{
652
  p.mg_xs_cache().t = get_temperature_index(p.sqrtkT());
114,505,679✔
653
  p.mg_xs_cache().sqrtkT = p.sqrtkT();
114,505,679✔
654
}
114,505,679✔
655

656
//==============================================================================
657

658
int Mgxs::get_angle_index(const Direction& u) const
2,134,807,873✔
659
{
660
  if (is_isotropic) {
2,134,807,873✔
661
    return 0;
2,134,763,631✔
662
  } else {
663
    // convert direction to polar and azimuthal angles
664
    double my_pol = std::acos(u.z);
44,242✔
665
    double my_azi = std::atan2(u.y, u.x);
44,242✔
666

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

673
    return n_azi * p + a;
44,242✔
674
  }
675
}
676

677
//==============================================================================
678

679
void Mgxs::set_angle_index(Particle& p)
2,063,937,414✔
680
{
681
  // See if we need to find the new index
682
  if (!is_isotropic) {
2,063,937,414✔
683
    p.mg_xs_cache().a = get_angle_index(p.u_local());
22,121✔
684
    p.mg_xs_cache().u = p.u_local();
22,121✔
685
  }
686
}
2,063,937,414✔
687

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

© 2025 Coveralls, Inc