|
bi-kappa 3.0.0
Monte Carlo particle samplers for plasma velocity and position distributions
|
Every argument of every sampler.
In the tables, "Default" is the value define(...) uses when the argument is omitted, and required marks an argument define(...) has no default for. bi_kappa_distribution and bi_maxwellian_distribution can also draw before any define(...) call; the values they then use are given below their tables. The other three samplers throw if called before define(...). A value outside its valid range makes define(...) throw std::invalid_argument, except for the few cases listed in Exceptions, which are caught later or not at all.
theta is a thermal speed, not a temperature, and has units of velocity. theta_perp applies to both directions perpendicular to ub and theta_par to the direction along it. Both must be positive.
For the bi-Maxwellian, theta = sqrt(2 k_B T / m) and a velocity component is distributed as N(0, theta^2 / 2).
For the bi-Kappa distribution, theta is the scale in its density,
f(v) ~ [1 + v_perp^2 / (kappa theta_perp^2) + v_par^2 / (kappa theta_par^2)]^-(kappa+1)
and the samplers do not tie it to a temperature. For kappa > 3/2 each velocity component has a finite variance,
Var(v_perp1) = Var(v_perp2) = kappa theta_perp^2 / (2 kappa - 3), Var(v_par) = kappa theta_par^2 / (2 kappa - 3).
A population whose kinetic temperature along a direction is T, in the sense k_B T = m Var(v_i), therefore has theta = sqrt((2 - 3/kappa) k_B T / m) for that direction. This tends to the bi-Maxwellian value as kappa -> infinity. For kappa <= 3/2 the variance is infinite and there is no such temperature.
A 3-vector {bx, by, bz} giving the magnetic-field direction in the global frame.
The two perpendicular axes are chosen internally. They matter only for a finite velocity cap, whose box is aligned with them (see The velocity cap).
seed(s) can be called at any time. bi_kappa_distribution and bi_maxwellian_distribution also accept the seed as the last argument of define(...).
| Value | Effect |
|---|---|
| s >= 0 | The same seed reproduces the same sequence. |
| s < 0 | Seeds from std::random_device. |
Without a seed the internal engine is seeded from std::random_device at construction, so runs are not reproducible.
dist(gen) draws from an engine you supply instead of the internal one, for example one std::mt19937 shared by several samplers. For bi_kappa_distribution 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: std::minstd_rand, which produces 2^31 - 2 distinct values, compiles without a warning, and the variates drawn from it are then not exactly uniform.
A sampler object holds its engine, a cached variate and, for bi_kappa_distribution, two counters, so one object must not be used by several threads at once. In a parallel code give each thread or MPI rank its own object and a distinct seed, for example base_seed + rank.
For bi_kappa_distribution, a given seed gives the same values under libc++ and libstdc++ on the same architecture. Compile with -ffp-contract=off to rule out differences from fused multiply-add. Values are not bitwise identical across architectures. CHANGELOG.md lists the versions in which the values for a given seed changed.
These apply to the three samplers that take a user function (general_velocity_generator, field_aligned_velocity_generator, general_position_generator).
| Parameter | Meaning | Default |
|---|---|---|
| probe_points | Number of points at which your function is evaluated once, in define(...), to estimate its maximum. The largest value found, times 1.05, is the rejection envelope. The velocity generators use evenly spaced points from w_min to w_max inclusive. The position generator uses uniformly random points in the box, drawn from a separate engine with a fixed seed, so they are the same for every call with the same box and do not depend on seed(). | 1024 (velocity generators, must be >= 64); 4096 (position generator, must be > 0) |
| max_reject_tries | Candidate draws allowed per sample before operator() throws std::runtime_error. | 100000 (velocity generators); 200000 (position generator) |
If a sampler exhausts max_reject_tries, the function is usually small over most of its range. Narrowing the range is more effective than raising the limit.
define(kappa, theta_perp, theta_par, ub, max_normalized_velocity, seed)
| Parameter | Meaning | Default | Valid range |
|---|---|---|---|
| kappa | Spectral index. Smaller values give heavier suprathermal tails; kappa -> infinity gives the bi-Maxwellian. | required | > 0.5, finite |
| theta_perp | Thermal speed perpendicular to ub. | 1.0 | > 0, finite |
| theta_par | Thermal speed along ub. | 1.0 | > 0, finite |
| ub | Field direction. See Field direction: ub. | {0, 0, 1} | non-zero, finite |
| max_normalized_velocity | Velocity cap in thermal speeds, or no_cap(). See The velocity cap. | 20 | > 0, or no_cap() |
| seed | See Seeding and random engines. | engine not reseeded | any int |
define(...) also rejects NaN values, infinite thermal speeds and non-finite ub components. It does not reject kappa = +infinity; use bi_maxwellian_distribution for that limit. A default-constructed bi_kappa_distribution has kappa = 2, theta_perp = theta_par = 1, ub = {0, 0, 1} and a cap of 20.
Moments of order l exist only for kappa > (l + 1) / 2. For 0.5 < kappa <= 1.5 the uncapped distribution has no finite second moment, so a temperature computed from the loaded particles does not converge as the number of particles grows. Use quantiles to characterize such a sample.
As kappa approaches 1/2, the bi-Kappa distribution puts a non-negligible probability on speeds above the largest representable floating-point number. For theta_perp = theta_par = 1 and ub = {0, 0, 1}, this probability is about 8.3e-4 at kappa = 0.505 and 6.8e-7 at kappa = 0.51 in double, and about 1.4e-4 at kappa = 0.55 and 1.9e-8 at kappa = 0.60 in float. At kappa = 0.75 it is about 5e-20 in float. bi_kappa_distribution<float> is therefore suitable when kappa is not close to 1/2 and about seven significant digits per component are enough; closer to 1/2, use double.
The sampler computes in the type it is instantiated with: bi_kappa_distribution<float> does all of its arithmetic in float. The relative error of a large radius grows like eps |log R|, which near FLT_MAX is about 1e-5. A float component whose exact value lies within that distance of FLT_MAX may therefore be returned either as a finite number or as an infinity.
Compiling with -ffast-math lets the compiler assume that no infinity occurs, so no_cap() and the counters are unreliable in that mode. The header then prints a compile-time note; define BI_KAPPA_ALLOW_FAST_MATH to silence it. A finite cap still works.
The paper's section on finite precision gives the derivation.
max_normalized_velocity (here cap) is a finite positive value or no_cap(). It works the same way on bi_kappa_distribution and bi_maxwellian_distribution.
With a finite cap, a draw is accepted only if every component lies inside the box
|v_perp1| / theta_perp <= cap, |v_perp2| / theta_perp <= cap, |v_par| / theta_par <= cap
in field-aligned coordinates. Otherwise it is redrawn. The samples therefore follow the distribution conditioned on the box: the density restricted to the box and renormalized. The sampler throws std::runtime_error after 10^6 consecutive rejections.
define(theta_perp, theta_par, ub, max_normalized_velocity, seed)
| Parameter | Meaning | Default | Valid range |
|---|---|---|---|
| theta_perp | Thermal speed perpendicular to ub. See Thermal speeds: theta_perp, theta_par. | required | > 0, finite |
| theta_par | Thermal speed along ub. | 1.0 | > 0, finite |
| ub | Field direction. See Field direction: ub. | {0, 0, 1} | non-zero, finite |
| max_normalized_velocity | Velocity cap in thermal speeds, or no_cap(). See The velocity cap. | 20 | > 0, or no_cap() |
| seed | See Seeding and random engines. | engine not reseeded | any int |
define(...) rejects a zero or negative thermal speed and a cap that is not positive. It does not check ub and does not reject a NaN or infinite thermal speed; a zero ub makes every draw throw (see Exceptions). A default-constructed bi_maxwellian_distribution has theta_perp = theta_par = 1, ub = {0, 0, 1} and a cap of 20.
define(speed_sq_pdf, speed_sq_min, speed_sq_max, probe_points, max_reject_tries)
| Parameter | Meaning | Default | Valid range |
|---|---|---|---|
| speed_sq_pdf | Your density g(w) in the speed squared w = vx^2 + vy^2 + vz^2. It need not be normalized. | required | callable; finite, non-negative, positive somewhere in range |
| speed_sq_min | Lower end of the sampled w. | required | >= 0 |
| speed_sq_max | Upper end of the sampled w. | required | >= speed_sq_min |
| probe_points | See Rejection-sampler settings: probe_points, max_reject_tries. | 1024 | >= 64 |
| max_reject_tries | See Rejection-sampler settings: probe_points, max_reject_tries. | 100000 | > 0 |
The speed is sqrt(w) and the direction is isotropic. The generator takes no mass: for a distribution written in energy, use w = 2E/m. For example, a Maxwellian with thermal speed theta is g(w) = sqrt(w) exp(-w / theta^2), since dw = 2 |v| d|v| and the speed density is |v|^2 exp(-|v|^2 / theta^2).
[speed_sq_min, speed_sq_max] is the support: no sample lies outside it. If speed_sq_min == speed_sq_max, every sample has speed sqrt(speed_sq_min) and g is not evaluated.
A call written for the 1.0.0 interface define(f, E_min, E_max, particle_mass, ...) passes the mass as probe_points, and throws if the mass is below 64; see CHANGELOG.md.
define(speed_sq_pdf, speed_sq_min, speed_sq_max, theta_perp, ub, parallel_sign,
probe_points, max_reject_tries, max_normalized_velocity)
| Parameter | Meaning | Default | Valid range |
|---|---|---|---|
| speed_sq_pdf | Your density g(w) for the parallel component, with w = v_par^2. | required | as above |
| speed_sq_min | Lower end of the sampled v_par^2. | required | >= 0 |
| speed_sq_max | Upper end of the sampled v_par^2. | required | >= speed_sq_min |
| theta_perp | Thermal speed of the two Maxwellian perpendicular components, each N(0, theta_perp^2 / 2). | required | > 0, finite |
| ub | Field direction. See Field direction: ub. | required | non-zero |
| parallel_sign | +1 puts the parallel component along ub, -1 opposite to it. | required | +1 or -1 |
| probe_points | See Rejection-sampler settings: probe_points, max_reject_tries. | 1024 | >= 64 |
| max_reject_tries | See Rejection-sampler settings: probe_points, max_reject_tries. | 100000 | > 0 |
| max_normalized_velocity | Cap on each perpendicular component, in units of theta_perp. A pair with a component above cap * theta_perp is redrawn. | 20 | > 0, or no_cap() |
Every sample has the same sign of v_par, which suits beams and drifting populations. For a symmetric parallel distribution, draw the sign yourself or use bi_kappa_distribution or bi_maxwellian_distribution. If speed_sq_min == speed_sq_max, every sample has parallel speed sqrt(speed_sq_min). Velocities are returned in the global frame.
define(dimension, lower_bounds, upper_bounds, density_function, probe_points,
max_reject_tries)
| Parameter | Meaning | Default | Valid range |
|---|---|---|---|
| dimension | Number of spatial dimensions. | required | 1, 2 or 3 |
| lower_bounds | Lower corner of the sampling box, a std::vector<Real>. | required | size dimension |
| upper_bounds | Upper corner. | required | size dimension, each above its lower bound |
| density_function | Your density rho(x), taking a std::vector<Real> of length dimension. It need not be normalized. | required | callable; finite, non-negative, positive somewhere |
| probe_points | See Rejection-sampler settings: probe_points, max_reject_tries. | 4096 | > 0 |
| max_reject_tries | See Rejection-sampler settings: probe_points, max_reject_tries. | 200000 | > 0 |
The returned point is a std::vector<Real> of length dimension.
| Exception | Thrown by | Cause |
|---|---|---|
| std::invalid_argument | define(...) and the single-parameter setters | A value outside the ranges above, a null callable, or mismatched bound sizes. Not detected here: NaN or infinite thermal speeds and any ub for bi_maxwellian_distribution; a NaN or infinite theta_perp and a NaN ub component for field_aligned_velocity_generator; kappa = +infinity for bi_kappa_distribution. |
| std::invalid_argument | operator() of bi_kappa_distribution and bi_maxwellian_distribution | A ub whose length is below the limit in Field direction: ub. bi_maxwellian_distribution checks ub only here, so a zero ub passes its define(...). bi_kappa_distribution rejects an exactly zero ub in define(...), but a non-zero ub below the limit reaches this check. |
| std::runtime_error | operator() | A general or field-aligned generator called before define(...); max_reject_tries exhausted; 10^6 consecutive cap rejections; or your function returned a negative or non-finite value. |