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

openmc-dev / openmc / 44413430952

18 Nov 2026 10:37PM UTC coverage: 82.378% (+0.02%) from 82.354%
44413430952

Pull #4141

github

web-flow
Merge 85b2906c8 into b3443897d
Pull Request #4141: Adding multigroup photon transport capability in MC mode

21006 of 29768 branches covered (70.57%)

Branch coverage included in aggregate %.

43 of 44 new or added lines in 6 files covered. (97.73%)

88 existing lines in 3 files now uncovered.

64485 of 74011 relevant lines covered (87.13%)

42516521.04 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,749 ✔
24
  int n_pol, int n_azi, size_t n_groups, size_t n_d_groups)
5,749 ✔
25
  : n_g_(n_groups), n_dg_(n_d_groups)
5,749 ✔
26
{
27
  size_t n_ang = n_pol * n_azi;
5,749 ✔
28

29
  // check to make sure scatter format is OK before we allocate
30
  if (scatter_format != AngleDistributionType::HISTOGRAM &&
5,749 ✔
31
      scatter_format != AngleDistributionType::TABULAR &&
5,749 !
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,749 ✔
37
  total = tensor::zeros<double>(shape);
5,749 ✔
38
  absorption = tensor::zeros<double>(shape);
5,749 ✔
39
  inverse_velocity = tensor::zeros<double>(shape);
5,749 ✔
40
  if (fissionable) {
5,749 ✔
41
    fission = tensor::zeros<double>(shape);
1,766 ✔
42
    nu_fission = tensor::zeros<double>(shape);
1,766 ✔
43
    prompt_nu_fission = tensor::zeros<double>(shape);
1,766 ✔
44
    kappa_fission = tensor::zeros<double>(shape);
3,532 ✔
45
  }
46

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

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

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

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

65
  for (int a = 0; a < n_ang; a++) {
11,618 ✔
66
    if (scatter_format == AngleDistributionType::HISTOGRAM) {
5,869 ✔
67
      scatter.emplace_back(new ScattDataHistogram);
100 ✔
68
    } else if (scatter_format == AngleDistributionType::TABULAR) {
5,769 ✔
69
      scatter.emplace_back(new ScattDataTabular);
5,389 ✔
70
    } else if (scatter_format == AngleDistributionType::LEGENDRE) {
380 ✔
71
      scatter.emplace_back(new ScattDataLegendre);
380 ✔
72
    }
73
  }
74
}
5,749 ✔
75

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

78
void XsData::from_hdf5(hid_t xsdata_grp, bool fissionable,
3,053 ✔
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;
3,053 ✔
85
  size_t energy_groups = total.shape(1);
3,053 !
86

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

96
  // Get scattering data
97
  scatter_from_hdf5(
3,053 ✔
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++)
14,517 ✔
103
    if (absorption.data()[i] == 0.0)
11,464 ✔
104
      absorption.data()[i] = 1.e-10;
16 ✔
105

106
  // Get or calculate the total x/s
107
  if (object_exists(xsdata_grp, "total")) {
3,053 !
108
    read_nd_tensor(xsdata_grp, "total", total);
3,053 ✔
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++)
14,517 ✔
120
    if (total.data()[i] == 0.0)
11,464 ✔
121
      total.data()[i] = 1.e-10;
16 ✔
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() &&
3,107 ✔
126
      !object_exists(xsdata_grp, "scatter_data/multiplicity_matrix")) {
54 ✔
127
    for (size_t a = 0; a < n_ang; a++) {
27 ✔
128
      for (size_t g = 0; g < energy_groups; g++) {
61 ✔
129
        double production = scatter[a]->scattxs[g];
51 !
130
        if (production <= 0.0)
51 !
NEW
131
          continue;
×
132
        double removal = total(a, g) - absorption(a, g);
51 ✔
133
        if (removal <= 0.0)
51 ✔
134
          fatal_error(fmt::format("Photon group {} produces photons but has "
7 ✔
135
                                  "no scattering to carry them.",
136
            g + 1));
7 !
137
        std::fill(scatter[a]->mult[g].begin(), scatter[a]->mult[g].end(),
44 ✔
138
          production / removal);
44 ✔
139
      }
140
    }
141
  }
142
}
3,046 ✔
143

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

