Particles: fluid capacity sizes a packed neighbour arena; the per-lane list carve is gone

ymir_particle_capacities and Box3DSoaParticleCapacities gain fluid (last, 0 default); the lists are CSR rows (nbr_start, nbr_count) in an arena of
fluid * 96 entries, 0 bytes without fluid; a batch of fluid past the capacity returns FULL before a lane is taken. No-fluid worlds carve 28 bytes
a lane instead of 388. Hashes unmoved.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-25 02:44:24 +01:00
commit e71c731bd5
7 changed files with 54 additions and 24 deletions

View file

@ -550,7 +550,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create(
!ymir_soa_finite(gravity_y) || !ymir_soa_finite(gravity_z) || !ymir_soa_finite(hit_threshold) ||
hit_threshold < 0.0f || (particle_capacities != NULL && (particle_capacities->particles < 0 || particle_capacities->distances < 0 ||
particle_capacities->bends < 0 || particle_capacities->attachments < 0 || particle_capacities->tethers < 0 ||
particle_capacities->materials < 0)))
particle_capacities->materials < 0 || particle_capacities->fluid < 0)))
{
return YMIR_SOA_INVALID_ARGUMENT;
}
@ -590,7 +590,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create(
world->contact_scratch = (b3ContactData*)calloc(n, sizeof(b3ContactData));
world->gravity = (b3Vec3){gravity_x, gravity_y, gravity_z};
if (particle_capacities != NULL && (particle_capacities->particles > 0 || particle_capacities->distances > 0 || particle_capacities->bends > 0 ||
particle_capacities->attachments > 0 || particle_capacities->tethers > 0 || particle_capacities->materials > 0))
particle_capacities->attachments > 0 || particle_capacities->tethers > 0 || particle_capacities->materials > 0 || particle_capacities->fluid > 0))
{
world->particles = ymir_particles_create(particle_capacities, capacity);
if (world->particles == NULL)

View file

@ -75,7 +75,7 @@ static void put(float* x, float* y, float* z, int i, v3 v)
// in 1/m^2.
#define YMIR_PBF_EPS 1.0e-4f
// Capacity of a fluid particle's neighbour list: about 33 kernel neighbours at rest (4 r reach), about 45 at the column's 1.34 compression.
// Neighbour-list entries a fluid lane adds to the arena (CSR, packed): about 33 kernel neighbours at rest (4 r reach), about 45 at the column's 1.34 compression.
#define YMIR_FLUID_NEIGHBOURS 96
// Capacity of the stack buffer a fluid row gathers its candidates into before the pair arithmetic; hits past it count in fluid_overflow.
@ -106,7 +106,7 @@ static size_t layout(ymir_particles* p, unsigned char* base)
CARVE(phase, uint32_t, np); CARVE(material, uint32_t, np); CARVE(particle_live, uint32_t, np);
CARVE(particle_generation, uint32_t, np); CARVE(particle_removed_at, uint64_t, np); CARVE(component, int32_t, np);
CARVE(density, float, np);
CARVE(nbr, int32_t, np * YMIR_FLUID_NEIGHBOURS); CARVE(nbr_count, int32_t, np);
CARVE(nbr, int32_t, (int64_t)p->fluid_capacity * YMIR_FLUID_NEIGHBOURS); CARVE(nbr_start, int32_t, np); CARVE(nbr_count, int32_t, np);
CARVE(dist_a, int32_t, nd); CARVE(dist_b, int32_t, nd); CARVE(dist_rest, float, nd); CARVE(dist_compliance, float, nd);
CARVE(dist_tension, float, nd); CARVE(dist_live, uint32_t, nd); CARVE(dist_generation, uint32_t, nd);
CARVE(dist_removed_at, uint64_t, nd);
@ -142,7 +142,7 @@ static size_t layout(ymir_particles* p, unsigned char* base)
ymir_particles* ymir_particles_create(const ymir_particle_capacities* c, int32_t body_capacity)
{
if (c == NULL || c->particles < 0 || c->distances < 0 || c->bends < 0 || c->attachments < 0 || c->tethers < 0 ||
c->materials < 0 || body_capacity < 0)
c->materials < 0 || c->fluid < 0 || body_capacity < 0)
{
return NULL;
}
@ -158,6 +158,7 @@ ymir_particles* ymir_particles_create(const ymir_particle_capacities* c, int32_t
p->tether_capacity = c->tethers;
p->material_capacity = c->materials;
p->body_capacity = body_capacity;
p->fluid_capacity = c->fluid;
uint32_t table = 16;
while (table < 2u * (uint32_t)c->particles)
{
@ -752,6 +753,15 @@ int32_t ymir_particles_create_particles(ymir_particles* p, int32_t n, const ymir
return YMIR_PARTICLES_INVALID_ARGUMENT;
}
}
int32_t fluid = 0; // the fluid capacity sizes the neighbour arena: a batch past it is refused whole
for (int32_t k = 0; k < n; ++k)
{
fluid += (d[k].phase & YMIR_PHASE_FLUID) != 0;
}
if (p->live_fluid + fluid > p->fluid_capacity)
{
return YMIR_PARTICLES_FULL;
}
if (free_count(p->particle_live, 0, p->particle_removed_at, p->particle_count, p->particle_capacity, step_count) < n)
{
return YMIR_PARTICLES_FULL;
@ -1435,7 +1445,7 @@ static void fluid_density(ymir_particles* p)
p->dp_x[i] = p->dp_y[i] = p->dp_z[i] = 0.0f;
}
float rmax = fluid_grid(p);
int32_t rings = p->fluid_rings;
int32_t rings = p->fluid_rings, used = 0, arena = p->fluid_capacity * YMIR_FLUID_NEIGHBOURS;
for (int32_t qi = 0; qi < p->live_particles; ++qi)
{
if (!(p->sphase[qi] & YMIR_PHASE_FLUID))
@ -1467,7 +1477,8 @@ static void fluid_density(ymir_particles* p)
}
}
p->fluid_overflow += hits - m;
int32_t* list = p->nbr + (size_t)i * YMIR_FLUID_NEIGHBOURS;
int32_t* list = p->nbr + used;
p->nbr_start[i] = used;
int32_t count = 0;
for (int32_t k = 0; k < m; ++k)
{
@ -1482,7 +1493,7 @@ static void fluid_density(ymir_particles* p)
{
continue;
}
if (count < YMIR_FLUID_NEIGHBOURS)
if (used + count < arena)
{
list[count++] = j;
}
@ -1499,6 +1510,7 @@ static void fluid_density(ymir_particles* p)
}
}
p->nbr_count[i] = count;
used += count;
float c = vmax(n / n0 - 1.0f, 0.0f);
p->density[i] = n / n0;
float den = (wi * vdot(gsum, gsum) + ssum) / (n0 * n0) + YMIR_PBF_EPS * wi;
@ -1510,7 +1522,7 @@ static void fluid_density(ymir_particles* p)
{
continue;
}
const int32_t* list = p->nbr + (size_t)i * YMIR_FLUID_NEIGHBOURS;
const int32_t* list = p->nbr + p->nbr_start[i];
for (int32_t k = 0; k < p->nbr_count[i]; ++k)
{
int32_t j = list[k];
@ -1545,7 +1557,7 @@ static void fluid_density(ymir_particles* p)
{
continue;
}
const int32_t* list = p->nbr + (size_t)i * YMIR_FLUID_NEIGHBOURS;
const int32_t* list = p->nbr + p->nbr_start[i];
for (int32_t k = 0; k < p->nbr_count[i]; ++k)
{
if (!(p->phase[list[k]] & YMIR_PHASE_FLUID))
@ -1615,7 +1627,7 @@ static void fluid_viscosity(ymir_particles* p)
{
continue;
}
const int32_t* list = p->nbr + (size_t)i * YMIR_FLUID_NEIGHBOURS;
const int32_t* list = p->nbr + p->nbr_start[i];
for (int32_t k = 0; k < p->nbr_count[i]; ++k)
{
int32_t j = list[k];

View file

@ -81,7 +81,7 @@ typedef struct ymir_handle
typedef struct ymir_particle_capacities
{
int32_t particles, distances, bends, attachments, tethers, materials;
int32_t particles, distances, bends, attachments, tethers, materials, fluid; // fluid: the most live particles with the FLUID phase
} ymir_particle_capacities;
// A particle to create. `mass` kg (0 = immovable); `velocity` m/s; `rest` is its place in the rest pose (rest lengths, bends and
@ -155,7 +155,7 @@ typedef struct ymir_particle_proxy
typedef struct ymir_particles
{
int32_t particle_capacity, distance_capacity, bend_capacity, attachment_capacity, tether_capacity, material_capacity;
int32_t body_capacity;
int32_t body_capacity, fluid_capacity; // fluid_capacity: the most live fluid particles; it sizes the neighbour arena
int32_t particle_count, distance_count, bend_count, attachment_count; // high-water marks
int32_t tether_count; // derived: the live tethers
int32_t body_count; // bodies filled in `proxies` this step
@ -171,7 +171,7 @@ typedef struct ymir_particles
uint32_t *phase, *material, *particle_live, *particle_generation;
uint64_t* particle_removed_at;
int32_t* component; // derived: the lowest lane of the particle's distance-connected component
int32_t *nbr, *nbr_count; // scratch: a fluid particle's kernel neighbours in walk order (YMIR_FLUID_NEIGHBOURS each), this substep's
int32_t *nbr, *nbr_start, *nbr_count; // scratch: the fluid rows' kernel neighbours in walk order, packed (CSR) in an arena of fluid_capacity * YMIR_FLUID_NEIGHBOURS; row i holds nbr_count[i] from nbr_start[i], this substep's
float* density; // sensor: a fluid particle's number density over its rest density n_i / n0 in the last substep it ran, else 0
// distance lanes

View file

@ -109,7 +109,7 @@ public static class Box3DSoaPhase
/// <summary>Capacities of the particle state; <c>null</c> or all zero is a world without particles (ABI 8 behaviour).</summary>
[StructLayout(LayoutKind.Sequential)]
public readonly record struct Box3DSoaParticleCapacities(int Particles, int Distances, int Bends, int Attachments, int Tethers, int Materials);
public readonly record struct Box3DSoaParticleCapacities(int Particles, int Distances, int Bends, int Attachments, int Tethers, int Materials, int Fluid = 0);
/// <summary>Native layout of <c>ymir_particle_desc</c>: a particle to create. Mass kg (0 = immovable); rest is its place in the rest pose.</summary>
[StructLayout(LayoutKind.Sequential)]

View file

@ -396,7 +396,7 @@ public sealed class Box3DSoaCouplingTests
/// <summary>The 16 x 16 sheet cost scene with one body, plus <paramref name="fluid"/> fluid particles (r 1.25 cm) of XSPH viscosity <paramref name="viscosity"/> poured on the sheet; ms per tick at 4 substeps.</summary>
private static double PourCost(int fluid, float viscosity = 0f)
{
using var world = new Box3DSoaWorld(64, gravityY: -G, particles: new Box3DSoaParticleCapacities(16 * 48 + 256 + fluid, 16 * 47 + 480, 16 * 46 + 448, 16, 600, 2));
using var world = new Box3DSoaWorld(64, gravityY: -G, particles: new Box3DSoaParticleCapacities(16 * 48 + 256 + fluid, 16 * 47 + 480, 16 * 46 + 448, 16, 600, 2, fluid));
world.AddStaticBox(1, (0f, -0.5f, 0f), (20f, 0.5f, 20f));
world.SetMaterial(0, 0f, 0, viscosity);
for (var c = 0; c < 16; c++)

View file

@ -36,7 +36,7 @@ public sealed class Box3DSoaFluidTests
private const float FluidMass = 0.015625f; // (2 r)^3 of water: 15.6 mL
private static Box3DSoaWorld FluidWorld(int particles, int bodies = 8, float gravityY = -10f, int materials = 2) =>
new(bodies, gravityY: gravityY, particles: new Box3DSoaParticleCapacities(particles, 16, 16, 16, 64, materials));
new(bodies, gravityY: gravityY, particles: new Box3DSoaParticleCapacities(particles, 16, 16, 16, 64, materials, particles));
/// <summary>A floor and four walls: an open well of interior half-width <paramref name="half"/> centred on the y axis.</summary>
private static void Well(Box3DSoaWorld world, float half)
@ -48,10 +48,28 @@ public sealed class Box3DSoaFluidTests
world.AddStaticBox(5, (0f, 1f, -half - 0.25f), (1f, 1f, 0.25f), friction: 0.3f);
}
[Fact]
public void Fluid_capacity_refuses_a_batch_whole()
{
// The fluid capacity sizes the neighbour arena: fluid past it is refused before any lane is taken, solids still go in, and a
// destroyed fluid particle frees its place at once.
using var world = Box3DSoaParticleTests.World(particles: 16, distances: 16, bends: 16, attachments: 4, tethers: 64, fluid: 3);
var ps = new Box3DSoaParticleHandle[2];
world.CreateParticles([Fluid((0f, 0f, 0f)), Fluid((0.05f, 0f, 0f))], ps);
Assert.Throws<InvalidOperationException>(() => world.CreateParticles([Fluid((0.1f, 0f, 0f)), Fluid((0.15f, 0f, 0f))]));
Assert.Equal(2, world.View.ParticleCount);
Assert.Equal(14, world.FreeParticleSlots);
world.CreateParticles([new((0f, 0.1f, 0f), 0.02f, 0.0125f), Fluid((0.1f, 0f, 0f))]);
Assert.Equal(4, world.View.ParticleCount);
Assert.Throws<InvalidOperationException>(() => world.CreateParticles([Fluid((0.15f, 0f, 0f))]));
world.DestroyParticles([ps[0]]);
world.CreateParticles([Fluid((0.15f, 0f, 0f))]);
}
[Fact]
public void Fluid_refusals()
{
using var world = Box3DSoaParticleTests.World(particles: 16, distances: 16, bends: 16, attachments: 4, tethers: 64);
using var world = Box3DSoaParticleTests.World(particles: 16, distances: 16, bends: 16, attachments: 4, tethers: 64, fluid: 16);
var ps = new Box3DSoaParticleHandle[3];
world.CreateParticles([new((0f, 0f, 0f), 0.02f, 0.0125f), Fluid((0.05f, 0f, 0f)), Fluid((0.1f, 0f, 0f))], ps);
var view = world.View;
@ -86,7 +104,7 @@ public sealed class Box3DSoaFluidTests
// A 32 x 32 sheet at 20 pins, then 64 fluid particles created every tick for 60 ticks and destroyed the tick after: creating a
// particle changes no constraint and destroying fluid touches none, so the tether lanes never change and the full derivation
// (3.6 to 4.0 ms on the 20th pin) is never run for them.
using var world = Box3DSoaParticleTests.World(particles: 1024 + 256, distances: 2048, bends: 2048, attachments: 32, tethers: 4096);
using var world = Box3DSoaParticleTests.World(particles: 1024 + 256, distances: 2048, bends: 2048, attachments: 32, tethers: 4096, fluid: 1024);
const float sp = 0.04f;
var sheet = Graph.Sheet(world, (0f, 2f, 0f), (sp, 0f, 0f), (0f, -sp, 0f), 32, 32, 0.02f, 0.02f);
var held = new HashSet<int>();
@ -131,7 +149,7 @@ public sealed class Box3DSoaFluidTests
public void Destroying_a_lone_attached_particle_leaves_the_tether_lanes_as_before_the_attach()
{
// A particle with a tether attachment and no distance: does it make a tether lane, and does destroying it leave one behind?
using var world = Box3DSoaParticleTests.World(particles: 16, distances: 16, bends: 16, attachments: 4, tethers: 64);
using var world = Box3DSoaParticleTests.World(particles: 16, distances: 16, bends: 16, attachments: 4, tethers: 64, fluid: 16);
var ps = new Box3DSoaParticleHandle[2];
world.CreateParticles([new((0f, 0f, 0f), 0.02f, 0.0125f), new((1f, 0f, 0f), 0.02f, 0.0125f)], ps);
var before = Snapshot(world.View);
@ -154,7 +172,7 @@ public sealed class Box3DSoaFluidTests
/// </summary>
private static Box3DSoaWorld Collision(float fluidMass, float solidMass)
{
var world = new Box3DSoaWorld(4, gravityY: 0f, particles: new Box3DSoaParticleCapacities(1024, 1024, 1024, 8, 64, 2));
var world = new Box3DSoaWorld(4, gravityY: 0f, particles: new Box3DSoaParticleCapacities(1024, 1024, 1024, 8, 64, 2, 1024));
world.AddStaticBox(1, (0f, -100f, 0f), (1f, 1f, 1f));
Graph.Sheet(world, (0f, 0f, 0f), (0.04f, 0f, 0f), (0f, 0.04f, 0f), 16, 16, solidMass, 0.02f, Box3DSoaPhase.Of(7, true, true));
Graph.Rope(world, Graph.Line((-0.3f, 0.25f, 0.3f), (0.03f, 0f, 0f), 48), solidMass, 0.0125f, Box3DSoaPhase.Of(8, true, true), bend: 1e-5f);
@ -198,7 +216,7 @@ public sealed class Box3DSoaFluidTests
/// </summary>
private static (Box3DSoaWorld World, Made Sheet, Made Rope, Box3DSoaParticleHandle[] Fluid) Pour(bool nudge = false, bool solid = false)
{
var world = new Box3DSoaWorld(4, gravityY: -9.81f, particles: new Box3DSoaParticleCapacities(640, 560, 520, 8, 1024, 2)); // the probe's g
var world = new Box3DSoaWorld(4, gravityY: -9.81f, particles: new Box3DSoaParticleCapacities(640, 560, 520, 8, 1024, 2, 640)); // the probe's g
world.AddStaticBox(1, (0f, -50f, 0f), (1f, 1f, 1f));
world.SetMaterial(1, 0.3f, 0, 0.01f); // the probe's XSPH coefficient
const float sp = 0.04f, r = 0.02f;

View file

@ -67,9 +67,9 @@ public sealed class Box3DSoaParticleTests
internal static Box3DSoaWorld World(
int bodies = 8, int particles = 256, int distances = 600, int bends = 600, int attachments = 8, int tethers = 600, int joints = 2,
float gravityX = 0f, float gravityY = -10f, float gravityZ = 0f) =>
float gravityX = 0f, float gravityY = -10f, float gravityZ = 0f, int fluid = 0) =>
new(bodies, gravityX: gravityX, gravityY: gravityY, gravityZ: gravityZ, jointCapacity: joints,
particles: new Box3DSoaParticleCapacities(particles, distances, bends, attachments, tethers, 2));
particles: new Box3DSoaParticleCapacities(particles, distances, bends, attachments, tethers, 2, fluid));
internal static void Run(Box3DSoaWorld world, int steps)
{