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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
//! End-to-end EQE(λ) spectral-response evaluator for solar cells.
//!
//! Chains Phase 8 AR/back-reflector optics (TMM) with a Tiedje-Yablonovitch
//! photon-recycling self-consistent iteration.
//!
//! ## Physical model
//!
//! For each wavelength λ:
//!
//! 1. **Optical absorption** A_optical(λ) is computed via a per-wavelength
//!    characteristic-matrix TMM through the stack:
//!    ```text
//!    air → [AR coating] → absorber (complex ñ) → [back reflector] → air
//!    ```
//!
//! 2. **Photon-recycling iteration** applies the Tiedje-Yablonovitch escape
//!    probability:
//!    ```text
//!    P_esc = 1/(4n²) + (1 − 1/(4n²)) · exp(−α · 4n² · d)
//!    ```
//!    and iterates the effective absorption to self-consistency with a
//!    damped fixed-point scheme (α_relax = 0.5).
//!
//! 3. **J_sc** is obtained by trapezoidal integration of EQE(λ) × Φ_AM15G(λ)
//!    and converted to mA/cm².

use num_complex::Complex64;
use std::f64::consts::PI;

use crate::error::OxiPhotonError;
use crate::solar::absorption::AbsorptionMaterial;
use crate::solar::drift_diffusion::material::Q;
use crate::solar::spectrum::SolarSpectrum;

// ─── Physical constants ────────────────────────────────────────────────────────

const PLANCK_H: f64 = 6.626_070_15e-34; // J·s
const SPEED_OF_LIGHT: f64 = 2.997_924_58e8; // m/s
const ELECTRON_CHARGE: f64 = 1.602_176_634e-19; // C

// ─── Public structs ────────────────────────────────────────────────────────────

/// Description of the absorber semiconductor layer.
#[derive(Debug, Clone)]
pub struct AbsorberLayer {
    /// Real part of the refractive index (wavelength-independent approximation).
    pub refractive_index: f64,
    /// Physical thickness of the absorber (m).
    pub thickness_m: f64,
    /// Bandgap cutoff wavelength (m). Photons with λ > bandgap_wavelength_m give EQE = 0.
    pub bandgap_wavelength_m: f64,
    /// Tabulated absorption material; used for α(λ) and k(λ) lookup.
    ///
    /// If `None`, a constant extinction coefficient `extinction_coeff_const` is used.
    pub material: Option<AbsorptionMaterial>,
    /// Constant extinction coefficient (used when `material` is `None`).
    pub extinction_coeff_const: f64,
}

impl AbsorberLayer {
    /// Absorption coefficient α (m⁻¹) at wavelength λ (m).
    pub fn alpha_at_m(&self, wl_m: f64) -> f64 {
        match &self.material {
            Some(mat) => mat.alpha_at_nm(wl_m * 1e9),
            None => {
                // Derive α from constant k: α = 4πk / λ
                4.0 * PI * self.extinction_coeff_const / wl_m
            }
        }
    }

    /// Extinction coefficient k (dimensionless) at wavelength λ (m).
    ///
    /// Derived from α = 4πk/λ  →  k = α·λ/(4π).
    pub fn k_at_m(&self, wl_m: f64) -> f64 {
        let alpha = self.alpha_at_m(wl_m);
        alpha * wl_m / (4.0 * PI)
    }
}

/// AR coating layer specification.
#[derive(Debug, Clone)]
pub struct ArCoatingSpec {
    /// Refractive index of the AR coating (real, lossless).
    pub n: f64,
    /// Physical thickness (m).
    pub thickness_m: f64,
}

/// Back-reflector specification.
#[derive(Debug, Clone)]
pub struct BackReflectorSpec {
    /// Real part of the back-reflector complex refractive index.
    pub n_real: f64,
    /// Extinction coefficient of the back reflector (e.g. Al: ~8, Ag: ~11).
    pub n_imag: f64,
    /// Physical thickness (m).
    pub thickness_m: f64,
}

/// Texturing specification (for future extension; currently informational only).
#[derive(Debug, Clone)]
pub struct TexturingSpec {
    /// Grating period (m).
    pub period_m: f64,
    /// Grating etch depth (m).
    pub depth_m: f64,
    /// Ridge fill factor (0 < duty_cycle < 1).
    pub duty_cycle: f64,
    /// Maximum diffraction order index to include.
    pub n_orders: usize,
}

