pub const MAXWELL_FDTD_1D: &str = r#"
struct EmParams {
dt: f32,
dx: f32,
eps: f32,
mu: f32,
sigma: f32,
n: u32,
stage: u32, // 0 = update Hz, 1 = update Ey
_pad: u32,
};
@group(0) @binding(0) var<storage, read_write> ey: array<f32>;
@group(0) @binding(1) var<storage, read_write> hz: array<f32>;
@group(0) @binding(2) var<uniform> p: EmParams;
@compute @workgroup_size(64, 1, 1)
fn maxwell_fdtd(@builtin(global_invocation_id) gid: vec3<u32>) {
let i = gid.x;
if (i >= p.n) { return; }
let n = p.n;
if (p.stage == 0u) {
let ip1 = select(i + 1u, 0u, i + 1u >= n);
hz[i] = hz[i] - (p.dt / (p.mu * p.dx)) * (ey[ip1] - ey[i]);
} else {
let im1 = select(i - 1u, n - 1u, i == 0u);
ey[i] = ey[i] - (p.dt / (p.eps * p.dx)) * (hz[i] - hz[im1])
- (p.dt * p.sigma / p.eps) * ey[i];
}
}
"#;Expand description
1D electromagnetics — the Yee FDTD leap-frog (Ey/Hz). One workgroup pass advances Hz, the next advances Ey; the host alternates them per step.