BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
SpecularSimulation Class Reference

Description

Specular reflectometry simulation.

Holds an instrument and sample model. Computes detected intensity as function of the glancing angles alpha_i=alpha_f.

Definition at line 33 of file SpecularSimulation.h.

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

Public Member Functions

 SpecularSimulation (const ISpecularScan &scan, const MultiLayer &sample)
 
 SpecularSimulation (SpecularSimulation &&)
 
 ~SpecularSimulation () override
 
void addParameterDistribution (const ParameterDistribution &par_distr)
 
void addParameterDistribution (ParameterDistribution::WhichParameter whichParameter, const IDistribution1D &distribution, size_t nbr_samples, double sigma_factor=0.0, const RealLimits &limits=RealLimits())
 
const IBackgroundbackground () const
 
void checkNodeArgs () const
 Raises exception if a parameter value is invalid. More...
 
std::string className () const final
 Returns the class name, to be hard-coded in each leaf class that inherits from INode. More...
 
ICoordSystemcreateCoordSystem () const override
 
bool force_polarized () const override
 Force polarized computation even in absence of sample magnetization or external fields. More...
 
const std::vector< ParameterDistribution > & getDistributions () const
 
size_t intensityMapSize () const override
 Returns the total number of the intensity values in the simulation result. More...
 
std::vector< const INode * > nodeChildren () const override
 Returns all children. More...
 
std::vector< const INode * > nodeOffspring () const
 Returns all descendants. More...
 
SimulationOptionsoptions ()
 
const SimulationOptionsoptions () const
 
virtual std::vector< ParaMetaparDefs () const
 Returns the parameter definitions, to be hard-coded in each leaf class. More...
 
void prepareSimulation () override
 Put into a clean state for running a simulation. More...
 
const MultiLayersample () const
 
const ISpecularScanscan () const
 Returns internal data handler. More...
 
void setBackground (const IBackground &bg)
 
void setTerminalProgressMonitor ()
 Initializes a progress monitor that prints to stdout. More...
 
SimulationResult simulate ()
 Run a simulation, and return the result. More...
 
void subscribe (const std::function< bool(size_t)> &inform)
 
std::string unitOfParameter (ParameterDistribution::WhichParameter which) const
 

Protected Member Functions

virtual void initDistributionHandler ()
 
ProgressHandlerprogress ()
 
virtual void updateIntensityMap ()
 

Protected Attributes

DistributionHandler m_distribution_handler
 
std::vector< double > m_P
 

Private Member Functions

void addBackgroundIntensity (size_t start_ind, size_t n_elements) override
 
void addDataToCache (double weight) override
 
void checkCache () const
 
std::unique_ptr< IComputationcreateComputation (const reSample &re_sample, size_t start, size_t n_elements) override
 Generate a single threaded computation for a given range of simulation elements. More...
 
void initElementVector () override
 Initializes the vector of ISimulation elements. More...
 
void moveDataFromCache () override
 
void normalize (size_t start_ind, size_t n_elements) override
 Normalize the detector counts to beam intensity, to solid angle, and to exposure angle. More...
 
size_t numberOfElements () const override
 Gets the number of elements this simulation needs to calculate. More...
 
SimulationResult pack_result () override
 Sets m_result. More...
 
void runSingleSimulation (const reSample &re_sample, size_t batch_start, size_t batch_size, double weight=1.0)
 Runs a single simulation with fixed parameter values. If desired, the simulation is run in several threads. More...
 
void validateParametrization (const ParameterDistribution &par_distr) const override
 Checks the distribution validity for simulation. More...
 

Private Attributes

std::shared_ptr< IBackgroundm_background
 
std::vector< double > m_cache
 
std::vector< SpecularElementm_eles
 
std::shared_ptr< SimulationOptionsm_options
 
std::shared_ptr< ProgressHandlerm_progress
 
std::shared_ptr< MultiLayerm_sample
 
std::unique_ptr< const ISpecularScanm_scan
 

Constructor & Destructor Documentation

◆ SpecularSimulation() [1/2]

SpecularSimulation::SpecularSimulation ( const ISpecularScan scan,
const MultiLayer sample 
)

Definition at line 33 of file SpecularSimulation.cpp.

