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

openmc-dev / openmc / 22109060455

17 Feb 2026 05:38PM UTC coverage: 81.693% (-0.03%) from 81.721%
22109060455

Pull #3813

github

web-flow
Merge d2d368c60 into 977ade79a
Pull Request #3813: Check for positive radii

16733 of 23304 branches covered (71.8%)

Branch coverage included in aggregate %.

4 of 11 new or added lines in 1 file covered. (36.36%)

1361 existing lines in 48 files now uncovered.

56648 of 66521 relevant lines covered (85.16%)

43301606.31 hits per line

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

92.75
/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)
39,236✔
23
{
24
  // Open incoming energy dataset
25
  hid_t dset = open_dataset(group, "energy");
39,236✔
26

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

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

34
  std::copy(temp_b.begin(), temp_b.end(), std::back_inserter(breakpoints_));
39,236✔
35
  for (const auto i : temp_i)
78,472✔
36
    interpolation_.push_back(int2interp(i));
39,236✔
37
  n_region_ = breakpoints_.size();
39,236✔
38

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

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

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

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

61
  for (int i = 0; i < n_energy; ++i) {
807,958✔
62
    // Determine number of outgoing energies
63
    int j = offsets[i];
768,722✔
64
    int n;
65
    if (i < n_energy - 1) {
768,722✔
66
      n = offsets[i + 1] - j;
729,486✔
67
    } else {
68
      n = eout.shape(1) - j;
39,236✔
69
    }
70

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

76
    // Copy data
77
    d.e_out = eout.slice(0, tensor::range(j, j + n));
768,722✔
78
    d.p = eout.slice(1, tensor::range(j, j + n));
768,722✔
79
    d.c = eout.slice(2, tensor::range(j, j + n));
768,722✔
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) {
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) {
18,180,538✔
115
      // Get interpolation scheme
116
      int interp_mu = std::lround(eout(3, offsets[i] + j));
17,411,816✔
117

118
      // Determine offset and size of distribution
119
      int offset_mu = std::lround(eout(4, offsets[i] + j));
17,411,816✔
120
      int m;
121
      if (offsets[i] + j + 1 < eout.shape(1)) {
17,411,816✔
122
        m = std::lround(eout(4, offsets[i] + j + 1)) - offset_mu;
17,372,580✔
123
      } else {
124
        m = mu.shape(1) - offset_mu;
39,236✔
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)
17,411,816✔
132
        interp_mu = 1;
432✔
133

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

142
      vector<double> x {xs.begin(), xs.end()};
17,411,816✔
143
      vector<double> p {ps.begin(), ps.end()};
17,411,816✔
144
      vector<double> c {cs.begin(), cs.end()};
17,411,816✔
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()};
17,411,816✔
151

152
      d.angle.emplace_back(mudist);
17,411,816✔
153
    } // outgoing energies
17,411,816✔
154

155
    distribution_.push_back(std::move(d));
768,722✔
156
  } // incoming energies
768,722✔
157
}
39,236✔
158

159
void CorrelatedAngleEnergy::sample(
295,404✔
160
  double E_in, double& E_out, double& mu, uint64_t* seed) const
161
{
162
  // Find energy bin and calculate interpolation factor
163
  int i;
164
  double r;
165
  get_energy_index(energy_, E_in, i, r);
295,404✔
166

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

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

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

181
  double E_1 = E_i_1 + r * (E_i1_1 - E_i_1);
295,404✔
182
  double E_K = E_i_K + r * (E_i1_K - E_i_K);
295,404✔
183

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

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

202
  // Continuous portion
203
  double c_k1;
204
  for (int j = n_discrete; j < end; ++j) {
1,980,046✔
205
    k = j;
1,979,107✔
206
    c_k1 = distribution_[l].c[k + 1];
1,979,107✔
207
    if (r1 < c_k1)
1,979,107✔
208
      break;
294,465✔
209
    k = j + 1;
1,684,642✔
210
    c_k = c_k1;
1,684,642✔
211
  }
212

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

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

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

240
  // Now interpolate between incident energy bins i and i + 1
241
  if (k >= n_discrete) {
295,404!
242
    if (l == i) {
295,404✔
243
      E_out = E_1 + (E_out - E_i_1) * (E_K - E_1) / (E_i_K - E_i_1);
94,060✔
244
    } else {
245
      E_out = E_1 + (E_out - E_i1_1) * (E_K - E_1) / (E_i1_K - E_i1_1);
201,344✔
246
    }
247
  }
248

249
  // Find correlated angular distribution for closest outgoing energy bin
250
  if (r1 - c_k < c_k1 - r1 ||
442,113✔
251
      distribution_[l].interpolation == Interpolation::histogram) {
146,709✔
252
    mu = distribution_[l].angle[k]->sample(seed).first;
159,009✔
253
  } else {
254
    mu = distribution_[l].angle[k + 1]->sample(seed).first;
136,395✔
255
  }
256
}
295,404✔
257

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