bi-kappa 3.0.0
Monte Carlo particle samplers for plasma velocity and position distributions
Loading...
Searching...
No Matches
bi_kappa_distribution< RealType > Class Template Reference

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 &params)
 Construct from a parameter set.
void define (const param_type &params)
 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 &params, 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 &params)
 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.

Detailed Description

template<typename RealType = double>
class bi_kappa_distribution< RealType >

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>.

dist.define(2.0, 1.0, 2.0, {0, 0, 1}); // kappa, theta_perp, theta_par, ub
auto v = dist(); // {vx, vy, vz}, global frame
void define(const param_type &params)
Replace all parameters with params (already validated by its constructor), then call reset().
bi_kappa_distribution()
Construct with kappa = 2, theta_perp = theta_par = 1, ub = {0, 0, 1} and a cap of 20.
Note
The cap defaults to 20 thermal speeds, so the default target is the bi-Kappa distribution conditioned on the component-wise box |v_i|/theta_i <= 20 in field-aligned coordinates. Pass no_cap() to sample the bi-Kappa distribution itself. At kappa <= 3/2 a cap of 20 removes an appreciable part of the tail; see The velocity cap, no_cap() and param_type::capped().
The radius is built in the logarithmic domain. R = sqrt(X1)/sqrt(X2) with X2 ~ Gamma(kappa - 1/2, 1), so as kappa -> 1/2 the denominator's shape goes to zero and X2 underflows to exactly zero. X2 is therefore never formed: the sampler carries log X2 and log R, and reaches the linear domain only at the last step, once per component. Whether a component is representable is then decided from the component itself – the number about to be returned – and never from its logarithm, because log(max()) is a rounded value that can sit on either side of the true limit; see bikappa_detail::materializeComponents. Two consequences a caller should know about:
  • Reproducibility. The Gamma, normal and uniform variates are generated by the code in bikappa_detail rather than by <random>, and the direction is drawn by a rejection method that uses no trigonometric function. A given seed therefore gives bit-identical output under libc++ and libstdc++ on the same architecture. The output still depends on whether the compiler fuses a multiply and an add; pass -ffp-contract=off to rule that out. Across architectures the output is not bitwise identical, because log and exp are not correctly rounded and their implementations differ. CHANGELOG.md lists the versions whose random stream differs.
  • Non-finite output near kappa = 1/2. Sufficiently near kappa = 1/2 the bi-Kappa distribution puts non-zero probability on velocities beyond the largest finite value of 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.
See also
Usage examples for more recipes (seeding, external RNG, reconfiguration).
Template Parameters
RealTypeThe floating-point type (e.g., double, float)

Definition at line 314 of file bi_kappa_distribution.H.

Member Typedef Documentation

◆ point_type

template<typename RealType = double>
using bi_kappa_distribution< RealType >::point_type = result_type

Same as result_type.

Definition at line 318 of file bi_kappa_distribution.H.

◆ result_type

template<typename RealType = double>
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.

Constructor & Destructor Documentation

◆ bi_kappa_distribution() [1/3]

template<typename RealType = double>
bi_kappa_distribution< RealType >::bi_kappa_distribution ( )
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.

◆ bi_kappa_distribution() [2/3]

template<typename RealType = double>
bi_kappa_distribution< RealType >::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) )
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.

◆ bi_kappa_distribution() [3/3]

template<typename RealType = double>
bi_kappa_distribution< RealType >::bi_kappa_distribution ( const param_type & params)
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.

Member Function Documentation

◆ define() [1/4]

template<typename RealType = double>
void bi_kappa_distribution< RealType >::define ( const param_type & params)
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().

◆ define() [2/4]

template<typename RealType = double>
void bi_kappa_distribution< RealType >::define ( const param_type & params,
int seed )
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().

◆ define() [3/4]

template<typename RealType = double>
void bi_kappa_distribution< RealType >::define ( RealType kappa,
RealType theta_perp,
RealType theta_par,
point_type ub,
RealType max_normalized_velocity,
int seed )
inline

define() with all five parameters, after seeding the internal engine with seed as seed() does (seed >= 0 reproducible, seed < 0 from std::random_device).

Definition at line 537 of file bi_kappa_distribution.H.

◆ define() [4/4]

template<typename RealType = double>
void bi_kappa_distribution< RealType >::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) )
inline

Configure the sampler.

Every parameter is checked here, so an invalid one throws from define() rather than from the first draw.

Parameters
kappaSpectral 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_perpThermal 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_parThermal speed along ub, > 0 and finite.
ubMagnetic-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_velocityVelocity 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.
Note
To seed reproducibly, either call seed() afterwards or use the overload taking a trailing int: >= 0 is reproducible, < 0 reseeds from std::random_device. Without either, the stream stays as seeded at construction, which is not reproducible.
Exceptions
std::invalid_argumentif 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.
See also
Parameter reference for the full table, Choosing a sampler to compare samplers.

Definition at line 524 of file bi_kappa_distribution.H.

◆ kappa() [1/2]

template<typename RealType = double>
RealType bi_kappa_distribution< RealType >::kappa ( ) const
inline

Spectral index.

Definition at line 595 of file bi_kappa_distribution.H.

◆ kappa() [2/2]