35  , m_scan(scan.clone())
36 {
37  // TODO: move inside AlphaScan when pointwise resolution is implemented
38  if (scan.coordinateAxis()->min() < 0.0)
39  throw std::runtime_error("Invalid scan: minimum value on coordinate axis is negative");
40 }
virtual double min() const =0
Returns value of first point of axis.
const MultiLayer * sample() const
ISimulation(const MultiLayer &sample)
virtual const IAxis * coordinateAxis() const =0
Returns coordinate axis assigned to the data holder.
ISpecularScan * clone() const override=0
const ISpecularScan * scan() const
Returns internal data handler.
std::unique_ptr< const ISpecularScan > m_scan

References ISpecularScan::coordinateAxis(), IAxis::min(), and scan().

Here is the call graph for this function:

◆ ~SpecularSimulation()

SpecularSimulation::~SpecularSimulation ( )
overridedefault

◆ SpecularSimulation() [2/2]

SpecularSimulation::SpecularSimulation ( SpecularSimulation &&  )
default

Member Function Documentation

◆ addBackgroundIntensity()

void SpecularSimulation::addBackgroundIntensity ( size_t  start_ind,
size_t  n_elements 
)
overrideprivatevirtual

Implements ISimulation.

Definition at line 115 of file SpecularSimulation.cpp.

116 {
117  if (!background())
118  return;
119  for (size_t i = start_ind, stop_point = start_ind + n_elements; i < stop_point; ++i) {
120  auto& element = m_eles[i];
121  element.setIntensity(background()->addBackground(element.intensity()));
122  }
123 }
const IBackground * background() const
Definition: ISimulation.h:76
std::vector< SpecularElement > m_eles

References ISimulation::background(), and m_eles.

Here is the call graph for this function:

◆ addDataToCache()

void SpecularSimulation::addDataToCache ( double  weight)
overrideprivatevirtual

Implements ISimulation.

Definition at line 125 of file SpecularSimulation.cpp.

126 {
127  checkCache();
128  for (size_t i = 0, size = m_eles.size(); i < size; ++i)
129  m_cache[i] += m_eles[i].intensity() * weight;
130 }
std::vector< double > m_cache

References checkCache(), m_cache, and m_eles.

Here is the call graph for this function:

◆ addParameterDistribution() [1/2]

void ISimulation::addParameterDistribution ( const ParameterDistribution par_distr)
inherited

Definition at line 231 of file ISimulation.cpp.

232 {
233  validateParametrization(par_distr);
235 }
void addParameterDistribution(const std::string &param_name, const IDistribution1D &distribution, size_t nbr_samples, double sigma_factor=0.0, const RealLimits &limits=RealLimits())
add a sampled parameter distribution
virtual void validateParametrization(const ParameterDistribution &) const
Checks the distribution validity for simulation.
Definition: ISimulation.h:120
DistributionHandler m_distribution_handler
Definition: ISimulation.h:104

References DistributionHandler::addParameterDistribution(), ISimulation::m_distribution_handler, and ISimulation::validateParametrization().

Here is the call graph for this function:

◆ addParameterDistribution() [2/2]

void ISimulation::addParameterDistribution ( ParameterDistribution::WhichParameter  whichParameter,
const IDistribution1D distribution,
size_t  nbr_samples,
double  sigma_factor = 0.0,
const RealLimits limits = RealLimits() 
)
inherited

Definition at line 222 of file ISimulation.cpp.

225 {
226  ParameterDistribution par_distr(whichParameter, distribution, nbr_samples, sigma_factor,
227  limits);
228  addParameterDistribution(par_distr);
229 }
void addParameterDistribution(ParameterDistribution::WhichParameter whichParameter, const IDistribution1D &distribution, size_t nbr_samples, double sigma_factor=0.0, const RealLimits &limits=RealLimits())
A parametric distribution function, for use with any model parameter.

◆ background()

const IBackground* ISimulation::background ( ) const
inlineinherited

Definition at line 76 of file ISimulation.h.

76 { return m_background.get(); }
std::shared_ptr< IBackground > m_background
Definition: ISimulation.h:137

References ISimulation::m_background.

Referenced by DepthProbeSimulation::addBackgroundIntensity(), ISimulation2D::addBackgroundIntensity(), and addBackgroundIntensity().