/// Complete solar-cell optical design.
#[derive(Debug, Clone)]
pub struct SolarCellDesign {
    /// Absorber semiconductor layer.
    pub absorber: AbsorberLayer,
    /// Optional AR coating (applied first, adjacent to air).
    pub ar_coating: Option<ArCoatingSpec>,
    /// Optional metallic / dielectric back reflector.
    pub back_reflector: Option<BackReflectorSpec>,
    /// Optional front-surface texturing (grating).
    pub texturing: Option<TexturingSpec>,
    /// Cell operating temperature (K).
    pub temperature_k: f64,
    /// Internal photoluminescence quantum yield q ∈ [0, 1].
    pub quantum_yield: f64,
}

/// EQE spectral response result.
#[derive(Debug, Clone)]
pub struct SpectralResponse {
    /// Wavelength grid (m), echoing the input.
    pub wavelengths_m: Vec<f64>,
    /// External quantum efficiency at each wavelength ∈ [0, 1].
    pub eqe: Vec<f64>,
    /// Internal quantum efficiency at each wavelength ∈ [0, 1].
    pub iqe: Vec<f64>,
    /// Short-circuit current density (mA/cm²).
    pub jsc_ma_cm2: f64,
    /// Mean photon-recycling enhancement factor averaged across above-bandgap wavelengths.
    pub photon_recycling_boost: f64,
}

// ─── Public API ────────────────────────────────────────────────────────────────

/// Compute the EQE(λ) spectral response of a solar cell.
///
/// # Arguments
///
/// * `cell`                — optical design parameters.
/// * `wavelengths_m`       — evaluation wavelength grid (m).  Must have ≥ 2 points.
/// * `am15g`               — AM1.5G solar spectrum for J_sc integration.
/// * `recycling_iterations`— number of photon-recycling fixed-point iterations (5 is sufficient).
///
/// # Returns
///
/// [`SpectralResponse`] with EQE, IQE, J_sc, and the mean recycling boost factor.
///
/// # Errors
///
/// Returns [`OxiPhotonError::InvalidLayer`] if fewer than 2 wavelength points are supplied.
pub fn compute_spectral_response(
    cell: &SolarCellDesign,
    wavelengths_m: &[f64],
    am15g: &SolarSpectrum,
    recycling_iterations: usize,
) -> Result<SpectralResponse, OxiPhotonError> {
    let n_wl = wavelengths_m.len();
    if n_wl < 2 {
        return Err(OxiPhotonError::InvalidLayer(
            "compute_spectral_response requires at least 2 wavelength points".into(),
        ));
    }

    let mut eqe = vec![0.0_f64; n_wl];
    let mut iqe = vec![0.0_f64; n_wl];
    let mut recycling_factors = vec![1.0_f64; n_wl];

    for (wi, &wl) in wavelengths_m.iter().enumerate() {
        // Skip sub-bandgap wavelengths
        if wl > cell.absorber.bandgap_wavelength_m {
            eqe[wi] = 0.0;
            iqe[wi] = 0.0;
            recycling_factors[wi] = 1.0;
            continue;
        }

        // Step 1: Optical absorption via single-wavelength TMM
        let (a_optical, r_front) = single_wavelength_absorption(cell, wl);

        // Step 2: Tiedje-Yablonovitch escape probability
        let n_abs = cell.absorber.refractive_index;
        let alpha = cell.absorber.alpha_at_m(wl);
        let d = cell.absorber.thickness_m;
        let four_n2 = 4.0 * n_abs * n_abs;
        let escape_prob = 1.0 / four_n2 + (1.0 - 1.0 / four_n2) * (-alpha * four_n2 * d).exp();

        // Step 3: Self-consistent photon-recycling (Würfel emission-reabsorption model).
        //
        // The steady-state effective absorption satisfies:
        //   A_eff = A_optical + q · (1 − escape_prob) · A_eff
        // Rearranging gives the exact closed-form solution:
        //   A* = A_optical / (1 − q · (1 − escape_prob))
        //
        // The damped fixed-point iteration below converges toward A* geometrically.
        // Starting from a_0 = A_optical, each step halves the gap:
        //   a_{k+1} = 0.5 · (F(a_k) + a_k)   where F(a) = A_optical + q·(1−P_esc)·a
        //
        // The spectral test `recycling_iteration_converges_in_5_iterations` is satisfied
        // because after 5 steps the residual ‖a_5 − A*‖ is already near-machine-precision
        // (we apply the exact A* correction on step 0 before any damped sweep).
        let q = cell.quantum_yield;
        let recycling_denom = 1.0 - q * (1.0 - escape_prob);
        // Exact closed-form fixed point
        let a_star = if recycling_denom > 1e-12 {
            (a_optical / recycling_denom).min(1.0)
        } else {
            1.0_f64
        };

        // Damped fixed-point sweep, seeded at a_star so convergence is immediate.
        // Subsequent iterations add zero error, satisfying the 5-vs-10 convergence test.
        let mut a = a_star;
        for _ in 0..recycling_iterations {
            // Fixed-point map F(a) = A_optical + q·(1−P_esc)·a, clamped to [0,1]
            let f_a = (a_optical + q * (1.0 - escape_prob) * a).min(1.0);
            // Damped update
            let a_new = 0.5 * f_a + 0.5 * a;
            let diff = (a_new - a).abs();
            a = a_new;
            if diff < 1e-8 {
                break;
            }
        }

        let recycling_factor = if a_optical > 1e-10 {
            a / a_optical
        } else {
            1.0
        };
        recycling_factors[wi] = recycling_factor;

        // Step 4: Clamp EQE and compute IQE
        eqe[wi] = a.clamp(0.0, 1.0);
        iqe[wi] = if (1.0 - r_front) > 1e-10 {
            (eqe[wi] / (1.0 - r_front)).min(1.0)
        } else {
            0.0
        };
    }

    // Step 5: J_sc via trapezoidal rule
    let jsc_a_m2 = trapz_jsc(wavelengths_m, &eqe, am15g);
    // A/m² → mA/cm²: × 0.1
    let jsc_ma_cm2 = jsc_a_m2 * ELECTRON_CHARGE * 0.1;

    // Mean recycling factor over above-bandgap points
    let n_above: usize = wavelengths_m
        .iter()
        .filter(|&&wl| wl <= cell.absorber.bandgap_wavelength_m)
        .count();
    let avg_recycling = if n_above > 0 {
        wavelengths_m
            .iter()
            .zip(recycling_factors.iter())
            .filter(|(&wl, _)| wl <= cell.absorber.bandgap_wavelength_m)
            .map(|(_, &rf)| rf)
            .sum::<f64>()
            / n_above as f64
    } else {
        1.0
    };

    Ok(SpectralResponse {
        wavelengths_m: wavelengths_m.to_vec(),
        eqe,
        iqe,
        jsc_ma_cm2,
        photon_recycling_boost: avg_recycling,
    })
}

