BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
SpecularScalarStrategy.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Sample/Specular/SpecularScalarStrategy.cpp
6 //! @brief Implements class SpecularScalarStrategy.
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 "Sample/Slice/Slice.h"
20 #include <Eigen/Dense>
21 #include <stdexcept>
22 
23 namespace {
24 const LayerRoughness* GetBottomRoughness(const std::vector<Slice>& slices,
25  const size_t slice_index);
26 } // namespace
27 
29  const kvector_t& k) const
30 {
31  std::vector<complex_t> kz = KzComputation::computeReducedKz(slices, k);
32  return Execute(slices, kz);
33 }
34 
36  const std::vector<complex_t>& kz) const
37 {
38  if (slices.size() != kz.size())
39  throw std::runtime_error("Number of slices does not match the size of the kz-vector");
40 
42  for (auto& coeff : computeTR(slices, kz))
43  result.push_back(std::make_unique<ScalarRTCoefficients>(coeff));
44 
45  return result;
46 }
47 
48 std::variant<complex_t, Eigen::Matrix2cd>
49 SpecularScalarStrategy::computeTopLayerR(const std::vector<Slice>& slices,
50  const std::vector<complex_t>& kz) const
51 {
52  if (slices.size() != kz.size())
53  throw std::runtime_error("Number of slices does not match the size of the kz-vector");
54 
55  auto N = slices.size();
56 
57  if (N == 1) { // If only one layer present, there's nothing left to calculate
58  return 0.;
59  }else if (kz[0] == 0.)
60  return -1.;
61 
62  complex_t R_i1 = 0.;
63 
64  for (int i = N - 2; i >= 0; i--) {
65  double sigma = 0.0;
66  if (const auto roughness = GetBottomRoughness(slices, i))
67  sigma = roughness->getSigma();
68 
69  const auto [mp, mm] = transition(kz[i], kz[i + 1], sigma);
70 
71  const complex_t delta = exp_I(kz[i] * slices[i].thickness());
72 
73  complex_t S = mp + mm * R_i1;
74  S = 1. / S * delta;
75 
76  R_i1 = delta * (mm + mp * R_i1) * S;
77  }
78 
79  return R_i1;
80 }
81 
82 std::vector<ScalarRTCoefficients>
83 SpecularScalarStrategy::computeTR(const std::vector<Slice>& slices,
84  const std::vector<complex_t>& kz) const
85 {
86  const size_t N = slices.size();
87  std::vector<ScalarRTCoefficients> coeff(N);
88 
89  for (size_t i = 0; i < N; ++i)
90  coeff[i].kz = kz[i];
91 
92  if (N == 1) { // If only one layer present, there's nothing left to calculate
93  coeff[0].t_r = {1.0, 0.0};
94  return coeff;
95  } else if (kz[0] == 0.0) { // If kz in layer 0 is zero, R0 = -T0 and all others equal to 0
96  coeff[0].t_r = {1.0, -1.0};
97  for (size_t i = 1; i < N; ++i)
98  coeff[i].t_r.setZero();
99  return coeff;
100  }
101 
102  // Calculate transmission/refraction coefficients t_r for each layer, from bottom to top.
103  calculateUpFromLayer(coeff, slices, kz);
104  return coeff;
105 }
106 
107 void SpecularScalarStrategy::setZeroBelow(std::vector<ScalarRTCoefficients>& coeff,
108  size_t current_layer)
109 {
110  size_t N = coeff.size();
111  for (size_t i = current_layer + 1; i < N; ++i) {
112  coeff[i].t_r.setZero();
113  }
114 }
115 
116 void SpecularScalarStrategy::calculateUpFromLayer(std::vector<ScalarRTCoefficients>& coeff,
117  const std::vector<Slice>& slices,
118  const std::vector<complex_t>& kz) const
119 {
120  auto N = slices.size();
121 
122  coeff.back().t_r(0) = 1.0;
123  coeff.back().t_r(1) = 0.0;
124  std::vector<complex_t> factors(N - 1);
125  for (int i = N - 2; i >= 0; i--) {
126  double sigma = 0.0;
127  if (const auto roughness = GetBottomRoughness(slices, i))
128  sigma = roughness->getSigma();
129 
130  const auto [mp, mm] = transition(kz[i], kz[i + 1], sigma);
131 
132  const complex_t delta = exp_I(kz[i] * slices[i].thickness());
133 
134  complex_t S = mp + mm * coeff[i + 1].t_r(1);
135  S = 1. / S * delta;
136  factors[i] = S;
137 
138  coeff[i].t_r(1) = delta * (mm + mp * coeff[i + 1].t_r(1)) * S;
139  }
140 
141  // now correct all amplitudes by dividing the with the remaining factors in forward direction
142  // at some point this divison underflows, which is the point when all further amplitudes are set
143  // to zero
144  complex_t dumpingFactor = 1;
145  for (size_t j = 1; j < N; ++j) {
146  dumpingFactor = dumpingFactor * factors[j - 1];
147 
148  coeff[j].t_r(0) = dumpingFactor;
149  coeff[j].t_r(1) *= dumpingFactor;
150  }
151 }
152 
153 namespace {
154 const LayerRoughness* GetBottomRoughness(const std::vector<Slice>& slices, const size_t slice_index)
155 {
156  if (slice_index + 1 < slices.size())
157  return slices[slice_index + 1].topRoughness();
158  return nullptr;
159 }
160 } // namespace
std::complex< double > complex_t
Definition: Complex.h:20
complex_t exp_I(complex_t z)
Returns exp(I*z), where I is the imaginary unit.
Definition: Complex.h:30
Declares functions in namespace KzComputation.
Defines class LayerRoughness.
Defines class Layer.
Defines class Slice.
Defines class SpecularScalarStrategy.
std::vector< std::unique_ptr< const ILayerRTCoefficients > > coeffs_t
A roughness of interface between two layers.
virtual ISpecularStrategy::coeffs_t Execute(const std::vector< Slice > &slices, const kvector_t &k) const override
Computes refraction angles and transmission/reflection coefficients for given coherent wave propagati...
std::vector< ScalarRTCoefficients > computeTR(const std::vector< Slice > &slices, const std::vector< complex_t > &kz) const
virtual std::pair< complex_t, complex_t > transition(complex_t kzi, complex_t kzi1, double sigma) const =0
virtual std::variant< complex_t, Eigen::Matrix2cd > computeTopLayerR(const std::vector< Slice > &slices, const std::vector< complex_t > &kz) const override
Computes the Fresnel R coefficient for the top layer only Introduced in order to speed up pure reflec...
void calculateUpFromLayer(std::vector< ScalarRTCoefficients > &coeff, const std::vector< Slice > &slices, const std::vector< complex_t > &kz) const
static void setZeroBelow(std::vector< ScalarRTCoefficients > &coeff, size_t current_layer)
std::vector< complex_t > computeReducedKz(const std::vector< Slice > &slices, kvector_t k)