BornAgain  1.19.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 reflection and scattering
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"
16 #include "Base/Math/Constants.h"
17 #include "Base/Utils/Assert.h"
20 
21 // Allow for 90 degrees by adding a relatively small constant to pi/2
22 static constexpr double INCLINATION_LIMIT = M_PI_2 + 1e-10;
23 
24 Beam::Beam(double intensity, double wavelength, const Direction& direction)
25  : m_intensity(intensity)
26  , m_wavelength(wavelength)
27  // , m_direction(direction)
28  , m_alpha(direction.alpha())
29  , m_phi(direction.phi())
30 {
31  setName("Beam");
33  registerParameter("Wavelength", &m_wavelength).setUnit("nm").setNonnegative();
34  registerParameter("InclinationAngle", &m_alpha).setUnit("rad").setLimited(0, INCLINATION_LIMIT);
35  registerParameter("AzimuthalAngle", &m_phi).setUnit("rad").setLimited(-M_PI_2, M_PI_2);
36  registerVector("BlochVector", &m_bloch_vector, "");
37 }
38 
40 {
41  return Beam(1.0, 1.0, {0, 0});
42 }
43 
44 Beam::Beam(const Beam& other) : Beam(other.m_intensity, other.m_wavelength, other.direction())
45 {
47  setName(other.getName());
48  if (other.m_shape_factor) {
49  m_shape_factor.reset(other.m_shape_factor->clone());
51  }
52 }
53 
54 Beam& Beam::operator=(const Beam& other)
55 {
56  m_intensity = other.m_intensity;
57  m_wavelength = other.m_wavelength;
58  // m_direction = other.m_direction;
59  m_alpha = other.m_alpha;
60  m_phi = other.m_phi;
62  setName(other.getName());
63  if (other.m_shape_factor) {
64  m_shape_factor.reset(other.m_shape_factor->clone());
66  } else
67  m_shape_factor.release();
68  return *this;
69 }
70 
71 Beam::~Beam() = default;
72 
74 {
75  return M_TWOPI / m_wavelength * Direction(-direction().alpha(), direction().phi()).vector();
76 }
77 
78 void Beam::setWavelength(double wavelength)
79 {
80  if (wavelength <= 0.0)
81  throw std::runtime_error(
82  "Beam::setCentralK() -> Error. Wavelength can't be negative or zero.");
84 }
85 
86 void Beam::setDirection(const Direction& direction)
87 {
88  if (direction.alpha() < 0.0)
89  throw std::runtime_error(
90  "Beam::setCentralK() -> Error. Inclination angle alpha_i can't be negative.");
91  // m_direction = direction;
93  m_phi = direction.phi();
94 }
95 
96 void Beam::setInclination(const double alpha)
97 {
98  m_alpha = alpha;
99 }
100 
102 {
103  return m_shape_factor.get();
104 }
105 
107 {
108  m_shape_factor.reset(shape_factor.clone());
110 }
111 
112 void Beam::setPolarization(const kvector_t bloch_vector)
113 {
114  if (bloch_vector.mag() > 1.0) {
115  throw std::runtime_error(
116  "Beam::setPolarization: "
117  "The given Bloch vector cannot represent a real physical ensemble");
118  }
119  m_bloch_vector = bloch_vector;
120 }
121 
123 {
124  return m_bloch_vector;
125 }
126 
127 Eigen::Matrix2cd Beam::getPolarization() const
128 {
129  Eigen::Matrix2cd result;
130  double x = m_bloch_vector.x();
131  double y = m_bloch_vector.y();
132  double z = m_bloch_vector.z();
133  result(0, 0) = (1.0 + z) / 2.0;
134  result(0, 1) = complex_t(x, -y) / 2.0;
135  result(1, 0) = complex_t(x, y) / 2.0;
136  result(1, 1) = (1.0 - z) / 2.0;
137  return result;
138 }
139 
140 std::vector<const INode*> Beam::getChildren() const
141 {
142  if (m_shape_factor)
143  return {m_shape_factor.get()};
144  return {};
145 }
Defines the macro ASSERT.
static constexpr double INCLINATION_LIMIT
Definition: Beam.cpp:22
Defines class Beam.
std::complex< double > complex_t
Definition: Complex.h:20
Defines M_PI and some more mathematical constants.
#define M_TWOPI
Definition: Constants.h:54
#define M_PI_2
Definition: Constants.h:45
Defines class FootprintGauss.
Defines class RealParameter.
double mag() const
Returns magnitude of the vector.
T z() const
Returns z-component in cartesian coordinate system.
Definition: BasicVector3D.h:67
T y() const
Returns y-component in cartesian coordinate system.
Definition: BasicVector3D.h:65
T x() const
Returns x-component in cartesian coordinate system.
Definition: BasicVector3D.h:63
An incident neutron or x-ray beam.
Definition: Beam.h:27
double m_alpha
Definition: Beam.h:72
Direction direction() const
Definition: Beam.h:45
static Beam horizontalBeam()
Definition: Beam.cpp:39
void setDirection(const Direction &direction)
Definition: Beam.cpp:86
double m_wavelength
Definition: Beam.h:70
void setPolarization(const kvector_t bloch_vector)
Sets the polarization density matrix according to the given Bloch vector.
Definition: Beam.cpp:112
kvector_t m_bloch_vector
Bloch vector encoding the beam's polarization.
Definition: Beam.h:75
Eigen::Matrix2cd getPolarization() const
Returns the polarization density matrix (in spin basis along z-axis)
Definition: Beam.cpp:127
double m_phi
Definition: Beam.h:73
double wavelength() const
Definition: Beam.h:43
Beam & operator=(const Beam &other)
Definition: Beam.cpp:54
const IFootprintFactor * footprintFactor() const
Returns footprint factor.
Definition: Beam.cpp:101
void setFootprintFactor(const IFootprintFactor &shape_factor)
Sets footprint factor to the beam.
Definition: Beam.cpp:106
kvector_t getCentralK() const
Returns the wavevector.
Definition: Beam.cpp:73
void setInclination(const double alpha)
Definition: Beam.cpp:96
std::vector< const INode * > getChildren() const override
Returns a vector of children.
Definition: Beam.cpp:140
std::unique_ptr< IFootprintFactor > m_shape_factor
footprint correction handler
Definition: Beam.h:74
kvector_t getBlochVector() const
Definition: Beam.cpp:122
virtual ~Beam()
void setWavelength(double wavelength)
Definition: Beam.cpp:78
double m_intensity
beam intensity (neutrons/sec)
Definition: Beam.h:69
A direction in three-dimensional space.
Definition: Direction.h:24
double phi() const
Definition: Direction.h:30
double alpha() const
Definition: Direction.h:29
kvector_t vector() const
Returns Cartesian 3D vector.
Definition: Direction.cpp:24
Abstract base for classes that calculate the beam footprint factor.
virtual IFootprintFactor * clone() const =0
void registerChild(INode *node)
Definition: INode.cpp:57
const std::string & getName() const
void registerVector(const std::string &base_name, kvector_t *p_vec, const std::string &units="nm")
void setName(const std::string &name)
RealParameter & registerParameter(const std::string &name, double *parpointer)
RealParameter & setNonnegative()
RealParameter & setLimited(double lower, double upper)
RealParameter & setUnit(const std::string &name)