Ymir kill_wet keeps the wet index valid: take_wet walks every sensor on one index, rederives once; taken is the summed removal
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
50d2252f21
commit
48aded2e69
6 changed files with 111 additions and 34 deletions
|
|
@ -263,8 +263,8 @@ separate native object (`ymir_box3d_soa.c`, ABI 21) so the Aetheria session's co
|
|||
`ArgumentException` for a handle that is not a sensor, unequal lengths, or a non-empty `taken` shorter than the sensors). Per sensor in call order: W is the pooled sum above, `f = W > 0 ? min(1, v / W) : 0`, and a second walk of
|
||||
the same sphere test (`pooled_walk`, shared with the reading, so the take removes from exactly the patches the reading summed, patches on solid particles included) scales every visited patch's volume by `1 - f`
|
||||
and its radius by `wet_disc_radius`; a patch at f = 1 is removed as `remove(WET)`. Proportional by design: the film on a face and the pool beneath it shrink together and no lane order decides whose liquid is taken. A later
|
||||
sensor in the call sees an earlier one's take (the index is rederived between sensors once a take has run, because a removed lane's centre is reset); `taken[k]` (m^3, W f) is what left; `rederive_wet` runs once more at
|
||||
the end, and not at all when nothing changed. The proxies are refreshed first, as `settle` does. `sensor_wet_volume` is written by the step's end alone: the reading changes at the next step. Ymir names no organ (a lung, an airway):
|
||||
sensor in the call sees an earlier one's take (one index serves the whole call: a killed lane keeps its centre until the call's one `rederive_wet`, which returns it to rest, and a walk skips a lane that is not live); `taken[k]` (m^3) is the summed volume that left the patches, in double and
|
||||
rounded once, so float storage can leave it below the ask or 0; `rederive_wet` runs once at the end, and not at all when nothing changed. The proxies are refreshed first, as `settle` does. `sensor_wet_volume` is written by the step's end alone: the reading changes at the next step. Ymir names no organ (a lung, an airway):
|
||||
the consumer decides when liquid is breathed. Live drops are not taken (the caller destroys whole drops).
|
||||
- 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
|
||||
|
|
|
|||
|
|
@ -375,13 +375,18 @@ static void wet_reset(ymir_particles* p, int32_t w)
|
|||
|
||||
static void kill_wet(ymir_particles* p, int32_t w, uint64_t step_count)
|
||||
{
|
||||
// The lane keeps its centre until rederive_wet, so the index's order holds for the rest of the command.
|
||||
float x = p->wet_x[w], y = p->wet_y[w], z = p->wet_z[w];
|
||||
wet_reset(p, w);
|
||||
p->wet_x[w] = x;
|
||||
p->wet_y[w] = y;
|
||||
p->wet_z[w] = z;
|
||||
p->wet_removed_at[w] = step_count;
|
||||
p->wet_generation[w] += 1;
|
||||
}
|
||||
|
||||
// wet_volume_body[b] = the sum of the live patches on body b, in wet lane order; particle_wet_mu[i] = the wet friction of the lowest live
|
||||
// patch on solid particle i, +inf when dry. The one writer; settle, remove(WET) and destroy call it.
|
||||
// patch on solid particle i, +inf when dry. The one writer; settle, take_wet, remove(WET) and destroy call it. It returns killed lanes (live 0, centre kept) to rest.
|
||||
static void rederive_wet(ymir_particles* p)
|
||||
{
|
||||
for (int32_t i = 0; i < p->particle_capacity; ++i)
|
||||
|
|
@ -394,11 +399,16 @@ static void rederive_wet(ymir_particles* p)
|
|||
}
|
||||
for (int32_t w = 0; w < p->wet_count; ++w)
|
||||
{
|
||||
if (p->wet_live[w] && p->wet_body[w] >= 0 && p->wet_body[w] < p->body_capacity)
|
||||
if (!p->wet_live[w])
|
||||
{
|
||||
wet_reset(p, w);
|
||||
continue;
|
||||
}
|
||||
if (p->wet_body[w] >= 0 && p->wet_body[w] < p->body_capacity)
|
||||
{
|
||||
p->wet_volume_body[p->wet_body[w]] += p->wet_volume[w];
|
||||
}
|
||||
if (p->wet_live[w] && p->wet_particle[w] >= 0 && p->wet_particle[w] < p->particle_capacity &&
|
||||
if (p->wet_particle[w] >= 0 && p->wet_particle[w] < p->particle_capacity &&
|
||||
p->particle_wet_mu[p->wet_particle[w]] == INFINITY)
|
||||
{
|
||||
p->particle_wet_mu[p->wet_particle[w]] = p->mat_wet_friction[p->wet_material[w]];
|
||||
|
|
@ -2604,16 +2614,18 @@ void ymir_particles_substep(ymir_particles* p, float h, float gx, float gy, floa
|
|||
static float wet_disc_radius(float volume);
|
||||
|
||||
// A patch keeps the fraction keep of its volume: its radius follows, and it dies at a kept volume of 0.
|
||||
static void take_from_wet(ymir_particles* p, int32_t w, float keep, uint64_t step_count)
|
||||
static double take_from_wet(ymir_particles* p, int32_t w, double keep, uint64_t step_count)
|
||||
{
|
||||
float kept = p->wet_volume[w] * keep;
|
||||
float was = p->wet_volume[w];
|
||||
float kept = (float)((double)was * keep);
|
||||
if (!(kept > 0.0f))
|
||||
{
|
||||
kill_wet(p, w, step_count);
|
||||
return;
|
||||
return was;
|
||||
}
|
||||
p->wet_volume[w] = kept;
|
||||
p->wet_radius[w] = wet_disc_radius(kept);
|
||||
return (double)was - (double)kept;
|
||||
}
|
||||
|
||||
// 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
|
||||
|
|
@ -2621,8 +2633,8 @@ static void take_from_wet(ymir_particles* p, int32_t w, float keep, uint64_t ste
|
|||
// (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.
|
||||
// The one sphere test of the wet patches: the sensor's reading walks it with keep 1 (read only); take_wet walks it again with keep < 1, which also scales
|
||||
// every patch it visits by keep (recomputing its radius) and kills one whose kept volume is 0. Returns the volume inside before any change.
|
||||
// A kill resets the lane, so the index is stale after a walk that killed: the caller rederives before the next walk.
|
||||
static float pooled_walk(ymir_particles* p, v3 c, float r, float keep, uint64_t step_count)
|
||||
// A killed lane keeps its centre until the command's rederive_wet, so the index's order holds across kills; the walk skips a lane that is not live.
|
||||
static float pooled_walk(ymir_particles* p, v3 c, float r, double keep, uint64_t step_count, double* removed)
|
||||
{
|
||||
float total = 0.0f;
|
||||
float r2 = r * r;
|
||||
|
|
@ -2656,13 +2668,17 @@ static float pooled_walk(ymir_particles* p, v3 c, float r, float keep, uint64_t
|
|||
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];
|
||||
if (!p->wet_live[w])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
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];
|
||||
if (keep < 1.0f)
|
||||
if (keep < 1.0)
|
||||
{
|
||||
take_from_wet(p, w, keep, step_count);
|
||||
*removed += take_from_wet(p, w, keep, step_count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2678,9 +2694,9 @@ static float pooled_walk(ymir_particles* p, v3 c, float r, float keep, uint64_t
|
|||
if (vdot(d, d) <= r2)
|
||||
{
|
||||
total += p->wet_volume[w];
|
||||
if (keep < 1.0f)
|
||||
if (keep < 1.0)
|
||||
{
|
||||
take_from_wet(p, w, keep, step_count);
|
||||
*removed += take_from_wet(p, w, keep, step_count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2711,7 +2727,7 @@ static void read_sensors(ymir_particles* p)
|
|||
{
|
||||
v3 c = sensor_centre(p, s);
|
||||
float r2 = p->sensor_radius[s] * p->sensor_radius[s];
|
||||
pooled = pooled_walk(p, c, p->sensor_radius[s], 1.0f, 0);
|
||||
pooled = pooled_walk(p, c, p->sensor_radius[s], 1.0, 0, NULL);
|
||||
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))
|
||||
|
|
@ -3024,7 +3040,7 @@ int32_t ymir_particles_take_wet(ymir_particles* p, int32_t n, const ymir_handle*
|
|||
}
|
||||
}
|
||||
}
|
||||
int stale = 0; // a take may have killed a lane the index still lists (its centre reset): the next sensor rederives before it walks
|
||||
int changed = 0;
|
||||
for (int32_t k = 0; k < n; ++k)
|
||||
{
|
||||
int32_t s = sensors[k].lane;
|
||||
|
|
@ -3036,28 +3052,23 @@ int32_t ymir_particles_take_wet(ymir_particles* p, int32_t n, const ymir_handle*
|
|||
{
|
||||
continue;
|
||||
}
|
||||
if (stale)
|
||||
{
|
||||
rederive_wet(p);
|
||||
stale = 0;
|
||||
}
|
||||
v3 c = sensor_centre(p, s);
|
||||
float r = p->sensor_radius[s];
|
||||
float w = pooled_walk(p, c, r, 1.0f, step_count);
|
||||
float f = w > 0.0f ? fminf(1.0f, volumes[k] / w) : 0.0f;
|
||||
float keep = 1.0f - f;
|
||||
if (!(keep < 1.0f)) // nothing to take, or a fraction below float's reach: the walk would change nothing
|
||||
float w = pooled_walk(p, c, r, 1.0, step_count, NULL);
|
||||
double f = w > 0.0f ? fmin(1.0, (double)volumes[k] / (double)w) : 0.0;
|
||||
if (!(f > 0.0))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
pooled_walk(p, c, r, keep, step_count);
|
||||
stale = 1;
|
||||
double removed = 0.0;
|
||||
pooled_walk(p, c, r, 1.0 - f, step_count, &removed);
|
||||
changed = 1;
|
||||
if (taken != NULL)
|
||||
{
|
||||
taken[k] = w * f;
|
||||
taken[k] = (float)removed;
|
||||
}
|
||||
}
|
||||
if (stale)
|
||||
if (changed)
|
||||
{
|
||||
rederive_wet(p);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -308,7 +308,7 @@ int32_t ymir_particles_settle(ymir_particles* p, int32_t n, const ymir_handle* h
|
|||
// sensor, a sensor named twice, and a volume that is not finite or is < 0. Then per sensor in call order: W is the pooled volume of the patches whose centres lie in the
|
||||
// sensor's sphere (the walk the reading sums, at the proxies' pose now), f = W > 0 ? min(1, volume / W) : 0, and every patch in the sphere keeps (1 - f) of its volume
|
||||
// (radius recomputed; a patch at f == 1 is removed as remove(WET)): proportional, so no lane order decides which patch gives. A later sensor sees an earlier one's take.
|
||||
// `taken` (NULL or n floats) receives per sensor the volume that left (m^3), W * f. Readings change at the next step's end; none is written here. One rederive_wet when anything changed.
|
||||
// `taken` (NULL or n floats) receives per sensor the summed volume that left the patches (m^3): float rounding can leave it below the ask, or 0. Readings change at the next step's end; none is written here. One rederive_wet at the end when anything changed.
|
||||
int32_t ymir_particles_take_wet(ymir_particles* p, int32_t n, const ymir_handle* sensors, const float* volumes, uint64_t step_count, float* taken);
|
||||
// Creates liquid sensors in the lowest free lanes. Refuses whole (INVALID_ARGUMENT) a body outside [0, body_count), a radius that is not finite and > 0, or a
|
||||
// non-finite point; FULL when there are not n free lanes; nothing changes. A sensor only reads: its volume moves nothing.
|
||||
|
|
|
|||
|
|
@ -1411,7 +1411,7 @@ public sealed class Box3DSoaWorld : IDisposable
|
|||
{
|
||||
ObjectDisposedException.ThrowIf(_handle == 0, this);
|
||||
if (!patches.IsEmpty && patches.Length < particles.Length) throw new ArgumentException("One handle per particle.", nameof(patches));
|
||||
var native = new Box3DSoaParticleHandle[particles.Length];
|
||||
Span<Box3DSoaParticleHandle> native = particles.Length <= 16 ? stackalloc Box3DSoaParticleHandle[particles.Length] : new Box3DSoaParticleHandle[particles.Length];
|
||||
fixed (Box3DSoaParticleHandle* h = particles)
|
||||
fixed (Box3DSoaParticleHandle* o = native)
|
||||
Check(Box3DSoaNative.ymir_box3d_soa_settle(_handle, particles.Length, h, patches.IsEmpty ? null : o));
|
||||
|
|
@ -1422,7 +1422,7 @@ public sealed class Box3DSoaWorld : IDisposable
|
|||
/// <summary>
|
||||
/// Takes up to <paramref name="volumes"/>[k] m^3 of pooled liquid from the wet patches sensor k reads (the patches whose centres lie in its sphere, at the bodies' poses now), every patch the
|
||||
/// same fraction, so no lane order decides which gives; a patch taken to nothing is removed. Sensors apply in call order, a later one seeing an earlier one's take.
|
||||
/// <paramref name="taken"/> (or empty) receives the volume that left per sensor. Refuses the whole batch, changing nothing, for a stale or removed sensor, a sensor named twice,
|
||||
/// <paramref name="taken"/> (or empty) receives the summed volume that left the patches per sensor (float rounding can leave it below the ask, or 0). Refuses the whole batch, changing nothing, for a stale or removed sensor, a sensor named twice,
|
||||
/// or a volume that is not finite or is negative; throws <see cref="ArgumentException"/> for a handle that is not a sensor, unequal lengths, or a non-empty <paramref name="taken"/> shorter than the sensors.
|
||||
/// Readings change at the next step's end. Which liquid is breathed, and when, is the caller's policy.
|
||||
/// </summary>
|
||||
|
|
@ -1431,7 +1431,7 @@ public sealed class Box3DSoaWorld : IDisposable
|
|||
ObjectDisposedException.ThrowIf(_handle == 0, this);
|
||||
if (sensors.Length != volumes.Length) throw new ArgumentException("One volume per sensor.", nameof(volumes));
|
||||
if (!taken.IsEmpty && taken.Length < sensors.Length) throw new ArgumentException("One taken per sensor.", nameof(taken));
|
||||
var native = new Box3DSoaParticleHandle[sensors.Length];
|
||||
Span<Box3DSoaParticleHandle> native = sensors.Length <= 16 ? stackalloc Box3DSoaParticleHandle[sensors.Length] : new Box3DSoaParticleHandle[sensors.Length];
|
||||
for (var i = 0; i < sensors.Length; i++)
|
||||
{
|
||||
if (sensors[i].Type != Box3DSoaConstraintType.Sensor) throw new ArgumentException("Sensors only.", nameof(sensors));
|
||||
|
|
|
|||
|
|
@ -197,6 +197,72 @@ public sealed partial class Box3DSoaFluidTests
|
|||
Assert.Equal(1, Enumerable.Range(0, world.View.WetCount).Count(w => world.View.WetLive[w] != 0)); // only C is left
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1f / 3f)]
|
||||
[InlineData(1e-6f)]
|
||||
[InlineData(1e-9f)]
|
||||
public void TakeWet_taken_is_what_left_the_patches(float share)
|
||||
{
|
||||
// A sensor over A and B: taken is the patches' own loss (float rounding included), not the ask.
|
||||
var (world, s) = TakeFloor((-0.2f, 0.15f));
|
||||
using var _ = world;
|
||||
var pooled = world.View.SensorWetVolume[s[0]];
|
||||
var before = world.View.WetPatchVolume.ToArray();
|
||||
var taken = new float[1];
|
||||
world.TakeWet(s, [pooled * share], taken);
|
||||
var after = world.View.WetPatchVolume;
|
||||
double lost = 0;
|
||||
for (var w = 0; w < before.Length; w++) lost += (double)before[w] - after[w];
|
||||
Assert.Equal((float)lost, taken[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TakeWet_a_kill_takes_the_whole_pool()
|
||||
{
|
||||
var (world, s) = TakeFloor((-0.2f, 0.15f));
|
||||
using var _ = world;
|
||||
var pooled = world.View.SensorWetVolume[s[0]];
|
||||
var taken = new float[1];
|
||||
world.TakeWet(s, [pooled * 2f], taken);
|
||||
Assert.Equal(pooled, taken[0], pooled * 1e-6f);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TakeWet_a_patch_an_earlier_sensor_killed_is_not_killed_again()
|
||||
{
|
||||
// the second sphere also holds A, which the first took to nothing: A's dead lane keeps its centre, and the walk must pass over it
|
||||
var (world, s) = TakeFloor((-0.3f, 0.05f), (-0.2f, 0.15f));
|
||||
using var _ = world;
|
||||
var gen = world.View.WetGeneration.ToArray();
|
||||
var live = world.View.WetLive.ToArray();
|
||||
var taken = new float[2];
|
||||
world.TakeWet(s, [10f, 10f], taken);
|
||||
Assert.True(taken[0] > 0f);
|
||||
Assert.True(taken[1] > 0f);
|
||||
var killed = 0;
|
||||
for (var w = 0; w < gen.Length; w++)
|
||||
{
|
||||
if (live[w] != 0 && world.View.WetLive[w] == 0)
|
||||
{
|
||||
killed++;
|
||||
Assert.Equal(gen[w] + 1, world.View.WetGeneration[w]);
|
||||
}
|
||||
else Assert.Equal(gen[w], world.View.WetGeneration[w]);
|
||||
}
|
||||
Assert.True(killed > 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TakeWet_rederives_the_wet_index_once()
|
||||
{
|
||||
// A command ends in one rederive_wet; a kill never repairs the index mid-call (pinned on the source, as Remove_wet_never_reaches_the_topology_derivation is).
|
||||
var source = NativeSource("ymir_particles.c").Replace("\r\n", "\n");
|
||||
var at = source.IndexOf("int32_t ymir_particles_take_wet(", StringComparison.Ordinal);
|
||||
Assert.True(at > 0);
|
||||
var body = source.Substring(at, source.IndexOf("ymir_particles_create_sensors(", at, StringComparison.Ordinal) - at);
|
||||
Assert.Equal(1, System.Text.RegularExpressions.Regex.Matches(body, @"rederive_wet\(").Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TakeWet_refusals_change_nothing()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -453,7 +453,7 @@ public sealed partial class Box3DSoaFluidTests
|
|||
Assert.Contains("rederive_wet(p);", branch);
|
||||
Assert.DoesNotContain("rederive_topology", branch);
|
||||
Assert.DoesNotContain("TOPO_", branch);
|
||||
Assert.Equal(6, System.Text.RegularExpressions.Regex.Matches(source, @"[^_]rederive_wet\(p\);").Count); // settle, take_wet (twice: before a later sensor walks over a lane a take killed, and at the end), remove(WET), release_body (a removed body takes its patches), and destroy when a destroyed solid takes its patches (particle_wet_mu goes dry again)
|
||||
Assert.Equal(5, System.Text.RegularExpressions.Regex.Matches(source, @"[^_]rederive_wet\(p\);").Count); // settle, take_wet (once, at the end), remove(WET), release_body (a removed body takes its patches), and destroy when a destroyed solid takes its patches (particle_wet_mu goes dry again)
|
||||
}
|
||||
|
||||
// ---- determinism ----------------------------------------------------------------------------------------
|
||||
|
|
|
|||
Loading…
Reference in a new issue