Skip to main content

refloxide_optics/
sld.rs

1//! Composable SLD primitives for deferred-energy uniaxial stacks.
2//!
3//! Thin single-point wrappers over [`crate::optics`]: density scaling,
4//! laboratory-frame rotation, and refnx slab-row packing. Batch OOC
5//! interpolation and diagonal packing stay in [`crate::optics`]; this
6//! module does not reimplement them.
7
8use num_complex::Complex;
9
10use crate::optics::{interpolate_ooc_linear, isotropic_tensor, lab_diagonal_uniaxial};
11
12type C = Complex<f64>;
13
14/// Density-scaled uniaxial molecular indices from tabulated OOC components.
15pub fn molecular_index(density: f64, n_xx: f64, n_ixx: f64, n_zz: f64, n_izz: f64) -> (C, C) {
16    (density * C::new(n_xx, n_ixx), density * C::new(n_zz, n_izz))
17}
18
19/// Linear OOC lookup followed by :func:`molecular_index` at ``query_ev``.
20pub fn molecular_index_at_ooc(
21    energy_ev: &[f64],
22    n_xx: &[f64],
23    n_ixx: &[f64],
24    n_zz: &[f64],
25    n_izz: &[f64],
26    query_ev: f64,
27    density: f64,
28) -> (C, C) {
29    let v = interpolate_ooc_linear(energy_ev, n_xx, n_ixx, n_zz, n_izz, query_ev);
30    molecular_index(density, v[0], v[1], v[2], v[3])
31}
32
33/// Laboratory ``(3, 3)`` tensor for one uniaxial orientation (radians).
34pub fn uniaxial_lab_tensor(n_mol_xx: C, n_mol_zz: C, orientation_rad: f64) -> [[C; 3]; 3] {
35    let [n_o, _, n_e] = lab_diagonal_uniaxial(n_mol_xx, n_mol_zz, orientation_rad);
36    [
37        [n_o, C::new(0.0, 0.0), C::new(0.0, 0.0)],
38        [C::new(0.0, 0.0), n_o, C::new(0.0, 0.0)],
39        [C::new(0.0, 0.0), C::new(0.0, 0.0), n_e],
40    ]
41}
42
43/// Isotropic laboratory ``(3, 3)`` tensor from scalar index ``n``.
44pub fn isotropic_lab_tensor(n: C) -> [[C; 3]; 3] {
45    isotropic_tensor(n)
46}
47
48/// Pack ``[thickness, delta, beta, roughness]`` from a diagonal laboratory tensor.
49///
50/// The tensor already holds ``delta + i*beta`` directly (the crate-wide
51/// convention: `berreman_dielectric` builds `eps = conj(I - 2*tensor)` from
52/// it, and `uniaxial_lab_tensor`/`molecular_index_at_ooc` produce it straight
53/// from density-scaled OOC delta/beta values). The mean diagonal index is
54/// therefore the packed `(delta, beta)` as-is — no `1 - n_avg` correction.
55pub fn tensor_to_slab_row(thickness: f64, roughness: f64, tensor: &[[C; 3]; 3]) -> [f64; 4] {
56    let n_avg = (tensor[0][0] + tensor[1][1] + tensor[2][2]) / 3.0;
57    [thickness, n_avg.re, n_avg.im, roughness]
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63    use approx::assert_relative_eq;
64
65    #[test]
66    fn molecular_index_scales_density() {
67        let (xx, zz) = molecular_index(2.0, 0.01, 0.001, 0.02, 0.002);
68        assert_relative_eq!(xx.re, 0.02);
69        assert_relative_eq!(zz.re, 0.04);
70    }
71
72    #[test]
73    fn tensor_to_slab_row_treats_tensor_as_delta_beta_directly() {
74        // delta/beta at real-material scale (~1e-6), not a refractive index near 1 —
75        // this is the actual convention every caller (uniaxial_lab_tensor,
76        // molecular_index_at_ooc) produces.
77        let delta = 5.0e-6;
78        let beta = 1.0e-7;
79        let tensor = isotropic_lab_tensor(C::new(delta, beta));
80        let row = tensor_to_slab_row(100.0, 2.0, &tensor);
81        assert_relative_eq!(row[0], 100.0);
82        assert_relative_eq!(row[1], delta);
83        assert_relative_eq!(row[2], beta);
84        assert_relative_eq!(row[3], 2.0);
85    }
86
87    #[test]
88    fn tensor_to_slab_row_accepts_delta_beta_tensor_from_ooc_pipeline() {
89        // exercises the real call path: OOC lookup + density scaling ->
90        // uniaxial_lab_tensor -> tensor_to_slab_row, confirming delta/beta
91        // survive the full pipeline at the right order of magnitude.
92        let (n_mol_xx, n_mol_zz) = molecular_index_at_ooc(
93            &[500.0, 700.0],
94            &[4.0e-6, 4.0e-6],
95            &[1.0e-7, 1.0e-7],
96            &[8.0e-6, 8.0e-6],
97            &[2.0e-7, 2.0e-7],
98            700.0,
99            1.0,
100        );
101        let tensor = uniaxial_lab_tensor(n_mol_xx, n_mol_zz, 0.0);
102        let row = tensor_to_slab_row(150.0, 3.0, &tensor);
103        assert!(
104            row[1].abs() < 1e-3,
105            "delta={} looks like it went through `1 - n`, not delta+i*beta directly",
106            row[1]
107        );
108        assert!(
109            row[1] > 0.0,
110            "expected a positive delta on the order of 1e-6, got {}",
111            row[1]
112        );
113    }
114}