OpenMPCD
MaxwellBoltzmannDistribution.cpp
2 
3 #include <boost/random/uniform_01.hpp>
4 #include <cmath>
5 
6 template<typename RNG>
8 {
9  /*
10  * Algorithm according to the "Hand-book on Statistical Distributions for experimentalists" by Christian Walck,
11  * SUF–PFY/96–01
12  */
13 
14  static boost::random::uniform_01<FP> dist;
15 
16  const FP xi_1=dist(rng);
17  FP r=-log(xi_1);
18 
19  FP w_1;
20  FP w;
21 
22  for(;;)
23  {
24  const FP xi_2=dist(rng);
25  const FP xi_3=dist(rng);
26 
27  w_1=xi_2*xi_2;
28  const FP w_2=xi_3*xi_3;
29  w=w_1+w_2;
30 
31  if(w<1 && w!=0)
32  break;
33  }
34 
35  r=r-w_1/w*log(dist(rng));
36 
37  return sqrt(2*r*kT/m);
38 }
MaxwellBoltzmannDistribution.hpp
OpenMPCD::FP
double FP
Default floating point type.
Definition: Types.hpp:13
OpenMPCD::Utility::MathematicalFunctions::sqrt
OPENMPCD_CUDA_HOST_AND_DEVICE T sqrt(const T x)
Returns the sqaure root of the argument.
OpenMPCD::MaxwellBoltzmannDistribution::getRandomMaxwell
FP getRandomMaxwell(const FP m, const FP kT, RNG &rng)
Generates a random number drawn from the Maxwell-Boltzmann distribution.