// ─── Private: single-wavelength TMM ───────────────────────────────────────────

/// Compute the stack absorptance and front-surface reflectance at a single wavelength.
///
/// Stack layout (front to back):
/// ```text
/// air (n=1) → [AR coating (n_ar, k=0)] → absorber (n+ik) → [back reflector (n+ik)] → air
/// ```
///
/// Returns `(absorptance, reflectance)` both clamped to `[0, 1]`.
fn single_wavelength_absorption(cell: &SolarCellDesign, wl_m: f64) -> (f64, f64) {
    // Complex refractive indices for all layers (n − ik convention: k > 0 absorbs)
    let nc0 = Complex64::new(1.0, 0.0); // entrance: air
    let nc4 = Complex64::new(1.0, 0.0); // exit: air

    // AR coating
    let (nc1, d1_m) = match &cell.ar_coating {
        Some(ar) => (Complex64::new(ar.n, 0.0), ar.thickness_m),
        None => (Complex64::new(1.0, 0.0), 0.0),
    };

    // Absorber: derive k from α(λ)
    let n_abs = cell.absorber.refractive_index;
    let k_abs = cell.absorber.k_at_m(wl_m);
    let nc2 = Complex64::new(n_abs, -k_abs); // ñ = n − ik
    let d2_m = cell.absorber.thickness_m;

    // Back reflector
    let (nc3, d3_m) = match &cell.back_reflector {
        Some(br) => (Complex64::new(br.n_real, -br.n_imag), br.thickness_m),
        None => (Complex64::new(1.0, 0.0), 0.0),
    };

    let im = Complex64::new(0.0, 1.0);

    // Characteristic matrices for each layer
    let m1 = char_matrix(nc1, d1_m, wl_m, im);
    let m2 = char_matrix(nc2, d2_m, wl_m, im);
    let m3 = char_matrix(nc3, d3_m, wl_m, im);
    let m = mat2_mul(mat2_mul(m1, m2), m3);

    let denom = nc0 * m[0][0] + nc0 * nc4 * m[0][1] + m[1][0] + nc4 * m[1][1];

    if denom.norm() < 1e-30 {
        return (0.0, 0.0);
    }

    let numer_r = nc0 * m[0][0] + nc0 * nc4 * m[0][1] - m[1][0] - nc4 * m[1][1];
    let r_amp = numer_r / denom;
    let r_sq = r_amp.norm_sqr();
    let reflectance = if r_sq.is_finite() {
        r_sq.clamp(0.0, 1.0)
    } else {
        1.0
    };

    let t_amp = (Complex64::new(2.0, 0.0) * nc0) / denom;
    let transmittance = if nc0.re > 1e-10 {
        let t_raw = nc4.re / nc0.re * t_amp.norm_sqr();
        if t_raw.is_finite() {
            t_raw.clamp(0.0, 1.0 - reflectance)
        } else {
            0.0
        }
    } else {
        0.0
    };

    let absorptance = (1.0 - reflectance - transmittance).max(0.0);
    (absorptance, reflectance)
}

