forked from openmc-dev/openmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththermal.cpp
318 lines (275 loc) · 10.4 KB
/
thermal.cpp
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include "openmc/thermal.h"
#include <algorithm> // for sort, move, min, max, find
#include <cmath> // for round, sqrt, abs
#include "xtensor/xarray.hpp"
#include "xtensor/xbuilder.hpp"
#include "xtensor/xmath.hpp"
#include "xtensor/xsort.hpp"
#include "xtensor/xtensor.hpp"
#include "xtensor/xview.hpp"
#include <fmt/core.h>
#include "openmc/constants.h"
#include "openmc/endf.h"
#include "openmc/error.h"
#include "openmc/random_lcg.h"
#include "openmc/search.h"
#include "openmc/secondary_correlated.h"
#include "openmc/secondary_thermal.h"
#include "openmc/settings.h"
namespace openmc {
//==============================================================================
// Global variables
//==============================================================================
namespace data {
std::unordered_map<std::string, int> thermal_scatt_map;
vector<unique_ptr<ThermalScattering>> thermal_scatt;
} // namespace data
//==============================================================================
// ThermalScattering implementation
//==============================================================================
ThermalScattering::ThermalScattering(
hid_t group, const vector<double>& temperature)
{
// Get name of table from group
name_ = object_name(group);
// Get rid of leading '/'
name_ = name_.substr(1);
read_attribute(group, "atomic_weight_ratio", awr_);
read_attribute(group, "energy_max", energy_max_);
read_attribute(group, "nuclides", nuclides_);
// Read temperatures
hid_t kT_group = open_group(group, "kTs");
// Determine temperatures available
auto dset_names = dataset_names(kT_group);
auto n = dset_names.size();
auto temps_available = xt::empty<double>({n});
for (int i = 0; i < dset_names.size(); ++i) {
// Read temperature value
double T;
read_dataset(kT_group, dset_names[i].data(), T);
temps_available[i] = T / K_BOLTZMANN;
}
std::sort(temps_available.begin(), temps_available.end());
// Determine actual temperatures to read -- start by checking whether a
// temperature range was given, in which case all temperatures in the range
// are loaded irrespective of what temperatures actually appear in the model
vector<int> temps_to_read;
if (settings::temperature_range[1] > 0.0) {
for (const auto& T : temps_available) {
if (settings::temperature_range[0] <= T &&
T <= settings::temperature_range[1]) {
temps_to_read.push_back(std::round(T));
}
}
}
switch (settings::temperature_method) {
case TemperatureMethod::NEAREST:
// Determine actual temperatures to read
for (const auto& T : temperature) {
auto i_closest = xt::argmin(xt::abs(temps_available - T))[0];
auto temp_actual = temps_available[i_closest];
if (std::abs(temp_actual - T) < settings::temperature_tolerance) {
if (std::find(temps_to_read.begin(), temps_to_read.end(),
std::round(temp_actual)) == temps_to_read.end()) {
temps_to_read.push_back(std::round(temp_actual));
}
} else {
fatal_error(fmt::format("Nuclear data library does not contain cross "
"sections for {} at or near {} K.",
name_, std::round(T)));
}
}
break;
case TemperatureMethod::INTERPOLATION:
// If temperature interpolation or multipole is selected, get a list of
// bounding temperatures for each actual temperature present in the model
for (const auto& T : temperature) {
bool found = false;
for (int j = 0; j < temps_available.size() - 1; ++j) {
if (temps_available[j] <= T && T < temps_available[j + 1]) {
int T_j = std::round(temps_available[j]);
int T_j1 = std::round(temps_available[j + 1]);
if (std::find(temps_to_read.begin(), temps_to_read.end(), T_j) ==
temps_to_read.end()) {
temps_to_read.push_back(T_j);
}
if (std::find(temps_to_read.begin(), temps_to_read.end(), T_j1) ==
temps_to_read.end()) {
temps_to_read.push_back(T_j1);
}
found = true;
}
}
if (!found) {
// If no pairs found, check if the desired temperature falls within
// bounds' tolerance
if (std::abs(T - temps_available[0]) <=
settings::temperature_tolerance) {
if (std::find(temps_to_read.begin(), temps_to_read.end(),
std::round(temps_available[0])) == temps_to_read.end()) {
temps_to_read.push_back(std::round(temps_available[0]));
}
} else if (std::abs(T - temps_available[n - 1]) <=
settings::temperature_tolerance) {
if (std::find(temps_to_read.begin(), temps_to_read.end(),
std::round(temps_available[n - 1])) == temps_to_read.end()) {
temps_to_read.push_back(std::round(temps_available[n - 1]));
}
} else {
fatal_error(
fmt::format("Nuclear data library does not contain cross "
"sections for {} at temperatures that bound {} K.",
name_, std::round(T)));
}
}
}
}
// Sort temperatures to read
std::sort(temps_to_read.begin(), temps_to_read.end());
auto n_temperature = temps_to_read.size();
kTs_.reserve(n_temperature);
data_.reserve(n_temperature);
for (auto T : temps_to_read) {
// Get temperature as a string
std::string temp_str = fmt::format("{}K", T);
// Read exact temperature value
double kT;
read_dataset(kT_group, temp_str.data(), kT);
kTs_.push_back(kT);
// Open group for this temperature
hid_t T_group = open_group(group, temp_str.data());
data_.emplace_back(T_group);
close_group(T_group);
}
close_group(kT_group);
}
void ThermalScattering::calculate_xs(double E, double sqrtkT, int* i_temp,
double* elastic, double* inelastic, uint64_t* seed) const
{
// Determine temperature for S(a,b) table
double kT = sqrtkT * sqrtkT;
int i = 0;
auto n = kTs_.size();
if (n > 1) {
if (settings::temperature_method == TemperatureMethod::NEAREST) {
while (kTs_[i + 1] < kT && i + 1 < n - 1)
++i;
// Pick closer of two bounding temperatures
if (kT - kTs_[i] > kTs_[i + 1] - kT)
++i;
} else {
// If current kT outside of the bounds of available, snap to the bound
if (kT < kTs_.front()) {
i = 0;
} else if (kT > kTs_.back()) {
i = kTs_.size() - 1;
} else {
// Find temperatures that bound the actual temperature
while (kTs_[i + 1] < kT && i + 1 < n - 1)
++i;
// Randomly sample between temperature i and i+1
double f = (kT - kTs_[i]) / (kTs_[i + 1] - kTs_[i]);
if (f > prn(seed))
++i;
}
}
}
// Set temperature index
*i_temp = i;
// Calculate cross sections for ith temperature
data_[i].calculate_xs(E, elastic, inelastic);
}
bool ThermalScattering::has_nuclide(const char* name) const
{
std::string nuc {name};
return std::find(nuclides_.begin(), nuclides_.end(), nuc) != nuclides_.end();
}
//==============================================================================
// ThermalData implementation
//==============================================================================
ThermalData::ThermalData(hid_t group)
{
// Coherent/incoherent elastic data
if (object_exists(group, "elastic")) {
// Read cross section data
hid_t elastic_group = open_group(group, "elastic");
// Read elastic cross section
elastic_.xs = read_function(elastic_group, "xs");
// Read angle-energy distribution
hid_t dgroup = open_group(elastic_group, "distribution");
std::string temp;
read_attribute(dgroup, "type", temp);
if (temp == "coherent_elastic") {
auto xs = dynamic_cast<CoherentElasticXS*>(elastic_.xs.get());
elastic_.distribution = make_unique<CoherentElasticAE>(*xs);
} else if (temp == "incoherent_elastic") {
elastic_.distribution = make_unique<IncoherentElasticAE>(dgroup);
} else if (temp == "incoherent_elastic_discrete") {
auto xs = dynamic_cast<Tabulated1D*>(elastic_.xs.get());
elastic_.distribution =
make_unique<IncoherentElasticAEDiscrete>(dgroup, xs->x());
} else if (temp == "mixed_elastic") {
// Get coherent/incoherent cross sections
auto mixed_xs = dynamic_cast<Sum1D*>(elastic_.xs.get());
const auto& coh_xs =
dynamic_cast<const CoherentElasticXS*>(mixed_xs->functions(0).get());
const auto& incoh_xs = mixed_xs->functions(1).get();
// Create mixed elastic distribution
elastic_.distribution =
make_unique<MixedElasticAE>(dgroup, *coh_xs, *incoh_xs);
}
close_group(elastic_group);
}
// Inelastic data
if (object_exists(group, "inelastic")) {
// Read type of inelastic data
hid_t inelastic_group = open_group(group, "inelastic");
// Read inelastic cross section
inelastic_.xs = read_function(inelastic_group, "xs");
// Read angle-energy distribution
hid_t dgroup = open_group(inelastic_group, "distribution");
std::string temp;
read_attribute(dgroup, "type", temp);
if (temp == "incoherent_inelastic") {
inelastic_.distribution = make_unique<IncoherentInelasticAE>(dgroup);
} else if (temp == "incoherent_inelastic_discrete") {
auto xs = dynamic_cast<Tabulated1D*>(inelastic_.xs.get());
inelastic_.distribution =
make_unique<IncoherentInelasticAEDiscrete>(dgroup, xs->x());
}
close_group(inelastic_group);
}
}
void ThermalData::calculate_xs(
double E, double* elastic, double* inelastic) const
{
// Calculate thermal elastic scattering cross section
if (elastic_.xs) {
*elastic = (*elastic_.xs)(E);
} else {
*elastic = 0.0;
}
// Calculate thermal inelastic scattering cross section
*inelastic = (*inelastic_.xs)(E);
}
void ThermalData::sample(const NuclideMicroXS& micro_xs, double E,
double* E_out, double* mu, uint64_t* seed)
{
// Determine whether inelastic or elastic scattering will occur
if (prn(seed) < micro_xs.thermal_elastic / micro_xs.thermal) {
elastic_.distribution->sample(E, *E_out, *mu, seed);
} else {
inelastic_.distribution->sample(E, *E_out, *mu, seed);
}
// Because of floating-point roundoff, it may be possible for mu to be
// outside of the range [-1,1). In these cases, we just set mu to exactly
// -1 or 1
if (std::abs(*mu) > 1.0)
*mu = std::copysign(1.0, *mu);
}
void free_memory_thermal()
{
data::thermal_scatt.clear();
data::thermal_scatt_map.clear();
}
} // namespace openmc