BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
RandomPositionBuilder Class Reference

Description

Definition at line 55 of file IPositionBuilder.h.

Inheritance diagram for RandomPositionBuilder:
[legend]
Collaboration diagram for RandomPositionBuilder:
[legend]

Public Member Functions

 RandomPositionBuilder ()
 
 ~RandomPositionBuilder () override
 
std::vector< std::vector< double > > generatePositions (double layer_size, double density=0.0) const
 

Private Member Functions

std::vector< std::vector< double > > generatePositionsImpl (double layer_size, double density=0.0) const override
 
double positionVariance () const override
 

Constructor & Destructor Documentation

◆ RandomPositionBuilder()

RandomPositionBuilder::RandomPositionBuilder ( )
default

◆ ~RandomPositionBuilder()

RandomPositionBuilder::~RandomPositionBuilder ( )
overridedefault

Member Function Documentation

◆ generatePositions()

std::vector< std::vector< double > > IPositionBuilder::generatePositions ( double  layer_size,
double  density = 0.0 
) const
inherited

Definition at line 29 of file IPositionBuilder.cpp.

31 {
32  std::vector<std::vector<double>> positions = generatePositionsImpl(layer_size, density);
33  double pos_var = positionVariance();
34  if (pos_var > 0.0) {
35  // random generator and distribution
36  std::random_device rd; // Will be used to obtain a seed for the random number engine
37  std::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd()
38  std::normal_distribution<double> dis(0.0, std::sqrt(pos_var));
39  for (auto& position : positions) {
40  for (auto& coordinate : position)
41  coordinate += dis(gen);
42  }
43  }
44  return positions;
45 }
virtual std::vector< std::vector< double > > generatePositionsImpl(double layer_size, double density=0.0) const =0
virtual double positionVariance() const =0

References IPositionBuilder::generatePositionsImpl(), and IPositionBuilder::positionVariance().

Here is the call graph for this function:

◆ generatePositionsImpl()

std::vector< std::vector< double > > RandomPositionBuilder::generatePositionsImpl ( double  layer_size,
double  density = 0.0 
) const
overrideprivatevirtual

Implements IPositionBuilder.

Definition at line 66 of file IPositionBuilder.cpp.

68 {
69  std::vector<std::vector<double>> lattice_positions;
70  std::vector<double> position;
71 
72  // to compute total number of particles we use the total particle density
73  // and multiply by the area of the layer
74  int num_particles = static_cast<int>(density * (2 * layer_size) * (2 * layer_size));
75 
76  // random generator and distribution
77  std::random_device rd; // Will be used to obtain a seed for the random number engine
78  std::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd()
79  std::uniform_real_distribution<double> dis(0.0, 1.0);
80 
81  for (int i = 1; i <= num_particles; ++i) {
82  // generate random x and y coordinates
83  position.push_back(dis(gen) * 2 * layer_size - layer_size); // x
84  position.push_back(dis(gen) * 2 * layer_size - layer_size); // y
85 
86  lattice_positions.push_back(position);
87  position.clear();
88  }
89  return lattice_positions;
90 }

◆ positionVariance()

double RandomPositionBuilder::positionVariance ( ) const
overrideprivatevirtual

Implements IPositionBuilder.

Definition at line 92 of file IPositionBuilder.cpp.

93 {
94  return 0.0; // no need for extra randomness here
95 }

The documentation for this class was generated from the following files: