BornAgain  1.18.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 scattering at grazing incidence
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 
16 #include "Base/Types/Exceptions.h"
20 
22 {
23  initialize();
24 }
25 
27  std::vector<kvector_t> positions)
28 {
29  initialize();
30  addParticles(particle, positions);
31 }
32 
34 
36 {
37  ParticleComposition* p_result = new ParticleComposition();
38  p_result->setAbundance(m_abundance);
39  for (size_t index = 0; index < m_particles.size(); ++index)
40  p_result->addParticle(*m_particles[index]);
41  if (mP_rotation)
42  p_result->setRotation(*mP_rotation);
43  p_result->setPosition(m_position);
44  return p_result;
45 }
46 
48 {
49  if (m_particles.empty())
50  return {};
51  auto* result = new FormFactorWeighted;
52  auto particles = decompose();
53  for (auto p_particle : particles) {
54  std::unique_ptr<IFormFactor> P_particle_ff{p_particle->createFormFactor()};
55  result->addFormFactor(*P_particle_ff);
56  }
57  return result;
58 }
59 
61 {
62  IParticle* np = particle.clone();
64 }
65 
66 void ParticleComposition::addParticle(const IParticle& particle, kvector_t position)
67 {
68  IParticle* np = particle.clone();
69  np->translate(position);
71 }
72 
73 // Please note, that positions is not const reference here. This is intentional, to
74 // enable python lists to std::vector conversion
75 void ParticleComposition::addParticles(const IParticle& particle, std::vector<kvector_t> positions)
76 {
77  for (size_t i = 0; i < positions.size(); ++i)
78  addParticle(particle, positions[i]);
79 }
80 
81 std::vector<const INode*> ParticleComposition::getChildren() const
82 {
83  std::vector<const INode*> result = IParticle::getChildren();
84  for (auto& P_particle : m_particles)
85  result.push_back(P_particle.get());
86  return result;
87 }
88 
90 {
92  auto p_rotation = rotation();
93  auto translation = position();
94  for (auto& P_particle : m_particles) {
95  auto sublist = P_particle->decompose();
96  for (auto p_subparticle : sublist) {
97  if (p_rotation)
98  p_subparticle->rotate(*p_rotation);
99  p_subparticle->translate(translation);
100  result.push_back(p_subparticle->clone());
101  }
102  }
103  return result;
104 }
105 
107 {
108  auto particles = decompose();
109  ParticleLimits result = particles[check_index(0)]->bottomTopZ();
110  for (auto& P_particle : particles) {
111  ParticleLimits limits = P_particle->bottomTopZ();
112  result.m_bottom = std::min(result.m_bottom, limits.m_bottom);
113  result.m_top = std::max(result.m_top, limits.m_top);
114  }
115  return result;
116 }
117 
118 size_t ParticleComposition::check_index(size_t index) const
119 {
120  return index < m_particles.size()
121  ? index
123  "ParticleComposition::check_index() -> Index is out of bounds");
124 }
125 
127 {
128  p_particle->registerAbundance(false);
129  registerChild(p_particle);
130  m_particles.emplace_back(p_particle);
131 }
132 
134 {
135  setName("ParticleComposition");
137 }
Defines many exception classes in namespace Exceptionss.
Defines class FormFactorWeighted.
Defines class ParticleComposition.
Defines class ParticleDistribution.
Defines IRotation classes.
Coherent sum of different scalar IFormFactor's with different weights.
void setAbundance(double abundance)
Sets particle abundance.
Pure virtual base class for all form factors.
Definition: IFormFactor.h:40
void registerChild(INode *node)
Definition: INode.cpp:58
void setName(const std::string &name)
Pure virtual base class for Particle, ParticleComposition, ParticleCoreShell, MesoCrystal.
Definition: IParticle.h:33
void registerAbundance(bool make_registered=true)
Definition: IParticle.cpp:66
void translate(kvector_t translation) override final
Translates the particle.
Definition: IParticle.cpp:34
std::vector< const INode * > getChildren() const override
Returns a vector of children (const).
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
std::unique_ptr< IRotation > mP_rotation
Definition: IParticle.h:95
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 ISample object.
kvector_t m_position
Definition: IParticle.h:94
void setRotation(const IRotation &rotation)
Sets transformation.
Definition: IParticle.cpp:44
void registerParticleProperties()
Registers abundance and position.
Definition: IParticle.cpp:125
A composition of particles at fixed positions.
ParticleComposition * clone() const override final
Returns a clone of this ISample object.
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.
SafePointerVector< IParticle > decompose() const override final
Decompose in constituent IParticle objects.
std::vector< const INode * > getChildren() const override final
Returns a vector of children (const).
ParticleLimits bottomTopZ() const override final
Top and bottom z-coordinate.
void addParticles(const IParticle &particle, std::vector< kvector_t > positions)
IFormFactor * createFormFactor() const override final
Creates a form factor for this particle.
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:21
double m_top
Definition: ZLimits.h:23
double m_bottom
Definition: ZLimits.h:22