BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
SpecularMagneticNCStrategy Class Reference

Implements the magnetic Fresnel computation with Nevot-Croce roughness. More...

Inheritance diagram for SpecularMagneticNCStrategy:
[legend]
Collaboration diagram for SpecularMagneticNCStrategy:
[legend]

Public Types

using coefficient_entry_type = Eigen::Matrix2cd
 
using coefficient_pointer_type = std::unique_ptr< const coefficient_type >
 
using coefficient_type = MatrixRTCoefficients
 
using coeffs_t = std::vector< coefficient_pointer_type >
 

Public Member Functions

virtual std::variant< complex_t, Eigen::Matrix2cd > computeTopLayerR (const std::vector< Slice > &slices, const std::vector< complex_t > &kzs) const override
 Computes the Fresnel R coefficient for the top layer only Introduced in order to speed up pure reflectivity computations. More...
 
ISpecularStrategy::coeffs_t Execute (const std::vector< Slice > &slices, const kvector_t &k) const
 Computes refraction angle reflection/transmission coefficients for given sliced multilayer and wavevector k. More...
 
ISpecularStrategy::coeffs_t Execute (const std::vector< Slice > &slices, const std::vector< complex_t > &kz) const
 Computes refraction angle reflection/transmission coefficients for given sliced multilayer and a set of kz projections corresponding to each slice. More...
 

Private Member Functions

void calculateUpwards (std::vector< MatrixRTCoefficients > &coeff, const std::vector< Slice > &slices) const
 
virtual std::pair< Eigen::Matrix2cd, Eigen::Matrix2cd > computeBackwardsSubmatrices (const MatrixRTCoefficients &coeff_i, const MatrixRTCoefficients &coeff_i1, double sigma) const
 
std::pair< Eigen::Matrix2cd, Eigen::Matrix2cd > computeRoughnessMatrices (const MatrixRTCoefficients &coeff_i, const MatrixRTCoefficients &coeff_i1, double sigma) const
 
std::vector< MatrixRTCoefficientscomputeTR (const std::vector< Slice > &slices, const std::vector< complex_t > &kzs) const
 

Detailed Description

Implements the magnetic Fresnel computation with Nevot-Croce roughness.

Implements the transfer matrix formalism for the calculation of wave amplitudes of the coherent wave solution in a multilayer with magnetization. For a description, see internal document "Polarized Implementation of the Transfer Matrix Method"

Definition at line 35 of file SpecularMagneticNCStrategy.h.

Member Typedef Documentation

◆ coefficient_entry_type

using SpecularMagneticStrategy::coefficient_entry_type = Eigen::Matrix2cd
inherited

Definition at line 42 of file SpecularMagneticStrategy.h.

◆ coefficient_pointer_type

using SpecularMagneticStrategy::coefficient_pointer_type = std::unique_ptr<const coefficient_type>
inherited

Definition at line 44 of file SpecularMagneticStrategy.h.

◆ coefficient_type

◆ coeffs_t

Definition at line 45 of file SpecularMagneticStrategy.h.

Member Function Documentation

◆ calculateUpwards()

void SpecularMagneticStrategy::calculateUpwards ( std::vector< MatrixRTCoefficients > &  coeff,
const std::vector< Slice > &  slices 
) const
privateinherited

Definition at line 156 of file SpecularMagneticStrategy.cpp.

