oxiphoton 0.1.1

Pure Rust Computational Photonics & Optical Simulation Framework
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
use crate::error::OxiPhotonError;
use std::f64::consts::PI;

// ---------------------------------------------------------------------------
// ParamSweep — single-parameter sweep
// ---------------------------------------------------------------------------

/// Single-parameter sweep framework.
///
/// Holds a named parameter and a list of values to sweep over.  Results are
/// collected by calling [`ParamSweep::run`] with a closure.
///
/// # Examples
/// ```
/// use oxiphoton::fdtd::sweep::parameter::ParamSweep;
///
/// let sweep = ParamSweep::linspace("gap_nm", 50.0, 500.0, 10);
/// let results: Vec<f64> = sweep.run(|g| g * g);
/// assert_eq!(results.len(), 10);
/// ```
pub struct ParamSweep {
    /// Human-readable parameter name (for logging / output).
    pub param_name: String,
    /// Values to sweep over.
    pub values: Vec<f64>,
}

impl ParamSweep {
    /// Create a new sweep from an explicit list of values.
    pub fn new(name: impl Into<String>, values: Vec<f64>) -> Self {
        Self {
            param_name: name.into(),
            values,
        }
    }

    /// Create a linearly-spaced sweep: `n` points from `start` to `end` (inclusive).
    pub fn linspace(name: impl Into<String>, start: f64, end: f64, n: usize) -> Self {
        let values = linspace_vec(start, end, n);
        Self::new(name, values)
    }

    /// Create a logarithmically-spaced sweep.
    ///
    /// Points span `10^start_exp` to `10^end_exp` with `n` points (inclusive).
    pub fn logspace(name: impl Into<String>, start_exp: f64, end_exp: f64, n: usize) -> Self {
        let exps = linspace_vec(start_exp, end_exp, n);
        let values = exps.iter().map(|&e| 10.0_f64.powf(e)).collect();
        Self::new(name, values)
    }

    /// Run the sweep sequentially, collecting results.
    ///
    /// # Arguments
    /// * `f` — closure that maps a parameter value to a result
    ///
    /// # Returns
    /// Vec of results in the same order as `self.values`.
    pub fn run<F, R>(&self, f: F) -> Vec<R>
    where
        F: Fn(f64) -> R,
    {
        self.values.iter().map(|&v| f(v)).collect()
    }

    /// Run sweep in parallel using Rayon (requires the `parallel` feature).
    #[cfg(feature = "parallel")]
    pub fn run_parallel<F, R>(&self, f: F) -> Vec<R>
    where
        F: Fn(f64) -> R + Send + Sync,
        R: Send,
    {
        use rayon::prelude::*;
        self.values.par_iter().map(|&v| f(v)).collect()
    }

    /// Run sweep sequentially, passing both the index and the value to `f`.
    pub fn run_indexed<F, R>(&self, f: F) -> Vec<(f64, R)>
    where
        F: Fn(usize, f64) -> R,
    {
        self.values
            .iter()
            .enumerate()
            .map(|(i, &v)| (v, f(i, v)))
            .collect()
    }

    /// Find the value that minimises an objective function.
    ///
    /// Evaluates `f` at every point in the sweep and returns
    /// `(minimising_value, minimum_result)`.
    ///
    /// Returns `(f64::NAN, f64::NAN)` if the sweep is empty.
    pub fn minimize<F>(&self, f: F) -> (f64, f64)
    where
        F: Fn(f64) -> f64,
    {
        self.values.iter().map(|&v| (v, f(v))).fold(
            (f64::NAN, f64::INFINITY),
            |(bv, br), (v, r)| {
                if r < br {
                    (v, r)
                } else {
                    (bv, br)
                }
            },
        )
    }

    /// Find the value that maximises an objective function.
    ///
    /// Returns `(maximising_value, maximum_result)`.
    ///
    /// Returns `(f64::NAN, f64::NEG_INFINITY)` if the sweep is empty.
    pub fn maximize<F>(&self, f: F) -> (f64, f64)
    where
        F: Fn(f64) -> f64,
    {
        self.values.iter().map(|&v| (v, f(v))).fold(
            (f64::NAN, f64::NEG_INFINITY),
            |(bv, br), (v, r)| {
                if r > br {
                    (v, r)
                } else {
                    (bv, br)
                }
            },
        )
    }
}

// ---------------------------------------------------------------------------
// ParamGrid — 2D parameter grid sweep
// ---------------------------------------------------------------------------