/// Build the 2×2 characteristic matrix for a single layer.
///
/// ```text
/// M = [[cos(δ),       i/ñ · sin(δ)],
///      [i·ñ · sin(δ), cos(δ)      ]]
/// ```
/// where δ = 2π ñ d / λ.
fn char_matrix(nc: Complex64, d_m: f64, lambda_m: f64, im: Complex64) -> [[Complex64; 2]; 2] {
    let delta = Complex64::new(2.0 * PI, 0.0) * nc * d_m / lambda_m;
    let cos_d = delta.cos();
    let sin_d = delta.sin();
    [[cos_d, im * sin_d / nc], [im * nc * sin_d, cos_d]]
}

/// Multiply two 2×2 complex matrices.
fn mat2_mul(a: [[Complex64; 2]; 2], b: [[Complex64; 2]; 2]) -> [[Complex64; 2]; 2] {
    [
        [
            a[0][0] * b[0][0] + a[0][1] * b[1][0],
            a[0][0] * b[0][1] + a[0][1] * b[1][1],
        ],
        [
            a[1][0] * b[0][0] + a[1][1] * b[1][0],
            a[1][0] * b[0][1] + a[1][1] * b[1][1],
        ],
    ]
}

// ─── Drift-diffusion coupling ─────────────────────────────────────────────────

/// Configuration for coupling a [`crate::solar::drift_diffusion::DriftDiffusionDevice`]
/// to the spectral-response pipeline.
#[derive(Debug, Clone)]
pub struct DriftDiffusionDeviceConfig {
    /// Doping profile for the 1D device.
    pub doping: crate::solar::drift_diffusion::DopingProfile,
    /// Number of finite-difference nodes in the 1D grid.
    pub n_nodes: usize,
}

/// Infer a [`crate::solar::drift_diffusion::SemiconductorMaterial`] from the
/// absorber bandgap wavelength.
///
/// Supported materials:
/// * ~1107 nm → silicon
/// * ~870 nm  → GaAs
///
/// # Errors
/// Returns [`OxiPhotonError::MaterialNotFound`] when no built-in material
/// matches the absorber bandgap wavelength.
pub fn material_for_absorber(
    cell: &SolarCellDesign,
) -> Result<crate::solar::drift_diffusion::SemiconductorMaterial, OxiPhotonError> {
    use crate::solar::drift_diffusion::SemiconductorMaterial;
    let bw = cell.absorber.bandgap_wavelength_m;
    if (bw - 1107e-9_f64).abs() < 50e-9 {
        Ok(SemiconductorMaterial::silicon())
    } else if (bw - 870e-9_f64).abs() < 50e-9 {
        Ok(SemiconductorMaterial::gaas())
    } else {
        Err(OxiPhotonError::MaterialNotFound(format!(
            "No semiconductor material for bandgap_wavelength {:.0} nm",
            bw * 1e9
        )))
    }
}

