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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
//! Near-to-far-field (NTFF) transforms for FDTD.
//!
//! Provides 2D and 3D near-to-far-field transforms using the field equivalence principle.
//! Records tangential E and H fields on a closed Huygens surface and computes
//! far-field radiation patterns via DFT integration.
//!
//! References:
//!   - Taflove & Hagness, "Computational Electrodynamics", 3rd Ed., Ch. 8
//!   - Balanis, "Antenna Theory", 3rd Ed.

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

// Speed of light (m/s)
const C: f64 = 2.997_924_58e8;

// ─────────────────────────────────────────────────────────────────
// 2D NTFF (original)
// ─────────────────────────────────────────────────────────────────

/// Near-to-far-field (NTFF) transform for 2D TE FDTD.
///
/// Uses the field equivalence principle (Huygens surface):
/// Records tangential E and H fields on a closed rectangular contour
/// and computes the far-field radiation pattern via the DFT.
///
/// For 2D TE (Hz, Ex, Ey), the far-field is computed as a function of
/// angle θ ∈ [0, 2π) from the DFT of the surface fields.
///
/// Reference: Taflove & Hagness, "Computational Electrodynamics", Ch. 8.
pub struct NearToFarField2d {
    /// Number of x cells in the main grid
    pub nx: usize,
    /// Number of y cells
    pub ny: usize,
    pub dx: f64,
    pub dy: f64,

    /// NTFF box boundaries (in cell indices)
    pub i_min: usize,
    pub i_max: usize,
    pub j_min: usize,
    pub j_max: usize,

    /// Angular frequency of interest (rad/s)
    pub omega: f64,

    // DFT accumulators on the four sides (complex)
    /// Bottom (j=j_min): Hz and Ex DFTs along i direction
    hz_bot: Vec<Complex64>,
    ex_bot: Vec<Complex64>,

    /// Top (j=j_max): Hz and Ex DFTs along i direction
    hz_top: Vec<Complex64>,
    ex_top: Vec<Complex64>,

    /// Left (i=i_min): Hz and Ey DFTs along j direction
    hz_left: Vec<Complex64>,
    ey_left: Vec<Complex64>,

    /// Right (i=i_max): Hz and Ey DFTs along j direction
    hz_right: Vec<Complex64>,
    ey_right: Vec<Complex64>,
}