/// Two-dimensional parameter grid sweep.
///
/// Evaluates a closure at every point on a grid formed by the Cartesian
/// product of two parameter lists.  Results are returned as a
/// `Vec<Vec<R>>` where the outer index corresponds to `param1_values`
/// and the inner index to `param2_values`.
///
/// # Examples
/// ```
/// use oxiphoton::fdtd::sweep::parameter::ParamGrid;
///
/// let grid = ParamGrid::new(
///     "width_nm",  vec![100.0, 200.0, 300.0],
///     "height_nm", vec![50.0, 100.0],
/// );
/// let results = grid.run(|w, h| w * h);
/// assert_eq!(results.len(), 3);
/// assert_eq!(results[0].len(), 2);
/// ```
pub struct ParamGrid {
    /// Name of the first parameter.
    pub param1_name: String,
    /// Values of the first parameter.
    pub param1_values: Vec<f64>,
    /// Name of the second parameter.
    pub param2_name: String,
    /// Values of the second parameter.
    pub param2_values: Vec<f64>,
}

impl ParamGrid {
    /// Create a new 2D parameter grid.
    pub fn new(
        name1: impl Into<String>,
        values1: Vec<f64>,
        name2: impl Into<String>,
        values2: Vec<f64>,
    ) -> Self {
        Self {
            param1_name: name1.into(),
            param1_values: values1,
            param2_name: name2.into(),
            param2_values: values2,
        }
    }

    /// Run the grid sweep sequentially.
    ///
    /// # Returns
    /// `results[i][j]` = `f(param1_values[i], param2_values[j])`.
    pub fn run<F, R>(&self, f: F) -> Vec<Vec<R>>
    where
        F: Fn(f64, f64) -> R,
    {
        self.param1_values
            .iter()
            .map(|&v1| self.param2_values.iter().map(|&v2| f(v1, v2)).collect())
            .collect()
    }

    /// Run the grid sweep in parallel (requires the `parallel` feature).
    ///
    /// Row parallelism: each row of the grid (fixed `param1`) is computed
    /// in parallel across available threads.
    #[cfg(feature = "parallel")]
    pub fn run_parallel<F, R>(&self, f: F) -> Vec<Vec<R>>
    where
        F: Fn(f64, f64) -> R + Send + Sync,
        R: Send + Default + Clone,
    {
        use rayon::prelude::*;
        self.param1_values
            .par_iter()
            .map(|&v1| self.param2_values.iter().map(|&v2| f(v1, v2)).collect())
            .collect()
    }
}

// ---------------------------------------------------------------------------
// ConvergenceSweep — keeps doubling until convergence
// ---------------------------------------------------------------------------

/// Convergence test sweep.
///
/// Evaluates a function starting from `initial_value`, doubles the parameter
/// at each step, and stops when the relative change between successive results
/// is smaller than `tolerance`.  Useful for convergence studies in mesh
/// resolution, time-step count, etc.
///
/// # Examples
/// ```
/// use oxiphoton::fdtd::sweep::parameter::ConvergenceSweep;
///
/// // Riemann-sum approximation of ∫₁² 1/x² dx = 1 − 1/2 = 0.5.
/// // As n doubles the approximation converges to 0.5.
/// let sweep = ConvergenceSweep::new("n_steps", 16.0, 12, 1e-4);
/// let (converged_val, result) = sweep.run(|n| {
///     let n = n as usize;
///     let dx = 1.0 / n as f64;           // step size on [1, 2]
///     (0..n).map(|i| dx / (1.0 + i as f64 * dx).powi(2)).sum::<f64>()
/// }).expect("should converge");
/// assert!((result - 0.5).abs() < 1e-3);
/// ```
pub struct ConvergenceSweep {
    /// Human-readable parameter name.
    pub param_name: String,
    /// Starting value of the parameter.
    pub initial_value: f64,
    /// Maximum number of doublings before giving up.
    pub max_doublings: usize,
    /// Relative convergence tolerance: |Δr / r| < tolerance.
    pub tolerance: f64,
}

impl ConvergenceSweep {
    /// Create a new convergence sweep.
    pub fn new(name: impl Into<String>, initial: f64, max_doublings: usize, tol: f64) -> Self {
        Self {
            param_name: name.into(),
            initial_value: initial,
            max_doublings,
            tolerance: tol,
        }
    }

    /// Run the convergence sweep.
    ///
    /// # Returns
    /// `Ok((converged_param, result))` if convergence is achieved within
    /// `max_doublings` steps, or an `Err(OxiPhotonError::NumericalError)`
    /// if the maximum number of doublings is exceeded.
    pub fn run<F>(&self, f: F) -> Result<(f64, f64), OxiPhotonError>
    where
        F: Fn(f64) -> f64,
    {
        let mut value = self.initial_value;
        let mut prev_result = f(value);

        for _ in 0..self.max_doublings {
            value *= 2.0;
            let result = f(value);

            let rel_change = if prev_result.abs() > f64::EPSILON {
                (result - prev_result).abs() / prev_result.abs()
            } else {
                result.abs()
            };

            if rel_change < self.tolerance {
                return Ok((value, result));
            }

            prev_result = result;
        }

        Err(OxiPhotonError::NumericalError(format!(
            "ConvergenceSweep '{}': did not converge in {} doublings (tol={:.2e})",
            self.param_name, self.max_doublings, self.tolerance
        )))
    }
}

