BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
profilehelper.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/quicksimeditor/profilehelper.cpp
6 //! @brief Implements class CLASS?
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 
17 #include <Sample/Slice/Slice.h>
18 
19 namespace {
20 const double prefactor = std::sqrt(2.0 / M_PI);
21 double Transition(double x, double sigma);
22 double TransitionTanh(double x);
23 } // namespace
24 
25 gui2::ProfileHelper::ProfileHelper(const std::vector<Slice>& sample)
26 {
27  auto N = sample.size();
28  m_materialdata.reserve(N);
29  if (N > 1) {
30  m_zlimits.reserve(N - 1);
31  m_sigmas.reserve(N - 1);
32  }
33  double bottom_z{0};
34  for (size_t i = 0; i < N; ++i) {
35  m_materialdata.push_back(sample[i].material().materialData());
36  bottom_z -= sample[i].thickness();
37  if (i + 1 < N) {
38  m_zlimits.push_back(bottom_z);
39  auto sigma = 0.;
40  if (auto roughness = sample[i + 1].topRoughness())
41  sigma = roughness->getSigma();
42 
43  m_sigmas.push_back(sigma);
44  }
45  }
46 }
47 
48 // Note: for refractive index materials, the material interpolation actually happens at the level
49 // of n^2. To first order in delta and beta, this implies the same smooth interpolation of delta
50 // and beta, as is done here.
51 std::vector<complex_t>
52 gui2::ProfileHelper::calculateProfile(const std::vector<double>& z_values) const
53 {
54  complex_t top_value = m_materialdata.size() ? m_materialdata[0] : 0.0;
55  std::vector<complex_t> result(z_values.size(), top_value);
56  for (size_t i = 0; i < m_zlimits.size(); ++i) {
57  auto sld_diff = m_materialdata[i + 1] - m_materialdata[i];
58  for (size_t j = 0; j < z_values.size(); ++j) {
59  auto arg = (z_values[j] - m_zlimits[i]);
60  auto t = Transition(arg, m_sigmas[i]);
61  result[j] += sld_diff * t;
62  }
63  }
64  return result;
65 }
66 
67 std::pair<double, double> gui2::ProfileHelper::defaultLimits() const
68 {
69  if (m_zlimits.size() < 1)
70  return {0.0, 0.0};
71  double interface_span = m_zlimits.front() - m_zlimits.back();
72  double default_margin = interface_span > 0.0 ? interface_span / 20.0 : 10.0;
73  double top_margin = m_sigmas.front() > 0.0 ? 5.0 * m_sigmas.front() : default_margin;
74  double bottom_margin = m_sigmas.back() > 0.0 ? 5.0 * m_sigmas.back() : default_margin;
75  double z_min = m_zlimits.back() - bottom_margin;
76  double z_max = m_zlimits.front() + top_margin;
77  return {z_min, z_max};
78 }
79 
81 
82 namespace {
83 double Transition(double x, double sigma)
84 {
85  if (sigma <= 0.0)
86  return x < 0.0 ? 1.0 : 0.0;
87  return TransitionTanh(x / sigma);
88 }
89 double TransitionTanh(double x)
90 {
91  return (1.0 - std::tanh(prefactor * x)) / 2.0;
92 }
93 } // namespace
#define M_PI
Definition: Constants.h:44
Defines class LayerRoughness.
Defines class Slice.
std::vector< double > m_sigmas
Definition: profilehelper.h:37
ProfileHelper(const std::vector< Slice > &sample)
std::pair< double, double > defaultLimits() const
std::vector< complex_t > calculateProfile(const std::vector< double > &z_values) const
std::vector< double > m_zlimits
Definition: profilehelper.h:36
std::vector< complex_t > m_materialdata
Definition: profilehelper.h:35
std::complex< double > complex_t
Defines class CLASS?