146
void XsData::fission_vector_beta_from_hdf5(
30 ✔
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_});
30 ✔
153
  read_nd_tensor(xsdata_grp, "chi", temp_chi, true);
30 ✔
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++) {
60 ✔
157
    tensor::View<double> row = temp_chi.slice(a);
30 ✔
158
    row /= row.sum();
30 ✔
159
  }
30 ✔
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++)
60 ✔
164
    for (size_t gin = 0; gin < n_g_; gin++)
90 ✔
165
      chi_prompt.slice(a, gin) = temp_chi.slice(a);
180 ✔
166

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

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

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

188
    // prompt_nu_fission = (1 - sum_of_beta) * nu_fission
189
    auto beta_sum = temp_beta.sum(1);
20 ✔
190
    for (size_t a = 0; a < n_ang; a++)
40 ✔
191
      for (size_t g = 0; g < n_g_; g++)
60 ✔
192
        prompt_nu_fission(a, g) = temp_nufiss(a, g) * (1.0 - beta_sum(a));
40 ✔
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++)
40 ✔
197
      for (size_t d = 0; d < n_dg_; d++)
100 ✔
198
        for (size_t g = 0; g < n_g_; g++)
240 ✔
199
          delayed_nu_fission(a, d, g) = temp_beta(a, d) * temp_nufiss(a, g);
160 ✔
200
  } else if (beta_ndims == ndim_target + 1) {
50 !
201
    tensor::Tensor<double> temp_beta =
10 ✔
202
      tensor::zeros<double>({n_ang, n_dg_, n_g_});
10 ✔
203
    read_nd_tensor(xsdata_grp, "beta", temp_beta, true);
10 ✔
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);
10 ✔
208
    for (size_t a = 0; a < n_ang; a++)
20 ✔
209
      for (size_t g = 0; g < n_g_; g++)
30 ✔
210
        prompt_nu_fission(a, g) = temp_nufiss(a, g) * (1.0 - beta_sum(a, g));
20 ✔
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++)
20 ✔
215
      for (size_t d = 0; d < n_dg_; d++)
30 ✔
216
        for (size_t g = 0; g < n_g_; g++)
60 ✔
217
          delayed_nu_fission(a, d, g) = temp_beta(a, d, g) * temp_nufiss(a, g);
40 ✔
218
  }
20 ✔
219
}
60 ✔
220

221
void XsData::fission_vector_no_beta_from_hdf5(hid_t xsdata_grp, size_t n_ang)
10 ✔
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_});
10 ✔
227
  read_nd_tensor(xsdata_grp, "chi-prompt", temp_chi_p, true);
10 ✔
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++) {
20 ✔
231
    tensor::View<double> row = temp_chi_p.slice(a);
10 ✔
232
    row /= row.sum();
10 ✔
233
  }
10 ✔
234

235
  // Get chi-delayed
236
  tensor::Tensor<double> temp_chi_d =
10 ✔
237
    tensor::zeros<double>({n_ang, n_dg_, n_g_});
10 ✔
238
  read_nd_tensor(xsdata_grp, "chi-delayed", temp_chi_d, true);
10 ✔
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++)
20 ✔
243
    for (size_t d = 0; d < n_dg_; d++) {
30 ✔
244
      tensor::View<double> row = temp_chi_d.slice(a, d);
20 ✔
245
      row /= row.sum();
20 ✔
246
    }
20 ✔
247

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

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

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

264
void XsData::fission_vector_no_delayed_from_hdf5(hid_t xsdata_grp, size_t n_ang)
818 ✔
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_});
818 ✔
271
  read_nd_tensor(xsdata_grp, "chi", temp_chi, true);
818 ✔
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,696 ✔
275
    tensor::View<double> row = temp_chi.slice(a);
878 ✔
276
    row /= row.sum();
878 ✔
277
  }
878 ✔
278

279
  // Replicate the energy spectrum across all incoming groups
280
  for (size_t a = 0; a < n_ang; a++)
1,696 ✔
281
    for (size_t gin = 0; gin < n_g_; gin++)
5,054 ✔
282
      chi_prompt.slice(a, gin) = temp_chi.slice(a);
12,528 ✔
283

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

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

