Liquid sensors read pooled liquid: sensor_wet_volume, the wet patches whose centre is inside, through the wet index; readings are end-of-step

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-25 08:24:09 +01:00
commit d82da6d1ff
6 changed files with 227 additions and 12 deletions

View file

@ -224,11 +224,14 @@ separate native object (`ymir_box3d_soa.c`, ABI 15) so the Aetheria session's co
(speed, age, film depth: `lodur:follow_up:bds-blood-settle-policy`); Ymir holds the data and the command.
- Sensors (ABI 15). A liquid sensor is a sphere fixed in a body that reads the liquid volume inside it: `create_sensors(n, desc{body, point, radius})` (`Box3DSoaWorld.CreateSensors`; `point` in the body's frame, `radius` m,
capacity `sensors`, `YMIR_LANES_SENSOR` = 5 for `remove` and `free_slots`, lanes tombstoned and reused as the others) refuses the whole batch, changing nothing, for a body outside the world's body count, a radius that is not
finite and > 0, a non-finite point, or too few free lanes. Lanes `sensor_body sensor_x sensor_y sensor_z sensor_radius sensor_liquid_volume sensor_live sensor_generation sensor_removed_at`; a free lane reads
finite and > 0, a non-finite point, or too few free lanes. Lanes `sensor_body sensor_x sensor_y sensor_z sensor_radius sensor_liquid_volume sensor_wet_volume sensor_live sensor_generation sensor_removed_at`; a free lane reads
live 0, body -1 and 0 elsewhere. At the end of every step `ymir_particles_end` writes `sensor_liquid_volume` (m^3): for each live sensor in lane order, the sum in lane order of `(2 r)^3` over the live fluid
particles whose centre lies within `radius` (distance squared <= radius squared) of the sensor's centre, the body's proxy position plus the proxy rotation applied to `point`, in the pose the last substep left the
proxy in; 0 when no fluid is live. The pass runs before `ymir_particles_end`'s early return for no live particle, so a sensor never keeps a reading once the liquid is gone. The reading is a sensor: the step
writes it and no pass reads it back, so a world with sensors steps byte for byte as one without, and a world with none steps as ABI 14. `remove(SENSOR)` ends in no topology derivation. Ymir names no organ
proxy in; 0 when no fluid is live. A second lane, `sensor_wet_volume` (`SensorWetVolume`), is the pooled liquid: the sum of the volumes of the live wet patches whose centre lies within `radius` of that centre, on any body or
solid particle (a body's patch centre is in its body's frame: the sphere is taken into that frame, culled against the body's disc box and walked through the wet index by centre x, bodies ascending; a patch on a solid particle
sits at the particle's position and is summed after, in wet lane order, by a scan of the wet lanes, which have no index). Two numbers, not a sum, so a consumer weighs pooled and moving liquid apart
(`lodur:ruling:liquid-sensor-reads-pooled-liquid`). Both are end-of-step readings: a `settle`, `destroy_particles` or `remove` between steps shows at the next step, which is the contract. The pass runs before `ymir_particles_end`'s early return for no live particle, so a sensor never keeps a reading once the liquid is gone. The readings are sensors: the step
writes them and no pass reads them back, so a world with sensors steps byte for byte as one without, and a world with none steps as ABI 14. `remove(SENSOR)` ends in no topology derivation. Ymir names no organ
(a head's airway, a lung): the consumer places a sensor and maps the volume (`lodur:follow_up:lodur-airway-liquid`). Gives up: a sphere only, not a shape-exact volume, and no per-liquid split.
- Wet friction. Settled liquid lowers friction where it lands, per contact, never per shape: `wet_mu(body, point, mu)` is `min(mu, wet_friction)` of the lowest live patch on that body whose
disc holds the point (the point in the body's frame, as `wet_x wet_y wet_z` are), and `mu` at once when the body's `wet_volume` is 0. It is the one patch test. A particle that touches a body

View file

@ -297,14 +297,16 @@ typedef struct ymir_box3d_soa_view
const float* body_wet_volume;
const float* material_wet_friction;
// Liquid sensors, indexed up to sensor_count: a sphere (point in the body's frame, radius m) fixed in body lane sensor_body; a free lane reads live 0, body -1 and 0 elsewhere.
// liquid_volume (m^3) is the sum of (2 r)^3 over the live fluid particles whose centre was inside it at the end of the last step, 0 with no liquid: a sensor, the
// step writes it and nothing reads it back.
// liquid_volume (m^3) is the sum of (2 r)^3 over the live fluid particles whose centre was inside it at the end of the last step, 0 with no liquid; wet_volume (m^3) is the sum of the
// volumes of the live wet patches whose centre was inside it then, on any body or solid particle. End-of-step values: a Settle, destroy or remove between steps shows at the next step.
// Sensors: the step writes them and nothing reads them back.
const int32_t* sensor_body;
const float* sensor_x;
const float* sensor_y;
const float* sensor_z;
const float* sensor_radius;
const float* sensor_liquid_volume;
const float* sensor_wet_volume;
const uint32_t* sensor_live;
const uint32_t* sensor_generation;
const uint64_t* sensor_removed_at;
@ -1658,6 +1660,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_get_view(const ymir_box3d_soa_world
view->sensor_z = pt->sensor_z;
view->sensor_radius = pt->sensor_radius;
view->sensor_liquid_volume = pt->sensor_liquid_volume;
view->sensor_wet_volume = pt->sensor_wet_volume;
view->sensor_live = pt->sensor_live;
view->sensor_generation = pt->sensor_generation;
view->sensor_removed_at = pt->sensor_removed_at;

View file

@ -115,7 +115,7 @@ static size_t layout(ymir_particles* p, unsigned char* base)
CARVE(wet_generation, uint32_t, nw); CARVE(wet_removed_at, uint64_t, nw); CARVE(wet_volume_body, float, nx); CARVE(particle_wet_mu, float, np);
CARVE(wet_order, int32_t, nw); CARVE(wet_body_start, int32_t, nx); CARVE(wet_body_count, int32_t, nx); CARVE(wet_body_box, float, (int64_t)nx * 7);
CARVE(sensor_body, int32_t, ns); CARVE(sensor_x, float, ns); CARVE(sensor_y, float, ns); CARVE(sensor_z, float, ns); CARVE(sensor_radius, float, ns);
CARVE(sensor_liquid_volume, float, ns); CARVE(sensor_live, uint32_t, ns); CARVE(sensor_generation, uint32_t, ns); CARVE(sensor_removed_at, uint64_t, ns);
CARVE(sensor_liquid_volume, float, ns); CARVE(sensor_wet_volume, float, ns); CARVE(sensor_live, uint32_t, ns); CARVE(sensor_generation, uint32_t, ns); CARVE(sensor_removed_at, uint64_t, ns);
CARVE(wu_code, int32_t, nf); CARVE(wu_x, float, nf); CARVE(wu_y, float, nf); CARVE(wu_z, float, nf); CARVE(wu_volume, float, nf);
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);
@ -356,7 +356,7 @@ static void kill_sensor(ymir_particles* p, int32_t s, uint64_t step_count)
p->sensor_live[s] = 0;
p->sensor_body[s] = -1;
p->sensor_x[s] = p->sensor_y[s] = p->sensor_z[s] = 0.0f;
p->sensor_radius[s] = p->sensor_liquid_volume[s] = 0.0f;
p->sensor_radius[s] = p->sensor_liquid_volume[s] = p->sensor_wet_volume[s] = 0.0f;
p->sensor_removed_at[s] = step_count;
p->sensor_generation[s] += 1;
}
@ -2359,6 +2359,64 @@ void ymir_particles_substep(ymir_particles* p, float h, float gx, float gy, floa
}
}
// The volume of the live wet patches whose centre lies within r of the world point c. A body's patches go through the wet cut's index: the sphere is taken into the
// body's frame (where its patches' centres are), culled against the box of the body's discs, and only centres within r of its x are walked, in the index's order
// (bodies ascending, then centre x). Patches on solid particles have no index and are summed after, in wet lane order, at their particle's position.
static float pooled_volume(const ymir_particles* p, v3 c, float r)
{
float total = 0.0f;
float r2 = r * r;
for (int32_t b = 0; b < p->body_count && b < p->body_capacity; ++b)
{
if (p->wet_body_count[b] == 0)
{
continue;
}
const ymir_particle_proxy* body = p->proxies + b;
v3 l = qrot_inv(body->rotation, vsub(c, (v3){body->position[0], body->position[1], body->position[2]}));
const float* box = p->wet_body_box + 7 * (int64_t)b;
if (l.x + r < box[0] || l.x - r > box[3] || l.y + r < box[1] || l.y - r > box[4] || l.z + r < box[2] || l.z - r > box[5])
{
continue;
}
const int32_t* slice = p->wet_order + p->wet_body_start[b];
int32_t lo = 0, hi = p->wet_body_count[b];
while (lo < hi)
{
int32_t mid = (lo + hi) / 2;
if (p->wet_x[slice[mid]] < l.x - r)
{
lo = mid + 1;
}
else
{
hi = mid;
}
}
for (int32_t k = lo; k < p->wet_body_count[b] && p->wet_x[slice[k]] <= l.x + r; ++k)
{
int32_t w = slice[k];
v3 d = {l.x - p->wet_x[w], l.y - p->wet_y[w], l.z - p->wet_z[w]};
if (vdot(d, d) <= r2)
{
total += p->wet_volume[w];
}
}
}
for (int32_t w = 0; w < p->wet_count; ++w)
{
if (p->wet_live[w] && p->wet_particle[w] >= 0 && p->wet_particle[w] < p->particle_count && p->particle_live[p->wet_particle[w]])
{
v3 d = vsub(P(p, p->pos_x, p->pos_y, p->pos_z, p->wet_particle[w]), c);
if (vdot(d, d) <= r2)
{
total += p->wet_volume[w];
}
}
}
return total;
}
// Each live sensor reads the liquid inside it: the sum, in lane order, of (2 r)^3 over the live fluid particles whose centre lies within its sphere, against the
// pose the last substep left each proxy in. It runs whether or not any particle is live, so a sensor never keeps a reading once the liquid is gone.
static void read_sensors(ymir_particles* p)
@ -2370,12 +2428,14 @@ static void read_sensors(ymir_particles* p)
continue;
}
float volume = 0.0f;
if (p->live_fluid > 0 && p->sensor_body[s] < p->body_count)
float pooled = 0.0f;
if (p->sensor_body[s] < p->body_count)
{
const ymir_particle_proxy* b = p->proxies + p->sensor_body[s];
v3 c = vadd((v3){b->position[0], b->position[1], b->position[2]}, qrot(b->rotation, (v3){p->sensor_x[s], p->sensor_y[s], p->sensor_z[s]}));
float r2 = p->sensor_radius[s] * p->sensor_radius[s];
for (int32_t i = 0; i < p->particle_count; ++i)
pooled = pooled_volume(p, c, p->sensor_radius[s]);
for (int32_t i = 0; p->live_fluid > 0 && i < p->particle_count; ++i)
{
if (!p->particle_live[i] || !(p->phase[i] & YMIR_PHASE_FLUID))
{
@ -2390,6 +2450,7 @@ static void read_sensors(ymir_particles* p)
}
}
p->sensor_liquid_volume[s] = volume;
p->sensor_wet_volume[s] = pooled;
}
}
@ -2601,7 +2662,7 @@ int32_t ymir_particles_create_sensors(ymir_particles* p, int32_t n, const ymir_s
p->sensor_y[s] = d[k].point[1];
p->sensor_z[s] = d[k].point[2];
p->sensor_radius[s] = d[k].radius;
p->sensor_liquid_volume[s] = 0.0f; // the first step's end reads it
p->sensor_liquid_volume[s] = p->sensor_wet_volume[s] = 0.0f; // the first step's end reads them
if (s >= p->sensor_count)
{
p->sensor_count = s + 1;

View file

@ -203,9 +203,10 @@ typedef struct ymir_particles
float *wu_x, *wu_y, *wu_z, *wu_volume;
// sensor lanes: a sphere fixed in a body; a free lane reads live 0, body -1 and 0 elsewhere except generation and removed_at. Written by create_sensors and
// remove; sensor_liquid_volume is written by end alone (m^3: the sum of (2 r)^3 over the live fluid particles whose centre is inside, at the step's end)
// remove; sensor_liquid_volume and sensor_wet_volume are written by end alone, at the step's end (m^3): the sum of (2 r)^3 over the live fluid particles whose centre is inside,
// and the sum of the volumes of the live wet patches whose centre is inside, on any body or solid particle
int32_t* sensor_body;
float *sensor_x, *sensor_y, *sensor_z, *sensor_radius, *sensor_liquid_volume;
float *sensor_x, *sensor_y, *sensor_z, *sensor_radius, *sensor_liquid_volume, *sensor_wet_volume;
uint32_t *sensor_live, *sensor_generation;
uint64_t* sensor_removed_at;

View file

@ -379,6 +379,7 @@ internal unsafe struct Box3DSoaViewNative
public float* SensorZ;
public float* SensorRadius;
public float* SensorLiquidVolume;
public float* SensorWetVolume;
public uint* SensorLive;
public uint* SensorGeneration;
public ulong* SensorRemovedAt;
@ -823,6 +824,8 @@ public readonly unsafe struct Box3DSoaView
public ReadOnlySpan<float> SensorRadius => Lane(_v.SensorRadius, SensorCount);
/// <summary>m^3 of liquid inside each sensor at the end of the last step: the sum of (2 r)^3 over the live fluid particles whose centre was inside, 0 with none. A sensor: the step writes it and nothing reads it back.</summary>
public ReadOnlySpan<float> SensorLiquidVolume => Lane(_v.SensorLiquidVolume, SensorCount);
/// <summary>m^3 of settled liquid at the end of the last step: the sum of the volumes of the live wet patches whose centre was inside each sensor, on any body or solid particle. A Settle, destroy or remove between steps shows at the next step.</summary>
public ReadOnlySpan<float> SensorWetVolume => Lane(_v.SensorWetVolume, SensorCount);
public ReadOnlySpan<uint> SensorLive => Lane(_v.SensorLive, SensorCount);
public ReadOnlySpan<uint> SensorGeneration => Lane(_v.SensorGeneration, SensorCount);
public ReadOnlySpan<ulong> SensorRemovedAt => Lane(_v.SensorRemovedAt, SensorCount);

View file

@ -0,0 +1,144 @@
using Ymir.Box3D;
namespace Ymir.Box3D.Soa.Tests;
using Pos = (float X, float Y, float Z);
/// <summary>Liquid sensors read settled liquid too (lodur:ruling:liquid-sensor-reads-pooled-liquid): the wet patches whose centre is inside the sphere, on any body or solid particle, in a lane of their own.</summary>
public sealed partial class Box3DSoaFluidTests
{
/// <summary>The volume the pooled lane must read, by a scan of every wet lane (what the sensor's culled lookup must equal): the patches' volumes summed in lane order over the live patches whose centre, taken to the world, is within <paramref name="radius"/> of <paramref name="centre"/>; with the closest any centre comes to the surface.</summary>
private static (float Volume, int Count, double Margin) PooledInside(Box3DSoaView v, Pos centre, float radius)
{
var volume = 0f;
var count = 0;
var margin = double.MaxValue;
for (var w = 0; w < v.WetCount; w++)
{
if (v.WetLive[w] == 0) continue;
Pos at;
if (v.WetBody[w] >= 0)
{
var b = v.WetBody[w];
var (qx, qy, qz, qw) = (v.RotationX[b], v.RotationY[b], v.RotationZ[b], v.RotationW[b]);
var (px, py, pz) = (v.WetX[w], v.WetY[w], v.WetZ[w]);
var (tx, ty, tz) = (qy * pz - qz * py + qw * px, qz * px - qx * pz + qw * py, qx * py - qy * px + qw * pz);
at = (v.PositionX[b] + px + 2f * (qy * tz - qz * ty), v.PositionY[b] + py + 2f * (qz * tx - qx * tz), v.PositionZ[b] + pz + 2f * (qx * ty - qy * tx));
}
else
{
var i = v.WetParticle[w];
at = (v.ParticleX[i], v.ParticleY[i], v.ParticleZ[i]);
}
double dx = at.X - centre.X, dy = at.Y - centre.Y, dz = at.Z - centre.Z;
var d = Math.Sqrt(dx * dx + dy * dy + dz * dz);
margin = Math.Min(margin, Math.Abs(d - radius));
if (d <= radius)
{
volume += v.WetPatchVolume[w];
count++;
}
}
return (volume, count, margin);
}
/// <summary>A floor (body lane 0, frame origin (0, -0.5, 0)) with one drop on it at each of <paramref name="at"/> (x, z), rested 20 ticks so each reads its support.</summary>
private static (Box3DSoaWorld World, Box3DSoaParticleHandle[] Drops) DropsOnFloor(Pos[] at, int wets, int sensors)
{
var world = FluidWorld(at.Length + 8, 8, gravityY: -9.81f, wets: wets, sensors: sensors);
world.AddStaticBox(1, (0f, -0.5f, 0f), (2f, 0.5f, 2f), friction: 0.3f);
var desc = at.Select(p => new Box3DSoaParticleDesc((p.X, R, p.Z), FluidMass, R, FluidPhase, 0u, null, default)).ToArray();
world.CreateParticles(desc);
Run(world, 20);
return (world, Enumerable.Range(0, at.Length).Select(i => Handle(world, i)).ToArray());
}
[Fact]
public void Settled_liquid_in_a_sphere_reads_in_the_pooled_lane_and_0_live()
{
var (world, drops) = DropsOnFloor([(-0.4f, 0f, 0f), (-0.2f, 0f, 0f), (0.2f, 0f, 0f), (0.4f, 0f, 0f)], 8, 4);
using var _ = world;
var anchor = world.AddAnchor(9, (0.2f, 0.3f, 0f));
var h = new Box3DSoaConstraintHandle[3];
// floor frame: body-frame y 0.5 is the floor's top. A holds the drops at x -0.4 and -0.2; B (r 5 cm) none; C is on the anchor, so the patch at x 0.2 is on another body.
world.CreateSensors([(0, (-0.3f, 0.5f, 0f), 0.15f), (0, (0.3f, 0.5f, 0f), 0.05f), (anchor, (0f, 0f, 0f), 0.31f)], h);
world.Step(Dt, Sub);
var live = world.View.SensorLiquidVolume.ToArray();
Assert.True(live[h[0]] >= 2f * DropVolume && world.View.SensorWetVolume.ToArray().All(x => x == 0f), "unsettled drops read live, none pooled");
world.Settle(drops);
Assert.Equal(live[h[0]], world.View.SensorLiquidVolume[h[0]]); // end-of-step values: a Settle between steps shows at the next step
Assert.Equal(0f, world.View.SensorWetVolume[h[0]]);
world.Step(Dt, Sub);
var v = world.View;
var a = PooledInside(v, (-0.3f, 0f, 0f), 0.15f);
var b = PooledInside(v, (0.3f, 0f, 0f), 0.05f);
var c = PooledInside(v, (0.2f, 0.3f, 0f), 0.31f);
Assert.True(Math.Min(a.Margin, Math.Min(b.Margin, c.Margin)) > 1e-4, $"a patch centre within 0.1 mm of a surface: {a.Margin} {b.Margin} {c.Margin}");
Assert.Equal(2, a.Count);
Assert.Equal(0, b.Count);
Assert.Equal(1, c.Count);
Assert.Equal(2f * DropVolume, a.Volume);
Assert.Equal(a.Volume, v.SensorWetVolume[h[0]]);
Assert.Equal(0f, v.SensorWetVolume[h[1]]); // a patch outside reads 0
Assert.Equal(c.Volume, v.SensorWetVolume[h[2]]); // a patch on another body counts
for (var s = 0; s < 3; s++) Assert.Equal(0f, v.SensorLiquidVolume[h[s]]);
Readings.Add($"SENSOR pooled: A {v.SensorWetVolume[h[0]]:E4} m^3 ({a.Count} patches), B {v.SensorWetVolume[h[1]]}, C (anchor, other body) {v.SensorWetVolume[h[2]]:E4}, live 0 after the step");
// The lane is a reading, not a record: removing a patch shows at the next step too.
world.Step(Dt, Sub);
Assert.Equal(a.Volume, world.View.SensorWetVolume[h[0]]);
}
[Fact]
public void Pooled_lane_equals_a_scan_of_every_patch_on_a_dense_floor()
{
// 64 patches on a 0.15 m grid (a patch disc is 7 cm, so none merge), every one the same volume, so the sum is the same in any order; sensors cut the grid at assorted places.
var at = new List<Pos>();
for (var i = 0; i < 8; i++) for (var j = 0; j < 8; j++) at.Add((-0.525f + 0.15f * i, 0f, -0.525f + 0.15f * j));
var (world, drops) = DropsOnFloor(at.ToArray(), 64, 8);
using var _ = world;
var spec = new (Pos Point, float Radius)[]
{
((0.075f, 0.5f, 0.075f), 0.12f), ((0.0f, 0.5f, 0.0f), 0.08f), ((-0.3f, 0.5f, 0.2f), 0.33f), ((0.45f, 0.5f, -0.45f), 0.2f),
((0.0f, 0.5f, 0.0f), 1.5f), ((0.0f, 0.9f, 0.0f), 0.3f), ((3f, 0.5f, 0f), 0.5f), ((-0.12f, 0.5f, 0.3f), 0.17f),
};
var h = new Box3DSoaConstraintHandle[spec.Length];
world.CreateSensors(spec.Select(x => (0, x.Point, x.Radius)).ToArray(), h);
world.Settle(drops);
world.Step(Dt, Sub);
var v = world.View;
Assert.Equal(64, v.WetLive.ToArray().Count(l => l != 0));
var seen = new List<int>();
for (var s = 0; s < spec.Length; s++)
{
var want = PooledInside(v, (spec[s].Point.X, spec[s].Point.Y - 0.5f, spec[s].Point.Z), spec[s].Radius);
Assert.True(want.Margin > 1e-4, $"sensor {s}: a patch centre within 0.1 mm of the surface ({want.Margin})");
Assert.Equal(want.Volume, v.SensorWetVolume[h[s]]);
seen.Add(want.Count);
}
Assert.Contains(seen, n => n > 0 && n < 64);
Assert.Contains(seen, n => n == 64);
Assert.Contains(seen, n => n == 0);
Readings.Add($"SENSOR pooled dense: patches inside per sensor {string.Join(",", seen)} of 64, each equal to a scan of every lane");
}
[Fact]
public void Settled_liquid_on_a_solid_particle_reads_pooled()
{
using var world = FluidWorld(64, 8, gravityY: -9.81f, wets: 4, sensors: 2);
world.AddStaticBox(1, (0f, -50f, 0f), (1f, 1f, 1f));
var plate = Graph.Block(world, (-0.05f, 0.2f, -0.05f), 5, 1, 5, R, 0f, Box3DSoaPhase.Of(4));
var drop = Graph.Block(world, (0.0f, 0.2f + 2f * R + 0.001f, 0.0f), 1, 1, 1, R, FluidMass, FluidPhase);
var h = new Box3DSoaConstraintHandle[2];
world.CreateSensors([(0, (0f, 50.2f, 0f), 0.3f), (0, (0f, 52f, 0f), 0.3f)], h);
Run(world, 20);
Assert.True(world.View.SupportParticle[drop[0].Lane] >= 0, "the drop rests on the plate");
world.Settle(drop);
world.Step(Dt, Sub);
var v = world.View;
var want = PooledInside(v, (0f, 0.2f, 0f), 0.3f);
Assert.Equal(1, want.Count);
Assert.Equal(DropVolume, v.SensorWetVolume[h[0]]);
Assert.Equal(0f, v.SensorWetVolume[h[1]]);
Assert.Equal(0f, v.SensorLiquidVolume[h[0]]);
}
}