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

Samples velocities from a user-defined speed-squared distribution g(w). More...

#include <general_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

 general_velocity_generator ()
 Construct an undefined generator; call define() before drawing.
 general_velocity_generator (speed_sq_pdf_function speed_sq_pdf, RealType speed_sq_min, RealType speed_sq_max, int probe_points=1024, int max_reject_tries=100000)
 Construct and call define() with these arguments.
void define (speed_sq_pdf_function speed_sq_pdf, RealType speed_sq_min, RealType speed_sq_max, int probe_points=1024, int max_reject_tries=100000)
 Configure the sampler.
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.
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 speedSqMin () const
 Lower end of the sampled w.
RealType speedSqMax () const
 Upper end of the sampled w.
RealType pdfUpperBound () const
 The rejection envelope: the largest probed value of g times 1.05, or 0 when speed_sq_min == speed_sq_max.

Detailed Description

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

Samples velocities from a user-defined speed-squared distribution g(w).

The sampled value is w = |V|^2, so the speed is |V| = sqrt(w) and the direction is isotropic in 3D. (For a distribution originally posed in energy, w = |V|^2 = 2E/m, so the mass drops out of this generator entirely.) If speed_sq_min == speed_sq_max, every sample has the single speed sqrt(speed_sq_min).

auto gW = [](double w){ return std::exp(-0.5 * w); }; // any positive g(w), w = |v|^2
gen.define(gW, 0.0, 40.0); // g, v2_min, v2_max
gen.seed(12345);
auto v = gen();
Samples velocities from a user-defined speed-squared distribution g(w).
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, int probe_points=1024, int max_reject_tries=100000)
Configure the sampler.
See also
Usage examples for more recipes.

Definition at line 30 of file general_velocity_generator.H.

Member Typedef Documentation

◆ point_type

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

Same as result_type.

Definition at line 34 of file general_velocity_generator.H.

◆ result_type

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

A velocity {vx, vy, vz}.

Definition at line 33 of file general_velocity_generator.H.

◆ speed_sq_pdf_function

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

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

Definition at line 36 of file general_velocity_generator.H.

Constructor & Destructor Documentation

◆ general_velocity_generator() [1/2]

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

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

The internal engine is seeded from std::random_device.

Definition at line 40 of file general_velocity_generator.H.

Referenced by general_velocity_generator().

◆ general_velocity_generator() [2/2]

template<typename RealType = double>
general_velocity_generator< RealType >::general_velocity_generator ( speed_sq_pdf_function speed_sq_pdf,
RealType speed_sq_min,
RealType speed_sq_max,
int probe_points = 1024,
int max_reject_tries = 100000 )
inline

Construct and call define() with these arguments.

Definition at line 51 of file general_velocity_generator.H.

References general_velocity_generator(), and define().

Member Function Documentation

◆ define()

template<typename RealType = double>
void general_velocity_generator< RealType >::define ( speed_sq_pdf_function speed_sq_pdf,
RealType speed_sq_min,
RealType speed_sq_max,
int probe_points = 1024,
int max_reject_tries = 100000 )
inline

Configure the sampler.

Estimates the rejection envelope here, once, so a badly chosen probe_points is a configuration-time decision.

Parameters
speed_sq_pdfYour g(w), whose argument is the speed squared, not the speed. Need not be normalized – any non-negative function proportional to the density works. Must be finite and non-negative wherever it is evaluated, and positive somewhere in range.
speed_sq_minLower end of the sampled w range, >= 0.
speed_sq_maxUpper end, >= speed_sq_min. This is a hard support: speeds beyond sqrt(speed_sq_max) are never produced, so choose it to cover the tail you care about. If it equals speed_sq_min, every sample has the speed sqrt(speed_sq_min) and speed_sq_pdf is not evaluated.
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()). If g has a peak narrow enough to fall between probes, the envelope is too low and the sampled distribution is silently wrong; raise this until pdfUpperBound() stops increasing. Must be >= 64.
max_reject_triesCandidate draws one sample may consume before operator() throws. Must be > 0. Exhausting it usually means g is tiny over most of [min,max]; narrowing the range fixes it better than raising this.
Exceptions
std::invalid_argumenton a null callable, a negative speed_sq_min, speed_sq_max < speed_sq_min, probe_points < 64, a non-positive max_reject_tries, or a g that is nowhere positive in range.
See also
Rejection-sampler settings: probe_points, max_reject_tries for how the envelope works, Choosing a sampler to compare samplers.

Definition at line 93 of file general_velocity_generator.H.

References reset().

Referenced by general_velocity_generator(), and main().

◆ operator()() [1/2]

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

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

Definition at line 207 of file general_velocity_generator.H.

◆ operator()() [2/2]

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

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

Returns
{vx, vy, vz}, with speed sqrt(w) and an isotropic direction.
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 179 of file general_velocity_generator.H.

◆ pdfUpperBound()

template<typename RealType = double>
RealType general_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 216 of file general_velocity_generator.H.

◆ reset()

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

Discard any state cached by the internal distributions.

Called by seed() and define().

Definition at line 167 of file general_velocity_generator.H.

Referenced by define(), and seed().

◆ seed()

template<typename RealType = double>
void general_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 159 of file general_velocity_generator.H.

References reset().

Referenced by main().

◆ speedSqMax()

template<typename RealType = double>
RealType general_velocity_generator< RealType >::speedSqMax ( ) const
inline

Upper end of the sampled w.

Definition at line 213 of file general_velocity_generator.H.

◆ speedSqMin()

template<typename RealType = double>
RealType general_velocity_generator< RealType >::speedSqMin ( ) const
inline

Lower end of the sampled w.

Definition at line 212 of file general_velocity_generator.H.


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