bi-kappa 3.0.0
Monte Carlo particle samplers for plasma velocity and position distributions
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <random>
2#include <cmath>
3#include <iostream>
4#include <fstream>
5
6typedef double Real;
7
8// Local headers
14
15int main()
16{
17 const Real kappa = 2.0;
18 const Real theta_perp = 1.0;
19 const Real theta_par = 2.0;
20 // Short worked examples of the five samplers; each writes its samples to a text file
21 // (vx vy vz per line) that python/visualize_samples.ipynb plots. The regression suite
22 // is separate: `make test`.
23 const int n_particle = 50000;
24
25 std::cout << "=== Example 1: BiKappaDistribution ===" << std::endl;
26 // no_cap(): these samples follow the bi-Kappa distribution itself. Omitting the fifth
27 // argument applies the default cap of 20 thermal speeds, which samples the bi-Kappa
28 // distribution conditioned on that component-wise box.
30 biKappa.define(kappa, theta_perp, theta_par, {0,0,1},
32 {
33 std::ofstream out("samples_bikappa.txt");
34 for (int i = 0; i < n_particle; ++i) {
36 out << v[0] << " " << v[1] << " " << v[2] << "\n";
37 }
38 std::cout << "wrote " << n_particle << " samples to samples_bikappa.txt" << std::endl;
39 }
40
41 std::cout << "\n=== Example 2: BiMaxwellianDistribution ===" << std::endl;
42 // no_cap(), as above; the default is again a cap of 20 thermal speeds.
44 biMaxwell.define(theta_perp, theta_par, {0,0,1},
46 {
47 std::ofstream out("samples_bimaxwellian.txt");
48 for (int i = 0; i < n_particle; ++i) {
50 out << v[0] << " " << v[1] << " " << v[2] << "\n";
51 }
52 std::cout << "wrote " << n_particle << " samples to samples_bimaxwellian.txt" << std::endl;
53 }
54
55 std::cout << "\n=== Example 3: general_velocity_generator (isotropic Maxwellian via w=|v|^2) ==="
56 << std::endl;
57 // g(w) = sqrt(w) * exp(-w/theta^2) with w = |v|^2 is the speed-squared density
58 // of an isotropic Maxwellian of thermal speed theta: with dw = 2|v| d|v| it maps
59 // to the Maxwell speed pdf ~ |v|^2 exp(-|v|^2/theta^2), i.e. each Cartesian
60 // component is N(0, theta^2/2). theta = theta_perp = 1 here, and w_max = 25
61 // truncates at |v| = 5 theta.
62 const Real theta_iso = theta_perp;
63 auto speedSqPdf = [theta_iso](Real w) -> Real {
64 return std::sqrt(w) * std::exp(-w / (theta_iso * theta_iso));
65 };
67 velocityGen.define(speedSqPdf,
68 static_cast<Real>(0.0),
69 static_cast<Real>(25.0));
70 velocityGen.seed(20030410);
71 {
72 std::ofstream out("samples_general_velocity.txt");
73 for (int i = 0; i < n_particle; ++i) {
75 out << v[0] << " " << v[1] << " " << v[2] << "\n";
76 }
77 std::cout << "wrote " << n_particle << " samples to samples_general_velocity.txt"
78 << std::endl;
79 }
80
81 std::cout << "\n=== Example 4: general_position_generator (rho=1+sin(x)sin(y)) ===" << std::endl;
82 typedef general_position_generator<Real>::point_type position_point_type;
83 auto rho = [](const position_point_type &x) -> Real {
84 return 1 + std::sin(x[0]) * std::sin(x[1]);
85 };
86 static double pi = 3.14159;
88 positionGen.define(2, {-pi, -pi}, {pi, pi}, rho);
89 positionGen.seed(20030410);
90 {
91 std::ofstream out("samples_general_position.txt");
92 for (int i = 0; i < n_particle; ++i) {
93 const position_point_type x = positionGen();
94 out << x[0] << " " << x[1] << "\n";
95 }
96 std::cout << "wrote " << n_particle << " samples to samples_general_position.txt"
97 << std::endl;
98 }
99
100 std::cout << "\n=== Example 5: field_aligned_velocity_generator (uniform w_par, B=(1,1,1)) ==="
101 << std::endl;
102 // The simplest possible input: g(w_par) = 1 makes w_par = v_par^2 uniform on
103 // [0, w_max], and the Jacobian dw = 2 v_par d(v_par) turns that into the linear
104 // ramp p(v_par) = 2 v_par / w_max on [0, sqrt(w_max)]. A density that is flat in
105 // w is a straight line in v_par -- the clearest illustration of what the change
106 // of variables does. The perpendicular plane is Maxwellian with thermal speed
107 // theta_perp, and B is tilted off the axes so the rotation is exercised.
108 const Real w_max_par = 4.0; // v_par spans [0, 2]
109 auto parallelSpeedSqPdf = [](Real) -> Real { return Real(1.0); };
111 fieldAlignedGen.define(parallelSpeedSqPdf,
112 static_cast<Real>(0.0),
113 w_max_par,
114 theta_perp,
115 {1, 1, 1},
116 +1);
117 fieldAlignedGen.seed(20030410);
118 {
119 std::ofstream out("samples_field_aligned.txt");
120 for (int i = 0; i < n_particle; ++i) {
121 const field_aligned_velocity_generator<Real>::point_type v = fieldAlignedGen();
122 out << v[0] << " " << v[1] << " " << v[2] << "\n";
123 }
124 std::cout << "wrote " << n_particle << " samples to samples_field_aligned.txt"
125 << std::endl;
126 }
127
128 return 0;
129}
Bi-Kappa distribution class for sampling 3D velocity vectors.
void define(const param_type &params)
Replace all parameters with params (already validated by its constructor), then call reset().
static constexpr RealType no_cap()
Value of the cap that selects the uncapped sampler: +infinity.
result_type point_type
Same as result_type.
Bi-Maxwellian distribution for 3D velocity sampling.
result_type point_type
Same as result_type.
static constexpr RealType no_cap()
Value of the cap that selects the uncapped sampler: +infinity.
void define(const param_type &params)
Replace all parameters with params, then call reset().
Samples field-aligned velocities: an arbitrary parallel speed-squared distribution along B,...
void seed(int s)
Reseed the internal engine and discard any variate cached from before, so that seed(s) followed by n ...
void define(speed_sq_pdf_function speed_sq_pdf, RealType speed_sq_min, RealType speed_sq_max, RealType theta_perp, point_type ub, int parallel_sign, int probe_points=1024, int max_reject_tries=100000, RealType max_normalized_velocity=RealType(20.0))
Configure the sampler: user-supplied law along the field, Maxwellian across it.
Samples positions in 1D/2D/3D from a user density rho(x), rho(x,y), or rho(x,y,z).
void define(int dimension, const point_type &lower_bounds, const point_type &upper_bounds, density_function density_function, int probe_points=4096, int max_reject_tries=200000)
Configure the sampler.
void seed(int s)
Reseed the internal engine and discard any variate cached from before, so that seed(s) followed by n ...
result_type point_type
Same as result_type.
Samples velocities from a user-defined speed-squared distribution g(w).
void seed(int s)
Reseed the internal engine and discard any variate cached from before, so that seed(s) followed by n ...
result_type point_type
Same as result_type.
void define(speed_sq_pdf_function speed_sq_pdf, RealType speed_sq_min, RealType speed_sq_max, int probe_points=1024, int max_reject_tries=100000)
Configure the sampler.
double Real
Definition main.cpp:6
int main()
Definition main.cpp:15