1#ifndef _BI_MAXWELLIAN_DISTRIBUTION_H_
2#define _BI_MAXWELLIAN_DISTRIBUTION_H_
50 if (m_theta_perp <= RealType(0.0) || m_theta_par <= RealType(0.0))
52 throw std::invalid_argument(
"Bi-Maxwellian temperatures must be positive");
57 if (!(m_maxNormalizedVelocity > RealType(0.0)))
59 throw std::invalid_argument(
60 "max_normalized_velocity must be positive, or no_cap() for the "
61 "untruncated distribution");
74 bool capped()
const {
return std::isfinite(m_maxNormalizedVelocity); }
79 return m_theta_perp == other.m_theta_perp && m_theta_par == other.m_theta_par &&
80 m_ub == other.m_ub && m_maxNormalizedVelocity == other.m_maxNormalizedVelocity;
87 RealType m_theta_perp;
90 RealType m_maxNormalizedVelocity;
113 return std::numeric_limits<RealType>::infinity();
130 m_gen(std::random_device{}()), m_standardNormal(RealType(0.0), RealType(1.0))
138 : m_params(params), m_gen(std::random_device{}()), m_standardNormal(RealType(0.0), RealType(1.0))
198 applySeed(m_gen,
seed);
208 define(value, m_params.theta_par(), m_params.ub(), m_params.max_normalized_velocity());
214 define(m_params.theta_perp(), value, m_params.ub(), m_params.max_normalized_velocity());
220 define(m_params.theta_perp(), m_params.theta_par(), value,
221 m_params.max_normalized_velocity());
230 define(m_params.theta_perp(), m_params.theta_par(), m_params.ub(), value);
238 param_type
param()
const {
return m_params; }
240 RealType
theta_perp()
const {
return m_params.theta_perp(); }
241 RealType
theta_par()
const {
return m_params.theta_par(); }
259 void reset() { m_standardNormal.reset(); }
268 return (*
this)(gen, m_params);
275 return (*
this)(m_gen);
282 template <
typename GeneratorType>
285 const RealType sigmaScale= RealType(1.0) / std::sqrt(RealType(2.0));
286 const int kMaxCapRejectTries= 1000000;
287 RealType maxNormalizedVelocity= params.max_normalized_velocity();
291 const bool capped= params.capped();
292 const int maxTries= capped ? kMaxCapRejectTries : 1;
295 for (tries= 0; tries < maxTries; ++tries)
297 RealType vx= params.theta_perp() * sigmaScale * m_standardNormal(gen);
298 RealType vy= params.theta_perp() * sigmaScale * m_standardNormal(gen);
299 RealType vz= params.theta_par() * sigmaScale * m_standardNormal(gen);
306 if (capped && !withinNormalizedVelocityCap(point, params.theta_perp(),
308 maxNormalizedVelocity))
315 if (approximatelyEqual(
ub[0], RealType(0.0)) &&
316 approximatelyEqual(
ub[1], RealType(0.0)) &&
317 approximatelyEqual(
ub[2], RealType(1.0)))
324 throw std::runtime_error(
"failed to sample velocity under max_normalized_velocity cap: "
325 "rejection sampling exceeded internal attempt limit");
336 const RealType kZero= RealType(0.0);
337 const RealType kOne= RealType(1.0);
339 RealType ubNorm2=
ub[0] *
ub[0] +
ub[1] *
ub[1] +
ub[2] *
ub[2];
340 if (approximatelyEqual(ubNorm2, kZero))
342 throw std::invalid_argument(
"ub must be a non-zero vector");
345 RealType ubNorm= std::sqrt(ubNorm2);
348 e3[0]=
ub[0] / ubNorm;
349 e3[1]=
ub[1] / ubNorm;
350 e3[2]=
ub[2] / ubNorm;
355 if (std::abs(e3[1]) > std::abs(e3[maxcomp]))
359 if (std::abs(e3[2]) > std::abs(e3[maxcomp]))
364 RealType sum= e3[0] + e3[1] + e3[2];
365 e2[maxcomp]= kOne - sum / e3[maxcomp];
367 RealType e2Norm2= e2[0] * e2[0] + e2[1] * e2[1] + e2[2] * e2[2];
368 RealType e2Norm= std::sqrt(e2Norm2);
374 e1[0]= e2[1] * e3[2] - e2[2] * e3[1];
375 e1[1]= e2[2] * e3[0] - e2[0] * e3[2];
376 e1[2]= e2[0] * e3[1] - e2[1] * e3[0];
379 point[0]= local[0] * e1[0] + local[1] * e2[0] + local[2] * e3[0];
380 point[1]= local[0] * e1[1] + local[1] * e2[1] + local[2] * e3[1];
381 point[2]= local[0] * e1[2] + local[1] * e2[2] + local[2] * e3[2];
391 return m_params == other.m_params;
401 static void applySeed(std::mt19937 &gen,
int s)
404 gen.seed(std::random_device{}());
406 gen.seed(
static_cast<unsigned int>(s));
409 static bool approximatelyEqual(RealType a, RealType b)
411 RealType scale= std::max({RealType(1.0), std::abs(a), std::abs(b)});
412 return std::abs(a - b) <= std::numeric_limits<RealType>::epsilon() * scale;
418 RealType norm_x= std::abs(point[0]) /
theta_perp;
419 RealType norm_y= std::abs(point[1]) /
theta_perp;
420 RealType norm_z= std::abs(point[2]) /
theta_par;
427 std::normal_distribution<RealType> m_standardNormal;
The parameters of a bi_maxwellian_distribution: theta_perp, theta_par, ub and the velocity cap.
bool operator==(const param_type &other) const
True if every parameter compares equal.
point_type ub() const
Field direction, as given.
RealType max_normalized_velocity() const
Velocity cap in thermal speeds; +infinity for no_cap().
RealType theta_perp() const
Perpendicular thermal speed.
RealType theta_par() const
Parallel thermal speed.
bool operator!=(const param_type &other) const
Negation of operator==.
bi_maxwellian_distribution< RealType > distribution_type
The owning sampler.
param_type(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))
Store the parameters.
bool capped() const
True when a finite component-wise cap is in force, that is, when the sampled distribution is the bi-M...
result_type operator()(GeneratorType &gen)
Draw one velocity from the engine gen instead of the internal one.
bi_maxwellian_distribution(const param_type ¶ms)
Construct from a parameter set.
void max_normalized_velocity(RealType value)
Set the velocity cap, keeping the other parameters.
result_type operator()(GeneratorType &gen, const param_type ¶ms)
Draw one velocity from gen with the parameters params instead of the stored ones.
bool operator!=(const bi_maxwellian_distribution &other) const
Negation of operator==.
RealType theta_perp() const
Perpendicular thermal speed.
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,...
result_type point_type
Same as result_type.
void define(RealType theta_perp, 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.
bool operator==(const bi_maxwellian_distribution &other) const
True if the two samplers have equal parameters. Engine state is not compared.
RealType theta_par() const
Parallel thermal speed.
RealType max_normalized_velocity() const
Velocity cap in thermal speeds; +infinity for no_cap().
bi_maxwellian_distribution(RealType theta_perp, 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.
point_type ub() const
Field direction, as given.
void theta_perp(RealType value)
Set theta_perp, keeping the other parameters; throws as define() does.
static constexpr RealType no_cap()
Value of the cap that selects the uncapped sampler: +infinity.
std::array< RealType, 3 > result_type
A velocity {vx, vy, vz}.
result_type operator()()
Draw one velocity from the internal engine.
void define(RealType theta_perp, RealType theta_par, point_type ub, RealType max_normalized_velocity, int seed)
define() with all four parameters, after seeding the internal engine with seed as seed() does (seed >...
bi_maxwellian_distribution()
Construct with theta_perp = theta_par = 1, ub = {0, 0, 1} and a cap of 20.
void ub(const point_type &value)
Set the field direction, keeping the other parameters.
void define(const param_type ¶ms)
Replace all parameters with params, then call reset().
void define(const param_type ¶ms, int seed)
define(params), after seeding the internal engine with seed as seed() does.
void reset()
Discard the normal variate cached by std::normal_distribution.
void theta_par(RealType value)
Set theta_par, keeping the other parameters; throws as define() does.
param_type param() const
The current parameters, for example to test param().capped().