290
void XsData::fission_matrix_beta_from_hdf5(
20 ✔
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 =
20 ✔
297
    tensor::zeros<double>({n_ang, n_g_, n_g_});
20 ✔
298
  read_nd_tensor(xsdata_grp, "nu-fission", temp_matrix, true);
20 ✔
299

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

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

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

319
    // chi_prompt = (1 - beta_total) * nu-fission matrix (unnormalized)
320
    for (size_t a = 0; a < n_ang; a++)
20 ✔
321
      for (size_t gin = 0; gin < n_g_; gin++)
30 ✔
322
        for (size_t gout = 0; gout < n_g_; gout++)
60 ✔
323
          chi_prompt(a, gin, gout) =
40 ✔
324
            (1.0 - beta_sum(a)) * temp_matrix(a, gin, gout);
40 ✔
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++)
20 ✔
329
      for (size_t d = 0; d < n_dg_; d++)
30 ✔
330
        for (size_t g = 0; g < n_g_; g++)
60 ✔
331
          delayed_nu_fission(a, d, g) = temp_beta(a, d) * matrix_gout_sum(a, g);
40 ✔
332

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

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

346
    auto beta_sum = temp_beta.sum(1);
10 ✔
347
    auto matrix_gout_sum = temp_matrix.sum(2);
10 ✔
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++)
20 ✔
352
      for (size_t g = 0; g < n_g_; g++)
30 ✔
353
        prompt_nu_fission(a, g) =
20 ✔
354
          matrix_gout_sum(a, g) * (1.0 - beta_sum(a, g));
20 ✔
355

356
    // chi_prompt = (1 - beta_sum) * nu-fission matrix (unnormalized)
357
    for (size_t a = 0; a < n_ang; a++)
20 ✔
358
      for (size_t gin = 0; gin < n_g_; gin++)
30 ✔
359
        for (size_t gout = 0; gout < n_g_; gout++)
60 ✔
360
          chi_prompt(a, gin, gout) =
40 ✔
361
            (1.0 - beta_sum(a, gin)) * temp_matrix(a, gin, gout);
40 ✔
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++)
20 ✔
366
      for (size_t d = 0; d < n_dg_; d++)
30 ✔
367
        for (size_t g = 0; g < n_g_; g++)
60 ✔
368
          delayed_nu_fission(a, d, g) =
40 ✔
369
            temp_beta(a, d, g) * matrix_gout_sum(a, g);
40 ✔
370

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

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

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

396
void XsData::fission_matrix_no_beta_from_hdf5(hid_t xsdata_grp, size_t n_ang)
10 ✔
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 =
10 ✔
402
    tensor::zeros<double>({n_ang, n_g_, n_g_});
10 ✔
403
  read_nd_tensor(xsdata_grp, "prompt-nu-fission", temp_matrix_p, true);
10 ✔
404

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

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

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

420
  // delayed_nu_fission is the sum over outgoing groups
421
  delayed_nu_fission = temp_matrix_d.sum(3);
10 ✔
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++)
20 ✔
426
    for (size_t d = 0; d < n_dg_; d++)
30 ✔
427
      for (size_t gin = 0; gin < n_g_; gin++)
60 ✔
428
        for (size_t gout = 0; gout < n_g_; gout++)
120 ✔
429
          chi_delayed(a, d, gin, gout) =
80 ✔
430
            temp_matrix_d(a, d, gin, gout) / delayed_nu_fission(a, d, gin);
80 ✔
431
}
20 ✔
432

433
void XsData::fission_matrix_no_delayed_from_hdf5(hid_t xsdata_grp, size_t n_ang)
50 ✔
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 =
50 ✔
440
    tensor::zeros<double>({n_ang, n_g_, n_g_});
50 ✔
441
  read_nd_tensor(xsdata_grp, "nu-fission", temp_matrix, true);
50 ✔
442

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

446
  // chi_prompt is the nu-fission matrix normalized over outgoing groups
447
  for (size_t a = 0; a < n_ang; a++)
100 ✔
448
    for (size_t gin = 0; gin < n_g_; gin++)
150 ✔
449
      for (size_t gout = 0; gout < n_g_; gout++)
300 ✔
450
        chi_prompt(a, gin, gout) =
