BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
DistributionHandler.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Param/Distrib/DistributionHandler.cpp
6 //! @brief Implements class DistributionHandler.
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 
18 
20 {
21  setName("DistributionHandler");
22 }
23 
25 
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 }
34 
36 {
37  return m_nbr_combinations;
38 }
39 
40 double DistributionHandler::setParameterValues(ParameterPool* p_parameter_pool, size_t index)
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 }
67 
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 }
79 
81 {
82  return m_distributions;
83 }
Defines class DistributionHandler.
Defines classes representing one-dimensional distributions.
Defines class ParameterPool.
Distributions_t m_distributions
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
const Distributions_t & getDistributions() const
virtual ~DistributionHandler()
double setParameterValues(ParameterPool *p_parameter_pool, size_t index)
set the parameter values of the simulation object to a specific combination of values,...
std::vector< ParameterDistribution > Distributions_t
void setParameterToMeans(ParameterPool *p_parameter_pool) const
Sets mean distribution values to the parameter pool.
size_t getTotalNumberOfSamples() const
get the total number of parameter value combinations (product of the individual sizes of each paramet...
std::vector< std::vector< ParameterSample > > m_cached_samples
void setName(const std::string &name)
A parametric distribution function, for use with any model parameter.
size_t getNbrSamples() const
get number of samples for this distribution
std::vector< ParameterSample > generateSamples() const
generate list of sampled values with their weight
Container with parameters for IParametricComponent object.
Definition: ParameterPool.h:29
int setMatchedParametersValue(const std::string &wildcards, double value)
Sets value of the nonzero parameters that match pattern ('*' allowed), or throws.