template<typename RealType = double>
void bi_kappa_distribution< RealType >::kappa ( RealType value)
inline

Set kappa, keeping the other parameters; throws as define() does.

Definition at line 548 of file bi_kappa_distribution.H.

◆ max_normalized_velocity() [1/2]

template<typename RealType = double>
RealType bi_kappa_distribution< RealType >::max_normalized_velocity ( ) const
inline

Velocity cap in thermal speeds; +infinity for no_cap().

Definition at line 600 of file bi_kappa_distribution.H.

◆ max_normalized_velocity() [2/2]

template<typename RealType = double>
void bi_kappa_distribution< RealType >::max_normalized_velocity ( RealType value)
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.

◆ n_attempts()

template<typename RealType = double>
unsigned long long bi_kappa_distribution< RealType >::n_attempts ( ) const
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.

◆ n_nonfinite()

template<typename RealType = double>
unsigned long long bi_kappa_distribution< RealType >::n_nonfinite ( ) const
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.

◆ no_cap()

template<typename RealType = double>
constexpr RealType bi_kappa_distribution< RealType >::no_cap ( )
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).

See also
The velocity cap and The velocity cap for what the capped distribution is, and why a small rejected fraction does not imply a faithful tail.

Definition at line 436 of file bi_kappa_distribution.H.

Referenced by main().

◆ operator!=()

template<typename RealType = double>
bool bi_kappa_distribution< RealType >::operator!= ( const bi_kappa_distribution< RealType > & other) const
inline

Negation of operator==.

Definition at line 961 of file bi_kappa_distribution.H.

◆ operator()() [1/3]

template<typename RealType = double>
result_type bi_kappa_distribution< RealType >::operator() ( )
inline

Draw one velocity from the internal engine.

Returns
{vx, vy, vz} in the global frame.

Definition at line 665 of file bi_kappa_distribution.H.

◆ operator()() [2/3]

template<typename RealType = double>
template<typename GeneratorType>
result_type bi_kappa_distribution< RealType >::operator() ( GeneratorType & gen)
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.

Returns
{vx, vy, vz} in the global frame.

Definition at line 658 of file bi_kappa_distribution.H.

◆ operator()() [3/3]

template<typename RealType = double>
template<typename GeneratorType>
result_type bi_kappa_distribution< RealType >::operator() ( GeneratorType & gen,
const param_type & params )
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.

Exceptions
std::runtime_errorafter 10^6 consecutive rejections by a finite cap.

Definition at line 674 of file bi_kappa_distribution.H.

◆ operator==()

template<typename RealType = double>
bool bi_kappa_distribution< RealType >::operator== ( const bi_kappa_distribution< RealType > & other) const
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.

◆ param()

template<typename RealType = double>
param_type bi_kappa_distribution< RealType >::param ( ) const
inline

The current parameters, for example to test param().capped().

Definition at line 593 of file bi_kappa_distribution.H.

◆ reset()

template<typename RealType = double>
void bi_kappa_distribution< RealType >::reset ( )
inline

Discard any variate cached inside the sampler and zero the observability counters.

Called by seed() and by every define() overload.

Definition at line 616 of file bi_kappa_distribution.H.

Referenced by define(), and seed().

◆ rotate_from_fieldAligned_frame()

template<typename RealType = double>
point_type bi_kappa_distribution< RealType >::rotate_from_fieldAligned_frame ( const point_type & local,
const point_type & ub ) const
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.

◆ rotateFromFieldAlignedFrame()

template<typename RealType = double>
point_type bi_kappa_distribution< RealType >::rotateFromFieldAlignedFrame ( const point_type & local,
const point_type & ub )
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().

◆ seed()

template<typename RealType = double>
void bi_kappa_distribution< RealType >::seed ( int s)
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.

◆ theta_par() [1/2]

template<typename RealType = double>
RealType bi_kappa_distribution< RealType >::theta_par ( ) const
inline

Parallel thermal speed.

Definition at line 597 of file bi_kappa_distribution.H.

◆ theta_par() [2/2]

template<typename RealType = double>
void bi_kappa_distribution< RealType >::theta_par ( RealType value)
inline

Set theta_par, keeping the other parameters; throws as define() does.

Definition at line 562 of file bi_kappa_distribution.H.

◆ theta_perp() [1/2]

template<typename RealType = double>
RealType bi_kappa_distribution< RealType >::theta_perp ( ) const
inline

Perpendicular thermal speed.

Definition at line 596 of file bi_kappa_distribution.H.

◆ theta_perp() [2/2]

template<typename RealType = double>
void bi_kappa_distribution< RealType >::theta_perp ( RealType value)
inline

Set theta_perp, keeping the other parameters; throws as define() does.

Definition at line 555 of file bi_kappa_distribution.H.

◆ ub() [1/2]

template<typename RealType = double>
point_type bi_kappa_distribution< RealType >::ub ( ) const
inline

Field direction, as given.

Definition at line 598 of file bi_kappa_distribution.H.

◆ ub() [2/2]

template<typename RealType = double>
void bi_kappa_distribution< RealType >::ub ( const point_type & value)
inline

Set the field direction, keeping the other parameters; throws as define() does.

Definition at line 569 of file bi_kappa_distribution.H.


The documentation for this class was generated from the following file: