BornAgain  1.18.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 scattering at grazing incidence
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 
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 BasicLattice
66 // ************************************************************************** //
67 
68 BasicLattice::BasicLattice(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("BasicLattice::BasicLattice() -> Error. Lattice length can't be "
73  "negative or zero.");
74 
75  setName("BasicLattice");
76  registerParameter("LatticeLength1", &m_length1).setUnit("nm").setPositive();
77  registerParameter("LatticeLength2", &m_length2).setUnit("nm").setPositive();
78  registerParameter("Alpha", &m_angle).setUnit("rad");
79 }
80 
82 {
84 }
85 
87 {
88  return std::abs(m_length1 * m_length2 * std::sin(m_angle));
89 }
90 
91 // ************************************************************************** //
92 // class SquareLattice
93 // ************************************************************************** //
94 
95 SquareLattice::SquareLattice(double length, double xi) : Lattice2D(xi), m_length(length)
96 {
97  if (m_length <= 0.0)
98  throw std::runtime_error("SquareLattice::SquareLattice() -> Error. Lattice length can't be "
99  "negative or zero.");
100 
101  setName("SquareLattice");
102  registerParameter("LatticeLength", &m_length).setUnit("nm").setPositive();
103 }
104 
106 {
107  return new SquareLattice(m_length, m_xi);
108 }
109 
111 {
112  return M_PI / 2.0;
113 }
114 
116 {
117  return std::abs(m_length * m_length);
118 }
119 
120 // ************************************************************************** //
121 // class HexagonalLattice
122 // ************************************************************************** //
123 
124 HexagonalLattice::HexagonalLattice(double length, double xi) : Lattice2D(xi), m_length(length)
125 {
126  if (m_length <= 0.0)
127  throw std::runtime_error("HexagonalLattice::HexagonalLattice() -> Error. "
128  "Lattice length can't be negative or zero.");
129 
130  setName("HexagonalLattice");
131  registerParameter("LatticeLength", &m_length).setUnit("nm").setPositive();
132 }
133 
135 {
136  return new HexagonalLattice(m_length, m_xi);
137 }
138 
140 {
141  return M_TWOPI / 3.0;
142 }
143 
145 {
146  static const double sinval = std::sin(latticeAngle());
147  return std::abs(m_length * m_length * sinval);
148 }
Defines classes of Lattice2D family.
Defines M_PI and some more mathematical constants.
#define M_TWOPI
Definition: MathConstants.h:49
#define M_PI
Definition: MathConstants.h:39
Defines class ParameterPool.
Defines class RealParameter.
virtual double unitCellArea() const
Definition: Lattice2D.cpp:86
BasicLattice * clone() const
Definition: Lattice2D.cpp:81
double m_length1
Definition: Lattice2D.h:65
BasicLattice(double length1, double length2, double angle, double xi)
Definition: Lattice2D.cpp:68
double m_angle
Definition: Lattice2D.h:66
double m_length2
Definition: Lattice2D.h:65
HexagonalLattice * clone() const
Definition: Lattice2D.cpp:134
virtual double unitCellArea() const
Definition: Lattice2D.cpp:144
virtual double latticeAngle() const
Definition: Lattice2D.cpp:139
HexagonalLattice(double length, double xi)
Definition: Lattice2D.cpp:124
Base class for tree-like structures containing parameterized objects.
Definition: INode.h:49
const INode * parent() const
Definition: INode.cpp:74
RealParameter & registerParameter(const std::string &name, double *parpointer)
RealParameter * parameter(const std::string &name) const
Returns parameter with given 'name'.
void removeParameter(const std::string &name)
virtual void onChange()
Action to be taken in inherited class when a parameter has changed.
void setName(const std::string &name)
double rotationAngle() const
Definition: Lattice2D.h:39
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:47
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)
SquareLattice(double length, double xi=0.0)
Definition: Lattice2D.cpp:95
virtual double unitCellArea() const
Definition: Lattice2D.cpp:115
SquareLattice * clone() const
Definition: Lattice2D.cpp:105
double m_length
Definition: Lattice2D.h:84
virtual double latticeAngle() const
Definition: Lattice2D.cpp:110
Metadata of one model node.
Definition: INode.h:37