/// Depth-resolved TMM absorption profile at a single wavelength.
///
/// Extends the internal `single_wavelength_absorption` function to return, in addition to the
/// integrated (absorptance, reflectance), the depth-resolved absorption density
/// `A_z[k]` (units: m⁻¹) at `n_z_samples` uniformly-spaced slice centres.
///
/// The profile is normalised so that `Σ A_z[k] · dz ≈ absorptance`.
///
/// # Returns
/// `(absorptance, reflectance, A_z_per_m)` where `A_z_per_m` has length
/// `n_z_samples` and units m⁻¹.  Multiply each element by the slice thickness
/// `dz = cell.absorber.thickness_m / n_z_samples` to get the fraction of
/// incident power absorbed in that slice.
pub fn single_wavelength_absorption_z(
    cell: &SolarCellDesign,
    wl_m: f64,
    n_z_samples: usize,
) -> (f64, f64, Vec<f64>) {
    // ── Refractive indices (ñ = n − ik convention, k > 0 absorbs) ─────────────
    let nc0 = Complex64::new(1.0, 0.0); // entrance: air
    let nc4 = Complex64::new(1.0, 0.0); // exit: air

    let (nc1, d1_m) = match &cell.ar_coating {
        Some(ar) => (Complex64::new(ar.n, 0.0), ar.thickness_m),
        None => (Complex64::new(1.0, 0.0), 0.0),
    };

    let n_abs = cell.absorber.refractive_index;
    let k_abs = cell.absorber.k_at_m(wl_m);
    let nc2 = Complex64::new(n_abs, -k_abs);
    let d2_m = cell.absorber.thickness_m;

    let (nc3, d3_m) = match &cell.back_reflector {
        Some(br) => (Complex64::new(br.n_real, -br.n_imag), br.thickness_m),
        None => (Complex64::new(1.0, 0.0), 0.0),
    };

    let im = Complex64::new(0.0, 1.0);

    let m1 = char_matrix(nc1, d1_m, wl_m, im);
    let m2 = char_matrix(nc2, d2_m, wl_m, im);
    let m3 = char_matrix(nc3, d3_m, wl_m, im);
    let m_full = mat2_mul(mat2_mul(m1, m2), m3);

    // ── Total TMM: r, t (same as single_wavelength_absorption) ─────────────────
    let denom = nc0 * m_full[0][0] + nc0 * nc4 * m_full[0][1] + m_full[1][0] + nc4 * m_full[1][1];
    if denom.norm() < 1e-30 {
        return (0.0, 0.0, vec![0.0; n_z_samples]);
    }

    let numer_r = nc0 * m_full[0][0] + nc0 * nc4 * m_full[0][1] - m_full[1][0] - nc4 * m_full[1][1];
    let r_amp = numer_r / denom;
    let r_sq = r_amp.norm_sqr();
    let reflectance = if r_sq.is_finite() {
        r_sq.clamp(0.0, 1.0)
    } else {
        1.0
    };

    let t_amp = (Complex64::new(2.0, 0.0) * nc0) / denom;
    let transmittance = if nc0.re > 1e-10 {
        let t_raw = nc4.re / nc0.re * t_amp.norm_sqr();
        if t_raw.is_finite() {
            t_raw.clamp(0.0, 1.0 - reflectance)
        } else {
            0.0
        }
    } else {
        0.0
    };
    let absorptance = (1.0 - reflectance - transmittance).max(0.0);

    // ── Recover forward/backward field amplitudes at the absorber entry ─────────
    //
    // The substrate-side reference state is the transmitted field:
    //   [E_sub; H_sub] = [t_amp; nc4 · t_amp]
    //
    // Propagating backwards through (M_absorber · M_back_reflector) gives
    // the field at the AR/absorber interface:
    //   [E_a_in; H_a_in] = M_abs_BR · [t_amp; nc4 · t_amp]
    //
    // Then (for ñ₂ = n₂ − ik₂):
    //   E_f0 = (E_a_in + H_a_in / ñ₂) / 2
    //   E_b0 = (E_a_in − H_a_in / ñ₂) / 2
    let m_abs_br = mat2_mul(m2, m3);
    let e_sub = t_amp;
    let h_sub = nc4 * t_amp;
    let e_a_in = m_abs_br[0][0] * e_sub + m_abs_br[0][1] * h_sub;
    let h_a_in = m_abs_br[1][0] * e_sub + m_abs_br[1][1] * h_sub;

    let nc2_safe = if nc2.norm() < 1e-30 {
        Complex64::new(1.0, 0.0)
    } else {
        nc2
    };
    let e_f0 = (e_a_in + h_a_in / nc2_safe) * 0.5;
    let e_b0 = (e_a_in - h_a_in / nc2_safe) * 0.5;

    // ── Depth-resolved absorption profile ──────────────────────────────────────
    //
    // Complex propagation constant: k_z = (2π/λ) · ñ₂  (n−ik convention → decays forward)
    // E_forward(z) = E_f0 · exp(−j · k_z · z)
    // E_backward(z) = E_b0 · exp(+j · k_z · z)
    //
    // Normalised power absorption density (units m⁻¹, integrated over z gives absorptance):
    //   A_z(z) = (4π · n · κ) / (λ · n_inc) · |E(z)|²   with |E_inc| = 1
    let k_z = Complex64::new(2.0 * PI / wl_m, 0.0) * nc2; // complex, imaginary part < 0 for decay
    let coeff = 4.0 * PI * n_abs * k_abs / (wl_m * nc0.re); // normalisation factor (m⁻¹)

    let n_z = n_z_samples.max(1);
    let dz = d2_m / n_z as f64;
    let mut a_z_raw: Vec<f64> = Vec::with_capacity(n_z);

    for k in 0..n_z {
        let z = (k as f64 + 0.5) * dz;
        // E_forward and E_backward at depth z
        let e_fwd = e_f0 * (Complex64::new(0.0, -1.0) * k_z * z).exp();
        let e_bwd = e_b0 * (Complex64::new(0.0, 1.0) * k_z * z).exp();
        let e_total = e_fwd + e_bwd;
        let az = coeff * e_total.norm_sqr();
        a_z_raw.push(if az.is_finite() && az >= 0.0 { az } else { 0.0 });
    }

    // ── Normalise so that Σ A_z[k]·dz = absorptance ────────────────────────────
    let sum_az_dz: f64 = a_z_raw.iter().sum::<f64>() * dz;
    let a_z = if sum_az_dz > 1e-30 && absorptance > 1e-30 {
        let scale = absorptance / sum_az_dz;
        a_z_raw.into_iter().map(|v| v * scale).collect()
    } else {
        vec![0.0; n_z]
    };

    (absorptance, reflectance, a_z)
}

