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

openmc-dev / openmc / 40712674926

28 Oct 2026 03:06PM UTC coverage: 82.137% (+0.1%) from 82.008%
40712674926

Pull #4141

github

web-flow
Merge 5a8471106 into b1bb3ec5f
Pull Request #4141: Adding multigroup photon transport capability in MC mode

20683 of 29457 branches covered (70.21%)

Branch coverage included in aggregate %.

194 of 214 new or added lines in 10 files covered. (90.65%)

1 existing line in 1 file now uncovered.

63845 of 73454 relevant lines covered (86.92%)

39742130.06 hits per line

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

96.16
/src/xsdata.cpp
1
#include "openmc/xsdata.h"
2

3
#include <algorithm>
4
#include <cmath>
5
#include <cstdlib>
6
#include <numeric>
7

8
#include "openmc/tensor.h"
9

10
#include "openmc/constants.h"
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

17
namespace openmc {
18

19
//==============================================================================
20
// XsData class methods
21
//==============================================================================
22

23
XsData::XsData(bool fissionable, AngleDistributionType scatter_format,
5,075 ✔
24
  int n_pol, int n_azi, size_t n_groups, size_t n_d_groups)
5,075 ✔
25
  : n_g_(n_groups), n_dg_(n_d_groups)
5,075 ✔
26
{
27
  size_t n_ang = n_pol * n_azi;
5,075 ✔
28

29
  // check to make sure scatter format is OK before we allocate
30
  if (scatter_format != AngleDistributionType::HISTOGRAM &&
5,075 ✔
31
      scatter_format != AngleDistributionType::TABULAR &&
5,075 !
32
      scatter_format != AngleDistributionType::LEGENDRE) {
33
    fatal_error("Invalid scatter_format!");
×
34
  }
35
  // allocate all [temperature][angle][in group] quantities
36
  vector<size_t> shape {n_ang, n_g_};
5,075 ✔
37
  total = tensor::zeros<double>(shape);
5,075 ✔
38
  absorption = tensor::zeros<double>(shape);
5,075 ✔
39
  inverse_velocity = tensor::zeros<double>(shape);
5,075 ✔
40
  if (fissionable) {
5,075 ✔
41
    fission = tensor::zeros<double>(shape);
1,535 ✔
42
    nu_fission = tensor::zeros<double>(shape);
1,535 ✔
43
    prompt_nu_fission = tensor::zeros<double>(shape);
1,535 ✔
44
    kappa_fission = tensor::zeros<double>(shape);
3,070 ✔
45
  }
46

47
  // allocate decay_rate; [temperature][angle][delayed group]
48
  shape[1] = n_dg_;
5,075 ✔
49
  decay_rate = tensor::zeros<double>(shape);
5,075 ✔
50

51
  if (fissionable) {
5,075 ✔
52
    shape = {n_ang, n_dg_, n_g_};
1,535 ✔
53
    // allocate delayed_nu_fission; [temperature][angle][delay group][in group]
54
    delayed_nu_fission = tensor::zeros<double>(shape);
1,535 ✔
55

56
    // chi_prompt; [temperature][angle][in group][out group]
57
    shape = {n_ang, n_g_, n_g_};
1,535 ✔
58
    chi_prompt = tensor::zeros<double>(shape);
1,535 ✔
59

60
    // chi_delayed; [temperature][angle][delay group][in group][out group]
61
    shape = {n_ang, n_dg_, n_g_, n_g_};
1,535 ✔
62
    chi_delayed = tensor::zeros<double>(shape);
3,070 ✔
63
  }
64

65
  for (int a = 0; a < n_ang; a++) {
10,258 ✔
66
    if (scatter_format == AngleDistributionType::HISTOGRAM) {
5,183 ✔
67
      scatter.emplace_back(new ScattDataHistogram);
72 ✔
68
    } else if (scatter_format == AngleDistributionType::TABULAR) {
5,111 ✔
69
      scatter.emplace_back(new ScattDataTabular);
4,787 ✔
70
    } else if (scatter_format == AngleDistributionType::LEGENDRE) {
324 ✔
71
      scatter.emplace_back(new ScattDataLegendre);
324 ✔
72
    }
73
  }
74
}
5,075 ✔
75

76
//==============================================================================
77

78
void XsData::from_hdf5(hid_t xsdata_grp, bool fissionable,
2,698 ✔
79
  AngleDistributionType scatter_format,
80
  AngleDistributionType final_scatter_format, int order_data, bool is_isotropic,
81
  int n_pol, int n_azi)
82
{
83
  // Reconstruct the dimension information so it doesn't need to be passed
84
  size_t n_ang = n_pol * n_azi;
2,698 ✔
85
  size_t energy_groups = total.shape(1);
2,698 !
86

87
  // Set the fissionable-specific data
88
  if (fissionable) {
2,698 ✔
89
    fission_from_hdf5(xsdata_grp, n_ang, is_isotropic);
817 ✔
90
  }
91
  // Get the non-fission-specific data
92
  read_nd_tensor(xsdata_grp, "decay-rate", decay_rate);
2,698 ✔
93
  read_nd_tensor(xsdata_grp, "absorption", absorption, true);
2,698 ✔
94
  read_nd_tensor(xsdata_grp, "inverse-velocity", inverse_velocity);
2,698 ✔
95

96
  // Get scattering data
97
  scatter_from_hdf5(
2,698 ✔
98
    xsdata_grp, n_ang, scatter_format, final_scatter_format, order_data);
99

100
  // Replace zero absorption values with a small number to avoid
101
  // division by zero in tally methods
102
  for (size_t i = 0; i < absorption.size(); i++)
12,929 ✔
103
    if (absorption.data()[i] == 0.0)
10,231 ✔
104
      absorption.data()[i] = 1.e-10;
14 ✔
105

106
  // Get or calculate the total x/s
107
  if (object_exists(xsdata_grp, "total")) {
2,698 !
108
    read_nd_tensor(xsdata_grp, "total", total);
2,698 ✔
109
  } else {
110
    for (size_t a = 0; a < n_ang; a++) {
×
111
      for (size_t gin = 0; gin < energy_groups; gin++) {
×
112
        total(a, gin) = absorption(a, gin) + scatter[a]->scattxs[gin];
×
113
      }
114
    }
115
  }
116

117
  // Replace zero total cross sections with a small number to avoid
118
  // division by zero in tally methods
119
  for (size_t i = 0; i < total.size(); i++)
12,929 ✔
120
    if (total.data()[i] == 0.0)
10,231 ✔
121
      total.data()[i] = 1.e-10;
14 ✔
122

123
  // Photon libraries fold secondary photons into the scatter matrix and store
124
  // no multiplicity. Derive the group-wise one the MC collision game needs.
125
  if (data::mg.particle_type_.is_photon() &&
2,745 ✔
126
      !object_exists(xsdata_grp, "scatter_data/multiplicity_matrix")) {
47 ✔
127
    for (size_t a = 0; a < n_ang; a++) {
24 ✔
128
      for (size_t g = 0; g < energy_groups; g++) {
54 ✔
129
        double production = scatter[a]->scattxs[g];
45 !
130
        if (production <= 0.0)
45 !
NEW
131
          continue;
×
132
        double removal = total(a, g) - absorption(a, g);
45 ✔
133
        if (removal <= 0.0)
45 ✔
134
          fatal_error(fmt::format("Photon group {} produces photons but has "
6 ✔
135
                                  "no scattering to carry them.",
136
            g + 1));
6 !
137
        std::fill(scatter[a]->mult[g].begin(), scatter[a]->mult[g].end(),
39 ✔
138
          production / removal);
39 ✔
139
      }
140
    }
141
  }
142
}
2,692 ✔
143

144
//==============================================================================
145

146
void XsData::fission_vector_beta_from_hdf5(
27 ✔
147
  hid_t xsdata_grp, size_t n_ang, bool is_isotropic)
148
{
149
  // Data is provided as nu-fission and chi with a beta for delayed info
150

151
  // Get chi
152
  tensor::Tensor<double> temp_chi = tensor::zeros<double>({n_ang, n_g_});
27 ✔
153
  read_nd_tensor(xsdata_grp, "chi", temp_chi, true);
27 ✔
154

155
  // Normalize chi so it sums to 1 over outgoing groups for each angle
156
  for (size_t a = 0; a < n_ang; a++) {
54 ✔
157
    tensor::View<double> row = temp_chi.slice(a);
27 ✔
158
    row /= row.sum();
27 ✔
159
  }
27 ✔
160

161
  // Replicate the energy spectrum across all incoming groups — the
162
  // spectrum is independent of the incoming neutron energy
163
  for (size_t a = 0; a < n_ang; a++)
54 ✔
164
    for (size_t gin = 0; gin < n_g_; gin++)
81 ✔
165
      chi_prompt.slice(a, gin) = temp_chi.slice(a);
162 ✔
166

167
  // Same spectrum for delayed neutrons, replicated across delayed groups
168
  for (size_t a = 0; a < n_ang; a++)
54 ✔
169
    for (size_t d = 0; d < n_dg_; d++)
117 ✔
170
      for (size_t gin = 0; gin < n_g_; gin++)
270 ✔
171
        chi_delayed.slice(a, d, gin) = temp_chi.slice(a);
540 ✔
172

173
  // Get nu-fission
174
  tensor::Tensor<double> temp_nufiss = tensor::zeros<double>({n_ang, n_g_});
27 ✔
175
  read_nd_tensor(xsdata_grp, "nu-fission", temp_nufiss, true);
27 ✔
176

177
  // Get beta (strategy will depend upon the number of dimensions in beta)
178
  hid_t beta_dset = open_dataset(xsdata_grp, "beta");
27 ✔
179
  int beta_ndims = dataset_ndims(beta_dset);
27 ✔
180
  close_dataset(beta_dset);
27 ✔
181
  int ndim_target = 1;
27 ✔
182
  if (!is_isotropic)
27 !
183
    ndim_target += 2;
×
184
  if (beta_ndims == ndim_target) {
27 ✔
185
    tensor::Tensor<double> temp_beta = tensor::zeros<double>({n_ang, n_dg_});
18 ✔
186
    read_nd_tensor(xsdata_grp, "beta", temp_beta, true);
18 ✔
187

188
    // prompt_nu_fission = (1 - sum_of_beta) * nu_fission
189
    auto beta_sum = temp_beta.sum(1);
18 ✔
190
    for (size_t a = 0; a < n_ang; a++)
36 ✔
191
      for (size_t g = 0; g < n_g_; g++)
54 ✔
192
        prompt_nu_fission(a, g) = temp_nufiss(a, g) * (1.0 - beta_sum(a));
36 ✔
193

194
    // Delayed nu-fission is the outer product of the delayed neutron
195
    // fraction (beta) and the fission production rate (nu-fission)
196
    for (size_t a = 0; a < n_ang; a++)
36 ✔
197
      for (size_t d = 0; d < n_dg_; d++)
90 ✔
198
        for (size_t g = 0; g < n_g_; g++)
216 ✔
199
          delayed_nu_fission(a, d, g) = temp_beta(a, d) * temp_nufiss(a, g);
144 ✔
200
  } else if (beta_ndims == ndim_target + 1) {
45 !
201
    tensor::Tensor<double> temp_beta =
9 ✔
202
      tensor::zeros<double>({n_ang, n_dg_, n_g_});
9 ✔
203
    read_nd_tensor(xsdata_grp, "beta", temp_beta, true);
9 ✔
204

205
    // prompt_nu_fission = (1 - sum_of_beta) * nu_fission
206
    // Here beta is energy-dependent, so sum over delayed groups (axis 1)
207
    auto beta_sum = temp_beta.sum(1);
9 ✔
208
    for (size_t a = 0; a < n_ang; a++)
18 ✔
209
      for (size_t g = 0; g < n_g_; g++)
27 ✔
210
        prompt_nu_fission(a, g) = temp_nufiss(a, g) * (1.0 - beta_sum(a, g));
18 ✔
211

212
    // Delayed nu-fission: beta is already energy-dependent [n_ang, n_dg, n_g],
213
    // so scale each delayed group's beta by the total nu-fission for that group
214
    for (size_t a = 0; a < n_ang; a++)
18 ✔
215
      for (size_t d = 0; d < n_dg_; d++)
27 ✔
216
        for (size_t g = 0; g < n_g_; g++)
54 ✔
217
          delayed_nu_fission(a, d, g) = temp_beta(a, d, g) * temp_nufiss(a, g);
36 ✔
218
  }
18 ✔
219
}
54 ✔
220

221
void XsData::fission_vector_no_beta_from_hdf5(hid_t xsdata_grp, size_t n_ang)
9 ✔
222
{
223
  // Data is provided separately as prompt + delayed nu-fission and chi
224

225
  // Get chi-prompt
226
  tensor::Tensor<double> temp_chi_p = tensor::zeros<double>({n_ang, n_g_});
9 ✔
227
  read_nd_tensor(xsdata_grp, "chi-prompt", temp_chi_p, true);
9 ✔
228

229
  // Normalize prompt chi so it sums to 1 over outgoing groups for each angle
230
  for (size_t a = 0; a < n_ang; a++) {
18 ✔
231
    tensor::View<double> row = temp_chi_p.slice(a);
9 ✔
232
    row /= row.sum();
9 ✔
233
  }
9 ✔
234

235
  // Get chi-delayed
236
  tensor::Tensor<double> temp_chi_d =
9 ✔
237
    tensor::zeros<double>({n_ang, n_dg_, n_g_});
9 ✔
238
  read_nd_tensor(xsdata_grp, "chi-delayed", temp_chi_d, true);
9 ✔
239

240
  // Normalize delayed chi so it sums to 1 over outgoing groups for each
241
  // angle and delayed group
242
  for (size_t a = 0; a < n_ang; a++)
18 ✔
243
    for (size_t d = 0; d < n_dg_; d++) {
27 ✔
244
      tensor::View<double> row = temp_chi_d.slice(a, d);
18 ✔
245
      row /= row.sum();
18 ✔
246
    }
18 ✔
247

248
  // Replicate the prompt spectrum across all incoming groups
249
  for (size_t a = 0; a < n_ang; a++)
18 ✔
250
    for (size_t gin = 0; gin < n_g_; gin++)
27 ✔
251
      chi_prompt.slice(a, gin) = temp_chi_p.slice(a);
54 ✔
252

253
  // Replicate the delayed spectrum across all incoming groups
254
  for (size_t a = 0; a < n_ang; a++)
18 ✔
255
    for (size_t d = 0; d < n_dg_; d++)
27 ✔
256
      for (size_t gin = 0; gin < n_g_; gin++)
54 ✔
257
        chi_delayed.slice(a, d, gin) = temp_chi_d.slice(a, d);
108 ✔
258

259
  // Get prompt and delayed nu-fission directly
260
  read_nd_tensor(xsdata_grp, "prompt-nu-fission", prompt_nu_fission, true);
9 ✔
261
  read_nd_tensor(xsdata_grp, "delayed-nu-fission", delayed_nu_fission, true);
9 ✔
262
}
18 ✔
263

264
void XsData::fission_vector_no_delayed_from_hdf5(hid_t xsdata_grp, size_t n_ang)
709 ✔
265
{
266
  // No beta is provided and there is no prompt/delay distinction.
267
  // Therefore, the code only considers the data as prompt.
268

269
  // Get chi
270
  tensor::Tensor<double> temp_chi = tensor::zeros<double>({n_ang, n_g_});
709 ✔
271
  read_nd_tensor(xsdata_grp, "chi", temp_chi, true);
709 ✔
272

273
  // Normalize chi so it sums to 1 over outgoing groups for each angle
274
  for (size_t a = 0; a < n_ang; a++) {
1,472 ✔
275
    tensor::View<double> row = temp_chi.slice(a);
763 ✔
276
    row /= row.sum();
763 ✔
277
  }
763 ✔
278

279
  // Replicate the energy spectrum across all incoming groups
280
  for (size_t a = 0; a < n_ang; a++)
1,472 ✔
281
    for (size_t gin = 0; gin < n_g_; gin++)
4,466 ✔
282
      chi_prompt.slice(a, gin) = temp_chi.slice(a);
11,109 ✔
283

284
  // Get nu-fission directly
285
  read_nd_tensor(xsdata_grp, "nu-fission", prompt_nu_fission, true);
709 ✔
286
}
709 ✔
287

288
//==============================================================================
289

290
void XsData::fission_matrix_beta_from_hdf5(
18 ✔
291
  hid_t xsdata_grp, size_t n_ang, bool is_isotropic)
292
{
293
  // Data is provided as nu-fission and chi with a beta for delayed info
294

295
  // Get nu-fission matrix
296
  tensor::Tensor<double> temp_matrix =
18 ✔
297
    tensor::zeros<double>({n_ang, n_g_, n_g_});
18 ✔
298
  read_nd_tensor(xsdata_grp, "nu-fission", temp_matrix, true);
18 ✔
299

300
  // Get beta (strategy will depend upon the number of dimensions in beta)
301
  hid_t beta_dset = open_dataset(xsdata_grp, "beta");
18 ✔
302
  int beta_ndims = dataset_ndims(beta_dset);
18 ✔
303
  close_dataset(beta_dset);
18 ✔
304
  int ndim_target = 1;
18 ✔
305
  if (!is_isotropic)
18 !
306
    ndim_target += 2;
×
307
  if (beta_ndims == ndim_target) {
18 ✔
308
    tensor::Tensor<double> temp_beta = tensor::zeros<double>({n_ang, n_dg_});
9 ✔
309
    read_nd_tensor(xsdata_grp, "beta", temp_beta, true);
9 ✔
310

311
    auto beta_sum = temp_beta.sum(1);
9 ✔
312
    auto matrix_gout_sum = temp_matrix.sum(2);
9 ✔
313

314
    // prompt_nu_fission = sum_gout(matrix) * (1 - beta_total)
315
    for (size_t a = 0; a < n_ang; a++)
18 ✔
316
      for (size_t g = 0; g < n_g_; g++)
27 ✔
317
        prompt_nu_fission(a, g) = matrix_gout_sum(a, g) * (1.0 - beta_sum(a));
18 ✔
318

319
    // chi_prompt = (1 - beta_total) * nu-fission matrix (unnormalized)
320
    for (size_t a = 0; a < n_ang; a++)
18 ✔
321
      for (size_t gin = 0; gin < n_g_; gin++)
27 ✔
322
        for (size_t gout = 0; gout < n_g_; gout++)
54 ✔
323
          chi_prompt(a, gin, gout) =
36 ✔
324
            (1.0 - beta_sum(a)) * temp_matrix(a, gin, gout);
36 ✔
325

326
    // Delayed nu-fission is the outer product of the delayed neutron
327
    // fraction (beta) and the total fission rate summed over outgoing groups
328
    for (size_t a = 0; a < n_ang; a++)
18 ✔
329
      for (size_t d = 0; d < n_dg_; d++)
27 ✔
330
        for (size_t g = 0; g < n_g_; g++)
54 ✔
331
          delayed_nu_fission(a, d, g) = temp_beta(a, d) * matrix_gout_sum(a, g);
36 ✔
332

333
    // chi_delayed = beta * nu-fission matrix, expanded across delayed groups
334
    for (size_t a = 0; a < n_ang; a++)
18 ✔
335
      for (size_t d = 0; d < n_dg_; d++)
27 ✔
336
        for (size_t gin = 0; gin < n_g_; gin++)
54 ✔
337
          for (size_t gout = 0; gout < n_g_; gout++)
108 ✔
338
            chi_delayed(a, d, gin, gout) =
72 ✔
339
              temp_beta(a, d) * temp_matrix(a, gin, gout);
72 ✔
340

341
  } else if (beta_ndims == ndim_target + 1) {
36 !
342
    tensor::Tensor<double> temp_beta =
9 ✔
343
      tensor::zeros<double>({n_ang, n_dg_, n_g_});
9 ✔
344
    read_nd_tensor(xsdata_grp, "beta", temp_beta, true);
9 ✔
345

346
    auto beta_sum = temp_beta.sum(1);
9 ✔
347
    auto matrix_gout_sum = temp_matrix.sum(2);
9 ✔
348

349
    // prompt_nu_fission = sum_gout(matrix) * (1 - beta_total)
350
    // Here beta is energy-dependent, so beta_sum is 2D [n_ang, n_g]
351
    for (size_t a = 0; a < n_ang; a++)
18 ✔
352
      for (size_t g = 0; g < n_g_; g++)
27 ✔
353
        prompt_nu_fission(a, g) =
18 ✔
354
          matrix_gout_sum(a, g) * (1.0 - beta_sum(a, g));
18 ✔
355

356
    // chi_prompt = (1 - beta_sum) * nu-fission matrix (unnormalized)
357
    for (size_t a = 0; a < n_ang; a++)
18 ✔
358
      for (size_t gin = 0; gin < n_g_; gin++)
27 ✔
359
        for (size_t gout = 0; gout < n_g_; gout++)
54 ✔
360
          chi_prompt(a, gin, gout) =
36 ✔
361
            (1.0 - beta_sum(a, gin)) * temp_matrix(a, gin, gout);
36 ✔
362

363
    // Delayed nu-fission: beta is energy-dependent [n_ang, n_dg, n_g],
364
    // scale by total fission rate summed over outgoing groups
365
    for (size_t a = 0; a < n_ang; a++)
18 ✔
366
      for (size_t d = 0; d < n_dg_; d++)
27 ✔
367
        for (size_t g = 0; g < n_g_; g++)
54 ✔
368
          delayed_nu_fission(a, d, g) =
36 ✔
369
            temp_beta(a, d, g) * matrix_gout_sum(a, g);
36 ✔
370

371
    // chi_delayed = beta * nu-fission matrix, expanded across delayed groups
372
    for (size_t a = 0; a < n_ang; a++)
18 ✔
373
      for (size_t d = 0; d < n_dg_; d++)
27 ✔
374
        for (size_t gin = 0; gin < n_g_; gin++)
54 ✔
375
          for (size_t gout = 0; gout < n_g_; gout++)
108 ✔
376
            chi_delayed(a, d, gin, gout) =
72 ✔
377
              temp_beta(a, d, gin) * temp_matrix(a, gin, gout);
72 ✔
378
  }
27 ✔
379

380
  // Normalize chi_prompt so it sums to 1 over outgoing groups
381
  for (size_t a = 0; a < n_ang; a++)
36 ✔
382
    for (size_t gin = 0; gin < n_g_; gin++) {
54 ✔
383
      tensor::View<double> row = chi_prompt.slice(a, gin);
36 ✔
384
      row /= row.sum();
36 ✔
385
    }
36 ✔
386

387
  // Normalize chi_delayed so it sums to 1 over outgoing groups
388
  for (size_t a = 0; a < n_ang; a++)
36 ✔
389
    for (size_t d = 0; d < n_dg_; d++)
54 ✔
390
      for (size_t gin = 0; gin < n_g_; gin++) {
108 ✔
391
        tensor::View<double> row = chi_delayed.slice(a, d, gin);
72 ✔
392
        row /= row.sum();
72 ✔
393
      }
72 ✔
394
}
18 ✔
395

396
void XsData::fission_matrix_no_beta_from_hdf5(hid_t xsdata_grp, size_t n_ang)
9 ✔
397
{
398
  // Data is provided separately as prompt + delayed nu-fission and chi
399

400
  // Get the prompt nu-fission matrix
401
  tensor::Tensor<double> temp_matrix_p =
9 ✔
402
    tensor::zeros<double>({n_ang, n_g_, n_g_});
9 ✔
403
  read_nd_tensor(xsdata_grp, "prompt-nu-fission", temp_matrix_p, true);
9 ✔
404

405
  // prompt_nu_fission is the sum over outgoing groups
406
  prompt_nu_fission = temp_matrix_p.sum(2);
9 ✔
407

408
  // chi_prompt is the nu-fission matrix normalized over outgoing groups
409
  for (size_t a = 0; a < n_ang; a++)
18 ✔
410
    for (size_t gin = 0; gin < n_g_; gin++)
27 ✔
411
      for (size_t gout = 0; gout < n_g_; gout++)
54 ✔
412
        chi_prompt(a, gin, gout) =
36 ✔
413
          temp_matrix_p(a, gin, gout) / prompt_nu_fission(a, gin);
36 ✔
414

415
  // Get the delayed nu-fission matrix
416
  tensor::Tensor<double> temp_matrix_d =
9 ✔
417
    tensor::zeros<double>({n_ang, n_dg_, n_g_, n_g_});
9 ✔
418
  read_nd_tensor(xsdata_grp, "delayed-nu-fission", temp_matrix_d, true);
9 ✔
419

420
  // delayed_nu_fission is the sum over outgoing groups
421
  delayed_nu_fission = temp_matrix_d.sum(3);
9 ✔
422

423
  // chi_delayed is the delayed nu-fission matrix normalized over outgoing
424
  // groups
425
  for (size_t a = 0; a < n_ang; a++)
18 ✔
426
    for (size_t d = 0; d < n_dg_; d++)
27 ✔
427
      for (size_t gin = 0; gin < n_g_; gin++)
54 ✔
428
        for (size_t gout = 0; gout < n_g_; gout++)
108 ✔
429
          chi_delayed(a, d, gin, gout) =
72 ✔
430
            temp_matrix_d(a, d, gin, gout) / delayed_nu_fission(a, d, gin);
72 ✔
431
}
18 ✔
432

433
void XsData::fission_matrix_no_delayed_from_hdf5(hid_t xsdata_grp, size_t n_ang)
45 ✔
434
{
435
  // No beta is provided and there is no prompt/delay distinction.
436
  // Therefore, the code only considers the data as prompt.
437

438
  // Get nu-fission matrix
439
  tensor::Tensor<double> temp_matrix =
45 ✔
440
    tensor::zeros<double>({n_ang, n_g_, n_g_});
45 ✔
441
  read_nd_tensor(xsdata_grp, "nu-fission", temp_matrix, true);
45 ✔
442

443
  // prompt_nu_fission is the sum over outgoing groups
444
  prompt_nu_fission = temp_matrix.sum(2);
45 ✔
445

446
  // chi_prompt is the nu-fission matrix normalized over outgoing groups
447
  for (size_t a = 0; a < n_ang; a++)
90 ✔
448
    for (size_t gin = 0; gin < n_g_; gin++)
135 ✔
449
      for (size_t gout = 0; gout < n_g_; gout++)
270 ✔
450
        chi_prompt(a, gin, gout) =
180 ✔
451
          temp_matrix(a, gin, gout) / prompt_nu_fission(a, gin);
180 ✔
452
}
45 ✔
453

454
//==============================================================================
455

456
void XsData::fission_from_hdf5(
817 ✔
457
  hid_t xsdata_grp, size_t n_ang, bool is_isotropic)
458
{
459
  // Get the fission and kappa_fission data xs; these are optional
460
  read_nd_tensor(xsdata_grp, "fission", fission);
817 ✔
461
  read_nd_tensor(xsdata_grp, "kappa-fission", kappa_fission);
817 ✔
462

463
  // Get the data; the strategy for doing so depends on if the data is provided
464
  // as a nu-fission matrix or a set of chi and nu-fission vectors
465
  if (object_exists(xsdata_grp, "chi") ||
898 ✔
466
      object_exists(xsdata_grp, "chi-prompt")) {
81 ✔
467
    if (n_dg_ == 0) {
745 ✔
468
      fission_vector_no_delayed_from_hdf5(xsdata_grp, n_ang);
709 ✔
469
    } else {
470
      if (object_exists(xsdata_grp, "beta")) {
36 ✔
471
        fission_vector_beta_from_hdf5(xsdata_grp, n_ang, is_isotropic);
27 ✔
472
      } else {
473
        fission_vector_no_beta_from_hdf5(xsdata_grp, n_ang);
9 ✔
474
      }
475
    }
476
  } else {
477
    if (n_dg_ == 0) {
72 ✔
478
      fission_matrix_no_delayed_from_hdf5(xsdata_grp, n_ang);
45 ✔
479
    } else {
480
      if (object_exists(xsdata_grp, "beta")) {
27 ✔
481
        fission_matrix_beta_from_hdf5(xsdata_grp, n_ang, is_isotropic);
18 ✔
482
      } else {
483
        fission_matrix_no_beta_from_hdf5(xsdata_grp, n_ang);
9 ✔
484
      }
485
    }
486
  }
487

488
  // Combine prompt_nu_fission and delayed_nu_fission into nu_fission
489
  if (n_dg_ == 0) {
817 ✔
490
    nu_fission = prompt_nu_fission;
754 ✔
491
  } else {
492
    nu_fission = prompt_nu_fission + delayed_nu_fission.sum(1);
189 ✔
493
  }
494
}
817 ✔
495

496
//==============================================================================
497

498
void XsData::scatter_from_hdf5(hid_t xsdata_grp, size_t n_ang,
2,698 ✔
499
  AngleDistributionType scatter_format,
500
  AngleDistributionType final_scatter_format, int order_data)
501
{
502
  if (!object_exists(xsdata_grp, "scatter_data")) {
2,698 !
503
    fatal_error("Must provide scatter_data group!");
×
504
  }
505
  hid_t scatt_grp = open_group(xsdata_grp, "scatter_data");
2,698 ✔
506

507
  // Get the outgoing group boundary indices
508
  tensor::Tensor<int> gmin = tensor::zeros<int>({n_ang, n_g_});
2,698 ✔
509
  read_nd_tensor(scatt_grp, "g_min", gmin, true);
2,698 ✔
510
  tensor::Tensor<int> gmax = tensor::zeros<int>({n_ang, n_g_});
2,698 ✔
511
  read_nd_tensor(scatt_grp, "g_max", gmax, true);
2,698 ✔
512

513
  // Make gmin and gmax start from 0 vice 1 as they do in the library
514
  gmin -= 1;
2,698 ✔
515
  gmax -= 1;
2,698 ✔
516

517
  // Now use this info to find the length of a vector to hold the flattened
518
  // data.
519
  size_t length = order_data * (gmax - gmin + 1).sum();
5,396 ✔
520

521
  double_4dvec input_scatt(n_ang, double_3dvec(n_g_));
4,229 ✔
522
  tensor::Tensor<double> temp_arr = tensor::zeros<double>({length});
2,698 ✔
523
  read_nd_tensor(scatt_grp, "scatter_matrix", temp_arr, true);
2,698 ✔
524

525
  // Compare the number of orders given with the max order of the problem;
526
  // strip off the superfluous orders if needed
527
  int order_dim;
2,698 ✔
528
  if (scatter_format == AngleDistributionType::LEGENDRE) {
2,698 ✔
529
    order_dim = std::min(order_data - 1, settings::max_order) + 1;
2,635 ✔
530
  } else {
531
    order_dim = order_data;
532
  }
533

534
  // convert the flattened temp_arr to a jagged array for passing to
535
  // scatt data
536
  size_t temp_idx = 0;
2,698 ✔
537
  for (size_t a = 0; a < n_ang; a++) {
5,450 ✔
538
    for (size_t gin = 0; gin < n_g_; gin++) {
12,983 ✔
539
      input_scatt[a][gin].resize(gmax(a, gin) - gmin(a, gin) + 1);
10,231 ✔
540
      for (size_t i_gout = 0; i_gout < input_scatt[a][gin].size(); i_gout++) {
60,937 ✔
541
        input_scatt[a][gin][i_gout].resize(order_dim);
50,706 ✔
542
        for (size_t l = 0; l < order_dim; l++) {
107,985 ✔
543
          input_scatt[a][gin][i_gout][l] = temp_arr[temp_idx++];
57,279 ✔
544
        }
545
        // Adjust index for the orders we didnt take
546
        temp_idx += (order_data - order_dim);
50,706 ✔
547
      }
548
    }
549
  }
550

551
  // Get multiplication matrix
552
  double_3dvec temp_mult(n_ang, double_2dvec(n_g_));
4,229 ✔
553
  if (object_exists(scatt_grp, "multiplicity_matrix")) {
2,698 ✔
554
    temp_arr.resize({length / order_data});
548 ✔
555
    read_nd_tensor(scatt_grp, "multiplicity_matrix", temp_arr);
548 ✔
556

557
    // convert the flat temp_arr to a jagged array for passing to scatt data
558
    size_t temp_idx = 0;
559
    for (size_t a = 0; a < n_ang; a++) {
1,096 ✔
560
      for (size_t gin = 0; gin < n_g_; gin++) {
6,094 ✔
561
        temp_mult[a][gin].resize(gmax(a, gin) - gmin(a, gin) + 1);
5,546 ✔
562
        for (size_t i_gout = 0; i_gout < temp_mult[a][gin].size(); i_gout++) {
46,249 ✔
563
          temp_mult[a][gin][i_gout] = temp_arr[temp_idx++];
40,703 ✔
564
        }
565
      }
566
    }
567
  } else {
568
    // Use a default: multiplicities are 1.0.
569
    for (size_t a = 0; a < n_ang; a++) {
4,354 ✔
570
      for (size_t gin = 0; gin < n_g_; gin++) {
6,889 ✔
571
        temp_mult[a][gin].resize(gmax(a, gin) - gmin(a, gin) + 1);
4,685 ✔
572
        for (size_t i_gout = 0; i_gout < temp_mult[a][gin].size(); i_gout++) {
14,688 ✔
573
          temp_mult[a][gin][i_gout] = 1.;
10,003 ✔
574
        }
575
      }
576
    }
577
  }
578
  close_group(scatt_grp);
2,698 ✔
579

580
  // Finally, convert the Legendre data to tabular, if needed
581
  if (scatter_format == AngleDistributionType::LEGENDRE &&
2,698 ✔
582
      final_scatter_format == AngleDistributionType::TABULAR) {
2,698 ✔
583
    for (size_t a = 0; a < n_ang; a++) {
5,009 ✔
584
      ScattDataLegendre legendre_scatt;
2,518 ✔
585
      tensor::Tensor<int> in_gmin(gmin.slice(a));
2,518 ✔
586
      tensor::Tensor<int> in_gmax(gmax.slice(a));
2,518 ✔
587

588
      legendre_scatt.init(in_gmin, in_gmax, temp_mult[a], input_scatt[a]);
2,518 ✔
589

590
      // Now create a tabular version of legendre_scatt
591
      convert_legendre_to_tabular(
2,518 ✔
592
        legendre_scatt, *static_cast<ScattDataTabular*>(scatter[a].get()));
2,518 ✔
593

594
      scatter_format = final_scatter_format;
2,518 ✔
595
    }
5,036 ✔
596
  } else {
597
    // We are sticking with the current representation
598
    // Initialize the ScattData object with this data
599
    for (size_t a = 0; a < n_ang; a++) {
441 ✔
600
      tensor::Tensor<int> in_gmin(gmin.slice(a));
234 ✔
601
      tensor::Tensor<int> in_gmax(gmax.slice(a));
234 ✔
602
      scatter[a]->init(in_gmin, in_gmax, temp_mult[a], input_scatt[a]);
234 ✔
603
    }
468 ✔
604
  }
605
}
10,792 ✔
606

607
//==============================================================================
608

609
void XsData::combine(
2,377 ✔
610
  const vector<XsData*>& those_xs, const vector<double>& scalars)
611
{
612
  // Combine the non-scattering data
613
  for (size_t i = 0; i < those_xs.size(); i++) {
5,078 ✔
614
    XsData* that = those_xs[i];
2,701 ✔
615
    if (!equiv(*that))
2,701 !
616
      fatal_error("Cannot combine the XsData objects!");
×
617
    double scalar = scalars[i];
2,701 ✔
618
    total += scalar * that->total;
2,701 ✔
619
    absorption += scalar * that->absorption;
2,701 ✔
620
    if (i == 0) {
2,701 ✔
621
      inverse_velocity = that->inverse_velocity;
2,377 ✔
622
    }
623
    if (!that->prompt_nu_fission.empty()) {
2,701 ✔
624
      nu_fission += scalar * that->nu_fission;
826 ✔
625
      prompt_nu_fission += scalar * that->prompt_nu_fission;
826 ✔
626
      kappa_fission += scalar * that->kappa_fission;
826 ✔
627
      fission += scalar * that->fission;
826 ✔
628
      delayed_nu_fission += scalar * that->delayed_nu_fission;
826 ✔
629
      // Accumulate chi_prompt weighted by total prompt nu-fission
630
      // (summed over energy groups) for this constituent
631
      {
826 ✔
632
        auto pnf_sum = that->prompt_nu_fission.sum(1);
826 ✔
633
        size_t n_ang = chi_prompt.shape(0);
826 !
634
        size_t n_g = chi_prompt.shape(1);
826 !
635
        for (size_t a = 0; a < n_ang; a++)
1,706 ✔
636
          for (size_t gin = 0; gin < n_g; gin++)
4,817 ✔
637
            for (size_t gout = 0; gout < n_g; gout++)
104,486 ✔
638
              chi_prompt(a, gin, gout) +=
100,549 ✔
639
                scalar * pnf_sum(a) * that->chi_prompt(a, gin, gout);
100,549 ✔
640
      }
826 ✔
641
      // Accumulate chi_delayed weighted by total delayed nu-fission
642
      // (summed over energy groups) for this constituent
643
      {
826 ✔
644
        auto dnf_sum = that->delayed_nu_fission.sum(2);
826 ✔
645
        size_t n_ang = chi_delayed.shape(0);
826 !
646
        size_t n_dg = chi_delayed.shape(1);
826 !
647
        size_t n_g = chi_delayed.shape(2);
826 !
648
        for (size_t a = 0; a < n_ang; a++)
1,706 ✔
649
          for (size_t d = 0; d < n_dg; d++)
1,042 ✔
650
            for (size_t gin = 0; gin < n_g; gin++)
486 ✔
651
              for (size_t gout = 0; gout < n_g; gout++)
972 ✔
652
                chi_delayed(a, d, gin, gout) +=
648 ✔
653
                  scalar * dnf_sum(a, d) * that->chi_delayed(a, d, gin, gout);
648 ✔
654
      }
826 ✔
655
    }
656
    decay_rate += scalar * that->decay_rate;
5,402 ✔
657
  }
658

659
  // Normalize chi_prompt so it sums to 1 over outgoing groups
660
  {
2,377 ✔
661
    size_t n_ang = chi_prompt.shape(0);
2,377 ✔
662
    size_t n_g = chi_prompt.shape(1);
2,377 ✔
663
    for (size_t a = 0; a < n_ang; a++)
3,149 ✔
664
      for (size_t gin = 0; gin < n_g; gin++) {
4,493 ✔
665
        tensor::View<double> row = chi_prompt.slice(a, gin);
3,721 ✔
666
        row /= row.sum();
3,721 ✔
667
      }
3,721 ✔
668
  }
669
  // Normalize chi_delayed so it sums to 1 over outgoing groups
670
  {
2,377 ✔
671
    size_t n_ang = chi_delayed.shape(0);
2,377 ✔
672
    size_t n_dg = chi_delayed.shape(1);
2,377 ✔
673
    size_t n_g = chi_delayed.shape(2);
2,377 ✔
674
    for (size_t a = 0; a < n_ang; a++)
3,149 ✔
675
      for (size_t d = 0; d < n_dg; d++)
934 ✔
676
        for (size_t gin = 0; gin < n_g; gin++) {
486 ✔
677
          tensor::View<double> row = chi_delayed.slice(a, d, gin);
324 ✔
678
          row /= row.sum();
324 ✔
679
        }
324 ✔
680
  }
681

682
  // Allow the ScattData object to combine itself
683
  for (size_t a = 0; a < total.shape(0); a++) {
9,616 !
684
    // Build vector of the scattering objects to incorporate
685
    vector<ScattData*> those_scatts(those_xs.size());
2,431 ✔
686
    for (size_t i = 0; i < those_xs.size(); i++) {
5,186 ✔
687
      those_scatts[i] = those_xs[i]->scatter[a].get();
2,755 ✔
688
    }
689

690
    // Now combine these guys
691
    scatter[a]->combine(those_scatts, scalars);
2,431 ✔
692
  }
2,431 ✔
693
}
2,377 ✔
694

695
//==============================================================================
696

697
bool XsData::equiv(const XsData& that)
2,701 ✔
698
{
699
  return (absorption.shape() == that.absorption.shape());
2,701 ✔
700
}
701

702
} // namespace openmc
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc