Particles: isolated-lane derivation for create and fluid destroy; FLUID phase bit refused by distances and attachments
rederive_topology takes a mode: ISOLATED writes the component lane of the lanes a command made or unmade and returns, FULL is the derivation. create_particles always isolates (a new particle holds no constraint); destroy_particles isolates unless a named particle held a live distance or attachment. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
cdf79b197d
commit
db3fd95eba
5 changed files with 168 additions and 17 deletions
|
|
@ -446,8 +446,25 @@ static int has_pair(const ymir_particles* p, int32_t a, const int32_t* label)
|
|||
// The only writer of the tether lanes and the component lane. Called at the end of every topology command. The
|
||||
// tether count is known from the component sizes before anything is written: a derivation that would not fit is
|
||||
// refused with every lane as it was.
|
||||
static int32_t rederive_topology(ymir_particles* p)
|
||||
// ISOLATED is for a command that changed no distance and no attachment, only which lanes are live: the graph, so every
|
||||
// tether and every other lane's component, is as it was. It writes the component of the `n` lanes in `lanes` (each
|
||||
// just created, which is its own component, or just destroyed, which reads 0) and returns.
|
||||
enum
|
||||
{
|
||||
TOPO_FULL = 0,
|
||||
TOPO_ISOLATED = 1
|
||||
};
|
||||
|
||||
static int32_t rederive_topology(ymir_particles* p, int mode, const int32_t* lanes, int32_t n)
|
||||
{
|
||||
if (mode == TOPO_ISOLATED)
|
||||
{
|
||||
for (int32_t k = 0; k < n; ++k)
|
||||
{
|
||||
p->component[lanes[k]] = p->particle_live[lanes[k]] ? lanes[k] : 0;
|
||||
}
|
||||
return YMIR_PARTICLES_OK;
|
||||
}
|
||||
int32_t np = p->particle_count;
|
||||
memset(p->adj_start, 0, sizeof(int32_t) * ((size_t)np + 1));
|
||||
for (int32_t e = 0; e < p->distance_count; ++e)
|
||||
|
|
@ -722,12 +739,14 @@ int32_t ymir_particles_create_particles(ymir_particles* p, int32_t n, const ymir
|
|||
p->material[i] = d[k].material;
|
||||
p->particle_live[i] = 1;
|
||||
p->live_particles++;
|
||||
p->live_fluid += (d[k].phase & YMIR_PHASE_FLUID) != 0;
|
||||
p->queue[k] = i;
|
||||
if (out != NULL)
|
||||
{
|
||||
out[k] = (ymir_handle){i, p->particle_generation[i]};
|
||||
}
|
||||
}
|
||||
return rederive_topology(p);
|
||||
return rederive_topology(p, TOPO_ISOLATED, p->queue, n); // a new particle holds no constraint
|
||||
}
|
||||
|
||||
int32_t ymir_particles_destroy_particles(ymir_particles* p, int32_t n, const ymir_handle* h, uint64_t step_count)
|
||||
|
|
@ -743,6 +762,20 @@ int32_t ymir_particles_destroy_particles(ymir_particles* p, int32_t n, const ymi
|
|||
return YMIR_PARTICLES_INVALID_ARGUMENT;
|
||||
}
|
||||
}
|
||||
int held = 0; // whether a named particle holds a live distance or attachment: then the graph changes
|
||||
for (int32_t k = 0; k < n && !held; ++k)
|
||||
{
|
||||
int32_t i = h[k].lane;
|
||||
for (int32_t e = 0; e < p->distance_count && !held; ++e)
|
||||
{
|
||||
held = p->dist_live[e] && (p->dist_a[e] == i || p->dist_b[e] == i);
|
||||
}
|
||||
for (int32_t a = 0; a < p->attachment_count && !held; ++a)
|
||||
{
|
||||
held = p->att_kind[a] != YMIR_ATTACH_REMOVED && p->att_particle[a] == i;
|
||||
}
|
||||
}
|
||||
int32_t killed = 0;
|
||||
for (int32_t k = 0; k < n; ++k)
|
||||
{
|
||||
int32_t i = h[k].lane;
|
||||
|
|
@ -750,6 +783,8 @@ int32_t ymir_particles_destroy_particles(ymir_particles* p, int32_t n, const ymi
|
|||
{
|
||||
continue; // named twice
|
||||
}
|
||||
p->queue[killed++] = i;
|
||||
p->live_fluid -= (p->phase[i] & YMIR_PHASE_FLUID) != 0;
|
||||
for (int32_t e = 0; e < p->distance_count; ++e)
|
||||
{
|
||||
if (p->dist_live[e] && (p->dist_a[e] == i || p->dist_b[e] == i))
|
||||
|
|
@ -774,7 +809,7 @@ int32_t ymir_particles_destroy_particles(ymir_particles* p, int32_t n, const ymi
|
|||
p->particle_removed_at[i] = step_count;
|
||||
p->live_particles--;
|
||||
}
|
||||
return rederive_topology(p);
|
||||
return rederive_topology(p, held ? TOPO_FULL : TOPO_ISOLATED, p->queue, killed);
|
||||
}
|
||||
|
||||
int32_t ymir_particles_create_distances(ymir_particles* p, int32_t n, const ymir_distance_desc* d, uint64_t step_count, ymir_handle* out)
|
||||
|
|
@ -786,7 +821,8 @@ int32_t ymir_particles_create_distances(ymir_particles* p, int32_t n, const ymir
|
|||
for (int32_t k = 0; k < n; ++k)
|
||||
{
|
||||
if (!particle_ok(p, d[k].a.lane, d[k].a.generation) || !particle_ok(p, d[k].b.lane, d[k].b.generation) ||
|
||||
d[k].a.lane == d[k].b.lane || !isfinite(d[k].rest) || !isfinite(d[k].compliance) || d[k].compliance < 0.0f)
|
||||
d[k].a.lane == d[k].b.lane || !isfinite(d[k].rest) || !isfinite(d[k].compliance) || d[k].compliance < 0.0f ||
|
||||
((p->phase[d[k].a.lane] | p->phase[d[k].b.lane]) & YMIR_PHASE_FLUID) != 0) // fluid holds no constraint
|
||||
{
|
||||
return YMIR_PARTICLES_INVALID_ARGUMENT;
|
||||
}
|
||||
|
|
@ -812,7 +848,7 @@ int32_t ymir_particles_create_distances(ymir_particles* p, int32_t n, const ymir
|
|||
p->dist_tension[e] = 0.0f;
|
||||
p->dist_live[e] = 1;
|
||||
}
|
||||
int32_t status = rederive_topology(p);
|
||||
int32_t status = rederive_topology(p, TOPO_FULL, NULL, 0);
|
||||
if (status != YMIR_PARTICLES_OK)
|
||||
{
|
||||
for (int32_t k = 0; k < n; ++k) // back out: the lanes read as they did before the command
|
||||
|
|
@ -891,14 +927,15 @@ int32_t ymir_particles_create_bends(ymir_particles* p, int32_t n, const ymir_ben
|
|||
out[k] = (ymir_handle){i, p->bend_generation[i]};
|
||||
}
|
||||
}
|
||||
return rederive_topology(p);
|
||||
return rederive_topology(p, TOPO_FULL, NULL, 0);
|
||||
}
|
||||
|
||||
int32_t ymir_particles_attach(ymir_particles* p, const ymir_attachment_desc* d, int32_t body_count, uint64_t step_count, ymir_handle* out)
|
||||
{
|
||||
if (p == NULL || d == NULL || (d->kind != YMIR_ATTACH_BODY && d->kind != YMIR_ATTACH_STATIC) ||
|
||||
(d->flags & ~(uint32_t)YMIR_ATTACH_FLAG_TETHER) != 0 || !particle_ok(p, d->particle.lane, d->particle.generation) ||
|
||||
!finite3(d->point) || !isfinite(d->compliance) || d->compliance < 0.0f)
|
||||
!finite3(d->point) || !isfinite(d->compliance) || d->compliance < 0.0f ||
|
||||
(p->phase[d->particle.lane] & YMIR_PHASE_FLUID) != 0) // fluid holds no constraint
|
||||
{
|
||||
return YMIR_PARTICLES_INVALID_ARGUMENT;
|
||||
}
|
||||
|
|
@ -922,7 +959,7 @@ int32_t ymir_particles_attach(ymir_particles* p, const ymir_attachment_desc* d,
|
|||
p->att_point_z[a] = d->point[2];
|
||||
p->att_compliance[a] = d->compliance;
|
||||
p->att_force_x[a] = p->att_force_y[a] = p->att_force_z[a] = 0.0f;
|
||||
int32_t status = rederive_topology(p);
|
||||
int32_t status = rederive_topology(p, TOPO_FULL, NULL, 0);
|
||||
if (status != YMIR_PARTICLES_OK)
|
||||
{
|
||||
p->att_kind[a] = a < old_count ? YMIR_ATTACH_REMOVED : 0;
|
||||
|
|
@ -974,7 +1011,7 @@ int32_t ymir_particles_remove(ymir_particles* p, uint32_t type, int32_t n, const
|
|||
kill_attachment(p, lane, step_count);
|
||||
}
|
||||
}
|
||||
return rederive_topology(p);
|
||||
return rederive_topology(p, TOPO_FULL, NULL, 0);
|
||||
}
|
||||
|
||||
int32_t ymir_particles_set_material(ymir_particles* p, int32_t index, const ymir_material_desc* d)
|
||||
|
|
|
|||
|
|
@ -32,13 +32,15 @@ enum
|
|||
YMIR_PARTICLES_OUT_OF_MEMORY = 4
|
||||
};
|
||||
|
||||
// Phase word: bits 0-19 are the group; the two flags below are the only other bits.
|
||||
// Phase word: bits 0-19 are the group; the three flags below are the only other bits.
|
||||
enum
|
||||
{
|
||||
YMIR_PHASE_GROUP_MASK = 0xFFFFFu,
|
||||
YMIR_PHASE_SELF_COLLIDE = 1u << 20, // particles of one group collide (both must set it)
|
||||
YMIR_PHASE_SELF_FILTER = 1u << 21, // ... except pairs closer than 1.5 (ri + rj) in the rest pose (either sets it)
|
||||
YMIR_PHASE_VALID = YMIR_PHASE_GROUP_MASK | YMIR_PHASE_SELF_COLLIDE | YMIR_PHASE_SELF_FILTER
|
||||
YMIR_PHASE_FLUID = 1u << 22, // a fluid particle: number density, not contact, with other fluid; contact with solids. Group and
|
||||
// the SELF_* flags are ignored between two fluid particles (all fluid shares one density field)
|
||||
YMIR_PHASE_VALID = YMIR_PHASE_GROUP_MASK | YMIR_PHASE_SELF_COLLIDE | YMIR_PHASE_SELF_FILTER | YMIR_PHASE_FLUID
|
||||
};
|
||||
|
||||
// Lane sets a command names (a type for remove, a set for free_slots).
|
||||
|
|
@ -158,6 +160,7 @@ typedef struct ymir_particles
|
|||
int32_t tether_count; // derived: the live tethers
|
||||
int32_t body_count; // bodies filled in `proxies` this step
|
||||
int32_t live_particles;
|
||||
int32_t live_fluid; // derived: the live particles with the FLUID phase bit; the create and destroy commands are its only writers
|
||||
ymir_particle_proxy* proxies;
|
||||
|
||||
// particle lanes; a free lane reads live 0 and every other lane 0 except generation and removed_at
|
||||
|
|
|
|||
|
|
@ -100,9 +100,11 @@ public static class Box3DSoaPhase
|
|||
public const uint SelfCollide = 1u << 20;
|
||||
/// <summary>...except pairs closer than 1.5 (ri + rj) in the rest pose (either may carry it).</summary>
|
||||
public const uint SelfFilter = 1u << 21;
|
||||
/// <summary>A fluid particle: number density, not contact, with other fluid; contact with solids. Fluid holds no distance, bend or attachment.</summary>
|
||||
public const uint Fluid = 1u << 22;
|
||||
|
||||
public static uint Of(uint group, bool selfCollide = false, bool selfFilter = false) =>
|
||||
(group & GroupMask) | (selfCollide ? SelfCollide : 0u) | (selfFilter ? SelfFilter : 0u);
|
||||
public static uint Of(uint group, bool selfCollide = false, bool selfFilter = false, bool fluid = false) =>
|
||||
(group & GroupMask) | (selfCollide ? SelfCollide : 0u) | (selfFilter ? SelfFilter : 0u) | (fluid ? Fluid : 0u);
|
||||
}
|
||||
|
||||
/// <summary>Capacities of the particle state; <c>null</c> or all zero is a world without particles (ABI 8 behaviour).</summary>
|
||||
|
|
|
|||
105
tests/Ymir.Box3D.Soa.Tests/Box3DSoaFluidTests.cs
Normal file
105
tests/Ymir.Box3D.Soa.Tests/Box3DSoaFluidTests.cs
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
using Ymir.Box3D;
|
||||
|
||||
namespace Ymir.Box3D.Soa.Tests;
|
||||
|
||||
using Pos = (float X, float Y, float Z);
|
||||
|
||||
/// <summary>Fluid as a phase bit of the one particle solver: docs/soa-world.md, particles section, Fluid.</summary>
|
||||
public sealed class Box3DSoaFluidTests
|
||||
{
|
||||
private const float Dt = Box3DSoaParticleTests.Dt;
|
||||
private const int Sub = Box3DSoaParticleTests.Sub;
|
||||
private static readonly uint FluidPhase = Box3DSoaPhase.Of(0, fluid: true);
|
||||
|
||||
private static Box3DSoaParticleDesc Fluid(Pos at, float mass = 0.02f, float radius = 0.0125f, Pos velocity = default, uint material = 0) =>
|
||||
new(at, mass, radius, FluidPhase, material, null, velocity);
|
||||
|
||||
private sealed record Graphs(int Count, int[] Att, int[] Particle, int[] Parent, int[] Edge, int[] Other, float[] Bound, int[] Component);
|
||||
|
||||
private static Graphs Snapshot(Box3DSoaView v) =>
|
||||
new(v.TetherCount, v.TetherAttachment.ToArray(), v.TetherParticle.ToArray(), v.TetherParent.ToArray(), v.TetherEdge.ToArray(),
|
||||
v.TetherOther.ToArray(), v.TetherBound.ToArray(), v.ParticleComponent.ToArray());
|
||||
|
||||
private static void SameTethers(Graphs a, Graphs b)
|
||||
{
|
||||
Assert.Equal(a.Count, b.Count);
|
||||
Assert.Equal(a.Att, b.Att);
|
||||
Assert.Equal(a.Particle, b.Particle);
|
||||
Assert.Equal(a.Parent, b.Parent);
|
||||
Assert.Equal(a.Edge, b.Edge);
|
||||
Assert.Equal(a.Other, b.Other);
|
||||
Assert.True(a.Bound.AsSpan().SequenceEqual(b.Bound), "tether bounds differ");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fluid_refusals()
|
||||
{
|
||||
using var world = Box3DSoaParticleTests.World(particles: 16, distances: 16, bends: 16, attachments: 4, tethers: 64);
|
||||
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;
|
||||
Assert.Equal(FluidPhase, view.ParticlePhase[1]);
|
||||
// a fluid particle holds no distance, bend or attachment; every refusal happens before anything changes
|
||||
Assert.Throws<InvalidOperationException>(() => world.CreateDistances([new Box3DSoaDistanceDesc(ps[0], ps[1])]));
|
||||
Assert.Throws<InvalidOperationException>(() => world.CreateDistances([new Box3DSoaDistanceDesc(ps[1], ps[2])]));
|
||||
Assert.Throws<InvalidOperationException>(() => world.Attach(Box3DSoaAttachKind.Static, ps[1], point: (0.05f, 0f, 0f), tether: true));
|
||||
Assert.Throws<InvalidOperationException>(() => world.Attach(Box3DSoaAttachKind.Static, ps[2], point: (0.1f, 0f, 0f)));
|
||||
Assert.Equal(0, view.DistanceCount);
|
||||
Assert.Equal(0, view.AttachmentCount);
|
||||
Assert.Equal(0, view.TetherCount);
|
||||
Assert.Equal(16, world.FreeDistanceSlots);
|
||||
// the solids still take both
|
||||
var other = new Box3DSoaParticleHandle[1];
|
||||
world.CreateParticles([new((0f, 0.05f, 0f), 0.02f, 0.0125f)], other);
|
||||
world.CreateDistances([new Box3DSoaDistanceDesc(ps[0], other[0])]);
|
||||
world.Attach(Box3DSoaAttachKind.Static, ps[0], point: (0f, 0f, 0f), tether: true);
|
||||
Assert.Equal(1, world.View.DistanceCount); // the view is a snapshot of the counts
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fluid_emission_keeps_the_graph()
|
||||
{
|
||||
// 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);
|
||||
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>();
|
||||
for (var a = 0; a < 20; a++)
|
||||
{
|
||||
var i = a == 0 ? 0 : a == 1 ? 31 : a * 37 % 1024;
|
||||
if (held.Add(i)) world.Attach(Box3DSoaAttachKind.Static, sheet.Particles[i], point: (i % 32 * sp, 2f - i / 32 * sp, 0f), tether: true);
|
||||
}
|
||||
var before = Snapshot(world.View);
|
||||
Assert.True(before.Count > 1900, $"{before.Count} tethers");
|
||||
Box3DSoaParticleHandle[]? previous = null;
|
||||
var emit = new TimeSpan();
|
||||
for (var tick = 0; tick < 60; tick++)
|
||||
{
|
||||
var watch = System.Diagnostics.Stopwatch.StartNew();
|
||||
if (previous is not null) world.DestroyParticles(previous);
|
||||
var batch = new Box3DSoaParticleDesc[64];
|
||||
for (var k = 0; k < 64; k++) batch[k] = Fluid((5f + 0.05f * (k % 8), 3f + 0.05f * (k / 8), 0.05f * (tick % 4)));
|
||||
var made = new Box3DSoaParticleHandle[64];
|
||||
world.CreateParticles(batch, made);
|
||||
emit += watch.Elapsed;
|
||||
var view = world.View;
|
||||
var now = Snapshot(view);
|
||||
SameTethers(before, now);
|
||||
for (var k = 0; k < 1024; k++) Assert.Equal(before.Component[k], now.Component[k]); // the sheet's components
|
||||
foreach (var h in made) Assert.Equal(h.Lane, view.ParticleComponent[h.Lane]);
|
||||
if (previous is not null) foreach (var h in previous) Assert.Equal(0, view.ParticleComponent[h.Lane]);
|
||||
world.Step(Dt, Sub);
|
||||
previous = made;
|
||||
}
|
||||
world.DestroyParticles(previous!);
|
||||
var last = world.View;
|
||||
SameTethers(before, Snapshot(last));
|
||||
for (var t = 0; t < last.TetherCount; t++) // every tether's parent is present
|
||||
Assert.True(last.TetherParent[t] < t);
|
||||
var line = $"FLUID_EMIT 64 fluid particles created and 64 destroyed per tick on the 20-pin 32 x 32 sheet: {emit.TotalMilliseconds / 60:F3} ms per tick of commands";
|
||||
Console.Error.WriteLine(line);
|
||||
Readings.Add(line);
|
||||
}
|
||||
}
|
||||
|
|
@ -868,7 +868,7 @@ public sealed class Box3DSoaParticleTests
|
|||
// refusals
|
||||
Assert.Throws<InvalidOperationException>(() => small.CreateParticles([new((float.NaN, 0f, 0f), 1f, 0.01f)]));
|
||||
Assert.Throws<InvalidOperationException>(() => small.CreateParticles([new((0f, 0f, 0f), 1f, 0f)])); // no radius
|
||||
Assert.Throws<InvalidOperationException>(() => small.CreateParticles([new((0f, 0f, 0f), 1f, 0.01f, 1u << 22)])); // an unknown phase bit
|
||||
Assert.Throws<InvalidOperationException>(() => small.CreateParticles([new((0f, 0f, 0f), 1f, 0.01f, 1u << 23)])); // an unknown phase bit
|
||||
Assert.Throws<InvalidOperationException>(() => small.CreateParticles([new((0f, 0f, 0f), 1f, 0.01f, 0, 2)])); // no such material
|
||||
Assert.Throws<InvalidOperationException>(() => small.CreateDistances([new Box3DSoaDistanceDesc(ps[0], ps[0])]));
|
||||
Assert.Throws<InvalidOperationException>(() => small.Attach(Box3DSoaAttachKind.Body, ps[0], 5)); // no such body
|
||||
|
|
@ -1079,14 +1079,18 @@ public sealed class Box3DSoaParticleSourceTests
|
|||
Assert.Equal(0, Count(c, @"\bomp\b|#pragma|pthread|_Atomic|atomic_"));
|
||||
// no object kind: a rope, a sheet, a cloth or a chain is what a caller builds, never a name in the solver or its binding
|
||||
var bindings = string.Join("\n", Directory.GetFiles(Path.Combine(Root(), "src", "Ymir.Box3D"), "*.cs").Select(f => StripComments(File.ReadAllText(f))));
|
||||
Assert.Equal(0, Count(code + bindings, @"(?<![A-Za-z])(rope|sheet|cloth|chain|Rope|Sheet|Cloth|Chain)(?![a-z])"));
|
||||
Assert.Equal(0, Count(code + bindings, @"(?<![A-Za-z])(rope|sheet|cloth|chain|blood|water|Rope|Sheet|Cloth|Chain|Blood|Water)(?![a-z])"));
|
||||
// tether lanes and the component lane have one writer: the derivation
|
||||
var outsideWriters = c.Replace(Body(c, "static int32_t rederive_topology("), "");
|
||||
var rest = StripComments(outsideWriters);
|
||||
Assert.Equal(0, Count(rest, @"->(component|tether_att|tether_particle|tether_parent|tether_edge|tether_bound)\[[^\]]*\]\s*=[^=]|->tether_count\s*=[^=]"));
|
||||
Assert.Contains("rederive_topology(p)", Body(c, "int32_t ymir_particles_remove("));
|
||||
// every call names a mode; only a command that changed no distance and no attachment may derive the isolated lanes
|
||||
Assert.Equal(0, Count(c, @"rederive_topology\(p\)"));
|
||||
foreach (var command in new[] { "create_particles", "destroy_particles", "create_distances", "create_bends", "attach", "remove" })
|
||||
Assert.Contains("rederive_topology(p)", Body(c, $"int32_t ymir_particles_{command}("));
|
||||
Assert.Contains("rederive_topology(p, ", Body(c, $"int32_t ymir_particles_{command}("));
|
||||
foreach (var command in new[] { "create_distances", "create_bends", "attach", "remove" })
|
||||
Assert.DoesNotContain("TOPO_ISOLATED", Body(c, $"int32_t ymir_particles_{command}("));
|
||||
Assert.Equal(0, Count(c + h, @"PxReal|physx"));
|
||||
// the retired owners stay gone
|
||||
var retired = string.Join("|", "create_" + "chain", "split_" + "chain", "destroy_" + "chain", "chain_" + "first", "chain_" + "size", "chain_stable_" + "id",
|
||||
"Chain" + "Handle", "Create" + "Chain", "Split" + "Chain", "Destroy" + "Chain", "FreeChain" + "Slots", "link_" + "tension", "Link" + "Tension");
|
||||
|
|
|
|||
Loading…
Reference in a new issue