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

Samples field-aligned velocities: an arbitrary parallel speed-squared distribution along B, with a Maxwellian perpendicular spread. More...

#include <field_aligned_velocity_generator.H>

Public Types

using result_type = std::array<RealType, 3>
 A velocity {vx, vy, vz}.
using point_type = result_type
 Same as result_type.
using speed_sq_pdf_function = std::function<RealType(RealType)>
 The type of the density g(w) passed to define().

Public Member Functions

 field_aligned_velocity_generator ()
 Construct an undefined generator; call define() before drawing.
 field_aligned_velocity_generator (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))
 Construct and call define() with these arguments.
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.
void seed (int s)
 Reseed the internal engine and discard any variate cached from before, so that seed(s) followed by n draws gives the same values however many draws came first.
void reset ()
 Discard any state cached by the internal distributions, including the second normal variate of std::normal_distribution.
template<typename GeneratorType>
point_type operator() (GeneratorType &gen)
 Draw one velocity from the engine gen instead of the internal one.
point_type operator() ()
 Draw one velocity from the internal engine; throws as operator()(gen) does.
RealType parallelSpeedSqMin () const
 Lower end of the sampled v_par^2.
RealType parallelSpeedSqMax () const
 Upper end of the sampled v_par^2.
RealType thetaPerp () const
 Perpendicular thermal speed.
point_type ub () const
 Field direction, as given.
RealType parallelSign () const
 +1 or -1, as a RealType.
RealType max_normalized_velocity () const
 Cap on each perpendicular component, in units of theta_perp; +infinity for no_cap().
RealType pdfUpperBound () const
 The rejection envelope: the largest probed value of g times 1.05, or 0 when speed_sq_min == speed_sq_max.
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.

Static Public Member Functions

static constexpr RealType no_cap ()
 Value of max_normalized_velocity that removes the perpendicular cap.

Detailed Description

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

Samples field-aligned velocities: an arbitrary parallel speed-squared distribution along B, with a Maxwellian perpendicular spread.

The parallel component is w_par = v_par^2, drawn from a user-defined distribution g(w_par); the parallel speed is |v_par| = sqrt(w_par) and its direction is set by a fixed sign (+1 along B, -1 opposite B). (For a distribution originally posed in energy, w_par = v_par^2 = 2E_par/m, so the mass drops out.) If speed_sq_min == speed_sq_max, every sample has the single parallel speed sqrt(speed_sq_min). The two perpendicular components are independent Gaussians with N(0, theta_perp^2/2), i.e. a Maxwellian with thermal speed theta_perp (same convention as bi_maxwellian_distribution), each capped at max_normalized_velocity thermal speeds (default 20, as in bi_maxwellian_distribution). The result is returned in the global (lab) frame.

auto gW = [](double w){ return std::exp(-0.5 * w); }; // any positive g(w), w = v_par^2
gen.define(gW, 0.0, 40.0, 1.0, {0, 0, 1}, +1); // g, v2_min, v2_max,
// theta_perp, B, sign
gen.seed(12345);
auto v = gen(); // {vx, vy, vz}
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.
See also
Usage examples for more recipes.

Definition at line 38 of file field_aligned_velocity_generator.H.

Member Typedef Documentation

◆ point_type

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

Same as result_type.

Definition at line 42 of file field_aligned_velocity_generator.H.

◆ result_type

template<typename RealType = double>
using field_aligned_velocity_generator< RealType >::result_type = std::array<RealType, 3>

A velocity {vx, vy, vz}.

Definition at line 41 of file field_aligned_velocity_generator.H.

◆ speed_sq_pdf_function

template<typename RealType = double>
using field_aligned_velocity_generator< RealType >::speed_sq_pdf_function = std::function<RealType(RealType)>

The type of the density g(w) passed to define().

Definition at line 44 of file field_aligned_velocity_generator.H.

Constructor & Destructor Documentation

◆ field_aligned_velocity_generator() [1/2]

template<typename RealType = double>
field_aligned_velocity_generator< RealType >::field_aligned_velocity_generator ( )
inline

Construct an undefined generator; call define() before drawing.

The internal engine is seeded from std::random_device.

Definition at line 48 of file field_aligned_velocity_generator.H.

Referenced by field_aligned_velocity_generator().

◆ field_aligned_velocity_generator() [2/2]

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

Construct and call define() with these arguments.

Definition at line 61 of file field_aligned_velocity_generator.H.

References field_aligned_velocity_generator(), define(), max_normalized_velocity(), and ub().

Member Function Documentation

◆ define()

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

Configure the sampler: user-supplied law along the field, Maxwellian across it.

Parameters
speed_sq_pdfYour g(w) for the parallel component, whose argument is v_par squared, not v_par. Need not be normalized; must be finite, non-negative, and positive somewhere in range.
speed_sq_minLower end of the sampled v_par^2, >= 0.
speed_sq_maxUpper end, >= speed_sq_min. A hard support. If it equals speed_sq_min, every sample has the parallel speed sqrt(speed_sq_min) and speed_sq_pdf is not evaluated.
theta_perpThermal speed of the two perpendicular components, > 0. Each is N(0, theta_perp^2/2), the same convention as bi_maxwellian_distribution.
ubMagnetic-field direction {bx,by,bz}. 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.
parallel_signExactly +1 (parallel component along ub) or -1 (opposite). This makes the result one-sided: every sample streams the same way along the field, which is what you want for a beam and not what you want for a symmetric population. For the symmetric case draw the sign yourself, or use bi_maxwellian_distribution / bi_kappa_distribution.
probe_pointsNumber of evenly spaced points, from speed_sq_min to speed_sq_max inclusive, at which g is evaluated once, here, to estimate its maximum; the largest value plus a 5% margin is the rejection envelope (pdfUpperBound()). See Rejection-sampler settings: probe_points, max_reject_tries for the silent-bias failure mode. Must be >= 64.
max_reject_triesCandidate draws per sample before operator() throws. Must be > 0.
max_normalized_velocityCap on each perpendicular component in units of theta_perp: a pair with |v_perp_i| > cap*theta_perp is redrawn. Default 20, as in bi_maxwellian_distribution; no_cap() removes it. Must be > 0.
Exceptions
std::invalid_argumenton a null callable, a bad range, a zero or negative theta_perp, a ub shorter than the limit above, a parallel_sign other than +1 or -1, probe_points < 64, a non-positive max_reject_tries or cap, or a g that is nowhere positive in range. A NaN or infinite theta_perp and a ub with a NaN component are not rejected.
See also
Parameter reference for the full table, Field-aligned velocities for a recipe.

Definition at line 118 of file field_aligned_velocity_generator.H.

References max_normalized_velocity(), reset(), and ub().

Referenced by field_aligned_velocity_generator(), and main().

◆ max_normalized_velocity()

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

Cap on each perpendicular component, in units of theta_perp; +infinity for no_cap().

Definition at line 272 of file field_aligned_velocity_generator.H.

Referenced by field_aligned_velocity_generator(), and define().

◆ no_cap()

template<typename RealType = double>
constexpr RealType field_aligned_velocity_generator< RealType >::no_cap ( )
inlinestaticconstexpr

Value of max_normalized_velocity that removes the perpendicular cap.

Definition at line 73 of file field_aligned_velocity_generator.H.

◆ operator()() [1/2]

template<typename RealType = double>
point_type field_aligned_velocity_generator< RealType >::operator() ( )
inline

Draw one velocity from the internal engine; throws as operator()(gen) does.

Definition at line 261 of file field_aligned_velocity_generator.H.

◆ operator()() [2/2]

template<typename RealType = double>
template<typename GeneratorType>
point_type field_aligned_velocity_generator< RealType >::operator() ( GeneratorType & gen)
inline

Draw one velocity from the engine gen instead of the internal one.

Returns
{vx, vy, vz} in the global frame.
Exceptions
std::runtime_errorif called before define(), if max_reject_tries is exhausted, or if g returns a negative or non-finite value.

Definition at line 221 of file field_aligned_velocity_generator.H.

References rotate_from_fieldAligned_frame().

◆ parallelSign()

template<typename RealType = double>
RealType field_aligned_velocity_generator< RealType >::parallelSign ( ) const
inline

+1 or -1, as a RealType.

Definition at line 270 of file field_aligned_velocity_generator.H.

◆ parallelSpeedSqMax()

template<typename RealType = double>
RealType field_aligned_velocity_generator< RealType >::parallelSpeedSqMax ( ) const
inline

Upper end of the sampled v_par^2.

Definition at line 267 of file field_aligned_velocity_generator.H.

◆ parallelSpeedSqMin()

template<typename RealType = double>
RealType field_aligned_velocity_generator< RealType >::parallelSpeedSqMin ( ) const
inline

Lower end of the sampled v_par^2.

Definition at line 266 of file field_aligned_velocity_generator.H.

◆ pdfUpperBound()

template<typename RealType = double>
RealType field_aligned_velocity_generator< RealType >::pdfUpperBound ( ) const
inline

The rejection envelope: the largest probed value of g times 1.05, or 0 when speed_sq_min == speed_sq_max.

Raise probe_points until this stops increasing.

Definition at line 275 of file field_aligned_velocity_generator.H.

◆ reset()

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

Discard any state cached by the internal distributions, including the second normal variate of std::normal_distribution.

Called by seed() and define().

Definition at line 210 of file field_aligned_velocity_generator.H.

Referenced by define(), and seed().

◆ rotate_from_fieldAligned_frame()

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

Throws std::invalid_argument if ub is numerically zero.

Definition at line 283 of file field_aligned_velocity_generator.H.

References ub().

Referenced by operator()().

◆ seed()

template<typename RealType = double>
void field_aligned_velocity_generator< RealType >::seed ( int s)
inline

Reseed the internal engine and discard any variate cached from before, so that seed(s) followed by n draws gives the same values however many draws came first.

Definition at line 202 of file field_aligned_velocity_generator.H.

References reset().

Referenced by main().

◆ thetaPerp()

template<typename RealType = double>
RealType field_aligned_velocity_generator< RealType >::thetaPerp ( ) const
inline

Perpendicular thermal speed.

Definition at line 268 of file field_aligned_velocity_generator.H.

◆ ub()

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

Field direction, as given.

Definition at line 269 of file field_aligned_velocity_generator.H.

Referenced by field_aligned_velocity_generator(), define(), and rotate_from_fieldAligned_frame().


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