158 {
159  const auto N = slices.size();
160  std::vector<Eigen::Matrix2cd> SMatrices(N - 1);
161  std::vector<complex_t> Normalization(N - 1);
162 
163  // bottom boundary condition
164  coeff.back().m_T = Eigen::Matrix2cd::Identity();
165  coeff.back().m_R = Eigen::Matrix2cd::Zero();
166 
167  for (int i = N - 2; i >= 0; --i) {
168  double sigma = 0.;
169  if (const auto roughness = GetBottomRoughness(slices, i))
170  sigma = roughness->getSigma();
171 
172  // compute the 2x2 submatrices in the write-up denoted as P+, P- and delta
173  const auto [mp, mm] = computeBackwardsSubmatrices(coeff[i], coeff[i + 1], sigma);
174 
175  const Eigen::Matrix2cd delta = coeff[i].computeDeltaMatrix(slices[i].thickness());
176 
177  // compute the rotation matrix
178  Eigen::Matrix2cd S, Si;
179  Si = mp + mm * coeff[i + 1].m_R;
180  S << Si(1, 1), -Si(0, 1), -Si(1, 0), Si(0, 0);
181  const complex_t norm = S(0, 0) * S(1, 1) - S(0, 1) * S(1, 0);
182  S = S * delta;
183 
184  // store the rotation matrix and normalization constant in order to rotate
185  // the coefficients for all lower slices at the end of the computation
186  SMatrices[i] = S;
187  Normalization[i] = norm;
188 
189  // compute the reflection matrix and
190  // rotate the polarization such that we have pure incoming states (T = I)
191  S /= norm;
192 
193  // T is always equal to the identity at this point, no need to store
194  coeff[i].m_R = delta * (mm + mp * coeff[i + 1].m_R) * S;
195  }
196 
197  // now correct all amplitudes in forward direction by dividing with the remaining
198  // normalization constants. In addition rotate the polarization by the amount
199  // that was rotated above the current interface
200  // if the normalization overflows, all amplitudes below that point are set to zero
201  complex_t dumpingFactor = 1;
202  Eigen::Matrix2cd S = Eigen::Matrix2cd::Identity();
203  for (size_t i = 1; i < N; ++i) {
204  dumpingFactor = dumpingFactor * Normalization[i - 1];
205  S = SMatrices[i - 1] * S;
206 
207  if (std::isinf(std::norm(dumpingFactor))) {
208  std::for_each(coeff.begin() + i, coeff.end(), [](auto& coeff) {
209  coeff.m_T = Eigen::Matrix2cd::Zero();
210  coeff.m_R = Eigen::Matrix2cd::Zero();
211  });
212  break;
213  }
214 
215  coeff[i].m_T = S / dumpingFactor; // T * S omitted, since T is always I
216  coeff[i].m_R *= S / dumpingFactor;
217  }
218 }
std::complex< double > complex_t
Definition: Complex.h:20
virtual std::pair< Eigen::Matrix2cd, Eigen::Matrix2cd > computeBackwardsSubmatrices(const MatrixRTCoefficients &coeff_i, const MatrixRTCoefficients &coeff_i1, double sigma) const =0

References SpecularMagneticStrategy::computeBackwardsSubmatrices().

Referenced by SpecularMagneticStrategy::computeTR().

Here is the call graph for this function:

◆ computeBackwardsSubmatrices()

std::pair< Eigen::Matrix2cd, Eigen::Matrix2cd > SpecularMagneticNCStrategy::computeBackwardsSubmatrices ( const MatrixRTCoefficients coeff_i,
const MatrixRTCoefficients coeff_i1,
double  sigma 
) const
privatevirtual

Implements SpecularMagneticStrategy.

Definition at line 67 of file SpecularMagneticNCStrategy.cpp.

70 {
71  Eigen::Matrix2cd roughness_sum{Eigen::Matrix2cd::Identity()};
72  Eigen::Matrix2cd roughness_diff{Eigen::Matrix2cd::Identity()};
73  if (sigma != 0.) {
74  const auto ret = computeRoughnessMatrices(coeff_i, coeff_i1, sigma);
75  roughness_sum = std::get<0>(ret);
76  roughness_diff = std::get<1>(ret);
77  }
78 
79  const auto P = Eigen::Matrix2cd(coeff_i.computeInverseP() * coeff_i1.computeP());
80  const auto mp = 0.5 * (Eigen::Matrix2cd::Identity() + P) * roughness_diff;
81  const auto mm = 0.5 * (Eigen::Matrix2cd::Identity() - P) * roughness_sum;
82 
83  return {mp, mm};
84 }
Eigen::Matrix2cd computeInverseP() const
Eigen::Matrix2cd computeP() const
std::pair< Eigen::Matrix2cd, Eigen::Matrix2cd > computeRoughnessMatrices(const MatrixRTCoefficients &coeff_i, const MatrixRTCoefficients &coeff_i1, double sigma) const

References MatrixRTCoefficients::computeInverseP(), MatrixRTCoefficients::computeP(), and computeRoughnessMatrices().

Here is the call graph for this function:

◆ computeRoughnessMatrices()

std::pair< Eigen::Matrix2cd, Eigen::Matrix2cd > SpecularMagneticNCStrategy::computeRoughnessMatrices ( const MatrixRTCoefficients coeff_i,
const MatrixRTCoefficients coeff_i1,
double  sigma 
) const
private

Definition at line 21 of file SpecularMagneticNCStrategy.cpp.