◆ checkCache()

void SpecularSimulation::checkCache ( ) const
private

Definition at line 88 of file SpecularSimulation.cpp.

89 {
90  ASSERT(m_eles.size() == m_cache.size());
91 }
#define ASSERT(condition)
Definition: Assert.h:45

References ASSERT, m_cache, and m_eles.

Referenced by addDataToCache(), and moveDataFromCache().

◆ checkNodeArgs()

void INode::checkNodeArgs ( ) const
inherited

Raises exception if a parameter value is invalid.

Definition at line 27 of file INode.cpp.

28 {
29  size_t nP = m_P.size();
30  if (parDefs().size() != nP) {
31  std::cerr << "BUG in class " << className() << std::endl;
32  std::cerr << "#m_P = " << nP << std::endl;
33  std::cerr << "#PDf = " << parDefs().size() << std::endl;
34  for (const ParaMeta& pm : parDefs())
35  std::cerr << " PDf: " << pm.name << std::endl;
36  ASSERT(0);
37  }
38  ASSERT(parDefs().size() == nP);
39  for (size_t i = 0; i < nP; ++i) {
40  const ParaMeta pm = parDefs()[i];
41 
43  if (pm.vMin == -INF) {
44  ASSERT(pm.vMax == +INF);
45  // nothing to do
46  } else if (pm.vMax == +INF) {
47  ASSERT(pm.vMin == 0);
48  limits = RealLimits::nonnegative();
49  } else {
50  limits = RealLimits::limited(pm.vMin, pm.vMax);
51  }
52  limits.check(pm.name, m_P[i]);
53  }
54 }
const double INF
Definition: INode.h:26
virtual std::vector< ParaMeta > parDefs() const
Returns the parameter definitions, to be hard-coded in each leaf class.
Definition: INode.h:51
std::vector< double > m_P
Definition: INode.h:63
virtual std::string className() const =0
Returns the class name, to be hard-coded in each leaf class that inherits from INode.
Limits for a real fit parameter.
Definition: RealLimits.h:24
static RealLimits limitless()
Creates an object without bounds (default)
Definition: RealLimits.cpp:139
void check(const std::string &name, double value) const
Throws if value is outside limits. Parameter 'name' is for exception message.
Definition: RealLimits.cpp:170
static RealLimits nonnegative()
Creates an object which can have only positive values with 0. included.
Definition: RealLimits.cpp:124
static RealLimits limited(double left_bound_value, double right_bound_value)
Creates an object bounded from the left and right.
Definition: RealLimits.cpp:134
Metadata of one model parameter.
Definition: INode.h:29
double vMin
Definition: INode.h:33
double vMax
Definition: INode.h:34
std::string name
Definition: INode.h:30

References ASSERT, RealLimits::check(), INode::className(), INF, RealLimits::limited(), RealLimits::limitless(), INode::m_P, ParaMeta::name, RealLimits::nonnegative(), INode::parDefs(), ParaMeta::vMax, and ParaMeta::vMin.

