diff --git a/native/Ymir.Box3D/ymir_particles.c b/native/Ymir.Box3D/ymir_particles.c index 3bf6cbd..f0dcb88 100644 --- a/native/Ymir.Box3D/ymir_particles.c +++ b/native/Ymir.Box3D/ymir_particles.c @@ -1369,8 +1369,8 @@ static int density_pair(const ymir_particles* p, int32_t i, int32_t j) return (fi | fj) != 0 && (fi != 0 && fj != 0 ? 1 : collides(p, i, j)); } -// Pass B: the pair's correction, (lambda_i + lambda_j) grad W / n0, split by inverse mass: each end moves 2 w / (wi + wj) of it, -// so the pair carries equal and opposite momentum at any mass ratio and a solid end is the reaction. +// Pass B: the pair's correction, (lambda_i + lambda_j) grad W / n0, with each end moving its own inverse mass times it (XPBD, +// Macklin et al. 2016): equal and opposite momentum at any mass ratio, equal masses unchanged, a static end takes nothing. static void density_scatter(ymir_particles* p, void* ctx, int32_t i, int32_t j) { (void)ctx; @@ -1386,14 +1386,14 @@ static void density_scatter(ymir_particles* p, void* ctx, int32_t i, int32_t j) return; } v3 corr = vmul(spiky(d, sqrtf(r2), h), l / n0_of(p, h)); - v3 di = vmul(corr, 2.0f * p->inv_mass[i] / wsum), dj = vmul(corr, 2.0f * p->inv_mass[j] / wsum); + v3 di = vmul(corr, p->inv_mass[i]), dj = vmul(corr, p->inv_mass[j]); p->dp_x[i] += di.x; p->dp_y[i] += di.y; p->dp_z[i] += di.z; p->dp_x[j] -= dj.x; p->dp_y[j] -= dj.y; p->dp_z[j] -= dj.z; } // Density constraint of every fluid particle, once per substep, after contact. Pass A gathers (fluid i over the full 27 // cells, in cell then index order): n_i = sum W (self included; the solids that touch i count too), C_i = max(n_i / n0 - 1, 0), -// lambda_i = -C_i / (sum |grad C_i|^2 + eps); a solid's lambda is 0. Pass B scatters the pair corrections over the contact +// lambda_i = -C_i / (sum_k w_k |grad_k C_i|^2 + eps w_i), 0 when that is 0; a solid's lambda is 0. Pass B scatters the pair corrections over the contact // pass's half-walk and applies them after it (Jacobi), so the sums are the same on every run. static void fluid_density(ymir_particles* p) { @@ -1409,7 +1409,7 @@ static void fluid_density(ymir_particles* p) continue; } v3 xi = P(p, p->pp_x, p->pp_y, p->pp_z, i), gsum = {0.0f, 0.0f, 0.0f}; - float h0 = 4.0f * p->radius[i], n0 = n0_of(p, h0), n = poly6(0.0f, h0), ssum = 0.0f; + float h0 = 4.0f * p->radius[i], n0 = n0_of(p, h0), n = poly6(0.0f, h0), ssum = 0.0f, wi = p->inv_mass[i]; for (int32_t o = 0; o < 27; ++o) { int32_t ca = p->cell_x[i] + o / 9 - 1, cb = p->cell_y[i] + o / 3 % 3 - 1, cc = p->cell_z[i] + o % 3 - 1; @@ -1432,13 +1432,14 @@ static void fluid_density(ymir_particles* p) { v3 g = spiky(d, sqrtf(r2), h); gsum = vadd(gsum, g); - ssum += vdot(g, g); + ssum += p->inv_mass[j] * vdot(g, g); } } } float c = vmax(n / n0 - 1.0f, 0.0f); p->density[i] = n / n0; - p->lambda[i] = c == 0.0f ? 0.0f : -c / ((vdot(gsum, gsum) + ssum) / (n0 * n0) + YMIR_PBF_EPS); + float den = (wi * vdot(gsum, gsum) + ssum) / (n0 * n0) + YMIR_PBF_EPS * wi; + p->lambda[i] = c == 0.0f || den == 0.0f ? 0.0f : -c / den; } walk_half(p, density_scatter, NULL); for (int32_t i = 0; i < p->particle_count; ++i) diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaFluidTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaFluidTests.cs index 0e06ad9..d38720b 100644 --- a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaFluidTests.cs +++ b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaFluidTests.cs @@ -280,11 +280,13 @@ public sealed class Box3DSoaFluidTests // lodur:ruling:deterministic-bound-zero-reading: bound sqrt(2.530e-3 x 1e-6) = 5.0e-5, separation 50. private const double KernelReachBound = 5.0e-5; - // Drift as a fraction of the initial momentum, worst of the three mass ratios: shipped 3.50e-4 (fluid : solid 1 : 30; 1.5e-5 at the other two, - // the float32 floor as Contact_conserves_momentum's), least claimed mutant 1.165e-2 (M1, solid end of pass B not moved, at 30 : 1; M3, each - // end takes the whole correction, reads 0.139 at least): bound sqrt(3.50e-4 x 1.165e-2) = 2.0e-3, separation 33. - // Kinetic energy, equal-mass-class scene only: shipped 1.000000 (no rise at 1e-6), never above initial to the float floor 1e-5 as the contact test. - // At 30 : 1 the density pass does raise it (1.108, pure contact 1.0001): stored constraint violation released against light solids; reported, not claimed. + // Drift as a fraction of the initial momentum, worst of the four mass ratios: shipped 5.72e-4 (fluid : solid 1 : 30; 6e-6 to 1e-5, the float32 + // floor as Contact_conserves_momentum's, at the other three). Mutants, worst row / least row: M1 (solid end of pass B not moved) 0.20 / 2.5e-2; + // M3 (each end takes the whole correction) 0.148 / 2.06e-3. The midpoint sqrt(5.72e-4 x 2.5e-2) = 3.8e-3 would rise, so the bound keeps + // its 2.0e-3 (never loosened); shipped is 3.5 under it and M1 and M3 fail it on every row but M3's 100 : 1. + // Kinetic energy rise (max / initial - 1) on every row: shipped reads 1.000000, no rise at the print's 1e-6 (half a unit, 5e-7, by + // lodur:ruling:deterministic-bound-zero-reading); M9 (the pair split 2 w / (wi + wj) over a unit-mass lambda) rises 0.108 at 30 : 1 and 0.130 at + // 100 : 1. The midpoint sqrt(5e-7 x 0.108) = 2.3e-4 would loosen the standing 1e-5, which stays: separation 1.1e4. private const double MomentumBound = 2.0e-3; private const double EnergyBound = 1e-5; @@ -292,6 +294,7 @@ public sealed class Box3DSoaFluidTests [InlineData(0.064f, 0.02f)] // fluid water, solids as the pour [InlineData(0.002f, 0.06f)] // fluid : solid 1 : 30 [InlineData(0.06f, 0.002f)] // fluid : solid 30 : 1 + [InlineData(0.1f, 0.001f)] // fluid : solid 100 : 1 public void Fluid_solid_momentum_zero_g(float fluidMass, float solidMass) { // A fluid block thrown into a free sheet and a free rope in zero gravity for 2 s: every pair (fluid and fluid, fluid and solid, solid @@ -302,7 +305,7 @@ public sealed class Box3DSoaFluidTests var line = $"MOMENTUM_FLUID m_f {fluidMass} m_s {solidMass}: drift {drift:E3} of the initial momentum, kinetic energy max/initial {energy:F6}"; Readings.Add(line); Assert.True(drift <= MomentumBound, line); - if (fluidMass == 0.064f) Assert.True(energy <= 1 + EnergyBound, line); + Assert.True(energy <= 1 + EnergyBound, line); } [Fact]