BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
Beam.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Device/Beam/Beam.cpp
6 //! @brief Implements class Beam.
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 
15 #include "Device/Beam/Beam.h"
17 #include "Base/Types/Complex.h"
18 #include "Base/Types/Exceptions.h"
19 #include "Base/Utils/Assert.h"
22 
23 // Allow for 90 degrees by adding a relatively small constant to pi/2
24 static constexpr double INCLINATION_LIMIT = M_PI_2 + 1e-10;
25 
26 Beam::Beam(double wavelength, double alpha, double phi, double intensity)
27  : m_wavelength(wavelength), m_alpha(alpha), m_phi(phi), m_intensity(intensity)
28 {
29  setName("Beam");
30  registerParameter("Wavelength", &m_wavelength).setUnit("nm").setNonnegative();
31  registerParameter("InclinationAngle", &m_alpha).setUnit("rad").setLimited(0, INCLINATION_LIMIT);
32  registerParameter("AzimuthalAngle", &m_phi).setUnit("rad").setLimited(-M_PI_2, M_PI_2);
33  registerParameter("Intensity", &m_intensity).setNonnegative();
34  registerVector("BlochVector", &m_bloch_vector, "");
35 }
36 
37 Beam Beam::horizontalBeam()
38 {
39  return Beam(1.0, 0.0, 0.0, 1.0);
40 }
41 
42 Beam::Beam(const Beam& other)
43  : Beam(other.m_wavelength, other.m_alpha, other.m_phi, other.m_intensity)
44 {
45  m_bloch_vector = other.m_bloch_vector;
46  setName(other.getName());
47  if (other.m_shape_factor) {
48  m_shape_factor.reset(other.m_shape_factor->clone());
49  registerChild(m_shape_factor.get());
50  }
51 }
52 
53 Beam& Beam::operator=(const Beam& other)
54 {
55  m_wavelength = other.m_wavelength;
56  m_alpha = other.m_alpha;
57  m_phi = other.m_phi;
58  m_intensity = other.m_intensity;
59  m_bloch_vector = other.m_bloch_vector;
60  setName(other.getName());
61  if (other.m_shape_factor) {
62  m_shape_factor.reset(other.m_shape_factor->clone());
63  registerChild(m_shape_factor.get());
64  } else
65  m_shape_factor.release();
66  return *this;
67 }
68 
69 Beam::~Beam() = default;
70 
72 {
73  return vecOfLambdaAlphaPhi(m_wavelength, -m_alpha, m_phi);
74 }
75 
76 void Beam::setCentralK(double wavelength, double alpha_i, double phi_i)
77 {
78  if (wavelength <= 0.0)
80  "Beam::setCentralK() -> Error. Wavelength can't be negative or zero.");
81  if (alpha_i < 0.0)
83  "Beam::setCentralK() -> Error. Inclination angle alpha_i can't be negative.");
84  m_wavelength = wavelength;
85  m_alpha = alpha_i;
86  m_phi = phi_i;
87 }
88 
90 {
91  return m_shape_factor.get();
92 }
93 
94 void Beam::setFootprintFactor(const IFootprintFactor& shape_factor)
95 {
96  m_shape_factor.reset(shape_factor.clone());
97  registerChild(m_shape_factor.get());
98 }
99 
100 void Beam::setWidthRatio(double width_ratio)
101 {
102  if (!m_shape_factor)
103  throw std::runtime_error("Error in Beam::setWidthRatio: footprint factor is nullptr. "
104  "Probably, you have forgotten to initialize it.");
105  m_shape_factor->setWidthRatio(width_ratio);
106 }
107 
108 void Beam::setPolarization(const kvector_t bloch_vector)
109 {
110  if (bloch_vector.mag() > 1.0) {
112  "Beam::setPolarization: "
113  "The given Bloch vector cannot represent a real physical ensemble");
114  }
115  m_bloch_vector = bloch_vector;
116 }
117 
118 kvector_t Beam::getBlochVector() const
119 {
120  return m_bloch_vector;
121 }
122 
123 Eigen::Matrix2cd Beam::getPolarization() const
124 {
125  Eigen::Matrix2cd result;
126  double x = m_bloch_vector.x();
127  double y = m_bloch_vector.y();
128  double z = m_bloch_vector.z();
129  result(0, 0) = (1.0 + z) / 2.0;
130  result(0, 1) = complex_t(x, -y) / 2.0;
131  result(1, 0) = complex_t(x, y) / 2.0;
132  result(1, 1) = (1.0 - z) / 2.0;
133  return result;
134 }
135 
136 std::vector<const INode*> Beam::getChildren() const
137 {
138  if (m_shape_factor)
139  return {m_shape_factor.get()};
140  return {};
141 }
Defines the macro ASSERT.
BasicVector3D< double > vecOfLambdaAlphaPhi(double _lambda, double _alpha, double _phi)
Creates a vector<double> as a wavevector with given wavelength and angles.
Defines class Beam.
Defines complex_t, and a few elementary functions.
Defines many exception classes in namespace Exceptionss.
Defines class FootprintGauss.
Defines M_PI and some more mathematical constants.
Defines class RealParameter.
double mag() const
Returns magnitude of the vector.
T z() const
Returns z-component in cartesian coordinate system.
Definition: BasicVector3D.h:68
T y() const
Returns y-component in cartesian coordinate system.
Definition: BasicVector3D.h:66
T x() const
Returns x-component in cartesian coordinate system.
Definition: BasicVector3D.h:64
Beam defined by wavelength, direction and intensity.
Definition: Beam.h:27
void setWidthRatio(double width_ratio)
Sets beam to sample width ratio in footprint factor.
Definition: Beam.cpp:100
void setPolarization(const kvector_t bloch_vector)
Sets the polarization density matrix according to the given Bloch vector.
Definition: Beam.cpp:108
Eigen::Matrix2cd getPolarization() const
Returns the polarization density matrix (in spin basis along z-axis)
Definition: Beam.cpp:123
const IFootprintFactor * footprintFactor() const
Returns footprint factor.
Definition: Beam.cpp:89
void setFootprintFactor(const IFootprintFactor &shape_factor)
Sets footprint factor to the beam.
Definition: Beam.cpp:94
kvector_t getCentralK() const
Returns the wavevector.
Definition: Beam.cpp:71
std::vector< const INode * > getChildren() const override
Returns a vector of children (const).
Definition: Beam.cpp:136
void setCentralK(double wavelength, double alpha_i, double phi_i)
Sets the wavevector in terms of wavelength and incoming angles.
Definition: Beam.cpp:76
Abstract base for classes that calculate the beam footprint factor.