forked from openmc-dev/openmc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdistribution_spatial.cpp
345 lines (300 loc) · 10.8 KB
/
distribution_spatial.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "openmc/distribution_spatial.h"
#include "openmc/error.h"
#include "openmc/mesh.h"
#include "openmc/random_lcg.h"
#include "openmc/search.h"
#include "openmc/xml_interface.h"
namespace openmc {
//==============================================================================
// SpatialDistribution implementation
//==============================================================================
unique_ptr<SpatialDistribution> SpatialDistribution::create(pugi::xml_node node)
{
// Check for type of spatial distribution and read
std::string type;
if (check_for_node(node, "type"))
type = get_node_value(node, "type", true, true);
if (type == "cartesian") {
return UPtrSpace {new CartesianIndependent(node)};
} else if (type == "cylindrical") {
return UPtrSpace {new CylindricalIndependent(node)};
} else if (type == "spherical") {
return UPtrSpace {new SphericalIndependent(node)};
} else if (type == "mesh") {
return UPtrSpace {new MeshSpatial(node)};
} else if (type == "box") {
return UPtrSpace {new SpatialBox(node)};
} else if (type == "fission") {
return UPtrSpace {new SpatialBox(node, true)};
} else if (type == "point") {
return UPtrSpace {new SpatialPoint(node)};
} else {
fatal_error(fmt::format(
"Invalid spatial distribution for external source: {}", type));
}
}
//==============================================================================
// CartesianIndependent implementation
//==============================================================================
CartesianIndependent::CartesianIndependent(pugi::xml_node node)
{
// Read distribution for x coordinate
if (check_for_node(node, "x")) {
pugi::xml_node node_dist = node.child("x");
x_ = distribution_from_xml(node_dist);
} else {
// If no distribution was specified, default to a single point at x=0
double x[] {0.0};
double p[] {1.0};
x_ = UPtrDist {new Discrete {x, p, 1}};
}
// Read distribution for y coordinate
if (check_for_node(node, "y")) {
pugi::xml_node node_dist = node.child("y");
y_ = distribution_from_xml(node_dist);
} else {
// If no distribution was specified, default to a single point at y=0
double x[] {0.0};
double p[] {1.0};
y_ = UPtrDist {new Discrete {x, p, 1}};
}
// Read distribution for z coordinate
if (check_for_node(node, "z")) {
pugi::xml_node node_dist = node.child("z");
z_ = distribution_from_xml(node_dist);
} else {
// If no distribution was specified, default to a single point at z=0
double x[] {0.0};
double p[] {1.0};
z_ = UPtrDist {new Discrete {x, p, 1}};
}
}
Position CartesianIndependent::sample(uint64_t* seed) const
{
return {x_->sample(seed), y_->sample(seed), z_->sample(seed)};
}
//==============================================================================
// CylindricalIndependent implementation
//==============================================================================
CylindricalIndependent::CylindricalIndependent(pugi::xml_node node)
{
// Read distribution for r-coordinate
if (check_for_node(node, "r")) {
pugi::xml_node node_dist = node.child("r");
r_ = distribution_from_xml(node_dist);
} else {
// If no distribution was specified, default to a single point at r=0
double x[] {0.0};
double p[] {1.0};
r_ = make_unique<Discrete>(x, p, 1);
}
// Read distribution for phi-coordinate
if (check_for_node(node, "phi")) {
pugi::xml_node node_dist = node.child("phi");
phi_ = distribution_from_xml(node_dist);
} else {
// If no distribution was specified, default to a single point at phi=0
double x[] {0.0};
double p[] {1.0};
phi_ = make_unique<Discrete>(x, p, 1);
}
// Read distribution for z-coordinate
if (check_for_node(node, "z")) {
pugi::xml_node node_dist = node.child("z");
z_ = distribution_from_xml(node_dist);
} else {
// If no distribution was specified, default to a single point at z=0
double x[] {0.0};
double p[] {1.0};
z_ = make_unique<Discrete>(x, p, 1);
}
// Read cylinder center coordinates
if (check_for_node(node, "origin")) {
auto origin = get_node_array<double>(node, "origin");
if (origin.size() == 3) {
origin_ = origin;
} else {
fatal_error(
"Origin for cylindrical source distribution must be length 3");
}
} else {
// If no coordinates were specified, default to (0, 0, 0)
origin_ = {0.0, 0.0, 0.0};
}
}
Position CylindricalIndependent::sample(uint64_t* seed) const
{
double r = r_->sample(seed);
double phi = phi_->sample(seed);
double x = r * cos(phi) + origin_.x;
double y = r * sin(phi) + origin_.y;
double z = z_->sample(seed) + origin_.z;
return {x, y, z};
}
//==============================================================================
// SphericalIndependent implementation
//==============================================================================
SphericalIndependent::SphericalIndependent(pugi::xml_node node)
{
// Read distribution for r-coordinate
if (check_for_node(node, "r")) {
pugi::xml_node node_dist = node.child("r");
r_ = distribution_from_xml(node_dist);
} else {
// If no distribution was specified, default to a single point at r=0
double x[] {0.0};
double p[] {1.0};
r_ = make_unique<Discrete>(x, p, 1);
}
// Read distribution for cos_theta-coordinate
if (check_for_node(node, "cos_theta")) {
pugi::xml_node node_dist = node.child("cos_theta");
cos_theta_ = distribution_from_xml(node_dist);
} else {
// If no distribution was specified, default to a single point at
// cos_theta=0
double x[] {0.0};
double p[] {1.0};
cos_theta_ = make_unique<Discrete>(x, p, 1);
}
// Read distribution for phi-coordinate
if (check_for_node(node, "phi")) {
pugi::xml_node node_dist = node.child("phi");
phi_ = distribution_from_xml(node_dist);
} else {
// If no distribution was specified, default to a single point at phi=0
double x[] {0.0};
double p[] {1.0};
phi_ = make_unique<Discrete>(x, p, 1);
}
// Read sphere center coordinates
if (check_for_node(node, "origin")) {
auto origin = get_node_array<double>(node, "origin");
if (origin.size() == 3) {
origin_ = origin;
} else {
fatal_error("Origin for spherical source distribution must be length 3");
}
} else {
// If no coordinates were specified, default to (0, 0, 0)
origin_ = {0.0, 0.0, 0.0};
}
}
Position SphericalIndependent::sample(uint64_t* seed) const
{
double r = r_->sample(seed);
double cos_theta = cos_theta_->sample(seed);
double phi = phi_->sample(seed);
// sin(theta) by sin**2 + cos**2 = 1
double x = r * std::sqrt(1 - cos_theta * cos_theta) * cos(phi) + origin_.x;
double y = r * std::sqrt(1 - cos_theta * cos_theta) * sin(phi) + origin_.y;
double z = r * cos_theta + origin_.z;
return {x, y, z};
}
//==============================================================================
// MeshSpatial implementation
//==============================================================================
MeshSpatial::MeshSpatial(pugi::xml_node node)
{
if (get_node_value(node, "type", true, true) != "mesh") {
fatal_error(fmt::format(
"Incorrect spatial type '{}' for a MeshSpatial distribution"));
}
// No in-tet distributions implemented, could include distributions for the
// barycentric coords Read in unstructured mesh from mesh_id value
int32_t mesh_id = std::stoi(get_node_value(node, "mesh_id"));
// Get pointer to spatial distribution
mesh_idx_ = model::mesh_map.at(mesh_id);
const auto mesh_ptr = model::meshes.at(mesh_idx_).get();
check_element_types();
size_t n_bins = this->n_sources();
std::vector<double> strengths(n_bins, 1.0);
// Create cdfs for sampling for an element over a mesh
// Volume scheme is weighted by the volume of each tet
// File scheme is weighted by an array given in the xml file
if (check_for_node(node, "strengths")) {
strengths = get_node_array<double>(node, "strengths");
if (strengths.size() != n_bins) {
fatal_error(
fmt::format("Number of entries in the source strengths array {} does "
"not match the number of entities in mesh {} ({}).",
strengths.size(), mesh_id, n_bins));
}
}
if (get_node_value_bool(node, "volume_normalized")) {
for (int i = 0; i < n_bins; i++) {
strengths[i] *= this->mesh()->volume(i);
}
}
elem_idx_dist_.assign(strengths);
}
MeshSpatial::MeshSpatial(int32_t mesh_idx, gsl::span<const double> strengths)
: mesh_idx_(mesh_idx)
{
check_element_types();
elem_idx_dist_.assign(strengths);
}
void MeshSpatial::check_element_types() const
{
const auto umesh_ptr = dynamic_cast<const UnstructuredMesh*>(this->mesh());
if (umesh_ptr) {
// ensure that the unstructured mesh contains only linear tets
for (int bin = 0; bin < umesh_ptr->n_bins(); bin++) {
if (umesh_ptr->element_type(bin) != ElementType::LINEAR_TET) {
fatal_error(
"Mesh specified for source must contain only linear tetrahedra.");
}
}
}
}
int32_t MeshSpatial::sample_element_index(uint64_t* seed) const
{
return elem_idx_dist_.sample(seed);
}
std::pair<int32_t, Position> MeshSpatial::sample_mesh(uint64_t* seed) const
{
// Sample the CDF defined in initialization above
int32_t elem_idx = this->sample_element_index(seed);
return {elem_idx, mesh()->sample_element(elem_idx, seed)};
}
Position MeshSpatial::sample(uint64_t* seed) const
{
return this->sample_mesh(seed).second;
}
//==============================================================================
// SpatialBox implementation
//==============================================================================
SpatialBox::SpatialBox(pugi::xml_node node, bool fission)
: only_fissionable_ {fission}
{
// Read lower-right/upper-left coordinates
auto params = get_node_array<double>(node, "parameters");
if (params.size() != 6)
openmc::fatal_error("Box/fission spatial source must have six "
"parameters specified.");
lower_left_ = Position {params[0], params[1], params[2]};
upper_right_ = Position {params[3], params[4], params[5]};
}
Position SpatialBox::sample(uint64_t* seed) const
{
Position xi {prn(seed), prn(seed), prn(seed)};
return lower_left_ + xi * (upper_right_ - lower_left_);
}
//==============================================================================
// SpatialPoint implementation
//==============================================================================
SpatialPoint::SpatialPoint(pugi::xml_node node)
{
// Read location of point source
auto params = get_node_array<double>(node, "parameters");
if (params.size() != 3)
openmc::fatal_error("Point spatial source must have three "
"parameters specified.");
// Set position
r_ = Position {params.data()};
}
Position SpatialPoint::sample(uint64_t* seed) const
{
return r_;
}
} // namespace openmc