200 ✔
451
          temp_matrix(a, gin, gout) / prompt_nu_fission(a, gin);
200 ✔
452
}
50 ✔
453

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

456
void XsData::fission_from_hdf5(
938 ✔
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);
938 ✔
461
  read_nd_tensor(xsdata_grp, "kappa-fission", kappa_fission);
938 ✔
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") ||
1,028 ✔
466
      object_exists(xsdata_grp, "chi-prompt")) {
90 ✔
467
    if (n_dg_ == 0) {
858 ✔
468
      fission_vector_no_delayed_from_hdf5(xsdata_grp, n_ang);
818 ✔
469
    } else {
470
      if (object_exists(xsdata_grp, "beta")) {
40 ✔
471
        fission_vector_beta_from_hdf5(xsdata_grp, n_ang, is_isotropic);
30 ✔
472
      } else {
473
        fission_vector_no_beta_from_hdf5(xsdata_grp, n_ang);
10 ✔
474
      }
475
    }
476
  } else {
477
    if (n_dg_ == 0) {
80 ✔
478
      fission_matrix_no_delayed_from_hdf5(xsdata_grp, n_ang);
50 ✔
479
    } else {
480
      if (object_exists(xsdata_grp, "beta")) {
30 ✔
481
        fission_matrix_beta_from_hdf5(xsdata_grp, n_ang, is_isotropic);
20 ✔
482
      } else {
483
        fission_matrix_no_beta_from_hdf5(xsdata_grp, n_ang);
10 ✔
484
      }
485
    }
486
  }
487

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

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

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

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

513
  // Make gmin and gmax start from 0 vice 1 as they do in the library
514
  gmin -= 1;
3,053 ✔
515
  gmax -= 1;
3,053 ✔
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();
6,106 ✔
520

521
  double_4dvec input_scatt(n_ang, double_3dvec(n_g_));
4,921 ✔
522
  tensor::Tensor<double> temp_arr = tensor::zeros<double>({length});
3,053 ✔
523
  read_nd_tensor(scatt_grp, "scatter_matrix", temp_arr, true);
3,053 ✔
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;
3,053 ✔
528
  if (scatter_format == AngleDistributionType::LEGENDRE) {
3,053 ✔
529
    order_dim = std::min(order_data - 1, settings::max_order) + 1;
2,963 ✔
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;
3,053 ✔
537
  for (size_t a = 0; a < n_ang; a++) {
6,166 ✔
538
    for (size_t gin = 0; gin < n_g_; gin++) {
14,577 ✔
539
      input_scatt[a][gin].resize(gmax(a, gin) - gmin(a, gin) + 1);
11,464 ✔
540
      for (size_t i_gout = 0; i_gout < input_scatt[a][gin].size(); i_gout++) {
67,948 ✔
541
        input_scatt[a][gin][i_gout].resize(order_dim);
56,484 ✔
542
        for (size_t l = 0; l < order_dim; l++) {
121,272 ✔
543
          input_scatt[a][gin][i_gout][l] = temp_arr[temp_idx++];
64,788 ✔
544
        }
545
        // Adjust index for the orders we didnt take
546
        temp_idx += (order_data - order_dim);
56,484 ✔
547
      }
548
    }
549
  }
550

551
  // Get multiplication matrix
552
  double_3dvec temp_mult(n_ang, double_2dvec(n_g_));
4,921 ✔
553
  if (object_exists(scatt_grp, "multiplicity_matrix")) {
3,053 ✔
554
    temp_arr.resize({length / order_data});
642 ✔
555
    read_nd_tensor(scatt_grp, "multiplicity_matrix", temp_arr);
642 ✔
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,284 ✔
560
      for (size_t gin = 0; gin < n_g_; gin++) {
6,875 ✔
561
        temp_mult[a][gin].resize(gmax(a, gin) - gmin(a, gin) + 1);
6,233 ✔
562
        for (size_t i_gout = 0; i_gout < temp_mult[a][gin].size(); i_gout++) {
51,569 ✔
563
          temp_mult[a][gin][i_gout] = temp_arr[temp_idx++];
45,336 ✔
564
        }
565
      }
566
    }
567
  } else {
568
    // Use a default: multiplicities are 1.0.
569
    for (size_t a = 0; a < n_ang; a++) {
4,882 ✔
570
      for (size_t gin = 0; gin < n_g_; gin++) {
7,702 ✔
571
        temp_mult[a][gin].resize(gmax(a, gin) - gmin(a, gin) + 1);
5,231 ✔
572
        for (size_t i_gout = 0; i_gout < temp_mult[a][gin].size(); i_gout++) {
16,379 ✔
573
          temp_mult[a][gin][i_gout] = 1.;
11,148 ✔
574
        }
575
      }
576
    }
577
  }
