BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
Lattice2D.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Sample/Lattice/Lattice2D.cpp
6 //! @brief Implements classes of Lattice2D family.
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/Math/Constants.h"
19 #include <cmath>
20 
21 // ************************************************************************************************
22 // class Lattice2D
23 // ************************************************************************************************
24 
25 Lattice2D::Lattice2D(const NodeMeta& meta, const std::vector<double>& PValues)
26  : INode(meta, PValues)
27 {
28 }
29 
30 Lattice2D::Lattice2D(double xi) : m_xi(xi)
31 {
32  registerParameter("Xi", &m_xi).setUnit("rad");
33 }
34 
36 {
37  const double sinalpha = std::sin(latticeAngle());
38  const double ainv = M_TWOPI / length1() / sinalpha;
39  const double binv = M_TWOPI / length2() / sinalpha;
40  const double xi = rotationAngle();
41  const double xialpha = xi + latticeAngle();
42 
43  return {+ainv * std::sin(xialpha), -ainv * std::cos(xialpha), -binv * std::sin(xi),
44  +binv * std::cos(xi)};
45 }
46 
48 {
49  if (parent())
50  parent()->onChange();
51 }
52 
53 void Lattice2D::setRotationEnabled(bool enabled) // TODO ASAP replace by generic mechanism
54 {
55  if (enabled) {
56  if (parameter("Xi"))
57  return;
58  registerParameter("Xi", &m_xi).setUnit("rad");
59  } else {
60  removeParameter("Xi");
61  }
62 }
63 
64 // ************************************************************************************************
65 // class BasicLattice2D
66 // ************************************************************************************************
67 
68 BasicLattice2D::BasicLattice2D(double length1, double length2, double angle, double xi)
69  : Lattice2D(xi), m_length1(length1), m_length2(length2), m_angle(angle)
70 {
71  if (m_length1 <= 0.0 || m_length2 <= 0.0)
72  throw std::runtime_error(
73  "BasicLattice2D::BasicLattice2D() -> Error. Lattice length can't be "
74  "negative or zero.");
75 
76  setName("BasicLattice2D");
77  registerParameter("LatticeLength1", &m_length1).setUnit("nm").setPositive();
78  registerParameter("LatticeLength2", &m_length2).setUnit("nm").setPositive();
79  registerParameter("Alpha", &m_angle).setUnit("rad");
80 }
81 
83 {
85 }
86 
88 {
89  return std::abs(m_length1 * m_length2 * std::sin(m_angle));
90 }
91 
92 // ************************************************************************************************
93 // class SquareLattice2D
94 // ************************************************************************************************
95 
96 SquareLattice2D::SquareLattice2D(double length, double xi) : Lattice2D(xi), m_length(length)
97 {
98  if (m_length <= 0.0)
99  throw std::runtime_error(
100  "SquareLattice2D::SquareLattice2D() -> Error. Lattice length can't be "
101  "negative or zero.");
102 
103  setName("SquareLattice2D");
104  registerParameter("LatticeLength", &m_length).setUnit("nm").setPositive();
105 }
106 
108 {
109  return new SquareLattice2D(m_length, m_xi);
110 }
111 
113 {
114  return M_PI / 2.0;
115 }
116 
118 {
119  return std::abs(m_length * m_length);
120 }
121 
122 // ************************************************************************************************
123 // class HexagonalLattice2D
124 // ************************************************************************************************
125 
126 HexagonalLattice2D::HexagonalLattice2D(double length, double xi) : Lattice2D(xi), m_length(length)
127 {
128  if (m_length <= 0.0)
129  throw std::runtime_error("HexagonalLattice2D::HexagonalLattice2D() -> Error. "
130  "Lattice length can't be negative or zero.");
131 
132  setName("HexagonalLattice2D");
133  registerParameter("LatticeLength", &m_length).setUnit("nm").setPositive();
134 }
135 
137 {
138  return new HexagonalLattice2D(m_length, m_xi);
139 }
140 
142 {
143  return M_TWOPI / 3.0;
144 }
145 
147 {
148  static const double sinval = std::sin(latticeAngle());
149  return std::abs(m_length * m_length * sinval);
150 }
Defines M_PI and some more mathematical constants.
#define M_TWOPI
Definition: Constants.h:54
#define M_PI
Definition: Constants.h:44
Defines classes of Lattice2D family.
Defines class ParameterPool.
Defines class RealParameter.
A two-dimensional Bravais lattice with no special symmetry.
Definition: Lattice2D.h:53
double m_length1
Definition: Lattice2D.h:67
virtual double unitCellArea() const
Definition: Lattice2D.cpp:87
BasicLattice2D * clone() const
Definition: Lattice2D.cpp:82
double m_length2
Definition: Lattice2D.h:67
double m_angle
Definition: Lattice2D.h:68
BasicLattice2D(double length1, double length2, double angle, double xi)
Definition: Lattice2D.cpp:68
A two-dimensional Bravais lattice with hexagonal symmetry.
Definition: Lattice2D.h:92
virtual double unitCellArea() const
Definition: Lattice2D.cpp:146
HexagonalLattice2D * clone() const
Definition: Lattice2D.cpp:136
virtual double latticeAngle() const
Definition: Lattice2D.cpp:141
HexagonalLattice2D(double length, double xi)
Definition: Lattice2D.cpp:126
Base class for tree-like structures containing parameterized objects.
Definition: INode.h:49
const INode * parent() const
Definition: INode.cpp:84
void removeParameter(const std::string &name)
RealParameter * parameter(const std::string &name) const
Returns parameter with given 'name'.
virtual void onChange()
Action to be taken in inherited class when a parameter has changed.
void setName(const std::string &name)
RealParameter & registerParameter(const std::string &name, double *parpointer)
A two-dimensional Bravais lattice.
Definition: Lattice2D.h:23
double rotationAngle() const
Definition: Lattice2D.h:40
ReciprocalBases reciprocalBases() const
Definition: Lattice2D.cpp:35
virtual double latticeAngle() const =0
virtual void onChange()
Action to be taken in inherited class when a parameter has changed.
Definition: Lattice2D.cpp:47
double m_xi
Definition: Lattice2D.h:48
virtual double length2() const =0
virtual double length1() const =0
void setRotationEnabled(bool enabled)
Definition: Lattice2D.cpp:53
Lattice2D(const NodeMeta &meta, const std::vector< double > &PValues)
Definition: Lattice2D.cpp:25
RealParameter & setPositive()
RealParameter & setUnit(const std::string &name)
A two-dimensional Bravais lattice with square unit cell.
Definition: Lattice2D.h:73
SquareLattice2D * clone() const
Definition: Lattice2D.cpp:107
SquareLattice2D(double length, double xi=0.0)
Definition: Lattice2D.cpp:96
double m_length
Definition: Lattice2D.h:87
virtual double latticeAngle() const
Definition: Lattice2D.cpp:112
virtual double unitCellArea() const
Definition: Lattice2D.cpp:117
Metadata of one model node.
Definition: INode.h:38