BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ParticleDistribution.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Sample/Particle/ParticleDistribution.cpp
6 //! @brief Implements class ParticleDistribution.
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 
16 #include "Base/Types/Exceptions.h"
22 #include <map>
23 
25  const ParameterDistribution& par_distr)
26  : m_par_distribution(par_distr)
27 {
28  setName("ParticleDistribution");
29  mP_particle.reset(prototype.clone());
31  mP_particle->registerAbundance(false);
32  if (auto dist = m_par_distribution.getDistribution())
33  registerChild(dist);
34  registerParameter("Abundance", &m_abundance);
35 }
36 
38 {
40  p_result->setAbundance(m_abundance);
41  return p_result;
42 }
43 
45 {
46  mP_particle->translate(translation);
47 }
48 
50 {
51  mP_particle->rotate(rotation);
52 }
53 
54 //! Returns particle clones with parameter values drawn from distribution.
55 
57 {
58  std::unique_ptr<ParameterPool> P_pool{mP_particle->createParameterTree()};
59  std::string main_par_name = m_par_distribution.getMainParameterName();
60  double main_par_value = P_pool->getUniqueMatch(main_par_name)->value();
61 
62  // Preset link ratios:
63  std::map<std::string, double> linked_ratios;
64  for (const std::string& name : m_par_distribution.getLinkedParameterNames())
65  linked_ratios[name] =
66  main_par_value == 0 ? 1.0 : P_pool->getUniqueMatch(name)->value() / main_par_value;
67 
68  // Draw distribution samples; for each sample, create one particle clone:
69  std::vector<ParameterSample> main_par_samples = m_par_distribution.generateSamples();
71  for (const ParameterSample& main_sample : main_par_samples) {
72  IParticle* p_particle_clone = mP_particle->clone();
73  std::unique_ptr<ParameterPool> P_new_pool{p_particle_clone->createParameterTree()};
74  P_new_pool->setUniqueMatchValue(main_par_name, main_sample.value);
75  for (auto it = linked_ratios.begin(); it != linked_ratios.end(); ++it)
76  P_new_pool->setUniqueMatchValue(it->first, main_sample.value * it->second);
77  p_particle_clone->setAbundance(abundance() * main_sample.weight);
78  result.push_back(p_particle_clone);
79  }
80  return result;
81 }
82 
83 std::vector<const INode*> ParticleDistribution::getChildren() const
84 {
85  std::vector<const INode*> result = std::vector<const INode*>() << mP_particle;
86  if (auto dist = m_par_distribution.getDistribution())
87  result.push_back(dist);
88  return result;
89 }
90 
92 {
94  parameterDistribution().getMainParameterName());
95 }
Defines classes representing one-dimensional distributions.
Defines many exception classes in namespace Exceptionss.
Defines interface IParticle.
Defines class ParameterPool.
Defines namespace ParameterUtils.
Defines class ParticleDistribution.
Defines class RealParameter.
void setAbundance(double abundance)
Sets particle abundance.
double abundance() const
ParameterPool * createParameterTree() const
Creates new parameter pool, with all local parameters and those of its children.
Definition: INode.cpp:116
void registerChild(INode *node)
Definition: INode.cpp:58
RealParameter & registerParameter(const std::string &name, double *parpointer)
void setName(const std::string &name)
Pure virtual base class for Particle, ParticleComposition, ParticleCoreShell, MesoCrystal.
Definition: IParticle.h:33
IParticle * clone() const override=0
Returns a clone of this ISample object.
Pure virtual interface for rotations.
Definition: Rotations.h:27
A parametric distribution function, for use with any model parameter.
const IDistribution1D * getDistribution() const
std::vector< ParameterSample > generateSamples() const
generate list of sampled values with their weight
std::vector< std::string > getLinkedParameterNames() const
get list of linked parameter names
std::string getMainParameterName() const
get the main parameter's name
void setUniqueMatchValue(const std::string &pattern, double value)
Sets value of the one parameter that matches pattern ('*' allowed), or throws.
A parameter value with a weight, as obtained when sampling from a distribution.
A particle type that is a parametric distribution of IParticle's.
ParameterDistribution m_par_distribution
const IParticle & prototype() const
Returns the prototype particle, used for generating multiple ones.
std::vector< const INode * > getChildren() const override final
Returns a vector of children (const).
std::unique_ptr< IParticle > mP_particle
SafePointerVector< IParticle > generateParticles() const
Returns list of new particles generated according to a distribution.
ParameterDistribution parameterDistribution() const
Returns the distributed parameter data.
std::string mainUnits() const
ParticleDistribution(const IParticle &prototype, const ParameterDistribution &par_distr)
void rotate(const IRotation &rotation) override final
Applies the given rotation to the particle.
ParticleDistribution * clone() const override final
Returns a clone of this ISample object.
void translate(kvector_t translation) override final
Translates the particle with the given vector.
A vector of pointers, owned by *this, with methods to handle them safely.
void push_back(T *pointer)
std::string poolParameterUnits(const IParameterized &node, const std::string &parName)
Returns units of main parameter.