impl NearToFarField2d {
    /// Create NTFF monitor recording at a single frequency `omega` (rad/s).
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        nx: usize,
        ny: usize,
        dx: f64,
        dy: f64,
        i_min: usize,
        i_max: usize,
        j_min: usize,
        j_max: usize,
        omega: f64,
    ) -> Self {
        let ni = i_max - i_min + 1;
        let nj = j_max - j_min + 1;
        Self {
            nx,
            ny,
            dx,
            dy,
            i_min,
            i_max,
            j_min,
            j_max,
            omega,
            hz_bot: vec![Complex64::new(0.0, 0.0); ni],
            ex_bot: vec![Complex64::new(0.0, 0.0); ni],
            hz_top: vec![Complex64::new(0.0, 0.0); ni],
            ex_top: vec![Complex64::new(0.0, 0.0); ni],
            hz_left: vec![Complex64::new(0.0, 0.0); nj],
            ey_left: vec![Complex64::new(0.0, 0.0); nj],
            hz_right: vec![Complex64::new(0.0, 0.0); nj],
            ey_right: vec![Complex64::new(0.0, 0.0); nj],
        }
    }

    /// Accumulate field values at time `t` with time step `dt`.
    pub fn accumulate(&mut self, hz: &[f64], ex: &[f64], ey: &[f64], t: f64, dt: f64) {
        let phase = Complex64::new(0.0, self.omega * t).exp() * dt;

        // Bottom (j = j_min)
        let j = self.j_min;
        for (k, i) in (self.i_min..=self.i_max).enumerate() {
            if j < self.ny && i < self.nx {
                self.hz_bot[k] += hz[j * self.nx + i] * phase;
            }
            if j < self.ny && i <= self.nx {
                self.ex_bot[k] += ex[j * (self.nx + 1) + i] * phase;
            }
        }

        // Top (j = j_max)
        let j = self.j_max;
        for (k, i) in (self.i_min..=self.i_max).enumerate() {
            if j < self.ny && i < self.nx {
                self.hz_top[k] += hz[j * self.nx + i] * phase;
            }
            if j < self.ny && i <= self.nx {
                self.ex_top[k] += ex[j * (self.nx + 1) + i] * phase;
            }
        }

        // Left (i = i_min)
        let i = self.i_min;
        for (k, j) in (self.j_min..=self.j_max).enumerate() {
            if j < self.ny && i < self.nx {
                self.hz_left[k] += hz[j * self.nx + i] * phase;
            }
            if j <= self.ny && i < self.nx {
                self.ey_left[k] += ey[j * self.nx + i] * phase;
            }
        }

        // Right (i = i_max)
        let i = self.i_max;
        for (k, j) in (self.j_min..=self.j_max).enumerate() {
            if j < self.ny && i < self.nx {
                self.hz_right[k] += hz[j * self.nx + i] * phase;
            }
            if j <= self.ny && i < self.nx {
                self.ey_right[k] += ey[j * self.nx + i] * phase;
            }
        }
    }

    /// Compute far-field radiation pattern as a function of angle θ.
    pub fn radiation_pattern(&self, theta_deg_values: &[f64]) -> Vec<f64> {
        let k0 = self.omega / C;
        let ni = self.i_max - self.i_min + 1;
        let nj = self.j_max - self.j_min + 1;

        let cx = (self.i_min + self.i_max) as f64 / 2.0 * self.dx;
        let cy = (self.j_min + self.j_max) as f64 / 2.0 * self.dy;

        theta_deg_values
            .iter()
            .map(|&theta_deg| {
                let theta = theta_deg * PI / 180.0;
                let cos_t = theta.cos();
                let sin_t = theta.sin();
                let mut f = Complex64::new(0.0, 0.0);

                // Bottom side
                let y_bot = self.j_min as f64 * self.dy - cy;
                for k in 0..ni {
                    let x = (self.i_min + k) as f64 * self.dx - cx;
                    let r_dot = x * cos_t + y_bot * sin_t;
                    let ph = Complex64::new(0.0, k0 * r_dot).exp();
                    f += (self.ex_bot[k] - self.hz_bot[k]) * ph * self.dx;
                }

                // Top side
                let y_top = self.j_max as f64 * self.dy - cy;
                for k in 0..ni {
                    let x = (self.i_min + k) as f64 * self.dx - cx;
                    let r_dot = x * cos_t + y_top * sin_t;
                    let ph = Complex64::new(0.0, k0 * r_dot).exp();
                    f += (-self.ex_top[k] + self.hz_top[k]) * ph * self.dx;
                }

                // Left side
                let x_left = self.i_min as f64 * self.dx - cx;
                for k in 0..nj {
                    let y = (self.j_min + k) as f64 * self.dy - cy;
                    let r_dot = x_left * cos_t + y * sin_t;
                    let ph = Complex64::new(0.0, k0 * r_dot).exp();
                    f += (-self.ey_left[k] - self.hz_left[k]) * ph * self.dy;
                }

                // Right side
                let x_right = self.i_max as f64 * self.dx - cx;
                for k in 0..nj {
                    let y = (self.j_min + k) as f64 * self.dy - cy;
                    let r_dot = x_right * cos_t + y * sin_t;
                    let ph = Complex64::new(0.0, k0 * r_dot).exp();
                    f += (self.ey_right[k] + self.hz_right[k]) * ph * self.dy;
                }

                f.norm_sqr()
            })
            .collect()
    }

    /// Directivity index: ratio of max to mean power in radiation pattern.
    pub fn directivity(&self, n_angles: usize) -> f64 {
        let thetas: Vec<f64> = (0..n_angles)
            .map(|i| i as f64 / n_angles as f64 * 360.0)
            .collect();
        let pattern = self.radiation_pattern(&thetas);
        let max_p = pattern.iter().cloned().fold(0.0_f64, f64::max);
        let mean_p = pattern.iter().sum::<f64>() / n_angles as f64;
        if mean_p > 0.0 {
            max_p / mean_p
        } else {
            0.0
        }
    }
}

// ─────────────────────────────────────────────────────────────────
// 3D NTFF
// ─────────────────────────────────────────────────────────────────

/// 3D near-to-far-field transform.
///
/// Integrates tangential E and H on a closed rectangular surface around the source
/// and computes far-field radiation patterns in spherical coordinates (θ, φ).
///
/// The Huygens surface has 6 faces. On each face, we accumulate DFT of the
/// equivalent surface currents:
///   J  = n̂ × H  (electric surface current, A/m)
///   M  = -n̂ × E  (magnetic surface current, V/m)
///
/// The far-field electric potential functions (N, L) are computed by
/// integrating J and M over the surface with appropriate phase factors.
///
/// # Grid Layout
/// Fields: `field[i * ny * nz + j * nz + k]`
pub struct NearToFarField3d {
    /// Free-space wavelength (m) (computed from omega)
    pub wavelength: f64,
    /// Surface extent
    pub i0: usize,
    pub i1: usize,
    pub j0: usize,
    pub j1: usize,
    pub k0: usize,
    pub k1: usize,
    /// Angular frequency (rad/s)
    pub omega: f64,
    /// DFT accumulators for equivalent surface currents on 6 faces
    /// Each face stores complex J and M current components
    /// Face 0: k=k0 (bottom, normal=-Z), Face 1: k=k1 (top, normal=+Z)
    /// Face 2: j=j0 (front, normal=-Y), Face 3: j=j1 (back, normal=+Y)
    /// Face 4: i=i0 (left, normal=-X), Face 5: i=i1 (right, normal=+X)
    // Bottom face (k=k0): Jx, Jy (tangential J) and Mx, My
    jx_bot: Vec<Complex64>,
    jy_bot: Vec<Complex64>,
    mx_bot: Vec<Complex64>,
    my_bot: Vec<Complex64>,
    // Top face (k=k1)
    jx_top: Vec<Complex64>,
    jy_top: Vec<Complex64>,
    mx_top: Vec<Complex64>,
    my_top: Vec<Complex64>,
    // Front face (j=j0): Jx, Jz and Mx, Mz
    jx_fnt: Vec<Complex64>,
    jz_fnt: Vec<Complex64>,
    mx_fnt: Vec<Complex64>,
    mz_fnt: Vec<Complex64>,
    // Back face (j=j1)
    jx_bck: Vec<Complex64>,
    jz_bck: Vec<Complex64>,
    mx_bck: Vec<Complex64>,
    mz_bck: Vec<Complex64>,
    // Left face (i=i0): Jy, Jz and My, Mz
    jy_lft: Vec<Complex64>,
    jz_lft: Vec<Complex64>,
    my_lft: Vec<Complex64>,
    mz_lft: Vec<Complex64>,
    // Right face (i=i1)
    jy_rgt: Vec<Complex64>,
    jz_rgt: Vec<Complex64>,
    my_rgt: Vec<Complex64>,
    mz_rgt: Vec<Complex64>,
    /// Number of accumulation samples
    pub n_samples: usize,
    /// Simulation time step (s)
    pub dt: f64,
    /// Grid spacings (m)
    pub dx: f64,
    pub dy: f64,
    pub dz: f64,
    /// Grid dimensions
    pub nx: usize,
    pub ny: usize,
    pub nz: usize,
    // Face sizes (precomputed)
    ni: usize,
    nj: usize,
    nk: usize,
}

