refloxide_core/layer.rs
1//! Slab descriptions and the packing of dispersion tensors into kernel records.
2//!
3//! Owns the plain-old-data types shared by every solver backend ([`Layer`],
4//! [`LayerCoeffs`]) and the wavelength conversion. It does not solve
5//! transfer chains or validate whole stacks; see [`crate::validate`] and the
6//! `refloxide-tmm` crate.
7
8use nalgebra::Matrix3;
9use num_complex::Complex;
10
11use crate::real::Real;
12
13/// Complex alias used throughout the kernel.
14type C = Complex<f64>;
15
16/// Photon energy to wavelength conversion constant in `eV * Angstrom`.
17pub const HC_EV_ANGSTROM: f64 = 12398.4193;
18
19/// One slab in a stratified medium.
20///
21/// `thickness` and `sigma` are in Angstroms. `sld_re` and `sld_im` carry the
22/// Henke-style `delta` and `beta` for the isotropic legacy path; the per-axis
23/// dispersion comes in through the `uniaxial_reflectivity` `tensor`
24/// argument.
25#[derive(Debug, Clone, Copy)]
26pub struct Layer {
27 /// Slab thickness in Angstroms. Ignored for the fronting and backing rows.
28 pub thickness: f64,
29 /// Real part of the isotropic SLD in `1e-6 * Angstrom^-2`.
30 pub sld_re: f64,
31 /// Imaginary part of the isotropic SLD in `1e-6 * Angstrom^-2`.
32 pub sld_im: f64,
33 /// Nevot-Croce roughness sigma in Angstroms.
34 pub sigma: f64,
35}
36
37impl Layer {
38 /// Builds a layer from its four scalar parameters.
39 pub fn new(thickness: f64, sld_re: f64, sld_im: f64, sigma: f64) -> Self {
40 Self {
41 thickness,
42 sld_re,
43 sld_im,
44 sigma,
45 }
46 }
47}
48
49impl From<[f64; 4]> for Layer {
50 fn from(row: [f64; 4]) -> Self {
51 Self::new(row[0], row[1], row[2], row[3])
52 }
53}
54
55/// Per-slab coefficients consumed by the uniaxial kernel.
56///
57/// This is the device-uploadable layer record: only the ordinary and
58/// extraordinary Berreman components of a uniaxial-z slab enter the
59/// closed-form eigenstructure, so the full 3x3 tensor is not carried.
60/// Components are stored as susceptibilities `chi = eps - 1` rather than
61/// `eps` because soft X-ray `|chi|` is `O(1e-3)`: packing `1 + chi` into a
62/// single-precision word would discard roughly three significant digits of
63/// the optical constants before any arithmetic happens.
64#[derive(Debug, Clone, Copy, PartialEq)]
65pub struct LayerCoeffs<T: Real> {
66 /// Ordinary susceptibility `eps_xx - 1 = eps_yy - 1` (dimensionless).
67 pub chi_o: Complex<T>,
68 /// Extraordinary susceptibility `eps_zz - 1` (dimensionless).
69 pub chi_e: Complex<T>,
70 /// Slab thickness in Angstroms. Ignored for the fronting and backing rows.
71 pub thickness: T,
72 /// Nevot-Croce roughness of the interface above this slab, in Angstroms.
73 pub sigma: T,
74}
75
76/// Vacuum wavenumber `2 pi / lambda` in `1/Angstrom` for a photon energy in eV.
77pub fn wavenumber(energy_ev: f64) -> f64 {
78 2.0 * std::f64::consts::PI / (HC_EV_ANGSTROM / energy_ev)
79}
80
81/// Packs per-slab rows and dispersion tensors into kernel [`LayerCoeffs`].
82///
83/// Reads the `xx` and `zz` tensor entries as the ordinary and extraordinary
84/// axes of a uniaxial-z slab; thickness and roughness come from `layers`.
85pub fn layer_coeffs(layers: &[Layer], tensor: &[Matrix3<C>]) -> Vec<LayerCoeffs<f64>> {
86 layers
87 .iter()
88 .zip(tensor)
89 .map(|(layer, t)| LayerCoeffs {
90 chi_o: berreman_susceptibility(t[(0, 0)]),
91 chi_e: berreman_susceptibility(t[(2, 2)]),
92 thickness: layer.thickness,
93 sigma: layer.sigma,
94 })
95 .collect()
96}
97
98/// Diagonal susceptibility `eps - 1 = conj(-2 t)` of `eps = conj(I - 2 t)`.
99fn berreman_susceptibility(t: C) -> C {
100 (t * C::new(-2.0, 0.0)).conj()
101}