photonoxide 0.1.0

Validated, fabrication-ready photonics for Rust: mode solvers, FDFD, FDTD, inverse design, layout and PDKs, with a live studio
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Typed quantities and the conventions every solver follows.
//!
//! # Units
//!
//! Lengths are in micrometres. A frequency is in units of c/µm, the reciprocal of the vacuum
//! wavelength in micrometres: 1.55 µm is the frequency 1/1.55. This is the natural unit system
//! of electromagnetic solvers (Oskooi et al., Comput. Phys. Commun. 181, 687 (2010),
//! [doi:10.1016/j.cpc.2009.11.008](https://doi.org/10.1016/j.cpc.2009.11.008)): with c = 1,
//! a time step and a grid step have the same unit. [`Frequency::thz`] and
//! [`Frequency::to_thz`] convert to SI with the exact speed of light, [`SPEED_OF_LIGHT`].
//!
//! [`Length`] (a coordinate or a size), [`Wavelength`] (in vacuum) and [`Frequency`] are
//! different types, so one can't be passed for another.
//!
//! # The time convention
//!
//! A time-harmonic field is the real part of a complex amplitude times **e^(−iωt)**, the
//! physicists' convention. It fixes every sign:
//!
//! - a wave travelling towards +x is `e^(i(kx − ωt))`: at a fixed time its phase grows with x;
//! - in a passive, lossy medium the permittivity has a **positive** imaginary part, and so does
//!   the refractive index, n = n′ + iκ with κ ≥ 0 ([`refractive_index`]);
//! - the amplitude of a real signal s(t) at angular frequency ω is recovered with the kernel
//!   e^(+iωt): `A = (2/T) ∫ s(t) e^(iωt) dt` over whole periods T.

use std::f64::consts::TAU;
use std::fmt;
use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};

use num_complex::Complex64;

use crate::{Error, Result};

/// The speed of light in vacuum, m/s: exact, by the definition of the metre (SI, 1983).
pub const SPEED_OF_LIGHT: f64 = 299_792_458.0;

/// A length or a coordinate, in micrometres. Any finite value, negative too.
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Length(f64);

impl Length {
    /// Zero.
    pub const ZERO: Length = Length(0.0);

    /// A length of `value` micrometres.
    pub const fn um(value: f64) -> Length {
        Length(value)
    }

    /// A length of `value` nanometres.
    pub const fn nm(value: f64) -> Length {
        Length(value / 1000.0)
    }

    /// The length in micrometres.
    pub const fn to_um(self) -> f64 {
        self.0
    }

    /// The length in nanometres.
    pub const fn to_nm(self) -> f64 {
        self.0 * 1000.0
    }

    /// The absolute value.
    pub fn abs(self) -> Length {
        Length(self.0.abs())
    }
}

impl fmt::Display for Length {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} um", self.0)
    }
}

impl Add for Length {
    type Output = Length;
    fn add(self, rhs: Length) -> Length {
        Length(self.0 + rhs.0)
    }
}

impl AddAssign for Length {
    fn add_assign(&mut self, rhs: Length) {
        self.0 += rhs.0;
    }
}

impl Sub for Length {
    type Output = Length;
    fn sub(self, rhs: Length) -> Length {
        Length(self.0 - rhs.0)
    }
}

impl SubAssign for Length {
    fn sub_assign(&mut self, rhs: Length) {
        self.0 -= rhs.0;
    }
}

impl Neg for Length {
    type Output = Length;
    fn neg(self) -> Length {
        Length(-self.0)
    }
}

impl Mul<f64> for Length {
    type Output = Length;
    fn mul(self, rhs: f64) -> Length {
        Length(self.0 * rhs)
    }
}

impl Mul<Length> for f64 {
    type Output = Length;
    fn mul(self, rhs: Length) -> Length {
        Length(self * rhs.0)
    }
}

impl Div<f64> for Length {
    type Output = Length;
    fn div(self, rhs: f64) -> Length {
        Length(self.0 / rhs)
    }
}

/// The ratio of two lengths.
impl Div for Length {
    type Output = f64;
    fn div(self, rhs: Length) -> f64 {
        self.0 / rhs.0
    }
}

/// A wavelength in vacuum: positive and finite.
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct Wavelength(f64);

impl Wavelength {
    /// A wavelength of `value` micrometres.
    ///
    /// # Errors
    ///
    /// [`Error::InvalidValue`] unless `value` is positive and finite.
    pub fn um(value: f64) -> Result<Wavelength> {
        if value.is_finite() && value > 0.0 {
            Ok(Wavelength(value))
        } else {
            Err(Error::invalid(
                "wavelength",
                format!("must be positive and finite, got {value} um"),
            ))
        }
    }