impl NearToFarField3d {
    /// Create a new 3D NTFF monitor.
    ///
    /// # Arguments
    /// * `i0..i1`, `j0..j1`, `k0..k1` — Huygens surface boundaries (inclusive)
    /// * `omega` — angular frequency to analyze (rad/s)
    /// * `dt` — simulation time step (s)
    /// * `dx`, `dy`, `dz` — grid spacings (m)
    /// * `nx`, `ny`, `nz` — grid dimensions
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        i0: usize,
        i1: usize,
        j0: usize,
        j1: usize,
        k0: usize,
        k1: usize,
        omega: f64,
        dt: f64,
        dx: f64,
        dy: f64,
        dz: f64,
        nx: usize,
        ny: usize,
        nz: usize,
    ) -> Self {
        let wavelength = 2.0 * PI * C / omega;
        let ni = i1.saturating_sub(i0) + 1;
        let nj = j1.saturating_sub(j0) + 1;
        let nk = k1.saturating_sub(k0) + 1;

        // Bottom/top face sizes: ni × nj cells
        let n_bot_top = ni * nj;
        // Front/back face sizes: ni × nk cells
        let n_fnt_bck = ni * nk;
        // Left/right face sizes: nj × nk cells
        let n_lft_rgt = nj * nk;

        let zero_vec = |n: usize| vec![Complex64::new(0.0, 0.0); n];

        Self {
            wavelength,
            i0,
            i1,
            j0,
            j1,
            k0,
            k1,
            omega,
            jx_bot: zero_vec(n_bot_top),
            jy_bot: zero_vec(n_bot_top),
            mx_bot: zero_vec(n_bot_top),
            my_bot: zero_vec(n_bot_top),
            jx_top: zero_vec(n_bot_top),
            jy_top: zero_vec(n_bot_top),
            mx_top: zero_vec(n_bot_top),
            my_top: zero_vec(n_bot_top),
            jx_fnt: zero_vec(n_fnt_bck),
            jz_fnt: zero_vec(n_fnt_bck),
            mx_fnt: zero_vec(n_fnt_bck),
            mz_fnt: zero_vec(n_fnt_bck),
            jx_bck: zero_vec(n_fnt_bck),
            jz_bck: zero_vec(n_fnt_bck),
            mx_bck: zero_vec(n_fnt_bck),
            mz_bck: zero_vec(n_fnt_bck),
            jy_lft: zero_vec(n_lft_rgt),
            jz_lft: zero_vec(n_lft_rgt),
            my_lft: zero_vec(n_lft_rgt),
            mz_lft: zero_vec(n_lft_rgt),
            jy_rgt: zero_vec(n_lft_rgt),
            jz_rgt: zero_vec(n_lft_rgt),
            my_rgt: zero_vec(n_lft_rgt),
            mz_rgt: zero_vec(n_lft_rgt),
            n_samples: 0,
            dt,
            dx,
            dy,
            dz,
            nx,
            ny,
            nz,
            ni,
            nj,
            nk,
        }
    }

    /// Flat index for 3D arrays: `field[i * ny * nz + j * nz + k]`
    #[inline]
    fn idx3(&self, i: usize, j: usize, k: usize) -> usize {
        i * self.ny * self.nz + j * self.nz + k
    }

    /// Accumulate surface current DFT from the current field state.
    ///
    /// On each face, computes J = n̂ × H and M = -n̂ × E and accumulates
    /// their running DFT at the monitor frequency.
    #[allow(clippy::too_many_arguments)]
    pub fn accumulate(
        &mut self,
        time_step: usize,
        ex: &[f64],
        ey: &[f64],
        ez: &[f64],
        hx: &[f64],
        hy: &[f64],
        hz: &[f64],
    ) {
        let t = time_step as f64 * self.dt;
        let phase = Complex64::new(0.0, self.omega * t).exp() * self.dt;

        // Helper: safe array access
        let get = |arr: &[f64], idx: usize| arr.get(idx).copied().unwrap_or(0.0);

        // ── Bottom face: k = k0, normal n̂ = -ẑ ───────────────────
        // J = n̂ × H = (-ẑ) × H = (-ẑ) × (Hx,Hy,Hz) = (Hy, -Hx, 0)
        // M = -n̂ × E = (ẑ) × E = (ẑ) × (Ex,Ey,Ez) = (-Ey, Ex, 0)
        let k = self.k0;
        if k < self.nz {
            for (ai, i) in (self.i0..=self.i1.min(self.nx.saturating_sub(1))).enumerate() {
                for (aj, j) in (self.j0..=self.j1.min(self.ny.saturating_sub(1))).enumerate() {
                    let idx = self.idx3(i, j, k);
                    let cell = ai * self.nj + aj;
                    if cell < self.jx_bot.len() {
                        self.jx_bot[cell] += get(hy, idx) * phase;
                        self.jy_bot[cell] += -get(hx, idx) * phase;
                        self.mx_bot[cell] += -get(ey, idx) * phase;
                        self.my_bot[cell] += get(ex, idx) * phase;
                    }
                }
            }
        }

        // ── Top face: k = k1, normal n̂ = +ẑ ─────────────────────
        // J = n̂ × H = ẑ × H = (-Hy, Hx, 0)
        // M = -n̂ × E = -ẑ × E = (Ey, -Ex, 0)
        let k = self.k1.min(self.nz.saturating_sub(1));
        for (ai, i) in (self.i0..=self.i1.min(self.nx.saturating_sub(1))).enumerate() {
            for (aj, j) in (self.j0..=self.j1.min(self.ny.saturating_sub(1))).enumerate() {
                let idx = self.idx3(i, j, k);
                let cell = ai * self.nj + aj;
                if cell < self.jx_top.len() {
                    self.jx_top[cell] += -get(hy, idx) * phase;
                    self.jy_top[cell] += get(hx, idx) * phase;
                    self.mx_top[cell] += get(ey, idx) * phase;
                    self.my_top[cell] += -get(ex, idx) * phase;
                }
            }
        }

        // ── Front face: j = j0, normal n̂ = -ŷ ───────────────────
        // J = (-ŷ) × H = (-ŷ) × (Hx,Hy,Hz) = (-Hz, 0, Hx)
        // M = -(−ŷ) × E = ŷ × E = ŷ × (Ex,Ey,Ez) = (Ez, 0, -Ex)
        let j = self.j0;
        if j < self.ny {
            for (ai, i) in (self.i0..=self.i1.min(self.nx.saturating_sub(1))).enumerate() {
                for (ak, k) in (self.k0..=self.k1.min(self.nz.saturating_sub(1))).enumerate() {
                    let idx = self.idx3(i, j, k);
                    let cell = ai * self.nk + ak;
                    if cell < self.jx_fnt.len() {
                        self.jx_fnt[cell] += -get(hz, idx) * phase;
                        self.jz_fnt[cell] += get(hx, idx) * phase;
                        self.mx_fnt[cell] += get(ez, idx) * phase;
                        self.mz_fnt[cell] += -get(ex, idx) * phase;
                    }
                }
            }
        }

        // ── Back face: j = j1, normal n̂ = +ŷ ────────────────────
        // J = ŷ × H = (Hz, 0, -Hx)
        // M = -(ŷ) × E = -ŷ × E = (-Ez, 0, Ex)
        let j = self.j1.min(self.ny.saturating_sub(1));
        for (ai, i) in (self.i0..=self.i1.min(self.nx.saturating_sub(1))).enumerate() {
            for (ak, k) in (self.k0..=self.k1.min(self.nz.saturating_sub(1))).enumerate() {
                let idx = self.idx3(i, j, k);
                let cell = ai * self.nk + ak;
                if cell < self.jx_bck.len() {
                    self.jx_bck[cell] += get(hz, idx) * phase;
                    self.jz_bck[cell] += -get(hx, idx) * phase;
                    self.mx_bck[cell] += -get(ez, idx) * phase;
                    self.mz_bck[cell] += get(ex, idx) * phase;
                }
            }
        }

        // ── Left face: i = i0, normal n̂ = -x̂ ────────────────────
        // J = (-x̂) × H = (0, Hz, -Hy)
        // M = -(-x̂) × E = x̂ × E = (0, -Ez, Ey)
        let i = self.i0;
        if i < self.nx {
            for (aj, j) in (self.j0..=self.j1.min(self.ny.saturating_sub(1))).enumerate() {
                for (ak, k) in (self.k0..=self.k1.min(self.nz.saturating_sub(1))).enumerate() {
                    let idx = self.idx3(i, j, k);
                    let cell = aj * self.nk + ak;
                    if cell < self.jy_lft.len() {
                        self.jy_lft[cell] += get(hz, idx) * phase;
                        self.jz_lft[cell] += -get(hy, idx) * phase;
                        self.my_lft[cell] += -get(ez, idx) * phase;
                        self.mz_lft[cell] += get(ey, idx) * phase;
                    }
                }
            }
        }

        // ── Right face: i = i1, normal n̂ = +x̂ ───────────────────
        // J = x̂ × H = (0, -Hz, Hy)
        // M = -(x̂) × E = (-x̂) × E = (0, Ez, -Ey)
        let i = self.i1.min(self.nx.saturating_sub(1));
        for (aj, j) in (self.j0..=self.j1.min(self.ny.saturating_sub(1))).enumerate() {
            for (ak, k) in (self.k0..=self.k1.min(self.nz.saturating_sub(1))).enumerate() {
                let idx = self.idx3(i, j, k);
                let cell = aj * self.nk + ak;
                if cell < self.jy_rgt.len() {
                    self.jy_rgt[cell] += -get(hz, idx) * phase;
                    self.jz_rgt[cell] += get(hy, idx) * phase;
                    self.my_rgt[cell] += get(ez, idx) * phase;
                    self.mz_rgt[cell] += -get(ey, idx) * phase;
                }
            }
        }

        self.n_samples += 1;
    }

    /// Compute the radiation integral vectors N and L at angle (theta, phi).
    ///
    /// N = ∫∫ J · exp(j·k·r'·r̂) dS'  (electric potential vector)
    /// L = ∫∫ M · exp(j·k·r'·r̂) dS'  (magnetic potential vector)
    ///
    /// Returns (N_theta, N_phi, L_theta, L_phi) complex amplitudes.
    fn radiation_integrals(&self, theta: f64, phi: f64) -> ([Complex64; 2], [Complex64; 2]) {
        let k0 = self.omega / C;
        let sin_t = theta.sin();
        let cos_t = theta.cos();
        let sin_p = phi.sin();
        let cos_p = phi.cos();

        // Observer direction unit vector: r̂ = (sin_t*cos_p, sin_t*sin_p, cos_t)
        let rx = sin_t * cos_p;
        let ry = sin_t * sin_p;
        let rz = cos_t;

        // Center of the NTFF box (physical coords)
        let cx = 0.5 * (self.i0 + self.i1) as f64 * self.dx;
        let cy = 0.5 * (self.j0 + self.j1) as f64 * self.dy;
        let cz = 0.5 * (self.k0 + self.k1) as f64 * self.dz;

        // Accumulate N (from J) and L (from M) vectors in Cartesian coords
        let mut nx_c = Complex64::new(0.0, 0.0);
        let mut ny_c = Complex64::new(0.0, 0.0);
        let mut nz_c = Complex64::new(0.0, 0.0);
        let mut lx_c = Complex64::new(0.0, 0.0);
        let mut ly_c = Complex64::new(0.0, 0.0);
        let mut lz_c = Complex64::new(0.0, 0.0);

        // ── Bottom face (k=k0, n=-z, dS=dx*dy) ──────────────────
        let z = self.k0 as f64 * self.dz - cz;
        let ds = self.dx * self.dy;
        for (ai, i) in (self.i0..=self.i1.min(self.nx.saturating_sub(1))).enumerate() {
            for (aj, j) in (self.j0..=self.j1.min(self.ny.saturating_sub(1))).enumerate() {
                let x = i as f64 * self.dx - cx;
                let y = j as f64 * self.dy - cy;
                let r_dot = rx * x + ry * y + rz * z;
                let ph = Complex64::new(0.0, k0 * r_dot).exp() * ds;
                let cell = ai * self.nj + aj;
                if cell < self.jx_bot.len() {
                    nx_c += self.jx_bot[cell] * ph;
                    ny_c += self.jy_bot[cell] * ph;
                    lx_c += self.mx_bot[cell] * ph;
                    ly_c += self.my_bot[cell] * ph;
                }
            }
        }

        // ── Top face (k=k1, n=+z, dS=dx*dy) ─────────────────────
        let z = self.k1 as f64 * self.dz - cz;
        for (ai, i) in (self.i0..=self.i1.min(self.nx.saturating_sub(1))).enumerate() {
            for (aj, j) in (self.j0..=self.j1.min(self.ny.saturating_sub(1))).enumerate() {
                let x = i as f64 * self.dx - cx;
                let y = j as f64 * self.dy - cy;
                let r_dot = rx * x + ry * y + rz * z;
                let ph = Complex64::new(0.0, k0 * r_dot).exp() * ds;
                let cell = ai * self.nj + aj;
                if cell < self.jx_top.len() {
                    nx_c += self.jx_top[cell] * ph;
                    ny_c += self.jy_top[cell] * ph;
                    lx_c += self.mx_top[cell] * ph;
                    ly_c += self.my_top[cell] * ph;
                }
            }
        }

        // ── Front face (j=j0, n=-y, dS=dx*dz) ───────────────────
        let y = self.j0 as f64 * self.dy - cy;
        let ds_fnt = self.dx * self.dz;
        for (ai, i) in (self.i0..=self.i1.min(self.nx.saturating_sub(1))).enumerate() {
            for (ak, k) in (self.k0..=self.k1.min(self.nz.saturating_sub(1))).enumerate() {
                let x = i as f64 * self.dx - cx;
                let z = k as f64 * self.dz - cz;
                let r_dot = rx * x + ry * y + rz * z;
                let ph = Complex64::new(0.0, k0 * r_dot).exp() * ds_fnt;
                let cell = ai * self.nk + ak;
                if cell < self.jx_fnt.len() {
                    nx_c += self.jx_fnt[cell] * ph;
                    nz_c += self.jz_fnt[cell] * ph;
                    lx_c += self.mx_fnt[cell] * ph;
                    lz_c += self.mz_fnt[cell] * ph;
                }
            }
        }

        // ── Back face (j=j1, n=+y, dS=dx*dz) ────────────────────
        let y = self.j1 as f64 * self.dy - cy;
        for (ai, i) in (self.i0..=self.i1.min(self.nx.saturating_sub(1))).enumerate() {
            for (ak, k) in (self.k0..=self.k1.min(self.nz.saturating_sub(1))).enumerate() {
                let x = i as f64 * self.dx - cx;
                let z = k as f64 * self.dz - cz;
                let r_dot = rx * x + ry * y + rz * z;
                let ph = Complex64::new(0.0, k0 * r_dot).exp() * ds_fnt;
                let cell = ai * self.nk + ak;
                if cell < self.jx_bck.len() {
                    nx_c += self.jx_bck[cell] * ph;
                    nz_c += self.jz_bck[cell] * ph;
                    lx_c += self.mx_bck[cell] * ph;
                    lz_c += self.mz_bck[cell] * ph;
                }
            }
        }

        // ── Left face (i=i0, n=-x, dS=dy*dz) ────────────────────
        let x = self.i0 as f64 * self.dx - cx;
        let ds_lft = self.dy * self.dz;
        for (aj, j) in (self.j0..=self.j1.min(self.ny.saturating_sub(1))).enumerate() {
            for (ak, k) in (self.k0..=self.k1.min(self.nz.saturating_sub(1))).enumerate() {
                let y = j as f64 * self.dy - cy;
                let z = k as f64 * self.dz - cz;
                let r_dot = rx * x + ry * y + rz * z;
                let ph = Complex64::new(0.0, k0 * r_dot).exp() * ds_lft;
                let cell = aj * self.nk + ak;
                if cell < self.jy_lft.len() {
                    ny_c += self.jy_lft[cell] * ph;
                    nz_c += self.jz_lft[cell] * ph;
                    ly_c += self.my_lft[cell] * ph;
                    lz_c += self.mz_lft[cell] * ph;
                }
            }
        }

        // ── Right face (i=i1, n=+x, dS=dy*dz) ───────────────────
        let x = self.i1 as f64 * self.dx - cx;
        for (aj, j) in (self.j0..=self.j1.min(self.ny.saturating_sub(1))).enumerate() {
            for (ak, k) in (self.k0..=self.k1.min(self.nz.saturating_sub(1))).enumerate() {
                let y = j as f64 * self.dy - cy;
                let z = k as f64 * self.dz - cz;
                let r_dot = rx * x + ry * y + rz * z;
                let ph = Complex64::new(0.0, k0 * r_dot).exp() * ds_lft;
                let cell = aj * self.nk + ak;
                if cell < self.jy_rgt.len() {
                    ny_c += self.jy_rgt[cell] * ph;
                    nz_c += self.jz_rgt[cell] * ph;
                    ly_c += self.my_rgt[cell] * ph;
                    lz_c += self.mz_rgt[cell] * ph;
                }
            }
        }

        // Project N and L onto (theta_hat, phi_hat) components
        // theta_hat = (cos_t*cos_p, cos_t*sin_p, -sin_t)
        // phi_hat   = (-sin_p, cos_p, 0)
        let n_theta = nx_c * (cos_t * cos_p) + ny_c * (cos_t * sin_p) + nz_c * (-sin_t);
        let n_phi = nx_c * (-sin_p) + ny_c * cos_p;
        let l_theta = lx_c * (cos_t * cos_p) + ly_c * (cos_t * sin_p) + lz_c * (-sin_t);
        let l_phi = lx_c * (-sin_p) + ly_c * cos_p;

        ([n_theta, n_phi], [l_theta, l_phi])
    }

    /// Compute far-field radiation pattern at angle (theta, phi) in spherical coordinates.
    ///
    /// Returns (E_theta, E_phi) complex amplitudes as (\[re, im\], \[re, im\]).
    ///
    /// Far-field approximation:
    ///   E_theta ≈ -j·k₀/(4π) · (L_phi + η₀·N_theta)
    ///   E_phi   ≈  j·k₀/(4π) · (L_theta - η₀·N_phi)
    ///
    /// where η₀ = 377 Ω is the free-space impedance.
    pub fn far_field(&self, theta: f64, phi: f64, r: f64) -> ([f64; 2], [f64; 2]) {
        let k0 = self.omega / C;
        let eta0 = 376.730_313_461_77_f64;
        let ([n_theta, n_phi], [l_theta, l_phi]) = self.radiation_integrals(theta, phi);

        // Far-field electric field (Balanis 6-122a,b)
        let j = Complex64::new(0.0, 1.0);
        let factor = -j * k0 / (4.0 * PI * r);
        let e_theta = factor * (l_phi + eta0 * n_theta);
        let e_phi = factor * (-l_theta + eta0 * n_phi);

        ([e_theta.re, e_theta.im], [e_phi.re, e_phi.im])
    }

    /// Compute the directivity pattern over a spherical grid.
    ///
    /// Returns a 2D grid of directivity (dBi) values:
    ///   result\[i_theta\]\[i_phi\]  where theta in \[0, π\], phi in \[0, 2π\]
    ///
    /// # Arguments
    /// * `n_theta` — number of theta samples (polar angle, 0=north pole)
    /// * `n_phi` — number of phi samples (azimuthal angle)
    pub fn directivity_pattern(&self, n_theta: usize, n_phi: usize) -> Vec<Vec<f64>> {
        let r = 1.0_f64; // Far-field distance (normalized)

        // Compute |E|² at all angles
        let mut power_grid = vec![vec![0.0_f64; n_phi]; n_theta];
        let mut total_power = 0.0_f64;

        for (it, theta_row) in power_grid.iter_mut().enumerate().take(n_theta) {
            let theta = it as f64 / (n_theta - 1).max(1) as f64 * PI;
            let sin_t = theta.sin();
            for (ip, cell) in theta_row.iter_mut().enumerate().take(n_phi) {
                let phi = ip as f64 / (n_phi - 1).max(1) as f64 * 2.0 * PI;
                let ([et_re, et_im], [ep_re, ep_im]) = self.far_field(theta, phi, r);
                let power = et_re * et_re + et_im * et_im + ep_re * ep_re + ep_im * ep_im;
                *cell = power;
                // Weight by sin(theta) for spherical integration
                total_power += power * sin_t;
            }
        }

        // Normalize to compute directivity (ratio to isotropic)
        // Isotropic power = total_power / (4π) if integrating over full sphere
        let d_theta = if n_theta > 1 {
            PI / (n_theta - 1) as f64
        } else {
            1.0
        };
        let d_phi = if n_phi > 1 {
            2.0 * PI / (n_phi - 1) as f64
        } else {
            1.0
        };
        let p_total = total_power * d_theta * d_phi;
        let p_isotropic = p_total / (4.0 * PI);

        // Convert to dBi
        power_grid
            .iter()
            .map(|row| {
                row.iter()
                    .map(|&p| {
                        if p_isotropic > 1e-60 && p > 1e-60 {
                            10.0 * (p / p_isotropic).log10()
                        } else {
                            -999.0
                        }
                    })
                    .collect()
            })
            .collect()
    }

    /// Estimate total radiated power (W) from near-field surface integration.
    ///
    /// Uses the Poynting theorem:
    ///   P_rad = 0.5 · Re ∫∫ (E × H*) · n̂ dS
    ///
    /// Approximated from the DFT accumulators as the norm of the total current.
    pub fn total_radiated_power(&self) -> f64 {
        let eta0 = 376.730_313_461_77_f64;
        // Simple estimate: ||N||² / (2*eta0) integrated over all directions
        // For a single angle, this gives a rough estimate
        // Full integration would require summing over the sphere
        let mut total = 0.0_f64;

        // Sum |J|² over all faces as a proxy for radiated power
        let sum_j_sq = |v: &[Complex64]| v.iter().map(|c| c.norm_sqr()).sum::<f64>();
        total += sum_j_sq(&self.jx_bot) + sum_j_sq(&self.jy_bot);
        total += sum_j_sq(&self.jx_top) + sum_j_sq(&self.jy_top);
        total += sum_j_sq(&self.jx_fnt) + sum_j_sq(&self.jz_fnt);
        total += sum_j_sq(&self.jx_bck) + sum_j_sq(&self.jz_bck);
        total += sum_j_sq(&self.jy_lft) + sum_j_sq(&self.jz_lft);
        total += sum_j_sq(&self.jy_rgt) + sum_j_sq(&self.jz_rgt);

        // Scale by eta0/(2) and face areas for dimensional consistency
        total * eta0 / 2.0 * self.dx * self.dy * self.dz
    }

    /// Returns the number of cells on each face: (n_bot_top, n_fnt_bck, n_lft_rgt).
    pub fn face_sizes(&self) -> (usize, usize, usize) {
        (self.ni * self.nj, self.ni * self.nk, self.nj * self.nk)
    }
}

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

    fn make_ntff() -> NearToFarField2d {
        let nx = 80;
        let ny = 80;
        let dx = 20e-9;
        let dy = 20e-9;
        NearToFarField2d::new(nx, ny, dx, dy, 15, 65, 15, 65, 2.0 * PI * 200e12)
    }

    #[test]
    fn ntff_initializes_zero() {
        let m = make_ntff();
        assert!(m.hz_bot.iter().all(|v| v.norm() == 0.0));
        assert!(m.ex_bot.iter().all(|v| v.norm() == 0.0));
    }

    #[test]
    fn ntff_accumulate_does_not_panic() {
        let mut m = make_ntff();
        let nx = m.nx;
        let ny = m.ny;
        let hz = vec![0.0f64; nx * ny];
        let ex = vec![0.0f64; (nx + 1) * ny];
        let ey = vec![0.0f64; nx * (ny + 1)];
        m.accumulate(&hz, &ex, &ey, 1e-15, 1e-17);
        // Accumulators stay zero when fields are zero
        assert!(m.hz_bot.iter().all(|v| v.norm() == 0.0));
    }

    #[test]
    fn ntff_radiation_pattern_returns_correct_size() {
        let m = make_ntff();
        let angles: Vec<f64> = (0..36).map(|i| i as f64 * 10.0).collect();
        let pattern = m.radiation_pattern(&angles);
        assert_eq!(pattern.len(), 36);
        assert!(pattern.iter().all(|&v| v >= 0.0));
    }

    #[test]
    fn ntff_zero_fields_give_zero_pattern() {
        let m = make_ntff();
        let angles = vec![0.0, 90.0, 180.0, 270.0];
        let pattern = m.radiation_pattern(&angles);
        for &p in &pattern {
            assert!(
                p.abs() < 1e-60,
                "pattern should be ~0 with no field accumulated"
            );
        }
    }

    // 3D NTFF tests
    #[test]
    fn ntff_3d_new_correct_dimensions() {
        let ntff = NearToFarField3d::new(
            5,
            15,
            5,
            15,
            5,
            15,
            2.0 * PI * 200e12,
            1e-17,
            20e-9,
            20e-9,
            20e-9,
            20,
            20,
            20,
        );
        let (n_bt, n_fb, n_lr) = ntff.face_sizes();
        assert_eq!(n_bt, 11 * 11, "Bottom/top face should be ni*nj");
        assert_eq!(n_fb, 11 * 11, "Front/back face should be ni*nk");
        assert_eq!(n_lr, 11 * 11, "Left/right face should be nj*nk");
        assert_eq!(ntff.n_samples, 0);
    }

    #[test]
    fn ntff_3d_accumulate_with_zero_fields() {
        let mut ntff = NearToFarField3d::new(
            5,
            15,
            5,
            15,
            5,
            15,
            2.0 * PI * 200e12,
            1e-17,
            20e-9,
            20e-9,
            20e-9,
            20,
            20,
            20,
        );
        let n = 20 * 20 * 20;
        let ex = vec![0.0f64; n];
        let ey = vec![0.0f64; n];
        let ez = vec![0.0f64; n];
        let hx = vec![0.0f64; n];
        let hy = vec![0.0f64; n];
        let hz = vec![0.0f64; n];
        ntff.accumulate(0, &ex, &ey, &ez, &hx, &hy, &hz);
        assert_eq!(ntff.n_samples, 1);
        // With zero fields, all currents should be zero
        assert!(ntff.jx_bot.iter().all(|c| c.norm() == 0.0));
    }

    #[test]
    fn ntff_3d_accumulate_with_nonzero_fields() {
        let mut ntff = NearToFarField3d::new(
            3,
            8,
            3,
            8,
            3,
            8,
            2.0 * PI * 200e12,
            1e-17,
            20e-9,
            20e-9,
            20e-9,
            12,
            12,
            12,
        );
        let n = 12 * 12 * 12;
        let ex = vec![1.0f64; n];
        let ey = vec![0.5f64; n];
        let ez = vec![0.0f64; n];
        let hx = vec![0.0f64; n];
        let hy = vec![1.0f64; n];
        let hz = vec![0.0f64; n];
        ntff.accumulate(0, &ex, &ey, &ez, &hx, &hy, &hz);
        // With nonzero Hy, jx_bot should accumulate (J = (-z) × H → Jx = Hy)
        let any_nonzero = ntff.jx_bot.iter().any(|c| c.norm() > 0.0)
            || ntff.jy_bot.iter().any(|c| c.norm() > 0.0)
            || ntff.mx_bot.iter().any(|c| c.norm() > 0.0);
        assert!(
            any_nonzero,
            "Nonzero fields should produce nonzero surface currents"
        );
    }

    #[test]
    fn ntff_3d_far_field_returns_finite() {
        let ntff = NearToFarField3d::new(
            5,
            15,
            5,
            15,
            5,
            15,
            2.0 * PI * 200e12,
            1e-17,
            20e-9,
            20e-9,
            20e-9,
            20,
            20,
            20,
        );
        let ([et_re, et_im], [ep_re, ep_im]) = ntff.far_field(PI / 2.0, 0.0, 1.0);
        assert!(et_re.is_finite() && et_im.is_finite());
        assert!(ep_re.is_finite() && ep_im.is_finite());
    }

    #[test]
    fn ntff_3d_directivity_pattern_shape() {
        let ntff = NearToFarField3d::new(
            5,
            10,
            5,
            10,
            5,
            10,
            2.0 * PI * 200e12,
            1e-17,
            20e-9,
            20e-9,
            20e-9,
            15,
            15,
            15,
        );
        let n_theta = 10;
        let n_phi = 12;
        let pattern = ntff.directivity_pattern(n_theta, n_phi);
        assert_eq!(pattern.len(), n_theta, "Pattern should have n_theta rows");
        assert_eq!(pattern[0].len(), n_phi, "Pattern should have n_phi columns");
    }

    #[test]
    fn ntff_3d_total_radiated_power_nonnegative() {
        let ntff = NearToFarField3d::new(
            5,
            10,
            5,
            10,
            5,
            10,
            2.0 * PI * 200e12,
            1e-17,
            20e-9,
            20e-9,
            20e-9,
            15,
            15,
            15,
        );
        let p = ntff.total_radiated_power();
        assert!(p >= 0.0, "Total radiated power should be non-negative: {p}");
    }

    #[test]
    fn ntff_3d_wavelength_computed_correctly() {
        let omega = 2.0 * PI * 200e12;
        let ntff = NearToFarField3d::new(
            5, 10, 5, 10, 5, 10, omega, 1e-17, 20e-9, 20e-9, 20e-9, 15, 15, 15,
        );
        let expected_lambda = C / 200e12;
        assert!(
            (ntff.wavelength - expected_lambda).abs() < 1e-12,
            "Wavelength mismatch: {} vs {}",
            ntff.wavelength,
            expected_lambda
        );
    }
}