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 
35 Lattice2D::ReciprocalBases Lattice2D::reciprocalBases() const
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 
81 BasicLattice* BasicLattice::clone() const
82 {
83  return new BasicLattice(m_length1, m_length2, m_angle, m_xi);
84 }
85 
86 double BasicLattice::unitCellArea() const
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 
105 SquareLattice* SquareLattice::clone() const
106 {
107  return new SquareLattice(m_length, m_xi);
108 }
109 
110 double SquareLattice::latticeAngle() const
111 {
112  return M_PI / 2.0;
113 }
114 
115 double SquareLattice::unitCellArea() const
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 
134 HexagonalLattice* HexagonalLattice::clone() const
135 {
136  return new HexagonalLattice(m_length, m_xi);
137 }
138 
139 double HexagonalLattice::latticeAngle() const
140 {
141  return M_TWOPI / 3.0;
142 }
143 
144 double HexagonalLattice::unitCellArea() const
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.
Defines class ParameterPool.
Defines class RealParameter.
Base class for tree-like structures containing parameterized objects.
Definition: INode.h:49
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.
virtual void onChange()
Action to be taken in inherited class when a parameter has changed.
Definition: Lattice2D.cpp:47
Metadata of one model node.
Definition: INode.h:37