41 : m_speedSqPdf(), m_speedSqMin(RealType(0.0)), m_speedSqMax(RealType(1.0)),
42 m_maxRejectTries(100000),
43 m_pdfUpperBound(RealType(0.0)), m_isDefined(false), m_gen(std::random_device{}()),
44 m_distSpeedSq(RealType(0.0), RealType(1.0)), m_distAccept(RealType(0.0), RealType(1.0)),
45 m_distCosTheta(RealType(-1.0), RealType(1.0)),
46 m_distPhi(RealType(0.0), RealType(2.0) * piValue())
94 int probe_points= 1024,
int max_reject_tries= 100000)
100 throw std::invalid_argument(
"speed_sq_pdf callable must be provided");
102 if (speed_sq_min < RealType(0.0))
104 throw std::invalid_argument(
"speed_sq_min must be non-negative (w = |v|^2 >= 0)");
106 if (!(speed_sq_min <= speed_sq_max))
108 throw std::invalid_argument(
"speed_sq_max must not be less than speed_sq_min");
110 if (probe_points < kMinProbePoints)
116 throw std::invalid_argument(
"probe_points must be at least 64");
118 if (max_reject_tries <= 0)
120 throw std::invalid_argument(
"max_reject_tries must be positive");
123 m_speedSqPdf= speed_sq_pdf;
124 m_speedSqMin= speed_sq_min;
125 m_speedSqMax= speed_sq_max;
126 m_maxRejectTries= max_reject_tries;
127 m_pdfUpperBound= RealType(0.0);
129 m_distSpeedSq.param(
typename std::uniform_real_distribution<RealType>::param_type(
130 m_speedSqMin, m_speedSqMax));
132 typename std::uniform_real_distribution<RealType>::param_type(RealType(0.0),
134 m_distCosTheta.param(
typename std::uniform_real_distribution<RealType>::param_type(
135 RealType(-1.0), RealType(1.0)));
136 m_distPhi.param(
typename std::uniform_real_distribution<RealType>::param_type(
137 RealType(0.0), RealType(2.0) * piValue()));
140 if (speed_sq_min < speed_sq_max)
142 m_pdfUpperBound= estimatePdfUpperBound(probe_points);
143 if (m_pdfUpperBound <= RealType(0.0))
145 throw std::invalid_argument(
146 "speed_sq_pdf must be positive somewhere within [speed_sq_min, speed_sq_max]");
149 m_distAccept.param(
typename std::uniform_real_distribution<RealType>::param_type(
150 RealType(0.0), m_pdfUpperBound));
183 throw std::runtime_error(
184 "general_velocity_generator must be define(...)d before sampling");
187 const RealType kOne= RealType(1.0);
189 RealType speed= std::sqrt(sampleSpeedSq(gen));
191 RealType cosTheta= m_distCosTheta(gen);
192 RealType sinTheta= std::sqrt(std::max(RealType(0.0), kOne - cosTheta * cosTheta));
193 RealType phi= m_distPhi(gen);
195 RealType vx= speed * sinTheta * std::cos(phi);
196 RealType vy= speed * sinTheta * std::sin(phi);
197 RealType vz= speed * cosTheta;
219 RealType evaluateSpeedSqPdf(RealType speed_sq)
const
221 RealType value= m_speedSqPdf(speed_sq);
222 if (!std::isfinite(value))
224 throw std::runtime_error(
"speed_sq_pdf returned a non-finite value");
226 if (value < RealType(0.0))
228 throw std::runtime_error(
229 "speed_sq_pdf returned a negative value; expected a non-negative PDF");
234 RealType estimatePdfUpperBound(
int probe_points)
const
236 RealType max_value= RealType(0.0);
238 for (i= 0; i < probe_points; ++i)
240 RealType ratio= RealType(i) / RealType(probe_points - 1);
241 RealType speed_sq= m_speedSqMin + ratio * (m_speedSqMax - m_speedSqMin);
242 RealType value= evaluateSpeedSqPdf(speed_sq);
243 if (value > max_value)
250 return max_value * RealType(1.05);
253 template <
typename GeneratorType> RealType sampleSpeedSq(GeneratorType &gen)
255 if (!(m_speedSqMin < m_speedSqMax))
260 for (tries= 0; tries < m_maxRejectTries; ++tries)
262 RealType speed_sq= m_distSpeedSq(gen);
263 RealType y= m_distAccept(gen);
264 RealType pdf_value= evaluateSpeedSqPdf(speed_sq);
271 throw std::runtime_error(
"failed to sample speed_sq from speed_sq_pdf: rejection sampling "
272 "exceeded max_reject_tries");
275 static void applySeed(std::mt19937 &gen,
int s)
278 gen.seed(std::random_device{}());
280 gen.seed(
static_cast<unsigned int>(s));
283 static RealType piValue() {
return RealType(3.14159265358979323846264338327950288L); }
285 static const int kMinProbePoints= 64;
288 RealType m_speedSqMin;
289 RealType m_speedSqMax;
290 int m_maxRejectTries;
291 RealType m_pdfUpperBound;
294 std::uniform_real_distribution<RealType> m_distSpeedSq;
295 std::uniform_real_distribution<RealType> m_distAccept;
296 std::uniform_real_distribution<RealType> m_distCosTheta;
297 std::uniform_real_distribution<RealType> m_distPhi;