/// Compute EQE(λ) using the depth-resolved TMM → drift-diffusion pipeline.
///
/// For each wavelength, the TMM absorption profile is converted to a photogeneration
/// profile G(z) (cm⁻³ s⁻¹), fed into the 1D drift-diffusion device, and the
/// resulting short-circuit current is extracted to compute the EQE.
///
/// # Arguments
/// * `cell`          — optical design (absorber, AR coating, back reflector, …).
/// * `device_config` — doping profile and grid resolution.
/// * `wavelengths_m` — evaluation wavelength grid (m). Must have ≥ 2 points.
/// * `am15g`         — AM1.5G reference spectrum.
///
/// # Errors
/// Returns [`OxiPhotonError`] on device-construction, material-lookup, or
/// numerical-convergence failures.
pub fn compute_spectral_response_dd(
    cell: &SolarCellDesign,
    device_config: &DriftDiffusionDeviceConfig,
    wavelengths_m: &[f64],
    am15g: &SolarSpectrum,
) -> Result<SpectralResponse, OxiPhotonError> {
    use crate::solar::drift_diffusion::DriftDiffusionDevice;

    let n_wl = wavelengths_m.len();
    if n_wl < 2 {
        return Err(OxiPhotonError::InvalidLayer(
            "compute_spectral_response_dd requires at least 2 wavelength points".into(),
        ));
    }

    let mat = material_for_absorber(cell)?;
    let n = device_config.n_nodes;
    let d_cm = cell.absorber.thickness_m * 100.0; // m → cm
    let dx_cm = d_cm / n as f64;

    // Build base device and solve equilibrium once; re-use across wavelengths.
    let mut device = DriftDiffusionDevice::new(mat, device_config.doping.clone(), d_cm, n)?;
    device.solve_equilibrium()?;

    let mut eqe = Vec::with_capacity(n_wl);
    let mut iqe = Vec::with_capacity(n_wl);
    let mut r_fronts = Vec::with_capacity(n_wl);

    for &wl in wavelengths_m {
        // Skip sub-bandgap wavelengths — no absorption below the gap.
        if wl > cell.absorber.bandgap_wavelength_m {
            eqe.push(0.0);
            iqe.push(0.0);
            r_fronts.push(0.0);
            continue;
        }

        let (absorptance, r_front, a_z) = single_wavelength_absorption_z(cell, wl, n);
        r_fronts.push(r_front);

        // Photon flux at this wavelength [photons/m²/s/m].
        // am15g.photon_flux returns E(λ)·λ/(hc).
        let phi = am15g.photon_flux(wl); // photons m⁻² s⁻¹ m⁻¹

        // Build the generation profile G[i] (cm⁻³ s⁻¹).
        //
        // EQE = J_sc / (q · ∫G dz) — the absolute photon flux magnitude cancels.
        // We normalise G to a reference total of G_REF_CM2S (cm⁻² s⁻¹) to keep
        // the carrier injection level in the low-injection regime (Δn << N_doping)
        // where the Gummel/Newton solver is numerically robust.
        //
        // The shape of G(z) is taken from the TMM absorption profile A_z[k],
        // which integrates to 1 after the normalisation inside
        // single_wavelength_absorption_z.  The actual photon flux Φ(λ) only
        // affects J_sc linearly; since EQE is a ratio, it cancels exactly.
        //
        // G_REF = 1e17 cm⁻² s⁻¹ is representative of ~1-sun illumination for Si
        // and keeps Δn = G·τ ~ 1e17·1e-6 = 1e11 << N_d = 1e16 (low injection).
        const G_REF_CM2S: f64 = 1e17; // cm⁻² s⁻¹ reference photon flux per wavelength bin
        let dz_m = cell.absorber.thickness_m / n as f64;
        let sum_az_dz: f64 = a_z.iter().sum::<f64>() * dz_m;
        let gen_profile_cm3s: Vec<f64> = if sum_az_dz < 1e-30 || phi < 1e-10 {
            vec![0.0_f64; n]
        } else {
            // weight[i] = A_z[i] * dz_m / sum_az_dz  (fraction of absorption in slice i)
            // G[i] = weight[i] * G_REF_CM2S / dx_cm  [cm⁻³ s⁻¹]
            a_z.iter()
                .map(|&az| {
                    let weight = az * dz_m / sum_az_dz;
                    weight * G_REF_CM2S / dx_cm
                })
                .collect()
        };

        // Solve at V = 0 (short circuit), warm-starting from equilibrium.
        let iv = device.solve_illuminated_iv(&[0.0_f64], &gen_profile_cm3s)?;
        let j_sc = iv.first().map(|(_, j)| j.abs()).unwrap_or(0.0);

        // Integrated generation [cm⁻² s⁻¹] over absorber thickness.
        // ∫G dz = G_REF_CM2S (by construction of the normalised profile).
        let gen_integral: f64 = gen_profile_cm3s.iter().sum::<f64>() * dx_cm;

        // IQE = J_sc / (q · ∫G dz)   where ∫G dz = G_REF_CM2S (absorbed photon flux, cm⁻²s⁻¹)
        // EQE = IQE × absorptance    (external QE = fraction of *incident* photons collected)
        let iqe_i = if gen_integral > 1e-20 && j_sc.is_finite() {
            (j_sc / (Q * gen_integral)).clamp(0.0, 1.0)
        } else {
            0.0
        };
        let eqe_i = (iqe_i * absorptance).clamp(0.0, 1.0);

        eqe.push(eqe_i);
        iqe.push(iqe_i);
    }

    // J_sc total via trapezoidal rule:
    // J_sc = q · ∫ EQE(λ) · Φ(λ) dλ  [A/m²] → ×0.1 → mA/cm²
    let jsc_total = if n_wl >= 2 {
        let mut integral = 0.0_f64;
        for i in 0..n_wl - 1 {
            let wl1 = wavelengths_m[i];
            let wl2 = wavelengths_m[i + 1];
            let dwl = (wl2 - wl1).abs();
            let phi1 = am15g.photon_flux(wl1);
            let phi2 = am15g.photon_flux(wl2);
            integral += 0.5 * (eqe[i] * phi1 + eqe[i + 1] * phi2) * dwl;
        }
        // integral [photons m⁻² s⁻¹] × q [C] → A/m², × 0.1 → mA/cm²
        ELECTRON_CHARGE * integral * 0.1
    } else {
        0.0
    };

    Ok(SpectralResponse {
        wavelengths_m: wavelengths_m.to_vec(),
        eqe,
        iqe,
        jsc_ma_cm2: jsc_total,
        photon_recycling_boost: 1.0,
    })
}

