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

openmc-dev / openmc / 23367229037

20 Mar 2026 11:54PM UTC coverage: 81.453% (-0.1%) from 81.58%
23367229037

Pull #3877

github

web-flow
Merge 7af343511 into 3ce6cbfdd
Pull Request #3877: Add VTKHDF output support for all structured mesh types (#3620)

17595 of 25365 branches covered (69.37%)

Branch coverage included in aggregate %.

128 of 133 new or added lines in 1 file covered. (96.24%)

372 existing lines in 15 files now uncovered.

58192 of 67679 relevant lines covered (85.98%)

44598959.65 hits per line

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

90.18
/src/secondary_correlated.cpp
1
#include "openmc/secondary_correlated.h"
2

3
#include <algorithm> // for copy
4
#include <cmath>
5
#include <cstddef>  // for size_t
6
#include <iterator> // for back_inserter
7

8
#include "openmc/tensor.h"
9

10
#include "openmc/endf.h"
11
#include "openmc/hdf5_interface.h"
12
#include "openmc/math_functions.h"
13
#include "openmc/random_lcg.h"
14
#include "openmc/search.h"
15

16
namespace openmc {
17

18
//==============================================================================
19
//! CorrelatedAngleEnergy implementation
20
//==============================================================================
21

22
CorrelatedAngleEnergy::CorrelatedAngleEnergy(hid_t group)
50,258✔
23
{
24
  // Open incoming energy dataset
25
  hid_t dset = open_dataset(group, "energy");
50,258✔
26

27
  // Get interpolation parameters
28
  tensor::Tensor<int> temp;
50,258✔
29
  read_attribute(dset, "interpolation", temp);
50,258✔
30

31
  tensor::View<int> temp_b = temp.slice(0); // breakpoints
50,258✔
32
  tensor::View<int> temp_i = temp.slice(1); // interpolation parameters
50,258✔
33

34
  std::copy(temp_b.begin(), temp_b.end(), std::back_inserter(breakpoints_));
50,258✔
35
  for (const auto i : temp_i)
100,516✔
36
    interpolation_.push_back(int2interp(i));
50,258✔
37
  n_region_ = breakpoints_.size();
50,258✔
38

39
  // Get incoming energies
40
  read_dataset(dset, energy_);
50,258✔
41
  std::size_t n_energy = energy_.size();
50,258✔
42
  close_dataset(dset);
50,258✔
43

44
  // Get outgoing energy distribution data
45
  dset = open_dataset(group, "energy_out");
50,258✔
46
  vector<int> offsets;
50,258✔
47
  vector<int> interp;
50,258✔
48
  vector<int> n_discrete;
50,258✔
49
  read_attribute(dset, "offsets", offsets);
50,258✔
50
  read_attribute(dset, "interpolation", interp);
50,258✔
51
  read_attribute(dset, "n_discrete_lines", n_discrete);
50,258✔
52

53
  tensor::Tensor<double> eout;
50,258✔
54
  read_dataset(dset, eout);
50,258✔
55
  close_dataset(dset);
50,258✔
56

57
  // Read angle distributions
58
  tensor::Tensor<double> mu;
50,258✔
59
  read_dataset(group, "mu", mu);
50,258✔
60

61
  for (int i = 0; i < n_energy; ++i) {
1,045,295✔
62
    // Determine number of outgoing energies
63
    int j = offsets[i];
995,037✔
64
    int n;
995,037✔
65
    if (i < n_energy - 1) {
995,037✔
66
      n = offsets[i + 1] - j;
944,779✔
67
    } else {
68
      n = eout.shape(1) - j;
100,516!
69
    }
70

71
    // Assign interpolation scheme and number of discrete lines
72
    CorrTable d;
995,037✔
73
    d.interpolation = int2interp(interp[i]);
995,037✔
74
    d.n_discrete = n_discrete[i];
995,037✔
75

76
    // Copy data
77
    d.e_out = eout.slice(0, tensor::range(j, j + n));
995,037✔
78
    d.p = eout.slice(1, tensor::range(j, j + n));
995,037✔
79
    d.c = eout.slice(2, tensor::range(j, j + n));
995,037✔
80

81
    // To get answers that match ACE data, for now we still use the tabulated
82
    // CDF values that were passed through to the HDF5 library. At a later
83
    // time, we can remove the CDF values from the HDF5 library and
84
    // reconstruct them using the PDF
85
    if (false) {
995,037✔
86
      // Calculate cumulative distribution function -- discrete portion
87
      for (int k = 0; k < d.n_discrete; ++k) {
88
        if (k == 0) {
89
          d.c[k] = d.p[k];
90
        } else {
91
          d.c[k] = d.c[k - 1] + d.p[k];
92
        }
93
      }
94

95
      // Continuous portion
96
      for (int k = d.n_discrete; k < n; ++k) {
97
        if (k == d.n_discrete) {
98
          d.c[k] = d.c[k - 1] + d.p[k];
99
        } else {
100
          if (d.interpolation == Interpolation::histogram) {
101
            d.c[k] = d.c[k - 1] + d.p[k - 1] * (d.e_out[k] - d.e_out[k - 1]);
102
          } else if (d.interpolation == Interpolation::lin_lin) {
103
            d.c[k] = d.c[k - 1] + 0.5 * (d.p[k - 1] + d.p[k]) *
104
                                    (d.e_out[k] - d.e_out[k - 1]);
105
          }
106
        }
107
      }
108

109
      // Normalize density and distribution functions
110
      d.p /= d.c[n - 1];
111
      d.c /= d.c[n - 1];
112
    }
113

114
    for (j = 0; j < n; ++j) {
23,954,949✔
115
      // Get interpolation scheme
116
      int interp_mu = std::lround(eout(3, offsets[i] + j));
22,959,912✔
117

118
      // Determine offset and size of distribution
119
      int offset_mu = std::lround(eout(4, offsets[i] + j));
22,959,912✔
120
      int m;
22,959,912✔
121
      if (offsets[i] + j + 1 < eout.shape(1)) {
45,919,824!
122
        m = std::lround(eout(4, offsets[i] + j + 1)) - offset_mu;
22,909,654✔
123
      } else {
124
        m = mu.shape(1) - offset_mu;
100,516!
125
      }
126

127
      // For incoherent inelastic thermal scattering, the angle distributions
128
      // may be given as discrete mu values. In this case, interpolation values
129
      // of zero appear in the HDF5 file. Here we change it to a 1 so that
130
      // int2interp doesn't fail.
131
      if (interp_mu == 0)
22,959,912✔
132
        interp_mu = 1;
528✔
133

134
      auto interp = int2interp(interp_mu);
22,959,912✔
135
      tensor::View<double> xs =
22,959,912✔
136
        mu.slice(0, tensor::range(offset_mu, offset_mu + m));
22,959,912✔
137
      tensor::View<double> ps =
22,959,912✔
138
        mu.slice(1, tensor::range(offset_mu, offset_mu + m));
22,959,912✔
139
      tensor::View<double> cs =
22,959,912✔
140
        mu.slice(2, tensor::range(offset_mu, offset_mu + m));
22,959,912✔
141

142
      vector<double> x {xs.begin(), xs.end()};
22,959,912✔
143
      vector<double> p {ps.begin(), ps.end()};
22,959,912✔
144
      vector<double> c {cs.begin(), cs.end()};
22,959,912✔
145

146
      // To get answers that match ACE data, for now we still use the tabulated
147
      // CDF values that were passed through to the HDF5 library. At a later
148
      // time, we can remove the CDF values from the HDF5 library and
149
      // reconstruct them using the PDF
150
      Tabular* mudist = new Tabular {x.data(), p.data(), m, interp, c.data()};
22,959,912✔
151

152
      d.angle.emplace_back(mudist);
22,959,912✔
153
    } // outgoing energies
91,839,648✔
154

155
    distribution_.push_back(std::move(d));
995,037✔
156
  } // incoming energies
995,037✔
157
}
251,290✔
158
Distribution& CorrelatedAngleEnergy::sample_dist(
374,254✔
159
  double E_in, double& E_out, uint64_t* seed) const
160
{
161
  // Find energy bin and calculate interpolation factor
162
  int i;
374,254✔
163
  double r;
374,254✔
164
  get_energy_index(energy_, E_in, i, r);
374,254✔
165

166
  // Sample between the ith and [i+1]th bin
167
  int l = r > prn(seed) ? i + 1 : i;
374,254✔
168

169
  // Interpolation for energy E1 and EK
170
  int n_energy_out = distribution_[i].e_out.size();
374,254✔
171
  int n_discrete = distribution_[i].n_discrete;
374,254✔
172
  double E_i_1 = distribution_[i].e_out[n_discrete];
374,254✔
173
  double E_i_K = distribution_[i].e_out[n_energy_out - 1];
374,254✔
174

175
  n_energy_out = distribution_[i + 1].e_out.size();
374,254✔
176
  n_discrete = distribution_[i + 1].n_discrete;
374,254✔
177
  double E_i1_1 = distribution_[i + 1].e_out[n_discrete];
374,254✔
178
  double E_i1_K = distribution_[i + 1].e_out[n_energy_out - 1];
374,254✔
179

180
  double E_1 = E_i_1 + r * (E_i1_1 - E_i_1);
374,254✔
181
  double E_K = E_i_K + r * (E_i1_K - E_i_K);
374,254✔
182

183
  // Determine outgoing energy bin
184
  n_energy_out = distribution_[l].e_out.size();
374,254✔
185
  n_discrete = distribution_[l].n_discrete;
374,254✔
186
  double r1 = prn(seed);
374,254✔
187
  double c_k = distribution_[l].c[0];
374,254✔
188
  int k = 0;
374,254✔
189
  int end = n_energy_out - 2;
374,254✔
190

191
  // Discrete portion
192
  for (int j = 0; j < n_discrete; ++j) {
374,254!
UNCOV
193
    k = j;
×
194
    c_k = distribution_[l].c[k];
×
195
    if (r1 < c_k) {
×
196
      end = j;
197
      break;
198
    }
199
  }
200

201
  // Continuous portion
202
  double c_k1;
374,254✔
203
  for (int j = n_discrete; j < end; ++j) {
2,523,574✔
204
    k = j;
2,522,402✔
205
    c_k1 = distribution_[l].c[k + 1];
2,522,402✔
206
    if (r1 < c_k1)
2,522,402✔
207
      break;
208
    k = j + 1;
209
    c_k = c_k1;
210
  }
211

212
  double E_l_k = distribution_[l].e_out[k];
374,254✔
213
  double p_l_k = distribution_[l].p[k];
374,254✔
214
  if (distribution_[l].interpolation == Interpolation::histogram) {
374,254✔
215
    // Histogram interpolation
216
    if (p_l_k > 0.0 && k >= n_discrete) {
24,453!
217
      E_out = E_l_k + (r1 - c_k) / p_l_k;
24,453✔
218
    } else {
UNCOV
219
      E_out = E_l_k;
×
220
    }
221

222
  } else if (distribution_[l].interpolation == Interpolation::lin_lin) {
349,801!
223
    // Linear-linear interpolation
224
    double E_l_k1 = distribution_[l].e_out[k + 1];
349,801!
225
    double p_l_k1 = distribution_[l].p[k + 1];
349,801✔
226

227
    double frac = (p_l_k1 - p_l_k) / (E_l_k1 - E_l_k);
349,801✔
228
    if (frac == 0.0) {
349,801!
UNCOV
229
      E_out = E_l_k + (r1 - c_k) / p_l_k;
×
230
    } else {
231
      E_out =
699,602✔
232
        E_l_k +
349,801✔
233
        (std::sqrt(std::max(0.0, p_l_k * p_l_k + 2.0 * frac * (r1 - c_k))) -
699,602!
234
          p_l_k) /
349,801✔
235
          frac;
236
    }
237
  }
238

239
  // Now interpolate between incident energy bins i and i + 1
240
  if (k >= n_discrete) {
374,254!
241
    if (l == i) {
374,254✔
242
      E_out = E_1 + (E_out - E_i_1) * (E_K - E_1) / (E_i_K - E_i_1);
120,984✔
243
    } else {
244
      E_out = E_1 + (E_out - E_i1_1) * (E_K - E_1) / (E_i1_K - E_i1_1);
253,270✔
245
    }
246
  }
247

248
  // Find correlated angular distribution for closest outgoing energy bin
249
  if (r1 - c_k < c_k1 - r1 ||
374,254✔
250
      distribution_[l].interpolation == Interpolation::histogram) {
185,737✔
251
    return *distribution_[l].angle[k];
201,249✔
252
  } else {
253
    return *distribution_[l].angle[k + 1];
173,005✔
254
  }
255
}
256

257
void CorrelatedAngleEnergy::sample(
374,254✔
258
  double E_in, double& E_out, double& mu, uint64_t* seed) const
259
{
260
  mu = sample_dist(E_in, E_out, seed).sample(seed).first;
374,254✔
261
}
374,254✔
262

UNCOV
263
double CorrelatedAngleEnergy::sample_energy_and_pdf(
×
264
  double E_in, double mu, double& E_out, uint64_t* seed) const
265
{
UNCOV
266
  return sample_dist(E_in, E_out, seed).evaluate(mu);
×
267
}
268

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

© 2026 Coveralls, Inc