Referenced by BarGauss::BarGauss(), BarLorentz::BarLorentz(), Bipyramid4::Bipyramid4(), Box::Box(), CantellatedCube::CantellatedCube(), Cone::Cone(), ConstantBackground::ConstantBackground(), CosineRippleBox::CosineRippleBox(), CosineRippleGauss::CosineRippleGauss(), CosineRippleLorentz::CosineRippleLorentz(), Cylinder::Cylinder(), DistributionCosine::DistributionCosine(), DistributionGate::DistributionGate(), DistributionGaussian::DistributionGaussian(), DistributionLogNormal::DistributionLogNormal(), DistributionLorentz::DistributionLorentz(), DistributionTrapezoid::DistributionTrapezoid(), Dodecahedron::Dodecahedron(), EllipsoidalCylinder::EllipsoidalCylinder(), FootprintGauss::FootprintGauss(), FootprintSquare::FootprintSquare(), FuzzySphere::FuzzySphere(), GaussSphere::GaussSphere(), HemiEllipsoid::HemiEllipsoid(), HollowSphere::HollowSphere(), HorizontalCylinder::HorizontalCylinder(), Icosahedron::Icosahedron(), LongBoxGauss::LongBoxGauss(), LongBoxLorentz::LongBoxLorentz(), PlatonicOctahedron::PlatonicOctahedron(), PlatonicTetrahedron::PlatonicTetrahedron(), Prism3::Prism3(), Prism6::Prism6(), Profile1DCauchy::Profile1DCauchy(), Profile1DCosine::Profile1DCosine(), Profile1DGate::Profile1DGate(), Profile1DGauss::Profile1DGauss(), Profile1DTriangle::Profile1DTriangle(), Profile1DVoigt::Profile1DVoigt(), Profile2DCauchy::Profile2DCauchy(), Profile2DCone::Profile2DCone(), Profile2DGate::Profile2DGate(), Profile2DGauss::Profile2DGauss(), Profile2DVoigt::Profile2DVoigt(), Pyramid2::Pyramid2(), Pyramid3::Pyramid3(), Pyramid4::Pyramid4(), Pyramid6::Pyramid6(), RotationEuler::RotationEuler(), RotationX::RotationX(), RotationY::RotationY(), RotationZ::RotationZ(), SawtoothRippleBox::SawtoothRippleBox(), SawtoothRippleGauss::SawtoothRippleGauss(), SawtoothRippleLorentz::SawtoothRippleLorentz(), Sphere::Sphere(), Spheroid::Spheroid(), TruncatedCube::TruncatedCube(), TruncatedSphere::TruncatedSphere(), and TruncatedSpheroid::TruncatedSpheroid().

Here is the call graph for this function:

◆ className()

std::string SpecularSimulation::className ( ) const
inlinefinalvirtual

Returns the class name, to be hard-coded in each leaf class that inherits from INode.

Implements INode.

Definition at line 39 of file SpecularSimulation.h.

39 { return "SpecularSimulation"; }

◆ createComputation()

std::unique_ptr< IComputation > SpecularSimulation::createComputation ( const reSample re_sample,
size_t  start,
size_t  n_elements 
)
overrideprivatevirtual

Generate a single threaded computation for a given range of simulation elements.

Parameters
re_samplePreprocessed version of our sample
startIndex of the first element to include into computation
n_elementsNumber of elements to process

Implements ISimulation.

Definition at line 79 of file SpecularSimulation.cpp.

81 {
82  ASSERT(start < m_eles.size() && start + n_elements <= m_eles.size());
83  const auto& begin = m_eles.begin() + static_cast<long>(start);
84  return std::make_unique<SpecularComputation>(re_sample, options(), progress(), begin,
85  begin + static_cast<long>(n_elements));
86 }
ProgressHandler & progress()
const SimulationOptions & options() const

References ASSERT, m_eles, ISimulation::options(), and ISimulation::progress().

Here is the call graph for this function:

◆ createCoordSystem()

ICoordSystem * SpecularSimulation::createCoordSystem ( ) const
overridevirtual

Implements ISimulation.

Definition at line 64 of file SpecularSimulation.cpp.

65 {
66  return m_scan->createCoordSystem();
67 }

References m_scan.

Referenced by pack_result().

◆ force_polarized()

bool SpecularSimulation::force_polarized ( ) const
overridevirtual

Force polarized computation even in absence of sample magnetization or external fields.

Implements ISimulation.

Definition at line 140 of file SpecularSimulation.cpp.

141 {
142  return m_scan->polarized();
143 }

References m_scan.

◆ getDistributions()

const std::vector< ParameterDistribution > & ISimulation::getDistributions ( ) const
inherited

Definition at line 237 of file ISimulation.cpp.

238 {
240 }
const std::vector< ParameterDistribution > & getDistributions() const

References DistributionHandler::getDistributions(), and ISimulation::m_distribution_handler.

Here is the call graph for this function:

◆ initDistributionHandler()

virtual void ISimulation::initDistributionHandler ( )
inlineprotectedvirtualinherited

Reimplemented in ISimulation2D.

Definition at line 106 of file ISimulation.h.

106 {}

Referenced by ISimulation::simulate().

◆ initElementVector()

void SpecularSimulation::initElementVector ( )
overrideprivatevirtual

Initializes the vector of ISimulation elements.

Implements ISimulation.

