Expand description
Softened inverse-square N-body step as a certified forge kernel.
Embeds shaders/kinematics.wgsl via
include_str! (single source of truth), grades it against the exact CPU oracle
nbody_step_cpu, and runs it on any wgpu adapter via nbody_step_gpu.
State is a flat f32 buffer, 8 scalars per particle:
[px, py, pz, vx, vy, vz, mass, charge]. The kernel is double-buffered: forces
are read from the input state only and written to a separate output, so the result is
independent of invocation order (no read/write race). The pairwise force on i is
F_i = coupling · Σ_{j≠i} q_i q_j (x_i−x_j) / (|x_i−x_j|² + soft)^{3/2} (Plummer
softening, no singular skip branch), then a symplectic-Euler update
v ← v + (F/m)dt, x ← x + v·dt. coupling selects the law: +k electrostatic
(repulsive like-charges), −G gravitational (put mass in the charge slot).
Constants§
- KIN_
STEP_ ENTRY - Entry-point name of
KIN_STEP_WGSL. - KIN_
STEP_ WGSL - The N-body step kernel source (embedded from the canonical
.wgsl). - KIN_
STRIDE - Scalars per particle in the flat state buffer.
Functions§
- nbody_
step_ cpu - Exact CPU oracle for one N-body step. Reads
state_in, returns the new state (same length), mirroring the WGSL scalar-for-scalar: forces accumulated in increasing-jorder (skipping self),1/(r²+soft)^{3/2}viar2 * sqrt(r2), thenvthenxusing the newv. Particles withmass == 0take a zero inverse mass. - nbody_
step_ gpu - Run one N-body step on the GPU and read back the new flat state. Builds a transient
wgpu context, uploads
state_in(binding 0, read), a zeroed output (binding 1, read_write) andparams = [dt, soft, coupling](binding 2, read), dispatches one invocation per particle, and reads the output back. Returns the same length asstate_in.