// ---------------------------------------------------------------------------
// WavelengthSweep — optical wavelength scan
// ---------------------------------------------------------------------------

/// Wavelength sweep for optical simulations.
///
/// Specifies a wavelength range in nanometres (for user convenience) but
/// stores and provides values in SI units (metres and Hz).
///
/// # Examples
/// ```
/// use oxiphoton::fdtd::sweep::parameter::WavelengthSweep;
///
/// let sweep = WavelengthSweep::new(1000.0, 1600.0, 61);
/// let lambdas_nm = sweep.wavelengths_nm();
/// assert!((lambdas_nm[0] - 1000.0).abs() < 1e-9);
/// assert!((lambdas_nm[60] - 1600.0).abs() < 1e-9);
/// ```
pub struct WavelengthSweep {
    /// Minimum wavelength in metres.
    pub lambda_min_m: f64,
    /// Maximum wavelength in metres.
    pub lambda_max_m: f64,
    /// Number of wavelength points.
    pub n_points: usize,
}

impl WavelengthSweep {
    /// Create a new wavelength sweep.
    ///
    /// # Arguments
    /// * `lambda_min_nm` — minimum wavelength in nanometres
    /// * `lambda_max_nm` — maximum wavelength in nanometres
    /// * `n_points`      — number of wavelength points (inclusive at both ends)
    pub fn new(lambda_min_nm: f64, lambda_max_nm: f64, n_points: usize) -> Self {
        Self {
            lambda_min_m: lambda_min_nm * 1e-9,
            lambda_max_m: lambda_max_nm * 1e-9,
            n_points,
        }
    }

    /// Wavelengths in metres (linearly spaced from λ_min to λ_max).
    pub fn wavelengths_m(&self) -> Vec<f64> {
        linspace_vec(self.lambda_min_m, self.lambda_max_m, self.n_points)
    }

    /// Wavelengths in nanometres.
    pub fn wavelengths_nm(&self) -> Vec<f64> {
        self.wavelengths_m().iter().map(|&l| l * 1e9).collect()
    }

    /// Frequencies in Hz: f = c / λ.
    ///
    /// Note: frequencies are *not* linearly spaced when wavelengths are.
    pub fn frequencies_hz(&self) -> Vec<f64> {
        use crate::units::conversion::SPEED_OF_LIGHT;
        self.wavelengths_m()
            .iter()
            .map(|&l| SPEED_OF_LIGHT / l)
            .collect()
    }

    /// Angular frequencies ω = 2πf in rad/s.
    pub fn angular_frequencies(&self) -> Vec<f64> {
        self.frequencies_hz()
            .iter()
            .map(|&f| 2.0 * PI * f)
            .collect()
    }

    /// Run the sweep sequentially.
    ///
    /// # Arguments
    /// * `f` — closure mapping wavelength in metres to a result
    ///
    /// # Returns
    /// Vec of `(wavelength_m, result)` pairs.
    pub fn run<F, R>(&self, f: F) -> Vec<(f64, R)>
    where
        F: Fn(f64) -> R,
    {
        self.wavelengths_m().iter().map(|&l| (l, f(l))).collect()
    }
}

// ---------------------------------------------------------------------------
// Internal helper
// ---------------------------------------------------------------------------

