BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
IDistribution1DSampler.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Sample/Correlations/IDistribution1DSampler.cpp
6 //! @brief Defines interface class IFTDistribution1D, and children thereof
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
16 #include <random>
17 
19 
21 {
22  // BornAgain Cauchy Distribution = std library Exponential distribution
23  std::random_device rd; // random device class instance
24  std::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd()
25  std::exponential_distribution<double> expDist(m_lambda);
26  double value = expDist(gen);
27 
28  std::bernoulli_distribution bernoulliDist(0.5);
29  bool sign = bernoulliDist(gen);
30 
31  if (sign == true)
32  return value;
33  else
34  return -value;
35 }
36 
38 {
39  // BornAgain Gauss Distribution = std library Normal distribution
40  std::random_device rd;
41  std::mt19937 gen(rd());
42  std::normal_distribution<double> normalDist(m_mean, m_stddev);
43 
44  return normalDist(gen);
45 }
46 
48 {
49  // BornAgain Gate Distribution = std library Uniform distribution
50  std::random_device rd;
51  std::mt19937 gen(rd());
52  std::uniform_real_distribution<double> uniformDist(m_a, m_b);
53 
54  return uniformDist(gen);
55 }
56 
58 {
59  std::random_device rd;
60  std::mt19937 gen(rd());
61 
62  // generate a cdf value between 0 and 1
63  std::uniform_real_distribution<> uniformDist(0.0, 1.0);
64  double cdf_value = uniformDist(gen);
65 
66  // solve for x by inverting the cdf of Triangle Distribution
67  if (cdf_value <= 0.5)
68  return (-m_omega + m_omega * std::sqrt(2 * cdf_value));
69  else
70  return (m_omega - m_omega * std::sqrt(2 * (1 - cdf_value)));
71 }
72 
74 {
75  std::random_device rd;
76  std::mt19937 gen(rd());
77 
78  // generate a cdf value between 0 and 1
79  std::uniform_real_distribution<> uniformDist(0.0, 1.0);
80  double cdf_value = uniformDist(gen);
81 
82  // solve for x from the cdf of Cosine Distribution using Newton-Raphson method
83  double func = 0.0, funcDeriv = 0.0, x = 0.0;
84 
85  // initial guess for x
86  if (cdf_value <= 0.5)
87  x = -m_omega / 2;
88  else
89  x = m_omega / 2;
90 
91  bool convergedSoln = false;
92  while (!convergedSoln) {
93  func = x + m_omega / M_PI * std::sin(M_PI * x / m_omega) + m_omega * (1 - 2 * cdf_value);
94  funcDeriv = 1 + std::cos(M_PI * x / m_omega);
95 
96  x = x - func / funcDeriv;
97 
98  if (std::abs(func / funcDeriv) < 0.001)
99  convergedSoln = true;
100  }
101 
102  return x;
103 }
Defines interface class IFTDistribution1D, and children thereof.
#define M_PI
Definition: MathConstants.h:39
virtual ~IDistribution1DSampler()