23 {
24  complex_t beta_i = coeff_i.m_lambda(1) - coeff_i.m_lambda(0);
25  complex_t beta_i1 = coeff_i1.m_lambda(1) - coeff_i1.m_lambda(0);
26 
27  auto roughness_matrix = [sigma, &coeff_i, &coeff_i1, beta_i, beta_i1](double sign) {
28  const complex_t alpha_p = coeff_i1.m_lambda(0) + coeff_i1.m_lambda(1)
29  + sign * (coeff_i.m_lambda(0) + coeff_i.m_lambda(1));
30  auto b_p_vec = beta_i1 * coeff_i1.m_b + sign * beta_i * coeff_i.m_b;
31 
32  auto square = [](auto& v) { return v.x() * v.x() + v.y() * v.y() + v.z() * v.z(); };
33  complex_t beta_p = std::sqrt(checkForUnderflow(square(b_p_vec)));
34  b_p_vec /= beta_p;
35 
36  const complex_t alpha_pp = -(alpha_p * alpha_p + beta_p * beta_p) * sigma * sigma / 8.;
37  const complex_t beta_pp = -alpha_p * beta_p * sigma * sigma / 4.;
38 
39  Eigen::Matrix2cd QL, QR;
40 
41  const complex_t factor1 = std::sqrt(2. * (1. + b_p_vec.z()));
42  const complex_t factor2 = std::sqrt(2. * (1. - b_p_vec.z()));
43  QL << (b_p_vec.z() + 1.) / factor1, (b_p_vec.z() - 1.) / factor2,
44  (b_p_vec.x() + I * b_p_vec.y()) / factor1, (b_p_vec.x() + I * b_p_vec.y()) / factor2;
45  QR << (b_p_vec.z() + 1.) / factor1, (b_p_vec.x() - I * b_p_vec.y()) / factor1,
46  (b_p_vec.z() - 1.) / factor2, (b_p_vec.x() - I * b_p_vec.y()) / factor2;
47 
48  const Eigen::Matrix2cd exp1 =
49  Eigen::DiagonalMatrix<complex_t, 2>({std::exp(alpha_pp), std::exp(alpha_pp)});
50 
51  if (std::abs(beta_p) > std::numeric_limits<double>::epsilon() * 10.) {
52  Eigen::Matrix2cd exp2 = Eigen::Matrix2cd(
53  Eigen::DiagonalMatrix<complex_t, 2>({std::exp(beta_pp), std::exp(-beta_pp)}));
54  return Eigen::Matrix2cd{exp1 * QL * exp2 * QR};
55  }
56 
57  return exp1;
58  };
59 
60  const Eigen::Matrix2cd roughness_sum = roughness_matrix(1.);
61  const Eigen::Matrix2cd roughness_diff = roughness_matrix(-1.);
62 
63  return {roughness_sum, roughness_diff};
64 }
constexpr complex_t I
Definition: Complex.h:21
Eigen::Vector2cd m_lambda
wave propagation direction (-1 for direct one, 1 for time reverse)

References I, MatrixRTCoefficients::m_b, MatrixRTCoefficients::m_lambda, and BasicVector3D< T >::x().

Referenced by computeBackwardsSubmatrices().

Here is the call graph for this function:

◆ computeTopLayerR()

std::variant< complex_t, Eigen::Matrix2cd > SpecularMagneticStrategy::computeTopLayerR ( const std::vector< Slice > &  slices,
const std::vector< complex_t > &  kzs 
) const
overridevirtualinherited

Computes the Fresnel R coefficient for the top layer only Introduced in order to speed up pure reflectivity computations.

Implements ISpecularStrategy.

Definition at line 55 of file SpecularMagneticStrategy.cpp.

57 {
58  if (slices.size() != kzs.size())
59  throw std::runtime_error("Number of slices does not match the size of the kz-vector");
60 
61  const auto N = slices.size();
62 
63  if(N == 1)
64  return Eigen::Matrix2cd::Zero();
65  else if(kzs[0] == 0.)
66  return -Eigen::Matrix2cd::Identity();
67 
68  auto B_0 = slices.front().bField();
69  const double kz_sign = kzs.front().real() >= 0.0 ? 1.0 : -1.0; // save sign to restore it later
70 
71  auto createCoeff = [&slices, &kzs, kz_sign, B_0](int i){
72  const auto B = slices[i].bField() - B_0;
73  const auto magnetic_SLD = magneticSLD(B);
74 
75  return MatrixRTCoefficients(kz_sign, checkForUnderflow(eigenvalues(kzs[i], magnetic_SLD)),
76  B.mag() > eps ? B / B.mag() : kvector_t{0.0, 0.0, 0.0}, magnetic_SLD);
77  };
78 
79  auto c_i1 = createCoeff(N-1);
80 
81  // bottom boundary condition
82  c_i1.m_R = Eigen::Matrix2cd::Zero();
83 
84  for (int i = N - 2; i >= 0; --i) {
85  auto c_i = createCoeff(i);
86 
87  double sigma = 0.;
88  if (const auto roughness = GetBottomRoughness(slices, i))
89  sigma = roughness->getSigma();
90 
91  // compute the 2x2 submatrices in the write-up denoted as P+, P- and delta
92  const auto [mp, mm] = computeBackwardsSubmatrices(c_i, c_i1, sigma);
93 
94  const Eigen::Matrix2cd delta = c_i.computeDeltaMatrix(slices[i].thickness());
95 
96  // compute the rotation matrix
97  Eigen::Matrix2cd S, Si;
98  Si = mp + mm * c_i1.m_R;
99  S << Si(1, 1), -Si(0, 1), -Si(1, 0), Si(0, 0);
100  const complex_t norm = S(0, 0) * S(1, 1) - S(0, 1) * S(1, 0);
101  S = S * delta;
102  S /= norm;
103 
104  c_i.m_R = delta * (mm + mp * c_i1.m_R) * S;
105  c_i1 = c_i;
106  }
107  return c_i1.m_R;
108 }
Specular reflection and transmission coefficients in a layer in case of magnetic interactions between...

