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

openmc-dev / openmc / 35915463369

23 Sep 2026 08:22PM UTC coverage: 81.584% (+0.1%) from 81.485%
35915463369

Pull #4141

github

web-flow
Merge cec81d375 into 1d75981db
Pull Request #4141: Adding multigroup photon transport capability in MC mode

20033 of 29057 branches covered (68.94%)

Branch coverage included in aggregate %.

185 of 215 new or added lines in 10 files covered. (86.05%)

4 existing lines in 4 files now uncovered.

62689 of 72338 relevant lines covered (86.66%)

49541697.52 hits per line

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

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

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

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

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

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

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

65
  for (int a = 0; a < n_ang; a++) {
16,206 ✔
66
    if (scatter_format == AngleDistributionType::HISTOGRAM) {
8,193 ✔
67
      scatter.emplace_back(new ScattDataHistogram);
120 ✔
68
    } else if (scatter_format == AngleDistributionType::TABULAR) {
8,073 ✔
69
      scatter.emplace_back(new ScattDataTabular);
7,533 ✔
70
    } else if (scatter_format == AngleDistributionType::LEGENDRE) {
540 ✔
71
      scatter.emplace_back(new ScattDataLegendre);
540 ✔
72
    }
73
  }
74
}
8,013 ✔
75

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

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

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

96
  // Get scattering data
97
  scatter_from_hdf5(
4,269 ✔
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++)
20,589 ✔
103
    if (absorption.data()[i] == 0.0)
16,320 ✔
104
      absorption.data()[i] = 1.e-10;
22 ✔
105

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

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

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

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

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

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

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

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

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

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

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

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

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

279
  // Replicate the energy spectrum across all incoming groups
280
  for (size_t a = 0; a < n_ang; a++)
2,392 ✔
281
    for (size_t gin = 0; gin < n_g_; gin++)
7,198 ✔
282
      chi_prompt.slice(a, gin) = temp_chi.slice(a);
17,871 ✔
283

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

456
void XsData::fission_from_hdf5(
1,331 ✔
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);
1,331 ✔
461
  read_nd_tensor(xsdata_grp, "kappa-fission", kappa_fission);
1,331 ✔
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,466 ✔
466
      object_exists(xsdata_grp, "chi-prompt")) {
135 ✔
467
    if (n_dg_ == 0) {
1,211 ✔
468
      fission_vector_no_delayed_from_hdf5(xsdata_grp, n_ang);
1,151 ✔
469
    } else {
470
      if (object_exists(xsdata_grp, "beta")) {
60 ✔
471
        fission_vector_beta_from_hdf5(xsdata_grp, n_ang, is_isotropic);
45 ✔
472
      } else {
473
        fission_vector_no_beta_from_hdf5(xsdata_grp, n_ang);
15 ✔
474
      }
475
    }
476
  } else {
477
    if (n_dg_ == 0) {
120 ✔
478
      fission_matrix_no_delayed_from_hdf5(xsdata_grp, n_ang);
75 ✔
479
    } else {
480
      if (object_exists(xsdata_grp, "beta")) {
45 ✔
481
        fission_matrix_beta_from_hdf5(xsdata_grp, n_ang, is_isotropic);
30 ✔
482
      } else {
483
        fission_matrix_no_beta_from_hdf5(xsdata_grp, n_ang);
15 ✔
484
      }
485
    }
486
  }
487

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

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

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

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

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

521
  double_4dvec input_scatt(n_ang, double_3dvec(n_g_));
6,311 ✔
522
  tensor::Tensor<double> temp_arr = tensor::zeros<double>({length});
4,269 ✔
523
  read_nd_tensor(scatt_grp, "scatter_matrix", temp_arr, true);
4,269 ✔
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;
4,269 ✔
528
  if (scatter_format == AngleDistributionType::LEGENDRE) {
4,269 ✔
529
    order_dim = std::min(order_data - 1, settings::max_order) + 1;
4,164 ✔
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;
4,269 ✔
537
  for (size_t a = 0; a < n_ang; a++) {
8,628 ✔
538
    for (size_t gin = 0; gin < n_g_; gin++) {
20,679 ✔
539
      input_scatt[a][gin].resize(gmax(a, gin) - gmin(a, gin) + 1);
16,320 ✔
540
      for (size_t i_gout = 0; i_gout < input_scatt[a][gin].size(); i_gout++) {
98,918 ✔
541
        input_scatt[a][gin][i_gout].resize(order_dim);
82,598 ✔
542
        for (size_t l = 0; l < order_dim; l++) {
175,921 ✔
543
          input_scatt[a][gin][i_gout][l] = temp_arr[temp_idx++];
93,323 ✔
544
        }
545
        // Adjust index for the orders we didnt take
546
        temp_idx += (order_data - order_dim);
82,598 ✔
547
      }
548
    }
549
  }
550

551
  // Get multiplication matrix
552
  double_3dvec temp_mult(n_ang, double_2dvec(n_g_));
6,311 ✔
553
  if (object_exists(scatt_grp, "multiplicity_matrix")) {
4,269 ✔
554
    temp_arr.resize({length / order_data});
872 ✔
555
    read_nd_tensor(scatt_grp, "multiplicity_matrix", temp_arr);
872 ✔
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,744 ✔
560
      for (size_t gin = 0; gin < n_g_; gin++) {
9,988 ✔
561
        temp_mult[a][gin].resize(gmax(a, gin) - gmin(a, gin) + 1);
9,116 ✔
562
        for (size_t i_gout = 0; i_gout < temp_mult[a][gin].size(); i_gout++) {
76,703 ✔
563
          temp_mult[a][gin][i_gout] = temp_arr[temp_idx++];
67,587 ✔
564
        }
565
      }
566
    }
567
  } else {
568
    // Use a default: multiplicities are 1.0.
569
    for (size_t a = 0; a < n_ang; a++) {
6,884 ✔
570
      for (size_t gin = 0; gin < n_g_; gin++) {
10,691 ✔
571
        temp_mult[a][gin].resize(gmax(a, gin) - gmin(a, gin) + 1);
7,204 ✔
572
        for (size_t i_gout = 0; i_gout < temp_mult[a][gin].size(); i_gout++) {
22,215 ✔
573
          temp_mult[a][gin][i_gout] = 1.;
15,011 ✔
574
        }
575
      }
576
    }
577
  }
578
  close_group(scatt_grp);
4,269 ✔
579

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

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

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

594
      scatter_format = final_scatter_format;
3,969 ✔
595
    }
7,938 ✔
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++) {
735 ✔
600
      tensor::Tensor<int> in_gmin(gmin.slice(a));
390 ✔
601
      tensor::Tensor<int> in_gmax(gmax.slice(a));
390 ✔
602
      scatter[a]->init(in_gmin, in_gmax, temp_mult[a], input_scatt[a]);
390 ✔
603
    }
780 ✔
604
  }
605
}
17,076 ✔
606

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

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

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

682
  // Allow the ScattData object to combine itself
683
  for (size_t a = 0; a < total.shape(0); a++) {
15,156 !
684
    // Build vector of the scattering objects to incorporate
685
    vector<ScattData*> those_scatts(those_xs.size());
3,834 ✔
686
    for (size_t i = 0; i < those_xs.size(); i++) {
8,208 ✔
687
      those_scatts[i] = those_xs[i]->scatter[a].get();
4,374 ✔
688
    }
689

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

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

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