BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ParameterDistribution.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Param/Distrib/ParameterDistribution.cpp
6 //! @brief Implements class ParameterDistribution.
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 
17 
19  const IDistribution1D& distribution,
20  size_t nbr_samples, double sigma_factor,
21  const RealLimits& limits)
22  : IParametricComponent("ParameterDistribution")
23  , m_parname(par_name)
24  , m_nbr_samples(nbr_samples)
25  , m_sigma_factor(sigma_factor)
26  , m_limits(limits)
27  , m_xmin(1.0)
28  , m_xmax(-1.0)
29 {
30  m_distribution.reset(distribution.clone());
31  if (m_sigma_factor < 0.0)
32  throw std::runtime_error("ParameterDistribution::ParameterDistribution() -> Error."
33  "sigma factor cannot be negative");
34  if (nbr_samples == 0)
35  throw std::runtime_error("ParameterDistribution::ParameterDistribution() -> Error."
36  "Number of samples can't be zero.");
37 }
38 
40  const IDistribution1D& distribution,
41  size_t nbr_samples, double xmin, double xmax)
42  : IParametricComponent("ParameterDistribution")
43  , m_parname(par_name)
44  , m_nbr_samples(nbr_samples)
45  , m_sigma_factor(0.0)
46  , m_xmin(xmin)
47  , m_xmax(xmax)
48 {
49  m_distribution.reset(distribution.clone());
50  if (m_sigma_factor < 0.0)
51  throw std::runtime_error("ParameterDistribution::ParameterDistribution() -> Error."
52  "sigma factor cannot be negative");
53  if (nbr_samples == 0)
54  throw std::runtime_error("ParameterDistribution::ParameterDistribution() -> Error."
55  "Number of samples can't be zero.");
56  if (xmin >= xmax)
57  throw std::runtime_error("ParameterDistribution::ParameterDistribution() -> Error."
58  "xmin>=xmax");
59 }
60 
62  : IParametricComponent("ParameterDistribution")
63  , m_parname(other.m_parname)
64  , m_nbr_samples(other.m_nbr_samples)
65  , m_sigma_factor(other.m_sigma_factor)
66  , m_linked_par_names(other.m_linked_par_names)
67  , m_limits(other.m_limits)
68  , m_xmin(other.m_xmin)
69  , m_xmax(other.m_xmax)
70 {
71  m_distribution.reset(other.m_distribution->clone());
72 }
73 
75 
77 {
78  if (this != &other) {
79  this->m_parname = other.m_parname;
82  m_distribution.reset(other.m_distribution->clone());
84  m_limits = other.m_limits;
85  m_xmin = other.m_xmin;
86  m_xmax = other.m_xmax;
87  }
88  return *this;
89 }
90 
92 {
93  m_linked_par_names.push_back(par_name);
94  return *this;
95 }
96 
98 {
99  if (m_distribution && m_distribution->isDelta())
100  return 1;
101  return m_nbr_samples;
102 }
103 
104 std::vector<ParameterSample> ParameterDistribution::generateSamples() const
105 {
106  if (m_xmin < m_xmax)
107  return m_distribution->equidistantSamplesInRange(m_nbr_samples, m_xmin, m_xmax);
108  return m_distribution->equidistantSamples(m_nbr_samples, m_sigma_factor, m_limits);
109 }
110 
112 {
113  return m_distribution.get();
114 }
115 
117 {
118  return m_distribution.get();
119 }
Defines classes representing one-dimensional distributions.
Defines class ParameterDistribution.
Interface for one-dimensional distributions.
Definition: Distributions.h:34
virtual IDistribution1D * clone() const =0
Manages a local parameter pool, and a tree of child pools.
A parametric distribution function, for use with any model parameter.
ParameterDistribution & operator=(const ParameterDistribution &other)
Overload assignment operator.
size_t getNbrSamples() const
get number of samples for this distribution
std::unique_ptr< IDistribution1D > m_distribution
const IDistribution1D * getDistribution() const
ParameterDistribution(const std::string &par_name, const IDistribution1D &distribution, size_t nbr_samples, double sigma_factor=0.0, const RealLimits &limits=RealLimits())
ParameterDistribution & linkParameter(std::string par_name)
std::vector< ParameterSample > generateSamples() const
generate list of sampled values with their weight
std::vector< std::string > m_linked_par_names
virtual ~ParameterDistribution()
Limits for a real fit parameter.
Definition: RealLimits.h:24