References SpecularMagneticStrategy::computeBackwardsSubmatrices().

Here is the call graph for this function:

◆ computeTR()

std::vector< MatrixRTCoefficients > SpecularMagneticStrategy::computeTR ( const std::vector< Slice > &  slices,
const std::vector< complex_t > &  kzs 
) const
privateinherited

Definition at line 111 of file SpecularMagneticStrategy.cpp.

113 {
114  const size_t N = slices.size();
115 
116  if (slices.size() != kzs.size())
117  throw std::runtime_error(
118  "Error in SpecularMagnetic_::execute: kz vector and slices size shall coinside.");
119  if (slices.empty())
120  return {};
121 
122  std::vector<MatrixRTCoefficients> result;
123  result.reserve(N);
124 
125  const double kz_sign = kzs.front().real() >= 0.0 ? 1.0 : -1.0; // save sign to restore it later
126 
127  auto B_0 = slices.front().bField();
128  result.emplace_back(kz_sign, eigenvalues(kzs.front(), 0.0), kvector_t{0.0, 0.0, 0.0}, 0.0);
129  for (size_t i = 1, size = slices.size(); i < size; ++i) {
130  auto B = slices[i].bField() - B_0;
131  auto magnetic_SLD = magneticSLD(B);
132  result.emplace_back(kz_sign, checkForUnderflow(eigenvalues(kzs[i], magnetic_SLD)),
133  B.mag() > eps ? B / B.mag() : kvector_t{0.0, 0.0, 0.0}, magnetic_SLD);
134  }
135 
136  if (N == 1) {
137  result[0].m_T = Eigen::Matrix2cd::Identity();
138  result[0].m_R = Eigen::Matrix2cd::Zero();
139  return result;
140 
141  } else if (kzs[0] == 0.) {
142  result[0].m_T = Eigen::Matrix2cd::Identity();
143  result[0].m_R = -Eigen::Matrix2cd::Identity();
144  for (size_t i = 1; i < N; ++i) {
145  result[i].m_T.setZero();
146  result[i].m_R.setZero();
147  }
148  return result;
149  }
150 
151  calculateUpwards(result, slices);
152 
153  return result;
154 }
void calculateUpwards(std::vector< MatrixRTCoefficients > &coeff, const std::vector< Slice > &slices) const

References SpecularMagneticStrategy::calculateUpwards().

Referenced by SpecularMagneticStrategy::Execute().

Here is the call graph for this function:

◆ Execute() [1/2]

ISpecularStrategy::coeffs_t SpecularMagneticStrategy::Execute ( const std::vector< Slice > &  slices,
const kvector_t k 
) const
virtualinherited

Computes refraction angle reflection/transmission coefficients for given sliced multilayer and wavevector k.

Implements ISpecularStrategy.

Definition at line 34 of file SpecularMagneticStrategy.cpp.

36 {
37  return Execute(slices, KzComputation::computeReducedKz(slices, k));
38 }
ISpecularStrategy::coeffs_t Execute(const std::vector< Slice > &slices, const kvector_t &k) const
Computes refraction angle reflection/transmission coefficients for given sliced multilayer and waveve...
std::vector< complex_t > computeReducedKz(const std::vector< Slice > &slices, kvector_t k)

References KzComputation::computeReducedKz().

Here is the call graph for this function:

◆ Execute() [2/2]

ISpecularStrategy::coeffs_t SpecularMagneticStrategy::Execute ( const std::vector< Slice > &  slices,
const std::vector< complex_t > &  kz 
) const
virtualinherited

Computes refraction angle reflection/transmission coefficients for given sliced multilayer and a set of kz projections corresponding to each slice.

Implements ISpecularStrategy.

Definition at line 41 of file SpecularMagneticStrategy.cpp.

43 {
44  if (slices.size() != kz.size())
45  throw std::runtime_error("Number of slices does not match the size of the kz-vector");
46 
48  for (auto& coeff : computeTR(slices, kz))
49  result.push_back(std::make_unique<MatrixRTCoefficients>(coeff));
50 
51  return result;
52 }
std::vector< std::unique_ptr< const ILayerRTCoefficients > > coeffs_t
std::vector< MatrixRTCoefficients > computeTR(const std::vector< Slice > &slices, const std::vector< complex_t > &kzs) const

References SpecularMagneticStrategy::computeTR().

Here is the call graph for this function:

The documentation for this class was generated from the following files: