-
Notifications
You must be signed in to change notification settings - Fork 649
Expand file tree
/
Copy pathsecondary_correlated.cpp
More file actions
274 lines (231 loc) · 8.29 KB
/
secondary_correlated.cpp
File metadata and controls
274 lines (231 loc) · 8.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "openmc/secondary_correlated.h"
#include <algorithm> // for copy
#include <cmath>
#include <cstddef> // for size_t
#include <iterator> // for back_inserter
#include "openmc/tensor.h"
#include "openmc/endf.h"
#include "openmc/hdf5_interface.h"
#include "openmc/math_functions.h"
#include "openmc/random_lcg.h"
#include "openmc/search.h"
namespace openmc {
//==============================================================================
//! CorrelatedAngleEnergy implementation
//==============================================================================
CorrelatedAngleEnergy::CorrelatedAngleEnergy(hid_t group)
{
// Open incoming energy dataset
hid_t dset = open_dataset(group, "energy");
// Get interpolation parameters
tensor::Tensor<int> temp;
read_attribute(dset, "interpolation", temp);
tensor::View<int> temp_b = temp.slice(0); // breakpoints
tensor::View<int> temp_i = temp.slice(1); // interpolation parameters
std::copy(temp_b.begin(), temp_b.end(), std::back_inserter(breakpoints_));
for (const auto i : temp_i)
interpolation_.push_back(int2interp(i));
n_region_ = breakpoints_.size();
// Get incoming energies
read_dataset(dset, energy_);
std::size_t n_energy = energy_.size();
close_dataset(dset);
// Get outgoing energy distribution data
dset = open_dataset(group, "energy_out");
vector<int> offsets;
vector<int> interp;
vector<int> n_discrete;
read_attribute(dset, "offsets", offsets);
read_attribute(dset, "interpolation", interp);
read_attribute(dset, "n_discrete_lines", n_discrete);
tensor::Tensor<double> eout;
read_dataset(dset, eout);
close_dataset(dset);
// Read angle distributions
tensor::Tensor<double> mu;
read_dataset(group, "mu", mu);
for (int i = 0; i < n_energy; ++i) {
// Determine number of outgoing energies
int j = offsets[i];
int n;
if (i < n_energy - 1) {
n = offsets[i + 1] - j;
} else {
n = eout.shape(1) - j;
}
// Assign interpolation scheme and number of discrete lines
CorrTable d;
d.interpolation = int2interp(interp[i]);
d.n_discrete = n_discrete[i];
// Copy data
d.e_out = eout.slice(0, tensor::range(j, j + n));
d.p = eout.slice(1, tensor::range(j, j + n));
d.c = eout.slice(2, tensor::range(j, j + n));
// To get answers that match ACE data, for now we still use the tabulated
// CDF values that were passed through to the HDF5 library. At a later
// time, we can remove the CDF values from the HDF5 library and
// reconstruct them using the PDF
if (false) {
// Calculate cumulative distribution function -- discrete portion
for (int k = 0; k < d.n_discrete; ++k) {
if (k == 0) {
d.c[k] = d.p[k];
} else {
d.c[k] = d.c[k - 1] + d.p[k];
}
}
// Continuous portion
for (int k = d.n_discrete; k < n; ++k) {
if (k == d.n_discrete) {
d.c[k] = d.c[k - 1] + d.p[k];
} else {
if (d.interpolation == Interpolation::histogram) {
d.c[k] = d.c[k - 1] + d.p[k - 1] * (d.e_out[k] - d.e_out[k - 1]);
} else if (d.interpolation == Interpolation::lin_lin) {
d.c[k] = d.c[k - 1] + 0.5 * (d.p[k - 1] + d.p[k]) *
(d.e_out[k] - d.e_out[k - 1]);
}
}
}
// Normalize density and distribution functions
d.p /= d.c[n - 1];
d.c /= d.c[n - 1];
}
for (j = 0; j < n; ++j) {
// Get interpolation scheme
int interp_mu = std::lround(eout(3, offsets[i] + j));
// Determine offset and size of distribution
int offset_mu = std::lround(eout(4, offsets[i] + j));
int m;
if (offsets[i] + j + 1 < eout.shape(1)) {
m = std::lround(eout(4, offsets[i] + j + 1)) - offset_mu;
} else {
m = mu.shape(1) - offset_mu;
}
// For incoherent inelastic thermal scattering, the angle distributions
// may be given as discrete mu values. In this case, interpolation values
// of zero appear in the HDF5 file. Here we change it to a 1 so that
// int2interp doesn't fail.
if (interp_mu == 0)
interp_mu = 1;
auto interp = int2interp(interp_mu);
tensor::View<double> xs =
mu.slice(0, tensor::range(offset_mu, offset_mu + m));
tensor::View<double> ps =
mu.slice(1, tensor::range(offset_mu, offset_mu + m));
tensor::View<double> cs =
mu.slice(2, tensor::range(offset_mu, offset_mu + m));
vector<double> x {xs.begin(), xs.end()};
vector<double> p {ps.begin(), ps.end()};
vector<double> c {cs.begin(), cs.end()};
// To get answers that match ACE data, for now we still use the tabulated
// CDF values that were passed through to the HDF5 library. At a later
// time, we can remove the CDF values from the HDF5 library and
// reconstruct them using the PDF
Tabular* mudist = new Tabular {x.data(), p.data(), m, interp, c.data()};
d.angle.emplace_back(mudist);
} // outgoing energies
distribution_.push_back(std::move(d));
} // incoming energies
}
Distribution& CorrelatedAngleEnergy::sample_dist(
double E_in, double& E_out, uint64_t* seed) const
{
// Find energy bin and calculate interpolation factor
int i;
double r;
get_energy_index(energy_, E_in, i, r);
// Sample between the ith and [i+1]th bin
int l = r > prn(seed) ? i + 1 : i;
// Interpolation for energy E1 and EK
int n_energy_out = distribution_[i].e_out.size();
int n_discrete = distribution_[i].n_discrete;
double E_i_1 = distribution_[i].e_out[n_discrete];
double E_i_K = distribution_[i].e_out[n_energy_out - 1];
n_energy_out = distribution_[i + 1].e_out.size();
n_discrete = distribution_[i + 1].n_discrete;
double E_i1_1 = distribution_[i + 1].e_out[n_discrete];
double E_i1_K = distribution_[i + 1].e_out[n_energy_out - 1];
double E_1 = E_i_1 + r * (E_i1_1 - E_i_1);
double E_K = E_i_K + r * (E_i1_K - E_i_K);
// Determine outgoing energy bin
n_energy_out = distribution_[l].e_out.size();
n_discrete = distribution_[l].n_discrete;
double r1 = prn(seed);
double c_k = distribution_[l].c[0];
int k = 0;
int end = n_energy_out - 2;
// Discrete portion
for (int j = 0; j < n_discrete; ++j) {
k = j;
c_k = distribution_[l].c[k];
if (r1 < c_k) {
end = j;
break;
}
}
// Continuous portion
double c_k1;
for (int j = n_discrete; j < end; ++j) {
k = j;
c_k1 = distribution_[l].c[k + 1];
if (r1 < c_k1)
break;
k = j + 1;
c_k = c_k1;
}
double E_l_k = distribution_[l].e_out[k];
double p_l_k = distribution_[l].p[k];
if (distribution_[l].interpolation == Interpolation::histogram) {
// Histogram interpolation
if (p_l_k > 0.0 && k >= n_discrete) {
E_out = E_l_k + (r1 - c_k) / p_l_k;
} else {
E_out = E_l_k;
}
} else if (distribution_[l].interpolation == Interpolation::lin_lin) {
// Linear-linear interpolation
double E_l_k1 = distribution_[l].e_out[k + 1];
double p_l_k1 = distribution_[l].p[k + 1];
double frac = (p_l_k1 - p_l_k) / (E_l_k1 - E_l_k);
if (frac == 0.0) {
E_out = E_l_k + (r1 - c_k) / p_l_k;
} else {
E_out =
E_l_k +
(std::sqrt(std::max(0.0, p_l_k * p_l_k + 2.0 * frac * (r1 - c_k))) -
p_l_k) /
frac;
}
}
// Now interpolate between incident energy bins i and i + 1
if (k >= n_discrete) {
if (l == i) {
E_out = E_1 + (E_out - E_i_1) * (E_K - E_1) / (E_i_K - E_i_1);
} else {
E_out = E_1 + (E_out - E_i1_1) * (E_K - E_1) / (E_i1_K - E_i1_1);
}
}
// Find correlated angular distribution for closest outgoing energy bin
if (r1 - c_k < c_k1 - r1 ||
distribution_[l].interpolation == Interpolation::histogram) {
return *distribution_[l].angle[k];
} else {
return *distribution_[l].angle[k + 1];
}
}
void CorrelatedAngleEnergy::sample(
double E_in, double& E_out, double& mu, uint64_t* seed) const
{
mu = sample_dist(E_in, E_out, seed).sample(seed).first;
}
double CorrelatedAngleEnergy::sample_energy_and_pdf(double E_in, double mu,
double& E_out, uint64_t* seed, bool is_com, double awr) const
{
auto& dist = sample_dist(E_in, E_out, seed);
double jac = 1.0;
if (is_com)
jac = get_jac_and_transform(E_in, mu, E_out, seed, awr);
return jac * dist.evaluate(mu);
}
} // namespace openmc