    /// A wavelength of `value` nanometres.
    ///
    /// # Errors
    ///
    /// [`Error::InvalidValue`] unless `value` is positive and finite.
    pub fn nm(value: f64) -> Result<Wavelength> {
        Wavelength::um(value / 1000.0).map_err(|_| {
            Error::invalid(
                "wavelength",
                format!("must be positive and finite, got {value} nm"),
            )
        })
    }

    /// A wavelength the caller knows is positive and finite (built-in data).
    pub(crate) const fn from_um_unchecked(value: f64) -> Wavelength {
        Wavelength(value)
    }

    /// The wavelength in micrometres.
    pub const fn to_um(self) -> f64 {
        self.0
    }

    /// The wavelength in nanometres.
    pub const fn to_nm(self) -> f64 {
        self.0 * 1000.0
    }

    /// The wavelength as a [`Length`].
    pub const fn length(self) -> Length {
        Length(self.0)
    }

    /// The frequency of light of this wavelength in vacuum.
    pub fn frequency(self) -> Frequency {
        Frequency(1.0 / self.0)
    }

    /// The vacuum wavenumber k₀ = 2π/λ, in 1/µm.
    pub fn wavenumber(self) -> f64 {
        TAU / self.0
    }
}

impl fmt::Display for Wavelength {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} um", self.0)
    }
}

/// A frequency in units of c/µm (see the [module docs](self)): positive and finite.
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct Frequency(f64);

impl Frequency {
    /// A frequency of `value` c/µm, the reciprocal of the vacuum wavelength in micrometres.
    ///
    /// # Errors
    ///
    /// [`Error::InvalidValue`] unless `value` is positive and finite.
    pub fn natural(value: f64) -> Result<Frequency> {
        if value.is_finite() && value > 0.0 {
            Ok(Frequency(value))
        } else {
            Err(Error::invalid(
                "frequency",
                format!("must be positive and finite, got {value} c/um"),
            ))
        }
    }

    /// A frequency of `value` terahertz.
    ///
    /// # Errors
    ///
    /// [`Error::InvalidValue`] unless `value` is positive and finite.
    pub fn thz(value: f64) -> Result<Frequency> {
        // f [c/um] = f [Hz] * 1 um / c
        Frequency::natural(value * 1e12 * 1e-6 / SPEED_OF_LIGHT).map_err(|_| {
            Error::invalid(
                "frequency",
                format!("must be positive and finite, got {value} THz"),
            )
        })
    }

    /// The frequency in c/µm.
    pub const fn to_natural(self) -> f64 {
        self.0
    }

    /// The frequency in terahertz.
    pub fn to_thz(self) -> f64 {
        self.0 * SPEED_OF_LIGHT / 1e-6 / 1e12
    }

    /// The angular frequency ω = 2πf, in units of c/µm.
    pub fn angular(self) -> f64 {
        TAU * self.0
    }

    /// The vacuum wavelength of light at this frequency.
    pub fn wavelength(self) -> Wavelength {
        Wavelength(1.0 / self.0)
    }
}

impl fmt::Display for Frequency {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} c/um", self.0)
    }
}

/// The refractive index n = n′ + iκ of a relative permittivity ε: the square root with κ ≥ 0,
/// which is the physical branch under the e^(−iωt) convention (a passive medium attenuates).
///
/// For a real, negative ε (an ideal metal) n is purely imaginary, n = i√|ε|.
pub fn refractive_index(permittivity: Complex64) -> Complex64 {
    let n = permittivity.sqrt();
    if n.im < 0.0 { -n } else { n }
}

/// The relative permittivity ε = n² of a refractive index.
pub fn permittivity(refractive_index: Complex64) -> Complex64 {
    refractive_index * refractive_index
}

#[cfg(test)]
mod tests {
    use super::*;

    fn close(a: f64, b: f64, tol: f64) -> bool {
        (a - b).abs() <= tol * b.abs().max(1.0)
    }

    #[test]
    fn lengths_convert_between_micrometres_and_nanometres() {
        assert_eq!(Length::nm(220.0).to_um(), 0.22);
        assert_eq!(Length::um(0.5).to_nm(), 500.0);
        assert_eq!(Length::um(1.0) + Length::nm(500.0), Length::um(1.5));
        assert_eq!(Length::um(3.0) / Length::um(1.5), 2.0);
        assert_eq!(-Length::um(2.0), Length::um(-2.0));
        assert_eq!(2.0 * Length::um(1.25), Length::um(2.5));
    }

