BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
Instrument.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Device/Instrument/Instrument.cpp
6 //! @brief Implements class Instrument.
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 
20 Instrument::Instrument() : m_detector(new SphericalDetector), m_beam(Beam::horizontalBeam())
21 {
22  setName("Instrument");
25 }
26 
27 Instrument::Instrument(const Instrument& other) : INode(), m_beam(other.m_beam)
28 {
29  if (other.m_detector)
30  setDetector(*other.m_detector);
32  setName(other.getName());
33 }
34 
35 Instrument::~Instrument() = default;
36 
38 {
39  if (this != &other) {
40  m_beam = other.m_beam;
42  if (other.m_detector)
43  setDetector(*other.m_detector);
44  }
45  return *this;
46 }
47 
48 void Instrument::setDetector(const IDetector& detector)
49 {
50  m_detector.reset(detector.clone());
52  initDetector();
53 }
54 
56 {
57  if (!m_detector)
59  "Instrument::initDetector() -> Error. Detector is not initialized.");
60  m_detector->init(getBeam());
61 }
62 
63 std::vector<const INode*> Instrument::getChildren() const
64 {
65  std::vector<const INode*> result;
66  result.push_back(&m_beam);
67  if (m_detector)
68  result.push_back(m_detector.get());
69  return result;
70 }
71 
73 {
74  m_detector->setResolutionFunction(p_resolution_function);
75 }
76 
78 {
79  m_detector->removeDetectorResolution();
80 }
81 
83 {
84  m_detector->applyDetectorResolution(p_intensity_map);
85 }
86 
87 void Instrument::setBeamParameters(double wavelength, double alpha_i, double phi_i)
88 {
89  m_beam.setCentralK(wavelength, alpha_i, phi_i);
90  if (m_detector)
91  initDetector();
92 }
93 
95 {
96  return m_detector->detectorMask();
97 }
98 
99 void Instrument::setBeam(const Beam& beam)
100 {
101  m_beam = beam;
102  if (m_detector)
103  initDetector();
104 }
105 
106 void Instrument::setBeamIntensity(double intensity)
107 {
108  m_beam.setIntensity(intensity);
109 }
110 
112 {
113  m_beam.setPolarization(bloch_vector);
114 }
115 
117 {
118  return m_beam.getIntensity();
119 }
120 
122 {
124  return m_detector.get();
125 }
126 
128 {
130  return *m_detector;
131 }
132 
134 {
136  return *m_detector;
137 }
138 
140 {
142  IDetector2D* p = dynamic_cast<IDetector2D*>(m_detector.get());
143  if (!p)
144  throw std::runtime_error("Error: Detector is not twodimensional");
145  return *p;
146 }
147 
149 {
151  IDetector2D* const p = dynamic_cast<IDetector2D* const>(m_detector.get());
152  if (!p)
153  throw std::runtime_error("Error: Detector is not twodimensional");
154  return *p;
155 }
156 
157 const IAxis& Instrument::getDetectorAxis(size_t index) const
158 {
159  return m_detector->getAxis(index);
160 }
161 
163 {
164  return m_detector->dimension();
165 }
166 
167 void Instrument::setAnalyzerProperties(const kvector_t direction, double efficiency,
168  double total_transmission)
169 {
170  m_detector->setAnalyzerProperties(direction, efficiency, total_transmission);
171 }
#define ASSERT(condition)
Definition: Assert.h:26
Defines class Histogram2D.
Defines interface class IResolutionFunction2D.
Defines class Instrument.
Defines class SphericalDetector.
Beam defined by wavelength, direction and intensity.
Definition: Beam.h:27
void setPolarization(const kvector_t bloch_vector)
Sets the polarization density matrix according to the given Bloch vector.
Definition: Beam.cpp:108
void setIntensity(double intensity)
Sets the beam intensity in neutrons/sec.
Definition: Beam.h:48
double getIntensity() const
Returns the beam intensity in neutrons/sec.
Definition: Beam.h:45
void setCentralK(double wavelength, double alpha_i, double phi_i)
Sets the wavevector in terms of wavelength and incoming angles.
Definition: Beam.cpp:76
Collection of detector masks.
Definition: DetectorMask.h:29
Interface for one-dimensional axes.
Definition: IAxis.h:25
Abstract 2D detector interface.
Definition: IDetector2D.h:31
Abstract detector interface.
Definition: IDetector.h:36
IDetector * clone() const override=0
Base class for tree-like structures containing parameterized objects.
Definition: INode.h:49
void registerChild(INode *node)
Definition: INode.cpp:58
const std::string & getName() const
void setName(const std::string &name)
Interface providing two-dimensional resolution function.
Assembles beam, detector and their relative positions with respect to the sample.
Definition: Instrument.h:34
Beam & getBeam()
Definition: Instrument.h:44
void applyDetectorResolution(OutputData< double > *p_intensity_map) const
apply the detector resolution to the given intensity map
Definition: Instrument.cpp:82
void setAnalyzerProperties(const kvector_t direction, double efficiency, double total_transmission)
Sets the polarization analyzer characteristics of the detector.
Definition: Instrument.cpp:167
void setBeamIntensity(double intensity)
Definition: Instrument.cpp:106
const IAxis & getDetectorAxis(size_t index) const
Definition: Instrument.cpp:157
double getBeamIntensity() const
Definition: Instrument.cpp:116
const DetectorMask * getDetectorMask() const
Definition: Instrument.cpp:94
virtual ~Instrument()
void setBeamParameters(double wavelength, double alpha_i, double phi_i)
Sets the beam wavelength and incoming angles.
Definition: Instrument.cpp:87
void setDetector(const IDetector &detector)
Sets the detector (axes can be overwritten later)
Definition: Instrument.cpp:48
IDetector & detector()
Definition: Instrument.cpp:133
void setDetectorResolutionFunction(const IResolutionFunction2D &p_resolution_function)
Sets detector resolution function.
Definition: Instrument.cpp:72
void initDetector()
init detector with beam settings
Definition: Instrument.cpp:55
void removeDetectorResolution()
Removes detector resolution function.
Definition: Instrument.cpp:77
const IDetector * getDetector() const
Definition: Instrument.cpp:121
std::vector< const INode * > getChildren() const
Returns a vector of children (const).
Definition: Instrument.cpp:63
void setBeam(const Beam &beam)
Definition: Instrument.cpp:99
size_t getDetectorDimension() const
Definition: Instrument.cpp:162
void setBeamPolarization(const kvector_t bloch_vector)
Sets the beam's polarization according to the given Bloch vector.
Definition: Instrument.cpp:111
IDetector2D & detector2D()
Definition: Instrument.cpp:139
Instrument & operator=(const Instrument &other)
Definition: Instrument.cpp:37
std::unique_ptr< IDetector > m_detector
Definition: Instrument.h:93
Beam m_beam
Definition: Instrument.h:94
A spherical detector with axes and resolution function.