Definition at line 70 of file SpecularSimulation.cpp.

71 {
72  m_eles = m_scan->generateElements();
73 
74  if (!m_cache.empty())
75  return;
76  m_cache.resize(m_eles.size(), 0);
77 }

References m_cache, m_eles, and m_scan.

◆ intensityMapSize()

size_t SpecularSimulation::intensityMapSize ( ) const
overridevirtual

Returns the total number of the intensity values in the simulation result.

Implements ISimulation.

Definition at line 58 of file SpecularSimulation.cpp.

59 {
60  return m_scan->coordinateAxis()->size();
61 }

References m_scan.

◆ moveDataFromCache()

void SpecularSimulation::moveDataFromCache ( )
overrideprivatevirtual

Implements ISimulation.

Definition at line 132 of file SpecularSimulation.cpp.

133 {
134  checkCache();
135  for (size_t i = 0, size = m_eles.size(); i < size; ++i)
136  m_eles[i].setIntensity(m_cache[i]);
137  m_cache.clear();
138  m_cache.shrink_to_fit();
139 }

References checkCache(), m_cache, and m_eles.

Here is the call graph for this function:

◆ nodeChildren()

std::vector< const INode * > ISimulation::nodeChildren ( ) const
overridevirtualinherited

Returns all children.

Reimplemented from INode.

Reimplemented in ISimulation2D.

Definition at line 201 of file ISimulation.cpp.

202 {
203  std::vector<const INode*> result;
204  if (m_sample)
205  result << m_sample.get();
206  return result;
207 }
std::shared_ptr< MultiLayer > m_sample
Definition: ISimulation.h:133

References ISimulation::m_sample.

Referenced by ISimulation2D::nodeChildren().

◆ nodeOffspring()

std::vector< const INode * > INode::nodeOffspring ( ) const
inherited

Returns all descendants.

Definition at line 61 of file INode.cpp.

62 {
63  std::vector<const INode*> result;
64  result.push_back(this);
65  for (const auto* child : nodeChildren()) {
66  for (const auto* p : child->nodeOffspring())
67  result.push_back(p);
68  }
69  return result;
70 }
virtual std::vector< const INode * > nodeChildren() const
Returns all children.
Definition: INode.cpp:56

References INode::nodeChildren().

Here is the call graph for this function:

◆ normalize()

void SpecularSimulation::normalize ( size_t  start_ind,
size_t  n_elements 
)
overrideprivatevirtual

Normalize the detector counts to beam intensity, to solid angle, and to exposure angle.

Parameters
start_indIndex of the first element to operate on
n_elementsNumber of elements to process

Implements ISimulation.

Definition at line 105 of file SpecularSimulation.cpp.

106 {
107  std::vector<double> footprints = m_scan->footprint(start_ind, n_elements);
108 
109  for (size_t i = start_ind, k = 0; i < start_ind + n_elements; ++i, ++k) {
110  auto& element = m_eles[i];
111  element.setIntensity(element.intensity() * footprints[k]);
112  }
113 }

References m_eles, and m_scan.

◆ numberOfElements()

size_t SpecularSimulation::numberOfElements ( ) const
overrideprivatevirtual

Gets the number of elements this simulation needs to calculate.

Implements ISimulation.

Definition at line 46 of file SpecularSimulation.cpp.

47 {
48  return m_scan->numberOfElements();
49 }

References m_scan.

◆ options() [1/2]

SimulationOptions & ISimulation::options ( )
inherited

Definition at line 123 of file ISimulation.cpp.

124 {
125  ASSERT(m_options);
126  return *m_options;
127 }
std::shared_ptr< SimulationOptions > m_options
Definition: ISimulation.h:135

References ASSERT, and ISimulation::m_options.

◆ options() [2/2]

const SimulationOptions & ISimulation::options ( ) const
inherited

◆ pack_result()

SimulationResult SpecularSimulation::pack_result ( )
overrideprivatevirtual

Sets m_result.

Implements ISimulation.

Definition at line 51 of file SpecularSimulation.cpp.

52 {
53  ASSERT(!m_eles.empty());
54  Datafield data({m_scan->coordinateAxis()->clone()}, m_scan->createIntensities(m_eles));
55  return {data, createCoordSystem()};
56 }
Stores radiation power per bin.
Definition: Datafield.h:30
ICoordSystem * createCoordSystem() const override

