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

openmc-dev / openmc / 21414796804

27 Jan 2026 09:24PM UTC coverage: 81.976% (-0.02%) from 81.994%
21414796804

Pull #3682

github

web-flow
Merge c34922eb5 into db426b66c
Pull Request #3682: Extend Kalbach Mann systematics to support incident photons

17219 of 23981 branches covered (71.8%)

Branch coverage included in aggregate %.

33 of 44 new or added lines in 4 files covered. (75.0%)

5 existing lines in 1 file now uncovered.

55715 of 64989 relevant lines covered (85.73%)

43993102.55 hits per line

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

84.93
/src/secondary_kalbach.cpp
1
#include "openmc/secondary_kalbach.h"
2

3
#include <algorithm> // for copy, move
4
#include <cmath>     // for log, sqrt, sinh
5
#include <cstddef>   // for size_t
6
#include <iterator>  // for back_inserter
7

8
#include "xtensor/xarray.hpp"
9
#include "xtensor/xview.hpp"
10

11
#include "openmc/hdf5_interface.h"
12
#include "openmc/math_functions.h"
13
#include "openmc/random_dist.h"
14
#include "openmc/random_lcg.h"
15
#include "openmc/search.h"
16
#include "openmc/vector.h"
17

18
namespace openmc {
19

20
//==============================================================================
21
//! KalbachMann implementation
22
//==============================================================================
23

24
KalbachMann::KalbachMann(hid_t group)
68,506✔
25
{
26
  is_photon_ = false;
68,506✔
27
  // Check if projectile is a photon
28
  if (attribute_exists(group, "particle")) {
68,506!
NEW
29
    std::string temp;
×
NEW
30
    read_attribute(group, "particle", temp);
×
NEW
31
    if (temp == "photon")
×
NEW
32
      is_photon_ = true;
×
NEW
33
  }
×
34
  // Open incoming energy dataset
35
  hid_t dset = open_dataset(group, "energy");
68,506✔
36

37
  // Get interpolation parameters
38
  xt::xarray<int> temp;
68,506✔
39
  read_attribute(dset, "interpolation", temp);
68,506✔
40

41
  auto temp_b = xt::view(temp, 0); // view of breakpoints
68,506✔
42
  auto temp_i = xt::view(temp, 1); // view of interpolation parameters
68,506✔
43

44
  std::copy(temp_b.begin(), temp_b.end(), std::back_inserter(breakpoints_));
68,506✔
45
  for (const auto i : temp_i)
137,012✔
46
    interpolation_.push_back(int2interp(i));
68,506✔
47
  n_region_ = breakpoints_.size();
68,506✔
48

49
  // Get incoming energies
50
  read_dataset(dset, energy_);
68,506✔
51
  std::size_t n_energy = energy_.size();
68,506✔
52
  close_dataset(dset);
68,506✔
53

54
  // Get outgoing energy distribution data
55
  dset = open_dataset(group, "distribution");
68,506✔
56
  vector<int> offsets;
68,506✔
57
  vector<int> interp;
68,506✔
58
  vector<int> n_discrete;
68,506✔
59
  read_attribute(dset, "offsets", offsets);
68,506✔
60
  read_attribute(dset, "interpolation", interp);
68,506✔
61
  read_attribute(dset, "n_discrete_lines", n_discrete);
68,506✔
62

63
  xt::xarray<double> eout;
68,506✔
64
  read_dataset(dset, eout);
68,506✔
65
  close_dataset(dset);
68,506✔
66

67
  for (int i = 0; i < n_energy; ++i) {
1,395,139✔
68
    // Determine number of outgoing energies
69
    int j = offsets[i];
1,326,633✔
70
    int n;
71
    if (i < n_energy - 1) {
1,326,633✔
72
      n = offsets[i + 1] - j;
1,258,127✔
73
    } else {
74
      n = eout.shape()[1] - j;
68,506✔
75
    }
76

77
    // Assign interpolation scheme and number of discrete lines
78
    KMTable d;
1,326,633✔
79
    d.interpolation = int2interp(interp[i]);
1,326,633✔
80
    d.n_discrete = n_discrete[i];
1,326,633✔
81

82
    // Copy data
83
    d.e_out = xt::view(eout, 0, xt::range(j, j + n));
1,326,633✔
84
    d.p = xt::view(eout, 1, xt::range(j, j + n));
1,326,633✔
85
    d.c = xt::view(eout, 2, xt::range(j, j + n));
1,326,633✔
86
    d.r = xt::view(eout, 3, xt::range(j, j + n));
1,326,633✔
87
    d.a = xt::view(eout, 4, xt::range(j, j + n));
1,326,633✔
88

89
    // To get answers that match ACE data, for now we still use the tabulated
90
    // CDF values that were passed through to the HDF5 library. At a later
91
    // time, we can remove the CDF values from the HDF5 library and
92
    // reconstruct them using the PDF
93
    if (false) {
94
      // Calculate cumulative distribution function -- discrete portion
95
      for (int k = 0; k < d.n_discrete; ++k) {
96
        if (k == 0) {
97
          d.c[k] = d.p[k];
98
        } else {
99
          d.c[k] = d.c[k - 1] + d.p[k];
100
        }
101
      }
102

103
      // Continuous portion
104
      for (int k = d.n_discrete; k < n; ++k) {
105
        if (k == d.n_discrete) {
106
          d.c[k] = d.c[k - 1] + d.p[k];
107
        } else {
108
          if (d.interpolation == Interpolation::histogram) {
109
            d.c[k] = d.c[k - 1] + d.p[k - 1] * (d.e_out[k] - d.e_out[k - 1]);
110
          } else if (d.interpolation == Interpolation::lin_lin) {
111
            d.c[k] = d.c[k - 1] + 0.5 * (d.p[k - 1] + d.p[k]) *
112
                                    (d.e_out[k] - d.e_out[k - 1]);
113
          }
114
        }
115
      }
116

117
      // Normalize density and distribution functions
118
      d.p /= d.c[n - 1];
119
      d.c /= d.c[n - 1];
120
    }
121

122
    distribution_.push_back(std::move(d));
1,326,633✔
123
  } // incoming energies
1,326,633✔
124
}
68,506✔
125

126
void KalbachMann::sample(
5,069,770✔
127
  double E_in, double& E_out, double& mu, uint64_t* seed) const
128
{
129
  // Find energy bin and calculate interpolation factor
130
  int i;
131
  double r;
132
  get_energy_index(energy_, E_in, i, r);
5,069,770✔
133

134
  // Sample between the ith and [i+1]th bin
135
  int l = r > prn(seed) ? i + 1 : i;
5,069,770✔
136

137
  // Interpolation for energy E1 and EK
138
  int n_energy_out = distribution_[i].e_out.size();
5,069,770✔
139
  int n_discrete = distribution_[i].n_discrete;
5,069,770✔
140
  double E_i_1 = distribution_[i].e_out[n_discrete];
5,069,770✔
141
  double E_i_K = distribution_[i].e_out[n_energy_out - 1];
5,069,770✔
142

143
  n_energy_out = distribution_[i + 1].e_out.size();
5,069,770✔
144
  n_discrete = distribution_[i + 1].n_discrete;
5,069,770✔
145
  double E_i1_1 = distribution_[i + 1].e_out[n_discrete];
5,069,770✔
146
  double E_i1_K = distribution_[i + 1].e_out[n_energy_out - 1];
5,069,770✔
147

148
  double E_1 = E_i_1 + r * (E_i1_1 - E_i_1);
5,069,770✔
149
  double E_K = E_i_K + r * (E_i1_K - E_i_K);
5,069,770✔
150

151
  // Determine outgoing energy bin
152
  n_energy_out = distribution_[l].e_out.size();
5,069,770✔
153
  n_discrete = distribution_[l].n_discrete;
5,069,770✔
154
  double r1 = prn(seed);
5,069,770✔
155
  double c_k = distribution_[l].c[0];
5,069,770✔
156
  int k = 0;
5,069,770✔
157
  int end = n_energy_out - 2;
5,069,770✔
158

159
  // Discrete portion
160
  for (int j = 0; j < n_discrete; ++j) {
5,069,770!
UNCOV
161
    k = j;
×
UNCOV
162
    c_k = distribution_[l].c[k];
×
163
    if (r1 < c_k) {
×
164
      end = j;
×
165
      break;
×
166
    }
167
  }
168

169
  // Continuous portion
170
  double c_k1;
171
  for (int j = n_discrete; j < end; ++j) {
128,292,900✔
172
    k = j;
128,097,566✔
173
    c_k1 = distribution_[l].c[k + 1];
128,097,566✔
174
    if (r1 < c_k1)
128,097,566✔
175
      break;
4,874,436✔
176
    k = j + 1;
123,223,130✔
177
    c_k = c_k1;
123,223,130✔
178
  }
179

180
  double E_l_k = distribution_[l].e_out[k];
5,069,770✔
181
  double p_l_k = distribution_[l].p[k];
5,069,770✔
182
  double km_r, km_a;
183
  if (distribution_[l].interpolation == Interpolation::histogram) {
5,069,770✔
184
    // Histogram interpolation
185
    if (p_l_k > 0.0 && k >= n_discrete) {
5,050,014!
186
      E_out = E_l_k + (r1 - c_k) / p_l_k;
5,050,014✔
187
    } else {
UNCOV
188
      E_out = E_l_k;
×
189
    }
190

191
    // Determine Kalbach-Mann parameters
192
    km_r = distribution_[l].r[k];
5,050,014✔
193
    km_a = distribution_[l].a[k];
5,050,014✔
194

195
  } else {
196
    // Linear-linear interpolation
197
    double E_l_k1 = distribution_[l].e_out[k + 1];
19,756✔
198
    double p_l_k1 = distribution_[l].p[k + 1];
19,756✔
199

200
    double frac = (p_l_k1 - p_l_k) / (E_l_k1 - E_l_k);
19,756✔
201
    if (frac == 0.0) {
19,756!
UNCOV
202
      E_out = E_l_k + (r1 - c_k) / p_l_k;
×
203
    } else {
204
      E_out =
19,756✔
205
        E_l_k +
19,756✔
206
        (std::sqrt(std::max(0.0, p_l_k * p_l_k + 2.0 * frac * (r1 - c_k))) -
19,756✔
207
          p_l_k) /
19,756✔
208
          frac;
209
    }
210

211
    // Determine Kalbach-Mann parameters
212
    km_r = distribution_[l].r[k] +
19,756✔
213
           (E_out - E_l_k) / (E_l_k1 - E_l_k) *
39,512✔
214
             (distribution_[l].r[k + 1] - distribution_[l].r[k]);
19,756✔
215
    km_a = distribution_[l].a[k] +
19,756✔
216
           (E_out - E_l_k) / (E_l_k1 - E_l_k) *
39,512✔
217
             (distribution_[l].a[k + 1] - distribution_[l].a[k]);
19,756✔
218
  }
219

220
  // Now interpolate between incident energy bins i and i + 1
221
  if (k >= n_discrete) {
5,069,770!
222
    if (l == i) {
5,069,770✔
223
      E_out = E_1 + (E_out - E_i_1) * (E_K - E_1) / (E_i_K - E_i_1);
2,523,461✔
224
    } else {
225
      E_out = E_1 + (E_out - E_i1_1) * (E_K - E_1) / (E_i1_K - E_i1_1);
2,546,309✔
226
    }
227
  }
228

229
  // Sampled correlated angle from Kalbach-Mann parameters
230
  if (is_photon_) {
5,069,770!
NEW
UNCOV
231
    double T = uniform_distribution(0., 1., seed);
×
NEW
232
    double sinha = std::sinh(km_a);
×
NEW
233
    mu = sinha * ((1 + T) - 2 * km_r) /
×
NEW
234
         (sinha * T + std::cosh(km_a) - std::exp(km_a * T));
×
235
  } else {
236
    if (prn(seed) > km_r) {
5,069,770✔
237
      double T = uniform_distribution(-1., 1., seed) * std::sinh(km_a);
4,988,392✔
238
      mu = std::log(T + std::sqrt(T * T + 1.0)) / km_a;
4,988,392✔
239
    } else {
240
      double r1 = prn(seed);
81,378✔
241
      mu = std::log(r1 * std::exp(km_a) + (1.0 - r1) * std::exp(-km_a)) / km_a;
81,378✔
242
    }
243
  }
244
}
5,069,770✔
245

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