forked from openmc-dev/openmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_dist.cpp
49 lines (39 loc) · 1.16 KB
/
random_dist.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
#include "openmc/random_dist.h"
#include <cmath>
#include "openmc/constants.h"
#include "openmc/random_lcg.h"
namespace openmc {
double uniform_distribution(double a, double b, uint64_t* seed)
{
return a + (b - a) * prn(seed);
}
double maxwell_spectrum(double T, uint64_t* seed)
{
// Set the random numbers
double r1 = prn(seed);
double r2 = prn(seed);
double r3 = prn(seed);
// determine cosine of pi/2*r
double c = std::cos(PI / 2. * r3);
// Determine outgoing energy
return -T * (std::log(r1) + std::log(r2) * c * c);
}
double watt_spectrum(double a, double b, uint64_t* seed)
{
double w = maxwell_spectrum(a, seed);
return w + 0.25 * a * a * b +
uniform_distribution(-1., 1., seed) * std::sqrt(a * a * b * w);
}
double normal_variate(double mean, double standard_deviation, uint64_t* seed)
{
// Sample a normal variate using Marsaglia's polar method
double x, y, r2;
do {
x = uniform_distribution(-1., 1., seed);
y = uniform_distribution(-1., 1., seed);
r2 = x * x + y * y;
} while (r2 > 1 || r2 == 0);
double z = std::sqrt(-2.0 * std::log(r2) / r2);
return mean + standard_deviation * z * x;
}
} // namespace openmc