578
  close_group(scatt_grp);
3,053 ✔
579

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

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

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

594
      scatter_format = final_scatter_format;
2,823 ✔
595
    }
5,646 ✔
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++) {
550 ✔
600
      tensor::Tensor<int> in_gmin(gmin.slice(a));
290 ✔
601
      tensor::Tensor<int> in_gmax(gmax.slice(a));
290 ✔
602
      scatter[a]->init(in_gmin, in_gmax, temp_mult[a], input_scatt[a]);
290 ✔
603
    }
580 ✔
604
  }
605
}
12,212 ✔
606

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

609
void XsData::combine(
2,696 ✔
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,752 ✔
614
    XsData* that = those_xs[i];
3,056 ✔
615
    if (!equiv(*that))
3,056 !
UNCOV
616
      fatal_error("Cannot combine the XsData objects!");
×
617
    double scalar = scalars[i];
3,056 ✔
618
    total += scalar * that->total;
3,056 ✔
619
    absorption += scalar * that->absorption;
3,056 ✔
620
    if (i == 0) {
3,056 ✔
621
      inverse_velocity = that->inverse_velocity;
2,696 ✔
622
    }
623
    if (!that->prompt_nu_fission.empty()) {
3,056 ✔
624
      nu_fission += scalar * that->nu_fission;
948 ✔
625
      prompt_nu_fission += scalar * that->prompt_nu_fission;
948 ✔
626
      kappa_fission += scalar * that->kappa_fission;
948 ✔
627
      fission += scalar * that->fission;
948 ✔
628
      delayed_nu_fission += scalar * that->delayed_nu_fission;
948 ✔
629
      // Accumulate chi_prompt weighted by total prompt nu-fission
630
      // (summed over energy groups) for this constituent
631
      {
948 ✔
632
        auto pnf_sum = that->prompt_nu_fission.sum(1);
948 ✔
633
        size_t n_ang = chi_prompt.shape(0);
948 !
634
        size_t n_g = chi_prompt.shape(1);
948 !
635
        for (size_t a = 0; a < n_ang; a++)
1,956 ✔
636
          for (size_t gin = 0; gin < n_g; gin++)
5,444 ✔
637
            for (size_t gout = 0; gout < n_g; gout++)
116,288 ✔
638
              chi_prompt(a, gin, gout) +=
111,852 ✔
639
                scalar * pnf_sum(a) * that->chi_prompt(a, gin, gout);
111,852 ✔
640
      }
948 ✔
641
      // Accumulate chi_delayed weighted by total delayed nu-fission
642
      // (summed over energy groups) for this constituent
643
      {
948 ✔
644
        auto dnf_sum = that->delayed_nu_fission.sum(2);
948 ✔
645
        size_t n_ang = chi_delayed.shape(0);
948 !
646
        size_t n_dg = chi_delayed.shape(1);
948 !
647
        size_t n_g = chi_delayed.shape(2);
948 !
648
        for (size_t a = 0; a < n_ang; a++)
1,956 ✔
649
          for (size_t d = 0; d < n_dg; d++)
1,188 ✔
650
            for (size_t gin = 0; gin < n_g; gin++)
540 ✔
651
              for (size_t gout = 0; gout < n_g; gout++)
1,080 ✔
652
                chi_delayed(a, d, gin, gout) +=
720 ✔
653
                  scalar * dnf_sum(a, d) * that->chi_delayed(a, d, gin, gout);
720 ✔
654
      }
948 ✔
655
    }
656
    decay_rate += scalar * that->decay_rate;
6,112 ✔
657
  }
658

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

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

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

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

697
bool XsData::equiv(const XsData& that)
3,056 ✔
698
{
699
  return (absorption.shape() == that.absorption.shape());
3,056 ✔
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