    #[test]
    fn wavelengths_must_be_positive_and_finite() {
        assert!(Wavelength::um(1.55).is_ok());
        for bad in [0.0, -1.55, f64::NAN, f64::INFINITY] {
            let e = Wavelength::um(bad).unwrap_err();
            assert!(
                matches!(
                    e,
                    Error::InvalidValue {
                        what: "wavelength",
                        ..
                    }
                ),
                "{bad}"
            );
        }
        let e = Wavelength::nm(-1550.0).unwrap_err();
        assert!(e.to_string().contains("-1550 nm"), "{e}");
    }

    #[test]
    fn frequencies_must_be_positive_and_finite() {
        for bad in [0.0, -1.0, f64::NAN] {
            assert!(Frequency::natural(bad).is_err());
            assert!(Frequency::thz(bad).is_err());
        }
    }

    #[test]
    fn frequency_and_wavelength_are_reciprocal_in_natural_units() {
        let lam = Wavelength::um(1.55).unwrap();
        let f = lam.frequency();
        assert!(close(f.to_natural(), 1.0 / 1.55, 1e-15));
        assert!(close(f.wavelength().to_um(), 1.55, 1e-15));
        assert!(close(f.angular(), TAU / 1.55, 1e-15));
        assert!(close(lam.wavenumber(), TAU / 1.55, 1e-15));
    }

    #[test]
    fn terahertz_use_the_exact_speed_of_light() {
        // 1.55 um is c / 1.55e-6 m = 193.414489... THz
        let f = Wavelength::um(1.55).unwrap().frequency();
        assert!(close(f.to_thz(), 299_792_458.0 / 1.55e-6 / 1e12, 1e-14));
        assert!(close(f.to_thz(), 193.414_489_032_258, 1e-12));
        let back = Frequency::thz(f.to_thz()).unwrap();
        assert!(close(back.wavelength().to_um(), 1.55, 1e-14));
    }

    #[test]
    fn the_refractive_index_of_a_lossy_medium_attenuates() {
        // Im(eps) > 0 is loss under e^(-i w t); the index must have kappa > 0 as well
        let n = refractive_index(Complex64::new(12.0, 0.5));
        assert!(n.re > 0.0 && n.im > 0.0, "{n}");
        assert!(close(permittivity(n).re, 12.0, 1e-14) && close(permittivity(n).im, 0.5, 1e-14));
        // a wave e^(i(n k0 x - w t)) decays towards +x
        let k0 = Wavelength::um(1.55).unwrap().wavenumber();
        let at = |x: f64| (Complex64::i() * n * k0 * x).exp().norm();
        assert!(at(1.0) < at(0.0));
    }

    #[test]
    fn a_lossless_dielectric_has_a_real_index() {
        let n = refractive_index(Complex64::new(2.085, 0.0));
        assert_eq!(n.im, 0.0);
        assert!(close(n.re, 2.085f64.sqrt(), 1e-15));
    }

    #[test]
    fn an_ideal_metal_has_an_imaginary_index() {
        let n = refractive_index(Complex64::new(-16.0, 0.0));
        assert!(n.re.abs() < 1e-15 && close(n.im, 4.0, 1e-15), "{n}");
    }

    #[test]
    fn a_forward_wave_has_a_phase_that_grows_with_x_and_falls_with_t() {
        // e^(i(kx - wt)): the convention every solver and monitor follows
        let (k, w) = (2.0, 3.0);
        let phase = |x: f64, t: f64| Complex64::new(0.0, k * x - w * t).exp().arg();
        assert!(phase(0.1, 0.0) > phase(0.0, 0.0));
        assert!(phase(0.0, 0.1) < phase(0.0, 0.0));
    }

    #[test]
    fn the_amplitude_of_a_real_signal_is_recovered_with_e_plus_i_w_t() {
        // s(t) = Re(A e^(-i w t)) and A = (2/T) sum s(t) e^(i w t) dt over whole periods
        let a = Complex64::new(0.7, -0.4);
        let w = TAU * 0.65;
        let periods = 20.0;
        let n = 20_000;
        let t_end = periods * TAU / w;
        let dt = t_end / n as f64;
        let mut sum = Complex64::new(0.0, 0.0);
        for i in 0..n {
            let t = (i as f64 + 0.5) * dt;
            let s = (a * Complex64::new(0.0, -w * t).exp()).re;
            sum += s * Complex64::new(0.0, w * t).exp() * dt;
        }
        let got = sum * (2.0 / t_end);
        assert!((got - a).norm() < 1e-9, "{got} vs {a}");
    }
}