BornAgain  1.18.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 scattering at grazing incidence
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 {
25 const LayerRoughness* GetBottomRoughness(const std::vector<Slice>& slices,
26  const size_t slice_index);
27 } // namespace
28 
30  const kvector_t& k) const
31 {
32  std::vector<complex_t> kz = KzComputation::computeReducedKz(slices, k);
33  return Execute(slices, kz);
34 }
35 
37  const std::vector<complex_t>& kz) const
38 {
39  if (slices.size() != kz.size())
40  throw std::runtime_error("Number of slices does not match the size of the kz-vector");
41 
43  for (auto& coeff : computeTR(slices, kz))
44  result.push_back(std::make_unique<ScalarRTCoefficients>(coeff));
45 
46  return result;
47 }
48 
49 std::vector<ScalarRTCoefficients>
50 SpecularScalarStrategy::computeTR(const std::vector<Slice>& slices,
51  const std::vector<complex_t>& kz) const
52 {
53  const size_t N = slices.size();
54  std::vector<ScalarRTCoefficients> coeff(N);
55 
56  for (size_t i = 0; i < N; ++i)
57  coeff[i].kz = kz[i];
58 
59  if (N == 1) { // If only one layer present, there's nothing left to calculate
60  coeff[0].t_r = {1.0, 0.0};
61  return coeff;
62  } else if (kz[0] == 0.0) { // If kz in layer 0 is zero, R0 = -T0 and all others equal to 0
63  coeff[0].t_r = {1.0, -1.0};
64  for (size_t i = 1; i < N; ++i)
65  coeff[i].t_r.setZero();
66  return coeff;
67  }
68 
69  // Calculate transmission/refraction coefficients t_r for each layer, from bottom to top.
70  calculateUpFromLayer(coeff, slices, kz);
71  return coeff;
72 }
73 
74 void SpecularScalarStrategy::setZeroBelow(std::vector<ScalarRTCoefficients>& coeff,
75  size_t current_layer)
76 {
77  size_t N = coeff.size();
78  for (size_t i = current_layer + 1; i < N; ++i) {
79  coeff[i].t_r.setZero();
80  }
81 }
82 
83 void SpecularScalarStrategy::calculateUpFromLayer(std::vector<ScalarRTCoefficients>& coeff,
84  const std::vector<Slice>& slices,
85  const std::vector<complex_t>& kz) const
86 {
87  auto N = slices.size();
88 
89  coeff.back().t_r(0) = 1.0;
90  coeff.back().t_r(1) = 0.0;
91  std::vector<complex_t> factors(N - 1);
92  for (int i = N - 2; i >= 0; i--) {
93  double sigma = 0.0;
94  if (const auto roughness = GetBottomRoughness(slices, i))
95  sigma = roughness->getSigma();
96 
97  const auto mpmm = transition(kz[i], kz[i + 1], sigma);
98  const complex_t mp = mpmm.first;
99  const complex_t mm = mpmm.second;
100 
101  const complex_t delta = exp_I(kz[i] * slices[i].thickness());
102 
103  complex_t S = mp + mm * coeff[i + 1].t_r(1);
104  S = 1. / S * delta;
105  factors[i] = S;
106 
107  coeff[i].t_r(1) = delta * (mm + mp * coeff[i + 1].t_r(1)) * S;
108  }
109 
110  // now correct all amplitudes by dividing the with the remaining factors in forward direction
111  // at some point this divison underflows, which is the point when all further amplitudes are set
112  // to zero
113  complex_t dumpingFactor = 1;
114  for (size_t j = 1; j < N; ++j) {
115  dumpingFactor = dumpingFactor * factors[j - 1];
116 
117  coeff[j].t_r(0) = dumpingFactor;
118  coeff[j].t_r(1) *= dumpingFactor;
119  }
120 }
121 
122 namespace
123 {
124 const LayerRoughness* GetBottomRoughness(const std::vector<Slice>& slices, const size_t slice_index)
125 {
126  if (slice_index + 1 < slices.size())
127  return slices[slice_index + 1].topRoughness();
128  return nullptr;
129 }
130 } // 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
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)
const LayerRoughness * GetBottomRoughness(const std::vector< Slice > &slices, const size_t slice_index)