/// Generate `n` linearly-spaced points from `start` to `end` (inclusive).
fn linspace_vec(start: f64, end: f64, n: usize) -> Vec<f64> {
    match n {
        0 => Vec::new(),
        1 => vec![start],
        _ => {
            let step = (end - start) / (n - 1) as f64;
            (0..n).map(|i| start + i as f64 * step).collect()
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    const TOL: f64 = 1e-10;

    #[test]
    fn test_param_sweep_linspace() {
        let sweep = ParamSweep::linspace("x", 1.0, 5.0, 5);
        assert_eq!(sweep.values.len(), 5);
        assert!((sweep.values[0] - 1.0).abs() < TOL);
        assert!((sweep.values[1] - 2.0).abs() < TOL);
        assert!((sweep.values[2] - 3.0).abs() < TOL);
        assert!((sweep.values[3] - 4.0).abs() < TOL);
        assert!((sweep.values[4] - 5.0).abs() < TOL);
    }

    #[test]
    fn test_param_sweep_run() {
        let sweep = ParamSweep::linspace("x", 1.0, 5.0, 5);
        let results: Vec<f64> = sweep.run(|x| x * x);
        let expected = [1.0, 4.0, 9.0, 16.0, 25.0];
        for (r, e) in results.iter().zip(expected.iter()) {
            assert!((r - e).abs() < TOL, "got {r}, expected {e}");
        }
    }

    #[test]
    fn test_param_grid_run() {
        let grid = ParamGrid::new("a", vec![1.0, 2.0, 3.0], "b", vec![10.0, 100.0, 1000.0]);
        let results = grid.run(|a, b| a * b);
        assert_eq!(results.len(), 3);
        assert_eq!(results[0].len(), 3);
        // results[i][j] = param1_values[i] * param2_values[j]
        assert!((results[0][0] - 10.0).abs() < TOL);
        assert!((results[0][1] - 100.0).abs() < TOL);
        assert!((results[1][0] - 20.0).abs() < TOL);
        assert!((results[2][2] - 3000.0).abs() < TOL);
    }

    #[test]
    fn test_wavelength_sweep() {
        let sweep = WavelengthSweep::new(1000.0, 1600.0, 61);
        let nm = sweep.wavelengths_nm();
        assert_eq!(nm.len(), 61);
        assert!((nm[0] - 1000.0).abs() < 1e-6, "min={}", nm[0]);
        assert!((nm[60] - 1600.0).abs() < 1e-6, "max={}", nm[60]);

        let m = sweep.wavelengths_m();
        assert!((m[0] - 1e-6).abs() < 1e-15);
        assert!((m[60] - 1.6e-6).abs() < 1e-15);

        // Frequencies should be c / lambda
        use crate::units::conversion::SPEED_OF_LIGHT;
        let freqs = sweep.frequencies_hz();
        let expected_f0 = SPEED_OF_LIGHT / (1000e-9);
        let expected_f1 = SPEED_OF_LIGHT / (1600e-9);
        assert!((freqs[0] - expected_f0).abs() / expected_f0 < 1e-10);
        assert!((freqs[60] - expected_f1).abs() / expected_f1 < 1e-10);
    }

    #[test]
    fn test_minimize() {
        // Minimum of f(x) = x² on the range [-1.0, 1.0] should be at x=0
        let sweep = ParamSweep::linspace("x", -1.0, 1.0, 201);
        let (best_x, best_val) = sweep.minimize(|x| x * x);
        assert!(best_val < 1e-4, "minimum value={best_val}");
        assert!(best_x.abs() < 0.02, "minimiser={best_x}");
    }

    #[test]
    fn test_convergence_sweep() {
        // Function whose value converges to π² / 6 as n → ∞  (Basel series)
        // f(n) ≈ sum_{k=1}^{n} 1/k²
        let sweep = ConvergenceSweep::new("n", 16.0, 20, 1e-3);
        let result = sweep.run(|n| {
            let n = n as usize;
            (1..=n).map(|k| 1.0 / (k as f64 * k as f64)).sum::<f64>()
        });
        assert!(result.is_ok(), "convergence failed: {:?}", result);
        let (_, val) = result.expect("convergence should succeed");
        // Basel series converges to π²/6 ≈ 1.6449
        let pi_sq_over_6 = PI * PI / 6.0;
        assert!(
            (val - pi_sq_over_6).abs() < 0.01,
            "val={val}, expected≈{pi_sq_over_6}"
        );
    }

    #[test]
    fn test_logspace() {
        let sweep = ParamSweep::logspace("freq", 0.0, 3.0, 4); // 1, 10, 100, 1000
        assert_eq!(sweep.values.len(), 4);
        assert!((sweep.values[0] - 1.0).abs() < TOL);
        assert!((sweep.values[1] - 10.0).abs() < 1e-9);
        assert!((sweep.values[2] - 100.0).abs() < 1e-7);
        assert!((sweep.values[3] - 1000.0).abs() < 1e-6);
    }

    #[test]
    fn test_run_indexed() {
        let sweep = ParamSweep::linspace("t", 0.0, 2.0, 3);
        let indexed = sweep.run_indexed(|i, v| i as f64 + v);
        // values: 0.0, 1.0, 2.0; f(0,0.0)=0.0, f(1,1.0)=2.0, f(2,2.0)=4.0
        assert_eq!(indexed.len(), 3);
        assert!((indexed[0].1 - 0.0).abs() < TOL);
        assert!((indexed[1].1 - 2.0).abs() < TOL);
        assert!((indexed[2].1 - 4.0).abs() < TOL);
    }
}