1use lensbench_core::{Error, Result};
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6
7use super::wave;
8use crate::gauss;
9use crate::registry::{Analysis, Cx, Meta};
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
13#[serde(rename_all = "lowercase")]
14pub enum Orientation {
15 #[default]
17 Y,
18 X,
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
24#[serde(default, deny_unknown_fields)]
25pub(super) struct GaussParams {
26 pub wave: Option<usize>,
28 #[schemars(extend("x-min" = 0.0))]
30 pub waist: f64,
31 pub surf1_to_waist: f64,
33 #[schemars(extend("x-min" = 1.0))]
35 pub m2: f64,
36 pub orientation: Orientation,
38}
39
40impl Default for GaussParams {
41 fn default() -> Self {
42 GaussParams {
43 wave: None,
44 waist: 0.5,
45 surf1_to_waist: 0.0,
46 m2: 1.0,
47 orientation: Orientation::Y,
48 }
49 }
50}
51
52#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
54pub struct GaussRow {
55 pub surface: usize,
57 #[serde(deserialize_with = "crate::nan::f")]
59 pub size: f64,
60 #[serde(deserialize_with = "crate::nan::f")]
62 pub waist: f64,
63 #[serde(deserialize_with = "crate::nan::f")]
65 pub position: f64,
66 pub radius: Option<f64>,
68 #[serde(deserialize_with = "crate::nan::f")]
70 pub divergence: f64,
71 #[serde(deserialize_with = "crate::nan::f")]
73 pub mixed_size: f64,
74 #[serde(deserialize_with = "crate::nan::f")]
75 pub mixed_waist: f64,
76 #[serde(deserialize_with = "crate::nan::f")]
77 pub mixed_divergence: f64,
78 #[serde(deserialize_with = "crate::nan::f")]
80 pub rayleigh: f64,
81 #[serde(deserialize_with = "crate::nan::f")]
83 pub index: f64,
84 #[serde(deserialize_with = "crate::nan::f")]
86 pub z: f64,
87}
88
89#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
91pub struct GaussOut {
92 pub unit: String,
93 #[serde(deserialize_with = "crate::nan::f")]
95 pub wavelength: f64,
96 pub wave: usize,
98 pub m2: f64,
99 pub orientation: Orientation,
100 pub rows: Vec<GaussRow>,
102 pub profile: Vec<Vec<[f64; 2]>>,
104 pub warnings: Vec<String>,
105}
106
107pub(super) struct Gauss;
108
109impl Analysis for Gauss {
110 type Params = GaussParams;
111 type Output = GaussOut;
112 fn meta(&self) -> Meta {
113 Meta {
114 name: "gaussian-beam",
115 about: "Paraxial Gaussian beam: beam size, waist, waist position, wavefront radius, divergence and Rayleigh range after each surface",
116 help: "Paraxial Gaussian beam (OpticStudio's Paraxial Gaussian Beam): the input beam given by the waist radius of its embedded Gaussian, the distance from surface 1 to the waist and M² is carried through the paraxial ABCD matrix of the system (from two paraxial rays), and for the space after each surface it reports the 1/e² beam radius at the vertex, the waist radius, the distance from the waist to the vertex (negative if the waist is downstream), the wavefront radius, the far-field half-angle divergence and the Rayleigh range in that medium, for the embedded Gaussian and for the mixed-mode beam (√M² times the radius and waist; divergence atan(√M² w₀ / z_R), √M² times at small angles). The position and the radius are signed along the local z of the surface (reversed after an odd number of mirrors, as in OpticStudio). The beam radius along the axis is in profile",
117 }
118 }
119 fn run(&self, cx: &Cx<'_>, p: &GaussParams) -> Result<GaussOut> {
120 let s = cx.system;
121 if p.orientation == Orientation::X && !s.symmetric() {
122 return Err(Error::Invalid(
123 "the x orientation of the Gaussian beam needs a rotationally symmetric system for now (use y)"
124 .into(),
125 ));
126 }
127 let w = wave(s, p.wave)?.unwrap_or(s.primary);
128 let g = gauss::gauss(
129 s,
130 &gauss::Beam {
131 wave: w,
132 waist: p.waist,
133 surf1_to_waist: p.surf1_to_waist,
134 m2: p.m2,
135 },
136 )?;
137 let m = p.m2.sqrt();
138 let mut warnings = Vec::new();
139 if s.surfaces.iter().any(|d| d.grin().is_some()) {
140 warnings.push(
141 "the beam is carried through the GRIN media paraxially, but the profile is not drawn inside them"
142 .into(),
143 );
144 }
145 Ok(GaussOut {
146 unit: "mm".into(),
147 wavelength: s.wavelengths[w],
148 wave: w + 1,
149 m2: p.m2,
150 orientation: p.orientation,
151 rows: g
152 .rows
153 .iter()
154 .map(|r| GaussRow {
155 surface: r.surface,
156 size: r.size,
157 waist: r.waist,
158 position: r.position,
159 radius: r.radius.is_finite().then_some(r.radius),
160 divergence: r.divergence,
161 mixed_size: r.size * m,
162 mixed_waist: r.waist * m,
163 mixed_divergence: (r.waist * m / r.rayleigh).atan(),
164 rayleigh: r.rayleigh,
165 index: r.index,
166 z: r.z,
167 })
168 .collect(),
169 profile: g.profile,
170 warnings,
171 })
172 }
173}