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

openmc-dev / openmc / 17361657497

31 Aug 2025 07:54PM UTC coverage: 84.729%. First build
17361657497

Pull #3550

github

web-flow
Merge 98ca32e81 into 00edc7769
Pull Request #3550: [Point Detector] Add distribution get_pdf functionality

7 of 356 new or added lines in 10 files covered. (1.97%)

52947 of 62490 relevant lines covered (84.73%)

38803853.85 hits per line

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

56.11
/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/nuclide.h"
13
#include "openmc/particle.h"
14
#include "openmc/random_dist.h"
15
#include "openmc/random_lcg.h"
16
#include "openmc/search.h"
17
#include "openmc/tallies/tally_scoring.h"
18
#include "openmc/vector.h"
19

20
namespace openmc {
21

22
//==============================================================================
23
//! KalbachMann implementation
24
//==============================================================================
25

26
KalbachMann::KalbachMann(hid_t group)
67,772✔
27
{
28
  // Open incoming energy dataset
29
  hid_t dset = open_dataset(group, "energy");
67,772✔
30

31
  // Get interpolation parameters
32
  xt::xarray<int> temp;
67,772✔
33
  read_attribute(dset, "interpolation", temp);
67,772✔
34

35
  auto temp_b = xt::view(temp, 0); // view of breakpoints
67,772✔
36
  auto temp_i = xt::view(temp, 1); // view of interpolation parameters
67,772✔
37

38
  std::copy(temp_b.begin(), temp_b.end(), std::back_inserter(breakpoints_));
67,772✔
39
  for (const auto i : temp_i)
135,544✔
40
    interpolation_.push_back(int2interp(i));
67,772✔
41
  n_region_ = breakpoints_.size();
67,772✔
42

43
  // Get incoming energies
44
  read_dataset(dset, energy_);
67,772✔
45
  std::size_t n_energy = energy_.size();
67,772✔
46
  close_dataset(dset);
67,772✔
47

48
  // Get outgoing energy distribution data
49
  dset = open_dataset(group, "distribution");
67,772✔
50
  vector<int> offsets;
67,772✔
51
  vector<int> interp;
67,772✔
52
  vector<int> n_discrete;
67,772✔
53
  read_attribute(dset, "offsets", offsets);
67,772✔
54
  read_attribute(dset, "interpolation", interp);
67,772✔
55
  read_attribute(dset, "n_discrete_lines", n_discrete);
67,772✔
56

57
  xt::xarray<double> eout;
67,772✔
58
  read_dataset(dset, eout);
67,772✔
59
  close_dataset(dset);
67,772✔
60

61
  for (int i = 0; i < n_energy; ++i) {
1,381,460✔
62
    // Determine number of outgoing energies
63
    int j = offsets[i];
1,313,688✔
64
    int n;
65
    if (i < n_energy - 1) {
1,313,688✔
66
      n = offsets[i + 1] - j;
1,245,916✔
67
    } else {
68
      n = eout.shape()[1] - j;
67,772✔
69
    }
70

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

76
    // Copy data
77
    d.e_out = xt::view(eout, 0, xt::range(j, j + n));
1,313,688✔
78
    d.p = xt::view(eout, 1, xt::range(j, j + n));
1,313,688✔
79
    d.c = xt::view(eout, 2, xt::range(j, j + n));
1,313,688✔
80
    d.r = xt::view(eout, 3, xt::range(j, j + n));
1,313,688✔
81
    d.a = xt::view(eout, 4, xt::range(j, j + n));
1,313,688✔
82

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

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

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

116
    distribution_.push_back(std::move(d));
1,313,688✔
117
  } // incoming energies
1,313,688✔
118
}
67,772✔
119

120
void KalbachMann::sample(
3,574,873✔
121
  double E_in, double& E_out, double& mu, uint64_t* seed) const
122
{
123
  // Find energy bin and calculate interpolation factor -- if the energy is
124
  // outside the range of the tabulated energies, choose the first or last bins
125
  auto n_energy_in = energy_.size();
3,574,873✔
126
  int i;
127
  double r;
128
  if (E_in < energy_[0]) {
3,574,873✔
129
    i = 0;
×
130
    r = 0.0;
×
131
  } else if (E_in > energy_[n_energy_in - 1]) {
3,574,873✔
132
    i = n_energy_in - 2;
×
133
    r = 1.0;
×
134
  } else {
135
    i = lower_bound_index(energy_.begin(), energy_.end(), E_in);
3,574,873✔
136
    r = (E_in - energy_[i]) / (energy_[i + 1] - energy_[i]);
3,574,873✔
137
  }
138

139
  // Sample between the ith and [i+1]th bin
140
  int l = r > prn(seed) ? i + 1 : i;
3,574,873✔
141

142
  // Interpolation for energy E1 and EK
143
  int n_energy_out = distribution_[i].e_out.size();
3,574,873✔
144
  int n_discrete = distribution_[i].n_discrete;
3,574,873✔
145
  double E_i_1 = distribution_[i].e_out[n_discrete];
3,574,873✔
146
  double E_i_K = distribution_[i].e_out[n_energy_out - 1];
3,574,873✔
147

148
  n_energy_out = distribution_[i + 1].e_out.size();
3,574,873✔
149
  n_discrete = distribution_[i + 1].n_discrete;
3,574,873✔
150
  double E_i1_1 = distribution_[i + 1].e_out[n_discrete];
3,574,873✔
151
  double E_i1_K = distribution_[i + 1].e_out[n_energy_out - 1];
3,574,873✔
152

153
  double E_1 = E_i_1 + r * (E_i1_1 - E_i_1);
3,574,873✔
154
  double E_K = E_i_K + r * (E_i1_K - E_i_K);
3,574,873✔
155

156
  // Determine outgoing energy bin
157
  n_energy_out = distribution_[l].e_out.size();
3,574,873✔
158
  n_discrete = distribution_[l].n_discrete;
3,574,873✔
159
  double r1 = prn(seed);
3,574,873✔
160
  double c_k = distribution_[l].c[0];
3,574,873✔
161
  int k = 0;
3,574,873✔
162
  int end = n_energy_out - 2;
3,574,873✔
163

164
  // Discrete portion
165
  for (int j = 0; j < n_discrete; ++j) {
3,574,873✔
166
    k = j;
×
167
    c_k = distribution_[l].c[k];
×
168
    if (r1 < c_k) {
×
169
      end = j;
×
170
      break;
×
171
    }
172
  }
173

174
  // Continuous portion
175
  double c_k1;
176
  for (int j = n_discrete; j < end; ++j) {
100,390,135✔
177
    k = j;
100,272,658✔
178
    c_k1 = distribution_[l].c[k + 1];
100,272,658✔
179
    if (r1 < c_k1)
100,272,658✔
180
      break;
3,457,396✔
181
    k = j + 1;
96,815,262✔
182
    c_k = c_k1;
96,815,262✔
183
  }
184

185
  double E_l_k = distribution_[l].e_out[k];
3,574,873✔
186
  double p_l_k = distribution_[l].p[k];
3,574,873✔
187
  double km_r, km_a;
188
  if (distribution_[l].interpolation == Interpolation::histogram) {
3,574,873✔
189
    // Histogram interpolation
190
    if (p_l_k > 0.0 && k >= n_discrete) {
3,554,545✔
191
      E_out = E_l_k + (r1 - c_k) / p_l_k;
3,554,545✔
192
    } else {
193
      E_out = E_l_k;
×
194
    }
195

196
    // Determine Kalbach-Mann parameters
197
    km_r = distribution_[l].r[k];
3,554,545✔
198
    km_a = distribution_[l].a[k];
3,554,545✔
199

200
  } else {
201
    // Linear-linear interpolation
202
    double E_l_k1 = distribution_[l].e_out[k + 1];
20,328✔
203
    double p_l_k1 = distribution_[l].p[k + 1];
20,328✔
204

205
    double frac = (p_l_k1 - p_l_k) / (E_l_k1 - E_l_k);
20,328✔
206
    if (frac == 0.0) {
20,328✔
207
      E_out = E_l_k + (r1 - c_k) / p_l_k;
×
208
    } else {
209
      E_out =
20,328✔
210
        E_l_k +
20,328✔
211
        (std::sqrt(std::max(0.0, p_l_k * p_l_k + 2.0 * frac * (r1 - c_k))) -
20,328✔
212
          p_l_k) /
20,328✔
213
          frac;
214
    }
215

216
    // Determine Kalbach-Mann parameters
217
    km_r = distribution_[l].r[k] +
20,328✔
218
           (E_out - E_l_k) / (E_l_k1 - E_l_k) *
40,656✔
219
             (distribution_[l].r[k + 1] - distribution_[l].r[k]);
20,328✔
220
    km_a = distribution_[l].a[k] +
20,328✔
221
           (E_out - E_l_k) / (E_l_k1 - E_l_k) *
40,656✔
222
             (distribution_[l].a[k + 1] - distribution_[l].a[k]);
20,328✔
223
  }
224

225
  // Now interpolate between incident energy bins i and i + 1
226
  if (k >= n_discrete) {
3,574,873✔
227
    if (l == i) {
3,574,873✔
228
      E_out = E_1 + (E_out - E_i_1) * (E_K - E_1) / (E_i_K - E_i_1);
1,792,248✔
229
    } else {
230
      E_out = E_1 + (E_out - E_i1_1) * (E_K - E_1) / (E_i1_K - E_i1_1);
1,782,625✔
231
    }
232
  }
233

234
  // Sampled correlated angle from Kalbach-Mann parameters
235
  if (prn(seed) > km_r) {
3,574,873✔
236
    double T = uniform_distribution(-1., 1., seed) * std::sinh(km_a);
3,500,368✔
237
    mu = std::log(T + std::sqrt(T * T + 1.0)) / km_a;
3,500,368✔
238
  } else {
239
    double r1 = prn(seed);
74,505✔
240
    mu = std::log(r1 * std::exp(km_a) + (1.0 - r1) * std::exp(-km_a)) / km_a;
74,505✔
241
  }
242
}
3,574,873✔
243

NEW
244
void KalbachMann::get_pdf(double det_pos[4], double E_in, double& E_out,
×
245
  uint64_t* seed, Particle& p, std::vector<double>& mu_cm,
246
  std::vector<double>& Js, std::vector<Particle>& ghost_particles,
247
  std::vector<double>& pdfs_lab) const
248
{
249
  // Find energy bin and calculate interpolation factor -- if the energy is
250
  // outside the range of the tabulated energies, choose the first or last bins
NEW
251
  const auto& nuc {data::nuclides[p.event_nuclide()]};
×
252

253
  // double E_out;
NEW
254
  auto n_energy_in = energy_.size();
×
255
  int i;
256
  double r;
NEW
257
  if (E_in < energy_[0]) {
×
NEW
258
    i = 0;
×
NEW
259
    r = 0.0;
×
NEW
260
  } else if (E_in > energy_[n_energy_in - 1]) {
×
NEW
261
    i = n_energy_in - 2;
×
NEW
262
    r = 1.0;
×
263
  } else {
NEW
264
    i = lower_bound_index(energy_.begin(), energy_.end(), E_in);
×
NEW
265
    r = (E_in - energy_[i]) / (energy_[i + 1] - energy_[i]);
×
266
  }
267

268
  // Sample between the ith and [i+1]th bin
NEW
269
  int l = r > prn(seed) ? i + 1 : i;
×
270

271
  // Interpolation for energy E1 and EK
NEW
272
  int n_energy_out = distribution_[i].e_out.size();
×
NEW
273
  int n_discrete = distribution_[i].n_discrete;
×
NEW
274
  double E_i_1 = distribution_[i].e_out[n_discrete];
×
NEW
275
  double E_i_K = distribution_[i].e_out[n_energy_out - 1];
×
276

NEW
277
  n_energy_out = distribution_[i + 1].e_out.size();
×
NEW
278
  n_discrete = distribution_[i + 1].n_discrete;
×
NEW
279
  double E_i1_1 = distribution_[i + 1].e_out[n_discrete];
×
NEW
280
  double E_i1_K = distribution_[i + 1].e_out[n_energy_out - 1];
×
281

NEW
282
  double E_1 = E_i_1 + r * (E_i1_1 - E_i_1);
×
NEW
283
  double E_K = E_i_K + r * (E_i1_K - E_i_K);
×
284

285
  // Determine outgoing energy bin
NEW
286
  n_energy_out = distribution_[l].e_out.size();
×
NEW
287
  n_discrete = distribution_[l].n_discrete;
×
NEW
288
  double r1 = prn(seed);
×
NEW
289
  double c_k = distribution_[l].c[0];
×
NEW
290
  int k = 0;
×
NEW
291
  int end = n_energy_out - 2;
×
292

293
  // Discrete portion
NEW
294
  for (int j = 0; j < n_discrete; ++j) {
×
NEW
295
    k = j;
×
NEW
296
    c_k = distribution_[l].c[k];
×
NEW
297
    if (r1 < c_k) {
×
NEW
298
      end = j;
×
NEW
299
      break;
×
300
    }
301
  }
302

303
  // Continuous portion
304
  double c_k1;
NEW
305
  for (int j = n_discrete; j < end; ++j) {
×
NEW
306
    k = j;
×
NEW
307
    c_k1 = distribution_[l].c[k + 1];
×
NEW
308
    if (r1 < c_k1)
×
NEW
309
      break;
×
NEW
310
    k = j + 1;
×
NEW
311
    c_k = c_k1;
×
312
  }
313

NEW
314
  double E_l_k = distribution_[l].e_out[k];
×
NEW
315
  double p_l_k = distribution_[l].p[k];
×
316
  double km_r, km_a;
NEW
317
  if (distribution_[l].interpolation == Interpolation::histogram) {
×
318
    // Histogram interpolation
NEW
319
    if (p_l_k > 0.0 && k >= n_discrete) {
×
NEW
320
      E_out = E_l_k + (r1 - c_k) / p_l_k;
×
321
    } else {
NEW
322
      E_out = E_l_k;
×
323
    }
324
    // Determine Kalbach-Mann parameters
NEW
325
    km_r = distribution_[l].r[k];
×
NEW
326
    km_a = distribution_[l].a[k];
×
327

328
  } else {
329
    // Linear-linear interpolation
NEW
330
    double E_l_k1 = distribution_[l].e_out[k + 1];
×
NEW
331
    double p_l_k1 = distribution_[l].p[k + 1];
×
332

NEW
333
    double frac = (p_l_k1 - p_l_k) / (E_l_k1 - E_l_k);
×
NEW
334
    if (frac == 0.0) {
×
NEW
335
      E_out = E_l_k + (r1 - c_k) / p_l_k;
×
336
    } else {
NEW
337
      E_out =
×
NEW
338
        E_l_k +
×
NEW
339
        (std::sqrt(std::max(0.0, p_l_k * p_l_k + 2.0 * frac * (r1 - c_k))) -
×
NEW
340
          p_l_k) /
×
341
          frac;
342
    }
343
    //  Linear-linear interpolation
344
    // Determine Kalbach-Mann parameters
NEW
345
    km_r = distribution_[l].r[k] +
×
NEW
346
           (E_out - E_l_k) / (E_l_k1 - E_l_k) *
×
NEW
347
             (distribution_[l].r[k + 1] - distribution_[l].r[k]);
×
NEW
348
    km_a = distribution_[l].a[k] +
×
NEW
349
           (E_out - E_l_k) / (E_l_k1 - E_l_k) *
×
NEW
350
             (distribution_[l].a[k + 1] - distribution_[l].a[k]);
×
351
  }
352

353
  // Now interpolate between incident energy bins i and i + 1
NEW
354
  if (k >= n_discrete) {
×
NEW
355
    if (l == i) {
×
NEW
356
      E_out = E_1 + (E_out - E_i_1) * (E_K - E_1) / (E_i_K - E_i_1);
×
357
    } else {
NEW
358
      E_out = E_1 + (E_out - E_i1_1) * (E_K - E_1) / (E_i1_K - E_i1_1);
×
359
    }
360
  }
361
}
362

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