use super::*;
use crate::{PerspectiveCamera, Vector3};
fn gpu() -> Option<(Arc<wgpu::Device>, Arc<wgpu::Queue>)> {
let gpu = crate::gpu::testing::context("fluid3d")?;
Some((gpu.device.clone(), gpu.queue.clone()))
}
fn sim(config: FluidConfig3d) -> Option<FluidSim3d> {
let (device, queue) = gpu()?;
FluidSim3d::new(device, queue, config).ok()
}
fn sim2(config: FluidConfig3d) -> Option<FluidSim3d> {
sim(config)
}
fn camera(aspect: f32) -> PerspectiveCamera {
let mut c = PerspectiveCamera::new(45.0, aspect, 0.05, 20.0);
c.position = Vector3::new(0.0, 0.0, 1.9);
c.look_at(Vector3::ZERO);
c
}
fn brightest(rgba: &[u8], w: u32, h: u32) -> (f32, f32, u8) {
let mut best = (0u32, 0u32, 0u8);
for y in 0..h {
for x in 0..w {
let at = ((y * w + x) * 4) as usize;
let v = rgba[at].max(rgba[at + 1]).max(rgba[at + 2]);
if v > best.2 {
best = (x, y, v);
}
}
}
(best.0 as f32 / w as f32, best.1 as f32 / h as f32, best.2)
}
fn read_channel(sim: &FluidSim3d, fbo: &Fbo, channel: usize) -> Vec<f32> {
crate::renderer::readback::texture_channel_f32(&sim.device, &sim.queue, &fbo.texture, channel)
}
#[test]
fn the_atlas_holds_every_slice() {
for grid in [2u32, 8, 16, 31, 64, 100, 128] {
let a = Atlas::new(grid);
let (w, h) = a.size();
assert!(
a.tiles.0 * a.tiles.1 >= grid,
"{grid}: {}x{} tiles cannot hold {grid} slices",
a.tiles.0,
a.tiles.1
);
assert_eq!(w, grid * a.tiles.0);
assert_eq!(h, grid * a.tiles.1);
let ratio = w.max(h) as f32 / w.min(h) as f32;
assert!(ratio <= 2.0, "{grid}: atlas is {w}x{h}, ratio {ratio}");
}
}
#[test]
fn an_oversized_volume_is_halved_until_it_fits() {
let a = Atlas::largest_within(512, 8192);
let (w, h) = a.size();
assert!(w <= 8192 && h <= 8192, "{w}x{h} is over the limit");
assert!(a.grid < 512 && a.grid >= 8, "degraded to {}", a.grid);
assert_eq!(Atlas::largest_within(64, 8192).grid, 64);
}
#[test]
fn a_splat_appears_where_it_was_put() {
let Some(mut sim) = sim(FluidConfig3d {
curl: 0.0,
buoyancy: 0.0,
density_dissipation: 0.0,
velocity_dissipation: 0.0,
splat_radius: 0.35,
..FluidConfig3d::default()
}) else {
eprintln!("skipping: no GPU adapter");
return;
};
let (w, h) = (192u32, 192u32);
let cam = camera(w as f32 / h as f32);
let at = [0.7f32, 0.3, 0.55];
sim.splat(at, [0.0, 0.0, 0.0], [1.0, 1.0, 1.0]);
sim.step(1.0 / 60.0);
let rgba = sim.capture_rgba(&cam, w, h);
let (bx, by, v) = brightest(&rgba, w, h);
assert!(v > 24, "nothing was drawn (brightest {v})");
let c = sim.config();
let world = Vector3::new(
c.bounds_min[0] + at[0] * (c.bounds_max[0] - c.bounds_min[0]),
c.bounds_min[1] + at[1] * (c.bounds_max[1] - c.bounds_min[1]),
c.bounds_min[2] + at[2] * (c.bounds_max[2] - c.bounds_min[2]),
);
let screen = cam.world_to_screen(world, w, h).expect("in view");
let (ex, ey) = (screen.x / w as f32, screen.y / h as f32);
assert!(
(bx - ex).abs() < 0.07,
"blob at x {bx:.3}, the voxel projects to {ex:.3}"
);
assert!(
(by - ey).abs() < 0.07,
"blob at y {by:.3}, the voxel projects to {ey:.3}"
);
}
#[test]
fn projection_removes_divergence_in_three_dimensions() {
let Some(mut sim) = sim(FluidConfig3d {
curl: 0.0,
buoyancy: 0.0,
gradient_scale: 0.5,
pressure_solver: PressureSolver::Jacobi,
..FluidConfig3d::default()
}) else {
eprintln!("skipping: no GPU adapter");
return;
};
sim.splat([0.5, 0.5, 0.5], [300.0, 0.0, 0.0], [0.2, 0.2, 0.2]);
let divergence_now = |sim: &mut FluidSim3d| -> f32 {
let mut enc = sim.encoder("test divergence");
sim.next_ubo.set(0);
let u = sim.params(sim.sim, Vol::default());
let bg = sim.bind(&u, &sim.velocity.read, None);
draw(
&mut enc,
&sim.divergence.view,
Load::Clear,
&sim.pipes.divergence,
&bg,
);
sim.queue.submit([enc.finish()]);
let d = read_channel(sim, &sim.divergence, 0);
d.iter().map(|x| x.abs()).sum::<f32>() / d.len() as f32
};
let raw = divergence_now(&mut sim);
assert!(raw > 1e-5, "the splat produced no divergence: {raw}");
sim.step(1.0 / 6000.0);
let at_twenty = divergence_now(&mut sim) / raw;
let mut long = sim2(FluidConfig3d {
curl: 0.0,
buoyancy: 0.0,
gradient_scale: 0.5,
pressure_solver: PressureSolver::Jacobi,
pressure_iterations: 300,
..FluidConfig3d::default()
})
.expect("a second device");
long.splat([0.5, 0.5, 0.5], [300.0, 0.0, 0.0], [0.2, 0.2, 0.2]);
let long_raw = divergence_now(&mut long);
long.step(1.0 / 6000.0);
let at_three_hundred = divergence_now(&mut long) / long_raw;
eprintln!(" 20 sweeps leave {at_twenty:.3}, 300 leave {at_three_hundred:.3}");
assert!(
at_twenty < 0.95,
"twenty sweeps left {at_twenty:.3} of the divergence"
);
assert!(
at_three_hundred < at_twenty * 0.7,
"three hundred sweeps left {at_three_hundred:.3} against twenty's \
{at_twenty:.3} — the solve is not converging"
);
}
#[test]
fn buoyancy_lifts_the_dye_and_not_the_rest() {
let Some(mut sim) = sim(FluidConfig3d {
buoyancy: 400.0,
curl: 0.0,
velocity_dissipation: 0.0,
splat_radius: 0.25,
..FluidConfig3d::default()
}) else {
eprintln!("skipping: no GPU adapter");
return;
};
sim.splat([0.3, 0.3, 0.3], [0.0, 0.0, 0.0], [1.0, 1.0, 1.0]);
for _ in 0..8 {
sim.step(1.0 / 60.0);
}
let vy = read_channel(&sim, &sim.velocity.read, 1);
let lifted = vy.iter().filter(|v| **v > 1.0).count();
let total = vy.len();
assert!(lifted > 0, "nothing rose at all");
assert!(
lifted < total / 3,
"{lifted} of {total} voxels are rising — buoyancy is pushing the \
empty fluid, not the dye"
);
let highest = vy.iter().copied().fold(0.0f32, f32::max);
assert!(highest > 5.0, "the strongest rise is only {highest}");
}
#[test]
fn a_hard_driven_volume_stays_finite() {
let Some(mut sim) = sim(FluidConfig3d {
curl: 40.0,
buoyancy: 300.0,
velocity_dissipation: 0.0,
density_dissipation: 0.0,
resolution: 32,
dye_resolution: 32,
..FluidConfig3d::default()
}) else {
eprintln!("skipping: no GPU adapter");
return;
};
for i in 0..24 {
let t = i as f32 / 24.0;
sim.splat(
[0.2 + 0.6 * t, 0.15, 0.5],
[600.0, 900.0, -600.0],
[1.0, 1.0, 1.0],
);
sim.step(1.0 / 60.0);
}
for _ in 0..60 {
sim.step(1.0 / 60.0);
}
for channel in 0..3 {
let v = read_channel(&sim, &sim.velocity.read, channel);
assert!(
v.iter().all(|x| x.is_finite()),
"velocity channel {channel} went non-finite"
);
let worst = v.iter().copied().fold(0.0f32, |a, b| a.max(b.abs()));
assert!(worst <= 1001.0, "velocity reached {worst}");
}
let dye = read_channel(&sim, &sim.dye.read, 0);
assert!(dye.iter().all(|x| x.is_finite()), "dye went non-finite");
}
#[test]
fn the_same_seed_renders_the_same_volume() {
let run = || -> Option<Vec<u8>> {
let mut sim = sim(FluidConfig3d {
resolution: 32,
dye_resolution: 32,
march_steps: 48,
..FluidConfig3d::default()
})?;
sim.multiple_splats(5);
for _ in 0..12 {
sim.step(1.0 / 60.0);
}
Some(sim.capture_rgba(&camera(1.0), 96, 96))
};
let (Some(a), Some(b)) = (run(), run()) else {
eprintln!("skipping: no GPU adapter");
return;
};
assert_eq!(a, b, "two runs of the same seed differ");
}
#[test]
fn a_uniform_flow_carries_the_dye_along_z() {
let Some(mut sim) = sim(FluidConfig3d {
curl: 0.0,
buoyancy: 0.0,
density_dissipation: 0.0,
velocity_dissipation: 0.0,
splat_radius: 0.3,
gradient_scale: 0.0,
..FluidConfig3d::default()
}) else {
eprintln!("skipping: no GPU adapter");
return;
};
sim.splat([0.5, 0.5, 0.25], [0.0, 0.0, 0.0], [1.0, 1.0, 1.0]);
sim.set_uniform_velocity([0.0, 0.0, 24.0]);
let centre_of_mass_z = |sim: &FluidSim3d| -> f32 {
let dye = read_channel(sim, &sim.dye.read, 0);
let atlas = sim.dye_atlas;
let (w, _) = atlas.size();
let (n, tx) = (atlas.grid as usize, atlas.tiles.0 as usize);
let mut sum = 0.0f64;
let mut mass = 0.0f64;
for (i, v) in dye.iter().enumerate() {
let (ax, ay) = (i % w as usize, i / w as usize);
let z = (ay / n) * tx + (ax / n);
sum += *v as f64 * z as f64;
mass += *v as f64;
}
(sum / mass.max(1e-9)) as f32
};
let before = centre_of_mass_z(&sim);
for _ in 0..40 {
sim.step(1.0 / 60.0);
}
let after = centre_of_mass_z(&sim);
let moved = after - before;
eprintln!(" the dye moved {moved:.1} slices along z");
assert!(
(3.0..34.0).contains(&moved),
"the dye moved {moved:.1} slices along z"
);
}
#[test]
fn zero_sweeps_is_refused() {
let Some((device, queue)) = gpu() else {
eprintln!("skipping: no GPU adapter");
return;
};
let built = FluidSim3d::new(
device,
queue,
FluidConfig3d {
pressure_iterations: 0,
..FluidConfig3d::default()
},
);
let Err(e) = built else {
panic!("a volume with no pressure sweeps was accepted");
};
assert!(e.contains("pressure_iterations"), "{e}");
}
#[test]
fn the_taps_hit_the_voxels_they_name() {
let grid = 16;
let Some(mut sim) = sim(FluidConfig3d {
resolution: grid,
dye_resolution: grid,
..FluidConfig3d::default()
}) else {
eprintln!("skipping: no GPU adapter");
return;
};
sim.splat([0.3, 0.6, 0.2], [400.0, -150.0, 90.0], [1.0, 1.0, 1.0]);
sim.splat([0.8, 0.2, 0.7], [-220.0, 310.0, -60.0], [1.0, 1.0, 1.0]);
sim.splat([0.05, 0.5, 0.95], [120.0, 40.0, -500.0], [1.0, 1.0, 1.0]);
let mut enc = sim.encoder("test taps");
sim.next_ubo.set(0);
let u = sim.params(sim.sim, Vol::default());
let bg = sim.bind(&u, &sim.velocity.read, None);
draw(
&mut enc,
&sim.divergence.view,
Load::Clear,
&sim.pipes.divergence,
&bg,
);
sim.queue.submit([enc.finish()]);
let vel: Vec<Vec<f32>> = (0..3)
.map(|c| read_channel(&sim, &sim.velocity.read, c))
.collect();
let got = read_channel(&sim, &sim.divergence, 0);
let atlas = sim.sim;
let (aw, _) = atlas.size();
let n = grid as i32;
let across = (aw / grid) as i32;
let at = |c: [i32; 3], channel: usize| -> f32 {
let p = c.map(|v| v.clamp(0, n - 1));
let (row, col) = (p[2] / across, p[2] % across);
let (ax, ay) = (col * n + p[0], row * n + p[1]);
vel[channel][(ay * aw as i32 + ax) as usize]
};
let mut worst = 0.0f32;
for z in 0..n {
for y in 0..n {
for x in 0..n {
let c = [x, y, z];
let centre = [at(c, 0), at(c, 1), at(c, 2)];
let face = |d: [i32; 3], channel: usize, wall: bool| -> f32 {
if wall {
-centre[channel]
} else {
at([x + d[0], y + d[1], z + d[2]], channel)
}
};
let l = face([-1, 0, 0], 0, x == 0);
let r = face([1, 0, 0], 0, x == n - 1);
let dn = face([0, -1, 0], 1, y == 0);
let up = face([0, 1, 0], 1, y == n - 1);
let b = face([0, 0, -1], 2, z == 0);
let f = face([0, 0, 1], 2, z == n - 1);
let want = 0.5 * ((r - l) + (up - dn) + (f - b));
let (row, col) = (z / across, z % across);
let idx = ((row * n + y) * aw as i32 + col * n + x) as usize;
worst = worst.max((got[idx] - want).abs() / want.abs().max(1.0));
}
}
}
assert!(
worst < 0.01,
"the GPU's divergence and the CPU's differ by {worst:.4} relative"
);
}
#[test]
fn the_volume_can_be_resized() {
let Some(mut sim) = sim(FluidConfig3d {
resolution: 32,
dye_resolution: 64,
buoyancy: 0.0,
..FluidConfig3d::default()
}) else {
eprintln!("skipping: no GPU adapter");
return;
};
sim.splat([0.5, 0.5, 0.5], [0.0, 0.0, 0.0], [1.0, 0.8, 0.6]);
sim.step(1.0 / 60.0);
assert_eq!(sim.grids(), (32, 64));
let small = sim.memory_bytes();
sim.set_resolution(64, 128);
assert_eq!(sim.grids(), (64, 128));
assert!(
sim.memory_bytes() > small * 4,
"{} bytes at 64³ against {small} at 32³",
sim.memory_bytes()
);
let (w, h) = (96, 96);
let empty = sim.capture_rgba(&camera(1.0), w, h);
let background = (FluidConfig3d::default().background[0] * 255.0).round() as u8 + 2;
assert!(
brightest(&empty, w, h).2 <= background,
"the rebuilt volume is not empty"
);
sim.splat([0.5, 0.5, 0.5], [0.0, 0.0, 0.0], [1.0, 0.8, 0.6]);
sim.step(1.0 / 60.0);
let filled = sim.capture_rgba(&camera(1.0), w, h);
assert!(
brightest(&filled, w, h).2 > 40,
"nothing rendered after the resize"
);
sim.set_config(FluidConfig3d {
resolution: 16,
dye_resolution: 32,
..*sim.config()
});
assert_eq!(sim.grids(), (16, 32));
assert!(sim.memory_bytes() < small);
sim.splat([0.5, 0.5, 0.5], [0.0, 0.0, 0.0], [1.0, 0.8, 0.6]);
sim.step(1.0 / 60.0);
let before = brightest(&sim.capture_rgba(&camera(1.0), w, h), w, h).2;
sim.set_resolution(16, 32);
let after = brightest(&sim.capture_rgba(&camera(1.0), w, h), w, h).2;
assert_eq!(
before, after,
"resizing to the same size emptied the volume"
);
}
#[test]
fn the_v_cycle_beats_the_sweeps() {
let base = FluidConfig3d {
curl: 0.0,
buoyancy: 0.0,
gradient_scale: 0.5,
..FluidConfig3d::default()
};
let residual_after = |config: FluidConfig3d| -> Option<f32> {
let mut sim = sim(config)?;
sim.splat([0.5, 0.5, 0.5], [300.0, 0.0, 0.0], [0.2, 0.2, 0.2]);
let divergence_now = |sim: &mut FluidSim3d| -> f32 {
let mut enc = sim.encoder("test divergence");
sim.next_ubo.set(0);
let u = sim.params(sim.sim, Vol::default());
let bg = sim.bind(&u, &sim.velocity.read, None);
draw(
&mut enc,
&sim.divergence.view,
Load::Clear,
&sim.pipes.divergence,
&bg,
);
sim.queue.submit([enc.finish()]);
let d = read_channel(sim, &sim.divergence, 0);
d.iter().map(|x| x.abs()).sum::<f32>() / d.len() as f32
};
let raw = divergence_now(&mut sim);
sim.step(1.0 / 6000.0);
Some(divergence_now(&mut sim) / raw)
};
let Some(jacobi) = residual_after(FluidConfig3d {
pressure_solver: PressureSolver::Jacobi,
pressure_iterations: 20,
..base
}) else {
eprintln!("skipping: no GPU adapter");
return;
};
let cycle = residual_after(FluidConfig3d {
pressure_solver: PressureSolver::Multigrid,
multigrid_cycles: 1,
pressure_iterations: 20,
..base
})
.expect("a second simulation");
let two = residual_after(FluidConfig3d {
pressure_solver: PressureSolver::Multigrid,
multigrid_cycles: 2,
pressure_iterations: 20,
..base
})
.expect("a third simulation");
let many = residual_after(FluidConfig3d {
pressure_solver: PressureSolver::Jacobi,
pressure_iterations: 300,
..base
})
.expect("a fourth simulation");
eprintln!(
" divergence left: 20 sweeps {jacobi:.3}, 300 sweeps {many:.3}, \
1 cycle {cycle:.3}, 2 cycles {two:.3}"
);
assert!(
cycle < jacobi * 0.5,
"one V-cycle left {cycle:.3} against twenty sweeps' {jacobi:.3}"
);
assert!(
two <= cycle,
"a second cycle made it worse: {two:.3} against {cycle:.3}"
);
}
#[test]
fn the_packed_dye_is_the_same_picture() {
let base = FluidConfig3d {
resolution: 32,
dye_resolution: 64,
buoyancy: 40.0,
..FluidConfig3d::default()
};
let render = |compact: bool| -> Option<(Vec<u8>, u64, wgpu::TextureFormat)> {
let mut sim = sim(FluidConfig3d {
compact_dye: compact,
..base
})?;
for frame in 0..12 {
if frame % 3 == 0 {
sim.splat([0.5, 0.2, 0.5], [0.0, 260.0, 0.0], [1.1, 0.4, 0.25]);
}
sim.step(1.0 / 60.0);
}
let format = sim.dye_texture().format();
let bytes = sim.memory_bytes();
Some((sim.capture_rgba(&camera(1.0), 128, 128), bytes, format))
};
let Some((packed, packed_bytes, format)) = render(true) else {
eprintln!("skipping: no GPU adapter");
return;
};
let (wide, wide_bytes, _) = render(false).expect("a second simulation");
if format != wgpu::TextureFormat::Rg11b10Ufloat {
eprintln!("skipping: this device will not render to rg11b10ufloat");
return;
}
let diffs: Vec<f64> = packed
.iter()
.zip(&wide)
.map(|(a, b)| a.abs_diff(*b) as f64)
.collect();
let mean = diffs.iter().sum::<f64>() / diffs.len() as f64;
let worst = diffs.iter().cloned().fold(0.0, f64::max);
eprintln!(
" packed dye: {packed_bytes} bytes against {wide_bytes}, \
mean |diff| {mean:.3}/255, worst {worst}"
);
assert!(
packed_bytes * 3 < wide_bytes * 2,
"the packed dye saved nothing: {packed_bytes} against {wide_bytes}"
);
assert!(
mean < 0.5,
"the packed dye changed the picture: mean |diff| {mean:.3}/255"
);
}
#[test]
fn empty_space_skipping_is_the_same_picture() {
let base = FluidConfig3d {
resolution: 32,
dye_resolution: 64,
buoyancy: 40.0,
march_steps: 128,
..FluidConfig3d::default()
};
let render = |skip: bool| -> Option<Vec<u8>> {
let mut sim = sim(FluidConfig3d {
empty_space_skip: skip,
..base
})?;
for frame in 0..14 {
if frame % 3 == 0 {
sim.splat([0.5, 0.2, 0.5], [0.0, 260.0, 0.0], [1.1, 0.4, 0.25]);
}
sim.step(1.0 / 60.0);
}
Some(sim.capture_rgba(&camera(1.0), 128, 128))
};
let Some(skipped) = render(true) else {
eprintln!("skipping: no GPU adapter");
return;
};
let every = render(false).expect("a second simulation");
let diffs: Vec<f64> = skipped
.iter()
.zip(&every)
.map(|(a, b)| a.abs_diff(*b) as f64)
.collect();
let mean = diffs.iter().sum::<f64>() / diffs.len() as f64;
let worst = diffs.iter().cloned().fold(0.0, f64::max);
assert!(
brightest(&every, 128, 128).2 > 40,
"the reference render is empty"
);
let over_8 = diffs.iter().filter(|d| **d > 8.0).count();
eprintln!(
" empty-space skip: mean |diff| {mean:.3}/255, worst {worst}, {over_8} of {} channels over 8",
diffs.len()
);
assert!(mean < 0.5, "skipping changed the picture: mean {mean:.3}");
}
#[test]
fn the_v_cycle_needs_its_coarse_levels() {
let base = FluidConfig3d {
curl: 0.0,
buoyancy: 0.0,
gradient_scale: 0.5,
..FluidConfig3d::default()
};
let residual_after = |coarsest: u32| -> Option<(f32, usize)> {
let mut sim = sim(FluidConfig3d {
multigrid_coarsest: coarsest,
..base
})?;
sim.splat([0.5, 0.5, 0.5], [300.0, 0.0, 0.0], [0.2, 0.2, 0.2]);
let divergence_now = |sim: &mut FluidSim3d| -> f32 {
let mut enc = sim.encoder("test divergence");
sim.next_ubo.set(0);
let u = sim.params(sim.sim, Vol::default());
let bg = sim.bind(&u, &sim.velocity.read, None);
draw(
&mut enc,
&sim.divergence.view,
Load::Clear,
&sim.pipes.divergence,
&bg,
);
sim.queue.submit([enc.finish()]);
let d = read_channel(sim, &sim.divergence, 0);
d.iter().map(|x| x.abs()).sum::<f32>() / d.len() as f32
};
let raw = divergence_now(&mut sim);
sim.step(1.0 / 6000.0);
Some((divergence_now(&mut sim) / raw, sim.solver_levels()))
};
let Some(deep) = residual_after(4) else {
eprintln!("skipping: no GPU adapter");
return;
};
let mut rows = vec![(4u32, deep)];
for coarsest in [8u32, 16, 32] {
rows.push((
coarsest,
residual_after(coarsest).expect("another simulation"),
));
}
for (coarsest, (left, levels)) in &rows {
eprintln!(" coarsest {coarsest:>2}³ ({levels} levels): {left:.3} of the divergence left");
}
let default = rows
.iter()
.find(|(c, _)| *c == FluidConfig3d::default().multigrid_coarsest)
.map(|(_, (left, _))| *left)
.expect("the default is in the table");
assert!(
default < deep.0 * 1.25,
"the default coarsest leaves {default:.3} against {:.3} for coarsening all the way",
deep.0
);
}
#[test]
fn the_cpu_and_gpu_solvers_agree() {
let config = FluidConfig3d {
resolution: 32,
dye_resolution: 32,
curl: 0.0,
buoyancy: 0.0,
gradient_scale: 0.5,
..FluidConfig3d::default()
};
let Some(mut gpu) = sim(config) else {
eprintln!("skipping: no GPU adapter");
return;
};
let divergence_now = |sim: &mut FluidSim3d| -> f32 {
let mut enc = sim.encoder("test divergence");
sim.next_ubo.set(0);
let u = sim.params(sim.sim, Vol::default());
let bg = sim.bind(&u, &sim.velocity.read, None);
draw(
&mut enc,
&sim.divergence.view,
Load::Clear,
&sim.pipes.divergence,
&bg,
);
sim.queue.submit([enc.finish()]);
let d = read_channel(sim, &sim.divergence, 0);
d.iter().map(|x| x.abs()).sum::<f32>() / d.len() as f32
};
gpu.splat([0.5, 0.5, 0.5], [300.0, 0.0, 0.0], [0.2, 0.2, 0.2]);
let gpu_before = divergence_now(&mut gpu);
gpu.step(1.0 / 6000.0);
let gpu_left = divergence_now(&mut gpu) / gpu_before;
let mut cpu = crate::fluid::CpuFluid3d::new(config);
cpu.splat([0.5, 0.5, 0.5], [300.0, 0.0, 0.0], [0.2, 0.2, 0.2]);
let cpu_before = cpu.mean_divergence();
cpu.step(1.0 / 6000.0);
let cpu_left = cpu.mean_divergence() / cpu_before;
eprintln!(" divergence left: gpu {gpu_left:.3}, cpu {cpu_left:.3}");
let ratio = cpu_before / gpu_before;
assert!(
(0.8..1.25).contains(&ratio),
"the same splat made {cpu_before:.5} of divergence on the cpu and \
{gpu_before:.5} on the gpu"
);
assert!(
(cpu_left - gpu_left).abs() < 0.1,
"the solvers disagree: cpu leaves {cpu_left:.3}, gpu {gpu_left:.3}"
);
}
#[test]
fn the_memory_estimate_is_the_memory() {
let Some((device, queue)) = gpu() else {
eprintln!("skipping: no GPU adapter");
return;
};
for (solver, dye) in [(16u32, 32u32), (24, 48), (32, 64), (48, 96), (50, 100)] {
let config = FluidConfig3d {
resolution: solver,
dye_resolution: dye,
..FluidConfig3d::default()
};
let sim = FluidSim3d::new(device.clone(), queue.clone(), config).expect("a simulation");
let predicted = memory_for(solver, dye, sim.formats());
let actual = sim.memory_bytes();
assert_eq!(
predicted, actual,
"{solver}³/{dye}³: predicted {predicted} bytes, allocated {actual}"
);
}
}
#[test]
fn a_budget_picks_the_largest_volume_that_fits() {
let formats = FluidFormats::default();
for budget_mb in [8u64, 32, 64, 128, 256, 512] {
let budget = budget_mb << 20;
let (solver, dye) = within_memory(budget, 2, formats);
let fits = memory_for(solver, dye, formats);
assert!(
fits <= budget,
"{budget_mb} MB: picked {solver}³ which needs {} MB",
fits >> 20
);
let next = memory_for(solver + 8, (solver + 8) * 2, formats);
assert!(
next > budget || solver >= 512,
"{budget_mb} MB: picked {solver}³ but {}³ also fits",
solver + 8
);
eprintln!(
" {budget_mb:>4} MB -> {solver}³ solver, {dye}³ dye ({} MB)",
fits >> 20
);
}
}
#[test]
fn speed_scales_update_and_not_step() {
let base = FluidConfig3d {
resolution: 24,
dye_resolution: 48,
buoyancy: 160.0,
velocity_dissipation: 0.0,
density_dissipation: 0.0,
..FluidConfig3d::default()
};
let height = |sim: &FluidSim3d| -> f32 {
let dye = crate::renderer::readback::texture_channel_f32(
&sim.device,
&sim.queue,
sim.dye_texture(),
0,
);
let atlas = sim.dye_atlas();
let (w, _) = atlas.size();
let (n, across) = (atlas.grid as usize, (w / atlas.grid) as usize);
let (mut sum, mut mass) = (0.0f64, 0.0f64);
for (i, v) in dye.iter().enumerate() {
let (ax, ay) = (i % w as usize, i / w as usize);
let _ = ax / n * across;
let y = ay % n;
sum += *v as f64 * y as f64;
mass += *v as f64;
}
(sum / mass.max(1e-9)) as f32
};
let rise = |scale: f32, use_update: bool| -> Option<f32> {
let mut sim = sim(FluidConfig3d {
time_scale: scale,
..base
})?;
sim.splat([0.5, 0.25, 0.5], [0.0; 3], [1.0, 1.0, 1.0]);
let before = height(&sim);
for _ in 0..24 {
if use_update {
sim.update(1.0 / 60.0);
} else {
sim.step(1.0 / 60.0);
}
}
Some(height(&sim) - before)
};
let Some(normal) = rise(1.0, true) else {
eprintln!("skipping: no GPU adapter");
return;
};
let fast = rise(2.5, true).expect("a second simulation");
let stepped = rise(2.5, false).expect("a third simulation");
eprintln!(" dye rose {normal:.2} at 1x, {fast:.2} at 2.5x, {stepped:.2} stepped directly");
assert!(normal > 0.1, "the dye did not rise at all: {normal:.2}");
assert!(
fast > normal * 1.3,
"2.5x speed rose {fast:.2} against {normal:.2}"
);
assert!(
(stepped - normal).abs() < normal * 0.1,
"step honoured the speed knob: {stepped:.2} against {normal:.2}"
);
}
#[test]
fn folding_the_clears_removed_five_passes() {
let config = FluidConfig3d {
resolution: 64,
dye_resolution: 128,
curl: 26.0,
multigrid_cycles: 1,
..FluidConfig3d::default()
};
let Some(mut sim) = sim(config) else {
eprintln!("skipping: no GPU adapter");
return;
};
sim.step(1.0 / 60.0);
let cycle = sim.passes_per_step();
let levels = sim.solver_levels();
let saved = 1 + levels;
eprintln!(" a step is {cycle} passes at 64³ with {levels} levels, {saved} fewer than before");
assert_eq!(
levels, 4,
"the level count moved, so the arithmetic below has"
);
assert!(
(40..=48).contains(&cycle),
"a step is {cycle} passes, which is not the shape this test was written against"
);
sim.set_config(FluidConfig3d {
multigrid_cycles: 2,
..config
});
sim.step(1.0 / 60.0);
let two = sim.passes_per_step();
eprintln!(" two cycles is {two} passes");
assert!(
two > cycle,
"a second V-cycle recorded no extra passes: {two} against {cycle}"
);
sim.set_config(FluidConfig3d {
pressure_solver: PressureSolver::Jacobi,
pressure_iterations: 20,
..config
});
sim.step(1.0 / 60.0);
let jacobi = sim.passes_per_step();
eprintln!(" twenty Jacobi sweeps is {jacobi} passes");
assert!(
jacobi < cycle,
"Jacobi recorded {jacobi} passes against the V-cycle's {cycle}"
);
}
#[test]
fn accumulation_converges_on_a_longer_march() {
let base = FluidConfig3d {
resolution: 32,
dye_resolution: 64,
march_steps: 24,
buoyancy: 40.0,
..FluidConfig3d::default()
};
let Some(mut sim) = sim(base) else {
eprintln!("skipping: no GPU adapter");
return;
};
for frame in 0..14 {
if frame % 3 == 0 {
sim.splat([0.5, 0.2, 0.5], [0.0, 260.0, 0.0], [1.1, 0.4, 0.25]);
}
sim.step(1.0 / 60.0);
}
let (w, h) = (96, 96);
let cam = camera(1.0);
sim.set_config(FluidConfig3d {
march_steps: 24 * 12,
..base
});
let reference = sim.capture_rgba(&cam, w, h);
sim.set_config(base);
let single = sim.capture_rgba(&cam, w, h);
let against_ref = |a: &[u8], reference: &[u8]| -> f64 {
a.iter()
.zip(reference)
.map(|(x, y)| x.abs_diff(*y) as f64)
.sum::<f64>()
/ a.len() as f64
};
for k in [0.25f32, 0.5, 1.0, 2.0, 8.0] {
sim.set_config(FluidConfig3d {
temporal_samples: 8,
temporal_clip: k,
..base
});
let mut acc = Vec::new();
for _ in 0..24 {
acc = sim.capture_rgba(&cam, w, h);
}
eprintln!(
" clip {k:>4} sigma: {:.3}",
against_ref(&acc, &reference)
);
}
sim.set_config(FluidConfig3d {
temporal_samples: 8,
..base
});
let mut accumulated = Vec::new();
for _ in 0..24 {
accumulated = sim.capture_rgba(&cam, w, h);
}
let against = |a: &[u8]| -> f64 {
a.iter()
.zip(&reference)
.map(|(x, y)| x.abs_diff(*y) as f64)
.sum::<f64>()
/ a.len() as f64
};
let (noisy, averaged) = (against(&single), against(&accumulated));
let _ = &against_ref;
eprintln!(
" from a 288-sample march: one 24-sample march is {noisy:.3}/255 away, \
24 accumulated are {averaged:.3}"
);
assert!(
brightest(&reference, w, h).2 > 40,
"the reference render is empty, so nothing below means anything"
);
assert!(
averaged < noisy * 0.75,
"accumulating did not help: {averaged:.3} against {noisy:.3}"
);
}
#[test]
fn skipping_keeps_rays_on_the_sample_lattice() {
let base = FluidConfig3d {
resolution: 32,
dye_resolution: 128,
march_steps: 24,
buoyancy: 40.0,
..FluidConfig3d::default()
};
let render = |skip: bool| -> Option<Vec<u8>> {
let mut sim = sim(FluidConfig3d {
empty_space_skip: skip,
..base
})?;
for frame in 0..16 {
if frame % 3 == 0 {
sim.splat([0.5, 0.2, 0.5], [0.0, 260.0, 0.0], [1.1, 0.4, 0.25]);
}
sim.step(1.0 / 60.0);
}
Some(sim.capture_rgba(&camera(1.0), 128, 128))
};
let Some(skipped) = render(true) else {
eprintln!("skipping: no GPU adapter");
return;
};
let every = render(false).expect("a second simulation");
assert!(
brightest(&every, 128, 128).2 > 40,
"the reference render is empty"
);
let diffs: Vec<f64> = skipped
.iter()
.zip(&every)
.map(|(a, b)| a.abs_diff(*b) as f64)
.collect();
let mean = diffs.iter().sum::<f64>() / diffs.len() as f64;
let over_8 = diffs.iter().filter(|d| **d > 8.0).count();
eprintln!(
" coarse march: mean |diff| {mean:.3}/255, {over_8} of {} channels over 8",
diffs.len()
);
assert_eq!(
over_8, 0,
"skipping moved {over_8} channels by more than 8 levels — the rays \
that skipped are sampling somewhere the rays that did not are not"
);
assert!(
mean < 0.02,
"skipping changed a coarse march by {mean:.3}/255"
);
}
#[test]
fn accumulation_ghosts_a_moving_fluid() {
let base = FluidConfig3d {
resolution: 32,
dye_resolution: 64,
march_steps: 24,
buoyancy: 90.0,
..FluidConfig3d::default()
};
let (w, h) = (96, 96);
let cam = camera(1.0);
let run = |samples: u32, steps: u32| -> Option<Vec<u8>> {
let mut sim = sim(FluidConfig3d {
temporal_samples: samples,
march_steps: steps,
..base
})?;
let mut out = Vec::new();
for frame in 0..30 {
if frame % 3 == 0 && frame < 18 {
sim.splat([0.5, 0.2, 0.5], [0.0, 260.0, 0.0], [1.1, 0.4, 0.25]);
}
sim.step(1.0 / 60.0);
out = sim.capture_rgba(&cam, w, h);
}
Some(out)
};
let Some(truth) = run(1, 24 * 12) else {
eprintln!("skipping: no GPU adapter");
return;
};
let single = run(1, 24).expect("a second simulation");
let against = |a: &[u8]| -> f64 {
a.iter()
.zip(&truth)
.map(|(x, y)| x.abs_diff(*y) as f64)
.sum::<f64>()
/ a.len() as f64
};
let one = against(&single);
eprintln!(" moving fluid, from the truth: one march {one:.3}/255");
for k in [0.25f32, 0.5, 1.0, 2.0, 8.0] {
let mut sim = sim(FluidConfig3d {
temporal_samples: 8,
temporal_clip: k,
..base
})
.expect("a simulation");
let mut out = Vec::new();
for frame in 0..30 {
if frame % 3 == 0 && frame < 18 {
sim.splat([0.5, 0.2, 0.5], [0.0, 260.0, 0.0], [1.1, 0.4, 0.25]);
}
sim.step(1.0 / 60.0);
out = sim.capture_rgba(&cam, w, h);
}
eprintln!(" clip {k:>4} sigma: {:.3}", against(&out));
}
let ghosted = |k: f32| -> f64 {
let mut sim = sim(FluidConfig3d {
temporal_samples: 8,
temporal_clip: k,
..base
})
.expect("a simulation");
let mut out = Vec::new();
for frame in 0..30 {
if frame % 3 == 0 && frame < 18 {
sim.splat([0.5, 0.2, 0.5], [0.0, 260.0, 0.0], [1.1, 0.4, 0.25]);
}
sim.step(1.0 / 60.0);
out = sim.capture_rgba(&cam, w, h);
}
against(&out)
};
let (clipped, unclipped) = (
ghosted(FluidConfig3d::default().temporal_clip),
ghosted(8.0),
);
eprintln!(" at the default clip {clipped:.3}, with none {unclipped:.3}");
assert!(
clipped < unclipped * 0.5,
"rejecting the history barely helped: {clipped:.3} against {unclipped:.3}"
);
assert!(
brightest(&truth, w, h).2 > 40,
"the reference render is empty"
);
}
#[test]
fn what_the_solve_spends_its_passes_on() {
let base = FluidConfig3d {
curl: 0.0,
buoyancy: 0.0,
gradient_scale: 0.5,
..FluidConfig3d::default()
};
let measure = |config: FluidConfig3d| -> Option<(f32, u32)> {
let mut sim = sim(config)?;
sim.splat([0.5, 0.5, 0.5], [300.0, 0.0, 0.0], [0.2, 0.2, 0.2]);
let divergence_now = |sim: &mut FluidSim3d| -> f32 {
let mut enc = sim.encoder("test divergence");
sim.next_ubo.set(0);
let u = sim.params(sim.sim, Vol::default());
let bg = sim.bind(&u, &sim.velocity.read, None);
draw(
&mut enc,
&sim.divergence.view,
Load::Clear,
&sim.pipes.divergence,
&bg,
);
sim.queue.submit([enc.finish()]);
let d = read_channel(sim, &sim.divergence, 0);
d.iter().map(|x| x.abs()).sum::<f32>() / d.len() as f32
};
let raw = divergence_now(&mut sim);
sim.step(1.0 / 6000.0);
Some((divergence_now(&mut sim) / raw, sim.passes_per_step()))
};
let Some(baseline) = measure(base) else {
eprintln!("skipping: no GPU adapter");
return;
};
eprintln!(" default: {:.3} left in {} passes", baseline.0, baseline.1);
for nu in [1u32, 2, 3, 4] {
let (left, passes) = measure(FluidConfig3d {
multigrid_smooth: nu,
..base
})
.expect("a simulation");
eprintln!(" smooth {nu}: {left:.3} left in {passes} passes");
}
let over_frames = |keep: f32| -> f32 {
let mut sim = sim(FluidConfig3d {
pressure: keep,
..base
})
.expect("a simulation");
let divergence_now = |sim: &mut FluidSim3d| -> f32 {
let mut enc = sim.encoder("test divergence");
sim.next_ubo.set(0);
let u = sim.params(sim.sim, Vol::default());
let bg = sim.bind(&u, &sim.velocity.read, None);
draw(
&mut enc,
&sim.divergence.view,
Load::Clear,
&sim.pipes.divergence,
&bg,
);
sim.queue.submit([enc.finish()]);
let d = read_channel(sim, &sim.divergence, 0);
d.iter().map(|x| x.abs()).sum::<f32>() / d.len() as f32
};
let mut raw = 0.0;
for frame in 0..8 {
sim.splat([0.5, 0.5, 0.5], [300.0, 0.0, 0.0], [0.2, 0.2, 0.2]);
if frame == 0 {
raw = divergence_now(&mut sim);
}
sim.step(1.0 / 6000.0);
}
divergence_now(&mut sim) / raw
};
for keep in [0.0f32, 0.5, 0.8, 1.0] {
eprintln!(
" carry over {keep}: {:.3} left after eight frames",
over_frames(keep)
);
}
let one = measure(FluidConfig3d {
multigrid_smooth: 1,
..base
})
.expect("a simulation");
assert!(one.1 < baseline.1, "fewer sweeps recorded no fewer passes");
}
#[test]
fn the_pressure_carry_over_is_damped_against_drift() {
let base = FluidConfig3d {
resolution: 24,
dye_resolution: 24,
buoyancy: 60.0,
..FluidConfig3d::default()
};
let peak = |keep: f32| -> Option<f32> {
let mut sim = sim(FluidConfig3d {
pressure: keep,
..base
})?;
for frame in 0..900 {
if frame % 4 == 0 {
sim.splat([0.5, 0.25, 0.5], [60.0, 300.0, -40.0], [1.0, 0.6, 0.3]);
}
sim.step(1.0 / 60.0);
}
let p = read_channel(&sim, &sim.pressure.read, 0);
Some(p.iter().fold(0.0f32, |m, v| m.max(v.abs())))
};
let Some(damped) = peak(0.8) else {
eprintln!("skipping: no GPU adapter");
return;
};
let undamped = peak(1.0).expect("a second simulation");
let jacobi = |keep: f32| -> f32 {
let mut sim = sim(FluidConfig3d {
pressure: keep,
pressure_solver: PressureSolver::Jacobi,
..base
})
.expect("a simulation");
for frame in 0..900 {
if frame % 4 == 0 {
sim.splat([0.5, 0.25, 0.5], [60.0, 300.0, -40.0], [1.0, 0.6, 0.3]);
}
sim.step(1.0 / 60.0);
}
let p = read_channel(&sim, &sim.pressure.read, 0);
p.iter().fold(0.0f32, |m, v| m.max(v.abs()))
};
eprintln!(
" jacobi, no coarse grid: damped {:.1}, undamped {:.1}",
jacobi(0.8),
jacobi(1.0)
);
eprintln!(" after 900 frames, peak |pressure|: damped {damped:.1}, undamped {undamped:.1}");
for (name, peak) in [("damped", damped), ("undamped", undamped)] {
assert!(
peak.is_finite() && peak < 1000.0,
"the {name} field reached {peak} — the constant mode is drifting \
after all, and `pressure` has to come back down"
);
}
}
#[test]
fn the_read_out_draws_only_where_it_says() {
let base = FluidConfig3d {
resolution: 24,
dye_resolution: 48,
buoyancy: 40.0,
..FluidConfig3d::default()
};
let (w, h) = (128, 128);
let render = |show: bool| -> Option<Vec<u8>> {
let mut sim = sim(FluidConfig3d {
show_fps: show,
..base
})?;
sim.splat([0.5, 0.3, 0.5], [0.0, 200.0, 0.0], [1.0, 0.5, 0.3]);
for _ in 0..6 {
sim.step(1.0 / 60.0);
sim.capture_rgba(&camera(1.0), w, h);
}
Some(sim.capture_rgba(&camera(1.0), w, h))
};
let Some(plain) = render(false) else {
eprintln!("skipping: no GPU adapter");
return;
};
let with = render(true).expect("a second simulation");
let in_read_out = |x: u32, y: u32| {
let digits = x < 10 + 12 * 3 && y < 10 + 5 * 3;
let chart = (10..10 + 64 * 2).contains(&x) && (29..29 + 18).contains(&y);
digits || chart
};
let (mut inside, mut outside) = (0usize, 0usize);
for y in 0..h {
for x in 0..w {
let i = ((y * w + x) * 4) as usize;
if plain[i..i + 4] == with[i..i + 4] {
continue;
}
if in_read_out(x, y) {
inside += 1;
} else {
outside += 1;
}
}
}
eprintln!(" the read-out changed {inside} pixels inside its box and {outside} outside");
assert!(
inside > 20,
"the read-out drew {inside} pixels, so it is not there"
);
assert_eq!(
outside, 0,
"the read-out changed {outside} pixels outside its own box"
);
}
#[test]
fn a_click_lands_where_it_was_aimed() {
let Some(mut sim) = sim(FluidConfig3d {
resolution: 32,
dye_resolution: 64,
buoyancy: 0.0,
curl: 0.0,
splat_radius: 0.3,
..FluidConfig3d::default()
}) else {
eprintln!("skipping: no GPU adapter");
return;
};
let cam = camera(1.0);
let (w, h) = (128, 128);
let aim = [0.62f32, 0.38];
sim.impulse_at_screen(&cam, aim, [0.0, 0.0], 0.0, [1.0, 1.0, 1.0]);
let (bx, by, peak) = brightest(&sim.capture_rgba(&cam, w, h), w, h);
eprintln!(
" aimed at ({:.2}, {:.2}), brightest at ({bx:.2}, {by:.2})",
aim[0], aim[1]
);
assert!(peak > 40, "the click left nothing to see");
assert!(
(bx - aim[0]).abs() < 0.08 && (by - aim[1]).abs() < 0.08,
"aimed at ({:.2}, {:.2}), landed at ({bx:.2}, {by:.2})",
aim[0],
aim[1]
);
let after_drag = |drag: [f32; 2]| -> f32 {
let mut sim = sim2(FluidConfig3d {
resolution: 32,
dye_resolution: 64,
buoyancy: 0.0,
curl: 0.0,
velocity_dissipation: 0.0,
density_dissipation: 0.0,
splat_radius: 0.3,
..FluidConfig3d::default()
})
.expect("a simulation");
sim.impulse_at_screen(&cam, [0.5, 0.5], drag, 6.0, [1.0, 1.0, 1.0]);
for _ in 0..24 {
sim.step(1.0 / 60.0);
}
brightest(&sim.capture_rgba(&cam, w, h), w, h).0
};
let still = after_drag([0.0, 0.0]);
let pushed = after_drag([0.25, 0.0]);
eprintln!(" undragged ends at x {still:.2}, dragged right ends at {pushed:.2}");
assert!(
pushed > still + 0.03,
"a rightward drag did not push right: {pushed:.2} against {still:.2}"
);
}
#[test]
fn a_disturbance_in_one_corner_is_felt_across_the_box() {
let Some(mut sim) = sim(FluidConfig3d {
resolution: 32,
dye_resolution: 32,
curl: 0.0,
buoyancy: 0.0,
..FluidConfig3d::default()
}) else {
eprintln!("skipping: no GPU adapter");
return;
};
sim.splat([0.15, 0.15, 0.15], [400.0, 0.0, 0.0], [0.2, 0.2, 0.2]);
sim.step(1.0 / 6000.0);
let p = read_channel(&sim, &sim.pressure.read, 0);
let atlas = sim.sim;
let (aw, _) = atlas.size();
let (n, across) = (atlas.grid as usize, (aw / atlas.grid) as usize);
let mut shells = [(0.0f64, 0usize); 4];
for z in 0..n {
for y in 0..n {
for x in 0..n {
let (row, col) = (z / across, z % across);
let v = p[(row * n + y) * aw as usize + col * n + x].abs() as f64;
let d = |a: usize, at: f32| (a as f32 / n as f32 - at).powi(2);
let dist = (d(x, 0.15) + d(y, 0.15) + d(z, 0.15)).sqrt() / 3f32.sqrt();
let shell = ((dist * 4.0) as usize).min(3);
shells[shell].0 += v;
shells[shell].1 += 1;
}
}
}
let mean: Vec<f64> = shells
.iter()
.map(|(sum, n)| sum / (*n).max(1) as f64)
.collect();
eprintln!(
" mean |pressure| by distance from the splat: near {:.4}, {:.4}, {:.4}, far {:.4}",
mean[0], mean[1], mean[2], mean[3]
);
eprintln!(
" the far quarter carries {:.0}% of what the near one does",
100.0 * mean[3] / mean[0].max(1e-12)
);
assert!(mean[0] > 1e-6, "the splat produced no pressure at all");
}
#[test]
fn buoyancy_goes_where_it_is_pointed() {
let base = FluidConfig3d {
resolution: 24,
dye_resolution: 24,
buoyancy: 300.0,
curl: 0.0,
velocity_dissipation: 0.0,
density_dissipation: 0.0,
splat_radius: 0.25,
..FluidConfig3d::default()
};
let drift = |axis: [f32; 3]| -> Option<[f32; 3]> {
let mut sim = sim(FluidConfig3d {
buoyancy_axis: axis,
..base
})?;
sim.splat([0.5, 0.5, 0.5], [0.0; 3], [1.0, 1.0, 1.0]);
let centre = |sim: &FluidSim3d| -> [f32; 3] {
let dye = read_channel(sim, &sim.dye.read, 0);
let atlas = sim.dye_atlas();
let (w, _) = atlas.size();
let (n, across) = (atlas.grid as usize, (w / atlas.grid) as usize);
let (mut sum, mut mass) = ([0.0f64; 3], 0.0f64);
for (i, v) in dye.iter().enumerate() {
let (ax, ay) = (i % w as usize, i / w as usize);
let (x, y) = (ax % n, ay % n);
let z = (ay / n) * across + ax / n;
let w = *v as f64;
sum[0] += w * x as f64;
sum[1] += w * y as f64;
sum[2] += w * z as f64;
mass += w;
}
let m = mass.max(1e-9);
[
(sum[0] / m) as f32,
(sum[1] / m) as f32,
(sum[2] / m) as f32,
]
};
let before = centre(&sim);
for _ in 0..40 {
sim.step(1.0 / 60.0);
}
let after = centre(&sim);
Some([
after[0] - before[0],
after[1] - before[1],
after[2] - before[2],
])
};
let Some(up) = drift([0.0, 1.0, 0.0]) else {
eprintln!("skipping: no GPU adapter");
return;
};
let along_x = drift([1.0, 0.0, 0.0]).expect("a second simulation");
let along_z = drift([0.0, 0.0, -1.0]).expect("a third simulation");
eprintln!(" drift up {up:?}");
eprintln!(" drift +x {along_x:?}");
eprintln!(" drift -z {along_z:?}");
assert!(up[1] > 0.4 && up[1] > up[0].abs() && up[1] > up[2].abs());
assert!(along_x[0] > 0.4 && along_x[0] > along_x[1].abs());
assert!(along_z[2] < -0.4 && -along_z[2] > along_z[1].abs());
}
#[test]
fn random_splats_push_in_every_direction() {
let Some(mut sim) = sim(FluidConfig3d {
resolution: 16,
dye_resolution: 16,
..FluidConfig3d::default()
}) else {
eprintln!("skipping: no GPU adapter");
return;
};
sim.multiple_splats(48);
let vy = read_channel(&sim, &sim.velocity.read, 1);
let (up, down) = vy.iter().fold((0, 0), |(u, d), v| {
if *v > 20.0 {
(u + 1, d)
} else if *v < -20.0 {
(u, d + 1)
} else {
(u, d)
}
});
eprintln!(" voxels pushed up {up}, down {down}");
assert!(
up > 0 && down > 0,
"the pushes are one-sided: {up} up against {down} down"
);
let ratio = up.min(down) as f32 / up.max(down).max(1) as f32;
assert!(ratio > 0.3, "lopsided: {up} up, {down} down");
}
#[test]
fn the_two_solvers_agree_about_which_way_is_up() {
let base = FluidConfig3d {
resolution: 24,
dye_resolution: 24,
buoyancy: 300.0,
buoyancy_axis: [0.6, 0.0, -0.8],
curl: 0.0,
velocity_dissipation: 0.0,
density_dissipation: 0.0,
splat_radius: 0.25,
..FluidConfig3d::default()
};
let Some(mut gpu) = sim(base) else {
eprintln!("skipping: no GPU adapter");
return;
};
let mut cpu = crate::fluid::CpuFluid3d::new(base);
gpu.splat([0.5, 0.5, 0.5], [0.0; 3], [1.0, 1.0, 1.0]);
cpu.splat([0.5, 0.5, 0.5], [0.0; 3], [1.0, 1.0, 1.0]);
for _ in 0..30 {
gpu.step(1.0 / 60.0);
cpu.step(1.0 / 60.0);
}
let n = base.dye_resolution as usize;
let gpu_dye = read_channel(&gpu, &gpu.dye.read, 0);
let atlas = gpu.dye_atlas();
let (aw, _) = atlas.size();
let across = (aw / atlas.grid) as usize;
let centre = |sample: &dyn Fn(usize, usize, usize) -> f32| -> [f32; 3] {
let (mut sum, mut mass) = ([0.0f64; 3], 0.0f64);
for z in 0..n {
for y in 0..n {
for x in 0..n {
let w = sample(x, y, z) as f64;
sum[0] += w * x as f64;
sum[1] += w * y as f64;
sum[2] += w * z as f64;
mass += w;
}
}
}
let m = mass.max(1e-9);
std::array::from_fn(|i| (sum[i] / m) as f32)
};
let g = centre(&|x, y, z| {
let (row, col) = (z / across, z % across);
gpu_dye[(row * n + y) * aw as usize + col * n + x]
});
let cpu_dye = cpu.dye();
let c = centre(&|x, y, z| cpu_dye[x + n * (y + n * z)][0]);
eprintln!(" dye centre: gpu {g:?}, cpu {c:?}");
for (i, axis) in ["x", "y", "z"].iter().enumerate() {
assert!(
(g[i] - c[i]).abs() < 1.0,
"the solvers disagree on {axis}: gpu {:.2}, cpu {:.2}",
g[i],
c[i]
);
}
assert!(
g[0] > n as f32 / 2.0 + 0.5 && g[2] < n as f32 / 2.0 - 0.5,
"the dye did not follow the axis: {g:?}"
);
}
#[test]
fn how_much_of_the_solve_is_over_still_fluid() {
let Some(mut sim) = sim(FluidConfig3d {
resolution: 48,
dye_resolution: 96,
buoyancy: 46.0,
curl: 26.0,
density_dissipation: 0.2,
..FluidConfig3d::default()
}) else {
eprintln!("skipping: no GPU adapter");
return;
};
for frame in 0..52 {
if frame < 34 && frame % 3 == 0 {
sim.splat([0.5, 0.11, 0.5], [0.0, 300.0, 0.0], [1.0, 0.4, 0.3]);
}
sim.step(1.0 / 60.0);
}
let d = read_channel(&sim, &sim.divergence, 0);
let atlas = sim.sim;
let (aw, _) = atlas.size();
let (n, across) = (atlas.grid as usize, (aw / atlas.grid) as usize);
let peak = d.iter().fold(0.0f32, |m, v| m.max(v.abs()));
eprintln!(" peak |divergence| {peak:.4}");
for cut in [0.01f32, 0.001] {
let floor = peak * cut;
let (mut active, mut lo, mut hi) = (0usize, [n; 3], [0usize; 3]);
for z in 0..n {
for y in 0..n {
for x in 0..n {
let (row, col) = (z / across, z % across);
if d[(row * n + y) * aw as usize + col * n + x].abs() <= floor {
continue;
}
active += 1;
for (i, v) in [x, y, z].iter().enumerate() {
lo[i] = lo[i].min(*v);
hi[i] = hi[i].max(*v);
}
}
}
}
let cells = n * n * n;
let boxed: usize = (0..3)
.map(|i| hi[i].saturating_sub(lo[i]) + 1)
.product::<usize>()
.min(cells);
eprintln!(
" above {:>6.1}% of peak: {:>5.1}% of voxels active, bounding box {:>5.1}% of the volume",
cut * 100.0,
100.0 * active as f32 / cells as f32,
100.0 * boxed as f32 / cells as f32
);
}
let p = read_channel(&sim, &sim.pressure.read, 0);
let ppeak = p.iter().fold(0.0f32, |m, v| m.max(v.abs()));
for cut in [0.01f32, 0.001] {
let floor = ppeak * cut;
let active = p.iter().filter(|v| v.abs() > floor).count();
let cells = n * n * n;
eprintln!(
" pressure above {:>6.1}% of peak: {:>5.1}% of voxels",
cut * 100.0,
100.0 * active.min(cells) as f32 / cells as f32
);
}
assert!(peak > 1e-5, "the plume produced no divergence to measure");
}
#[test]
fn accumulation_survives_an_orbit() {
let base = FluidConfig3d {
resolution: 32,
dye_resolution: 64,
march_steps: 24,
buoyancy: 40.0,
..FluidConfig3d::default()
};
let (w, h) = (96, 96);
let orbit = |frame: usize| -> PerspectiveCamera {
let a = frame as f32 * 0.02;
let mut c = PerspectiveCamera::new(45.0, 1.0, 0.05, 20.0);
c.position = Vector3::new(1.9 * a.sin(), 0.0, 1.9 * a.cos());
c.look_at(Vector3::ZERO);
c
};
let settle = |sim: &mut FluidSim3d| {
for frame in 0..14 {
if frame % 3 == 0 {
sim.splat([0.5, 0.2, 0.5], [0.0, 260.0, 0.0], [1.1, 0.4, 0.25]);
}
sim.step(1.0 / 60.0);
}
};
const FRAMES: usize = 20;
let Some(mut sim) = sim(base) else {
eprintln!("skipping: no GPU adapter");
return;
};
settle(&mut sim);
sim.set_config(FluidConfig3d {
march_steps: 24 * 12,
..base
});
let truth = sim.capture_rgba(&orbit(FRAMES - 1), w, h);
sim.set_config(base);
let single = sim.capture_rgba(&orbit(FRAMES - 1), w, h);
sim.set_config(FluidConfig3d {
temporal_samples: 8,
..base
});
let mut orbited = Vec::new();
for frame in 0..FRAMES {
orbited = sim.capture_rgba(&orbit(frame), w, h);
}
let against = |a: &[u8]| -> f64 {
a.iter()
.zip(&truth)
.map(|(x, y)| x.abs_diff(*y) as f64)
.sum::<f64>()
/ a.len() as f64
};
let (one, moved) = (against(&single), against(&orbited));
eprintln!(" through an orbit: one march {one:.3}/255, accumulated {moved:.3}");
assert!(
brightest(&truth, w, h).2 > 40,
"the reference render is empty"
);
assert!(
moved < one,
"accumulating through an orbit was no better than not accumulating: \
{moved:.3} against {one:.3}"
);
}