Skip to main content

Module kinematics

Module kinematics 

Source
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-j order (skipping self), 1/(r²+soft)^{3/2} via r2 * sqrt(r2), then v then x using the new v. Particles with mass == 0 take 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) and params = [dt, soft, coupling] (binding 2, read), dispatches one invocation per particle, and reads the output back. Returns the same length as state_in.