BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
DistributionHandler Class Reference

Provides the functionality to average over parameter distributions with weights. More...

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

Public Types

typedef std::vector< ParameterDistributionDistributions_t
 

Public Member Functions

 DistributionHandler ()
 
virtual ~DistributionHandler ()
 
void addParameterDistribution (const ParameterDistribution &par_distr)
 
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 More...
 
virtual ParameterPoolcreateParameterTree () const
 Creates new parameter pool, with all local parameters and those of its children. More...
 
const Distributions_tgetDistributions () const
 
const std::string & getName () const
 
size_t getTotalNumberOfSamples () const
 get the total number of parameter value combinations (product of the individual sizes of each parameter distribution More...
 
virtual void onChange ()
 Action to be taken in inherited class when a parameter has changed. More...
 
RealParameterparameter (const std::string &name) const
 Returns parameter with given 'name'. More...
 
ParameterPoolparameterPool () const
 Returns pointer to the parameter pool. More...
 
std::string parametersToString () const
 Returns multiline string representing available parameters. More...
 
RealParameterregisterParameter (const std::string &name, double *parpointer)
 
void registerVector (const std::string &base_name, kvector_t *p_vec, const std::string &units="nm")
 
void removeParameter (const std::string &name)
 
void removeVector (const std::string &base_name)
 
void setName (const std::string &name)
 
void setParameterToMeans (ParameterPool *p_parameter_pool) const
 Sets mean distribution values to the parameter pool. More...
 
void setParameterValue (const std::string &name, double value)
 
double setParameterValues (ParameterPool *p_parameter_pool, size_t index)
 set the parameter values of the simulation object to a specific combination of values, determined by the index (which must be smaller than the total number of combinations) and returns the weight associated with this combination of parameter values More...
 
void setVectorValue (const std::string &base_name, kvector_t value)
 

Static Public Member Functions

static std::string XComponentName (const std::string &base_name)
 
static std::string YComponentName (const std::string &base_name)
 
static std::string ZComponentName (const std::string &base_name)
 

Private Attributes

std::vector< std::vector< ParameterSample > > m_cached_samples
 
Distributions_t m_distributions
 
std::string m_name
 
size_t m_nbr_combinations
 
std::unique_ptr< ParameterPoolm_pool
 parameter pool (kind of pointer-to-implementation) More...
 

Detailed Description

Provides the functionality to average over parameter distributions with weights.

Definition at line 29 of file DistributionHandler.h.

Member Typedef Documentation

◆ Distributions_t

Definition at line 31 of file DistributionHandler.h.

Constructor & Destructor Documentation

◆ DistributionHandler()

DistributionHandler::DistributionHandler ( )

Definition at line 19 of file DistributionHandler.cpp.

20 {
21  setName("DistributionHandler");
22 }
void setName(const std::string &name)

References IParametricComponent::setName().

Here is the call graph for this function:

◆ ~DistributionHandler()

DistributionHandler::~DistributionHandler ( )
virtualdefault

Member Function Documentation

◆ addParameterDistribution() [1/2]

void DistributionHandler::addParameterDistribution ( const ParameterDistribution par_distr)

Definition at line 26 of file DistributionHandler.cpp.

27 {
28  if (par_distr.getNbrSamples() > 0) {
29  m_distributions.push_back(par_distr);
30  m_nbr_combinations *= par_distr.getNbrSamples();
31  m_cached_samples.push_back(par_distr.generateSamples());
32  }
33 }
Distributions_t m_distributions
std::vector< std::vector< ParameterSample > > m_cached_samples
size_t getNbrSamples() const
get number of samples for this distribution
std::vector< ParameterSample > generateSamples() const
generate list of sampled values with their weight

References ParameterDistribution::generateSamples(), ParameterDistribution::getNbrSamples(), m_cached_samples, m_distributions, and m_nbr_combinations.

Here is the call graph for this function:

◆ addParameterDistribution() [2/2]

void DistributionHandler::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

Referenced by ISimulation::addParameterDistribution().

◆ createParameterTree()

ParameterPool * IParametricComponent::createParameterTree ( ) const
virtualinherited

Creates new parameter pool, with all local parameters and those of its children.

Reimplemented in INode.

Definition at line 36 of file IParametricComponent.cpp.

37 {
38  auto* result = new ParameterPool;
39  m_pool->copyToExternalPool("/" + getName() + "/", result);
40  return result;
41 }
const std::string & getName() const
std::unique_ptr< ParameterPool > m_pool
parameter pool (kind of pointer-to-implementation)
Container with parameters for IParametricComponent object.
Definition: ParameterPool.h:29

References IParametricComponent::getName(), and IParametricComponent::m_pool.

Referenced by IParametricComponent::parametersToString(), ParameterUtils::poolParameterUnits(), and IParametricComponent::setParameterValue().

Here is the call graph for this function:

◆ getDistributions()

const DistributionHandler::Distributions_t & DistributionHandler::getDistributions ( ) const

Definition at line 80 of file DistributionHandler.cpp.

81 {
82  return m_distributions;
83 }

References m_distributions.

Referenced by TransformFromDomain::setGISASBeamItem().

◆ getName()

◆ getTotalNumberOfSamples()

size_t DistributionHandler::getTotalNumberOfSamples ( ) const

get the total number of parameter value combinations (product of the individual sizes of each parameter distribution

Definition at line 35 of file DistributionHandler.cpp.

36 {
37  return m_nbr_combinations;
38 }

References m_nbr_combinations.

Referenced by ISimulation::runSimulation().

◆ onChange()

◆ parameter()

RealParameter * IParametricComponent::parameter ( const std::string &  name) const
inherited

Returns parameter with given 'name'.

Definition at line 86 of file IParametricComponent.cpp.

87 {
88  return m_pool->parameter(name);
89 }
QString const & name(EShape k)
Definition: particles.cpp:21

References IParametricComponent::m_pool, and RealSpace::Particles::name().

Referenced by DepthProbeSimulation::initialize(), SpecularSimulation::initialize(), Lattice3D::initialize(), IParticle::registerAbundance(), ParticleLayout::registerParticleDensity(), IParticle::registerPosition(), Layer::registerThickness(), Lattice2D::setRotationEnabled(), and DistributionLogNormal::setUnits().

Here is the call graph for this function:

◆ parameterPool()

ParameterPool* IParametricComponent::parameterPool ( ) const
inlineinherited

Returns pointer to the parameter pool.

Definition at line 39 of file IParametricComponent.h.

39 { return m_pool.get(); } // has non-const usages!

References IParametricComponent::m_pool.

Referenced by INode::INode(), IParametricComponent::IParametricComponent(), pyfmt2::argumentList(), SampleBuilderNode::borrow_builder_parameters(), SampleBuilderNode::reset(), and IDistribution1D::setUnits().

◆ parametersToString()

std::string IParametricComponent::parametersToString ( ) const
inherited

Returns multiline string representing available parameters.

Definition at line 43 of file IParametricComponent.cpp.

44 {
45  std::ostringstream result;
46  std::unique_ptr<ParameterPool> P_pool(createParameterTree());
47  result << *P_pool << "\n";
48  return result.str();
49 }
virtual ParameterPool * createParameterTree() const
Creates new parameter pool, with all local parameters and those of its children.

References IParametricComponent::createParameterTree().

Here is the call graph for this function:

◆ registerParameter()

RealParameter & IParametricComponent::registerParameter ( const std::string &  name,
double *  parpointer 
)
inherited

Definition at line 51 of file IParametricComponent.cpp.

52 {
53  return m_pool->addParameter(
54  new RealParameter(name, data, getName(), [&]() -> void { onChange(); }));
55 }
virtual void onChange()
Action to be taken in inherited class when a parameter has changed.
Wraps a parameter of type double.
Definition: RealParameter.h:31

References IParametricComponent::getName(), IParametricComponent::m_pool, RealSpace::Particles::name(), and IParametricComponent::onChange().

Referenced by BasicLattice2D::BasicLattice2D(), Beam::Beam(), CylindersInBABuilder::CylindersInBABuilder(), DetectionProperties::DetectionProperties(), HexagonalLattice2D::HexagonalLattice2D(), IInterferenceFunction::IInterferenceFunction(), INode::INode(), InterferenceFunction1DLattice::InterferenceFunction1DLattice(), InterferenceFunction2DParaCrystal::InterferenceFunction2DParaCrystal(), InterferenceFunctionHardDisk::InterferenceFunctionHardDisk(), InterferenceFunctionRadialParaCrystal::InterferenceFunctionRadialParaCrystal(), InterferenceFunctionTwin::InterferenceFunctionTwin(), Lattice2D::Lattice2D(), LayerRoughness::LayerRoughness(), MultiLayer::MultiLayer(), ParticleDistribution::ParticleDistribution(), PlainMultiLayerBySLDBuilder::PlainMultiLayerBySLDBuilder(), ResolutionFunction2DGaussian::ResolutionFunction2DGaussian(), ResonatorBuilder::ResonatorBuilder(), SquareLattice2D::SquareLattice2D(), TriangularRippleBuilder::TriangularRippleBuilder(), IParticle::registerAbundance(), ParticleLayout::registerParticleDensity(), Layer::registerThickness(), IParametricComponent::registerVector(), ParticleLayout::registerWeight(), and Lattice2D::setRotationEnabled().

Here is the call graph for this function:

◆ registerVector()

void IParametricComponent::registerVector ( const std::string &  base_name,
kvector_t p_vec,
const std::string &  units = "nm" 
)
inherited

Definition at line 57 of file IParametricComponent.cpp.

59 {
60  registerParameter(XComponentName(base_name), &((*p_vec)[0])).setUnit(units);
61  registerParameter(YComponentName(base_name), &((*p_vec)[1])).setUnit(units);
62  registerParameter(ZComponentName(base_name), &((*p_vec)[2])).setUnit(units);
63 }
static std::string XComponentName(const std::string &base_name)
static std::string ZComponentName(const std::string &base_name)
RealParameter & registerParameter(const std::string &name, double *parpointer)
static std::string YComponentName(const std::string &base_name)
RealParameter & setUnit(const std::string &name)
MVVM_MODEL_EXPORT std::string base_name(const std::string &path)
Provide the filename of a file path.
Definition: fileutils.cpp:78

References ModelView::Utils::base_name(), IParametricComponent::registerParameter(), RealParameter::setUnit(), IParametricComponent::XComponentName(), IParametricComponent::YComponentName(), and IParametricComponent::ZComponentName().

Referenced by Beam::Beam(), DetectionProperties::DetectionProperties(), InterferenceFunctionTwin::InterferenceFunctionTwin(), MultiLayer::MultiLayer(), Lattice3D::initialize(), and IParticle::registerPosition().

Here is the call graph for this function:

◆ removeParameter()

void IParametricComponent::removeParameter ( const std::string &  name)
inherited

Definition at line 91 of file IParametricComponent.cpp.

92 {
93  m_pool->removeParameter(name);
94 }

References IParametricComponent::m_pool, and RealSpace::Particles::name().

Referenced by IParticle::registerAbundance(), ParticleLayout::registerParticleDensity(), Layer::registerThickness(), IParametricComponent::removeVector(), and Lattice2D::setRotationEnabled().

Here is the call graph for this function:

◆ removeVector()

void IParametricComponent::removeVector ( const std::string &  base_name)
inherited

Definition at line 96 of file IParametricComponent.cpp.

References ModelView::Utils::base_name(), IParametricComponent::removeParameter(), IParametricComponent::XComponentName(), IParametricComponent::YComponentName(), and IParametricComponent::ZComponentName().

Referenced by IParticle::registerPosition().

Here is the call graph for this function:

◆ setName()

void IParametricComponent::setName ( const std::string &  name)
inlineinherited

Definition at line 69 of file IParametricComponent.h.

69 { m_name = name; }

References IParametricComponent::m_name, and RealSpace::Particles::name().

Referenced by BasicLattice2D::BasicLattice2D(), Beam::Beam(), ConvolutionDetectorResolution::ConvolutionDetectorResolution(), Crystal::Crystal(), DetectionProperties::DetectionProperties(), DistributionHandler(), FormFactorCoreShell::FormFactorCoreShell(), FormFactorCrystal::FormFactorCrystal(), FormFactorDecoratorMaterial::FormFactorDecoratorMaterial(), FormFactorDecoratorPositionFactor::FormFactorDecoratorPositionFactor(), FormFactorDecoratorRotation::FormFactorDecoratorRotation(), FormFactorWeighted::FormFactorWeighted(), HexagonalLattice2D::HexagonalLattice2D(), IDetector::IDetector(), INode::INode(), Instrument::Instrument(), InterferenceFunction1DLattice::InterferenceFunction1DLattice(), InterferenceFunction2DLattice::InterferenceFunction2DLattice(), InterferenceFunction2DParaCrystal::InterferenceFunction2DParaCrystal(), InterferenceFunction2DSuperLattice::InterferenceFunction2DSuperLattice(), InterferenceFunction3DLattice::InterferenceFunction3DLattice(), InterferenceFunctionFinite2DLattice::InterferenceFunctionFinite2DLattice(), InterferenceFunctionFinite3DLattice::InterferenceFunctionFinite3DLattice(), InterferenceFunctionHardDisk::InterferenceFunctionHardDisk(), InterferenceFunctionNone::InterferenceFunctionNone(), InterferenceFunctionRadialParaCrystal::InterferenceFunctionRadialParaCrystal(), InterferenceFunctionTwin::InterferenceFunctionTwin(), ISampleBuilder::ISampleBuilder(), IsGISAXSDetector::IsGISAXSDetector(), Lattice3D::Lattice3D(), Layer::Layer(), LayerInterface::LayerInterface(), LayerRoughness::LayerRoughness(), MultiLayer::MultiLayer(), ParticleCoreShell::ParticleCoreShell(), ParticleDistribution::ParticleDistribution(), ParticleLayout::ParticleLayout(), RectangularDetector::RectangularDetector(), ResolutionFunction2DGaussian::ResolutionFunction2DGaussian(), SampleBuilderNode::SampleBuilderNode(), SphericalDetector::SphericalDetector(), SquareLattice2D::SquareLattice2D(), Layer::clone(), LayersWithAbsorptionBuilder::createSampleByIndex(), Basic2DParaCrystalBuilder::createSampleByIndex(), ParticleInVacuumBuilder::createSampleByIndex(), SimpleMagneticRotationBuilder::createSampleByIndex(), DepthProbeSimulation::initialize(), GISASSimulation::initialize(), OffSpecularSimulation::initialize(), SpecularSimulation::initialize(), SpecularDetector1D::initialize(), MesoCrystal::initialize(), Particle::initialize(), ParticleComposition::initialize(), Beam::operator=(), SampleBuilderNode::operator=(), SampleBuilderNode::reset(), and SampleBuilderNode::setSBN().

Here is the call graph for this function:

◆ setParameterToMeans()

void DistributionHandler::setParameterToMeans ( ParameterPool p_parameter_pool) const

Sets mean distribution values to the parameter pool.

Definition at line 68 of file DistributionHandler.cpp.

69 {
70  for (auto& distribution : m_distributions) {
71  const std::string par_name = distribution.getMainParameterName();
72  const double mean_val = distribution.getDistribution()->getMean();
73  if (p_parameter_pool->setMatchedParametersValue(par_name, mean_val) != 1)
74  throw std::runtime_error("Error in DistributionHandler::setParameterToMeans: parameter "
75  "name matches nothing or more than "
76  "one parameter");
77  }
78 }
int setMatchedParametersValue(const std::string &wildcards, double value)
Sets value of the nonzero parameters that match pattern ('*' allowed), or throws.

References m_distributions, and ParameterPool::setMatchedParametersValue().

Referenced by ISimulation::runSimulation().

Here is the call graph for this function:

◆ setParameterValue()

void IParametricComponent::setParameterValue ( const std::string &  name,
double  value 
)
inherited

Definition at line 65 of file IParametricComponent.cpp.

66 {
67  if (name.find('*') == std::string::npos && name.find('/') == std::string::npos) {
68  m_pool->setParameterValue(name, value);
69  } else {
70  std::unique_ptr<ParameterPool> P_pool{createParameterTree()};
71  if (name.find('*') != std::string::npos)
72  P_pool->setMatchedParametersValue(name, value);
73  else
74  P_pool->setParameterValue(name, value);
75  }
76 }

References IParametricComponent::createParameterTree(), IParametricComponent::m_pool, RealSpace::Particles::name(), and ParameterPool::setMatchedParametersValue().

Referenced by AsymRippleBuilder::buildSample(), and IParametricComponent::setVectorValue().

Here is the call graph for this function:

◆ setParameterValues()

double DistributionHandler::setParameterValues ( ParameterPool p_parameter_pool,
size_t  index 
)

set the parameter values of the simulation object to a specific combination of values, determined by the index (which must be smaller than the total number of combinations) and returns the weight associated with this combination of parameter values

Definition at line 40 of file DistributionHandler.cpp.

41 {
42  if (index >= m_nbr_combinations)
43  throw std::runtime_error(
44  "DistributionWeighter::setParameterValues: "
45  "index must be smaller than the total number of parameter combinations");
46  size_t n_distr = m_distributions.size();
47  double weight = 1.0;
48  if (n_distr == 0)
49  return weight;
50  for (size_t param_index = n_distr - 1;; --param_index) {
51  size_t remainder = index % m_distributions[param_index].getNbrSamples();
52  index /= m_distributions[param_index].getNbrSamples();
53  int changed = p_parameter_pool->setMatchedParametersValue(
54  m_distributions[param_index].getMainParameterName(),
55  m_cached_samples[param_index][remainder].value);
56  if (changed != 1) {
57  throw std::runtime_error("DistributionWeighter::setParameterValues: "
58  "parameter name matches nothing or more than "
59  "one parameter");
60  }
61  weight *= m_cached_samples[param_index][remainder].weight;
62  if (param_index == 0)
63  break;
64  }
65  return weight;
66 }

References m_cached_samples, m_distributions, m_nbr_combinations, and ParameterPool::setMatchedParametersValue().

Referenced by ISimulation::runSimulation().

Here is the call graph for this function:

◆ setVectorValue()

void IParametricComponent::setVectorValue ( const std::string &  base_name,
kvector_t  value 
)
inherited

Definition at line 78 of file IParametricComponent.cpp.

79 {
83 }
T z() const
Returns z-component in cartesian coordinate system.
Definition: BasicVector3D.h:67
T y() const
Returns y-component in cartesian coordinate system.
Definition: BasicVector3D.h:65
T x() const
Returns x-component in cartesian coordinate system.
Definition: BasicVector3D.h:63
void setParameterValue(const std::string &name, double value)

References ModelView::Utils::base_name(), IParametricComponent::setParameterValue(), BasicVector3D< T >::x(), IParametricComponent::XComponentName(), BasicVector3D< T >::y(), IParametricComponent::YComponentName(), BasicVector3D< T >::z(), and IParametricComponent::ZComponentName().

Here is the call graph for this function:

◆ XComponentName()

std::string IParametricComponent::XComponentName ( const std::string &  base_name)
staticinherited

Definition at line 103 of file IParametricComponent.cpp.

104 {
105  return base_name + "X";
106 }

References ModelView::Utils::base_name().

Referenced by Lattice3D::initialize(), IParticle::registerPosition(), IParametricComponent::registerVector(), IParametricComponent::removeVector(), IParametricComponent::setVectorValue(), and VectorParameterTranslator::translate().

Here is the call graph for this function:

◆ YComponentName()

std::string IParametricComponent::YComponentName ( const std::string &  base_name)
staticinherited

Definition at line 108 of file IParametricComponent.cpp.

109 {
110  return base_name + "Y";
111 }

References ModelView::Utils::base_name().

Referenced by IParametricComponent::registerVector(), IParametricComponent::removeVector(), IParametricComponent::setVectorValue(), and VectorParameterTranslator::translate().

Here is the call graph for this function:

◆ ZComponentName()

std::string IParametricComponent::ZComponentName ( const std::string &  base_name)
staticinherited

Definition at line 113 of file IParametricComponent.cpp.

114 {
115  return base_name + "Z";
116 }

References ModelView::Utils::base_name().

Referenced by IParametricComponent::registerVector(), IParametricComponent::removeVector(), IParametricComponent::setVectorValue(), and VectorParameterTranslator::translate().

Here is the call graph for this function:

Member Data Documentation

◆ m_cached_samples

std::vector<std::vector<ParameterSample> > DistributionHandler::m_cached_samples
private

Definition at line 61 of file DistributionHandler.h.

Referenced by addParameterDistribution(), and setParameterValues().

◆ m_distributions

Distributions_t DistributionHandler::m_distributions
private

◆ m_name

std::string IParametricComponent::m_name
privateinherited

◆ m_nbr_combinations

size_t DistributionHandler::m_nbr_combinations
private

◆ m_pool


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