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 Sample/Processed/ProfileHelper.cpp
6 //! @brief Implements class ProfileHelper.
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 
18 
19 namespace {
20 const double prefactor = std::sqrt(2.0 / M_PI);
21 double TransitionTanh(double x)
22 {
23  return (1.0 - std::tanh(prefactor * x)) / 2.0;
24 }
25 double Transition(double x, double sigma)
26 {
27  if (sigma <= 0.0)
28  return x < 0.0 ? 1.0 : 0.0;
29  return TransitionTanh(x / sigma);
30 }
31 } // namespace
32 
34 {
35  auto N = sample.numberOfSlices();
36  m_materialdata.reserve(N);
37  if (N > 1) {
38  m_zlimits.reserve(N - 1);
39  m_sigmas.reserve(N - 1);
40  }
41  auto& slices = sample.averageSlices();
42  for (size_t i = 0; i < N; ++i) {
43  m_materialdata.push_back(slices[i].material().materialData());
44  if (i + 1 < N) {
45  m_zlimits.push_back(sample.sliceBottomZ(i));
46  if (const auto* roughness = sample.bottomRoughness(i)) {
47  m_sigmas.push_back(roughness->getSigma());
48  } else {
49  m_sigmas.push_back(0.0);
50  }
51  }
52  }
53 }
54 
56 
57 // Note: for refractive index materials, the material interpolation actually happens at the level
58 // of n^2. To first order in delta and beta, this implies the same smooth interpolation of delta
59 // and beta, as is done here.
60 std::vector<complex_t> ProfileHelper::calculateProfile(const std::vector<double>& z_values) const
61 {
62  complex_t top_value = m_materialdata.size() ? m_materialdata[0] : 0.0;
63  std::vector<complex_t> result(z_values.size(), top_value);
64  for (size_t i = 0; i < m_zlimits.size(); ++i) {
65  auto sld_diff = m_materialdata[i + 1] - m_materialdata[i];
66  for (size_t j = 0; j < z_values.size(); ++j) {
67  auto arg = (z_values[j] - m_zlimits[i]);
68  auto t = Transition(arg, m_sigmas[i]);
69  result[j] += sld_diff * t;
70  }
71  }
72  return result;
73 }
74 
75 std::pair<double, double> ProfileHelper::defaultLimits() const
76 {
77  if (m_zlimits.size() < 1)
78  return {0.0, 0.0};
79  double interface_span = m_zlimits.front() - m_zlimits.back();
80  double default_margin = interface_span > 0.0 ? interface_span / 20.0 : 10.0;
81  double top_margin = m_sigmas.front() > 0.0 ? 5.0 * m_sigmas.front() : default_margin;
82  double bottom_margin = m_sigmas.back() > 0.0 ? 5.0 * m_sigmas.back() : default_margin;
83  double z_min = m_zlimits.back() - bottom_margin;
84  double z_max = m_zlimits.front() + top_margin;
85  return {z_min, z_max};
86 }
std::complex< double > complex_t
Definition: Complex.h:20
#define M_PI
Definition: Constants.h:44
Defines class LayerRoughness.
Defines class ProcessedSample.
Defines class ProfileHelper.
Data structure that contains all the necessary data for scattering calculations.
double sliceBottomZ(size_t i) const
size_t numberOfSlices() const
const std::vector< Slice > & averageSlices() const
const LayerRoughness * bottomRoughness(size_t i) const
std::vector< complex_t > m_materialdata
Definition: ProfileHelper.h:45
std::pair< double, double > defaultLimits() const
std::vector< complex_t > calculateProfile(const std::vector< double > &z_values) const
std::vector< double > m_sigmas
Definition: ProfileHelper.h:47
ProfileHelper(const ProcessedSample &sample)
std::vector< double > m_zlimits
Definition: ProfileHelper.h:46