BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ParticleComposition.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Sample/Particle/ParticleComposition.cpp
6 //! @brief Implements class ParticleComposition.
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 
19 
21 {
22  initialize();
23 }
24 
26  std::vector<kvector_t> positions)
27 {
28  initialize();
29  addParticles(particle, positions);
30 }
31 
33 
35 {
37  result->setAbundance(m_abundance);
38  for (size_t index = 0; index < m_particles.size(); ++index)
39  result->addParticle(*m_particles[index]);
40  if (m_rotation)
41  result->setRotation(*m_rotation);
42  result->setPosition(m_position);
43  return result;
44 }
45 
47 {
48  if (m_particles.empty())
49  return {};
50  auto* result = new FormFactorWeighted;
51  auto particles = decompose();
52  for (const auto* particle : particles) {
53  std::unique_ptr<IFormFactor> P_particle_ff(particle->createFormFactor());
54  result->addFormFactor(*P_particle_ff);
55  }
56  return result;
57 }
58 
60 {
61  IParticle* np = particle.clone();
63 }
64 
65 void ParticleComposition::addParticle(const IParticle& particle, kvector_t position)
66 {
67  IParticle* np = particle.clone();
68  np->translate(position);
70 }
71 
72 // Please note, that positions is not const reference here. This is intentional, to
73 // enable python lists to std::vector conversion
74 void ParticleComposition::addParticles(const IParticle& particle, std::vector<kvector_t> positions)
75 {
76  for (size_t i = 0; i < positions.size(); ++i)
77  addParticle(particle, positions[i]);
78 }
79 
80 std::vector<const INode*> ParticleComposition::getChildren() const
81 {
82  std::vector<const INode*> result = IParticle::getChildren();
83  for (auto& P_particle : m_particles)
84  result.push_back(P_particle.get());
85  return result;
86 }
87 
89 {
91  auto* rot = rotation();
92  auto translation = position();
93  for (const auto& particle : m_particles) {
94  const auto sublist = particle->decompose();
95  for (auto* subparticle : sublist) {
96  if (rot)
97  subparticle->rotate(*rot);
98  subparticle->translate(translation);
99  result.push_back(subparticle->clone());
100  }
101  }
102  return result;
103 }
104 
106 {
107  const auto particles = decompose();
108  ParticleLimits result = particles[check_index(0)]->bottomTopZ();
109  for (const auto& particle : particles) {
110  ParticleLimits limits = particle->bottomTopZ();
111  result.m_bottom = std::min(result.m_bottom, limits.m_bottom);
112  result.m_top = std::max(result.m_top, limits.m_top);
113  }
114  return result;
115 }
116 
117 size_t ParticleComposition::check_index(size_t index) const
118 {
119  return index < m_particles.size()
120  ? index
121  : throw std::runtime_error(
122  "ParticleComposition::check_index() -> Index is out of bounds");
123 }
124 
126 {
127  p_particle->registerAbundance(false);
128  registerChild(p_particle);
129  m_particles.emplace_back(p_particle);
130 }
131 
133 {
134  setName("ParticleComposition");
136 }
Defines class FormFactorWeighted.
Defines class ParticleComposition.
Defines class ParticleDistribution.
Defines IRotation classes.
Coherent sum of different scalar IFormFactors with different weights.
void setAbundance(double abundance)
Sets particle abundance.
Abstract base class for all form factors.
Definition: IFormFactor.h:36
void registerChild(INode *node)
Definition: INode.cpp:57
void setName(const std::string &name)
Abstract base class for Particle, ParticleComposition, ParticleCoreShell, MesoCrystal.
Definition: IParticle.h:33
void registerAbundance(bool make_registered=true)
Definition: IParticle.cpp:66
std::vector< const INode * > getChildren() const override
Returns a vector of children.
Definition: IParticle.cpp:61
void setPosition(kvector_t position)
Sets relative position of the particle's reference point in the coordinate system of parent.
Definition: IParticle.h:50
kvector_t position() const
Returns particle position.
Definition: IParticle.h:45
const IRotation * rotation() const
Returns rotation object.
Definition: IParticle.cpp:39
IParticle * clone() const override=0
Returns a clone of this ISampleNode object.
kvector_t m_position
Definition: IParticle.h:94
std::unique_ptr< IRotation > m_rotation
Definition: IParticle.h:95
void setRotation(const IRotation &rotation)
Sets transformation.
Definition: IParticle.cpp:44
void translate(kvector_t translation) final
Translates the particle.
Definition: IParticle.cpp:34
void registerParticleProperties()
Registers abundance and position.
Definition: IParticle.cpp:125
A composition of particles at fixed positions.
IFormFactor * createFormFactor() const final
Creates a form factor for this particle.
void addParticle(const IParticle &particle)
std::vector< std::unique_ptr< IParticle > > m_particles
size_t check_index(size_t index) const
void addParticlePointer(IParticle *p_particle)
For internal use.
ParticleLimits bottomTopZ() const final
Top and bottom z-coordinate.
ParticleComposition * clone() const final
Returns a clone of this ISampleNode object.
std::vector< const INode * > getChildren() const final
Returns a vector of children.
void addParticles(const IParticle &particle, std::vector< kvector_t > positions)
SafePointerVector< IParticle > decompose() const final
Decompose in constituent IParticle objects.
A vector of pointers, owned by *this, with methods to handle them safely.
void push_back(T *pointer)
Vertical extension of a particle, specified by bottom and top z coordinate.
Definition: ZLimits.h:26
double m_top
Definition: ZLimits.h:28
double m_bottom
Definition: ZLimits.h:27