BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
ParticleLayoutItem.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Sample/ParticleLayoutItem.cpp
6 //! @brief Implements class ParticleLayoutItem
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2021
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
24 
26  : m_materials(materials)
27 {
28  m_ownDensity.init("Total particle density",
29  "Number of particles per area (particle surface density).\n "
30  "Should be defined for disordered and 1d-ordered particle collections.",
31  0.01, Unit::nanometerMinus2, 10, RealLimits::nonnegative(), "density");
32  m_weight.init("Weight",
33  "Weight of this particle layout.\nShould be used when multiple layouts define "
34  "different domains in the sample.",
35  1.0, Unit::unitless, "weight");
36 
37  m_interference.init<InterferenceItemCatalog>("Interference function", "", "interference");
38 }
39 
41 {
42  return m_ownDensity;
43 }
44 
46 {
47  // create descriptor with same value as own density, then change the getter and setter
49  d.set = [=](double d) {
51  const_cast<DoubleProperty*>(&m_ownDensity)->set(d);
52  };
53  d.get = [=]() { return totalDensityValue(); };
54  d.path = [=]() { return m_ownDensity.uid() + "/totalDensity"; };
55  return d;
56 }
57 
59 {
61  return m_ownDensity;
62 
63  ASSERT(m_interference.get());
64 
65  if (const auto* interLatticeItem =
66  dynamic_cast<const Interference2DAbstractLatticeItem*>(m_interference.get())) {
67  Lattice2DItem* latticeItem = interLatticeItem->latticeType().currentItem();
68  try {
69  const double area = latticeItem->unitCellArea();
70  return area == 0.0 ? 0.0 : 1.0 / area;
71  } catch (const std::exception&) {
72  // nothing to do here; new exception will be caught during job execution
73  return 0.0;
74  }
75  }
76 
77  if (const auto* hd = dynamic_cast<const InterferenceHardDiskItem*>(m_interference.get()))
78  return hd->density();
79 
80  ASSERT(false);
81 }
82 
84 {
85  return m_weight;
86 }
87 
88 QVector<ItemWithParticles*> ParticleLayoutItem::particles() const
89 {
90  return m_particles;
91 }
92 
94 {
95  m_particles << particle;
96 }
97 
99 {
100  m_particles.removeAll(particle);
101  delete particle;
102 }
103 
104 QVector<ItemWithParticles*> ParticleLayoutItem::containedItemsWithParticles() const
105 {
106  QVector<ItemWithParticles*> result;
107  for (auto* p : m_particles)
108  result << p << p->containedItemsWithParticles();
109  return result;
110 }
111 
113 {
114  return m_interference;
115 }
116 
118 {
120 }
121 
123 {
124  m_interference.set(nullptr);
125 }
126 
128 {
129  return dynamic_cast<const Interference2DAbstractLatticeItem*>(m_interference.get())
130  || dynamic_cast<const InterferenceHardDiskItem*>(m_interference.get());
131 }
132 
134 {
135  s.assertVersion(0);
138  Serialize::rwSelected<InterferenceItemCatalog>(s, m_interference);
139  Serialize::rwCatalogized<ItemWithParticlesCatalog>(s, "Particles", m_particles, m_materials);
140 }
Defines class InterferenceItemCatalog.
Defines InterferenceItems's classes.
Defines class ItemWithParticlesCatalog.
Defines classes Lattice2DItems.
Defines class MesoCrystalItem.
Defines class ParticleCompositionItem.
Defines class ParticleItem.
Defines class ParticleLayoutItem.
Defines class Streamer.
@ nanometerMinus2
@ unitless
Describes properties of a double value which are necessary to allow GUI representation,...
function< void(double)> set
function to set the value
function< double()> get
function to get the current value
function< QString()> path
Path describing this value. Used e.g. for undo/redo.
Class for representing a double value, its attributes and its accessors.
QString uid() const
Unique id of this double property.
void set(double d)
Set the contained value.
void init(const QString &label, const QString &tooltip, double value, const variant< QString, Unit > &unit, const QString &persistentTag)
double unitCellArea() const
DoubleDescriptor ownDensity() const
The density value which belonging only to the layout.
QVector< ItemWithParticles * > m_particles
SelectionProperty< InterferenceItem * > m_interference
const MaterialItems * m_materials
DoubleProperty m_weight
void addParticle(ItemWithParticles *particle)
DoubleProperty m_ownDensity
void removeParticle(ItemWithParticles *particle)
ParticleLayoutItem(const MaterialItems *materials)
bool totalDensityIsDefinedByInterference() const
Returns whether total density is defined by the currently selected interference.
QVector< ItemWithParticles * > containedItemsWithParticles() const
Return full hierarchical contained items with particles.
DoubleDescriptor weight() const
DoubleDescriptor totalDensity() const
The real density.
double totalDensityValue() const
QVector< ItemWithParticles * > particles() const
The particles this layout contains.
SelectionDescriptor< InterferenceItem * > interference() const
void serialize(Streamer &s)
void setInterference(InterferenceItem *interference)
Describes a selection (various possibilities and the current one).
void set(T t, bool callInitializer=false)
Directly set the new item.
T get() const
Direct access to the stored pointer.
void init(const QString &label, const QString &tooltip, const QString &persistentTag, ArgsForCreation... argsForCreation)
Initialize by means of a catalog class and optional creation arguments.
Supports serialization to or deserialization from QXmlStream.
Definition: Streamer.h:36
void assertVersion(unsigned expectedVersion) const
As reader, throws DeserializationException unless the expected version is read. As writer,...
Definition: Streamer.cpp:26
void rwProperty(Streamer &s, DoubleProperty &d)