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);
34  registerVector("BlochVector", &m_bloch_vector, "");
35 }
36 
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 {
46  setName(other.getName());
47  if (other.m_shape_factor) {
48  m_shape_factor.reset(other.m_shape_factor->clone());
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;
60  setName(other.getName());
61  if (other.m_shape_factor) {
62  m_shape_factor.reset(other.m_shape_factor->clone());
64  } else
65  m_shape_factor.release();
66  return *this;
67 }
68 
69 Beam::~Beam() = default;
70 
72 {
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.");
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());
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 
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.
static constexpr double INCLINATION_LIMIT
Definition: Beam.cpp:24
Defines class Beam.
Defines complex_t, and a few elementary functions.
std::complex< double > complex_t
Definition: Complex.h:20
Defines many exception classes in namespace Exceptionss.
Defines class FootprintGauss.
Defines M_PI and some more mathematical constants.
#define M_PI_2
Definition: MathConstants.h:40
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
double m_alpha
Definition: Beam.h:78
Beam(double wavelength, double alpha, double phi, double intensity)
Definition: Beam.cpp:26
static Beam horizontalBeam()
Definition: Beam.cpp:37
void setWidthRatio(double width_ratio)
Sets beam to sample width ratio in footprint factor.
Definition: Beam.cpp:100
double m_wavelength
Definition: Beam.h:77
void setPolarization(const kvector_t bloch_vector)
Sets the polarization density matrix according to the given Bloch vector.
Definition: Beam.cpp:108
kvector_t m_bloch_vector
Bloch vector encoding the beam's polarization.
Definition: Beam.h:82
Eigen::Matrix2cd getPolarization() const
Returns the polarization density matrix (in spin basis along z-axis)
Definition: Beam.cpp:123
double m_phi
Definition: Beam.h:79
Beam & operator=(const Beam &other)
Definition: Beam.cpp:53
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
std::unique_ptr< IFootprintFactor > m_shape_factor
footprint correction handler
Definition: Beam.h:81
kvector_t getBlochVector() const
Definition: Beam.cpp:118
virtual ~Beam()
void setCentralK(double wavelength, double alpha_i, double phi_i)
Sets the wavevector in terms of wavelength and incoming angles.
Definition: Beam.cpp:76
double m_intensity
beam intensity (neutrons/sec)
Definition: Beam.h:80
Abstract base for classes that calculate the beam footprint factor.
virtual IFootprintFactor * clone() const =0
void registerChild(INode *node)
Definition: INode.cpp:58
RealParameter & registerParameter(const std::string &name, double *parpointer)
const std::string & getName() const
void setName(const std::string &name)
void registerVector(const std::string &base_name, kvector_t *p_vec, const std::string &units="nm")
RealParameter & setNonnegative()
RealParameter & setLimited(double lower, double upper)
RealParameter & setUnit(const std::string &name)