BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
IDistribution2DSampler.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Sample/Correlations/IDistribution2DSampler.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 
18 namespace
19 {
20 double sigma_scale = 3.0;
21 size_t n_boxes = 256; // number of boxes for Ziggurat sampling
22 
23 struct ZigguratBox {
24  ZigguratBox(double x_min, double x_max, double y_max, double y_lower)
25  : m_x_min(x_min), m_x_max(x_max), m_y_max(y_max), m_y_lower(y_lower)
26  {
27  }
28 
29  double m_x_min; // left edge of the box
30  double m_x_max; // right edge of the box
31  // m_y_min is inherently 0 for every box and hence has not been defined
32  double m_y_max; // height of box
33  double m_y_lower; // minimum height of the box for which points below that height
34  // are located below the density function curve in the box
35 };
36 
37 std::pair<double, double> samplingZiggurat(double r, double x_func_max, double (*func_phi)(double))
38 {
39  // This sampling is based on vertical boxes instead of the conventional
40  // Ziggurat sampling that is done with horizontal boxes
41 
42  std::random_device rd; // random device class instance
43  std::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd()
44  std::uniform_real_distribution<double> uniformDist(0.0, 1.0);
45 
46  double box_width = (x_func_max + r) / n_boxes; // r = rightmost box's right-edge from x_func_max
47  std::vector<ZigguratBox> boxes;
48  std::vector<double> cum_area_vector;
49 
50  double x_min = 0, x_max = 0, y_max = 0, y_lower = 0, cum_area_box = 0;
51 
52  // Establising vectors of boxes and cumulative area (probability of each box) for Ziggurat
53  // sampling
54  for (size_t i = 0; i < n_boxes; ++i) {
55  if (i != 0)
56  x_min = x_max;
57 
58  x_max += box_width;
59 
60  if (x_func_max >= x_max) {
61  y_max = func_phi(x_max);
62  y_lower = func_phi(x_min);
63  } else if (x_func_max > x_min && x_func_max <= x_max) {
64  y_max = func_phi(x_func_max);
65  y_lower = std::min(func_phi(x_min), func_phi(x_max));
66  } else {
67  y_max = func_phi(x_min);
68  y_lower = func_phi(x_max);
69  }
70 
71  boxes.emplace_back(ZigguratBox(x_min, x_max, y_max, y_lower));
72 
73  cum_area_box += box_width * y_max;
74  cum_area_vector.emplace_back(cum_area_box);
75  }
76 
77  // Normalizing the cumulative area to 1
78  for (size_t i = 0; i < n_boxes; ++i)
79  cum_area_vector[i] = cum_area_vector[i] / cum_area_vector.back();
80 
81  // Sampling a phi value
82  double phi = 0;
83  bool solnFound(false);
84 
85  while (!solnFound) {
86  double random_cum_area = uniformDist(gen);
87  for (size_t i = 0; i < n_boxes; ++i) {
88  if (random_cum_area <= cum_area_vector[i]) {
89  double random_y = uniformDist(gen) * boxes[i].m_y_max;
90 
91  std::uniform_real_distribution<double> uniformDistAB(boxes[i].m_x_min,
92  boxes[i].m_x_max);
93  double phi_attempt = uniformDistAB(gen);
94 
95  if (random_y <= boxes[i].m_y_lower) {
96  phi = phi_attempt;
97  solnFound = true;
98  } else {
99  if (random_y <= func_phi(phi_attempt)) {
100  phi = phi_attempt;
101  solnFound = true;
102  }
103  }
104  break;
105  }
106  }
107  }
108 
109  // Sampling an alpha value
110  double alpha = 2 * M_PI * uniformDist(gen);
111  return std::make_pair(phi, alpha);
112 }
113 
114 double func_phi_Cauchy(double phi)
115 {
116  // The independent "phi" density function of the 2D Cauchy distribution
117  return phi * std::exp(-phi);
118 }
119 
120 double func_phi_Cone(double phi)
121 {
122  // The independent "phi" density function of the 2D Cone distribution
123  return 6 * (1 - phi) * phi;
124 }
125 } // namespace
126 
128 
129 std::pair<double, double> Distribution2DCauchySampler::randomSample() const
130 {
131  // Use Ziggurat sampling instead of Inverse Transform Sampling (ITS requires numerical solver)
132 
133  double phi_max_Cauchy = 1.0;
134  // rightmost box's right-edge from phi_max_Cauchy for Ziggurat Sampling
135  double r = sigma_scale * std::sqrt(2); // standard dev of func_phi_Cauchy is sqrt(2)
136  std::pair<double, double> samples = samplingZiggurat(r, phi_max_Cauchy, func_phi_Cauchy);
137  return std::make_pair(m_omega_x * samples.first * std::cos(samples.second),
138  m_omega_y * samples.first * std::sin(samples.second));
139 }
140 
141 std::pair<double, double> Distribution2DGaussSampler::randomSample() const
142 {
143  std::random_device rd; // random device class instance
144  std::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd()
145  std::uniform_real_distribution<double> uniformDist(0.0, 1.0);
146 
147  double cdf_value_phi = uniformDist(gen);
148 
149  // Use ITS and solve for phi from the cdf of radial (phi) distribution
150  double phi = std::sqrt(-2 * std::log(1 - cdf_value_phi));
151  double alpha = 2 * M_PI * uniformDist(gen);
152  return std::make_pair(m_omega_x * phi * std::cos(alpha), m_omega_y * phi * std::sin(alpha));
153 }
154 
155 std::pair<double, double> Distribution2DGateSampler::randomSample() const
156 {
157  std::random_device rd; // random device class instance
158  std::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd()
159  std::uniform_real_distribution<double> uniformDist(0.0, 1.0);
160 
161  double cdf_value_phi = uniformDist(gen);
162 
163  // Use ITS and solve for phi from the cdf of radial (phi) distribution
164  double phi = std::sqrt(cdf_value_phi);
165  double alpha = 2 * M_PI * uniformDist(gen);
166  return std::make_pair(m_omega_x * phi * std::cos(alpha), m_omega_y * phi * std::sin(alpha));
167 }
168 
169 std::pair<double, double> Distribution2DConeSampler::randomSample() const
170 {
171  // Use Ziggurat sampling instead of Inverse Transform Sampling (ITS requires numerical solver)
172 
173  double phi_max_Cone = 0.5;
174  // rightmost box's right-edge from phi_max_Cone for Ziggurat Sampling
175  double r = 0.5;
176  std::pair<double, double> samples = samplingZiggurat(r, phi_max_Cone, func_phi_Cone);
177  return std::make_pair(m_omega_x * samples.first * std::cos(samples.second),
178  m_omega_y * samples.first * std::sin(samples.second));
179 }
Defines interface class IFTDistribution1D, and children thereof.
#define M_PI
Definition: MathConstants.h:39
std::pair< double, double > randomSample() const final
std::pair< double, double > randomSample() const final
std::pair< double, double > randomSample() const final
std::pair< double, double > randomSample() const final
virtual ~IDistribution2DSampler()
std::pair< double, double > samplingZiggurat(double r, double x_func_max, double(*func_phi)(double))
ZigguratBox(double x_min, double x_max, double y_max, double y_lower)