References ASSERT, createCoordSystem(), m_eles, and m_scan.

Here is the call graph for this function:

◆ parDefs()

virtual std::vector<ParaMeta> INode::parDefs ( ) const
inlinevirtualinherited

Returns the parameter definitions, to be hard-coded in each leaf class.

Reimplemented in ConstantBackground, GaussSphere, FuzzySphere, RotationEuler, RotationZ, RotationY, RotationX, Crystal, Layer, HexagonalLattice2D, SquareLattice2D, BasicLattice2D, LayerRoughness, TruncatedSpheroid, TruncatedSphere, TruncatedCube, Spheroid, Sphere, SawtoothRippleLorentz, SawtoothRippleGauss, SawtoothRippleBox, Pyramid6, Pyramid4, Pyramid3, Pyramid2, Prism6, Prism3, PlatonicTetrahedron, PlatonicOctahedron, LongBoxLorentz, LongBoxGauss, Icosahedron, HorizontalCylinder, HollowSphere, HemiEllipsoid, EllipsoidalCylinder, Dodecahedron, Cylinder, CosineRippleLorentz, CosineRippleGauss, CosineRippleBox, Cone, CantellatedCube, Box, Bipyramid4, BarLorentz, BarGauss, Profile2DVoigt, Profile2DCone, Profile2DGate, Profile2DGauss, Profile2DCauchy, Profile1DVoigt, Profile1DCosine, Profile1DTriangle, Profile1DGate, Profile1DGauss, Profile1DCauchy, MisesGaussPeakShape, MisesFisherGaussPeakShape, LorentzFisherPeakShape, GaussFisherPeakShape, IsotropicLorentzPeakShape, IsotropicGaussPeakShape, ParticleLayout, InterferenceTwin, InterferenceRadialParaCrystal, InterferenceHardDisk, Interference2DSuperLattice, Interference2DParaCrystal, Interference1DLattice, DistributionTrapezoid, DistributionCosine, DistributionLogNormal, DistributionGaussian, DistributionLorentz, DistributionGate, ResolutionFunction2DGaussian, PolFilter, FootprintSquare, and FootprintGauss.

Definition at line 51 of file INode.h.

51 { return {}; }

Referenced by INode::checkNodeArgs(), and IFormFactor::pythonConstructor().

◆ prepareSimulation()

void SpecularSimulation::prepareSimulation ( )
inlineoverridevirtual

Put into a clean state for running a simulation.

Implements ISimulation.

Definition at line 48 of file SpecularSimulation.h.

48 {}

◆ progress()

ProgressHandler & ISimulation::progress ( )
protectedinherited

Definition at line 129 of file ISimulation.cpp.

130 {
132  return *m_progress;
133 }
std::shared_ptr< ProgressHandler > m_progress
Definition: ISimulation.h:136

References ASSERT, and ISimulation::m_progress.

Referenced by DepthProbeSimulation::createComputation(), ISimulation2D::createComputation(), and createComputation().

◆ runSingleSimulation()

void ISimulation::runSingleSimulation ( const reSample re_sample,
size_t  batch_start,
size_t  batch_size,
double  weight = 1.0 
)
privateinherited

Runs a single simulation with fixed parameter values. If desired, the simulation is run in several threads.

Definition at line 245 of file ISimulation.cpp.

247 {
249 
250  const size_t n_threads = m_options->getNumberOfThreads();
251  ASSERT(n_threads > 0);
252 
253  std::vector<std::unique_ptr<IComputation>> computations;
254 
255  for (size_t i_thread = 0; i_thread < n_threads; ++i_thread) {
256  const size_t thread_start = batch_start + startIndex(n_threads, i_thread, batch_size);
257  const size_t thread_size = batchSize(n_threads, i_thread, batch_size);
258  if (thread_size == 0)
259  break;
260  computations.emplace_back(createComputation(re_sample, thread_start, thread_size));
261  }
262  runComputations(computations);
263 
264  normalize(batch_start, batch_size);
265  addBackgroundIntensity(batch_start, batch_size);
266  addDataToCache(weight);
267 }
virtual void initElementVector()=0
Initializes the vector of ISimulation elements.
virtual void addDataToCache(double weight)=0
virtual void addBackgroundIntensity(size_t start_ind, size_t n_elements)=0
virtual std::unique_ptr< IComputation > createComputation(const reSample &re_sample, size_t start, size_t n_elements)=0
Generate a single threaded computation for a given range of simulation elements.
virtual void normalize(size_t start_ind, size_t n_elements)=0
Normalize the detector counts to beam intensity, to solid angle, and to exposure angle.

