BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
MatrixFresnelMap.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Sample/Fresnel/MatrixFresnelMap.cpp
6 //! @brief Implements class MatrixFresnelMap.
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 
17 #include "Sample/Slice/Slice.h"
19 #include <functional>
20 
21 MatrixFresnelMap::MatrixFresnelMap(std::unique_ptr<ISpecularStrategy> strategy)
22  : IFresnelMap(std::move(strategy)){};
23 
25 
26 //! Returns hash value of a 3-vector, computed by exclusive-or of the component hash values.
27 size_t MatrixFresnelMap::HashKVector::operator()(const kvector_t& kvec) const noexcept
28 {
29  return std::hash<double>{}(kvec.x()) ^ std::hash<double>{}(kvec.y())
30  ^ std::hash<double>{}(kvec.z());
31 }
32 
33 std::unique_ptr<const ILayerRTCoefficients>
34 MatrixFresnelMap::getOutCoefficients(const SimulationElement& sim_element, size_t layer_index) const
35 {
36  return getCoefficients(-sim_element.getMeanKf(), layer_index, m_inverted_slices,
38 }
39 
40 void MatrixFresnelMap::setSlices(const std::vector<Slice>& slices)
41 {
43  m_inverted_slices.clear();
44  for (auto slice : slices) {
45  slice.invertBField();
46  m_inverted_slices.push_back(slice);
47  }
48 }
49 
50 std::unique_ptr<const ILayerRTCoefficients>
51 MatrixFresnelMap::getCoefficients(const kvector_t& kvec, size_t layer_index) const
52 {
53  return getCoefficients(kvec, layer_index, m_slices, m_hash_table_in);
54 }
55 
56 std::unique_ptr<const ILayerRTCoefficients>
57 MatrixFresnelMap::getCoefficients(const kvector_t& kvec, size_t layer_index,
58  const std::vector<Slice>& slices,
59  CoefficientHash& hash_table) const
60 {
61  if (!m_use_cache) {
62  auto coeffs = m_Strategy->Execute(slices, kvec);
63  return std::unique_ptr<const ILayerRTCoefficients>(coeffs[layer_index]->clone());
64  }
65  const auto& coef_vector = getCoefficientsFromCache(kvec, slices, hash_table);
66  return std::unique_ptr<const ILayerRTCoefficients>(coef_vector[layer_index]->clone());
67 }
68 
70 MatrixFresnelMap::getCoefficientsFromCache(kvector_t kvec, const std::vector<Slice>& slices,
71  MatrixFresnelMap::CoefficientHash& hash_table) const
72 {
73  auto it = hash_table.find(kvec);
74  if (it == hash_table.end())
75  it = hash_table.emplace(kvec, m_Strategy->Execute(slices, kvec)).first;
76  return it->second;
77 }
Defines class MatrixFresnelMap.
Defines class SimulationElement.
Defines class Slice.
Defines class SpecularMagneticOldStrategy.
Holds the necessary information to calculate the radiation wavefunction in every layer for different ...
Definition: IFresnelMap.h:30
std::vector< Slice > m_slices
Definition: IFresnelMap.h:57
virtual void setSlices(const std::vector< Slice > &slices)
Definition: IFresnelMap.cpp:23
std::unique_ptr< ISpecularStrategy > m_Strategy
Definition: IFresnelMap.h:60
const std::vector< Slice > & slices() const
Definition: IFresnelMap.cpp:28
bool m_use_cache
Definition: IFresnelMap.h:58
std::vector< std::unique_ptr< const ILayerRTCoefficients > > coeffs_t
size_t operator()(const kvector_t &kvec) const noexcept
Returns hash value of a 3-vector, computed by exclusive-or of the component hash values.
std::vector< Slice > m_inverted_slices
CoefficientHash m_hash_table_in
std::unordered_map< kvector_t, ISpecularStrategy::coeffs_t, HashKVector > CoefficientHash
std::unique_ptr< const ILayerRTCoefficients > getCoefficients(const kvector_t &kvec, size_t layer_index) const override
void setSlices(const std::vector< Slice > &slices) final override
const ISpecularStrategy::coeffs_t & getCoefficientsFromCache(kvector_t kvec, const std::vector< Slice > &slices, CoefficientHash &hash_table) const
std::unique_ptr< const ILayerRTCoefficients > getOutCoefficients(const SimulationElement &sim_element, size_t layer_index) const final override
Retrieves the amplitude coefficients for a (time-reversed) outgoing wavevector.
MatrixFresnelMap(std::unique_ptr< ISpecularStrategy > strategy)
CoefficientHash m_hash_table_out
~MatrixFresnelMap() override
Data stucture containing both input and output of a single detector cell.
kvector_t getMeanKf() const