summaryrefslogtreecommitdiff
path: root/modules/core/tests
diff options
context:
space:
mode:
Diffstat (limited to 'modules/core/tests')
-rw-r--r--modules/core/tests/cell_list.cpp197
-rw-r--r--modules/core/tests/component_instantiation.cpp199
-rw-r--r--modules/core/tests/contact_dynamics.cpp272
-rw-r--r--modules/core/tests/mask.cpp238
-rw-r--r--modules/core/tests/particle_3d.cpp265
-rw-r--r--modules/core/tests/particle_consistency.cpp269
-rw-r--r--modules/core/tests/particle_edges.cpp224
-rw-r--r--modules/core/tests/particle_gaps.cpp297
-rw-r--r--modules/core/tests/particles.cpp414
-rw-r--r--modules/core/tests/rotation.cpp211
-rw-r--r--modules/core/tests/voxel_narrow.cpp191
11 files changed, 2777 insertions, 0 deletions
diff --git a/modules/core/tests/cell_list.cpp b/modules/core/tests/cell_list.cpp
new file mode 100644
index 0000000..00b260c
--- /dev/null
+++ b/modules/core/tests/cell_list.cpp
@@ -0,0 +1,197 @@
+#include <forstio/test/suite.hpp>
+
+#include <cmath>
+#include <iostream>
+
+#include "../c++/particle/cell_list.hpp"
+#include "../c++/particle/couple.hpp"
+
+namespace {
+namespace sch {
+using namespace kel::lbm::sch;
+using T = Float64;
+}
+
+constexpr uint64_t D = 2u;
+constexpr uint64_t Cap = 8u;
+
+template<uint64_t PC>
+auto make_group(double radius){
+ kel::lbm::data<sch::Scalar<sch::T>> r,d;
+ r.at({}).set(radius);
+ d.at({}).set(1.0);
+ return kel::lbm::create_spheroid_particle_group<sch::T,D,PC>(r,d,{4u});
+}
+
+template<typename G>
+void place(G& g, uint64_t i, double x, double y){
+ auto& pos = g.template get<"particles">().at({i})
+ .template get<"rigid_body">().template get<"position">();
+ pos.at({{0u}}).set(x);
+ pos.at({{1u}}).set(y);
+}
+
+SAW_TEST("CellList / bins and finds particles"){
+ using namespace kel;
+
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{64u,32u}};
+ auto cl = (lbm::make_cell_list<sch::T,D,Cap>)(extent, 2.0);
+
+ auto g = make_group<3u>(2.0);
+ place(g, 0u, 5.0, 5.0);
+ place(g, 1u, 6.0, 5.0); // same neighbourhood as 0
+ place(g, 2u, 50.0, 25.0); // far away
+
+ auto& parts = g.template get<"particles">();
+ (lbm::build_cell_list<sch::T,D,Cap>)(parts, cl);
+
+ SAW_EXPECT(cl.template get<"overflow">().at({0u}).get() == 0u, "no overflow expected");
+
+ // A cell next to particles 0 and 1 must see both, and not particle 2.
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> near{{5u,5u}};
+ bool saw[3] = {false,false,false};
+ (lbm::for_each_bin_candidate<sch::T,D,Cap>)(cl, near, [&](uint64_t p){ saw[p] = true; });
+
+ SAW_EXPECT(saw[0], "particle 0 must be a candidate");
+ SAW_EXPECT(saw[1], "particle 1 must be a candidate");
+ SAW_EXPECT(!saw[2], "a distant particle must be pruned");
+
+ // And the far cell sees only particle 2.
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> far{{50u,25u}};
+ bool saw2[3] = {false,false,false};
+ (lbm::for_each_bin_candidate<sch::T,D,Cap>)(cl, far, [&](uint64_t p){ saw2[p] = true; });
+ SAW_EXPECT(saw2[2], "particle 2 must be a candidate at its own cell");
+ SAW_EXPECT(!saw2[0] && !saw2[1], "distant particles must be pruned");
+}
+
+SAW_TEST("CellList / never misses a covering particle"){
+ using namespace kel;
+
+ /**
+ * The bin size rule is load bearing: bins are one diameter wide so a
+ * covering particle is always within the 3^D neighbourhood. Check it
+ * exhaustively against brute force rather than trusting the argument.
+ */
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{48u,24u}};
+ const double radius = 3.0;
+ auto cl = (lbm::make_cell_list<sch::T,D,Cap>)(extent, radius);
+
+ auto g = make_group<4u>(radius);
+ place(g, 0u, 4.0, 4.0);
+ place(g, 1u, 20.0, 12.0);
+ place(g, 2u, 21.5, 13.0);
+ place(g, 3u, 44.0, 20.0);
+
+ auto& parts = g.template get<"particles">();
+ (lbm::build_cell_list<sch::T,D,Cap>)(parts, cl);
+
+ uint64_t checked = 0u;
+ lbm::iterator<D>::apply([&](const auto& cell){
+ ++checked;
+
+ bool candidate[4] = {false,false,false,false};
+ (lbm::for_each_bin_candidate<sch::T,D,Cap>)(cl, cell, [&](uint64_t p){ candidate[p] = true; });
+
+ for(uint64_t p = 0u; p < 4u; ++p){
+ auto& pos = parts.at({p}).template get<"rigid_body">().template get<"position">();
+ double dx = static_cast<double>(cell.at({0u}).get()) - pos.at({{0u}}).get();
+ double dy = static_cast<double>(cell.at({1u}).get()) - pos.at({{1u}}).get();
+ bool covers = (dx*dx + dy*dy) <= radius*radius;
+
+ // Over-reporting is fine; missing a cover is not.
+ SAW_EXPECT(!covers || candidate[p],
+ std::string{"bin query missed particle "} + std::to_string(p));
+ }
+ },{},extent);
+
+ SAW_EXPECT(checked == 48u*24u, "expected to sweep the whole lattice");
+}
+
+SAW_TEST("CellList / overflow is reported not dropped"){
+ using namespace kel;
+
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{16u,16u}};
+ auto cl = (lbm::make_cell_list<sch::T,D,2u>)(extent, 2.0);
+
+ // Four particles stacked in one bin, capacity two.
+ auto g = make_group<4u>(2.0);
+ for(uint64_t i = 0u; i < 4u; ++i){
+ place(g, i, 5.0, 5.0);
+ }
+
+ auto& parts = g.template get<"particles">();
+ (lbm::build_cell_list<sch::T,D,2u>)(parts, cl);
+
+ SAW_EXPECT(cl.template get<"overflow">().at({0u}).get() == 2u,
+ std::string{"expected 2 overflows, got "}
+ + std::to_string(cl.template get<"overflow">().at({0u}).get()));
+}
+
+SAW_TEST("Couple / overlapping particles compose instead of clobbering"){
+ using namespace kel;
+
+ /**
+ * The per-particle stamp assigned porosity, so for two overlapping
+ * particles whichever work item ran last simply won. The per-cell gather
+ * multiplies the fluid fractions instead, which is the whole point of
+ * inverting the loop.
+ */
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{32u,32u}};
+ auto cl = (lbm::make_cell_list<sch::T,D,Cap>)(extent, 3.0);
+
+ auto g = make_group<2u>(3.0);
+ place(g, 0u, 15.0, 16.0);
+ place(g, 1u, 17.0, 16.0); // overlaps particle 0
+
+ auto& parts = g.template get<"particles">();
+ (lbm::build_cell_list<sch::T,D,Cap>)(parts, cl);
+
+ kel::lbm::data<sch::Scalar<sch::T>> eps;
+ eps.at({}).set(1.5);
+
+ // Deep inside both bodies: fully solid.
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> mid{{16u,16u}};
+ auto p_mid = (lbm::cell_porosity<sch::T,D,Cap>)(parts,cl,mid,eps);
+ SAW_EXPECT(p_mid.at({}).get() == 0.0,
+ std::string{"overlap region must read fully solid: "}
+ + std::to_string(p_mid.at({}).get()));
+
+ // Well outside both: fully fluid.
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> out{{2u,2u}};
+ auto p_out = (lbm::cell_porosity<sch::T,D,Cap>)(parts,cl,out,eps);
+ SAW_EXPECT(p_out.at({}).get() == 1.0,
+ std::string{"clear fluid must read 1: "} + std::to_string(p_out.at({}).get()));
+}
+
+SAW_TEST("Couple / is order independent"){
+ using namespace kel;
+
+ /**
+ * The property the per-particle scatter could not provide: the result
+ * must not depend on which particle was processed first.
+ */
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{32u,32u}};
+ kel::lbm::data<sch::Scalar<sch::T>> eps;
+ eps.at({}).set(1.5);
+
+ auto a = make_group<2u>(3.0);
+ place(a, 0u, 15.0, 16.0);
+ place(a, 1u, 17.0, 16.0);
+
+ auto b = make_group<2u>(3.0);
+ place(b, 0u, 17.0, 16.0); // same pair, swapped order
+ place(b, 1u, 15.0, 16.0);
+
+ auto cl_a = (lbm::make_cell_list<sch::T,D,Cap>)(extent, 3.0);
+ auto cl_b = (lbm::make_cell_list<sch::T,D,Cap>)(extent, 3.0);
+ (lbm::build_cell_list<sch::T,D,Cap>)(a.template get<"particles">(), cl_a);
+ (lbm::build_cell_list<sch::T,D,Cap>)(b.template get<"particles">(), cl_b);
+
+ lbm::iterator<D>::apply([&](const auto& cell){
+ auto pa = (lbm::cell_porosity<sch::T,D,Cap>)(a.template get<"particles">(),cl_a,cell,eps);
+ auto pb = (lbm::cell_porosity<sch::T,D,Cap>)(b.template get<"particles">(),cl_b,cell,eps);
+ SAW_EXPECT(pa.at({}).get() == pb.at({}).get(),
+ "porosity must not depend on particle ordering");
+ },{{10u,10u}},{{24u,24u}});
+}
+}
diff --git a/modules/core/tests/component_instantiation.cpp b/modules/core/tests/component_instantiation.cpp
new file mode 100644
index 0000000..94d0536
--- /dev/null
+++ b/modules/core/tests/component_instantiation.cpp
@@ -0,0 +1,199 @@
+#include <forstio/test/suite.hpp>
+
+#include <cmath>
+#include <iostream>
+
+#include "../c++/particle/particle.hpp"
+#include "../c++/particle/integrate.hpp"
+#include "../c++/particle/contact.hpp"
+#include "../c++/abstract/data_ptr.hpp"
+
+namespace {
+namespace sch {
+using namespace kel::lbm::sch;
+using T = Float64;
+}
+
+struct TestDesc { static constexpr uint64_t D = 2u; static constexpr uint64_t Q = 9u; };
+constexpr uint64_t D = 2u;
+
+/**
+ * The components are what a step function actually launches, but they take
+ * their group through a borrowed view -- const handle, mutable target -- so
+ * they could not be instantiated against the honestly const-correct owning
+ * types. Templates are only checked on instantiation, so a component naming a
+ * member that does not exist stayed quiet until a real kernel compiled it.
+ * Twice, here: ParticleSchema::PartAmount and CellListSchema::CellCapacity.
+ *
+ * With a host view carrying the same const semantics, the wrappers can be
+ * exercised directly.
+ */
+
+using GroupSch = kel::lbm::sch::ParticleGroup<sch::T,D,2u,kel::lbm::coll::Spheroid<sch::T>>;
+using GroupView = kel::lbm::data<sch::Ptr<GroupSch>, kel::lbm::enc::Native>;
+
+auto make_group(double radius){
+ kel::lbm::data<sch::Scalar<sch::T>> r,d;
+ r.at({}).set(radius);
+ d.at({}).set(1.0);
+ return kel::lbm::create_spheroid_particle_group<sch::T,D,2u>(r,d,{8u});
+}
+
+template<typename G>
+void rest_at(G& g, uint64_t i, double x, double y){
+ auto& rb = g.template get<"particles">().at({i}).template get<"rigid_body">();
+ const double v[2] = {x,y};
+ for(uint64_t k = 0u; k < D; ++k){
+ rb.template get<"position">().at({{k}}).set(v[k]);
+ rb.template get<"position_old">().at({{k}}).set(v[k]);
+ }
+}
+
+SAW_TEST("Component / host view keeps device const semantics"){
+ using namespace kel;
+
+ /**
+ * The whole point: a const view must still allow writing through it, the
+ * way a device handle does.
+ */
+ auto g = make_group(2.0);
+ auto& parts = g.template get<"particles">();
+
+ const kel::lbm::data<sch::Ptr<sch::FixedArray<
+ sch::Particle<sch::T,D,lbm::coll::Spheroid<sch::T>>, 2u>>, lbm::enc::Native> view{parts};
+
+ view.at({0u}).template get<"rigid_body">()
+ .template get<"position">().at({{0u}}).set(7.0);
+
+ SAW_EXPECT(parts.at({0u}).template get<"rigid_body">()
+ .template get<"position">().at({{0u}}).get() == 7.0,
+ "a write through a const view must reach the owning data");
+
+ SAW_EXPECT(view.get_dims().at({0u}).get() == 2u,
+ "the view must report the backing extent");
+}
+
+SAW_TEST("Component / ParticleForceReset instantiates and clears"){
+ using namespace kel;
+
+ auto g = make_group(2.0);
+ auto& parts = g.template get<"particles">();
+
+ auto& rb = parts.at({0u}).template get<"rigid_body">();
+ rb.template get<"force">().at({{0u}}).set(5.0);
+ rb.template get<"torque">().at({}).set(3.0);
+
+ lbm::component<sch::T,TestDesc,lbm::cmpt::ParticleForceReset,lbm::enc::Native> reset;
+ GroupView view{g};
+ reset.apply(view,{{0u}},{0u});
+
+ SAW_EXPECT(rb.template get<"force">().at({{0u}}).get() == 0.0, "force must clear");
+ SAW_EXPECT(rb.template get<"torque">().at({}).get() == 0.0, "torque must clear");
+}
+
+SAW_TEST("Component / ParticleIntegrate instantiates and moves the body"){
+ using namespace kel;
+
+ auto g = make_group(2.0);
+ auto& parts = g.template get<"particles">();
+ rest_at(g, 0u, 0.0, 0.0);
+
+ auto& rb = parts.at({0u}).template get<"rigid_body">();
+ auto mass = rb.template get<"mass">().at({}).get();
+ rb.template get<"force">().at({{0u}}).set(mass); // unit acceleration
+
+ kel::lbm::data<sch::Vector<sch::T,D>> gravity;
+ gravity.at({{1u}}).set(-0.5);
+
+ lbm::component<sch::T,TestDesc,lbm::cmpt::ParticleIntegrate,lbm::enc::Native> integrate{gravity};
+ GroupView view{g};
+ integrate.apply(view,{{0u}},{0u},{1u});
+
+ SAW_EXPECT(std::abs(rb.template get<"acceleration">().at({{0u}}).get() - 1.0) < 1e-12,
+ "force/mass must reach acceleration");
+ SAW_EXPECT(std::abs(rb.template get<"acceleration">().at({{1u}}).get() + 0.5) < 1e-12,
+ "the body acceleration must be added in");
+ SAW_EXPECT(rb.template get<"position">().at({{0u}}).get() > 0.0, "and the body must move");
+}
+
+SAW_TEST("Component / ParticleContact instantiates and accumulates"){
+ using namespace kel;
+
+ /**
+ * This is the component that previously only compiled once a GPU kernel
+ * referenced it -- the PartAmount slip lived exactly here.
+ */
+ auto g = make_group(2.0);
+ auto& parts = g.template get<"particles">();
+
+ rest_at(g, 0u, 20.0, 16.0);
+ rest_at(g, 1u, 23.0, 16.0); // overlapping by 1
+
+ lbm::dem_params<sch::T> dem{4.0, 0.0, 0.0, 0.0};
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{64u,32u}};
+
+ lbm::component<sch::T,TestDesc,lbm::cmpt::ParticleContact,lbm::enc::Native> contact{dem,extent};
+
+ for(uint64_t i = 0u; i < 2u; ++i){
+ (lbm::reset_particle_accumulators<sch::T,D>)(parts.at({i}));
+ }
+ GroupView view{g};
+ contact.apply(view,{{0u}},{0u},{1u});
+ contact.apply(view,{{1u}},{0u},{1u});
+
+ auto f0 = parts.at({0u}).template get<"rigid_body">().template get<"force">().at({{0u}}).get();
+ auto f1 = parts.at({1u}).template get<"rigid_body">().template get<"force">().at({{0u}}).get();
+
+ // Undamped, overlap 1, k = 4, so the pair pushes apart with magnitude 4.
+ SAW_EXPECT(std::abs(f0 + 4.0) < 1e-12,
+ std::string{"particle 0 should be pushed -x by k*delta: "} + std::to_string(f0));
+ SAW_EXPECT(f0 == -f1, "and the halves must cancel exactly");
+}
+
+SAW_TEST("Component / wall contact runs through the component too"){
+ using namespace kel;
+
+ auto g = make_group(2.0);
+ auto& parts = g.template get<"particles">();
+
+ // Against the low x wall, far from the other particle.
+ rest_at(g, 0u, 1.5, 16.0);
+ rest_at(g, 1u, 50.0, 16.0);
+
+ const double k_n = 6.0;
+ lbm::dem_params<sch::T> dem{k_n, 0.0, 0.0, 0.0};
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{64u,32u}};
+
+ lbm::component<sch::T,TestDesc,lbm::cmpt::ParticleContact,lbm::enc::Native> contact{dem,extent};
+
+ (lbm::reset_particle_accumulators<sch::T,D>)(parts.at({0u}));
+ GroupView view{g};
+ contact.apply(view,{{0u}},{0u},{1u});
+
+ auto fx = parts.at({0u}).template get<"rigid_body">().template get<"force">().at({{0u}}).get();
+ SAW_EXPECT(std::abs(fx - k_n * 0.5) < 1e-12,
+ std::string{"wall should push +x by k*depth: "} + std::to_string(fx));
+}
+
+SAW_TEST("Component / contact without walls leaves the wall term out"){
+ using namespace kel;
+
+ // The two-argument constructor opts out of wall contact; a particle
+ // overlapping a wall must then feel nothing from it.
+ auto g = make_group(2.0);
+ auto& parts = g.template get<"particles">();
+ rest_at(g, 0u, 1.5, 16.0);
+ rest_at(g, 1u, 50.0, 16.0);
+
+ lbm::dem_params<sch::T> dem{6.0, 0.0, 0.0, 0.0};
+ lbm::component<sch::T,TestDesc,lbm::cmpt::ParticleContact,lbm::enc::Native> contact{dem};
+
+ (lbm::reset_particle_accumulators<sch::T,D>)(parts.at({0u}));
+ GroupView view{g};
+ contact.apply(view,{{0u}},{0u},{1u});
+
+ SAW_EXPECT(parts.at({0u}).template get<"rigid_body">()
+ .template get<"force">().at({{0u}}).get() == 0.0,
+ "no walls configured means no wall force");
+}
+}
diff --git a/modules/core/tests/contact_dynamics.cpp b/modules/core/tests/contact_dynamics.cpp
new file mode 100644
index 0000000..b6545b9
--- /dev/null
+++ b/modules/core/tests/contact_dynamics.cpp
@@ -0,0 +1,272 @@
+#include <forstio/test/suite.hpp>
+
+#include <cmath>
+#include <iostream>
+
+#include "../c++/particle/particle.hpp"
+#include "../c++/particle/integrate.hpp"
+#include "../c++/particle/contact.hpp"
+
+namespace {
+namespace sch {
+using namespace kel::lbm::sch;
+using T = Float64;
+}
+
+constexpr uint64_t D = 2u;
+
+template<uint64_t PC>
+auto make_group(double radius, double density){
+ kel::lbm::data<sch::Scalar<sch::T>> r,d;
+ r.at({}).set(radius);
+ d.at({}).set(density);
+ return kel::lbm::create_spheroid_particle_group<sch::T,D,PC>(r,d,{8u});
+}
+
+template<typename G>
+void set_state(G& g, uint64_t i, double x, double y, double vx, double vy){
+ 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);
+ // Position-Verlet carries velocity implicitly in the position pair.
+ rb.template get<"position_old">().at({{0u}}).set(x - vx);
+ rb.template get<"position_old">().at({{1u}}).set(y - vy);
+}
+
+template<typename G>
+double vel_x(G& g, uint64_t i){
+ auto& rb = g.template get<"particles">().at({i}).template get<"rigid_body">();
+ return rb.template get<"position">().at({{0u}}).get()
+ - rb.template get<"position_old">().at({{0u}}).get();
+}
+
+/**
+ * One full step of the pipeline for every particle: clear, accumulate
+ * contacts, integrate.
+ */
+template<typename G>
+void step_all(G& g, uint64_t n, const kel::lbm::dem_params<sch::T>& dem){
+ using namespace kel;
+ auto& parts = g.template get<"particles">();
+ for(uint64_t i = 0u; i < n; ++i){
+ (lbm::reset_particle_accumulators<sch::T,D>)(parts.at({i}));
+ }
+ for(uint64_t i = 0u; i < n; ++i){
+ (lbm::accumulate_particle_contacts<sch::T,D>)(parts, i, dem, {1u});
+ }
+ for(uint64_t i = 0u; i < n; ++i){
+ (lbm::integrate_particle<sch::T,D>)(parts.at({i}), {}, {1u});
+ }
+}
+
+SAW_TEST("Dynamics / head-on collision reverses both bodies"){
+ using namespace kel;
+
+ auto g = make_group<2u>(2.0, 1.0);
+ // Approaching, just touching.
+ set_state(g, 0u, 0.0, 0.0, 0.05, 0.0);
+ set_state(g, 1u, 4.0, 0.0, -0.05, 0.0);
+
+ // Stiff enough to turn them around inside the window, lightly damped.
+ lbm::dem_params<sch::T> dem{2.0, 0.1, 0.0, 0.0};
+
+ bool reversed = false;
+ for(uint64_t step = 0u; step < 400u; ++step){
+ step_all(g, 2u, dem);
+ if(vel_x(g,0u) < 0.0 && vel_x(g,1u) > 0.0){
+ reversed = true;
+ break;
+ }
+ }
+
+ SAW_EXPECT(reversed, "an approaching pair must end up separating");
+}
+
+SAW_TEST("Dynamics / collision does not create energy"){
+ using namespace kel;
+
+ /**
+ * The dashpot can only remove energy. If the sign of the damping term or
+ * the velocity convention were wrong, a bounce would come back faster
+ * than it went in -- which is the classic way a DEM blows up slowly
+ * enough that you do not notice for a thousand steps.
+ */
+ auto g = make_group<2u>(2.0, 1.0);
+ const double v0 = 0.05;
+ set_state(g, 0u, 0.0, 0.0, v0, 0.0);
+ set_state(g, 1u, 4.0, 0.0, -v0, 0.0);
+
+ lbm::dem_params<sch::T> dem{2.0, 0.2, 0.0, 0.0};
+
+ double peak = 0.0;
+ for(uint64_t step = 0u; step < 600u; ++step){
+ step_all(g, 2u, dem);
+
+ auto s0 = std::abs(vel_x(g,0u));
+ auto s1 = std::abs(vel_x(g,1u));
+ if(s0 > peak){ peak = s0; }
+ if(s1 > peak){ peak = s1; }
+ }
+
+ SAW_EXPECT(peak <= v0 * 1.02,
+ std::string{"no body may exceed its incoming speed, peak was "}
+ + std::to_string(peak) + " against " + std::to_string(v0));
+}
+
+SAW_TEST("Dynamics / resting contact settles at the force balance"){
+ using namespace kel;
+
+ /**
+ * A body held against a wall by a constant body acceleration must come to
+ * rest where the spring balances the load, at an overlap of m*g/k.
+ *
+ * Note this needs confinement to mean anything: two FREE bodies pushed
+ * apart just separate and coast, so there is no equilibrium to find.
+ */
+ auto g = make_group<1u>(2.0, 1.0);
+ auto& parts = g.template get<"particles">();
+ auto& rb = parts.at({0u}).template get<"rigid_body">();
+
+ const double mass = rb.template get<"mass">().at({}).get();
+ const double k_n = 1.0;
+ const double gravity = 0.0005;
+
+ // Start just touching the low x wall, at rest.
+ set_state(g, 0u, 2.0, 16.0, 0.0, 0.0);
+
+ lbm::dem_params<sch::T> dem{k_n, 0.8, 0.0, 0.0};
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{64u,32u}};
+
+ // Gravity pointing at the wall, as a body acceleration.
+ kel::lbm::data<sch::Vector<sch::T,D>> body_acc;
+ body_acc.at({{0u}}).set(-gravity);
+ body_acc.at({{1u}}).set(0.0);
+
+ auto x_of = [&](){
+ return rb.template get<"position">().at({{0u}}).get();
+ };
+
+ for(uint64_t step = 0u; step < 4000u; ++step){
+ (lbm::reset_particle_accumulators<sch::T,D>)(parts.at({0u}));
+ (lbm::accumulate_box_wall_contacts<sch::T,D>)(parts.at({0u}), extent, dem, {1u});
+ (lbm::integrate_particle<sch::T,D>)(parts.at({0u}), body_acc, {1u});
+ }
+
+ auto settled = x_of();
+ auto expect_overlap = mass * gravity / k_n;
+ auto got_overlap = 2.0 - settled;
+
+ SAW_EXPECT(std::isfinite(settled), "must stay finite");
+ SAW_EXPECT(std::abs(got_overlap - expect_overlap) < 0.1 * expect_overlap,
+ std::string{"expected overlap m*g/k = "} + std::to_string(expect_overlap)
+ + " got " + std::to_string(got_overlap));
+
+ // And it must actually be at rest, not still travelling.
+ auto speed = std::abs(x_of() - rb.template get<"position_old">().at({{0u}}).get());
+ SAW_EXPECT(speed < 1e-4,
+ std::string{"should have come to rest, residual speed "} + std::to_string(speed));
+}
+
+SAW_TEST("Dynamics / three body momentum is conserved"){
+ using namespace kel;
+
+ /**
+ * Two bodies cancelling is weak evidence -- a sign error can cancel
+ * pairwise and still be wrong once a particle has two neighbours at once.
+ */
+ auto g = make_group<3u>(2.0, 1.0);
+ set_state(g, 0u, 0.0, 0.0, 0.03, 0.01);
+ set_state(g, 1u, 3.5, 0.0, -0.01, 0.02);
+ set_state(g, 2u, 7.0, 0.3, 0.00, -0.02);
+
+ lbm::dem_params<sch::T> dem{5.0, 0.3, 2.0, 0.4};
+
+ auto& parts = g.template get<"particles">();
+ for(uint64_t i = 0u; i < 3u; ++i){
+ (lbm::reset_particle_accumulators<sch::T,D>)(parts.at({i}));
+ }
+ for(uint64_t i = 0u; i < 3u; ++i){
+ (lbm::accumulate_particle_contacts<sch::T,D>)(parts, i, dem, {1u});
+ }
+
+ // The middle body touches both neighbours, so this is the case that
+ // pairwise cancellation alone would not catch.
+ for(uint64_t k = 0u; k < D; ++k){
+ double sum = 0.0;
+ for(uint64_t i = 0u; i < 3u; ++i){
+ sum += parts.at({i}).template get<"rigid_body">()
+ .template get<"force">().at({{k}}).get();
+ }
+ SAW_EXPECT(std::abs(sum) < 1e-12,
+ std::string{"net force in component "} + std::to_string(k)
+ + " should vanish: " + std::to_string(sum));
+ }
+
+ // And the middle body must actually be feeling both sides.
+ auto f1 = parts.at({1u}).template get<"rigid_body">().template get<"force">().at({{0u}}).get();
+ SAW_EXPECT(std::abs(f1) > 0.0, "the middle body must be loaded from both sides");
+}
+
+SAW_TEST("Dynamics / wall stops an incoming body"){
+ using namespace kel;
+
+ auto g = make_group<1u>(2.0, 1.0);
+ // Heading into the low x wall.
+ set_state(g, 0u, 2.5, 16.0, -0.05, 0.0);
+
+ lbm::dem_params<sch::T> dem{2.0, 0.2, 0.0, 0.0};
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{64u,32u}};
+
+ auto& parts = g.template get<"particles">();
+ double deepest = 2.5;
+
+ for(uint64_t step = 0u; step < 400u; ++step){
+ (lbm::reset_particle_accumulators<sch::T,D>)(parts.at({0u}));
+ (lbm::accumulate_box_wall_contacts<sch::T,D>)(parts.at({0u}), extent, dem, {1u});
+ (lbm::integrate_particle<sch::T,D>)(parts.at({0u}), {}, {1u});
+
+ auto x = parts.at({0u}).template get<"rigid_body">()
+ .template get<"position">().at({{0u}}).get();
+ if(x < deepest){ deepest = x; }
+ }
+
+ auto final_x = parts.at({0u}).template get<"rigid_body">()
+ .template get<"position">().at({{0u}}).get();
+
+ SAW_EXPECT(deepest > 0.0,
+ std::string{"the body must not pass through the wall, reached "}
+ + std::to_string(deepest));
+ SAW_EXPECT(final_x > deepest,
+ "and it must be pushed back out again");
+}
+
+SAW_TEST("Dynamics / sub steps do not change the trajectory"){
+ using namespace kel;
+
+ /**
+ * sub_steps subdivides one unit of time, so the displacement under a
+ * constant acceleration should come out the same regardless of how many
+ * pieces it is cut into. A mis-scaled dt would show up here immediately.
+ */
+ auto run = [&](uint64_t steps){
+ auto g = make_group<1u>(2.0, 1.0);
+ auto& p = g.template get<"particles">().at({0u});
+ auto& rb = p.template get<"rigid_body">();
+ rb.template get<"force">().at({{0u}}).set(rb.template get<"mass">().at({}).get());
+ (lbm::integrate_particle<sch::T,D>)(p, {}, {steps});
+ return rb.template get<"position">().at({{0u}}).get();
+ };
+
+ auto one = run(1u);
+ auto four = run(4u);
+ auto sixteen = run(16u);
+
+ SAW_EXPECT(one > 0.0, "must move at all");
+ // Verlet from rest: total displacement converges as the step shrinks.
+ SAW_EXPECT(std::abs(four - one) / one < 0.5,
+ std::string{"1 vs 4 sub steps diverged: "} + std::to_string(one)
+ + " vs " + std::to_string(four));
+ SAW_EXPECT(std::abs(sixteen - four) < std::abs(four - one),
+ "refining the sub step should be converging, not drifting");
+}
+}
diff --git a/modules/core/tests/mask.cpp b/modules/core/tests/mask.cpp
new file mode 100644
index 0000000..dfb5ef8
--- /dev/null
+++ b/modules/core/tests/mask.cpp
@@ -0,0 +1,238 @@
+#include <forstio/test/suite.hpp>
+
+#include <cmath>
+#include <iostream>
+
+#include "../c++/particle/particle.hpp"
+#include "../c++/particle/mask.hpp"
+
+namespace {
+namespace sch {
+using namespace kel::lbm::sch;
+using T = Float64;
+}
+
+constexpr uint64_t D = 2u;
+
+auto make_one(double radius){
+ kel::lbm::data<sch::Scalar<sch::T>> r,d;
+ r.at({}).set(radius);
+ d.at({}).set(1.0);
+ return kel::lbm::create_spheroid_particle_group<sch::T,D,1u>(r,d,{32u});
+}
+
+SAW_TEST("Mask / is actually filled"){
+ using namespace kel;
+
+ /**
+ * The voxelizer used to discard everything it computed, leaving every
+ * voxel at zero. A mask of all zeros reads as "surface everywhere", which
+ * is indistinguishable from a working mask until you look at a value.
+ */
+ auto g = make_one(4.0);
+ auto& mask = g.template get<"mask">();
+
+ bool any_negative = false;
+ bool any_positive = false;
+ lbm::iterator<D>::apply([&](const auto& k){
+ auto v = mask.at(k).get();
+ if(v < -0.1){ any_negative = true; }
+ if(v > 0.1){ any_positive = true; }
+ },{},mask.get_dims());
+
+ SAW_EXPECT(any_negative, "mask must have interior (negative) voxels");
+ SAW_EXPECT(any_positive, "mask must have exterior (positive) voxels");
+}
+
+SAW_TEST("Mask / stores a signed distance"){
+ using namespace kel;
+
+ auto g = make_one(4.0);
+ auto& mask = g.template get<"mask">();
+ const uint64_t R = 32u;
+
+ // Centre of a unit sphere mask: distance -1.
+ kel::lbm::data<sch::Vector<sch::T,D>> centre;
+ centre.at({{0u}}).set(0.0);
+ centre.at({{1u}}).set(0.0);
+ auto d_centre = (lbm::mask_sample_local<sch::T,D>)(mask,centre,R);
+ SAW_EXPECT(std::abs(d_centre + 1.0) < 0.05,
+ std::string{"centre distance should be -1: "} + std::to_string(d_centre));
+
+ // On the surface along +x: distance 0.
+ kel::lbm::data<sch::Vector<sch::T,D>> surf;
+ surf.at({{0u}}).set(1.0);
+ surf.at({{1u}}).set(0.0);
+ auto d_surf = (lbm::mask_sample_local<sch::T,D>)(mask,surf,R);
+ SAW_EXPECT(std::abs(d_surf) < 0.05,
+ std::string{"surface distance should be 0: "} + std::to_string(d_surf));
+
+ // Halfway out: distance -0.5.
+ kel::lbm::data<sch::Vector<sch::T,D>> half;
+ half.at({{0u}}).set(0.5);
+ half.at({{1u}}).set(0.0);
+ auto d_half = (lbm::mask_sample_local<sch::T,D>)(mask,half,R);
+ SAW_EXPECT(std::abs(d_half + 0.5) < 0.05,
+ std::string{"half radius distance should be -0.5: "} + std::to_string(d_half));
+}
+
+SAW_TEST("Mask / centre of mass is the body centre"){
+ using namespace kel;
+
+ // A sphere is symmetric, so its centroid must sit at the origin of the
+ // local frame. The old code divided by a zero mass and produced 0/0.
+ auto g = make_one(4.0);
+ auto& com = g.template get<"center_of_mass">().at({0u});
+
+ for(uint64_t i = 0u; i < D; ++i){
+ SAW_EXPECT(std::isfinite(com.at({{i}}).get()), "com must be finite");
+ SAW_EXPECT(std::abs(com.at({{i}}).get()) < 0.02,
+ std::string{"com component "} + std::to_string(i) + ": "
+ + std::to_string(com.at({{i}}).get()));
+ }
+}
+
+SAW_TEST("Mask / transform round trips"){
+ using namespace kel;
+
+ auto g = make_one(3.0);
+ auto& p = g.template get<"particles">().at({0u});
+ auto& rb = p.template get<"rigid_body">();
+ rb.template get<"position">().at({{0u}}).set(20.0);
+ rb.template get<"position">().at({{1u}}).set(11.0);
+ rb.template get<"rotation">().at({}).set(0.7);
+
+ auto& com = g.template get<"center_of_mass">().at({0u});
+
+ const double pts[4][2] = {{0.0,0.0},{0.5,-0.25},{-0.9,0.1},{1.0,0.0}};
+ for(uint64_t c = 0u; c < 4u; ++c){
+ kel::lbm::data<sch::Vector<sch::T,D>> u;
+ u.at({{0u}}).set(pts[c][0]);
+ u.at({{1u}}).set(pts[c][1]);
+
+ auto x = (lbm::mask_local_to_global<sch::T,D>)(u,p,com);
+ auto back = (lbm::mask_global_to_local<sch::T,D>)(x,p,com);
+
+ for(uint64_t i = 0u; i < D; ++i){
+ SAW_EXPECT(std::abs(back.at({{i}}).get() - u.at({{i}}).get()) < 1e-12,
+ std::string{"round trip failed at point "} + std::to_string(c));
+ }
+ }
+}
+
+SAW_TEST("Mask / scale maps to physical radius"){
+ using namespace kel;
+
+ /**
+ * The mask is normalized, so the physical bounding radius is exactly the
+ * particle's radius. That identity is what lets the bounding-sphere broad
+ * phase and the voxel narrow phase agree by construction.
+ */
+ auto g = make_one(3.0);
+ auto& p = g.template get<"particles">().at({0u});
+ auto& rb = p.template get<"rigid_body">();
+ rb.template get<"position">().at({{0u}}).set(10.0);
+ rb.template get<"position">().at({{1u}}).set(10.0);
+ rb.template get<"rotation">().at({}).set(0.0);
+
+ auto& com = g.template get<"center_of_mass">().at({0u});
+ const uint64_t R = 32u;
+
+ // Centre: distance -radius.
+ kel::lbm::data<sch::Vector<sch::T,D>> at_centre;
+ at_centre.at({{0u}}).set(10.0);
+ at_centre.at({{1u}}).set(10.0);
+ auto d0 = (lbm::mask_signed_distance<sch::T,D>)(g,p,at_centre,R);
+ SAW_EXPECT(std::abs(d0 + 3.0) < 0.2,
+ std::string{"centre should read -radius: "} + std::to_string(d0));
+
+ // Exactly one radius out: on the surface.
+ kel::lbm::data<sch::Vector<sch::T,D>> at_surface;
+ at_surface.at({{0u}}).set(13.0);
+ at_surface.at({{1u}}).set(10.0);
+ auto d1 = (lbm::mask_signed_distance<sch::T,D>)(g,p,at_surface,R);
+ SAW_EXPECT(std::abs(d1) < 0.2,
+ std::string{"one radius out should read 0: "} + std::to_string(d1));
+}
+
+SAW_TEST("Mask / solid fraction ramp"){
+ using namespace kel;
+
+ // Deep inside is fully solid, far outside fully fluid, and the ramp is
+ // monotone across the shell rather than doubling back.
+ SAW_EXPECT((lbm::solid_fraction_from_sdf<sch::T>)(-5.0, 1.5) == 1.0, "inside must be solid");
+ SAW_EXPECT((lbm::solid_fraction_from_sdf<sch::T>)( 5.0, 1.5) == 0.0, "outside must be fluid");
+
+ double prev = 1.0;
+ for(int i = 0; i <= 20; ++i){
+ double sdf = -0.75 + 1.5 * (i / 20.0);
+ double f = (lbm::solid_fraction_from_sdf<sch::T>)(sdf, 1.5);
+ SAW_EXPECT(f <= prev + 1e-12,
+ std::string{"ramp must be monotone, broke at sdf="} + std::to_string(sdf));
+ SAW_EXPECT(f >= -1e-12 && f <= 1.0 + 1e-12, "ramp must stay in [0,1]");
+ prev = f;
+ }
+}
+
+SAW_TEST("Mask / cube sdf is not a sphere"){
+ using namespace kel;
+
+ // The corner of a cube is further from the centre than its face, which is
+ // what a sphere cannot express -- the point of keeping the mask generic.
+ kel::lbm::data<sch::Vector<sch::T,D>> face;
+ face.at({{0u}}).set(1.0);
+ face.at({{1u}}).set(0.0);
+
+ kel::lbm::data<sch::Vector<sch::T,D>> corner;
+ corner.at({{0u}}).set(1.0);
+ corner.at({{1u}}).set(1.0);
+
+ SAW_EXPECT(std::abs((lbm::sdf::cube<sch::T,D>)(face)) < 1e-12, "face is on the surface");
+ SAW_EXPECT(std::abs((lbm::sdf::cube<sch::T,D>)(corner)) < 1e-12, "corner is on the surface too");
+ SAW_EXPECT((lbm::sdf::sphere<sch::T,D>)(corner) > 0.4, "but a sphere puts the corner outside");
+}
+
+SAW_TEST("Mask / porosity agrees with the analytic sphere"){
+ using namespace kel;
+
+ /**
+ * The mask path and the analytic path describe the same body, so they
+ * must agree to within the voxel pitch. This is the check that the whole
+ * "one field serves both consumers" idea rests on.
+ */
+ auto g = make_one(5.0);
+ auto& p = g.template get<"particles">().at({0u});
+ auto& rb = p.template get<"rigid_body">();
+ rb.template get<"position">().at({{0u}}).set(16.0);
+ rb.template get<"position">().at({{1u}}).set(16.0);
+ rb.template get<"rotation">().at({}).set(0.0);
+
+ kel::lbm::data<sch::Scalar<sch::T>> eps;
+ eps.at({}).set(1.5);
+ auto rad = p.template get<"collision">().template get<"radius">();
+
+ double worst = 0.0;
+ for(int i = 0; i <= 40; ++i){
+ double x = 16.0 + 10.0 * (i / 40.0) - 5.0;
+
+ kel::lbm::data<sch::Vector<sch::T,D>> pos;
+ pos.at({{0u}}).set(x);
+ pos.at({{1u}}).set(16.0);
+
+ kel::lbm::data<sch::Vector<sch::T,D>> rel;
+ rel.at({{0u}}).set(x - 16.0);
+ rel.at({{1u}}).set(0.0);
+
+ auto analytic = (lbm::particle_porosity<sch::T,D,1u,lbm::por::ParticleSpheroid<sch::T>>::calculate)(rel,rad,eps);
+ auto voxel = (lbm::particle_porosity<sch::T,D,1u,lbm::por::ParticleVoxel<sch::T>>::calculate)(g,0u,pos,eps);
+
+ auto diff = std::abs(analytic.at({}).get() - voxel.at({}).get());
+ if(diff > worst){ worst = diff; }
+ }
+
+ // Voxel pitch is 2*5/32 ~ 0.3 lattice units, and the ramp spans 1.5, so
+ // the two profiles can differ by roughly a fifth of the ramp.
+ SAW_EXPECT(worst < 0.35,
+ std::string{"mask and analytic porosity disagree by "} + std::to_string(worst));
+}
+}
diff --git a/modules/core/tests/particle_3d.cpp b/modules/core/tests/particle_3d.cpp
new file mode 100644
index 0000000..8c3e579
--- /dev/null
+++ b/modules/core/tests/particle_3d.cpp
@@ -0,0 +1,265 @@
+#include <forstio/test/suite.hpp>
+
+#include <cmath>
+#include <iostream>
+
+#include "../c++/particle/particle.hpp"
+#include "../c++/particle/integrate.hpp"
+#include "../c++/particle/contact.hpp"
+#include "../c++/particle/mask.hpp"
+#include "../c++/particle/cell_list.hpp"
+
+namespace {
+namespace sch {
+using namespace kel::lbm::sch;
+using T = Float64;
+}
+
+constexpr uint64_t D = 3u;
+
+/**
+ * Nothing anywhere else in the suite instantiates D = 3, and templates are only
+ * checked when instantiated -- which is how two "member does not exist" bugs
+ * already slipped past the host tests and were only caught by a GPU build.
+ * These exercise the 3D specializations so they cannot rot silently.
+ */
+
+template<uint64_t PC>
+auto make_group(double radius, double density, uint64_t res = 12u){
+ kel::lbm::data<sch::Scalar<sch::T>> r,d;
+ r.at({}).set(radius);
+ d.at({}).set(density);
+ return kel::lbm::create_spheroid_particle_group<sch::T,D,PC>(r,d,{res});
+}
+
+template<typename G>
+void place(G& g, uint64_t i, double x, double y, double z){
+ auto& rb = g.template get<"particles">().at({i}).template get<"rigid_body">();
+ auto& pos = rb.template get<"position">();
+ auto& old = rb.template get<"position_old">();
+ const double v[3] = {x,y,z};
+ for(uint64_t i2 = 0u; i2 < 3u; ++i2){
+ pos.at({{i2}}).set(v[i2]);
+ // At rest. position_old defaults to zero, which would otherwise read
+ // as an enormous separating velocity and damp the contact to nothing.
+ old.at({{i2}}).set(v[i2]);
+ }
+}
+
+SAW_TEST("3D / group construction fills mass and inertia"){
+ using namespace kel;
+
+ /**
+ * The 3D mass branch used to be empty, leaving total_mass at zero and then
+ * dividing the centre of mass by it.
+ */
+ const double r = 2.0;
+ const double rho = 1.5;
+ auto g = make_group<2u>(r, rho);
+
+ constexpr double pi = 3.14159265358979323846;
+ const double expect_m = (4.0/3.0) * pi * r*r*r * rho;
+ const double expect_i = 0.4 * expect_m * r * r;
+
+ for(uint64_t i = 0u; i < 2u; ++i){
+ auto& rb = g.template get<"particles">().at({i}).template get<"rigid_body">();
+
+ auto m = rb.template get<"mass">().at({}).get();
+ SAW_EXPECT(std::abs(m - expect_m) < 1e-9,
+ std::string{"3D mass should be 4/3 pi r^3 rho: "} + std::to_string(m));
+
+ auto in = rb.template get<"inertia">().at({}).get();
+ SAW_EXPECT(std::abs(in - expect_i) < 1e-9,
+ std::string{"3D inertia should be 2/5 m r^2: "} + std::to_string(in));
+ }
+
+ auto& com = g.template get<"center_of_mass">().at({0u});
+ for(uint64_t i = 0u; i < D; ++i){
+ SAW_EXPECT(std::isfinite(com.at({{i}}).get()),
+ "centre of mass must be finite, not 0/0");
+ SAW_EXPECT(std::abs(com.at({{i}}).get()) < 0.05,
+ std::string{"a sphere's centroid should sit at the origin, component "}
+ + std::to_string(i) + " = " + std::to_string(com.at({{i}}).get()));
+ }
+}
+
+SAW_TEST("3D / mask is filled with a signed distance"){
+ using namespace kel;
+
+ auto g = make_group<1u>(2.0, 1.0, 16u);
+ auto& mask = g.template get<"mask">();
+
+ kel::lbm::data<sch::Vector<sch::T,D>> centre;
+ for(uint64_t i = 0u; i < D; ++i){ centre.at({{i}}).set(0.0); }
+ auto d_centre = (lbm::mask_sample_local<sch::T,D>)(mask,centre,16u);
+ SAW_EXPECT(std::abs(d_centre + 1.0) < 0.12,
+ std::string{"3D centre should read -1: "} + std::to_string(d_centre));
+
+ kel::lbm::data<sch::Vector<sch::T,D>> surf;
+ surf.at({{0u}}).set(1.0);
+ surf.at({{1u}}).set(0.0);
+ surf.at({{2u}}).set(0.0);
+ auto d_surf = (lbm::mask_sample_local<sch::T,D>)(mask,surf,16u);
+ SAW_EXPECT(std::abs(d_surf) < 0.12,
+ std::string{"3D surface should read 0: "} + std::to_string(d_surf));
+}
+
+SAW_TEST("3D / Rodrigues rotation round trips"){
+ using namespace kel;
+
+ /**
+ * 3D rotation goes through Rodrigues on a rotation vector, a path with no
+ * other coverage at all.
+ */
+ auto g = make_group<1u>(3.0, 1.0);
+ auto& p = g.template get<"particles">().at({0u});
+ auto& rb = p.template get<"rigid_body">();
+
+ place(g, 0u, 20.0, 15.0, 10.0);
+
+ // A rotation about a tilted axis, so no component is trivially preserved.
+ auto& rot = rb.template get<"rotation">();
+ rot.at({{0u}}).set(0.3);
+ rot.at({{1u}}).set(-0.5);
+ rot.at({{2u}}).set(0.9);
+
+ auto& com = g.template get<"center_of_mass">().at({0u});
+
+ const double pts[3][3] = {{1.0,0.0,0.0},{0.0,0.6,-0.3},{-0.4,0.2,0.8}};
+ for(uint64_t c = 0u; c < 3u; ++c){
+ kel::lbm::data<sch::Vector<sch::T,D>> u;
+ for(uint64_t i = 0u; i < D; ++i){ u.at({{i}}).set(pts[c][i]); }
+
+ auto x = (lbm::mask_local_to_global<sch::T,D>)(u,p,com);
+ auto back = (lbm::mask_global_to_local<sch::T,D>)(x,p,com);
+
+ for(uint64_t i = 0u; i < D; ++i){
+ SAW_EXPECT(std::abs(back.at({{i}}).get() - u.at({{i}}).get()) < 1e-10,
+ std::string{"3D round trip failed at point "} + std::to_string(c)
+ + " component " + std::to_string(i));
+ }
+ }
+}
+
+SAW_TEST("3D / zero rotation vector is the identity"){
+ using namespace kel;
+
+ // Rodrigues divides by the angle, so the zero vector is the degenerate
+ // case that would produce NaN without a guard.
+ auto g = make_group<1u>(2.0, 1.0);
+ auto& p = g.template get<"particles">().at({0u});
+ place(g, 0u, 10.0, 10.0, 10.0);
+
+ auto& rot = p.template get<"rigid_body">().template get<"rotation">();
+ for(uint64_t i = 0u; i < D; ++i){ rot.at({{i}}).set(0.0); }
+
+ auto& com = g.template get<"center_of_mass">().at({0u});
+
+ kel::lbm::data<sch::Vector<sch::T,D>> u;
+ u.at({{0u}}).set(1.0);
+ u.at({{1u}}).set(0.0);
+ u.at({{2u}}).set(0.0);
+
+ auto x = (lbm::mask_local_to_global<sch::T,D>)(u,p,com);
+ for(uint64_t i = 0u; i < D; ++i){
+ SAW_EXPECT(std::isfinite(x.at({{i}}).get()),
+ "zero rotation must not produce NaN");
+ }
+ SAW_EXPECT(std::abs(x.at({{0u}}).get() - 12.0) < 0.05,
+ std::string{"unrotated +x should sit at centre plus radius: "}
+ + std::to_string(x.at({{0u}}).get()));
+}
+
+SAW_TEST("3D / contact is antisymmetric and conserves momentum"){
+ using namespace kel;
+
+ auto g = make_group<2u>(2.0, 1.0);
+ auto& parts = g.template get<"particles">();
+
+ // Overlapping, offset on every axis so nothing cancels by symmetry.
+ place(g, 0u, 10.0, 10.0, 10.0);
+ place(g, 1u, 12.5, 10.8, 11.2);
+
+ lbm::dem_params<sch::T> dem{8.0, 0.3, 2.0, 0.5};
+
+ for(uint64_t i = 0u; i < 2u; ++i){
+ (lbm::reset_particle_accumulators<sch::T,D>)(parts.at({i}));
+ }
+ for(uint64_t i = 0u; i < 2u; ++i){
+ (lbm::accumulate_particle_contacts<sch::T,D>)(parts, i, dem, {1u});
+ }
+
+ 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{"3D force component "} + std::to_string(k)
+ + " not exactly opposite");
+ }
+
+ auto fx = parts.at({0u}).template get<"rigid_body">()
+ .template get<"force">().at({{0u}}).get();
+ SAW_EXPECT(std::abs(fx) > 0.0, "an overlapping 3D pair must feel a force");
+}
+
+SAW_TEST("3D / linear integration works and rotation stays gated"){
+ using namespace kel;
+
+ auto g = make_group<1u>(2.0, 1.0);
+ auto& p = g.template get<"particles">().at({0u});
+ auto& rb = p.template get<"rigid_body">();
+
+ place(g, 0u, 0.0, 0.0, 0.0);
+ for(uint64_t i = 0u; i < D; ++i){
+ rb.template get<"position_old">().at({{i}}).set(0.0);
+ }
+
+ auto mass = rb.template get<"mass">().at({}).get();
+ rb.template get<"force">().at({{2u}}).set(mass); // unit acceleration in z
+ rb.template get<"torque">().at({{0u}}).set(100.0); // and a large torque
+
+ (lbm::integrate_particle<sch::T,D>)(p, {}, {1u});
+
+ SAW_EXPECT(std::abs(rb.template get<"position">().at({{2u}}).get() - 1.0) < 1e-12,
+ std::string{"3D linear motion should follow a dt^2: "}
+ + std::to_string(rb.template get<"position">().at({{2u}}).get()));
+
+ /**
+ * 3D rotation is deliberately not integrated: a scalar inertia only
+ * describes an isotropic body, and integrating the rotation vector as a
+ * displacement cannot represent non-commuting composition. Assert the gate
+ * holds, so a future change cannot quietly start producing wrong spin.
+ */
+ static_assert(!lbm::particle_angular_supported<3u>, "3D rotation must stay gated");
+ for(uint64_t i = 0u; i < D; ++i){
+ SAW_EXPECT(rb.template get<"angular_acceleration">().at({{i}}).get() == 0.0,
+ "3D angular acceleration must stay untouched even under torque");
+ }
+}
+
+SAW_TEST("3D / cell list bins in three dimensions"){
+ using namespace kel;
+
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{24u,24u,24u}};
+ auto cl = (lbm::make_cell_list<sch::T,D,8u>)(extent, 2.0);
+
+ auto g = make_group<3u>(2.0, 1.0);
+ auto& parts = g.template get<"particles">();
+ place(g, 0u, 5.0, 5.0, 5.0);
+ place(g, 1u, 6.0, 5.5, 5.0);
+ place(g, 2u, 20.0, 20.0, 20.0);
+
+ (lbm::build_cell_list<sch::T,D,8u>)(parts, cl);
+ SAW_EXPECT(cl.template get<"overflow">().at({0u}).get() == 0u, "no overflow");
+
+ // The 3^D neighbourhood is 27 bins here, not 9.
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> near{{5u,5u,5u}};
+ bool saw[3] = {false,false,false};
+ (lbm::for_each_bin_candidate<sch::T,D,8u>)(cl, near, [&](uint64_t p){ saw[p] = true; });
+
+ SAW_EXPECT(saw[0] && saw[1], "neighbours must be found in 3D");
+ SAW_EXPECT(!saw[2], "the distant particle must be pruned in 3D");
+}
+}
diff --git a/modules/core/tests/particle_consistency.cpp b/modules/core/tests/particle_consistency.cpp
new file mode 100644
index 0000000..ae40aa8
--- /dev/null
+++ b/modules/core/tests/particle_consistency.cpp
@@ -0,0 +1,269 @@
+#include <forstio/test/suite.hpp>
+
+#include <cmath>
+#include <iostream>
+
+#include "../c++/particle/particle.hpp"
+#include "../c++/particle/contact.hpp"
+#include "../c++/particle/cell_list.hpp"
+#include "../c++/particle/voxel_narrow.hpp"
+
+namespace {
+namespace sch {
+using namespace kel::lbm::sch;
+using T = Float64;
+}
+
+constexpr uint64_t D = 2u;
+constexpr uint64_t Cap = 16u;
+
+template<uint64_t PC>
+auto make_group(double radius, uint64_t res = 24u){
+ kel::lbm::data<sch::Scalar<sch::T>> r,d;
+ r.at({}).set(radius);
+ d.at({}).set(1.0);
+ return kel::lbm::create_spheroid_particle_group<sch::T,D,PC>(r,d,{res});
+}
+
+template<typename G>
+void place(G& g, uint64_t i, double x, double y){
+ auto& pos = g.template get<"particles">().at({i})
+ .template get<"rigid_body">().template get<"position">();
+ pos.at({{0u}}).set(x);
+ pos.at({{1u}}).set(y);
+}
+
+SAW_TEST("Consistency / broad phase never rejects a real contact"){
+ using namespace kel;
+
+ /**
+ * The broad phase is allowed to over-report, never to under-report: a
+ * missed pair is a contact that silently never happens. Sweep a grid of
+ * separations and assert the implication in the direction that matters.
+ */
+ auto g = make_group<2u>(3.0);
+ auto& parts = g.template get<"particles">();
+
+ for(int i = 0; i <= 60; ++i){
+ double sep = 0.1 * i;
+ place(g, 0u, 20.0, 20.0);
+ place(g, 1u, 20.0 + sep, 20.0);
+
+ bool broad = (lbm::broadphase_collision_check<sch::T,D>)(parts.at({0u}),parts.at({1u}));
+
+ kel::lbm::data<sch::ContactManifold<sch::T,D>> m;
+ bool narrow = (lbm::narrow_phase<lbm::coll::Spheroid<sch::T>>::template contact<D>)(
+ parts.at({0u}), parts.at({1u}), m);
+
+ SAW_EXPECT(!narrow || broad,
+ std::string{"narrow found a contact the broad phase rejected at sep="}
+ + std::to_string(sep));
+ }
+}
+
+SAW_TEST("Consistency / cell list finds the same contacts as brute force"){
+ using namespace kel;
+
+ /**
+ * The cell list is only an accelerator. If it ever disagrees with the
+ * exhaustive pair loop about WHICH pairs touch, every result built on it
+ * is suspect -- and the disagreement would be invisible in output.
+ */
+ const double radius = 3.0;
+ auto g = make_group<6u>(radius);
+ auto& parts = g.template get<"particles">();
+
+ // A cluster plus a couple of outliers, so some pairs touch and some do not.
+ place(g, 0u, 20.0, 20.0);
+ place(g, 1u, 24.5, 20.0);
+ place(g, 2u, 22.0, 24.0);
+ place(g, 3u, 21.0, 16.5);
+ place(g, 4u, 50.0, 40.0);
+ place(g, 5u, 54.0, 40.0);
+
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{64u,64u}};
+ auto cl = (lbm::make_cell_list<sch::T,D,Cap>)(extent, radius);
+ (lbm::build_cell_list<sch::T,D,Cap>)(parts, cl);
+
+ for(uint64_t i = 0u; i < 6u; ++i){
+ // Brute force: everyone.
+ bool brute[6] = {false,false,false,false,false,false};
+ for(uint64_t j = 0u; j < 6u; ++j){
+ if(j == i){ continue; }
+ brute[j] = (lbm::broadphase_collision_check<sch::T,D>)(parts.at({i}),parts.at({j}));
+ }
+
+ // Via the bins.
+ bool binned[6] = {false,false,false,false,false,false};
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> cell;
+ auto& pos = parts.at({i}).template get<"rigid_body">().template get<"position">();
+ for(uint64_t k = 0u; k < D; ++k){
+ cell.at({k}).set(static_cast<uint64_t>(pos.at({{k}}).get()));
+ }
+ (lbm::for_each_bin_candidate<sch::T,D,Cap>)(cl, cell, [&](uint64_t j){
+ if(j == i){ return; }
+ if((lbm::broadphase_collision_check<sch::T,D>)(parts.at({i}),parts.at({j}))){
+ binned[j] = true;
+ }
+ });
+
+ for(uint64_t j = 0u; j < 6u; ++j){
+ SAW_EXPECT(brute[j] == binned[j],
+ std::string{"pair ("} + std::to_string(i) + "," + std::to_string(j)
+ + ") disagrees: brute=" + std::to_string(brute[j])
+ + " binned=" + std::to_string(binned[j]));
+ }
+ }
+}
+
+SAW_TEST("Consistency / contact accumulation is deterministic"){
+ using namespace kel;
+
+ /**
+ * Same input twice must give bitwise identical output. Anything else
+ * means a result that cannot be reproduced or bisected.
+ */
+ lbm::dem_params<sch::T> dem{7.0, 0.35, 3.0, 0.6};
+
+ auto run = [&](){
+ auto g = make_group<4u>(3.0);
+ auto& parts = g.template get<"particles">();
+ place(g, 0u, 20.0, 20.0);
+ place(g, 1u, 24.5, 20.3);
+ place(g, 2u, 22.0, 24.0);
+ place(g, 3u, 18.0, 23.0);
+
+ for(uint64_t i = 0u; i < 4u; ++i){
+ (lbm::reset_particle_accumulators<sch::T,D>)(parts.at({i}));
+ }
+ for(uint64_t i = 0u; i < 4u; ++i){
+ (lbm::accumulate_particle_contacts<sch::T,D>)(parts, i, dem, {1u});
+ }
+
+ std::array<double,8> out{};
+ for(uint64_t i = 0u; i < 4u; ++i){
+ auto& f = parts.at({i}).template get<"rigid_body">().template get<"force">();
+ out[i*2u] = f.at({{0u}}).get();
+ out[i*2u + 1u] = f.at({{1u}}).get();
+ }
+ return out;
+ };
+
+ auto a = run();
+ auto b = run();
+
+ for(uint64_t i = 0u; i < 8u; ++i){
+ SAW_EXPECT(a[i] == b[i],
+ std::string{"run to run difference at slot "} + std::to_string(i));
+ }
+}
+
+SAW_TEST("Consistency / voxel narrow phase handles a cube mask"){
+ using namespace kel;
+
+ /**
+ * The point of the voxel narrow phase is that it is shape agnostic. A
+ * cube is the cheapest shape a sphere test cannot stand in for: its
+ * corner is further out than its face, so a face-on contact and a
+ * corner-on contact at the same centre distance differ.
+ */
+ auto g = make_group<2u>(5.0, 32u);
+ auto& mask = g.template get<"mask">();
+
+ // Overwrite the sphere mask with a cube of the same bounding radius.
+ auto com = (lbm::fill_mask_from_sdf<sch::T,D>)(
+ mask, 32u,
+ [](const kel::lbm::data<sch::Vector<sch::T,D>>& u){
+ return (lbm::sdf::cube<sch::T,D>)(u);
+ }
+ );
+ g.template get<"center_of_mass">().at({0u}) = com;
+
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{64u,64u}};
+ kel::lbm::data<sch::Scalar<sch::T>> eps;
+ eps.at({}).set(1.0);
+
+ auto& parts = g.template get<"particles">();
+
+ // Face to face along x, overlapping.
+ place(g, 0u, 28.0, 32.0);
+ place(g, 1u, 36.0, 32.0);
+ kel::lbm::data<sch::ContactManifold<sch::T,D>> m_face;
+ bool face = (lbm::voxel_contact<sch::T,D>)(g,parts.at({0u}),parts.at({1u}),extent,eps,m_face);
+
+ SAW_EXPECT(face, "overlapping cubes must register a contact");
+ // A flat face gives a clean axis-aligned normal, which is exactly what a
+ // centre-to-centre normal would also give here -- so check the shape
+ // really is a cube by its reach instead.
+ SAW_EXPECT(std::abs(m_face.template get<"normal">().at({{0u}}).get()) > 0.9,
+ "face contact normal should be close to the x axis");
+
+ // Diagonal placement: a sphere of the same bounding radius would already
+ // have separated at this distance, the cube corners still meet.
+ place(g, 0u, 28.0, 28.0);
+ place(g, 1u, 35.0, 35.0);
+ kel::lbm::data<sch::ContactManifold<sch::T,D>> m_corner;
+ bool corner = (lbm::voxel_contact<sch::T,D>)(g,parts.at({0u}),parts.at({1u}),extent,eps,m_corner);
+
+ SAW_EXPECT(corner,
+ "cube corners should still touch where spheres would not");
+}
+
+SAW_TEST("Consistency / voxel and analytic agree on a sphere across offsets"){
+ using namespace kel;
+
+ /**
+ * Spot checks pass easily; sweep the configuration instead. Both routes
+ * describe the same body, so they must track each other everywhere, not
+ * just at one convenient separation.
+ */
+ const double radius = 6.0;
+ auto g = make_group<2u>(radius, 32u);
+ auto& parts = g.template get<"particles">();
+
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{72u,72u}};
+ kel::lbm::data<sch::Scalar<sch::T>> eps;
+ eps.at({}).set(1.0);
+
+ uint64_t compared = 0u;
+ for(int dx = 2; dx <= 11; ++dx){
+ for(int dy = 0; dy <= 4; ++dy){
+ place(g, 0u, 36.0, 36.0);
+ place(g, 1u, 36.0 + dx, 36.0 + dy);
+
+ kel::lbm::data<sch::ContactManifold<sch::T,D>> mv, ma;
+ bool hv = (lbm::voxel_contact<sch::T,D>)(g,parts.at({0u}),parts.at({1u}),extent,eps,mv);
+ bool ha = (lbm::narrow_phase<lbm::coll::Spheroid<sch::T>>::template contact<D>)(
+ parts.at({0u}), parts.at({1u}), ma);
+
+ if(!ha){
+ continue;
+ }
+ SAW_EXPECT(hv,
+ std::string{"voxel missed a contact the analytic found at dx="}
+ + std::to_string(dx) + " dy=" + std::to_string(dy));
+
+ auto nv = mv.template get<"normal">();
+ auto na = ma.template get<"normal">();
+ auto dot = nv.at({{0u}}).get()*na.at({{0u}}).get()
+ + nv.at({{1u}}).get()*na.at({{1u}}).get();
+
+ SAW_EXPECT(dot > 0.9,
+ std::string{"normals diverged at dx="} + std::to_string(dx)
+ + " dy=" + std::to_string(dy) + " dot=" + std::to_string(dot));
+
+ auto diff = std::abs(mv.template get<"depth">().at({}).get()
+ - ma.template get<"depth">().at({}).get());
+ SAW_EXPECT(diff < 2.0,
+ std::string{"depths diverged at dx="} + std::to_string(dx)
+ + " dy=" + std::to_string(dy) + " by " + std::to_string(diff));
+
+ ++compared;
+ }
+ }
+
+ SAW_EXPECT(compared > 10u,
+ std::string{"expected a meaningful number of overlapping cases, got "}
+ + std::to_string(compared));
+}
+}
diff --git a/modules/core/tests/particle_edges.cpp b/modules/core/tests/particle_edges.cpp
new file mode 100644
index 0000000..cbc1bab
--- /dev/null
+++ b/modules/core/tests/particle_edges.cpp
@@ -0,0 +1,224 @@
+#include <forstio/test/suite.hpp>
+
+#include <cmath>
+#include <iostream>
+
+#include "../c++/particle/particle.hpp"
+#include "../c++/particle/contact.hpp"
+#include "../c++/particle/cell_list.hpp"
+
+namespace {
+namespace sch {
+using namespace kel::lbm::sch;
+using T = Float64;
+}
+
+constexpr uint64_t D = 2u;
+
+template<uint64_t PC>
+auto make_group(double radius){
+ kel::lbm::data<sch::Scalar<sch::T>> r,d;
+ r.at({}).set(radius);
+ d.at({}).set(1.0);
+ return kel::lbm::create_spheroid_particle_group<sch::T,D,PC>(r,d,{8u});
+}
+
+template<typename G>
+void place(G& g, uint64_t i, double x, double y){
+ auto& pos = g.template get<"particles">().at({i})
+ .template get<"rigid_body">().template get<"position">();
+ pos.at({{0u}}).set(x);
+ pos.at({{1u}}).set(y);
+}
+
+SAW_TEST("Edges / AABB clamps at the low wall too"){
+ using namespace kel;
+
+ /**
+ * The high side needed an explicit clamp against meta. The low side is
+ * handled by a ternary on the radius, which is easy to get backwards --
+ * and an index that underflows to a huge unsigned would sweep the whole
+ * field rather than failing visibly.
+ */
+ using PG = lbm::sch::ParticleGroup<sch::T,D,2u,lbm::coll::Spheroid<sch::T>>;
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> meta{{64u,32u}};
+
+ auto g = make_group<2u>(4.0);
+ place(g, 0u, 1.0, 0.5); // well inside both low walls
+ place(g, 1u, 0.0, 0.0); // exactly at the origin
+
+ for(uint64_t i = 0u; i < 2u; ++i){
+ kel::lbm::data<sch::FixedArray<sch::UInt64,1u>> idx;
+ idx.at({{0u}}).set(i);
+
+ auto eo = lbm::particle_aabb<PG>::calculate(g, idx, meta);
+ SAW_EXPECT(eo.is_value(), "aabb must succeed near the origin");
+
+ auto& a = eo.get_value().template get<"a">();
+ auto& b = eo.get_value().template get<"b">();
+
+ for(uint64_t k = 0u; k < D; ++k){
+ // Unsigned underflow would show up as an absurd lower bound.
+ SAW_EXPECT(a.at({k}).get() <= meta.at({k}).get(),
+ std::string{"lower bound underflowed for particle "} + std::to_string(i));
+ SAW_EXPECT(b.at({k}).get() <= meta.at({k}).get(),
+ std::string{"upper bound not clamped for particle "} + std::to_string(i));
+ SAW_EXPECT(a.at({k}).get() < b.at({k}).get(),
+ "box must be non-empty");
+ }
+ }
+}
+
+SAW_TEST("Edges / AABB of a particle larger than the domain"){
+ using namespace kel;
+
+ // Radius exceeds the field, so both bounds want to run off the ends.
+ using PG = lbm::sch::ParticleGroup<sch::T,D,1u,lbm::coll::Spheroid<sch::T>>;
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> meta{{8u,8u}};
+
+ auto g = make_group<1u>(20.0);
+ place(g, 0u, 4.0, 4.0);
+
+ auto eo = lbm::particle_aabb<PG>::calculate(g, {{0u}}, meta);
+ SAW_EXPECT(eo.is_value(), "oversized particle must still produce a box");
+
+ auto& a = eo.get_value().template get<"a">();
+ auto& b = eo.get_value().template get<"b">();
+ for(uint64_t k = 0u; k < D; ++k){
+ SAW_EXPECT(a.at({k}).get() == 0u, "should start at the field edge");
+ SAW_EXPECT(b.at({k}).get() == meta.at({k}).get(), "and stop at the far edge");
+ }
+}
+
+SAW_TEST("Edges / cell list tolerates particles outside the domain"){
+ using namespace kel;
+
+ /**
+ * A particle that has drifted out of bounds must still bin somewhere
+ * rather than indexing past the end of the grid.
+ */
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{32u,32u}};
+ auto cl = (lbm::make_cell_list<sch::T,D,8u>)(extent, 2.0);
+
+ auto g = make_group<3u>(2.0);
+ place(g, 0u, -50.0, -50.0); // far negative
+ place(g, 1u, 500.0, 500.0); // far positive
+ place(g, 2u, 16.0, 16.0); // legitimately inside
+
+ auto& parts = g.template get<"particles">();
+ (lbm::build_cell_list<sch::T,D,8u>)(parts, cl);
+
+ SAW_EXPECT(cl.template get<"overflow">().at({0u}).get() == 0u,
+ "clamped out-of-range particles should not overflow a bin");
+
+ // The interior particle is still findable where it actually is.
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> cell{{16u,16u}};
+ bool found = false;
+ (lbm::for_each_bin_candidate<sch::T,D,8u>)(cl, cell, [&](uint64_t p){
+ if(p == 2u){ found = true; }
+ });
+ SAW_EXPECT(found, "the in-bounds particle must still be found");
+}
+
+SAW_TEST("Edges / cell list rebuild clears the previous frame"){
+ using namespace kel;
+
+ /**
+ * Bins are not self-clearing. If a rebuild forgot to reset the counts,
+ * stale entries would accumulate and particles would appear to be in two
+ * places at once -- for a few frames, then overflow.
+ */
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{32u,32u}};
+ auto cl = (lbm::make_cell_list<sch::T,D,8u>)(extent, 2.0);
+
+ auto g = make_group<1u>(2.0);
+ auto& parts = g.template get<"particles">();
+
+ place(g, 0u, 5.0, 5.0);
+ (lbm::build_cell_list<sch::T,D,8u>)(parts, cl);
+
+ // Move it well away and rebuild.
+ place(g, 0u, 25.0, 25.0);
+ (lbm::build_cell_list<sch::T,D,8u>)(parts, cl);
+
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> old_cell{{5u,5u}};
+ uint64_t stale = 0u;
+ (lbm::for_each_bin_candidate<sch::T,D,8u>)(cl, old_cell, [&](uint64_t){ ++stale; });
+ SAW_EXPECT(stale == 0u,
+ std::string{"old position still reports "} + std::to_string(stale) + " entries");
+
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> new_cell{{25u,25u}};
+ uint64_t fresh = 0u;
+ (lbm::for_each_bin_candidate<sch::T,D,8u>)(cl, new_cell, [&](uint64_t){ ++fresh; });
+ SAW_EXPECT(fresh == 1u, "new position must report exactly one entry");
+}
+
+SAW_TEST("Edges / zero relative velocity contact is pure spring"){
+ using namespace kel;
+
+ /**
+ * With both bodies at rest the dashpot contributes nothing, so the force
+ * must be exactly k*delta. Any velocity-convention slip shows up as a
+ * spurious damping term here.
+ */
+ auto g = make_group<2u>(2.0);
+ auto& parts = g.template get<"particles">();
+
+ // At rest: position_old equals position.
+ for(uint64_t i = 0u; i < 2u; ++i){
+ auto& rb = parts.at({i}).template get<"rigid_body">();
+ auto& pos = rb.template get<"position">();
+ auto& old = rb.template get<"position_old">();
+ for(uint64_t k = 0u; k < D; ++k){
+ old.at({{k}}).set(pos.at({{k}}).get());
+ }
+ }
+ place(g, 0u, 0.0, 0.0);
+ place(g, 1u, 3.0, 0.0);
+ for(uint64_t i = 0u; i < 2u; ++i){
+ auto& rb = parts.at({i}).template get<"rigid_body">();
+ auto& pos = rb.template get<"position">();
+ auto& old = rb.template get<"position_old">();
+ for(uint64_t k = 0u; k < D; ++k){
+ old.at({{k}}).set(pos.at({{k}}).get());
+ }
+ }
+
+ const double k_n = 3.0;
+ lbm::dem_params<sch::T> dem{k_n, 0.9, 1.0, 0.5};
+
+ kel::lbm::data<sch::ContactManifold<sch::T,D>> m;
+ (void)(lbm::narrow_phase<lbm::coll::Spheroid<sch::T>>::template contact<D>)(
+ parts.at({0u}), parts.at({1u}), m);
+
+ auto res = (lbm::particle_contact_response<sch::T,D>)(
+ m, parts.at({0u}), parts.at({1u}), true, dem, {1u});
+
+ // Overlap is 2+2-3 = 1, so the spring alone gives k*1 pointing -x.
+ SAW_EXPECT(std::abs(res.force.at({{0u}}).get() + k_n) < 1e-12,
+ std::string{"expected exactly -k*delta: "} + std::to_string(res.force.at({{0u}}).get()));
+ SAW_EXPECT(res.force.at({{1u}}).get() == 0.0,
+ "no tangential force without relative motion");
+}
+
+SAW_TEST("Edges / single particle group has no self contact"){
+ using namespace kel;
+
+ // The pair loop must skip j == i; a self contact would read as a huge
+ // permanent overlap.
+ auto g = make_group<1u>(2.0);
+ auto& parts = g.template get<"particles">();
+ place(g, 0u, 10.0, 10.0);
+
+ lbm::dem_params<sch::T> dem{5.0, 0.3, 0.0, 0.0};
+
+ (lbm::reset_particle_accumulators<sch::T,D>)(parts.at({0u}));
+ (lbm::accumulate_particle_contacts<sch::T,D>)(parts, 0u, dem, {1u});
+
+ for(uint64_t k = 0u; k < D; ++k){
+ SAW_EXPECT(parts.at({0u}).template get<"rigid_body">()
+ .template get<"force">().at({{k}}).get() == 0.0,
+ "a lone particle must feel no contact force");
+ }
+}
+}
diff --git a/modules/core/tests/particle_gaps.cpp b/modules/core/tests/particle_gaps.cpp
new file mode 100644
index 0000000..3db831e
--- /dev/null
+++ b/modules/core/tests/particle_gaps.cpp
@@ -0,0 +1,297 @@
+#include <forstio/test/suite.hpp>
+
+#include <cmath>
+#include <iostream>
+
+#include "../c++/particle/particle.hpp"
+#include "../c++/particle/contact.hpp"
+#include "../c++/particle/mask.hpp"
+#include "../c++/particle/voxel_narrow.hpp"
+
+namespace {
+namespace sch {
+using namespace kel::lbm::sch;
+using T = Float64;
+}
+
+constexpr uint64_t D = 2u;
+
+template<uint64_t PC>
+auto make_group(double radius, uint64_t res = 32u){
+ kel::lbm::data<sch::Scalar<sch::T>> r,d;
+ r.at({}).set(radius);
+ d.at({}).set(1.0);
+ return kel::lbm::create_spheroid_particle_group<sch::T,D,PC>(r,d,{res});
+}
+
+template<typename G>
+void rest_at(G& g, uint64_t i, double x, double y){
+ auto& rb = g.template get<"particles">().at({i}).template get<"rigid_body">();
+ const double v[2] = {x,y};
+ for(uint64_t k = 0u; k < D; ++k){
+ rb.template get<"position">().at({{k}}).set(v[k]);
+ rb.template get<"position_old">().at({{k}}).set(v[k]);
+ }
+}
+
+SAW_TEST("Gaps / mask sampling outside the box extrapolates monotonically"){
+ using namespace kel;
+
+ /**
+ * mask_sample_local clamps the lookup to the stored box and adds back the
+ * distance to the box. That extrapolation branch has no other coverage,
+ * and getting it wrong would make far-field queries read as *inside*.
+ */
+ auto g = make_group<1u>(3.0);
+ auto& mask = g.template get<"mask">();
+ const uint64_t R = 32u;
+
+ double prev = -2.0;
+ for(int i = 0; i <= 12; ++i){
+ double d = 0.5 * i; // walk out along +x, past the box edge at 1.0
+
+ kel::lbm::data<sch::Vector<sch::T,D>> u;
+ u.at({{0u}}).set(d);
+ u.at({{1u}}).set(0.0);
+
+ auto s = (lbm::mask_sample_local<sch::T,D>)(mask,u,R);
+
+ SAW_EXPECT(std::isfinite(s),
+ std::string{"sample must stay finite at u="} + std::to_string(d));
+ SAW_EXPECT(s > prev - 1e-9,
+ std::string{"distance must grow outward, broke at u="} + std::to_string(d));
+ if(d > 1.5){
+ SAW_EXPECT(s > 0.0,
+ std::string{"well outside must read positive, got "} + std::to_string(s)
+ + " at u=" + std::to_string(d));
+ }
+ prev = s;
+ }
+}
+
+SAW_TEST("Gaps / solid fraction is exact at the shell edges"){
+ using namespace kel;
+
+ // The ramp endpoints are where an off-by-half would hide.
+ const double eps = 2.0;
+ SAW_EXPECT((lbm::solid_fraction_from_sdf<sch::T>)(-eps/2, eps) == 1.0,
+ "the inner edge of the shell must be fully solid");
+ SAW_EXPECT((lbm::solid_fraction_from_sdf<sch::T>)( eps/2, eps) == 0.0,
+ "the outer edge must be fully fluid");
+
+ // Midpoint of a cos^2 ramp is exactly one half.
+ auto mid = (lbm::solid_fraction_from_sdf<sch::T>)(0.0, eps);
+ SAW_EXPECT(std::abs(mid - 0.5) < 1e-12,
+ std::string{"the surface itself should read 0.5: "} + std::to_string(mid));
+}
+
+SAW_TEST("Gaps / one shared mask serves particles of different sizes"){
+ using namespace kel;
+
+ /**
+ * This is the central claim of the normalized-mask design: the group holds
+ * one shape and each particle scales it by its own radius. If the scaling
+ * were dropped the two bodies below would read identically.
+ */
+ auto g = make_group<2u>(4.0);
+ auto& parts = g.template get<"particles">();
+ auto& com = g.template get<"center_of_mass">().at({0u});
+ const uint64_t R = 32u;
+
+ rest_at(g, 0u, 20.0, 20.0);
+ rest_at(g, 1u, 50.0, 20.0);
+
+ // Shrink the second particle; same group, same mask.
+ parts.at({1u}).template get<"collision">().template get<"radius">().at({}).set(1.0);
+
+ // Two lattice units out from each centre: inside the big one, outside the
+ // small one.
+ kel::lbm::data<sch::Vector<sch::T,D>> probe_big;
+ probe_big.at({{0u}}).set(22.0);
+ probe_big.at({{1u}}).set(20.0);
+
+ kel::lbm::data<sch::Vector<sch::T,D>> probe_small;
+ probe_small.at({{0u}}).set(52.0);
+ probe_small.at({{1u}}).set(20.0);
+
+ auto d_big = (lbm::mask_signed_distance<sch::T,D>)(g,parts.at({0u}),probe_big,R);
+ auto d_small = (lbm::mask_signed_distance<sch::T,D>)(g,parts.at({1u}),probe_small,R);
+
+ SAW_EXPECT(d_big < -0.5,
+ std::string{"2 units into a radius 4 body must be inside: "} + std::to_string(d_big));
+ SAW_EXPECT(d_small > 0.5,
+ std::string{"2 units from a radius 1 body must be outside: "} + std::to_string(d_small));
+
+ // And the physical bounding radius really is the particle's radius, which
+ // is what makes the broad phase a valid bound for the voxel narrow phase.
+ kel::lbm::data<sch::Vector<sch::T,D>> at_edge;
+ at_edge.at({{0u}}).set(51.0);
+ at_edge.at({{1u}}).set(20.0);
+ auto d_edge = (lbm::mask_signed_distance<sch::T,D>)(g,parts.at({1u}),at_edge,R);
+ SAW_EXPECT(std::abs(d_edge) < 0.15,
+ std::string{"one radius out should be the surface: "} + std::to_string(d_edge));
+}
+
+SAW_TEST("Gaps / mixed size pair contacts correctly"){
+ using namespace kel;
+
+ auto g = make_group<2u>(4.0);
+ auto& parts = g.template get<"particles">();
+
+ parts.at({1u}).template get<"collision">().template get<"radius">().at({}).set(1.0);
+
+ // Centres 4.5 apart: radii 4 + 1 = 5, so they overlap by 0.5.
+ rest_at(g, 0u, 20.0, 20.0);
+ rest_at(g, 1u, 24.5, 20.0);
+
+ kel::lbm::data<sch::ContactManifold<sch::T,D>> m;
+ bool hit = (lbm::narrow_phase<lbm::coll::Spheroid<sch::T>>::template contact<D>)(
+ parts.at({0u}), parts.at({1u}), m);
+
+ SAW_EXPECT(hit, "a mixed size overlapping pair must register");
+ SAW_EXPECT(std::abs(m.template get<"depth">().at({}).get() - 0.5) < 1e-12,
+ std::string{"depth should be r0+r1-d = 0.5: "}
+ + std::to_string(m.template get<"depth">().at({}).get()));
+
+ // The contact point must lie on the small body's surface, i.e. much
+ // closer to it than to the large one.
+ auto px = m.template get<"point">().at({{0u}}).get();
+ SAW_EXPECT(px > 23.0,
+ std::string{"contact should sit near the small body: "} + std::to_string(px));
+}
+
+SAW_TEST("Gaps / the high wall pushes inward"){
+ using namespace kel;
+
+ /**
+ * Only the low wall and the corner had coverage. The high face has its own
+ * sign and its own offset from the extent, either of which could be wrong
+ * independently.
+ */
+ auto g = make_group<1u>(2.0);
+ auto& parts = g.template get<"particles">();
+
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{64u,32u}};
+ const double k_n = 4.0;
+ lbm::dem_params<sch::T> dem{k_n, 0.0, 0.0, 0.0};
+
+ // Overlapping the high x wall by 0.5.
+ rest_at(g, 0u, 62.5, 16.0);
+ (lbm::reset_particle_accumulators<sch::T,D>)(parts.at({0u}));
+ (lbm::accumulate_box_wall_contacts<sch::T,D>)(parts.at({0u}), extent, dem, {1u});
+
+ auto fx = parts.at({0u}).template get<"rigid_body">()
+ .template get<"force">().at({{0u}}).get();
+ SAW_EXPECT(std::abs(fx + k_n * 0.5) < 1e-12,
+ std::string{"high wall should push -x by k*depth: "} + std::to_string(fx));
+
+ // Same on the high y face, which uses the other extent.
+ auto g2 = make_group<1u>(2.0);
+ auto& p2 = g2.template get<"particles">();
+ rest_at(g2, 0u, 32.0, 30.5);
+ (lbm::reset_particle_accumulators<sch::T,D>)(p2.at({0u}));
+ (lbm::accumulate_box_wall_contacts<sch::T,D>)(p2.at({0u}), extent, dem, {1u});
+
+ auto fy = p2.at({0u}).template get<"rigid_body">()
+ .template get<"force">().at({{1u}}).get();
+ SAW_EXPECT(std::abs(fy + k_n * 0.5) < 1e-12,
+ std::string{"high y wall should push -y by k*depth: "} + std::to_string(fy));
+}
+
+SAW_TEST("Gaps / friction is capped by Coulomb"){
+ using namespace kel;
+
+ /**
+ * The tangential force is min(gamma_t |v_t|, mu f_n). With a large
+ * gamma_t the cap must bind, otherwise friction can exceed the normal
+ * load and a sliding contact gains energy.
+ */
+ auto g = make_group<2u>(2.0);
+ auto& parts = g.template get<"particles">();
+
+ // Overlapping, sliding hard past each other.
+ auto set = [&](uint64_t i, double x, double y, double vy){
+ auto& rb = parts.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);
+ rb.template get<"position_old">().at({{1u}}).set(y - vy);
+ };
+ set(0u, 0.0, 0.0, 1.0);
+ set(1u, 3.0, 0.0, -1.0);
+
+ const double k_n = 2.0;
+ const double mu = 0.25;
+ // gamma_t absurdly large so the viscous term would dominate uncapped.
+ lbm::dem_params<sch::T> dem{k_n, 0.0, 1000.0, mu};
+
+ kel::lbm::data<sch::ContactManifold<sch::T,D>> m;
+ (void)(lbm::narrow_phase<lbm::coll::Spheroid<sch::T>>::template contact<D>)(
+ parts.at({0u}), parts.at({1u}), m);
+
+ auto res = (lbm::particle_contact_response<sch::T,D>)(
+ m, parts.at({0u}), parts.at({1u}), true, dem, {1u});
+
+ // Undamped normal force is k*delta = 2*1 = 2, so friction caps at 0.5.
+ auto f_n = std::abs(res.force.at({{0u}}).get());
+ auto f_t = std::abs(res.force.at({{1u}}).get());
+
+ SAW_EXPECT(std::abs(f_n - k_n * 1.0) < 1e-12,
+ std::string{"normal force should be k*delta: "} + std::to_string(f_n));
+ SAW_EXPECT(std::abs(f_t - mu * f_n) < 1e-9,
+ std::string{"friction should sit exactly on the Coulomb cap "}
+ + std::to_string(mu * f_n) + ", got " + std::to_string(f_t));
+}
+
+SAW_TEST("Gaps / stiffness helper matches its own definition"){
+ using namespace kel;
+
+ /**
+ * dem_stiffness_for is the only guidance a caller gets for choosing k_n,
+ * so it should at least be self consistent: a body hitting at v0 with the
+ * suggested stiffness should stop near delta_max.
+ */
+ const double m_eff = 4.0;
+ const double v0 = 0.1;
+ const double delta_max = 0.05;
+
+ auto k = (lbm::dem_stiffness_for<sch::T>)(m_eff, v0, delta_max);
+
+ // Energy balance: k delta^2 / 2 == m v^2 / 2.
+ auto spring_energy = 0.5 * k * delta_max * delta_max;
+ auto kinetic = 0.5 * m_eff * v0 * v0;
+
+ SAW_EXPECT(std::abs(spring_energy - kinetic) < 1e-12,
+ std::string{"energies should match: "} + std::to_string(spring_energy)
+ + " vs " + std::to_string(kinetic));
+ SAW_EXPECT(k > 0.0, "stiffness must be positive");
+}
+
+SAW_TEST("Gaps / voxel contact is deterministic"){
+ using namespace kel;
+
+ // The overlap integral walks the lattice; the sweep order must not drift
+ // between calls or results stop being reproducible.
+ auto g = make_group<2u>(5.0);
+ auto& parts = g.template get<"particles">();
+ rest_at(g, 0u, 28.0, 30.0);
+ rest_at(g, 1u, 35.0, 32.0);
+
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{64u,64u}};
+ kel::lbm::data<sch::Scalar<sch::T>> eps;
+ eps.at({}).set(1.0);
+
+ kel::lbm::data<sch::ContactManifold<sch::T,D>> a, b;
+ bool ha = (lbm::voxel_contact<sch::T,D>)(g,parts.at({0u}),parts.at({1u}),extent,eps,a);
+ bool hb = (lbm::voxel_contact<sch::T,D>)(g,parts.at({0u}),parts.at({1u}),extent,eps,b);
+
+ SAW_EXPECT(ha && hb, "both calls must find the contact");
+ SAW_EXPECT(a.template get<"depth">().at({}).get() == b.template get<"depth">().at({}).get(),
+ "depth must be bitwise reproducible");
+ for(uint64_t k = 0u; k < D; ++k){
+ SAW_EXPECT(a.template get<"normal">().at({{k}}).get()
+ == b.template get<"normal">().at({{k}}).get(),
+ "normal must be bitwise reproducible");
+ }
+}
+}
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 <forstio/test/suite.hpp>
+#include <cmath>
#include <iostream>
#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<<res<<std::endl;
}
+
+SAW_TEST("Spheroid Particle / AABB per particle"){
+ using namespace kel;
+
+ using T = sch::Float64;
+ constexpr uint64_t D = 2u;
+ constexpr uint64_t PC = 3u;
+
+ kel::lbm::data<sch::Scalar<T>> radi,dense;
+ radi.at({}) = 2.2f;
+ dense.at({}) = 1.0f;
+ auto pgrp = lbm::create_spheroid_particle_group<T,D,PC>(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<T,D,PC,lbm::coll::Spheroid<T>>;
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> 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<PG>::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<PG>::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<PG>::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<sch::Scalar<T>> radi,dense;
+ radi.at({}) = 2.0;
+ dense.at({}) = 3.0;
+ auto pgrp = lbm::create_spheroid_particle_group<T,D,PC>(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<sch::Particle<T,D>> 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<T,D>(a,b)), "r=2,2 at distance 10 must not overlap");
+
+ set(b, 10.0, 9.0);
+ SAW_EXPECT((lbm::broadphase_collision_check<T,D>(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<T,D>)(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<sch::Scalar<T>> radi,dense;
+ radi.at({}) = 1.0;
+ dense.at({}) = 1.0 / 3.14159265358979323846; // gives mass == 1
+ auto pgrp = lbm::create_spheroid_particle_group<T,D,PC>(radi,dense,{4u});
+
+ auto& rb = pgrp.template get<"particles">().at({0u}).template get<"rigid_body">();
+
+
+
+
+ lbm::reset_particle_accumulators<T,D>(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<T,D>)(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<sch::Scalar<T>> radi,dense;
+ radi.at({}) = 1.0;
+ dense.at({}) = 1.0 / 3.14159265358979323846;
+ auto pgrp = lbm::create_spheroid_particle_group<T,D,PC>(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<T,D>)(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<uint64_t PC>
+auto make_group(double radius, double density){
+ kel::lbm::data<sch::Scalar<T>> r,d;
+ r.at({}).set(radius);
+ d.at({}).set(density);
+ return kel::lbm::create_spheroid_particle_group<T,D,PC>(r,d,{4u});
+}
+
+template<typename G>
+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<sch::ContactManifold<T,D>> m;
+
+ bool hit = (lbm::narrow_phase<lbm::coll::Spheroid<T>>::template contact<D>)(
+ 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<T> dem{10.0, 0.4, 2.0, 0.5};
+
+ auto& parts = g.template get<"particles">();
+ kel::lbm::data<sch::ContactManifold<T,D>> m;
+ bool hit = (lbm::narrow_phase<lbm::coll::Spheroid<T>>::template contact<D>)(
+ parts.at({0u}), parts.at({1u}), m);
+ SAW_EXPECT(hit, "expected contact");
+
+ auto r0 = (lbm::particle_contact_response<T,D>)(m, parts.at({0u}), parts.at({1u}), true, dem, {1u});
+ auto r1 = (lbm::particle_contact_response<T,D>)(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<T> dem{1.0, 0.9, 0.0, 0.0};
+
+ auto& parts = g.template get<"particles">();
+ kel::lbm::data<sch::ContactManifold<T,D>> m;
+ (void)(lbm::narrow_phase<lbm::coll::Spheroid<T>>::template contact<D>)(
+ parts.at({0u}), parts.at({1u}), m);
+
+ auto r0 = (lbm::particle_contact_response<T,D>)(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<T> dem{1.0, 0.3, 0.0, 0.0};
+
+ auto& parts = g.template get<"particles">();
+ kel::lbm::data<sch::ContactManifold<T,D>> m;
+ bool hit = (lbm::narrow_phase<lbm::coll::Spheroid<T>>::template contact<D>)(
+ parts.at({0u}), parts.at({1u}), m);
+ SAW_EXPECT(hit, "coincident centres are in contact");
+
+ auto r0 = (lbm::particle_contact_response<T,D>)(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<T> 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<T,D>(parts.at({i}));
+ }
+ for(uint64_t i = 0u; i < 2u; ++i){
+ (lbm::accumulate_particle_contacts<T,D>)(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<T> dem{4.0, 0.0, 0.0, 0.0};
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{64u,32u}};
+
+ auto& parts = g.template get<"particles">();
+ lbm::reset_particle_accumulators<T,D>(parts.at({0u}));
+ (lbm::accumulate_box_wall_contacts<T,D>)(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<T,D>(p2.at({0u}));
+ (lbm::accumulate_box_wall_contacts<T,D>)(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<T> dem{4.0, 0.0, 0.0, 0.0};
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{64u,32u}};
+
+ auto& parts = g.template get<"particles">();
+ lbm::reset_particle_accumulators<T,D>(parts.at({0u}));
+ (lbm::accumulate_box_wall_contacts<T,D>)(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");
+}
}
diff --git a/modules/core/tests/rotation.cpp b/modules/core/tests/rotation.cpp
new file mode 100644
index 0000000..e1e8c46
--- /dev/null
+++ b/modules/core/tests/rotation.cpp
@@ -0,0 +1,211 @@
+#include <forstio/test/suite.hpp>
+
+#include <cmath>
+#include <iostream>
+
+#include "../c++/particle/particle.hpp"
+#include "../c++/particle/integrate.hpp"
+#include "../c++/particle/contact.hpp"
+#include "../c++/particle/mask.hpp"
+
+namespace {
+namespace sch {
+using namespace kel::lbm::sch;
+using T = Float64;
+}
+
+constexpr uint64_t D = 2u;
+
+template<uint64_t PC>
+auto make_group(double radius, double density){
+ kel::lbm::data<sch::Scalar<sch::T>> r,d;
+ r.at({}).set(radius);
+ d.at({}).set(density);
+ return kel::lbm::create_spheroid_particle_group<sch::T,D,PC>(r,d,{16u});
+}
+
+SAW_TEST("Rotation / support flag matches the integrator"){
+ using namespace kel;
+
+ /**
+ * 3D rotation is deliberately absent rather than approximated: a scalar
+ * inertia only describes an isotropic body, and integrating the rotation
+ * vector as a displacement cannot represent non-commuting composition.
+ * The flag exists so torque accumulation and angular integration cannot
+ * drift apart -- accumulating a torque nothing consumes would be a silent
+ * no-op.
+ */
+ static_assert(lbm::particle_angular_supported<2u>, "2D rotation must be live");
+ static_assert(!lbm::particle_angular_supported<3u>, "3D rotation must stay gated");
+}
+
+SAW_TEST("Rotation / constant torque gives constant angular acceleration"){
+ using namespace kel;
+
+ /**
+ * Nothing in the library ever wrote angular_acceleration before contact
+ * started producing torque, so verlet_step_lambda's angular half had
+ * never actually run. This is the first exercise of that path.
+ */
+ auto g = make_group<1u>(2.0, 1.0);
+ auto& p = g.template get<"particles">().at({0u});
+ auto& rb = p.template get<"rigid_body">();
+
+ auto inertia = rb.template get<"inertia">().at({}).get();
+ SAW_EXPECT(inertia > 0.0,
+ std::string{"inertia must be positive: "} + std::to_string(inertia));
+
+ // Pure torque, no force.
+ const double torque = 3.0;
+ rb.template get<"torque">().at({}).set(torque);
+
+ (lbm::integrate_particle<sch::T,D>)(p, {}, {1u});
+
+ auto ang_acc = rb.template get<"angular_acceleration">().at({}).get();
+ SAW_EXPECT(std::abs(ang_acc - torque / inertia) < 1e-12,
+ std::string{"angular acceleration should be torque/inertia: "}
+ + std::to_string(ang_acc));
+
+ // One unit step of theta = 2 theta - theta_old + alpha dt^2, from rest.
+ auto rot = rb.template get<"rotation">().at({}).get();
+ SAW_EXPECT(std::abs(rot - ang_acc) < 1e-12,
+ std::string{"rotation after one unit step should equal alpha: "}
+ + std::to_string(rot));
+}
+
+SAW_TEST("Rotation / spins up under sustained torque"){
+ using namespace kel;
+
+ auto g = make_group<1u>(2.0, 1.0);
+ auto& p = g.template get<"particles">().at({0u});
+ auto& rb = p.template get<"rigid_body">();
+
+ auto inertia = rb.template get<"inertia">().at({}).get();
+ const double torque = 1.0;
+ const double alpha = torque / inertia;
+
+ // Hold the torque for 10 unit steps; position-Verlet from rest should
+ // track theta = alpha t^2 / 2.
+ for(uint64_t step = 0u; step < 10u; ++step){
+ rb.template get<"torque">().at({}).set(torque);
+ (lbm::integrate_particle<sch::T,D>)(p, {}, {1u});
+ }
+
+ auto rot = rb.template get<"rotation">().at({}).get();
+ auto expect = 0.5 * alpha * 10.0 * 10.0;
+
+ // Verlet from rest lags the closed form by about half a step of drift.
+ SAW_EXPECT(rot > 0.0, "must have spun up");
+ SAW_EXPECT(std::abs(rot - expect) / expect < 0.15,
+ std::string{"expected about "} + std::to_string(expect)
+ + " got " + std::to_string(rot));
+}
+
+SAW_TEST("Rotation / no torque means no spin"){
+ using namespace kel;
+
+ auto g = make_group<1u>(2.0, 1.0);
+ auto& p = g.template get<"particles">().at({0u});
+ auto& rb = p.template get<"rigid_body">();
+
+ // Linear force only.
+ rb.template get<"force">().at({{0u}}).set(5.0);
+ (lbm::reset_particle_accumulators<sch::T,D>)(p);
+ rb.template get<"force">().at({{0u}}).set(5.0);
+
+ (lbm::integrate_particle<sch::T,D>)(p, {}, {4u});
+
+ SAW_EXPECT(rb.template get<"rotation">().at({}).get() == 0.0,
+ "a pure force must not induce spin");
+ SAW_EXPECT(rb.template get<"position">().at({{0u}}).get() > 0.0,
+ "but it must move the body");
+}
+
+SAW_TEST("Rotation / tangential contact produces torque"){
+ using namespace kel;
+
+ /**
+ * A glancing contact with friction must spin the bodies, and the two
+ * halves must be consistent: equal and opposite forces, with each torque
+ * taken about its own centre.
+ */
+ auto g = make_group<2u>(2.0, 1.0);
+ auto& parts = g.template get<"particles">();
+
+ auto place = [&](uint64_t i, double x, double y, double ox, double oy){
+ auto& rb = parts.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);
+ };
+
+ // Overlapping, sliding past each other along y.
+ place(0u, 0.0, 0.0, 0.0, 0.2);
+ place(1u, 3.0, 0.0, 0.0, -0.2);
+
+ lbm::dem_params<sch::T> dem{10.0, 0.2, 5.0, 0.8};
+
+ for(uint64_t i = 0u; i < 2u; ++i){
+ (lbm::reset_particle_accumulators<sch::T,D>)(parts.at({i}));
+ }
+ for(uint64_t i = 0u; i < 2u; ++i){
+ (lbm::accumulate_particle_contacts<sch::T,D>)(g.template get<"particles">(), i, dem, {1u});
+ }
+
+ auto t0 = parts.at({0u}).template get<"rigid_body">().template get<"torque">().at({}).get();
+ auto t1 = parts.at({1u}).template get<"rigid_body">().template get<"torque">().at({}).get();
+
+ SAW_EXPECT(std::abs(t0) > 0.0, "sliding contact must produce torque");
+
+ // Equal and opposite forces about mirrored lever arms means same-sign
+ // torque: both bodies are spun the same way by the shear between them.
+ SAW_EXPECT(t0 * t1 > 0.0,
+ std::string{"both bodies should spin the same way: "}
+ + std::to_string(t0) + " and " + std::to_string(t1));
+
+ // Linear halves still 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, "linear momentum must still be conserved exactly");
+ }
+}
+
+SAW_TEST("Rotation / mask actually turns with the body"){
+ using namespace kel;
+
+ /**
+ * mask_local_to_global applies Rot(theta). Until torque existed, theta
+ * never changed, so this had no observable effect.
+ */
+ auto g = make_group<1u>(3.0, 1.0);
+ auto& p = g.template get<"particles">().at({0u});
+ auto& rb = p.template get<"rigid_body">();
+ rb.template get<"position">().at({{0u}}).set(20.0);
+ rb.template get<"position">().at({{1u}}).set(20.0);
+
+ auto& com = g.template get<"center_of_mass">().at({0u});
+
+ // A point on the local +x axis.
+ kel::lbm::data<sch::Vector<sch::T,D>> u;
+ u.at({{0u}}).set(1.0);
+ u.at({{1u}}).set(0.0);
+
+ rb.template get<"rotation">().at({}).set(0.0);
+ auto x0 = (lbm::mask_local_to_global<sch::T,D>)(u,p,com);
+
+ // Quarter turn: +x should swing to +y.
+ rb.template get<"rotation">().at({}).set(3.14159265358979323846 / 2.0);
+ auto x90 = (lbm::mask_local_to_global<sch::T,D>)(u,p,com);
+
+ SAW_EXPECT(std::abs(x0.at({{0u}}).get() - 23.0) < 0.05,
+ std::string{"unrotated should sit at +radius in x: "}
+ + std::to_string(x0.at({{0u}}).get()));
+ SAW_EXPECT(std::abs(x90.at({{1u}}).get() - 23.0) < 0.05,
+ std::string{"quarter turn should sit at +radius in y: "}
+ + std::to_string(x90.at({{1u}}).get()));
+ SAW_EXPECT(std::abs(x90.at({{0u}}).get() - 20.0) < 0.05,
+ "and back on the centre line in x");
+}
+}
diff --git a/modules/core/tests/voxel_narrow.cpp b/modules/core/tests/voxel_narrow.cpp
new file mode 100644
index 0000000..5cffc76
--- /dev/null
+++ b/modules/core/tests/voxel_narrow.cpp
@@ -0,0 +1,191 @@
+#include <forstio/test/suite.hpp>
+
+#include <cmath>
+#include <iostream>
+
+#include "../c++/particle/particle.hpp"
+#include "../c++/particle/voxel_narrow.hpp"
+
+namespace {
+namespace sch {
+using namespace kel::lbm::sch;
+using T = Float64;
+}
+
+constexpr uint64_t D = 2u;
+
+auto make_pair(double radius, uint64_t res = 32u){
+ kel::lbm::data<sch::Scalar<sch::T>> r,d;
+ r.at({}).set(radius);
+ d.at({}).set(1.0);
+ return kel::lbm::create_spheroid_particle_group<sch::T,D,2u>(r,d,{res});
+}
+
+template<typename G>
+void place(G& g, uint64_t i, double x, double y){
+ auto& pos = g.template get<"particles">().at({i})
+ .template get<"rigid_body">().template get<"position">();
+ pos.at({{0u}}).set(x);
+ pos.at({{1u}}).set(y);
+}
+
+SAW_TEST("VoxelNarrow / depth matches the analytic oracle"){
+ using namespace kel;
+
+ /**
+ * For spheres the answer is known exactly: delta = r_lo + r_hi - |d|.
+ * The voxel result should land within about a voxel pitch of it. This is
+ * the check that the overlap integral is measuring what it claims to.
+ */
+ const double radius = 6.0;
+ auto g = make_pair(radius);
+ auto& parts = g.template get<"particles">();
+
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{64u,64u}};
+ kel::lbm::data<sch::Scalar<sch::T>> eps;
+ eps.at({}).set(1.0);
+
+ for(int step = 1; step <= 4; ++step){
+ double sep = 2.0 * radius - step * 1.5;
+ place(g, 0u, 32.0 - sep/2.0, 32.0);
+ place(g, 1u, 32.0 + sep/2.0, 32.0);
+
+ kel::lbm::data<sch::ContactManifold<sch::T,D>> m;
+ bool hit = (lbm::voxel_contact<sch::T,D>)(g,parts.at({0u}),parts.at({1u}),extent,eps,m);
+ SAW_EXPECT(hit, "expected an overlap");
+
+ double expect = 2.0 * radius - sep;
+ double got = m.template get<"depth">().at({}).get();
+
+ SAW_EXPECT(std::abs(got - expect) < 1.6,
+ std::string{"sep="} + std::to_string(sep) + " expected depth "
+ + std::to_string(expect) + " got " + std::to_string(got));
+ }
+}
+
+SAW_TEST("VoxelNarrow / normal points hi to lo"){
+ using namespace kel;
+
+ const double radius = 6.0;
+ auto g = make_pair(radius);
+ auto& parts = g.template get<"particles">();
+
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{64u,64u}};
+ kel::lbm::data<sch::Scalar<sch::T>> eps;
+ eps.at({}).set(1.0);
+
+ place(g, 0u, 28.0, 32.0);
+ place(g, 1u, 36.0, 32.0);
+
+ kel::lbm::data<sch::ContactManifold<sch::T,D>> m;
+ bool hit = (lbm::voxel_contact<sch::T,D>)(g,parts.at({0u}),parts.at({1u}),extent,eps,m);
+ SAW_EXPECT(hit, "expected an overlap");
+
+ auto& n = m.template get<"normal">();
+ SAW_EXPECT(n.at({{0u}}).get() < -0.9,
+ std::string{"normal x should be about -1: "} + std::to_string(n.at({{0u}}).get()));
+ SAW_EXPECT(std::abs(n.at({{1u}}).get()) < 0.1,
+ std::string{"normal y should be about 0: "} + std::to_string(n.at({{1u}}).get()));
+
+ // Contact point sits between the two centres.
+ auto px = m.template get<"point">().at({{0u}}).get();
+ SAW_EXPECT(px > 28.0 && px < 36.0,
+ std::string{"contact point x out of range: "} + std::to_string(px));
+}
+
+SAW_TEST("VoxelNarrow / exactly antisymmetric under pair swap"){
+ using namespace kel;
+
+ /**
+ * The reason the lattice is swept rather than either particle's local
+ * grid: both owners must visit identical points in identical order, so
+ * the two halves of the pair cancel bitwise. Sampling on a particle-local
+ * grid would make the sample set depend on which particle you started
+ * from, and every contact would leak a little momentum.
+ */
+ const double radius = 5.0;
+ auto g = make_pair(radius);
+ auto& parts = g.template get<"particles">();
+
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{64u,64u}};
+ kel::lbm::data<sch::Scalar<sch::T>> eps;
+ eps.at({}).set(1.0);
+
+ // Off-axis, so a symmetry accident cannot hide a real asymmetry.
+ place(g, 0u, 29.0, 30.0);
+ place(g, 1u, 36.0, 33.5);
+
+ kel::lbm::data<sch::ContactManifold<sch::T,D>> m01, m10;
+ bool a = (lbm::voxel_contact<sch::T,D>)(g,parts.at({0u}),parts.at({1u}),extent,eps,m01);
+ bool b = (lbm::voxel_contact<sch::T,D>)(g,parts.at({1u}),parts.at({0u}),extent,eps,m10);
+
+ SAW_EXPECT(a && b, "both orderings must find the contact");
+
+ SAW_EXPECT(m01.template get<"depth">().at({}).get() == m10.template get<"depth">().at({}).get(),
+ "depth must be identical under swap");
+
+ for(uint64_t i = 0u; i < D; ++i){
+ SAW_EXPECT(m01.template get<"normal">().at({{i}}).get()
+ == -m10.template get<"normal">().at({{i}}).get(),
+ std::string{"normal component "} + std::to_string(i) + " not exactly opposite");
+ SAW_EXPECT(m01.template get<"point">().at({{i}}).get()
+ == m10.template get<"point">().at({{i}}).get(),
+ std::string{"contact point component "} + std::to_string(i) + " must match");
+ }
+}
+
+SAW_TEST("VoxelNarrow / separated bodies report nothing"){
+ using namespace kel;
+
+ auto g = make_pair(4.0);
+ auto& parts = g.template get<"particles">();
+
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{64u,64u}};
+ kel::lbm::data<sch::Scalar<sch::T>> eps;
+ eps.at({}).set(1.0);
+
+ place(g, 0u, 16.0, 32.0);
+ place(g, 1u, 48.0, 32.0);
+
+ kel::lbm::data<sch::ContactManifold<sch::T,D>> m;
+ SAW_EXPECT(!(lbm::voxel_contact<sch::T,D>)(g,parts.at({0u}),parts.at({1u}),extent,eps,m),
+ "well separated bodies must not report a contact");
+}
+
+SAW_TEST("VoxelNarrow / agrees with the analytic narrow phase"){
+ using namespace kel;
+
+ /**
+ * Same bodies, two independent routes to a manifold. They should agree on
+ * direction closely and on depth to within the voxel pitch; if they do
+ * not, one of the two is wrong about the geometry.
+ */
+ const double radius = 6.0;
+ auto g = make_pair(radius);
+ auto& parts = g.template get<"particles">();
+
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> extent{{64u,64u}};
+ kel::lbm::data<sch::Scalar<sch::T>> eps;
+ eps.at({}).set(1.0);
+
+ place(g, 0u, 29.0, 31.0);
+ place(g, 1u, 36.0, 33.0);
+
+ kel::lbm::data<sch::ContactManifold<sch::T,D>> mv, ma;
+ bool hv = (lbm::voxel_contact<sch::T,D>)(g,parts.at({0u}),parts.at({1u}),extent,eps,mv);
+ bool ha = (lbm::narrow_phase<lbm::coll::Spheroid<sch::T>>::template contact<D>)(
+ parts.at({0u}), parts.at({1u}), ma);
+
+ SAW_EXPECT(hv && ha, "both narrow phases must find the contact");
+
+ auto dot = mv.template get<"normal">().at({{0u}}).get() * ma.template get<"normal">().at({{0u}}).get()
+ + mv.template get<"normal">().at({{1u}}).get() * ma.template get<"normal">().at({{1u}}).get();
+ SAW_EXPECT(dot > 0.97,
+ std::string{"normals should nearly agree, dot = "} + std::to_string(dot));
+
+ auto diff = std::abs(mv.template get<"depth">().at({}).get()
+ - ma.template get<"depth">().at({}).get());
+ SAW_EXPECT(diff < 1.6,
+ std::string{"depths disagree by "} + std::to_string(diff));
+}
+}