References ISimulation::addBackgroundIntensity(), ISimulation::addDataToCache(), ASSERT, ISimulation::createComputation(), ISimulation::initElementVector(), ISimulation::m_options, and ISimulation::normalize().

Referenced by ISimulation::simulate().

Here is the call graph for this function:

◆ sample()

const MultiLayer * ISimulation::sample ( ) const
inherited

Definition at line 191 of file ISimulation.cpp.

192 {
193  return m_sample.get();
194 }

References ISimulation::m_sample.

Referenced by ISimulation::simulate(), and DepthProbeSimulation::validityCheck().

◆ scan()

const ISpecularScan* SpecularSimulation::scan ( ) const
inline

Returns internal data handler.

Definition at line 52 of file SpecularSimulation.h.

52 { return m_scan.get(); }

References m_scan.

Referenced by SpecularSimulation().

◆ setBackground()

void ISimulation::setBackground ( const IBackground bg)
inherited

Definition at line 196 of file ISimulation.cpp.

197 {
198  m_background.reset(bg.clone());
199 }
IBackground * clone() const override=0

References IBackground::clone(), and ISimulation::m_background.

Here is the call graph for this function:

◆ setTerminalProgressMonitor()

void ISimulation::setTerminalProgressMonitor ( )
inherited

Initializes a progress monitor that prints to stdout.

Definition at line 142 of file ISimulation.cpp.

143 {
144  m_progress->subscribe([](size_t percentage_done) -> bool {
145  if (percentage_done < 100)
146  std::cout << std::setprecision(2) << "\r... " << percentage_done << "%" << std::flush;
147  else // wipe out
148  std::cout << "\r... 100%\n";
149  return true;
150  });
151 }

References ISimulation::m_progress.

◆ simulate()

SimulationResult ISimulation::simulate ( )
inherited

Run a simulation, and return the result.

Runs simulation with possible averaging over parameter distributions; returns result.

Definition at line 154 of file ISimulation.cpp.

