Skip to main content

lensbench_analysis/defs/
beam.rs

1//! 近軸ガウスビーム(Paraxial Gaussian Beam)
2
3use 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/// ビームを追う断面
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
13#[serde(rename_all = "lowercase")]
14pub enum Orientation {
15    /// YZ 面(トロイダル面などは YZ 面の近軸の曲率)
16    #[default]
17    Y,
18    /// XZ 面(今は回転対称の系だけ。YZ 面と同じ値)
19    X,
20}
21
22/// 近軸ガウスビームの設定
23#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
24#[serde(default, deny_unknown_fields)]
25pub(super) struct GaussParams {
26    /// Wavelength number (from 1; the primary wavelength if omitted)
27    pub wave: Option<usize>,
28    /// Waist radius of the embedded Gaussian beam (1/e² intensity, in object space; the mixed-mode beam is √M² times larger)
29    #[schemars(extend("x-min" = 0.0))]
30    pub waist: f64,
31    /// Distance from surface 1 to the waist (positive if the waist is after surface 1)
32    pub surf1_to_waist: f64,
33    /// M² factor of the beam (1 for a pure Gaussian)
34    #[schemars(extend("x-min" = 1.0))]
35    pub m2: f64,
36    /// Cross section to trace (y or x)
37    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/// 近軸ガウスビームの 1 面(面の後の空間)
53#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
54pub struct GaussRow {
55    /// 面の番号
56    pub surface: usize,
57    /// 面の頂点でのビームの半径(1/e²、埋め込みのガウスビーム=基本モード)
58    #[serde(deserialize_with = "crate::nan::f")]
59    pub size: f64,
60    /// ウエストの半径(基本モード)
61    #[serde(deserialize_with = "crate::nan::f")]
62    pub waist: f64,
63    /// ウエストから面の頂点までの距離(面の局所 z の向き。反射が偶数回なら、負は面がウエストより前=ウエストはこの先)
64    #[serde(deserialize_with = "crate::nan::f")]
65    pub position: f64,
66    /// 面の頂点での波面の曲率半径(面の局所 z の向き、反射が偶数回なら正は発散。ウエストの上なら null)
67    pub radius: Option<f64>,
68    /// 広がり角の半角 [rad](媒質の中、基本モード)
69    #[serde(deserialize_with = "crate::nan::f")]
70    pub divergence: f64,
71    /// M² の混合モードのビームの半径・ウエスト・広がり角(半径・ウエストは基本モードの √M² 倍、広がり角は atan(√M² w₀ / z_R)。曲率半径・位置・レイリー長は同じ)
72    #[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    /// レイリー長(媒質の中)
79    #[serde(deserialize_with = "crate::nan::f")]
80    pub rayleigh: f64,
81    /// 面の後の媒質の屈折率
82    #[serde(deserialize_with = "crate::nan::f")]
83    pub index: f64,
84    /// 面の頂点の、展開した面 1 の頂点からの位置
85    #[serde(deserialize_with = "crate::nan::f")]
86    pub z: f64,
87}
88
89/// 近軸ガウスビームの結果
90#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
91pub struct GaussOut {
92    pub unit: String,
93    /// 波長 [µm]
94    #[serde(deserialize_with = "crate::nan::f")]
95    pub wavelength: f64,
96    /// 波長の番号(1 始まり)
97    pub wave: usize,
98    pub m2: f64,
99    pub orientation: Orientation,
100    /// 面 1 から像面まで(座標ブレーク・無視する面は直前の面と同じ値)
101    pub rows: Vec<GaussRow>,
102    /// 光軸に沿ったビームの半径(基本モード。混合モードは √M² 倍): 面と面の間ごとの (展開した面 1 からの位置, 半径) の並び(GRIN の媒質・無限の間隔は入れない)
103    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}