BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
BeamItems.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Device/BeamItems.cpp
6 //! @brief Implements BeamItem hierarchy
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/Axis/IAxis.h"
17 #include "Base/Const/Units.h"
18 #include "Device/Beam/Beam.h"
25 #include <cmath>
26 
27 namespace {
28 
29 // defines wavelength limits according to given maximum q value
30 RealLimits getLimits(double max_q)
31 {
32  double upper_lim = std::nextafter(4.0 * M_PI / max_q, 0.0);
33  RealLimits result = RealLimits::positive();
34  result.setUpperLimit(upper_lim);
35  return result;
36 }
37 
38 } // namespace
39 
41 {
42  m_intensity.init("Intensity", "Beam intensity in neutrons (or gammas) per sec.", 1e+08,
43  Unit::unitless, 3, RealLimits::limited(0.0, 1e+32), "intensity");
44 
46 }
47 
49 {
50  s.assertVersion(0);
51  Serialize::rwProperty(s, m_intensity);
52  Serialize::rwClass(s, "wavelength", *m_wavelengthItem);
54  Serialize::rwClass(s, "inclination", *m_inclinationAngleItem);
55 }
56 
57 double BeamItem::wavelength() const
58 {
59  return m_wavelengthItem->wavelength();
60 }
61 
62 void BeamItem::setWavelength(double value)
63 {
64  m_wavelengthItem->resetToValue(value);
65 }
66 
68 {
69  return m_wavelengthItem.get();
70 }
71 
73 {
74  m_inclinationAngleItem->resetToValue(value);
75 }
76 
78 {
79  return m_inclinationAngleItem.get();
80 }
81 
83 {
84  return m_azimuthalAngleItem->azimuthalAngle();
85 }
86 
87 void BeamItem::setAzimuthalAngle(double value)
88 {
89  m_azimuthalAngleItem->resetToValue(value);
90 }
91 
93 {
94  return m_azimuthalAngleItem.get();
95 }
96 
97 std::unique_ptr<Beam> BeamItem::createBeam() const
98 {
99  double lambda = wavelength();
100  double inclination_angle = Units::deg2rad(getInclinationAngle());
101  double azimuthal_angle = Units::deg2rad(getAzimuthalAngle());
102 
103  auto result =
104  std::make_unique<Beam>(intensity(), lambda, Direction(inclination_angle, azimuthal_angle));
105 
106  return result;
107 }
108 
109 template <typename T>
111 {
112  m_wavelengthItem.reset(new T());
113 }
114 
115 template <typename T>
117 {
118  m_inclinationAngleItem.reset(new T());
119 }
120 
121 // Specular beam item
122 /* ------------------------------------------------------------------------- */
123 
125 {
126  m_inclinationAngleItem.reset(new SpecularBeamInclinationItem(owningInstrument));
127  initWavelength<SpecularBeamWavelengthItem>();
128 
129  m_footprint.init<FootprintItemCatalog>("Type", "Footprint type", "footprint");
130 }
131 
133 {
134  s.assertVersion(0);
135  Serialize::rwBaseClass<BeamItem>(s, "BeamItem", this);
136  Serialize::rwSelected<FootprintItemCatalog>(s, m_footprint);
137 }
138 
140 {
141  return 0.0;
142 }
143 
145 {
146  ASSERT(value == 0.0);
147  value = 0.0;
149 }
150 
152 {
154 }
155 
157 {
158  return inclinationAngleItem()->alphaAxis();
159 }
160 
162 {
163  return m_footprint.get();
164 }
165 
167 {
168  return m_footprint;
169 }
170 
172 {
174 }
175 
177 {
178  m_footprint.set(new FootprintSquareItem(value));
179 }
180 
181 void SpecularBeamItem::updateToData(const IAxis& axis, QString units)
182 {
183  if (units == "nbins") {
186  } else {
187  inclinationAngleItem()->initPointwiseAxis(axis, units);
189  }
191 }
192 
194 {
195  auto* item = inclinationAngleItem()->alphaAxis();
196  auto* wavelength = dynamic_cast<SpecularBeamWavelengthItem*>(wavelengthItem());
197  if (auto* pointwiseAxis = dynamic_cast<PointwiseAxisItem*>(item)) {
198  const auto* axis = pointwiseAxis->axis();
199  if (axis && pointwiseAxis->getUnitsLabel() == "q-space")
200  wavelength->setToRange(getLimits(axis->max()));
201  } else
202  wavelength->setToRange(RealLimits::positive());
203 }
204 
205 // GISAS beam item
206 /* ------------------------------------------------------------------------- */
207 
209 {
210  initInclinationAngle<BeamInclinationAngleItem>();
211  initWavelength<BeamWavelengthItem>();
212 }
213 
215 {
219  ->inclinationAngle();
220 }
221 
222 void GISASBeamItem::setInclinationAngleGetter(std::function<double()> getter)
223 {
224  m_inclinationAngleGetter = getter;
225 }
Defines class BeamAngleItems.
Defines BeamItem hierarchy.
Defines class BeamWavelengthItem.
Defines class FootprintItemCatalog.
Declares FootprintItem classes.
Defines pointwise axis item.
Defines class Streamer.
@ unitless
The BeamDistributionItem handles wavelength, inclination and azimuthal parameter distribution for Bea...
virtual BeamDistributionItem * inclinationAngleItem() const
Definition: BeamItems.cpp:77
std::unique_ptr< Beam > createBeam() const
Definition: BeamItems.cpp:97
void initInclinationAngle()
Definition: BeamItems.cpp:116
void setWavelength(double value)
Definition: BeamItems.cpp:62
std::unique_ptr< BeamDistributionItem > m_inclinationAngleItem
Definition: BeamItems.h:66
BeamAzimuthalAngleItem * azimuthalAngleItem() const
Definition: BeamItems.cpp:92
virtual void serialize(Streamer &s)
Definition: BeamItems.cpp:48
std::unique_ptr< BeamWavelengthItem > m_wavelengthItem
Definition: BeamItems.h:64
virtual void setInclinationAngle(double value)
Definition: BeamItems.cpp:72
void setAzimuthalAngle(double value)
Definition: BeamItems.cpp:87
double wavelength() const
Definition: BeamItems.cpp:57
virtual double getInclinationAngle() const =0
void initWavelength()
Definition: BeamItems.cpp:110
std::unique_ptr< BeamAzimuthalAngleItem > m_azimuthalAngleItem
Definition: BeamItems.h:65
double getAzimuthalAngle() const
Definition: BeamItems.cpp:82
BeamWavelengthItem * wavelengthItem() const
Definition: BeamItems.cpp:67
double getInclinationAngle() const override
Definition: BeamItems.cpp:214
void setInclinationAngleGetter(std::function< double()> getter)
Explicitly set a getter function for the inclination angle.
Definition: BeamItems.cpp:222
std::function< double()> m_inclinationAngleGetter
Definition: BeamItems.h:107
Abstract base class for instrument-specific item classes.
Item for non-uniform axis with specified coordinates.
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.
BeamInclinationAxisItem contains inclination angle axis for specular/off-specular simulations and als...
BasicAxisItem * alphaAxis() const
The currently selected axis.
void initPointwiseAxis(const IAxis &axis, QString units)
void setSquareFootprint(double value)
Definition: BeamItems.cpp:176
SelectionDescriptor< FootprintItem * > footprintSelection() const
Definition: BeamItems.cpp:166
SelectionProperty< FootprintItem * > m_footprint
Definition: BeamItems.h:91
void updateWavelength()
Definition: BeamItems.cpp:193
void setInclinationAngle(double value) override
Definition: BeamItems.cpp:144
FootprintItem * footprint() const
Definition: BeamItems.cpp:161
BasicAxisItem * inclinationAxis() const
Definition: BeamItems.cpp:156
void setGaussianFootprint(double value)
Definition: BeamItems.cpp:171
SpecularBeamItem(const InstrumentItem *owningInstrument)
Definition: BeamItems.cpp:124
void updateToData(const IAxis &axis, QString units)
Definition: BeamItems.cpp:181
SpecularBeamInclinationItem * inclinationAngleItem() const override
Definition: BeamItems.cpp:151
double getInclinationAngle() const override
Definition: BeamItems.cpp:139
void serialize(Streamer &s) override
Definition: BeamItems.cpp:132
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)
void rwClass(Streamer &s, const QString &tag, T &t)
Serializes an item from a class that provides the function void serialize(Streamer&).
Definition: Serialize.h:77