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

Samples positions in 1D/2D/3D from a user density rho(x), rho(x,y), or rho(x,y,z). More...

#include <general_position_generator.H>

Public Types

using result_type = std::vector<RealType>
 A position, of length dimension().
using point_type = result_type
 Same as result_type.
using density_function = std::function<RealType(const point_type &)>
 The type of the density rho(x) passed to define().

Public Member Functions

 general_position_generator ()
 Construct an undefined generator; call define() before drawing.
 general_position_generator (int dimension, const point_type &lower_bounds, const point_type &upper_bounds, density_functiondensity_function density_functiondensity_function, int probe_points=4096, int max_reject_tries=200000)
 Construct and call define() with these arguments.
void define (int dimension, const point_type &lower_bounds, const point_type &upper_bounds, density_functiondensity_function density_functiondensity_function, int probe_points=4096, int max_reject_tries=200000)
 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 uniform distributions.
int dimension () const
 1, 2 or 3; 0 before define().
const point_type & lowerBounds () const
 Lower corner of the box.
const point_type & upperBounds () const
 Upper corner of the box.
RealType densityUpperBound () const
 The rejection envelope: the largest probed value of rho times 1.05.
template<typename GeneratorType>
point_type operator() (GeneratorType &gen) const
 Draw one position from the engine gen instead of the internal one.
point_type operator() () const
 Draw one position from the internal engine; throws as operator()(gen) does.

Detailed Description

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

Samples positions in 1D/2D/3D from a user density rho(x), rho(x,y), or rho(x,y,z).

The generator uses rejection sampling in an axis-aligned box domain.

auto rho = [](const std::vector<double>& x){ return 1.0 + std::sin(x[0]) * std::sin(x[1]); };
gen.define(2, {-3.14159, -3.14159}, {3.14159, 3.14159}, rho); // dim, lower, upper, density
gen.seed(12345);
auto x = gen(); // std::vector<double> of length 2
Samples positions in 1D/2D/3D from a user density rho(x), rho(x,y), or rho(x,y,z).
void define(int dimension, const point_type &lower_bounds, const point_type &upper_bounds, density_function density_function, int probe_points=4096, int max_reject_tries=200000)
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 ...
See also
Usage examples for more recipes.

Definition at line 26 of file general_position_generator.H.

Member Typedef Documentation

◆ density_function

template<typename RealType = double>
using general_position_generator< RealType >::density_function = std::function<RealType(const point_type &)>

The type of the density rho(x) passed to define().

Definition at line 32 of file general_position_generator.H.

◆ point_type

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

Same as result_type.

Definition at line 30 of file general_position_generator.H.

◆ result_type

template<typename RealType = double>
using general_position_generator< RealType >::result_type = std::vector<RealType>

A position, of length dimension().

Definition at line 29 of file general_position_generator.H.

Constructor & Destructor Documentation

◆ general_position_generator() [1/2]

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

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

The internal engine is seeded from std::random_device.

Definition at line 36 of file general_position_generator.H.

Referenced by general_position_generator().

◆ general_position_generator() [2/2]

template<typename RealType = double>
general_position_generator< RealType >::general_position_generator ( int dimension,
const point_type & lower_bounds,
const point_type & upper_bounds,
density_function density_function,
int probe_points = 4096,
int max_reject_tries = 200000 )
inline

Construct and call define() with these arguments.

Definition at line 45 of file general_position_generator.H.

References general_position_generator(), define(), and dimension().

Member Function Documentation

◆ define()

template<typename RealType = double>
void general_position_generator< RealType >::define ( int dimension,
const point_type & lower_bounds,
const point_type & upper_bounds,
density_function density_function,
int probe_points = 4096,
int max_reject_tries = 200000 )
inline

Configure the sampler.

Parameters
dimension1, 2, or 3. Fixes the length of the bounds vectors and of every returned point.
lower_boundsLower corner of the sampling box, size dimension.
upper_boundsUpper corner, size dimension, each entry above its counterpart in lower_bounds. The box is a hard support: nothing outside it is ever produced.
density_functionYour rho(x), taking a std::vector<RealType> of length dimension. Need not be normalized; must be finite, non-negative, and positive somewhere in the box.
probe_pointsNumber of points at which rho is evaluated once, here, to estimate its maximum. The points are uniformly random in the box, drawn from a separate engine with a fixed seed, so they do not depend on seed() and are the same for every call with the same box. The largest value found, plus a 5% margin, is the rejection envelope (densityUpperBound()). A density with a spike narrow enough to fall between probes yields a silently wrong sample – see Rejection-sampler settings: probe_points, max_reject_tries. Must be > 0.
max_reject_triesCandidate draws per sample before operator() throws. Must be > 0. A density concentrated in a small corner of a large box exhausts this; shrink the box instead.
Exceptions
std::invalid_argumentif dimension is outside 1-3, the bounds sizes do not match it, a bound is inverted, the callable is null, a knob is non-positive, or rho is nowhere positive in the box.
See also
Parameter reference for the full table, Positions from your own density for a recipe.

Definition at line 84 of file general_position_generator.H.

References dimension(), and reset().

Referenced by general_position_generator(), and main().

◆ densityUpperBound()

template<typename RealType = double>
RealType general_position_generator< RealType >::densityUpperBound ( ) const
inline

The rejection envelope: the largest probed value of rho times 1.05.

Raise probe_points until this stops increasing; see Rejection-sampler settings: probe_points, max_reject_tries.

Definition at line 176 of file general_position_generator.H.

◆ dimension()

template<typename RealType = double>
int general_position_generator< RealType >::dimension ( ) const
inline

1, 2 or 3; 0 before define().

Definition at line 171 of file general_position_generator.H.

Referenced by general_position_generator(), and define().

◆ lowerBounds()

template<typename RealType = double>
const point_type & general_position_generator< RealType >::lowerBounds ( ) const
inline

Lower corner of the box.

Definition at line 172 of file general_position_generator.H.

◆ operator()() [1/2]

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

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

Definition at line 206 of file general_position_generator.H.

◆ operator()() [2/2]

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

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

Exceptions
std::runtime_errorif called before define(), if max_reject_tries is exhausted, or if rho returns a negative or non-finite value.

Definition at line 181 of file general_position_generator.H.

◆ reset()

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

Discard any state cached by the internal uniform distributions.

Called by seed() and define().

Definition at line 165 of file general_position_generator.H.

Referenced by define(), and seed().

◆ seed()

template<typename RealType = double>
void general_position_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 157 of file general_position_generator.H.

References reset().

Referenced by main().

◆ upperBounds()

template<typename RealType = double>
const point_type & general_position_generator< RealType >::upperBounds ( ) const
inline

Upper corner of the box.

Definition at line 173 of file general_position_generator.H.


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