oxiphoton 0.1.2

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
use crate::fdtd::boundary::pml::Cpml;
use crate::fdtd::config::{BoundaryConfig, Dimensions, GridSpacing};
use crate::fdtd::courant::courant_dt;

use super::{
    buffers::{
        readback, storage_entry, storage_init, storage_init_updatable, storage_rw_aux,
        storage_rw_field, uniform_entry, uniform_from, SimDims, SrcUniform,
    },
    context::GpuContext,
    GpuError,
};

const SHADER_INJECT: &str = include_str!("shaders/te2d_inject.wgsl");
const SHADER_HZ: &str = include_str!("shaders/te2d_hz.wgsl");
const SHADER_EX: &str = include_str!("shaders/te2d_ex.wgsl");
const SHADER_EY: &str = include_str!("shaders/te2d_ey.wgsl");

/// 2D TE FDTD solver accelerated via wgpu compute shaders.
///
/// Fields (Hz, Ex, Ey) and psi arrays reside on-GPU across `run()`; no per-step
/// readback. Coefficients are precomputed in f64 (identical to `Fdtd2dTe`) then
/// cast to f32 for upload, keeping CPML precision under control.
///
/// # GPU availability
/// If no adapter is found, `new` returns `Err(GpuError::NotAvailable)`.
/// The caller is responsible for graceful fallback to `Fdtd2dTe`.
// GPU buffers are owned for lifetime management — the bind groups hold wgpu
// references to them, so they must outlive the bind groups. The compiler
// cannot track this cross-API ownership, hence the allow.
#[allow(dead_code)]
pub struct Fdtd2dGpu {
    ctx: GpuContext,
    pub nx: usize,
    pub ny: usize,
    pub dt: f64,
    pub time_step: usize,

    // Field buffers (STORAGE | COPY_SRC for readback)
    buf_hz: wgpu::Buffer,
    buf_ex: wgpu::Buffer,
    buf_ey: wgpu::Buffer,

    // psi auxiliary buffers (STORAGE only)
    buf_psi_hz_x: wgpu::Buffer,
    buf_psi_hz_y: wgpu::Buffer,
    buf_psi_ex_y: wgpu::Buffer,
    buf_psi_ey_x: wgpu::Buffer,

    // Material buffers (STORAGE | COPY_DST so fill_eps_box can update them)
    buf_mu_hz: wgpu::Buffer,
    buf_eps_ex: wgpu::Buffer,
    buf_eps_ey: wgpu::Buffer,

    // PML coefficient buffers, packed [b, c, kappa] (STORAGE, uploaded once)
    buf_pml_h_x: wgpu::Buffer,
    buf_pml_h_y: wgpu::Buffer,
    buf_pml_e_x: wgpu::Buffer,
    buf_pml_e_y: wgpu::Buffer,

    // Uniform buffers
    buf_dims: wgpu::Buffer,
    buf_src: wgpu::Buffer,

    // Compute pipelines
    pipeline_inject: wgpu::ComputePipeline,
    pipeline_hz: wgpu::ComputePipeline,
    pipeline_ex: wgpu::ComputePipeline,
    pipeline_ey: wgpu::ComputePipeline,

    // Bind groups (one per pipeline)
    bg_inject: wgpu::BindGroup,
    bg_hz: wgpu::BindGroup,
    bg_ex: wgpu::BindGroup,
    bg_ey: wgpu::BindGroup,

    // CPU-side eps copies (f32) — kept in sync for fill_eps_box uploads
    cpu_eps_ex: Vec<f32>,
    cpu_eps_ey: Vec<f32>,

    // Pending source for next step
    src_flat_idx: u32,
    src_val: f32,
}

