From ff88ea49eca3961e1aa4194944b93a3bfbf3d3d8 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Mon, 9 Nov 2026 14:17:11 +0100 Subject: Work on masking --- modules/core/tests/particles.cpp | 414 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 414 insertions(+) (limited to 'modules/core/tests/particles.cpp') diff --git a/modules/core/tests/particles.cpp b/modules/core/tests/particles.cpp index 823bcd7..2105abc 100644 --- a/modules/core/tests/particles.cpp +++ b/modules/core/tests/particles.cpp @@ -1,9 +1,13 @@ #include +#include #include #include "../c++/particle/particle.hpp" +#include "../c++/particle/integrate.hpp" +#include "../c++/particle/contact.hpp" namespace { +struct TestDesc2D { static constexpr uint64_t D = 2u; static constexpr uint64_t Q = 9u; }; namespace sch { using namespace kel::lbm::sch; @@ -319,4 +323,414 @@ SAW_TEST("Spheroid Particle / AABB"){ // std::cout<> radi,dense; + radi.at({}) = 2.2f; + dense.at({}) = 1.0f; + auto pgrp = lbm::create_spheroid_particle_group(radi,dense,{8u}); + + auto& parts = pgrp.template get<"particles">(); + + auto place = [&](uint64_t i, double x, double y){ + auto& pos = parts.at({i}).template get<"rigid_body">().template get<"position">(); + pos.at({{0u}}).set(x); + pos.at({{1u}}).set(y); + }; + + place(0u, 4.1, 2.9); + place(1u, 40.0, 20.0); + place(2u, 63.0, 31.0); // hard against the far wall + + using PG = lbm::sch::ParticleGroup>; + kel::lbm::data> meta{{64u,32u}}; + + // Each particle must get its OWN box. Before the fix every call site + // handed particle 0's index in, so this returned particle 0's box. + auto eo_1 = lbm::particle_aabb::calculate(pgrp,{{1u}},meta); + SAW_EXPECT(eo_1.is_value(), "particle_aabb errored for particle 1"); + auto& a1 = eo_1.get_value().template get<"a">(); + auto& b1 = eo_1.get_value().template get<"b">(); + + SAW_EXPECT(a1.at({{0u}}).get() == 37u, + std::string{"particle 1 a.x: "} + std::to_string(a1.at({{0u}}).get())); + SAW_EXPECT(a1.at({{1u}}).get() == 17u, + std::string{"particle 1 a.y: "} + std::to_string(a1.at({{1u}}).get())); + SAW_EXPECT(b1.at({{0u}}).get() == 43u, + std::string{"particle 1 b.x: "} + std::to_string(b1.at({{0u}}).get())); + SAW_EXPECT(b1.at({{1u}}).get() == 23u, + std::string{"particle 1 b.y: "} + std::to_string(b1.at({{1u}}).get())); + + // The far-wall particle must be clamped to the domain. calculate() takes + // meta and, before the fix, never looked at it. + auto eo_2 = lbm::particle_aabb::calculate(pgrp,{{2u}},meta); + SAW_EXPECT(eo_2.is_value(), "particle_aabb errored for particle 2"); + auto& b2 = eo_2.get_value().template get<"b">(); + + SAW_EXPECT(b2.at({{0u}}).get() == 64u, + std::string{"particle 2 b.x should clamp to 64: "} + std::to_string(b2.at({{0u}}).get())); + SAW_EXPECT(b2.at({{1u}}).get() == 32u, + std::string{"particle 2 b.y should clamp to 32: "} + std::to_string(b2.at({{1u}}).get())); + + // Out of range index is still an error, not a silent clamp. + auto eo_bad = lbm::particle_aabb::calculate(pgrp,{{PC}},meta); + SAW_EXPECT(eo_bad.is_error(), "out of range particle index must error"); +} + +SAW_TEST("Spheroid Particle / per-particle inertial fan-out"){ + using namespace kel; + + using T = sch::Float64; + constexpr uint64_t D = 2u; + constexpr uint64_t PC = 4u; + + kel::lbm::data> radi,dense; + radi.at({}) = 2.0; + dense.at({}) = 3.0; + auto pgrp = lbm::create_spheroid_particle_group(radi,dense,{8u}); + + auto& parts = pgrp.template get<"particles">(); + + constexpr double pi = 3.14159265358979323846; + const double expect_m = pi * 2.0 * 2.0 * 3.0; // pi r^2 rho + const double expect_i = 0.5 * expect_m * 2.0 * 2.0; // m r^2 / 2 + + // Every particle must carry its own copy, not just particle 0. + for(uint64_t i = 0u; i < PC; ++i){ + auto& pi_p = parts.at({i}); + auto& rb = pi_p.template get<"rigid_body">(); + + SAW_EXPECT(pi_p.template get<"collision">().template get<"radius">().at({}).get() == 2.0, + std::string{"radius not fanned out at "} + std::to_string(i)); + SAW_EXPECT(rb.template get<"density">().at({}).get() == 3.0, + std::string{"density not fanned out at "} + std::to_string(i)); + + auto m = rb.template get<"mass">().at({}).get(); + SAW_EXPECT(std::abs(m - expect_m) < 1e-9, + std::string{"mass at "} + std::to_string(i) + ": " + std::to_string(m)); + + auto in = rb.template get<"inertia">().at({}).get(); + SAW_EXPECT(std::abs(in - expect_i) < 1e-9, + std::string{"inertia at "} + std::to_string(i) + ": " + std::to_string(in)); + } + + // Accumulators start clean. + auto& rb0 = parts.at({0u}).template get<"rigid_body">(); + SAW_EXPECT(rb0.template get<"force">().at({{0u}}).get() == 0.0, "force must start zeroed"); + SAW_EXPECT(rb0.template get<"torque">().at({}).get() == 0.0, "torque must start zeroed"); + + // Sizes are now independent per particle. + parts.at({1u}).template get<"collision">().template get<"radius">().at({}).set(5.0); + SAW_EXPECT(parts.at({0u}).template get<"collision">().template get<"radius">().at({}).get() == 2.0, + "changing one particle's radius must not touch another's"); +} + +SAW_TEST("Spheroid Particle / broadphase uses per-particle radii"){ + using namespace kel; + + using T = sch::Float64; + constexpr uint64_t D = 2u; + + kel::lbm::data> a, b; + + auto set = [](auto& p, double x, double r){ + p.template get<"rigid_body">().template get<"position">().at({{0u}}).set(x); + p.template get<"collision">().template get<"radius">().at({}).set(r); + }; + + // Centres 10 apart. Radii 2+2 do not reach; 2+9 do. + set(a, 0.0, 2.0); + set(b, 10.0, 2.0); + SAW_EXPECT((!lbm::broadphase_collision_check(a,b)), "r=2,2 at distance 10 must not overlap"); + + set(b, 10.0, 9.0); + SAW_EXPECT((lbm::broadphase_collision_check(a,b)), "r=2,9 at distance 10 must overlap"); + + // Squared centre distance comes back for the narrow phase to reuse. + auto res = (lbm::broadphase_collision_distance_squared)(a,b); + SAW_EXPECT(std::abs(res.second.at({}).get() - 100.0) < 1e-9, + std::string{"expected squared distance 100: "} + std::to_string(res.second.at({}).get())); +} + +SAW_TEST("Particle / force accumulates then integrates"){ + using namespace kel; + using T = sch::Float64; + constexpr uint64_t D = 2u; + constexpr uint64_t PC = 2u; + + + + kel::lbm::data> radi,dense; + radi.at({}) = 1.0; + dense.at({}) = 1.0 / 3.14159265358979323846; // gives mass == 1 + auto pgrp = lbm::create_spheroid_particle_group(radi,dense,{4u}); + + auto& rb = pgrp.template get<"particles">().at({0u}).template get<"rigid_body">(); + + + + + lbm::reset_particle_accumulators(pgrp.template get<"particles">().at({0u})); + SAW_EXPECT(rb.template get<"force">().at({{0u}}).get() == 0.0, "reset must zero force"); + + // Two contributors must sum, not overwrite each other. + auto& f = rb.template get<"force">(); + f.at({{0u}}).set(f.at({{0u}}).get() + 3.0); + f.at({{0u}}).set(f.at({{0u}}).get() + 1.0); + SAW_EXPECT(f.at({{0u}}).get() == 4.0, "contributors must accumulate"); + + // mass == 1, so acceleration == force. One step of x = 2x - x_old + a dt^2. + (lbm::integrate_particle)(pgrp.template get<"particles">().at({0u}), {}, {1u}); + SAW_EXPECT(std::abs(rb.template get<"acceleration">().at({{0u}}).get() - 4.0) < 1e-9, + "acceleration must come from accumulated force"); + SAW_EXPECT(std::abs(rb.template get<"position">().at({{0u}}).get() - 4.0) < 1e-9, + std::string{"expected x=4 after one unit step: "} + + std::to_string(rb.template get<"position">().at({{0u}}).get())); +} + +SAW_TEST("Particle / zero sub steps still advances"){ + using namespace kel; + using T = sch::Float64; + constexpr uint64_t D = 2u; + constexpr uint64_t PC = 1u; + + + + kel::lbm::data> radi,dense; + radi.at({}) = 1.0; + dense.at({}) = 1.0 / 3.14159265358979323846; + auto pgrp = lbm::create_spheroid_particle_group(radi,dense,{4u}); + + auto& rb = pgrp.template get<"particles">().at({0u}).template get<"rigid_body">(); + rb.template get<"force">().at({{0u}}).set(2.0); + + + + /** + * sub_steps == 0 used to give ts = 1/0 = inf and a loop that never ran, + * so the particle silently froze. Several examples pass {0u}. + */ + (lbm::integrate_particle)(pgrp.template get<"particles">().at({0u}), {}, {0u}); + + SAW_EXPECT(rb.template get<"position">().at({{0u}}).get() != 0.0, + "zero sub steps must still advance the particle"); + SAW_EXPECT(std::isfinite(rb.template get<"position">().at({{0u}}).get()), + "position must stay finite"); +} + +namespace contact_fx { +using T = sch::Float64; +constexpr uint64_t D = 2u; + +template +auto make_group(double radius, double density){ + kel::lbm::data> r,d; + r.at({}).set(radius); + d.at({}).set(density); + return kel::lbm::create_spheroid_particle_group(r,d,{4u}); +} + +template +void place(G& g, uint64_t i, double x, double y, double ox = 0.0, double oy = 0.0){ + auto& rb = g.template get<"particles">().at({i}).template get<"rigid_body">(); + rb.template get<"position">().at({{0u}}).set(x); + rb.template get<"position">().at({{1u}}).set(y); + rb.template get<"position_old">().at({{0u}}).set(x - ox); + rb.template get<"position_old">().at({{1u}}).set(y - oy); +} +} + +SAW_TEST("Contact / manifold geometry"){ + using namespace kel; + using namespace contact_fx; + + auto g = make_group<2u>(2.0, 1.0); + place(g, 0u, 0.0, 0.0); + place(g, 1u, 3.0, 0.0); + + auto& parts = g.template get<"particles">(); + kel::lbm::data> m; + + bool hit = (lbm::narrow_phase>::template contact)( + parts.at({0u}), parts.at({1u}), m); + + SAW_EXPECT(hit, "r=2,2 at distance 3 must be in contact"); + SAW_EXPECT(std::abs(m.template get<"depth">().at({}).get() - 1.0) < 1e-12, + std::string{"depth: "} + std::to_string(m.template get<"depth">().at({}).get())); + // normal points from hi (particle 1, at +x) back toward lo (particle 0) + SAW_EXPECT(std::abs(m.template get<"normal">().at({{0u}}).get() + 1.0) < 1e-12, + "normal must point from hi toward lo"); + SAW_EXPECT(std::abs(m.template get<"point">().at({{0u}}).get() - 1.5) < 1e-12, + std::string{"contact point x: "} + std::to_string(m.template get<"point">().at({{0u}}).get())); +} + +SAW_TEST("Contact / exact antisymmetry"){ + using namespace kel; + using namespace contact_fx; + + auto g = make_group<2u>(2.0, 1.0); + // Approaching each other, off-axis so the tangential path is exercised. + place(g, 0u, 0.0, 0.3, 0.05, 0.0); + place(g, 1u, 3.0, 0.0, -0.05, 0.0); + + lbm::dem_params dem{10.0, 0.4, 2.0, 0.5}; + + auto& parts = g.template get<"particles">(); + kel::lbm::data> m; + bool hit = (lbm::narrow_phase>::template contact)( + parts.at({0u}), parts.at({1u}), m); + SAW_EXPECT(hit, "expected contact"); + + auto r0 = (lbm::particle_contact_response)(m, parts.at({0u}), parts.at({1u}), true, dem, {1u}); + auto r1 = (lbm::particle_contact_response)(m, parts.at({1u}), parts.at({0u}), false, dem, {1u}); + + /** + * Bitwise, not approximate. An epsilon here would hide exactly the + * ordering mistake this test exists to catch: if the two owners evaluate + * the pair in different orders the halves differ by a rounding step and + * every contact leaks momentum. + */ + for(uint64_t i = 0u; i < D; ++i){ + SAW_EXPECT(r0.force.at({{i}}).get() == -r1.force.at({{i}}).get(), + std::string{"force component "} + std::to_string(i) + " not exactly opposite: " + + std::to_string(r0.force.at({{i}}).get()) + " vs " + + std::to_string(r1.force.at({{i}}).get())); + } +} + +SAW_TEST("Contact / never attracts"){ + using namespace kel; + using namespace contact_fx; + + auto g = make_group<2u>(2.0, 1.0); + // Overlapping but separating fast: the dashpot alone would pull them back. + place(g, 0u, 0.0, 0.0, -1.0, 0.0); + place(g, 1u, 3.0, 0.0, 1.0, 0.0); + + lbm::dem_params dem{1.0, 0.9, 0.0, 0.0}; + + auto& parts = g.template get<"particles">(); + kel::lbm::data> m; + (void)(lbm::narrow_phase>::template contact)( + parts.at({0u}), parts.at({1u}), m); + + auto r0 = (lbm::particle_contact_response)(m, parts.at({0u}), parts.at({1u}), true, dem, {1u}); + + // Particle 0 sits at -x, so any push on it must be toward -x, never +x. + SAW_EXPECT(r0.force.at({{0u}}).get() <= 0.0, + std::string{"separating pair must not be pulled together: "} + + std::to_string(r0.force.at({{0u}}).get())); +} + +SAW_TEST("Contact / degenerate pair stays finite"){ + using namespace kel; + using namespace contact_fx; + + auto g = make_group<2u>(2.0, 1.0); + // Exactly coincident centres: no geometric normal exists. + place(g, 0u, 5.0, 5.0); + place(g, 1u, 5.0, 5.0); + + lbm::dem_params dem{1.0, 0.3, 0.0, 0.0}; + + auto& parts = g.template get<"particles">(); + kel::lbm::data> m; + bool hit = (lbm::narrow_phase>::template contact)( + parts.at({0u}), parts.at({1u}), m); + SAW_EXPECT(hit, "coincident centres are in contact"); + + auto r0 = (lbm::particle_contact_response)(m, parts.at({0u}), parts.at({1u}), true, dem, {1u}); + + for(uint64_t i = 0u; i < D; ++i){ + SAW_EXPECT(std::isfinite(r0.force.at({{i}}).get()), + "degenerate contact must not produce NaN"); + } + // Penetration is capped at the smaller radius, not 2r. + SAW_EXPECT(std::abs(m.template get<"depth">().at({}).get() - 2.0) < 1e-12, + std::string{"depth should cap at the radius: "} + + std::to_string(m.template get<"depth">().at({}).get())); +} + +SAW_TEST("Contact / pair loop conserves momentum"){ + using namespace kel; + using namespace contact_fx; + + auto g = make_group<2u>(2.0, 1.0); + place(g, 0u, 0.0, 0.4, 0.1, 0.0); + place(g, 1u, 3.0, 0.0, -0.1, 0.0); + + lbm::dem_params dem{10.0, 0.4, 1.0, 0.3}; + + auto& parts = g.template get<"particles">(); + for(uint64_t i = 0u; i < 2u; ++i){ + lbm::reset_particle_accumulators(parts.at({i})); + } + for(uint64_t i = 0u; i < 2u; ++i){ + (lbm::accumulate_particle_contacts)(g.template get<"particles">(), i, dem, {1u}); + } + + // Each owner computed only its own half; the halves must cancel exactly. + for(uint64_t k = 0u; k < D; ++k){ + auto f0 = parts.at({0u}).template get<"rigid_body">().template get<"force">().at({{k}}).get(); + auto f1 = parts.at({1u}).template get<"rigid_body">().template get<"force">().at({{k}}).get(); + SAW_EXPECT(f0 == -f1, + std::string{"net force must be zero in component "} + std::to_string(k) + + ": " + std::to_string(f0) + " vs " + std::to_string(f1)); + } +} + +SAW_TEST("Contact / wall soft shell"){ + using namespace kel; + using namespace contact_fx; + + auto g = make_group<1u>(2.0, 1.0); + // Overlapping the low x wall by 0.5, drifting further in. + place(g, 0u, 1.5, 10.0, -0.1, 0.0); + + lbm::dem_params dem{4.0, 0.0, 0.0, 0.0}; + kel::lbm::data> extent{{64u,32u}}; + + auto& parts = g.template get<"particles">(); + lbm::reset_particle_accumulators(parts.at({0u})); + (lbm::accumulate_box_wall_contacts)(parts.at({0u}), extent, dem, {1u}); + + auto fx = parts.at({0u}).template get<"rigid_body">().template get<"force">().at({{0u}}).get(); + // Undamped: f = k * depth = 4 * 0.5 = 2, pushing inward (+x). + SAW_EXPECT(std::abs(fx - 2.0) < 1e-12, + std::string{"wall push: "} + std::to_string(fx)); + + // Well clear of every wall means no contact at all. + auto g2 = make_group<1u>(2.0, 1.0); + place(g2, 0u, 32.0, 16.0); + auto& p2 = g2.template get<"particles">(); + lbm::reset_particle_accumulators(p2.at({0u})); + (lbm::accumulate_box_wall_contacts)(p2.at({0u}), extent, dem, {1u}); + SAW_EXPECT(p2.at({0u}).template get<"rigid_body">().template get<"force">().at({{0u}}).get() == 0.0, + "a particle away from the walls must feel nothing"); +} + +SAW_TEST("Contact / corner touches both axes"){ + using namespace kel; + using namespace contact_fx; + + auto g = make_group<1u>(2.0, 1.0); + place(g, 0u, 1.0, 1.0); // inside both the low x and low y walls + + lbm::dem_params dem{4.0, 0.0, 0.0, 0.0}; + kel::lbm::data> extent{{64u,32u}}; + + auto& parts = g.template get<"particles">(); + lbm::reset_particle_accumulators(parts.at({0u})); + (lbm::accumulate_box_wall_contacts)(parts.at({0u}), extent, dem, {1u}); + + auto& f = parts.at({0u}).template get<"rigid_body">().template get<"force">(); + SAW_EXPECT(f.at({{0u}}).get() > 0.0, "corner must push in +x"); + SAW_EXPECT(f.at({{1u}}).get() > 0.0, "corner must push in +y"); +} } -- cgit v1.3.1