Skip to main content

refloxide_optics/
optics.rs

1//! Energy-dependent optical constants and laboratory-frame tensor assembly.
2//!
3//! Supports deferred evaluation: tabulated OOC curves are interpolated at
4//! query energy, then combined with orientation profiles to form diagonal
5//! dielectric tensors used by the uniaxial transfer-matrix kernel.
6
7use num_complex::Complex;
8
9type C = Complex<f64>;
10
11/// Piecewise-linear interpolation on a sorted energy grid.
12///
13/// Returns `[n_xx, n_ixx, n_zz, n_izz]` at `query_ev`. Out-of-range queries
14/// clamp to the grid endpoints.
15pub fn interpolate_ooc_linear(
16    energy_ev: &[f64],
17    n_xx: &[f64],
18    n_ixx: &[f64],
19    n_zz: &[f64],
20    n_izz: &[f64],
21    query_ev: f64,
22) -> [f64; 4] {
23    let n = energy_ev.len();
24    if n == 0 {
25        return [0.0; 4];
26    }
27    if n == 1 {
28        return [n_xx[0], n_ixx[0], n_zz[0], n_izz[0]];
29    }
30    if query_ev <= energy_ev[0] {
31        return [n_xx[0], n_ixx[0], n_zz[0], n_izz[0]];
32    }
33    if query_ev >= energy_ev[n - 1] {
34        return [n_xx[n - 1], n_ixx[n - 1], n_zz[n - 1], n_izz[n - 1]];
35    }
36    let mut hi = 1usize;
37    while hi < n && energy_ev[hi] < query_ev {
38        hi += 1;
39    }
40    let lo = hi - 1;
41    let t = (query_ev - energy_ev[lo]) / (energy_ev[hi] - energy_ev[lo]);
42    [
43        n_xx[lo] + t * (n_xx[hi] - n_xx[lo]),
44        n_ixx[lo] + t * (n_ixx[hi] - n_ixx[lo]),
45        n_zz[lo] + t * (n_zz[hi] - n_zz[lo]),
46        n_izz[lo] + t * (n_izz[hi] - n_izz[lo]),
47    ]
48}
49
50/// Builds laboratory-frame diagonal `(n_o, n_o, n_e)` from uniaxial molecular axes.
51///
52/// `n_mol_xx` and `n_mol_zz` are complex index values along principal axes;
53/// `orientation_rad` is the polar rotation in radians.
54pub fn lab_diagonal_uniaxial(n_mol_xx: C, n_mol_zz: C, orientation_rad: f64) -> [C; 3] {
55    let c = orientation_rad.cos();
56    let cos2 = c * c;
57    let sin2 = 1.0 - cos2;
58    let n_o = (n_mol_xx * (1.0 + cos2) + n_mol_zz * sin2) / 2.0;
59    let n_e = n_mol_xx * sin2 + n_mol_zz * cos2;
60    [n_o, n_o, n_e]
61}
62
63/// Batch laboratory tensors as length-`n` vectors of diagonal `(n_o, n_o, n_e)`.
64pub fn lab_diagonal_uniaxial_batch(
65    n_mol_xx: C,
66    n_mol_zz: C,
67    orientations_rad: &[f64],
68) -> Vec<[C; 3]> {
69    orientations_rad
70        .iter()
71        .map(|&theta| lab_diagonal_uniaxial(n_mol_xx, n_mol_zz, theta))
72        .collect()
73}
74
75/// Packs diagonal laboratory indices into `(3, 3)` tensors with off-diagonals zero.
76pub fn pack_diagonal_tensors(diagonals: &[[C; 3]]) -> Vec<[[C; 3]; 3]> {
77    diagonals
78        .iter()
79        .map(|[n_o, _, n_e]| {
80            [
81                [*n_o, C::new(0.0, 0.0), C::new(0.0, 0.0)],
82                [C::new(0.0, 0.0), *n_o, C::new(0.0, 0.0)],
83                [C::new(0.0, 0.0), C::new(0.0, 0.0), *n_e],
84            ]
85        })
86        .collect()
87}
88
89/// Isotropic `(3, 3)` tensor from a scalar laboratory index `n`.
90pub fn isotropic_tensor(n: C) -> [[C; 3]; 3] {
91    [
92        [n, C::new(0.0, 0.0), C::new(0.0, 0.0)],
93        [C::new(0.0, 0.0), n, C::new(0.0, 0.0)],
94        [C::new(0.0, 0.0), C::new(0.0, 0.0), n],
95    ]
96}
97
98#[cfg(test)]
99mod tests {
100    use super::*;
101    use approx::assert_relative_eq;
102
103    #[test]
104    fn ooc_linear_interp_midpoint() {
105        let e = [250.0, 300.0];
106        let xx = [1.0, 3.0];
107        let ix = [0.0, 0.0];
108        let zz = [2.0, 4.0];
109        let iz = [0.0, 0.0];
110        let v = interpolate_ooc_linear(&e, &xx, &ix, &zz, &iz, 275.0);
111        assert_relative_eq!(v[0], 2.0);
112        assert_relative_eq!(v[2], 3.0);
113    }
114}