155 {
157  throw std::runtime_error("Sample contains incompatible material definitions");
158  gsl_set_error_handler_off();
159 
161 
162  const auto re_sample = reSample::make(*sample(), options(), force_polarized());
163 
164  const size_t total_size = numberOfElements();
165  size_t param_combinations = m_distribution_handler.getTotalNumberOfSamples();
166 
167  m_progress->reset();
168  m_progress->setExpectedNTicks(param_combinations * total_size);
169 
170  // restrict calculation to current batch
171  const size_t n_batches = m_options->getNumberOfBatches();
172  const size_t current_batch = m_options->getCurrentBatch();
173 
174  const size_t batch_start = startIndex(n_batches, current_batch, total_size);
175  const size_t batch_size = batchSize(n_batches, current_batch, total_size);
176  ASSERT(batch_size);
177 
178  if (param_combinations == 1)
179  runSingleSimulation(re_sample, batch_start, batch_size, 1.);
180  else {
182  for (size_t index = 0; index < param_combinations; ++index) {
183  double weight = m_distribution_handler.setParameterValues(index);
184  runSingleSimulation(re_sample, batch_start, batch_size, weight);
185  }
186  }
188  return pack_result();
189 }
double setParameterValues(size_t index)
set the parameter values of the simulation object to a specific combination of values,...
size_t getTotalNumberOfSamples() const
get the total number of parameter value combinations (product of the individual sizes of each paramet...
virtual size_t numberOfElements() const =0
Gets the number of elements this simulation needs to calculate.
virtual void prepareSimulation()=0
Put into a clean state for running a simulation.
virtual SimulationResult pack_result()=0
Sets m_result.
virtual void moveDataFromCache()=0
void runSingleSimulation(const reSample &re_sample, size_t batch_start, size_t batch_size, double weight=1.0)
Runs a single simulation with fixed parameter values. If desired, the simulation is run in several th...
virtual bool force_polarized() const =0
Force polarized computation even in absence of sample magnetization or external fields.
virtual void initDistributionHandler()
Definition: ISimulation.h:106
static reSample make(const MultiLayer &sample, const SimulationOptions &options, bool forcePolarized=false)
Factory method that wraps the private constructor.
Definition: ReSample.cpp:309
bool ContainsCompatibleMaterials(const MultiLayer &sample)
Returns true if the sample contains non-default materials of one type only.

References ASSERT, SampleUtils::Multilayer::ContainsCompatibleMaterials(), ISimulation::force_polarized(), DistributionHandler::getTotalNumberOfSamples(), ISimulation::initDistributionHandler(), ISimulation::m_distribution_handler, ISimulation::m_options, ISimulation::m_progress, ISimulation::m_sample, reSample::make(), ISimulation::moveDataFromCache(), ISimulation::numberOfElements(), ISimulation::options(), ISimulation::pack_result(), ISimulation::prepareSimulation(), ISimulation::runSingleSimulation(), ISimulation::sample(), and DistributionHandler::setParameterValues().

Here is the call graph for this function:

◆ subscribe()

void ISimulation::subscribe ( const std::function< bool(size_t)> &  inform)
inherited

Definition at line 135 of file ISimulation.cpp.

136 {
138  m_progress->subscribe(inform);
139 }

References ASSERT, and ISimulation::m_progress.

◆ unitOfParameter()

std::string ISimulation::unitOfParameter ( ParameterDistribution::WhichParameter  which) const
inherited

Definition at line 209 of file ISimulation.cpp.

210 {
211  switch (which) {
213  return "nm";
216  return "rad";
217  default:
218  throw std::runtime_error("ISimulation::unitOfParameter(): missing implementation");
219  }
220 }

References ParameterDistribution::BeamAzimuthalAngle, ParameterDistribution::BeamInclinationAngle, and ParameterDistribution::BeamWavelength.

◆ updateIntensityMap()

virtual void ISimulation::updateIntensityMap ( )
inlineprotectedvirtualinherited

Reimplemented in OffspecSimulation.

Definition at line 97 of file ISimulation.h.

97 {}

◆ validateParametrization()

void SpecularSimulation::validateParametrization ( const ParameterDistribution par_distr) const
overrideprivatevirtual

Checks the distribution validity for simulation.

Reimplemented from ISimulation.

Definition at line 93 of file SpecularSimulation.cpp.

94 {
95  const bool zero_mean = par_distr.getDistribution()->mean() == 0.0;
96  if (zero_mean)
97  return;
98  /* TODO come back to this after refactoring of distribution handling
99  if (par_distr.whichParameter() == ParameterDistribution::BeamInclinationAngle)
100  throw std::runtime_error("Error in SpecularSimulation: parameter distribution of "
101  "beam inclination angle should have zero mean.");
102  */
103 }
virtual double mean() const =0
Returns the distribution-specific mean.
const IDistribution1D * getDistribution() const

References ParameterDistribution::getDistribution(), and IDistribution1D::mean().

Here is the call graph for this function:

Member Data Documentation

◆ m_background

std::shared_ptr<IBackground> ISimulation::m_background
privateinherited

Definition at line 137 of file ISimulation.h.

Referenced by ISimulation::background(), and ISimulation::setBackground().

◆ m_cache

std::vector<double> SpecularSimulation::m_cache
private

◆ m_distribution_handler

DistributionHandler ISimulation::m_distribution_handler
protectedinherited

◆ m_eles

std::vector<SpecularElement> SpecularSimulation::m_eles
private

◆ m_options

std::shared_ptr<SimulationOptions> ISimulation::m_options
privateinherited

◆ m_P

◆ m_progress

std::shared_ptr<ProgressHandler> ISimulation::m_progress
privateinherited

◆ m_sample

std::shared_ptr<MultiLayer> ISimulation::m_sample
privateinherited

◆ m_scan

std::unique_ptr<const ISpecularScan> SpecularSimulation::m_scan
private

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