BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
SpecularMagneticNewNCStrategy.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Sample/Specular/SpecularMagneticNewNCStrategy.cpp
6 //! @brief Implements class SpecularMagneticNewNCStrategy.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
16 
17 namespace
18 {
20 }
21 
22 std::pair<Eigen::Matrix2cd, Eigen::Matrix2cd>
24  const MatrixRTCoefficients_v3& coeff_i1,
25  double sigma) const
26 {
27  complex_t beta_i = coeff_i.m_lambda(1) - coeff_i.m_lambda(0);
28  complex_t beta_i1 = coeff_i1.m_lambda(1) - coeff_i1.m_lambda(0);
29 
30  auto roughness_matrix = [sigma, &coeff_i, &coeff_i1, beta_i, beta_i1](double sign) {
31  const complex_t alpha_p = coeff_i1.m_lambda(0) + coeff_i1.m_lambda(1)
32  + sign * (coeff_i.m_lambda(0) + coeff_i.m_lambda(1));
33  auto b_p_vec = beta_i1 * coeff_i1.m_b + sign * beta_i * coeff_i.m_b;
34 
35  auto square = [](auto& v) { return v.x() * v.x() + v.y() * v.y() + v.z() * v.z(); };
36  complex_t beta_p = std::sqrt(checkForUnderflow(square(b_p_vec)));
37  b_p_vec /= beta_p;
38 
39  const complex_t alpha_pp = -(alpha_p * alpha_p + beta_p * beta_p) * sigma * sigma / 8.;
40  const complex_t beta_pp = -alpha_p * beta_p * sigma * sigma / 4.;
41 
42  Eigen::Matrix2cd QL, QR;
43 
44  const complex_t factor1 = std::sqrt(2. * (1. + b_p_vec.z()));
45  const complex_t factor2 = std::sqrt(2. * (1. - b_p_vec.z()));
46  QL << (b_p_vec.z() + 1.) / factor1, (b_p_vec.z() - 1.) / factor2,
47  (b_p_vec.x() + I * b_p_vec.y()) / factor1, (b_p_vec.x() + I * b_p_vec.y()) / factor2;
48  QR << (b_p_vec.z() + 1.) / factor1, (b_p_vec.x() - I * b_p_vec.y()) / factor1,
49  (b_p_vec.z() - 1.) / factor2, (b_p_vec.x() - I * b_p_vec.y()) / factor2;
50 
51  const Eigen::Matrix2cd exp1 =
52  Eigen::DiagonalMatrix<complex_t, 2>({std::exp(alpha_pp), std::exp(alpha_pp)});
53 
54  if (std::abs(beta_p) > std::numeric_limits<double>::epsilon() * 10.) {
55  Eigen::Matrix2cd exp2 = Eigen::Matrix2cd(
56  Eigen::DiagonalMatrix<complex_t, 2>({std::exp(beta_pp), std::exp(-beta_pp)}));
57  return Eigen::Matrix2cd{exp1 * QL * exp2 * QR};
58  }
59 
60  return exp1;
61  };
62 
63  const Eigen::Matrix2cd roughness_sum = roughness_matrix(1.);
64  const Eigen::Matrix2cd roughness_diff = roughness_matrix(-1.);
65 
66  return {roughness_sum, roughness_diff};
67 }
68 
69 std::pair<Eigen::Matrix2cd, Eigen::Matrix2cd>
71  const MatrixRTCoefficients_v3& coeff_i1,
72  double sigma) const
73 {
74  Eigen::Matrix2cd roughness_sum{Eigen::Matrix2cd::Identity()};
75  Eigen::Matrix2cd roughness_diff{Eigen::Matrix2cd::Identity()};
76  if (sigma != 0.) {
77  const auto ret = computeRoughnessMatrices(coeff_i, coeff_i1, sigma);
78  roughness_sum = std::get<0>(ret);
79  roughness_diff = std::get<1>(ret);
80  }
81 
82  const auto P = Eigen::Matrix2cd(coeff_i.computeInverseP() * coeff_i1.computeP());
83  const auto mp = 0.5 * (Eigen::Matrix2cd::Identity() + P) * roughness_diff;
84  const auto mm = 0.5 * (Eigen::Matrix2cd::Identity() - P) * roughness_sum;
85 
86  return {mp, mm};
87 }
88 
89 namespace
90 {
92 {
93  return std::abs(val.imag()) < 1e-80 && val.real() < 0 ? complex_t(val.real(), 1e-40) : val;
94 }
95 } // namespace
constexpr complex_t I
Definition: Complex.h:21
std::complex< double > complex_t
Definition: Complex.h:20
Defines class SpecularMagneticNewNCStrategy.
T x() const
Returns x-component in cartesian coordinate system.
Definition: BasicVector3D.h:64
Specular reflection and transmission coefficients in a layer in case of magnetic interactions between...
Eigen::Vector2cd m_lambda
wave propagation direction (-1 for direct one, 1 for time reverse)
Eigen::Matrix2cd computeInverseP() const
Eigen::Matrix2cd computeP() const
kvector_t m_b
unit magnetic field vector
std::pair< Eigen::Matrix2cd, Eigen::Matrix2cd > computeRoughnessMatrices(const MatrixRTCoefficients_v3 &coeff_i, const MatrixRTCoefficients_v3 &coeff_i1, double sigma) const
virtual std::pair< Eigen::Matrix2cd, Eigen::Matrix2cd > computeBackwardsSubmatrices(const MatrixRTCoefficients_v3 &coeff_i, const MatrixRTCoefficients_v3 &coeff_i1, double sigma) const