Repository navigation
Expand file tree
/
Copy pathdistribution_spatial.cpp
More file actions
494 lines (426 loc) · 15.8 KB
/
Copy pathdistribution_spatial.cpp
File metadata and controls
494 lines (426 loc) · 15.8 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#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 == "cloud") {
return UPtrSpace {new PointCloud(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}};
}
}
std::pair<Position, double> CartesianIndependent::sample(uint64_t* seed) const
{
auto [x_val, x_wgt] = x_->sample(seed);
auto [y_val, y_wgt] = y_->sample(seed);
auto [z_val, z_wgt] = z_->sample(seed);
Position xi {x_val, y_val, z_val};
return {xi, x_wgt * y_wgt * z_wgt};
}
//==============================================================================
// 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};
}
// Read cylinder z_dir
if (check_for_node(node, "z_dir")) {
auto z_dir = get_node_array<double>(node, "z_dir");
if (z_dir.size() == 3) {
z_dir_ = z_dir;
z_dir_ /= z_dir_.norm();
} else {
fatal_error("z_dir for cylindrical source distribution must be length 3");
}
} else {
// If no z_dir was specified, default to (0, 0, 1)
z_dir_ = {0.0, 0.0, 1.0};
}
// Read cylinder r_dir
if (check_for_node(node, "r_dir")) {
auto r_dir = get_node_array<double>(node, "r_dir");
if (r_dir.size() == 3) {
r_dir_ = r_dir;
r_dir_ /= r_dir_.norm();
} else {
fatal_error("r_dir for cylindrical source distribution must be length 3");
}
} else {
// If no r_dir was specified, default to (1, 0, 0)
r_dir_ = {1.0, 0.0, 0.0};
}
if (r_dir_.dot(z_dir_) > 1e-12)
fatal_error("r_dir must be perpendicular to z_dir");
auto phi_dir = z_dir_.cross(r_dir_);
phi_dir /= phi_dir.norm();
phi_dir_ = phi_dir;
}
std::pair<Position, double> CylindricalIndependent::sample(uint64_t* seed) const
{
auto [r, r_wgt] = r_->sample(seed);
auto [phi, phi_wgt] = phi_->sample(seed);
auto [z, z_wgt] = z_->sample(seed);
Position xi =
r * (cos(phi) * r_dir_ + sin(phi) * phi_dir_) + z * z_dir_ + origin_;
return {xi, r_wgt * phi_wgt * z_wgt};
}
//==============================================================================
// 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};
}
}
std::pair<Position, double> SphericalIndependent::sample(uint64_t* seed) const
{
auto [r, r_wgt] = r_->sample(seed);
auto [cos_theta, cos_theta_wgt] = cos_theta_->sample(seed);
auto [phi, phi_wgt] = 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;
Position xi {x, y, z};
return {xi, r_wgt * cos_theta_wgt * phi_wgt};
}
//==============================================================================
// MeshSpatial implementation
//==============================================================================
MeshSpatial::MeshSpatial(pugi::xml_node node)
{
auto spatial_type = get_node_value(node, "type", true, true);
if (spatial_type != "mesh") {
fatal_error(
fmt::format("Incorrect spatial type '{}' for a MeshSpatial distribution",
spatial_type));
}
// 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);
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);
if (check_for_node(node, "bias")) {
pugi::xml_node bias_node = node.child("bias");
if (check_for_node(bias_node, "strengths")) {
std::vector<double> bias_strengths(n_bins, 1.0);
bias_strengths = get_node_array<double>(node, "strengths");
if (bias_strengths.size() != n_bins) {
fatal_error(
fmt::format("Number of entries in the bias strengths array {} does "
"not match the number of entities in mesh {} ({}).",
bias_strengths.size(), mesh_id, n_bins));
}
if (get_node_value_bool(node, "volume_normalized")) {
for (int i = 0; i < n_bins; i++) {
bias_strengths[i] *= this->mesh()->volume(i);
}
}
// Compute importance weights
weight_ = compute_importance_weights(strengths, bias_strengths);
// Re-initialize DiscreteIndex with bias strengths for sampling
elem_idx_dist_.assign(bias_strengths);
} else {
fatal_error(fmt::format(
"Bias node for mesh {} found without strengths array.", mesh_id));
}
}
}
MeshSpatial::MeshSpatial(int32_t mesh_idx, 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)};
}
std::pair<Position, double> MeshSpatial::sample(uint64_t* seed) const
{
auto [elem_idx, u] = this->sample_mesh(seed);
double wgt = weight_.empty() ? 1.0 : weight_[elem_idx];
return {u, wgt};
}
//==============================================================================
// PointCloud implementation
//==============================================================================
PointCloud::PointCloud(pugi::xml_node node)
{
if (check_for_node(node, "coords")) {
point_cloud_ = get_node_position_array(node, "coords");
} else {
fatal_error("No coordinates were provided for the PointCloud "
"spatial distribution");
}
std::vector<double> strengths;
if (check_for_node(node, "strengths"))
strengths = get_node_array<double>(node, "strengths");
else
strengths.resize(point_cloud_.size(), 1.0);
if (strengths.size() != point_cloud_.size()) {
fatal_error(
fmt::format("Number of entries for the strengths array {} does "
"not match the number of spatial points provided {}.",
strengths.size(), point_cloud_.size()));
}
point_idx_dist_.assign(strengths);
if (check_for_node(node, "bias")) {
pugi::xml_node bias_node = node.child("bias");
if (check_for_node(bias_node, "strengths")) {
std::vector<double> bias_strengths(point_cloud_.size(), 1.0);
bias_strengths = get_node_array<double>(node, "strengths");
if (bias_strengths.size() != point_cloud_.size()) {
fatal_error(
fmt::format("Number of entries in the bias strengths array {} does "
"not match the number of spatial points provided {}.",
bias_strengths.size(), point_cloud_.size()));
}
// Compute importance weights
weight_ = compute_importance_weights(strengths, bias_strengths);
// Re-initialize DiscreteIndex with bias strengths for sampling
point_idx_dist_.assign(bias_strengths);
} else {
fatal_error(
fmt::format("Bias node for PointCloud found without strengths array."));
}
}
}
PointCloud::PointCloud(
std::vector<Position> point_cloud, span<const double> strengths)
{
point_cloud_.assign(point_cloud.begin(), point_cloud.end());
point_idx_dist_.assign(strengths);
}
std::pair<Position, double> PointCloud::sample(uint64_t* seed) const
{
int32_t index = point_idx_dist_.sample(seed);
double wgt = weight_.empty() ? 1.0 : weight_[index];
return {point_cloud_[index], wgt};
}
//==============================================================================
// 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]};
}
SpatialBox::SpatialBox(Position lower_left, Position upper_right, bool fission)
: lower_left_(lower_left), upper_right_(upper_right),
only_fissionable_(fission)
{}
std::pair<Position, double> SpatialBox::sample(uint64_t* seed) const
{
Position xi {prn(seed), prn(seed), prn(seed)};
return {lower_left_ + xi * (upper_right_ - lower_left_), 1.0};
}
//==============================================================================
// 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()};
}
std::pair<Position, double> SpatialPoint::sample(uint64_t* seed) const
{
return {r_, 1.0};
}
} // namespace openmc