BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
MaterialUtils.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Sample/Material/MaterialUtils.cpp
6 //! @brief Implements functions in namespace MaterialUtils
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 
19 using PhysConsts::h_bar;
20 using PhysConsts::m_n;
21 using PhysConsts::mu_N;
22 // The factor 1e-18 is here to have unit: 1/T*nm^-2
23 constexpr double magnetic_prefactor = (m_n * g_factor_n * mu_N / h_bar / h_bar) * 1e-18;
24 
25 // Unit 2x2 matrix
26 const Eigen::Matrix2cd Unit_Matrix(Eigen::Matrix2cd::Identity());
27 
28 namespace
29 {
30 // Pauli matrices
31 const Eigen::Matrix2cd Pauli_X((Eigen::Matrix2cd() << 0, 1, 1, 0).finished());
32 const Eigen::Matrix2cd Pauli_Y((Eigen::Matrix2cd() << 0, -I, I, 0).finished());
33 const Eigen::Matrix2cd Pauli_Z((Eigen::Matrix2cd() << 1, 0, 0, -1).finished());
34 } // namespace
35 
36 template <typename T>
38  double magnetic_factor,
39  BasicVector3D<T> polarization)
40 {
41  Eigen::Matrix2cd result =
42  unit_factor * Unit_Matrix
43  + magnetic_factor
44  * (Pauli_X * polarization[0] + Pauli_Y * polarization[1] + Pauli_Z * polarization[2]);
45  return result;
46 }
47 
48 // Prompt compilation for real and complex vectors:
49 template Eigen::Matrix2cd MaterialUtils::MagnetizationCorrection(complex_t unit_factor,
50  double magnetic_factor,
51  kvector_t polarization);
52 template Eigen::Matrix2cd MaterialUtils::MagnetizationCorrection(complex_t unit_factor,
53  double magnetic_factor,
54  cvector_t polarization);
55 
57 {
58  return n * n - n_ref * n_ref * k.sin2Theta();
59 }
60 
62  kvector_t k, double n_ref)
63 {
64  Eigen::Matrix2cd result;
65  double factor = magnetic_prefactor / k.mag2();
66  complex_t unit_factor = ScalarReducedPotential(n, k, n_ref);
67  return MagnetizationCorrection(unit_factor, factor, b_field);
68 }
69 
70 MATERIAL_TYPES MaterialUtils::checkMaterialTypes(const std::vector<const Material*>& materials)
71 {
73  bool isDefault = true;
74  for (const Material* mat : materials) {
75  if (isDefault) {
76  result = mat->typeID();
77  isDefault = mat->isDefaultMaterial();
78  continue;
79  }
80  if (mat->typeID() != result && !mat->isDefaultMaterial())
82  }
83  return result;
84 }
MATERIAL_TYPES
constexpr complex_t I
Definition: Complex.h:21
std::complex< double > complex_t
Definition: Complex.h:20
const Eigen::Matrix2cd Unit_Matrix(Eigen::Matrix2cd::Identity())
constexpr double magnetic_prefactor
Declares functions in namespace MaterialUtils.
Defines the values of physical constants (SI)
Forked from CLHEP/Geometry by E.
Definition: BasicVector3D.h:28
double mag2() const
Returns magnitude squared of the vector.
double sin2Theta() const
Returns squared sine of polar angle.
A wrapper for underlying material implementation.
Definition: Material.h:29
MATERIAL_TYPES checkMaterialTypes(const std::vector< const Material * > &materials)
Checks if all non-default materials in materials are of the same type and returns this type.
complex_t ScalarReducedPotential(complex_t n, kvector_t k, double n_ref)
Function for calculating the reduced potential, used for obtaining the Fresnel coefficients (non-pola...
Eigen::Matrix2cd PolarizedReducedPotential(complex_t n, kvector_t b_field, kvector_t k, double n_ref)
Function for calculating the reduced potential, used for obtaining the Fresnel coefficients (polarize...
Eigen::Matrix2cd MagnetizationCorrection(complex_t unit_factor, double magnetic_factor, BasicVector3D< T > polarization)
constexpr double g_factor_n
neutron g-factor
constexpr double h_bar
Reduced Plank constant, J s.
constexpr double m_n
Neutron mass, kg.
constexpr double mu_N
Nuclear magneton ( ), J/T.
const Eigen::Matrix2cd Pauli_X((Eigen::Matrix2cd()<< 0, 1, 1, 0).finished())
const Eigen::Matrix2cd Pauli_Z((Eigen::Matrix2cd()<< 1, 0, 0, -1).finished())
const Eigen::Matrix2cd Pauli_Y((Eigen::Matrix2cd()<< 0, -I, I, 0).finished())