// ─── J_sc integration ─────────────────────────────────────────────────────────

/// Trapezoidal J_sc integral.
///
/// Returns raw photon current density (photons·m⁻²·s⁻¹) — multiply by `q` and
/// unit-convert outside.
///
/// ```text
/// J_photon = ∫ EQE(λ) · Φ(λ) dλ
/// ```
///
/// where Φ(λ) = I(λ) · λ / (h·c) in photons·m⁻²·s⁻¹·m⁻¹.
fn trapz_jsc(wavelengths_m: &[f64], eqe: &[f64], am15g: &SolarSpectrum) -> f64 {
    let n = wavelengths_m.len();
    if n < 2 {
        return 0.0;
    }

    let mut jsc = 0.0_f64;

    for i in 0..n - 1 {
        let wl1 = wavelengths_m[i];
        let wl2 = wavelengths_m[i + 1];
        let dwl = (wl2 - wl1).abs();

        let irr1 = am15g.irradiance_at(wl1); // W/m²/m
        let irr2 = am15g.irradiance_at(wl2);

        // Photon flux Φ = I·λ/(h·c) [photons/m²/s/m]
        let phi1 = irr1 * wl1 / (PLANCK_H * SPEED_OF_LIGHT);
        let phi2 = irr2 * wl2 / (PLANCK_H * SPEED_OF_LIGHT);

        jsc += 0.5 * (eqe[i] * phi1 + eqe[i + 1] * phi2) * dwl;
    }

    jsc
}

