bi-kappa 3.0.0
Monte Carlo particle samplers for plasma velocity and position distributions
Loading...
Searching...
No Matches
bi_maxwellian_distribution.H
Go to the documentation of this file.
1#ifndef _BI_MAXWELLIAN_DISTRIBUTION_H_
2#define _BI_MAXWELLIAN_DISTRIBUTION_H_
3
4#include <array>
5#include <algorithm>
6#include <cmath>
7#include <limits>
8#include <random>
9#include <stdexcept>
10
29template <typename RealType= double> class bi_maxwellian_distribution
30{
31 public:
32 using result_type= std::array<RealType, 3>;
34
38 {
39 public:
41
44 param_type(RealType theta_perp= RealType(1.0), RealType theta_par= RealType(1.0),
45 point_type ub= point_type({RealType(0.0), RealType(0.0), RealType(1.0)}),
46 RealType max_normalized_velocity= RealType(20.0))
47 : m_theta_perp(theta_perp), m_theta_par(theta_par), m_ub(ub),
48 m_maxNormalizedVelocity(max_normalized_velocity)
49 {
50 if (m_theta_perp <= RealType(0.0) || m_theta_par <= RealType(0.0))
51 {
52 throw std::invalid_argument("Bi-Maxwellian temperatures must be positive");
53 }
54 // A positive finite value selects the capped mode. no_cap() (= +inf)
55 // selects the untruncated mode, in which no acceptance-rejection loop
56 // runs at all and samples follow the bi-Maxwellian distribution itself.
57 if (!(m_maxNormalizedVelocity > RealType(0.0)))
58 {
59 throw std::invalid_argument(
60 "max_normalized_velocity must be positive, or no_cap() for the "
61 "untruncated distribution");
62 }
63 }
64
65 RealType theta_perp() const { return m_theta_perp; }
66 RealType theta_par() const { return m_theta_par; }
67 point_type ub() const { return m_ub; }
69 RealType max_normalized_velocity() const { return m_maxNormalizedVelocity; }
70
74 bool capped() const { return std::isfinite(m_maxNormalizedVelocity); }
75
77 bool operator==(const param_type &other) const
78 {
79 return m_theta_perp == other.m_theta_perp && m_theta_par == other.m_theta_par &&
80 m_ub == other.m_ub && m_maxNormalizedVelocity == other.m_maxNormalizedVelocity;
81 }
82
84 bool operator!=(const param_type &other) const { return !(*this == other); }
85
86 private:
87 RealType m_theta_perp;
88 RealType m_theta_par;
89 point_type m_ub;
90 RealType m_maxNormalizedVelocity;
91 };
92
111 static constexpr RealType no_cap()
112 {
113 return std::numeric_limits<RealType>::infinity();
114 }
115
116 // ====================================================================
117 // Construction
118 // ====================================================================
121 bi_maxwellian_distribution() : m_params(), m_gen(std::random_device{}()), m_standardNormal(RealType(0.0), RealType(1.0)) {}
122
125 explicit bi_maxwellian_distribution(RealType theta_perp, RealType theta_par= RealType(1.0),
126 point_type ub= point_type({RealType(0.0), RealType(0.0),
127 RealType(1.0)}),
128 RealType max_normalized_velocity= RealType(20.0))
130 m_gen(std::random_device{}()), m_standardNormal(RealType(0.0), RealType(1.0))
131 {
133 }
134
137 explicit bi_maxwellian_distribution(const param_type &params)
138 : m_params(params), m_gen(std::random_device{}()), m_standardNormal(RealType(0.0), RealType(1.0))
139 {
140 define(params);
141 }
142
143 // ====================================================================
144 // Setters
145 // ====================================================================
146
148 void define(const param_type &params)
149 {
150 m_params= params;
151 reset(); // just to clear cache
152 }
153
183 void define(RealType theta_perp, RealType theta_par= RealType(1.0),
184 point_type ub= point_type({RealType(0.0), RealType(0.0), RealType(1.0)}),
185 RealType max_normalized_velocity= RealType(20.0))
186 {
188 }
189
191 void define(const param_type &params, int seed) { applySeed(m_gen, seed); define(params); }
192
195 void define(RealType theta_perp, RealType theta_par,
197 {
198 applySeed(m_gen, seed);
200 }
201
202 // Individual parameter setters. Each calls define() with the other parameters
203 // unchanged.
204
206 void theta_perp(RealType value)
207 {
208 define(value, m_params.theta_par(), m_params.ub(), m_params.max_normalized_velocity());
209 }
210
212 void theta_par(RealType value)
213 {
214 define(m_params.theta_perp(), value, m_params.ub(), m_params.max_normalized_velocity());
215 }
216
218 void ub(const point_type &value)
219 {
220 define(m_params.theta_perp(), m_params.theta_par(), value,
221 m_params.max_normalized_velocity());
222 }
223
228 void max_normalized_velocity(RealType value)
229 {
230 define(m_params.theta_perp(), m_params.theta_par(), m_params.ub(), value);
231 }
232
233 // ====================================================================
234 // Getters
235 // ====================================================================
236
238 param_type param() const { return m_params; }
239
240 RealType theta_perp() const { return m_params.theta_perp(); }
241 RealType theta_par() const { return m_params.theta_par(); }
242 point_type ub() const { return m_params.ub(); }
244 RealType max_normalized_velocity() const { return m_params.max_normalized_velocity(); }
245
246 // ====================================================================
247 // Random engine control
248 // ====================================================================
251 void seed(int s)
252 {
253 applySeed(m_gen, s);
254 reset();
255 }
256
259 void reset() { m_standardNormal.reset(); }
260
261 // ====================================================================
262 // Sampling
263 // ====================================================================
266 template <typename GeneratorType> result_type operator()(GeneratorType &gen)
267 {
268 return (*this)(gen, m_params);
269 }
270
274 {
275 return (*this)(m_gen);
276 }
277
282 template <typename GeneratorType>
283 result_type operator()(GeneratorType &gen, const param_type &params)
284 {
285 const RealType sigmaScale= RealType(1.0) / std::sqrt(RealType(2.0));
286 const int kMaxCapRejectTries= 1000000;
287 RealType maxNormalizedVelocity= params.max_normalized_velocity();
288
289 // Untruncated mode runs the core draw exactly once; capped mode redraws
290 // until the sample falls inside the box.
291 const bool capped= params.capped();
292 const int maxTries= capped ? kMaxCapRejectTries : 1;
293
294 int tries= 0;
295 for (tries= 0; tries < maxTries; ++tries)
296 {
297 RealType vx= params.theta_perp() * sigmaScale * m_standardNormal(gen);
298 RealType vy= params.theta_perp() * sigmaScale * m_standardNormal(gen);
299 RealType vz= params.theta_par() * sigmaScale * m_standardNormal(gen);
300
301 point_type point;
302 point[0]= vx;
303 point[1]= vy;
304 point[2]= vz;
305
306 if (capped && !withinNormalizedVelocityCap(point, params.theta_perp(),
307 params.theta_par(),
308 maxNormalizedVelocity))
309 {
310 continue;
311 }
312
313 // {0,0,1} is the identity rotation, so skip the transform for it.
314 point_type ub= params.ub();
315 if (approximatelyEqual(ub[0], RealType(0.0)) &&
316 approximatelyEqual(ub[1], RealType(0.0)) &&
317 approximatelyEqual(ub[2], RealType(1.0)))
318 {
319 return point;
320 }
321 return rotate_from_fieldAligned_frame(point, ub);
322 }
323
324 throw std::runtime_error("failed to sample velocity under max_normalized_velocity cap: "
325 "rejection sampling exceeded internal attempt limit");
326 }
327
328 // ====================================================================
329 // Geometry helper
330 // ====================================================================
335 {
336 const RealType kZero= RealType(0.0);
337 const RealType kOne= RealType(1.0);
338
339 RealType ubNorm2= ub[0] * ub[0] + ub[1] * ub[1] + ub[2] * ub[2];
340 if (approximatelyEqual(ubNorm2, kZero))
341 {
342 throw std::invalid_argument("ub must be a non-zero vector");
343 }
344
345 RealType ubNorm= std::sqrt(ubNorm2);
346
347 point_type e3;
348 e3[0]= ub[0] / ubNorm;
349 e3[1]= ub[1] / ubNorm;
350 e3[2]= ub[2] / ubNorm;
351
352 point_type e2({kOne, kOne, kOne});
353
354 int maxcomp= 0;
355 if (std::abs(e3[1]) > std::abs(e3[maxcomp]))
356 {
357 maxcomp= 1;
358 }
359 if (std::abs(e3[2]) > std::abs(e3[maxcomp]))
360 {
361 maxcomp= 2;
362 }
363
364 RealType sum= e3[0] + e3[1] + e3[2];
365 e2[maxcomp]= kOne - sum / e3[maxcomp];
366
367 RealType e2Norm2= e2[0] * e2[0] + e2[1] * e2[1] + e2[2] * e2[2];
368 RealType e2Norm= std::sqrt(e2Norm2);
369 e2[0]/= e2Norm;
370 e2[1]/= e2Norm;
371 e2[2]/= e2Norm;
372
373 point_type e1;
374 e1[0]= e2[1] * e3[2] - e2[2] * e3[1];
375 e1[1]= e2[2] * e3[0] - e2[0] * e3[2];
376 e1[2]= e2[0] * e3[1] - e2[1] * e3[0];
377
378 point_type point;
379 point[0]= local[0] * e1[0] + local[1] * e2[0] + local[2] * e3[0];
380 point[1]= local[0] * e1[1] + local[1] * e2[1] + local[2] * e3[1];
381 point[2]= local[0] * e1[2] + local[1] * e2[2] + local[2] * e3[2];
382 return point;
383 }
384
385 // ====================================================================
386 // Comparison
387 // ====================================================================
389 bool operator==(const bi_maxwellian_distribution &other) const
390 {
391 return m_params == other.m_params;
392 }
393
395 bool operator!=(const bi_maxwellian_distribution &other) const { return !(*this == other); }
396
397 private:
398 // ====================================================================
399 // Internal helpers
400 // ====================================================================
401 static void applySeed(std::mt19937 &gen, int s)
402 {
403 if (s < 0)
404 gen.seed(std::random_device{}());
405 else
406 gen.seed(static_cast<unsigned int>(s));
407 }
408
409 static bool approximatelyEqual(RealType a, RealType b)
410 {
411 RealType scale= std::max({RealType(1.0), std::abs(a), std::abs(b)});
412 return std::abs(a - b) <= std::numeric_limits<RealType>::epsilon() * scale;
413 }
414
415 static bool withinNormalizedVelocityCap(const point_type &point, RealType theta_perp,
416 RealType theta_par, RealType max_normalized_velocity)
417 {
418 RealType norm_x= std::abs(point[0]) / theta_perp;
419 RealType norm_y= std::abs(point[1]) / theta_perp;
420 RealType norm_z= std::abs(point[2]) / theta_par;
421 return norm_x <= max_normalized_velocity && norm_y <= max_normalized_velocity &&
422 norm_z <= max_normalized_velocity;
423 }
424
425 param_type m_params;
426 std::mt19937 m_gen;
427 std::normal_distribution<RealType> m_standardNormal;
428};
429
430#endif // _BI_MAXWELLIAN_DISTRIBUTION_H_
The parameters of a bi_maxwellian_distribution: theta_perp, theta_par, ub and the velocity cap.
bool operator==(const param_type &other) const
True if every parameter compares equal.
point_type ub() const
Field direction, as given.
RealType max_normalized_velocity() const
Velocity cap in thermal speeds; +infinity for no_cap().
RealType theta_perp() const
Perpendicular thermal speed.
RealType theta_par() const
Parallel thermal speed.
bool operator!=(const param_type &other) const
Negation of operator==.
bi_maxwellian_distribution< RealType > distribution_type
The owning sampler.
param_type(RealType theta_perp=RealType(1.0), RealType theta_par=RealType(1.0), point_type ub=point_type({RealType(0.0), RealType(0.0), RealType(1.0)}), RealType max_normalized_velocity=RealType(20.0))
Store the parameters.
bool capped() const
True when a finite component-wise cap is in force, that is, when the sampled distribution is the bi-M...
result_type operator()(GeneratorType &gen)
Draw one velocity from the engine gen instead of the internal one.
bi_maxwellian_distribution(const param_type &params)
Construct from a parameter set.
void max_normalized_velocity(RealType value)
Set the velocity cap, keeping the other parameters.
result_type operator()(GeneratorType &gen, const param_type &params)
Draw one velocity from gen with the parameters params instead of the stored ones.
bool operator!=(const bi_maxwellian_distribution &other) const
Negation of operator==.
RealType theta_perp() const
Perpendicular thermal speed.
point_type rotate_from_fieldAligned_frame(const point_type &local, const point_type &ub) const
Rotate local = {v_perp1, v_perp2, v_par} from field-aligned components to the global frame,...
result_type point_type
Same as result_type.
void define(RealType theta_perp, RealType theta_par=RealType(1.0), point_type ub=point_type({RealType(0.0), RealType(0.0), RealType(1.0)}), RealType max_normalized_velocity=RealType(20.0))
Configure the sampler.
bool operator==(const bi_maxwellian_distribution &other) const
True if the two samplers have equal parameters. Engine state is not compared.
RealType theta_par() const
Parallel thermal speed.
RealType max_normalized_velocity() const
Velocity cap in thermal speeds; +infinity for no_cap().
bi_maxwellian_distribution(RealType theta_perp, RealType theta_par=RealType(1.0), point_type ub=point_type({RealType(0.0), RealType(0.0), RealType(1.0)}), RealType max_normalized_velocity=RealType(20.0))
Construct and call define() with these arguments.
point_type ub() const
Field direction, as given.
void theta_perp(RealType value)
Set theta_perp, keeping the other parameters; throws as define() does.
static constexpr RealType no_cap()
Value of the cap that selects the uncapped sampler: +infinity.
std::array< RealType, 3 > result_type
A velocity {vx, vy, vz}.
result_type operator()()
Draw one velocity from the internal engine.
void define(RealType theta_perp, RealType theta_par, point_type ub, RealType max_normalized_velocity, int seed)
define() with all four parameters, after seeding the internal engine with seed as seed() does (seed >...
bi_maxwellian_distribution()
Construct with theta_perp = theta_par = 1, ub = {0, 0, 1} and a cap of 20.
void ub(const point_type &value)
Set the field direction, keeping the other parameters.
void define(const param_type &params)
Replace all parameters with params, then call reset().
void define(const param_type &params, int seed)
define(params), after seeding the internal engine with seed as seed() does.
void reset()
Discard the normal variate cached by std::normal_distribution.
void theta_par(RealType value)
Set theta_par, keeping the other parameters; throws as define() does.
param_type param() const
The current parameters, for example to test param().capped().