|
bi-kappa 3.0.0
Monte Carlo particle samplers for plasma velocity and position distributions
|
Bi-Kappa distribution class for sampling 3D velocity vectors. More...
#include <bi_kappa_distribution.H>
Classes | |
| class | param_type |
| The parameters of a bi_kappa_distribution: kappa, theta_perp, theta_par, ub and the velocity cap. More... | |
Public Types | |
| using | result_type = std::array<RealType, 3> |
| A velocity {vx, vy, vz}. | |
| using | point_type = result_type |
| Same as result_type. | |
Public Member Functions | |
| bi_kappa_distribution () | |
| Construct with kappa = 2, theta_perp = theta_par = 1, ub = {0, 0, 1} and a cap of 20. | |
| bi_kappa_distribution (RealType kappa, 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)) | |
| Construct and call define() with these arguments. | |
| bi_kappa_distribution (const param_type ¶ms) | |
| Construct from a parameter set. | |
| void | define (const param_type ¶ms) |
Replace all parameters with params (already validated by its constructor), then call reset(). | |
| void | define (RealType kappa, 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)) |
| Configure the sampler. | |
| void | define (const param_type ¶ms, int seed) |
define(params), after seeding the internal engine with seed as seed() does. | |
| void | define (RealType kappa, RealType theta_perp, RealType theta_par, point_type ub, RealType max_normalized_velocity, int seed) |
define() with all five parameters, after seeding the internal engine with seed as seed() does (seed >= 0 reproducible, seed < 0 from std::random_device). | |
| void | kappa (RealType value) |
| Set kappa, keeping the other parameters; throws as define() does. | |
| void | theta_perp (RealType value) |
| Set theta_perp, keeping the other parameters; throws as define() does. | |
| void | theta_par (RealType value) |
| Set theta_par, keeping the other parameters; throws as define() does. | |
| void | ub (const point_type &value) |
| Set the field direction, keeping the other parameters; throws as define() does. | |
| void | max_normalized_velocity (RealType value) |
| Set the velocity cap, keeping the other parameters. | |
| param_type | param () const |
| The current parameters, for example to test param().capped(). | |
| RealType | kappa () const |
| Spectral index. | |
| RealType | theta_perp () const |
| Perpendicular thermal speed. | |
| RealType | theta_par () const |
| Parallel thermal speed. | |
| point_type | ub () const |
| Field direction, as given. | |
| RealType | max_normalized_velocity () const |
| Velocity cap in thermal speeds; +infinity for no_cap(). | |
| void | seed (int s) |
| Reseed the internal engine. | |
| void | reset () |
| Discard any variate cached inside the sampler and zero the observability counters. | |
| unsigned long long | n_attempts () const |
| Core-mapping attempts made since the last reset(), define() or seed(). | |
| unsigned long long | n_nonfinite () const |
Attempts since the last reset(), define() or seed() whose intended velocity vector had no representation in RealType. | |
| template<typename GeneratorType> | |
| result_type | operator() (GeneratorType &gen) |
Draw one velocity from the engine gen instead of the internal one. | |
| result_type | operator() () |
| Draw one velocity from the internal engine. | |
| template<typename GeneratorType> | |
| result_type | operator() (GeneratorType &gen, const param_type ¶ms) |
Draw one velocity from gen with the parameters params instead of the stored ones. | |
| 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, for the field direction ub. | |
| bool | operator== (const bi_kappa_distribution &other) const |
| True if the two samplers have equal parameters. | |
| bool | operator!= (const bi_kappa_distribution &other) const |
| Negation of operator==. | |
Static Public Member Functions | |
| static constexpr RealType | no_cap () |
| Value of the cap that selects the uncapped sampler: +infinity. | |
| static point_type | rotateFromFieldAlignedFrame (const point_type &local, const point_type &ub) |
| The same rotation as rotate_from_fieldAligned_frame(), as a static function that needs no instance. | |
Bi-Kappa distribution class for sampling 3D velocity vectors.
This class follows the interface pattern of standard library distributions like std::uniform_real_distribution<T>.
RealType. Such a draw cannot be returned as a number. Without a cap it is returned with infinite components, and n_nonfinite() counts it. Because no intermediate of the radius can underflow, these are the only draws lost: in double at kappa = 0.505 they are about 8.3e-4 of all draws. Finite precision near kappa = 1/2 gives more values.| RealType | The floating-point type (e.g., double, float) |
Definition at line 314 of file bi_kappa_distribution.H.
| using bi_kappa_distribution< RealType >::point_type = result_type |
Same as result_type.
Definition at line 318 of file bi_kappa_distribution.H.
| using bi_kappa_distribution< RealType >::result_type = std::array<RealType, 3> |
A velocity {vx, vy, vz}.
Definition at line 317 of file bi_kappa_distribution.H.
|
inline |
Construct with kappa = 2, theta_perp = theta_par = 1, ub = {0, 0, 1} and a cap of 20.
The internal engine is seeded from std::random_device.
Definition at line 446 of file bi_kappa_distribution.H.
|
inlineexplicit |
Construct and call define() with these arguments.
The internal engine is seeded from std::random_device; call seed() for a reproducible stream.
Definition at line 454 of file bi_kappa_distribution.H.
|
inlineexplicit |
Construct from a parameter set.
The internal engine is seeded from std::random_device.
Definition at line 468 of file bi_kappa_distribution.H.
|
inline |
Replace all parameters with params (already validated by its constructor), then call reset().
Definition at line 481 of file bi_kappa_distribution.H.
Referenced by bi_kappa_distribution(), define(), kappa(), main(), max_normalized_velocity(), theta_par(), theta_perp(), and ub().
|
inline |
define(params), after seeding the internal engine with seed as seed() does.
Definition at line 533 of file bi_kappa_distribution.H.
Referenced by define().
|
inline |
|
inline |
Configure the sampler.
Every parameter is checked here, so an invalid one throws from define() rather than from the first draw.
| kappa | Spectral index. Smaller means heavier suprathermal tails; kappa -> infinity recovers the bi-Maxwellian. Must be > 0.5. Note that moments of order l exist only for kappa > (l+1)/2, so for kappa <= 1.5 the loaded population has no finite variance and no convergent temperature. |
| theta_perp | Thermal speed transverse to ub, > 0 and finite. It is the scale in the density, a speed rather than a temperature; see Thermal speeds: theta_perp, theta_par for its relation to a temperature. |
| theta_par | Thermal speed along ub, > 0 and finite. |
| ub | Magnetic-field direction {bx,by,bz} in the global frame. Need not be normalized; must be non-zero, with a length above about 1.5e-8 in double (3.5e-4 in float); see Field direction: ub. Samples are drawn in the field-aligned frame and rotated into the global frame before return. |
| max_normalized_velocity | Velocity cap in thermal speeds (default 20), or no_cap() for the bi-Kappa distribution itself. A finite value selects a different, bounded target distribution; see The velocity cap. Must be > 0; passing 0 to mean "no cap" throws. |
| std::invalid_argument | if kappa is not greater than 0.5, either theta is not positive and finite, a component of ub is not finite, ub is the zero vector, or max_normalized_velocity is not positive. A NaN in any argument throws. A ub that is non-zero but shorter than the limit above passes define() and makes the first draw throw std::invalid_argument. |
Definition at line 524 of file bi_kappa_distribution.H.
|
inline |
Spectral index.
Definition at line 595 of file bi_kappa_distribution.H.
|
inline |
Set kappa, keeping the other parameters; throws as define() does.
Definition at line 548 of file bi_kappa_distribution.H.
|
inline |
Velocity cap in thermal speeds; +infinity for no_cap().
Definition at line 600 of file bi_kappa_distribution.H.
|
inline |
Set the velocity cap, keeping the other parameters.
Unlike the other setters, this one changes which distribution is sampled, not merely its parameters. Pass no_cap() to return to the bi-Kappa distribution itself; passing 0 to mean "no cap" throws, as every non-positive value does.
dist.max_normalized_velocity(50.0); // capped distribution dist.max_normalized_velocity(decltype(dist)::no_cap()); // bi-Kappa distribution
Definition at line 582 of file bi_kappa_distribution.H.
|
inline |
Core-mapping attempts made since the last reset(), define() or seed().
In untruncated mode this equals the number of returned samples; in capped mode it also counts the draws the box rejected.
Definition at line 631 of file bi_kappa_distribution.H.
|
inline |
Attempts since the last reset(), define() or seed() whose intended velocity vector had no representation in RealType.
Sufficiently near kappa = 1/2 the bi-Kappa distribution puts non-zero probability outside every finite floating-point range, so such draws exist as mathematics and cannot be returned as numbers. Untruncated mode returns them as non-finite components and counts them here. Capped mode rejects them, as it rejects anything outside the box, and still counts them here, so that a caller can tell "the box rejected a representable sample" from "the box rejected a sample that could not have been returned at all". In both modes the vector whose representability is counted is the velocity the caller would have received, rotation and thermal scaling included – not the normalized coordinate the cap predicate is written in, which understates the largest component whenever theta_par and theta_perp differ. Without this counter the second case is invisible: the returned samples are all finite and the acceptance rate alone does not distinguish the two.
Definition at line 649 of file bi_kappa_distribution.H.
|
inlinestaticconstexpr |
Value of the cap that selects the uncapped sampler: +infinity.
In this mode each draw uses a fixed sequence of variates with no outer acceptance-rejection loop, and the samples follow the bi-Kappa distribution itself.
It must be passed explicitly, because the default cap is 20:
bi_kappa_distribution<double> d(2.0, 1.0, 2.0, {0,0,1},
bi_kappa_distribution<double>::no_cap());
With a finite cap the sampled law is instead that distribution conditioned on all normalized components lying inside the component-wise box – a different, bounded distribution with its own normalization. At kappa <= 3/2 no practical cap value gives a good approximation to the bi-Kappa distribution, and the conditioned distribution is not symmetric under rotation about the field.
The cap is tested in the field-aligned frame, before the rotation into the global frame, so the returned components are not individually bounded by cap*theta unless ub is {0,0,1}; the box rotates with ub. The speed is bounded in every frame, by cap*sqrt(2*theta_perp^2 + theta_par^2).
Definition at line 436 of file bi_kappa_distribution.H.
Referenced by main().
|
inline |
Negation of operator==.
Definition at line 961 of file bi_kappa_distribution.H.
|
inline |
Draw one velocity from the internal engine.
Definition at line 665 of file bi_kappa_distribution.H.
|
inline |
Draw one velocity from the engine gen instead of the internal one.
The engine must produce a power-of-two number of distinct values (max() - min() + 1), as std::mt19937 and std::mt19937_64 do; this is not checked at compile time.
Definition at line 658 of file bi_kappa_distribution.H.
|
inline |
Draw one velocity from gen with the parameters params instead of the stored ones.
The counters and the cached normal variate of this object are still used.
| std::runtime_error | after 10^6 consecutive rejections by a finite cap. |
Definition at line 674 of file bi_kappa_distribution.H.
|
inline |
True if the two samplers have equal parameters.
Engine state and counters are not compared.
Definition at line 958 of file bi_kappa_distribution.H.
|
inline |
The current parameters, for example to test param().capped().
Definition at line 593 of file bi_kappa_distribution.H.
|
inline |
|
inline |
Rotate local = {v_perp1, v_perp2, v_par} from field-aligned components to the global frame, for the field direction ub.
The perpendicular axes are the ones the sampler uses. Throws std::invalid_argument if ub is numerically zero.
Definition at line 887 of file bi_kappa_distribution.H.
|
inlinestatic |
The same rotation as rotate_from_fieldAligned_frame(), as a static function that needs no instance.
Definition at line 894 of file bi_kappa_distribution.H.
Referenced by operator()(), and rotate_from_fieldAligned_frame().
|
inline |
Reseed the internal engine.
This also calls reset(), so a reseeded stream restarts cleanly: without it the cached second normal variate of the previous stream would be consumed by the first draw of the new one, and seed(s); draw() would not reproduce.
Definition at line 608 of file bi_kappa_distribution.H.
|
inline |
Parallel thermal speed.
Definition at line 597 of file bi_kappa_distribution.H.
|
inline |
Set theta_par, keeping the other parameters; throws as define() does.
Definition at line 562 of file bi_kappa_distribution.H.
|
inline |
Perpendicular thermal speed.
Definition at line 596 of file bi_kappa_distribution.H.
|
inline |
Set theta_perp, keeping the other parameters; throws as define() does.
Definition at line 555 of file bi_kappa_distribution.H.
|
inline |
Field direction, as given.
Definition at line 598 of file bi_kappa_distribution.H.
|
inline |
Set the field direction, keeping the other parameters; throws as define() does.
Definition at line 569 of file bi_kappa_distribution.H.