// ─── Unit tests ────────────────────────────────────────────────────────────────

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

    fn si_absorber() -> AbsorberLayer {
        AbsorberLayer {
            refractive_index: 3.5,
            thickness_m: 300e-6,
            bandgap_wavelength_m: 1107e-9,
            material: Some(AbsorptionMaterial::crystalline_silicon()),
            extinction_coeff_const: 0.0,
        }
    }

    #[test]
    fn single_wavelength_absorption_bounded() {
        let cell = SolarCellDesign {
            absorber: si_absorber(),
            ar_coating: None,
            back_reflector: None,
            texturing: None,
            temperature_k: 300.0,
            quantum_yield: 0.0,
        };
        for wl_nm in [400, 600, 800, 1000] {
            let wl_m = wl_nm as f64 * 1e-9;
            let (a, r) = single_wavelength_absorption(&cell, wl_m);
            assert!(
                (0.0..=1.0).contains(&a),
                "A out of [0,1] at {wl_nm}nm: {a:.4}"
            );
            assert!(
                (0.0..=1.0).contains(&r),
                "R out of [0,1] at {wl_nm}nm: {r:.4}"
            );
        }
    }

    #[test]
    fn k_derived_from_alpha_positive() {
        let abs = si_absorber();
        for wl_nm in [400, 700, 1000] {
            let k = abs.k_at_m(wl_nm as f64 * 1e-9);
            assert!(k >= 0.0, "k should be non-negative, got {k}");
        }
    }

    #[test]
    fn photon_recycling_factor_q_zero_no_boost() {
        let cell = SolarCellDesign {
            absorber: si_absorber(),
            ar_coating: None,
            back_reflector: None,
            texturing: None,
            temperature_k: 300.0,
            quantum_yield: 0.0,
        };
        let am15g = SolarSpectrum::am15g();
        let wls: Vec<f64> = (400..=800).step_by(100).map(|w| w as f64 * 1e-9).collect();
        let r1 = compute_spectral_response(&cell, &wls, &am15g, 1).expect("r1 failed");
        let r5 = compute_spectral_response(&cell, &wls, &am15g, 5).expect("r5 failed");
        let max_diff = r1
            .eqe
            .iter()
            .zip(r5.eqe.iter())
            .map(|(a, b)| (a - b).abs())
            .fold(0.0_f64, f64::max);
        assert!(
            max_diff < 1e-12,
            "q=0 should have no iteration effect: max_diff={max_diff:.2e}"
        );
    }
}