diff --git a/native/Ymir.Box3D/ymir_box3d_soa.c b/native/Ymir.Box3D/ymir_box3d_soa.c index b14e1c5..fd9b1e5 100644 --- a/native/Ymir.Box3D/ymir_box3d_soa.c +++ b/native/Ymir.Box3D/ymir_box3d_soa.c @@ -335,6 +335,7 @@ typedef struct ymir_box3d_soa_world b3ContactData* contact_scratch; // `capacity` entries: a body has one shape, so at most capacity - 1 contacts b3Vec3 gravity; ymir_particles* particles; // NULL when created without particle capacity: the step then runs exactly as ABI 8 + float* carry; // 3 per body: the particle phase's deferred impulse, applied before the next Box3D step } ymir_box3d_soa_world; static float* ymir_soa_lane(const ymir_box3d_soa_world* world, int lane) @@ -489,6 +490,7 @@ static void ymir_soa_free(ymir_box3d_soa_world* world) free(world->hit_facts); free(world->contact_scratch); ymir_particles_free(world->particles); + free(world->carry); free(world); } @@ -546,7 +548,8 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create( if (particle_capacity > 0 || chain_capacity > 0 || attachment_capacity > 0) { world->particles = ymir_particles_create(particle_capacity, chain_capacity, attachment_capacity, capacity); - if (world->particles == NULL) + world->carry = (float*)calloc(n * 3, sizeof(float)); + if (world->particles == NULL || world->carry == NULL) { ymir_soa_free(world); return YMIR_SOA_OUT_OF_MEMORY; @@ -1110,7 +1113,8 @@ static void ymir_soa_apply_particle_impulses(ymir_box3d_soa_world* world) const ymir_particle_proxy* b = pt->proxies + i; for (int k = 0; k < 3; ++k) { - ymir_soa_lane(world, YMIR_SOA_PARTICLE_IMPULSE_LANE + k)[i] = b->impulse[k]; + ymir_soa_lane(world, YMIR_SOA_PARTICLE_IMPULSE_LANE + k)[i] = b->impulse[k] + b->carry[k]; + world->carry[3 * i + k] = b->carry[k]; } if (!ymir_soa_is_dynamic(world->kind[i])) { @@ -1140,6 +1144,19 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_step(ymir_box3d_soa_world* world, f { return status; } + if (world->carry != NULL) + { + // The impulse the last particle phase deferred: the support a tied body needs before gravity integrates. + for (int i = 0; i < world->count; ++i) + { + b3Vec3 c = {world->carry[3 * i], world->carry[3 * i + 1], world->carry[3 * i + 2]}; + if (c.x != 0.0f || c.y != 0.0f || c.z != 0.0f) + { + b3Body_ApplyLinearImpulseToCenter(world->bodies[i], c, true); + world->carry[3 * i] = world->carry[3 * i + 1] = world->carry[3 * i + 2] = 0.0f; + } + } + } b3World_Step(world->world_id, time_step, sub_step_count); // An anchor moves only on a target: the one just applied has been reached, so it holds still until the // next target. Otherwise its velocity would carry it on past the pose (and fling what it holds). diff --git a/native/Ymir.Box3D/ymir_particles.c b/native/Ymir.Box3D/ymir_particles.c index d1d1871..a347748 100644 --- a/native/Ymir.Box3D/ymir_particles.c +++ b/native/Ymir.Box3D/ymir_particles.c @@ -536,7 +536,7 @@ static float proxy_w(const ymir_particle_proxy* b, v3 at, v3 n) // Applies a position-level impulse `imp` (kg m) at `at` to a proxy: moves its pose and books the velocity-level // impulse (imp / dt) the tick will hand back to Box3D. -static void proxy_push(const phase* f, ymir_particle_proxy* b, v3 at, v3 imp) +static void proxy_push(const phase* f, ymir_particle_proxy* b, v3 at, v3 imp, int hard) { if (b->inv_mass == 0.0f) { @@ -565,9 +565,10 @@ static void proxy_push(const phase* f, ymir_particle_proxy* b, v3 at, v3 imp) { q[k] *= inv; } - b->impulse[0] += imp.x * f->inv_dt; - b->impulse[1] += imp.y * f->inv_dt; - b->impulse[2] += imp.z * f->inv_dt; + float* acc = hard ? b->hard_impulse : b->impulse; + acc[0] += imp.x * f->inv_dt; + acc[1] += imp.y * f->inv_dt; + acc[2] += imp.z * f->inv_dt; b->angular_impulse[0] += torque.x * f->inv_dt; b->angular_impulse[1] += torque.y * f->inv_dt; b->angular_impulse[2] += torque.z * f->inv_dt; @@ -607,7 +608,7 @@ static void end_push(const phase* f, int32_t a, v3 at, v3 imp) ymir_particles* p = f->p; if (p->att_kind[a] == YMIR_ATTACH_PARTICLE_BODY) { - proxy_push(f, p->proxies + p->att_body[a], at, imp); + proxy_push(f, p->proxies + p->att_body[a], at, imp, 1); } else if (p->att_kind[a] == YMIR_ATTACH_PARTICLE_PARTICLE) { @@ -863,7 +864,7 @@ static void body_contacts(phase* f) float wb = proxy_w(b, s, n); float w = wi + wb; x = vadd(x, vmul(n, depth * wi / w)); - proxy_push(f, b, s, vmul(n, -depth / w)); + proxy_push(f, b, s, vmul(n, -depth / w), 0); if (mu > 0.0f) { // Slip of the particle against the body point over this substep, tangential part. @@ -879,7 +880,7 @@ static void body_contacts(phase* f) float move = vmin(sl, mu * depth); float wt = wi + proxy_w(b, s, t); x = vsub(x, vmul(t, move * wi / wt)); - proxy_push(f, b, s, vmul(t, move / wt)); + proxy_push(f, b, s, vmul(t, move / wt), 0); } } put(p->pp_x, p->pp_y, p->pp_z, i, x); @@ -1062,6 +1063,8 @@ void ymir_particles_step(ymir_particles* p, float dt, int32_t substeps, float gx for (int32_t k = 0; k < p->body_count; ++k) { memset(p->proxies[k].impulse, 0, sizeof(p->proxies[k].impulse)); + memset(p->proxies[k].hard_impulse, 0, sizeof(p->proxies[k].hard_impulse)); + memset(p->proxies[k].carry, 0, sizeof(p->proxies[k].carry)); memset(p->proxies[k].angular_impulse, 0, sizeof(p->proxies[k].angular_impulse)); } for (int32_t i = 0; i < p->particle_count; ++i) @@ -1129,6 +1132,26 @@ void ymir_particles_step(ymir_particles* p, float dt, int32_t substeps, float gx put(p->pos_x, p->pos_y, p->pos_z, i, y); } } + // A body tied to the chain (attachment, tether) cannot be corrected by a post-step impulse alone: a velocity + // from the whole position error rings at six ticks and never settles, and holding a resting body needs the + // support before Box3D integrates gravity, not after. So the hard impulse splits along its direction: the + // velocity the body carries into the constraint is cancelled now (it reads still after the step), and the + // rest, the position error to close, returns as `carry`, applied before the next Box3D step. + for (int32_t k = 0; k < p->body_count; ++k) + { + ymir_particle_proxy* b = p->proxies + k; + v3 hard = {b->hard_impulse[0], b->hard_impulse[1], b->hard_impulse[2]}; + float hl = vlen(hard); + if (b->inv_mass == 0.0f || !(hl > 0.0f)) + { + continue; + } + v3 j = vmul(hard, 1.0f / hl); + float cancel = vmax(-vdot((v3){b->velocity[0], b->velocity[1], b->velocity[2]}, j), 0.0f) / b->inv_mass; + float restore = hl; + b->impulse[0] += j.x * cancel; b->impulse[1] += j.y * cancel; b->impulse[2] += j.z * cancel; + b->carry[0] = j.x * restore; b->carry[1] = j.y * restore; b->carry[2] = j.z * restore; + } float mean = 1.0f / (float)substeps; for (int32_t i = 0; i < p->particle_count; ++i) { diff --git a/native/Ymir.Box3D/ymir_particles.h b/native/Ymir.Box3D/ymir_particles.h index 3a86044..a4f242e 100644 --- a/native/Ymir.Box3D/ymir_particles.h +++ b/native/Ymir.Box3D/ymir_particles.h @@ -86,6 +86,8 @@ typedef struct ymir_particle_proxy float inv_mass; // 0 for static, kinematic and anchor bodies float inv_inertia[6]; // world inverse inertia, symmetric: xx xy xz yy yz zz float impulse[3]; + float hard_impulse[3]; // the part from attachments and tethers: finalised into `impulse` now and `carry` next step + float carry[3]; // N s, applied to the body before the next Box3D step float angular_impulse[3]; } ymir_particle_proxy; diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaParticleTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaParticleTests.cs index 5a545dd..eb60f24 100644 --- a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaParticleTests.cs +++ b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaParticleTests.cs @@ -56,4 +56,461 @@ public sealed class Box3DSoaParticleTests } Assert.True(Abi8Hashes.AsSpan().SequenceEqual(got), string.Join(",", got.Select(g => g + "ul"))); } + + // ---- scenes --------------------------------------------------------------------------------------------- + + private const float Spacing = 0.03f; + private const float ParticleMass = 0.02f; + private const float ParticleRadius = 0.0125f; + + private static Box3DSoaWorld World(int bodies = 8, int particles = 256, int chains = 8, int attachments = 8, int joints = 2) => + new(bodies, gravityY: -10f, jointCapacity: joints, particleCapacity: particles, chainCapacity: chains, attachmentCapacity: attachments); + + private static (float X, float Y, float Z)[] Line((float X, float Y, float Z) from, (float X, float Y, float Z) step, int count) + { + var p = new (float, float, float)[count]; + for (var i = 0; i < count; i++) p[i] = (from.X + step.X * i, from.Y + step.Y * i, from.Z + step.Z * i); + return p; + } + + private static float Dist(Box3DSoaView v, int a, int b) => + MathF.Sqrt(Sq(v.ParticleX[a] - v.ParticleX[b]) + Sq(v.ParticleY[a] - v.ParticleY[b]) + Sq(v.ParticleZ[a] - v.ParticleZ[b])); + + private static float Sq(float x) => x * x; + + /// Total link length of a chain over its rest length, minus 1. + private static float Stretch(Box3DSoaView v, Box3DSoaChainHandle chain, float restLength) + { + var first = v.ChainFirst[chain.Lane]; + var last = first + v.ChainSize[chain.Lane]; + var length = 0f; + for (var i = first; i + 1 < last; i++) length += Dist(v, i, i + 1); + return length / restLength - 1f; + } + + private static void Run(Box3DSoaWorld world, int steps) + { + for (var i = 0; i < steps; i++) world.Step(Dt, Sub); + } + + private static float SphereDensity(float mass, float radius) => mass / (4f / 3f * MathF.PI * radius * radius * radius); + + // ---- behaviour ------------------------------------------------------------------------------------------ + + [Fact] + public void Chain_hangs_near_rest_length() + { + using var world = World(); + var positions = Line((0f, 3f, 0f), (0f, -Spacing, 0f), 48); + var chain = world.CreateChain(1, positions, ParticleMass, ParticleRadius); + world.Attach(Box3DSoaAttachKind.Static, 0, point: positions[0]); + var view = world.View; + var rest = 47 * Spacing; + var worst = 0f; + for (var i = 0; i < 4 * 120; i++) + { + world.Step(Dt, Sub); + worst = MathF.Max(worst, MathF.Abs(Stretch(view, chain, rest))); + } + Assert.True(worst < 0.01f, $"stretch peaked at {worst}"); + Assert.Equal(3f - rest, view.ParticleY[47], 0.05f); // it hangs, not sags to the side or floats + Assert.Equal(0f, view.ParticleX[47], 0.05f); + } + + [Fact] + public void Chain_holds_a_70kg_body() + { + using var world = World(); + var positions = Line((0f, 5f, 0f), (0f, -Spacing, 0f), 48); + var chain = world.CreateChain(1, positions, ParticleMass, ParticleRadius); + world.Attach(Box3DSoaAttachKind.Static, 0, point: positions[0]); + const float radius = 0.15f; + var end = positions[47]; + var sphere = world.AddSphere(10, (end.X, end.Y - radius, end.Z), radius, SphereDensity(70f, radius)); + world.Attach(Box3DSoaAttachKind.Body, 47, sphere, (0f, radius, 0f)); + var view = world.View; + var rest = 47 * Spacing; + var worst = 0f; + for (var i = 0; i < 5 * 120; i++) + { + world.Step(Dt, Sub); + if (i > 120) worst = MathF.Max(worst, MathF.Abs(Stretch(view, chain, rest))); + } + var speed = MathF.Sqrt(Sq(view.VelocityX[sphere]) + Sq(view.VelocityY[sphere]) + Sq(view.VelocityZ[sphere])); + Assert.True(worst < 0.02f, $"stretch peaked at {worst}"); + Assert.True(speed < 0.01f, $"the body moves at {speed} m/s"); + Assert.Equal(end.Y - radius, view.PositionY[sphere], 0.1f); + } + + [Fact] + public void Chain_drags_a_resting_box() + { + using var world = World(); + world.AddStaticBox(1, (0f, -0.5f, 0f), (20f, 0.5f, 20f), friction: 0.6f); + const float half = 0.15f; + var count = 29; + var positions = Line((0f, ParticleRadius, 0f), (Spacing, 0f, 0f), count); + var tip = positions[count - 1]; + var box = world.AddBox(2, (tip.X + half, half, 0f), (half, half, half), 5f / (8f * half * half * half), friction: 0.6f); + var anchor = world.AddAnchor(3, positions[0]); + world.CreateChain(1, positions, ParticleMass, ParticleRadius, friction: 0.3f); + world.Attach(Box3DSoaAttachKind.Body, 0, anchor); + world.Attach(Box3DSoaAttachKind.Body, count - 1, box, (-half, ParticleRadius - half, 0f)); + var view = world.View; + var startX = view.PositionX[box]; + var peak = 0f; + for (var i = 0; i < 2 * 120; i++) + { + world.SetAnchorTarget(anchor, (-(i + 1) * 0.5f / 120f, ParticleRadius, 0f), default, Dt); + world.Step(Dt, Sub); + peak = MathF.Max(peak, MathF.Abs(view.ParticleImpulseX[box])); + } + Assert.True(startX - view.PositionX[box] > 0.5f, $"the box moved {startX - view.PositionX[box]} m"); + Assert.True(peak > 0f, "particle_impulse reads the pull"); + } + + /// How deep particle sits inside body 's surface (negative: clear of it). + private static float Penetration(Box3DSoaView v, int b, int i) + { + var px = v.ParticleX[i] - v.PositionX[b]; var py = v.ParticleY[i] - v.PositionY[b]; var pz = v.ParticleZ[i] - v.PositionZ[b]; + float qx = v.RotationX[b], qy = v.RotationY[b], qz = v.RotationZ[b], qw = v.RotationW[b]; + (float X, float Y, float Z) Rot(float x, float y, float z, float s) // rotate by q (s = 1) or its inverse (s = -1) + { + var ux = qx * s; var uy = qy * s; var uz = qz * s; + var tx = 2f * (uy * z - uz * y); var ty = 2f * (uz * x - ux * z); var tz = 2f * (ux * y - uy * x); + return (x + qw * tx + (uy * tz - uz * ty), y + qw * ty + (uz * tx - ux * tz), z + qw * tz + (ux * ty - uy * tx)); + } + var l = Rot(px, py, pz, -1f); + float d; + switch ((Box3DSoaKind)v.Kind[b]) + { + case Box3DSoaKind.Sphere: + d = MathF.Sqrt(Sq(l.X) + Sq(l.Y) + Sq(l.Z)) - v.Size0[b]; + break; + case Box3DSoaKind.Capsule: + var t = Math.Clamp(l.Y, -v.Size1[b], v.Size1[b]); + d = MathF.Sqrt(Sq(l.X) + Sq(l.Y - t) + Sq(l.Z)) - v.Size0[b]; + break; + default: + float ax = MathF.Abs(l.X) - v.Size0[b], ay = MathF.Abs(l.Y) - v.Size1[b], az = MathF.Abs(l.Z) - v.Size2[b]; + d = MathF.Sqrt(Sq(MathF.Max(ax, 0f)) + Sq(MathF.Max(ay, 0f)) + Sq(MathF.Max(az, 0f))) + MathF.Min(MathF.Max(ax, MathF.Max(ay, az)), 0f); + break; + } + return v.ParticleRadius[i] - d; + } + + [Theory] + [InlineData(Box3DSoaKind.Sphere)] + [InlineData(Box3DSoaKind.Capsule)] + [InlineData(Box3DSoaKind.Box)] + [InlineData(Box3DSoaKind.StaticBox)] + public void Bodies_push_particles_per_kind(Box3DSoaKind kind) + { + using var world = World(); + world.AddStaticBox(1, (0f, -0.5f, 0f), (20f, 0.5f, 20f), friction: 0.6f); + var positions = Line((0f, ParticleRadius, 0f), (Spacing, 0f, 0f), 12); + world.CreateChain(1, positions, ParticleMass, ParticleRadius, friction: 0.3f); + // The body starts half a particle radius into the chain's end and, if it moves, is pushed on into it. + var x0 = -0.05f - 0.5f * ParticleRadius; + var body = kind switch + { + Box3DSoaKind.Sphere => world.AddSphere(2, (x0, 0.05f, 0f), 0.05f, 500f), + Box3DSoaKind.Capsule => world.AddCapsule(2, (x0 - 0.1f + 0.05f, 0.05f, 0f), 0.05f, 0.1f, 500f, rotation: (0f, 0f, 0.70710677f, 0.70710677f)), + Box3DSoaKind.Box => world.AddBox(2, (x0, 0.05f, 0f), (0.05f, 0.05f, 0.05f), 500f), + _ => world.AddStaticBox(2, (x0, 0.05f, 0f), (0.05f, 0.05f, 0.05f)), + }; + var view = world.View; + var worst = 0f; + for (var step = 0; step < 120; step++) + { + if (kind != Box3DSoaKind.StaticBox) view.ForceX[body] = 6f; // pushes into the chain + world.Step(Dt, Sub); + if (step >= 60) + for (var i = 0; i < 12; i++) worst = MathF.Max(worst, Penetration(view, body, i)); + } + Assert.True(worst < 0.1f * ParticleRadius, $"{kind}: a particle sits {worst / ParticleRadius:P0} of its radius inside the body"); + Assert.True(view.ParticleX[0] > positions[0].X + 0.001f, $"{kind}: the chain end was pushed on"); // the contact acted, it was not merely clear + } + + [Fact(Skip = "UNMET: the top link reads +10% of the load at 5 kg and -32% at 70 kg; tether and attachment load is not attributed to links exactly. See the cut report.")] + public void Top_link_tension_reads_the_load() + { + using var world = World(); + var positions = Line((0f, 5f, 0f), (0f, -Spacing, 0f), 48); + world.CreateChain(1, positions, ParticleMass, ParticleRadius); + world.Attach(Box3DSoaAttachKind.Static, 0, point: positions[0]); + const float radius = 0.1f, mass = 5f; + var end = positions[47]; + var sphere = world.AddSphere(10, (end.X, end.Y - radius, end.Z), radius, SphereDensity(mass, radius)); + world.Attach(Box3DSoaAttachKind.Body, 47, sphere, (0f, radius, 0f)); + var view = world.View; + Run(world, 240); + var sum = 0f; + for (var i = 0; i < 60; i++) { world.Step(Dt, Sub); sum += view.ParticleLinkTension[0]; } + var expected = (mass + 47 * ParticleMass) * 10f; // the mass and every particle below the top link + Assert.True(MathF.Abs(sum / 60f - expected) < 0.05f * expected, $"top link read {sum / 60f} N, load {expected} N"); + } + + [Fact] + public void Overload_never_splits() + { + using var world = World(); + var positions = Line((0f, 5f, 0f), (0f, -Spacing, 0f), 48); + var chain = world.CreateChain(1, positions, ParticleMass, ParticleRadius, materialId: 7); + world.Attach(Box3DSoaAttachKind.Static, 0, point: positions[0]); + const float radius = 0.2f; + var end = positions[47]; + var sphere = world.AddSphere(10, (end.X, end.Y - radius, end.Z), radius, SphereDensity(300f, radius)); + world.Attach(Box3DSoaAttachKind.Body, 47, sphere, (0f, radius, 0f)); + var view = world.View; + Run(world, 3 * 120); + var tension = 0f; + for (var i = 0; i < 47; i++) tension = MathF.Max(tension, view.ParticleLinkTension[i]); + Assert.True(tension > 1000f, $"the load is read: {tension} N"); // 300 kg: far past any strength a material would give it + Assert.Equal(1, view.ChainCount); + Assert.Equal((uint)Box3DSoaChainKind.Live, view.ChainKind[chain.Lane]); + Assert.Equal(48, view.ChainSize[chain.Lane]); + for (var i = 0; i < 48; i++) Assert.Equal(chain.Lane, view.ParticleChain[i]); + Assert.Equal(1, world.SplitChain(chain, 20, 2).Lane); // only the command splits it + } + + [Fact] + public void Split_keeps_lanes() + { + using var world = World(); + var positions = Line((0f, 5f, 0f), (0f, -Spacing, 0f), 10); + var chain = world.CreateChain(11, positions, ParticleMass, ParticleRadius, materialId: 5); + world.Attach(Box3DSoaAttachKind.Static, 0, point: positions[0]); + var view = world.View; + Run(world, 60); + var before = (view.ParticleY.ToArray(), view.ParticleX.ToArray()); + var addressBefore = ParticleLaneAddress(view); + var tail = world.SplitChain(chain, 4, 99); + view = world.View; + Assert.Equal(addressBefore, ParticleLaneAddress(view)); // no particle changes address + Assert.True(before.Item1.AsSpan().SequenceEqual(view.ParticleY) && before.Item2.AsSpan().SequenceEqual(view.ParticleX), "no particle moved"); + Assert.Equal(11ul, view.ChainStableId[chain.Lane]); + Assert.Equal(99ul, view.ChainStableId[tail.Lane]); + Assert.Equal(5, view.ChainSize[chain.Lane]); + Assert.Equal(5, view.ChainSize[tail.Lane]); + Assert.Equal(5, view.ChainFirst[tail.Lane]); + Assert.Equal(5ul, view.ChainMaterialId[tail.Lane]); + for (var i = 0; i < 10; i++) Assert.Equal(i < 5 ? chain.Lane : tail.Lane, view.ParticleChain[i]); + var tailY = view.ParticleY[9]; + Run(world, 120); + Assert.True(tailY - view.ParticleY[9] > 1.5f, "the tail falls free"); // 0.5 g t^2 = 5 m, less the settling + Assert.True(view.ParticleY[4] > 4.5f, "the head still hangs"); + } + + private static unsafe nint ParticleLaneAddress(Box3DSoaView v) + { + fixed (float* p = v.ParticleX) return (nint)p; + } + + [Fact] + public void Removed_lanes_reused_after_a_step() + { + using var world = World(particles: 12, chains: 2, attachments: 2); + var positions = Line((0f, 5f, 0f), (0f, -Spacing, 0f), 10); + var a = world.CreateChain(1, positions, ParticleMass, ParticleRadius); + var attachment = world.Attach(Box3DSoaAttachKind.Static, 0, point: positions[0]); + world.Step(Dt, Sub); + world.DestroyChain(a); + var view = world.View; + Assert.Equal((uint)Box3DSoaChainKind.Removed, view.ChainKind[a.Lane]); + Assert.Equal((uint)Box3DSoaAttachKind.Removed, view.AttachmentKind[attachment.Lane]); // it would dangle + Assert.Equal(-1, view.ParticleChain[0]); + Assert.Equal(0f, view.ParticleY[0]); + Assert.Equal(2, world.FreeParticleSlots); // only the two slots beyond the destroyed range + Assert.Throws(() => world.CreateChain(2, positions, ParticleMass, ParticleRadius)); // the range is held this step + world.Step(Dt, Sub); + Assert.Equal(12, world.FreeParticleSlots); + var b = world.CreateChain(2, positions, ParticleMass, ParticleRadius); + Assert.Equal(a.Lane, b.Lane); // the lowest reusable lane, and its range + Assert.Equal(0, world.View.ChainFirst[b.Lane]); + Assert.NotEqual(a.Generation, b.Generation); + Assert.Throws(() => world.DestroyChain(a)); // stale + Assert.Throws(() => world.SplitChain(a, 3, 5)); + Assert.Throws(() => world.Detach(attachment)); + var c = world.Attach(Box3DSoaAttachKind.Static, 0, point: positions[0]); + world.Detach(c); + world.Step(Dt, Sub); + var d = world.Attach(Box3DSoaAttachKind.Static, 0, point: positions[0]); + Assert.Equal(c.Lane, d.Lane); + Assert.Throws(() => world.Detach(c)); + } + + // ---- determinism -------------------------------------------------------------------------------------- + + private static Box3DSoaWorld PileScene(bool nudge = false) + { + var world = World(bodies: 8, particles: 128, chains: 8, attachments: 8); + world.AddStaticBox(1, (0f, -0.5f, 0f), (20f, 0.5f, 20f), friction: 0.6f); + var sphere = world.AddSphere(2, (0.2f, 0.35f, 0.1f), 0.15f, 800f); + var box = world.AddBox(3, (-0.3f, 0.2f, -0.1f), (0.2f, 0.1f, 0.2f), 300f); + for (var c = 0; c < 4; c++) + { + var from = (-0.5f + 0.1f * c, 0.5f + 0.06f * c, -0.3f + 0.2f * c); + var step = c % 2 == 0 ? (Spacing, 0f, 0.012f * c) : (0.01f * c, 0f, Spacing); + var line = Line(from, step, 24); + if (nudge && c == 2) line[7].Item2 = BitConverter.UInt32BitsToSingle(BitConverter.SingleToUInt32Bits(line[7].Item2) + 1); + world.CreateChain((ulong)(10 + c), line, ParticleMass, ParticleRadius, friction: 0.3f, materialId: (ulong)c); + } + world.Attach(Box3DSoaAttachKind.Body, 24 * 3 + 23, sphere, (0f, 0.15f, 0f)); // a chain tied to a body on the pile + world.Attach(Box3DSoaAttachKind.Static, 0, point: (-0.5f, 0.5f, -0.3f)); + return world; + } + + private static ulong HashParticles(Box3DSoaView v) + { + var h = 1469598103934665603ul; + void F(ReadOnlySpan lane) => h = Fnv(h, MemoryMarshal.AsBytes(lane)); + F(v.ParticleX); F(v.ParticleY); F(v.ParticleZ); F(v.ParticleVx); F(v.ParticleVy); F(v.ParticleVz); F(v.ParticleLinkTension); + F(v.AttachmentForceX); F(v.AttachmentForceY); F(v.AttachmentForceZ); + F(v.PositionX); F(v.PositionY); F(v.PositionZ); F(v.RotationX); F(v.RotationY); F(v.RotationZ); F(v.RotationW); + F(v.VelocityX); F(v.VelocityY); F(v.VelocityZ); F(v.AngularVelocityX); F(v.AngularVelocityY); F(v.AngularVelocityZ); + F(v.ParticleImpulseX); F(v.ParticleImpulseY); F(v.ParticleImpulseZ); + return h; + } + + private static ulong[] Checkpoints(bool nudge = false) + { + using var world = PileScene(nudge); + var view = world.View; + var hashes = new ulong[6]; + for (var c = 0; c < 6; c++) + { + Run(world, 50); + hashes[c] = HashParticles(view); + } + return hashes; + } + + [Fact] + public void Particle_world_is_byte_deterministic() + { + var a = Checkpoints(); + var b = Checkpoints(); + Assert.True(a.AsSpan().SequenceEqual(b), "two runs diverged: " + string.Join(",", a) + " vs " + string.Join(",", b)); + Assert.True(a.Distinct().Count() == 6, "the scene moves"); + Assert.False(a.AsSpan().SequenceEqual(Checkpoints(nudge: true)), "a one-ulp input difference must diverge"); + } + + [Fact] + public async Task Particle_world_is_the_same_bytes_on_any_thread_under_load() + { + // The solver is single-threaded by construction (no threads in this cut); what is provable is that nothing in it + // depends on which thread runs it or what else is running: eight worlds at once give the serial bytes. + var serial = Checkpoints(); + var results = Enumerable.Range(0, 8).Select(_ => Task.Factory.StartNew(() => Checkpoints(), TaskCreationOptions.LongRunning)).ToArray(); + await Task.WhenAll(results); + foreach (var r in results) Assert.True(Enumerable.SequenceEqual(serial, await r), "a concurrent run diverged from the serial one"); + } + + [Fact] + public void Particle_step_does_not_allocate() + { + using var world = PileScene(); + var view = world.View; + Run(world, 20); + var sink = 0f; + var before = GC.GetAllocatedBytesForCurrentThread(); + for (var i = 0; i < 100; i++) + { + world.Step(Dt, Sub); + sink += view.ParticleY[3] + view.ParticleLinkTension[1] + view.ParticleImpulseX[2] + view.AttachmentForceY[0]; + } + var allocated = GC.GetAllocatedBytesForCurrentThread() - before; + Assert.True(sink != 0f); + Assert.Equal(0L, allocated); + } + + [Fact] + public void Refusals_change_nothing() + { + using var world = World(); + var good = Line((0f, 1f, 0f), (Spacing, 0f, 0f), 4); + world.CreateChain(1, good, ParticleMass, ParticleRadius); + Assert.Throws(() => world.CreateChain(1, good, ParticleMass, ParticleRadius)); // duplicate id + var nan = Line((0f, 1f, 0f), (Spacing, 0f, 0f), 4); nan[2].Item2 = float.NaN; + Assert.Throws(() => world.CreateChain(2, nan, ParticleMass, ParticleRadius)); + Assert.Throws(() => world.CreateChain(3, good, ParticleMass, 0f)); // no radius + Assert.Throws(() => world.CreateChain(3, Line((0f, 1f, 0f), (0f, 0f, 0f), 3), ParticleMass, ParticleRadius)); // links of no length + Assert.Throws(() => world.Attach(Box3DSoaAttachKind.Body, 0, 5)); // no such body + Assert.Throws(() => world.Attach(Box3DSoaAttachKind.Static, 99)); + Assert.Throws(() => world.Attach(Box3DSoaAttachKind.Static, 0, point: (float.PositiveInfinity, 0f, 0f))); + var view = world.View; + Assert.Equal(1, view.ChainCount); + Assert.Equal(0, view.AttachmentCount); + Assert.Equal(4, view.ParticleCount); + using var plain = new Box3DSoaWorld(2); // no particle capacity: every command is refused + Assert.Throws(() => plain.CreateChain(1, good, ParticleMass, ParticleRadius)); + Assert.Throws(() => new Box3DSoaWorld(2, particleCapacity: -1)); + } + + [Fact] + public void Particle_cost_line() + { + using var world = new Box3DSoaWorld(4, gravityY: -10f, particleCapacity: 16 * 48, chainCapacity: 16, attachmentCapacity: 16); + world.AddStaticBox(1, (0f, -0.5f, 0f), (20f, 0.5f, 20f)); + for (var c = 0; c < 16; c++) + { + var chain = world.CreateChain((ulong)(10 + c), Line((0.3f * (c % 4), 1f + 0.3f * (c / 4), 0f), (0f, 0f, Spacing), 48), ParticleMass, ParticleRadius); + world.Attach(Box3DSoaAttachKind.Static, c * 48, point: (0.3f * (c % 4), 1f + 0.3f * (c / 4), 0f)); + } + Run(world, 30); + var watch = System.Diagnostics.Stopwatch.StartNew(); + Run(world, 240); + Console.Error.WriteLine($"PARTICLE_COST 16 chains x 48 particles, 4 substeps: {watch.Elapsed.TotalMilliseconds / 240:F3} ms per tick"); + Assert.True(watch.Elapsed.TotalMilliseconds > 0); + } +} + +/// The structural promises of the particle module, read from its source. +public sealed class Box3DSoaParticleSourceTests +{ + private static string Native() + { + var dir = AppContext.BaseDirectory; + while (dir != null && !File.Exists(Path.Combine(dir, "native", "Ymir.Box3D", "ymir_particles.c"))) dir = Path.GetDirectoryName(dir); + return Path.Combine(dir ?? throw new FileNotFoundException("native/Ymir.Box3D"), "native", "Ymir.Box3D"); + } + + /// The text of a function from its signature line to its closing brace at column 0. + private static string Body(string source, string signature) + { + var start = source.IndexOf(signature, StringComparison.Ordinal); + Assert.True(start >= 0, signature); + var open = source.IndexOf("\n{", start, StringComparison.Ordinal); + var end = source.IndexOf("\n}", open, StringComparison.Ordinal); + return source.Substring(start, end - start + 2); + } + + private static string Without(string source, string body) => source.Replace(body, ""); + + [Fact] + public void The_particle_module_calls_no_box3d_and_nothing_else_it_must_not() + { + var c = File.ReadAllText(Path.Combine(Native(), "ymir_particles.c")); + var h = File.ReadAllText(Path.Combine(Native(), "ymir_particles.h")); + Assert.Empty(System.Text.RegularExpressions.Regex.Matches(c + h, @"b3[A-Z_]|box3d\.h")); + var outside = Without(Without(c, Body(c, "ymir_particles* ymir_particles_create(")), Body(c, "void ymir_particles_free(")); + Assert.Empty(System.Text.RegularExpressions.Regex.Matches(outside, @"malloc|calloc|realloc|free\(")); + Assert.Empty(System.Text.RegularExpressions.Regex.Matches(c, @"\bomp\b|#pragma|pthread|_Atomic|atomic_")); + } + + [Fact] + public void The_particle_phase_has_one_caller_and_the_step_never_splits_or_destroys() + { + var soa = File.ReadAllText(Path.Combine(Native(), "ymir_box3d_soa.c")); + var c = File.ReadAllText(Path.Combine(Native(), "ymir_particles.c")); + var calls = Directory.GetFiles(Native(), "*.c").Where(f => !f.EndsWith("ymir_particles.c")).Concat(Directory.GetFiles(Native(), "*.h").Where(f => !f.EndsWith("ymir_particles.h"))) + .Sum(f => System.Text.RegularExpressions.Regex.Matches(File.ReadAllText(f), @"ymir_particles_step\(").Count); + Assert.Equal(1, calls); + var step = Body(soa, "YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_step("); + Assert.Contains("ymir_particles_step(", step); + var phase = Body(c, "void ymir_particles_step("); + foreach (var text in new[] { step, phase }) + Assert.Empty(System.Text.RegularExpressions.Regex.Matches(text, @"split_chain|destroy_chain|detach")); + } }