Repository navigation
Expand file tree
/
Copy pathdistribution_multi.cpp
More file actions
192 lines (166 loc) · 5.88 KB
/
Copy pathdistribution_multi.cpp
File metadata and controls
192 lines (166 loc) · 5.88 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
#include "openmc/distribution_multi.h"
#include <algorithm> // for move, clamp
#include <cmath> // for sqrt, sin, cos, max
#include "openmc/constants.h"
#include "openmc/error.h"
#include "openmc/math_functions.h"
#include "openmc/random_dist.h"
#include "openmc/random_lcg.h"
#include "openmc/xml_interface.h"
namespace openmc {
unique_ptr<UnitSphereDistribution> UnitSphereDistribution::create(
pugi::xml_node node)
{
// Check for type of angular distribution
std::string type;
if (check_for_node(node, "type"))
type = get_node_value(node, "type", true, true);
if (type == "isotropic") {
return UPtrAngle {new Isotropic(node)};
} else if (type == "monodirectional") {
return UPtrAngle {new Monodirectional(node)};
} else if (type == "mu-phi") {
return UPtrAngle {new PolarAzimuthal(node)};
} else {
fatal_error(fmt::format(
"Invalid angular distribution for external source: {}", type));
}
}
//==============================================================================
// UnitSphereDistribution implementation
//==============================================================================
UnitSphereDistribution::UnitSphereDistribution(pugi::xml_node node)
{
// Read reference directional unit vector
if (check_for_node(node, "reference_uvw")) {
auto u_ref = get_node_array<double>(node, "reference_uvw");
if (u_ref.size() != 3)
fatal_error("Angular distribution reference direction must have "
"three parameters specified.");
u_ref_ = Direction(u_ref.data());
u_ref_ /= u_ref_.norm();
}
}
//==============================================================================
// PolarAzimuthal implementation
//==============================================================================
PolarAzimuthal::PolarAzimuthal(Direction u, UPtrDist mu, UPtrDist phi)
: UnitSphereDistribution {u}, mu_ {std::move(mu)}, phi_ {std::move(phi)}
{}
PolarAzimuthal::PolarAzimuthal(pugi::xml_node node)
: UnitSphereDistribution {node}
{
// Read reference directional unit vector
if (check_for_node(node, "reference_vwu")) {
auto v_ref = get_node_array<double>(node, "reference_vwu");
if (v_ref.size() != 3)
fatal_error("Angular distribution reference v direction must have "
"three parameters specified.");
v_ref_ = Direction(v_ref.data());
v_ref_ /= v_ref_.norm();
}
w_ref_ = u_ref_.cross(v_ref_);
if (check_for_node(node, "mu")) {
pugi::xml_node node_dist = node.child("mu");
mu_ = distribution_from_xml(node_dist);
} else {
mu_ = UPtrDist {new Uniform(-1., 1.)};
}
if (check_for_node(node, "phi")) {
pugi::xml_node node_dist = node.child("phi");
phi_ = distribution_from_xml(node_dist);
} else {
phi_ = UPtrDist {new Uniform(0.0, 2.0 * PI)};
}
}
std::pair<Direction, double> PolarAzimuthal::sample(uint64_t* seed) const
{
return sample_impl(seed, false);
}
std::pair<Direction, double> PolarAzimuthal::sample_as_bias(
uint64_t* seed) const
{
return sample_impl(seed, true);
}
std::pair<Direction, double> PolarAzimuthal::sample_impl(
uint64_t* seed, bool return_pdf) const
{
// Sample cosine of polar angle
auto [mu, mu_wgt] = mu_->sample(seed);
// Sample azimuthal angle
auto [phi, phi_wgt] = phi_->sample(seed);
// Compute either the PDF value or the importance weight
double weight =
return_pdf ? (mu_->evaluate(mu) * phi_->evaluate(phi)) : (mu_wgt * phi_wgt);
if (mu == 1.0)
return {u_ref_, weight};
if (mu == -1.0)
return {-u_ref_, weight};
double f = std::sqrt(1 - mu * mu);
return {mu * u_ref_ + f * std::cos(phi) * v_ref_ + f * std::sin(phi) * w_ref_,
weight};
}
double PolarAzimuthal::evaluate(Direction u) const
{
double mu = std::clamp(u.dot(u_ref_), -1.0, 1.0);
double phi = 0.0;
double sin_theta_sq = std::max(0.0, 1.0 - mu * mu);
if (sin_theta_sq > 0.0) {
double sin_theta = std::sqrt(sin_theta_sq);
double cos_phi = u.dot(v_ref_) / sin_theta;
double sin_phi = u.dot(w_ref_) / sin_theta;
phi = std::atan2(sin_phi, cos_phi);
if (phi < 0.0)
phi += 2.0 * PI;
}
return mu_->evaluate(mu) * phi_->evaluate(phi);
}
//==============================================================================
// Isotropic implementation
//==============================================================================
Isotropic::Isotropic(pugi::xml_node node) : UnitSphereDistribution {node}
{
if (check_for_node(node, "bias")) {
pugi::xml_node bias_node = node.child("bias");
std::string bias_type = get_node_value(bias_node, "type", true, true);
if (bias_type != "mu-phi") {
openmc::fatal_error(
"Isotropic distributions may only be biased by a PolarAzimuthal.");
}
auto bias = std::make_unique<PolarAzimuthal>(bias_node);
if (bias->mu()->bias() || bias->phi()->bias()) {
openmc::fatal_error(
"Attempted to bias Isotropic distribution with a biased PolarAzimuthal "
"distribution. Please ensure bias distributions are unbiased.");
}
this->set_bias(std::move(bias));
}
}
Direction isotropic_direction(uint64_t* seed)
{
double phi = uniform_distribution(0., 2.0 * PI, seed);
double mu = uniform_distribution(-1., 1., seed);
return {mu, std::sqrt(1.0 - mu * mu) * std::cos(phi),
std::sqrt(1.0 - mu * mu) * std::sin(phi)};
}
std::pair<Direction, double> Isotropic::sample(uint64_t* seed) const
{
if (bias()) {
auto [val, eval] = bias()->sample_as_bias(seed);
return {val, 1.0 / (4.0 * PI * eval)};
} else {
return {isotropic_direction(seed), 1.0};
}
}
double Isotropic::evaluate(Direction u) const
{
return 1.0 / (4.0 * PI);
}
//==============================================================================
// Monodirectional implementation
//==============================================================================
std::pair<Direction, double> Monodirectional::sample(uint64_t* seed) const
{
return {u_ref_, 1.0};
}
} // namespace openmc