impl Fdtd2dGpu {
    /// Create a GPU FDTD solver with the same parameters as `Fdtd2dTe::new`.
    pub fn new(
        nx: usize,
        ny: usize,
        dx: f64,
        dy: f64,
        boundary: &BoundaryConfig,
    ) -> Result<Self, GpuError> {
        // Replicate CPU dt and CPML exactly
        let spacing = GridSpacing { dx, dy, dz: dx };
        let dt = 0.99 * courant_dt(Dimensions::TwoD { nx, ny }, spacing, 1.0);
        let pml_x = Cpml::new(
            nx,
            boundary.pml_cells,
            dx,
            dt,
            boundary.pml_m,
            boundary.pml_r0,
        );
        let pml_y = Cpml::new(
            ny,
            boundary.pml_cells,
            dy,
            dt,
            boundary.pml_m,
            boundary.pml_r0,
        );

        let ctx = GpuContext::new()?;
        let dev = &ctx.device;

        // ── Buffer sizes ──────────────────────────────────────────────────────
        let n_hz = nx * ny;
        let n_ex = (nx + 1) * ny;
        let n_ey = nx * (ny + 1);

        // ── Field buffers ─────────────────────────────────────────────────────
        let buf_hz = storage_rw_field(dev, n_hz, "hz");
        let buf_ex = storage_rw_field(dev, n_ex, "ex");
        let buf_ey = storage_rw_field(dev, n_ey, "ey");

        // ── psi auxiliary buffers ───────────────────────────────────────────────
        let buf_psi_hz_x = storage_rw_aux(dev, n_hz, "psi_hz_x");
        let buf_psi_hz_y = storage_rw_aux(dev, n_hz, "psi_hz_y");
        let buf_psi_ex_y = storage_rw_aux(dev, n_ex, "psi_ex_y");
        let buf_psi_ey_x = storage_rw_aux(dev, n_ey, "psi_ey_x");

        // ── Material buffers (initialised to vacuum = 1.0) ───────────────────
        let cpu_eps_ex = vec![1.0f32; n_ex];
        let cpu_eps_ey = vec![1.0f32; n_ey];
        let cpu_mu_hz = vec![1.0f32; n_hz];

        let buf_eps_ex = storage_init_updatable(dev, &cpu_eps_ex, "eps_ex");
        let buf_eps_ey = storage_init_updatable(dev, &cpu_eps_ey, "eps_ey");
        let buf_mu_hz = storage_init_updatable(dev, &cpu_mu_hz, "mu_hz");

        // ── PML coefficient buffers (packed [b, c, kappa]) ───────────────────
        let buf_pml_h_x = storage_init(dev, &pack_pml_h(&pml_x, nx), "pml_h_x");
        let buf_pml_h_y = storage_init(dev, &pack_pml_h(&pml_y, ny), "pml_h_y");
        let buf_pml_e_x = storage_init(dev, &pack_pml_e(&pml_x, nx), "pml_e_x");
        let buf_pml_e_y = storage_init(dev, &pack_pml_e(&pml_y, ny), "pml_e_y");

        // ── Uniform buffers ───────────────────────────────────────────────────
        let sim_dims = SimDims {
            nx: nx as u32,
            ny: ny as u32,
            dx: dx as f32,
            dy: dy as f32,
            dt: dt as f32,
            _p0: 0,
            _p1: 0,
            _p2: 0,
        };
        let buf_dims = uniform_from(dev, &sim_dims, "sim_dims");

        let init_src = SrcUniform {
            flat_idx: 0,
            _p0: 0,
            _p1: 0,
            val: 0.0,
        };
        let buf_src = uniform_from(dev, &init_src, "src_uniform");

        // ── Shaders ───────────────────────────────────────────────────────────
        let sm_inject = dev.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("te2d_inject"),
            source: wgpu::ShaderSource::Wgsl(SHADER_INJECT.into()),
        });
        let sm_hz = dev.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("te2d_hz"),
            source: wgpu::ShaderSource::Wgsl(SHADER_HZ.into()),
        });
        let sm_ex = dev.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("te2d_ex"),
            source: wgpu::ShaderSource::Wgsl(SHADER_EX.into()),
        });
        let sm_ey = dev.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("te2d_ey"),
            source: wgpu::ShaderSource::Wgsl(SHADER_EY.into()),
        });

        // ── Bind group layouts & pipelines ────────────────────────────────────

        // --- Inject ---
        let bgl_inject = dev.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("bgl_inject"),
            entries: &[storage_entry(0, false), uniform_entry(1)],
        });
        let pl_inject = dev.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("pl_inject"),
            bind_group_layouts: &[Some(&bgl_inject)],
            immediate_size: 0,
        });
        let pipeline_inject = dev.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
            label: Some("inject"),
            layout: Some(&pl_inject),
            module: &sm_inject,
            entry_point: Some("main"),
            compilation_options: wgpu::PipelineCompilationOptions::default(),
            cache: None,
        });
        let bg_inject = dev.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("bg_inject"),
            layout: &bgl_inject,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: buf_hz.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: buf_src.as_entire_binding(),
                },
            ],
        });

        // --- Hz ---
        let bgl_hz = dev.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("bgl_hz"),
            entries: &[
                storage_entry(0, false), // hz rw
                storage_entry(1, true),  // ex r
                storage_entry(2, true),  // ey r
                storage_entry(3, false), // psi_hz_x rw
                storage_entry(4, false), // psi_hz_y rw
                storage_entry(5, true),  // mu_hz r
                storage_entry(6, true),  // pml_h_x r
                storage_entry(7, true),  // pml_h_y r
                uniform_entry(8),        // dims
            ],
        });
        let pl_hz = dev.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("pl_hz"),
            bind_group_layouts: &[Some(&bgl_hz)],
            immediate_size: 0,
        });
        let pipeline_hz = dev.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
            label: Some("hz"),
            layout: Some(&pl_hz),
            module: &sm_hz,
            entry_point: Some("main"),
            compilation_options: wgpu::PipelineCompilationOptions::default(),
            cache: None,
        });
        let bg_hz = dev.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("bg_hz"),
            layout: &bgl_hz,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: buf_hz.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: buf_ex.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: buf_ey.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: buf_psi_hz_x.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: buf_psi_hz_y.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 5,
                    resource: buf_mu_hz.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 6,
                    resource: buf_pml_h_x.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 7,
                    resource: buf_pml_h_y.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 8,
                    resource: buf_dims.as_entire_binding(),
                },
            ],
        });

        // --- Ex ---
        let bgl_ex = dev.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("bgl_ex"),
            entries: &[
                storage_entry(0, false), // ex rw
                storage_entry(1, true),  // hz r
                storage_entry(2, false), // psi_ex_y rw
                storage_entry(3, true),  // eps_ex r
                storage_entry(4, true),  // pml_e_y r
                uniform_entry(5),        // dims
            ],
        });
        let pl_ex = dev.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("pl_ex"),
            bind_group_layouts: &[Some(&bgl_ex)],
            immediate_size: 0,
        });
        let pipeline_ex = dev.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
            label: Some("ex"),
            layout: Some(&pl_ex),
            module: &sm_ex,
            entry_point: Some("main"),
            compilation_options: wgpu::PipelineCompilationOptions::default(),
            cache: None,
        });
        let bg_ex = dev.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("bg_ex"),
            layout: &bgl_ex,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: buf_ex.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: buf_hz.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: buf_psi_ex_y.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: buf_eps_ex.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: buf_pml_e_y.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 5,
                    resource: buf_dims.as_entire_binding(),
                },
            ],
        });

        // --- Ey ---
        let bgl_ey = dev.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("bgl_ey"),
            entries: &[
                storage_entry(0, false), // ey rw
                storage_entry(1, true),  // hz r
                storage_entry(2, false), // psi_ey_x rw
                storage_entry(3, true),  // eps_ey r
                storage_entry(4, true),  // pml_e_x r
                uniform_entry(5),        // dims
            ],
        });
        let pl_ey = dev.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("pl_ey"),
            bind_group_layouts: &[Some(&bgl_ey)],
            immediate_size: 0,
        });
        let pipeline_ey = dev.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
            label: Some("ey"),
            layout: Some(&pl_ey),
            module: &sm_ey,
            entry_point: Some("main"),
            compilation_options: wgpu::PipelineCompilationOptions::default(),
            cache: None,
        });
        let bg_ey = dev.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("bg_ey"),
            layout: &bgl_ey,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: buf_ey.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: buf_hz.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: buf_psi_ey_x.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: buf_eps_ey.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: buf_pml_e_x.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 5,
                    resource: buf_dims.as_entire_binding(),
                },
            ],
        });

        Ok(Self {
            ctx,
            nx,
            ny,
            dt,
            time_step: 0,
            buf_hz,
            buf_ex,
            buf_ey,
            buf_psi_hz_x,
            buf_psi_hz_y,
            buf_psi_ex_y,
            buf_psi_ey_x,
            buf_mu_hz,
            buf_eps_ex,
            buf_eps_ey,
            buf_pml_h_x,
            buf_pml_h_y,
            buf_pml_e_x,
            buf_pml_e_y,
            buf_dims,
            buf_src,
            pipeline_inject,
            pipeline_hz,
            pipeline_ex,
            pipeline_ey,
            bg_inject,
            bg_hz,
            bg_ex,
            bg_ey,
            cpu_eps_ex,
            cpu_eps_ey,
            src_flat_idx: 0,
            src_val: 0.0,
        })
    }

    /// Queue a point-source injection at (i, j) into Hz for the next `step()`.
    /// Matches the additive semantics of `Fdtd2dTe::inject_hz`.
    pub fn inject_hz(&mut self, i: usize, j: usize, val: f64) {
        if i < self.nx && j < self.ny {
            self.src_flat_idx = (j * self.nx + i) as u32;
            self.src_val = val as f32;
        }
    }

    /// Fill a dielectric box [ix0,ix1) x [iy0,iy1) with eps_r.
    /// Mirrors `Fdtd2dTe::fill_eps_box` staggering exactly.
    pub fn fill_eps_box(&mut self, ix0: usize, ix1: usize, iy0: usize, iy1: usize, eps_r: f64) {
        let eps_r = eps_r as f32;
        let nx = self.nx;
        let ny = self.ny;
        for j in iy0..iy1.min(ny) {
            for i in ix0..ix1.min(nx) {
                self.cpu_eps_ex[(j + 1) * (nx + 1) + i] = eps_r;
                self.cpu_eps_ey[j * nx + i] = eps_r;
            }
        }
        self.ctx
            .queue
            .write_buffer(&self.buf_eps_ex, 0, bytemuck::cast_slice(&self.cpu_eps_ex));
        self.ctx
            .queue
            .write_buffer(&self.buf_eps_ey, 0, bytemuck::cast_slice(&self.cpu_eps_ey));
    }

    /// Advance one time step on the GPU.
    /// Source from the last `inject_hz` call is applied first, then cleared.
    pub fn step(&mut self) -> Result<(), GpuError> {
        // Update source uniform
        let src_uni = SrcUniform {
            flat_idx: self.src_flat_idx,
            _p0: 0,
            _p1: 0,
            val: self.src_val,
        };
        self.ctx
            .queue
            .write_buffer(&self.buf_src, 0, bytemuck::bytes_of(&src_uni));

        let mut enc = self
            .ctx
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("fdtd_step"),
            });

        // Pass 1: inject source into Hz (1x1 dispatch)
        {
            let mut cp = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("inject"),
                timestamp_writes: None,
            });
            cp.set_pipeline(&self.pipeline_inject);
            cp.set_bind_group(0, &self.bg_inject, &[]);
            cp.dispatch_workgroups(1, 1, 1);
        }

        // Pass 2: update Hz (reads Ex/Ey from previous step)
        {
            let mut cp = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("hz"),
                timestamp_writes: None,
            });
            cp.set_pipeline(&self.pipeline_hz);
            cp.set_bind_group(0, &self.bg_hz, &[]);
            let gx = (self.nx as u32).div_ceil(8);
            let gy = (self.ny as u32).div_ceil(8);
            cp.dispatch_workgroups(gx, gy, 1);
        }

        // Pass 3: update Ex (reads updated Hz — separated by compute pass barrier)
        {
            let mut cp = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("ex"),
                timestamp_writes: None,
            });
            cp.set_pipeline(&self.pipeline_ex);
            cp.set_bind_group(0, &self.bg_ex, &[]);
            let gx = ((self.nx + 1) as u32).div_ceil(8);
            let gy = (self.ny as u32).div_ceil(8);
            cp.dispatch_workgroups(gx, gy, 1);
        }

        // Pass 4: update Ey (reads updated Hz)
        {
            let mut cp = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("ey"),
                timestamp_writes: None,
            });
            cp.set_pipeline(&self.pipeline_ey);
            cp.set_bind_group(0, &self.bg_ey, &[]);
            let gx = (self.nx as u32).div_ceil(8);
            let gy = ((self.ny + 1) as u32).div_ceil(8);
            cp.dispatch_workgroups(gx, gy, 1);
        }

        self.ctx.queue.submit(std::iter::once(enc.finish()));

        // Clear pending source (CPU-side; next step gets 0 unless inject_hz called)
        self.src_val = 0.0;
        self.time_step += 1;

        Ok(())
    }

    /// Run for `steps` time steps. Fields remain on-GPU; no per-step readback.
    pub fn run(&mut self, steps: usize) -> Result<(), GpuError> {
        for _ in 0..steps {
            self.step()?;
        }
        // Flush and wait for all submitted work
        self.ctx
            .device
            .poll(wgpu::PollType::wait_indefinitely())
            .map_err(|e| GpuError::Internal(format!("poll: {e}")))?;
        Ok(())
    }

    /// Download Hz field from GPU as f64 (row-major, j*nx+i).
    pub fn download_hz(&self) -> Result<Vec<f64>, GpuError> {
        // Ensure all pending GPU work is done before reading
        self.ctx
            .device
            .poll(wgpu::PollType::wait_indefinitely())
            .map_err(|e| GpuError::Internal(format!("poll before hz dl: {e}")))?;
        let n = self.nx * self.ny;
        let f32s = readback(
            &self.ctx.device,
            &self.ctx.queue,
            &self.buf_hz,
            (n * 4) as u64,
            "dl_hz",
        )?;
        Ok(f32s.iter().map(|&v| v as f64).collect())
    }

    /// Download Ex field from GPU as f64 (j*(nx+1)+i).
    pub fn download_ex(&self) -> Result<Vec<f64>, GpuError> {
        self.ctx
            .device
            .poll(wgpu::PollType::wait_indefinitely())
            .map_err(|e| GpuError::Internal(format!("poll before ex dl: {e}")))?;
        let n = (self.nx + 1) * self.ny;
        let f32s = readback(
            &self.ctx.device,
            &self.ctx.queue,
            &self.buf_ex,
            (n * 4) as u64,
            "dl_ex",
        )?;
        Ok(f32s.iter().map(|&v| v as f64).collect())
    }

    /// Download Ey field from GPU as f64 (j*nx+i).
    pub fn download_ey(&self) -> Result<Vec<f64>, GpuError> {
        self.ctx
            .device
            .poll(wgpu::PollType::wait_indefinitely())
            .map_err(|e| GpuError::Internal(format!("poll before ey dl: {e}")))?;
        let n = self.nx * (self.ny + 1);
        let f32s = readback(
            &self.ctx.device,
            &self.ctx.queue,
            &self.buf_ey,
            (n * 4) as u64,
            "dl_ey",
        )?;
        Ok(f32s.iter().map(|&v| v as f64).collect())
    }
}

// ── PML packing helpers ───────────────────────────────────────────────────────

/// Pack Cpml H-field coefficients as [b_h[0..n], c_h[0..n], kappa_h[0..n]].
fn pack_pml_h(pml: &Cpml, n: usize) -> Vec<f32> {
    let mut v = vec![0.0f32; 3 * n];
    for k in 0..n {
        v[k] = pml.b_h[k] as f32;
        v[n + k] = pml.c_h[k] as f32;
        v[2 * n + k] = pml.kappa_h[k] as f32;
    }
    v
}

/// Pack Cpml E-field coefficients as [b_e[0..n], c_e[0..n], kappa_e[0..n]].
fn pack_pml_e(pml: &Cpml, n: usize) -> Vec<f32> {
    let mut v = vec![0.0f32; 3 * n];
    for k in 0..n {
        v[k] = pml.b_e[k] as f32;
        v[n + k] = pml.c_e[k] as f32;
        v[2 * n + k] = pml.kappa_e[k] as f32;
    }
    v
}