BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
InterferenceFunction2DSuperLattice.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Sample/Aggregate/InterferenceFunction2DSuperLattice.cpp
6 //! @brief Implements class InterferenceFunction2DSuperLattice.
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 
17 #include "Base/Types/Exceptions.h"
18 #include "Base/Utils/Integrator.h"
22 
23 #include <limits>
24 
26  unsigned size_1,
27  unsigned size_2)
28  : IInterferenceFunction(0), m_integrate_xi(false), mP_substructure(nullptr), m_size_1(size_1),
29  m_size_2(size_2)
30 {
31  setName("Interference2DSuperLattice");
34 }
35 
36 //! Constructor of two-dimensional interference function.
37 //! @param length_1: length of first lattice vector in nanometers
38 //! @param length_2: length of second lattice vector in nanometers
39 //! @param alpha: angle between lattice vectors in radians
40 //! @param xi: rotation of lattice with respect to x-axis (beam direction) in radians
41 //! @param size_1: correlation length in direction 1
42 //! @param size_2: correlation length in direction 2
44  double length_1, double length_2, double alpha, double xi, unsigned size_1, unsigned size_2)
45  : InterferenceFunction2DSuperLattice(BasicLattice(length_1, length_2, alpha, xi), size_1,
46  size_2)
47 {
48 }
49 
51 
53 {
55  ret->setPositionVariance(m_position_var);
56  ret->setSubstructureIFF(*mP_substructure);
57  ret->setIntegrationOverXi(integrationOverXi());
58  return ret;
59 }
60 
62 {
63  mP_substructure.reset(sub_iff.clone());
65 }
66 
68 {
69  return *mP_substructure;
70 }
71 
72 //! Creates square lattice.
73 // @param lattice_length: length of first and second lattice vectors in nanometers
74 // @param xi: rotation of lattice with respect to x-axis in radians
76 InterferenceFunction2DSuperLattice::createSquare(double lattice_length, double xi, unsigned size_1,
77  unsigned size_2)
78 {
79  return new InterferenceFunction2DSuperLattice(SquareLattice(lattice_length, xi), size_1,
80  size_2);
81 }
82 
83 //! Creates hexagonal lattice.
84 // @param lattice_length: length of first and second lattice vectors in nanometers
85 // @param xi: rotation of lattice with respect to x-axis in radians
87 InterferenceFunction2DSuperLattice::createHexagonal(double lattice_length, double xi,
88  unsigned size_1, unsigned size_2)
89 {
90  return new InterferenceFunction2DSuperLattice(HexagonalLattice(lattice_length, xi), size_1,
91  size_2);
92 }
93 
94 double InterferenceFunction2DSuperLattice::evaluate(const kvector_t q, double outer_iff) const
95 {
96  m_outer_iff = outer_iff;
97  m_qx = q.x();
98  m_qy = q.y();
99  if (!m_integrate_xi)
100  return interferenceForXi(mP_lattice->rotationAngle());
101  return RealIntegrator().integrate([&](double xi) -> double { return interferenceForXi(xi); },
102  0.0, M_TWOPI)
103  / M_TWOPI;
104 }
105 
107 {
108  m_integrate_xi = integrate_xi;
109  mP_lattice->setRotationEnabled(!m_integrate_xi); // deregister Xi in the case of integration
110 }
111 
113 {
114  if (!mP_lattice)
115  throw std::runtime_error("InterferenceFunctionFinite2DLattice::lattice() -> Error. "
116  "No lattice defined.");
117  return *mP_lattice;
118 }
119 
120 std::vector<const INode*> InterferenceFunction2DSuperLattice::getChildren() const
121 {
122  return std::vector<const INode*>() << mP_lattice << mP_substructure;
123 }
124 
126 {
127  using MathFunctions::Laue;
128 
129  const double a = mP_lattice->length1();
130  const double b = mP_lattice->length2();
131  const double xialpha = m_xi + mP_lattice->latticeAngle();
132 
133  const double qadiv2 = (q.x() * a * std::cos(m_xi) + q.y() * a * std::sin(m_xi)) / 2.0;
134  const double qbdiv2 = (q.x() * b * std::cos(xialpha) + q.y() * b * std::sin(xialpha)) / 2.0;
135  const double ampl = Laue(qadiv2, m_size_1) * Laue(qbdiv2, m_size_2);
136  return ampl * ampl / (m_size_1 * m_size_2);
137 }
138 
140 {
141  mP_lattice.reset(lattice.clone());
142  registerChild(mP_lattice.get());
143 }
144 
146 {
147  m_xi = xi; // TODO ASAP don't set as collateratel effect; rm mutable
148  const kvector_t q = kvector_t(m_qx, m_qy, 0.0);
149  const double outer_iff = iff_no_inner(q, m_outer_iff);
150  const double delta_xi = xi - mP_lattice->rotationAngle();
151  return mP_substructure->evaluate(q.rotatedZ(-delta_xi), outer_iff);
152 }
Defines many exception classes in namespace Exceptionss.
Defines classes RealIntegrator, ComplexIntegrator.
Defines class InterferenceFunction2DSuperLattice.
Defines class InterferenceFunctionNone.
Defines M_PI and some more mathematical constants.
#define M_TWOPI
Definition: MathConstants.h:49
Defines namespace MathFunctions.
Defines class RealParameter.
BasicVector3D< double > kvector_t
Definition: Vectors3D.h:21
BasicVector3D< T > rotatedZ(double a) const
Returns result of rotation around z-axis.
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
Pure virtual base class of interference functions.
double iff_no_inner(const kvector_t q, double outer_iff) const
Calculates the structure factor in the absence of extra inner structure.
virtual IInterferenceFunction * clone() const =0
Returns a clone of this ISample object.
void registerChild(INode *node)
Definition: INode.cpp:58
void setName(const std::string &name)
Interference function of a 2D superlattice with a configurable interference function for each lattice...
unsigned m_size_2
Size of the finite lattice in lattice units.
InterferenceFunction2DSuperLattice(const Lattice2D &lattice, unsigned size_1, unsigned size_2)
std::unique_ptr< IInterferenceFunction > mP_substructure
IFF of substructure.
void setSubstructureIFF(const IInterferenceFunction &sub_iff)
double evaluate(const kvector_t q, double outer_iff=1.0) const override final
Evaluates the interference function for a given wavevector transfer.
static InterferenceFunction2DSuperLattice * createHexagonal(double lattice_length, double xi, unsigned size_1, unsigned size_2)
Creates hexagonal lattice.
static InterferenceFunction2DSuperLattice * createSquare(double lattice_length, double xi, unsigned size_1, unsigned size_2)
Creates square lattice.
bool m_integrate_xi
Integrate over the orientation xi.
double iff_without_dw(const kvector_t q) const override final
Calculates the structure factor without Debye-Waller factor.
InterferenceFunction2DSuperLattice * clone() const override final
Returns a clone of this ISample object.
const IInterferenceFunction & substructureIFF() const
std::vector< const INode * > getChildren() const override final
Returns a vector of children (const).
Default interference function (i.e.
virtual Lattice2D * clone() const =0
To integrate a real function of a real variable.
Definition: Integrator.h:24
double integrate(const std::function< double(double)> &f, double lmin, double lmax)
Definition: Integrator.cpp:27
double Laue(const double x, size_t N)
Real Laue function: .