lensbench_analysis/
pupil_amp.rs1use crate::polgrid::{basis_coeffs, PolGrid};
15use lensbench_core::coating::{Coatings, C64};
16use lensbench_core::pupil::PupilAmp;
17use lensbench_core::system::{FieldSel, Launch, System};
18use lensbench_core::{invalid, Result};
19
20pub const POL_GRID: usize = 17;
22
23#[derive(Debug, Clone)]
25pub struct PolAmp {
26 grids: Vec<Option<PolGrid>>,
28 pol: Option<[C64; 2]>,
30}
31
32impl PolAmp {
33 pub fn new(
36 s: &System,
37 field: FieldSel,
38 waves: &[usize],
39 coat: &Coatings,
40 n: usize,
41 pol: Option<[C64; 2]>,
42 ) -> Result<PolAmp> {
43 if let Some(j) = pol {
44 if !(j[0].norm_sqr() + j[1].norm_sqr() > 0.0) {
45 invalid!("the input polarization (Ex, Ey) must not be zero");
46 }
47 }
48 let mut grids = vec![None; s.wavelengths.len()];
49 for &w in waves {
50 let c = s.try_trace(field, &[0.0], &[0.0], Some(w), None, false)?;
52 let (hx, hy) = c.hx_hy;
53 let l = Launch::Pupil {
54 spec: s.object_spec(hx, hy)?,
55 signed: false,
56 px: &[],
57 py: &[],
58 vig: Some(s.vig_factors(hx, hy)?),
59 hxy: Some((hx, hy)),
60 };
61 grids[w] = Some(PolGrid::trace(s, &l, w, coat, n, n)?);
62 }
63 Ok(PolAmp { grids, pol })
64 }
65}
66
67impl PupilAmp for PolAmp {
68 fn amp(&self, w: usize, px: f64, py: f64) -> f64 {
69 let Some(Some(g)) = self.grids.get(w) else {
70 return f64::NAN;
71 };
72 let Some(v) = g.eval(px, py) else {
73 return f64::NAN;
74 };
75 let (kx, ky) = (v[6].re, v[7].re);
77 let t = |j: [C64; 2]| -> f64 {
78 let c = basis_coeffs(j, kx, ky);
79 let ex = v[0] * c[0] + v[1] * c[1];
80 let ey = v[2] * c[0] + v[3] * c[1];
81 let ez = v[4] * c[0] + v[5] * c[1];
82 (ex.norm_sqr() + ey.norm_sqr() + ez.norm_sqr()) / (j[0].norm_sqr() + j[1].norm_sqr())
83 };
84 let one = C64::new(1.0, 0.0);
85 let zero = C64::new(0.0, 0.0);
86 let tr = match self.pol {
87 Some(j) => t(j),
88 None => 0.5 * (t([one, zero]) + t([zero, one])),
89 };
90 tr.sqrt()
91 }
92}