From 0c0087ef9482634273ec1a23d3a915754f94926d Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Wed, 11 Nov 2026 01:16:44 +0100 Subject: Snapshot. Migration to Lbm Unit --- modules/core/tests/cell_list.cpp | 3 +- modules/core/tests/component_instantiation.cpp | 6 +- modules/core/tests/contact_dynamics.cpp | 5 +- modules/core/tests/descriptor.cpp | 84 ++++++ modules/core/tests/macroscopic.cpp | 113 ++++++++ modules/core/tests/mask.cpp | 6 +- modules/core/tests/particle_3d.cpp | 3 +- modules/core/tests/particle_consistency.cpp | 3 +- modules/core/tests/particle_edges.cpp | 3 +- modules/core/tests/particle_gaps.cpp | 3 +- modules/core/tests/particles.cpp | 24 +- modules/core/tests/rotation.cpp | 3 +- modules/core/tests/rotation_3d.cpp | 254 ++++++++++++++++++ modules/core/tests/unit.cpp | 343 +++++++++++++++++++++++++ modules/core/tests/voxel_narrow.cpp | 3 +- modules/core/tests/vtk_write.cpp | 6 +- 16 files changed, 838 insertions(+), 24 deletions(-) create mode 100644 modules/core/tests/macroscopic.cpp create mode 100644 modules/core/tests/rotation_3d.cpp create mode 100644 modules/core/tests/unit.cpp (limited to 'modules/core/tests') diff --git a/modules/core/tests/cell_list.cpp b/modules/core/tests/cell_list.cpp index 00b260c..f04e065 100644 --- a/modules/core/tests/cell_list.cpp +++ b/modules/core/tests/cell_list.cpp @@ -17,7 +17,8 @@ constexpr uint64_t Cap = 8u; template auto make_group(double radius){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(1.0); return kel::lbm::create_spheroid_particle_group(r,d,{4u}); diff --git a/modules/core/tests/component_instantiation.cpp b/modules/core/tests/component_instantiation.cpp index 94d0536..e4a890b 100644 --- a/modules/core/tests/component_instantiation.cpp +++ b/modules/core/tests/component_instantiation.cpp @@ -33,7 +33,8 @@ using GroupSch = kel::lbm::sch::ParticleGroup, kel::lbm::enc::Native>; auto make_group(double radius){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(1.0); return kel::lbm::create_spheroid_particle_group(r,d,{8u}); @@ -102,7 +103,8 @@ SAW_TEST("Component / ParticleIntegrate instantiates and moves the body"){ auto mass = rb.template get<"mass">().at({}).get(); rb.template get<"force">().at({{0u}}).set(mass); // unit acceleration - kel::lbm::data> gravity; + // Gravity is an acceleration, and now has to say so. + kel::lbm::data>> gravity; gravity.at({{1u}}).set(-0.5); lbm::component integrate{gravity}; diff --git a/modules/core/tests/contact_dynamics.cpp b/modules/core/tests/contact_dynamics.cpp index b6545b9..b918bc1 100644 --- a/modules/core/tests/contact_dynamics.cpp +++ b/modules/core/tests/contact_dynamics.cpp @@ -17,7 +17,8 @@ constexpr uint64_t D = 2u; template auto make_group(double radius, double density){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(density); return kel::lbm::create_spheroid_particle_group(r,d,{8u}); @@ -138,7 +139,7 @@ SAW_TEST("Dynamics / resting contact settles at the force balance"){ kel::lbm::data> extent{{64u,32u}}; // Gravity pointing at the wall, as a body acceleration. - kel::lbm::data> body_acc; + kel::lbm::data>> body_acc; body_acc.at({{0u}}).set(-gravity); body_acc.at({{1u}}).set(0.0); diff --git a/modules/core/tests/descriptor.cpp b/modules/core/tests/descriptor.cpp index 7f743ce..96d6531 100644 --- a/modules/core/tests/descriptor.cpp +++ b/modules/core/tests/descriptor.cpp @@ -2,8 +2,63 @@ #include "../c++/descriptor.hpp" +#include + namespace { +/** + * The lattice moment conditions: + * + * sum_i w_i = 1 + * sum_i w_i e_ia = 0 + * sum_i w_i e_ia e_ib = cs^2 * delta_ab + * + * A weight table that violates these still compiles and still passes an + * opposite-index check, but it makes the equilibrium return the wrong mass and + * momentum, and the only symptom is a simulation that diverges within a few + * steps. D3Q27 carried exactly that bug: its six face weights read 1/36 rather + * than 2/27, so the weights summed to 13/18. + * + * The second moment is the useful one -- it pins the weights per shell, where + * the plain sum only pins them in aggregate. + */ +template +void check_weight_moments(){ + using namespace kel; + + using dfi = lbm::df_info; + + constexpr double tol = 1e-12; + + double sum_w = 0.0; + for(uint64_t k = 0u; k < Descriptor::Q; ++k){ + sum_w += dfi::weights[k]; + } + SAW_EXPECT(std::abs(sum_w - 1.0) < tol, "Weights have to sum to 1"); + + for(uint64_t a = 0u; a < Descriptor::D; ++a){ + double first = 0.0; + for(uint64_t k = 0u; k < Descriptor::Q; ++k){ + first += dfi::weights[k] * static_cast(dfi::directions[k][a]); + } + SAW_EXPECT(std::abs(first) < tol, "First moment has to vanish"); + } + + for(uint64_t a = 0u; a < Descriptor::D; ++a){ + for(uint64_t b = 0u; b < Descriptor::D; ++b){ + double second = 0.0; + for(uint64_t k = 0u; k < Descriptor::Q; ++k){ + second += + dfi::weights[k] + * static_cast(dfi::directions[k][a]) + * static_cast(dfi::directions[k][b]); + } + const double expected = (a == b) ? static_cast(dfi::cs2) : 0.0; + SAW_EXPECT(std::abs(second - expected) < tol, "Second moment has to be cs^2 delta_ab"); + } + } +} + template void check_opposite_dirs(){ using namespace kel; @@ -32,8 +87,37 @@ SAW_TEST("Opposites and Dirs D2Q9"){ using namespace kel; check_opposite_dirs>(); } +SAW_TEST("Opposites and Dirs D3Q19"){ + using namespace kel; + check_opposite_dirs>(); +} SAW_TEST("Opposites and Dirs D3Q27"){ using namespace kel; check_opposite_dirs>(); } + +/** + * D2Q4 is deliberately absent: it is experimental and its weights sum to 5/6, + * so it would fail these outright. + */ +SAW_TEST("Weight moments D1Q3"){ + using namespace kel; + check_weight_moments>(); +} +SAW_TEST("Weight moments D2Q5"){ + using namespace kel; + check_weight_moments>(); +} +SAW_TEST("Weight moments D2Q9"){ + using namespace kel; + check_weight_moments>(); +} +SAW_TEST("Weight moments D3Q19"){ + using namespace kel; + check_weight_moments>(); +} +SAW_TEST("Weight moments D3Q27"){ + using namespace kel; + check_weight_moments>(); +} } diff --git a/modules/core/tests/macroscopic.cpp b/modules/core/tests/macroscopic.cpp new file mode 100644 index 0000000..9184819 --- /dev/null +++ b/modules/core/tests/macroscopic.cpp @@ -0,0 +1,113 @@ +#include + +#include + +#include "../c++/macroscopic.hpp" +#include "../c++/equilibrium.hpp" + +namespace { +namespace sch { +using namespace kel::lbm::sch; +using T = Float64; +} + +/** + * The macro field used to be called "momentum" while holding velocity: + * compute_rho_u divides the first moment by the density (macroscopic.hpp). + * These pin that the name now matches the content, so the rename cannot be + * quietly undone by removing the division. + */ + +template +void roundtrip(double rho_in, double ux_in){ + using namespace kel; + + lbm::data> rho; + rho.at({}).set(rho_in); + + lbm::data> vel; + for(uint64_t i = 0u; i < Desc::D; ++i){ + vel.at({{i}}).set(0.0); + } + vel.at({{0u}}).set(ux_in); + + auto dfs = lbm::equilibrium(rho,vel); + + lbm::data> rho_out; + lbm::data> vel_out; + lbm::compute_rho_u(dfs,rho_out,vel_out); + + SAW_EXPECT(std::abs(rho_out.at({}).get() - rho_in) < 1e-12, + "density must round trip through equilibrium"); + SAW_EXPECT(std::abs(vel_out.at({{0u}}).get() - ux_in) < 1e-12, + "velocity must round trip through equilibrium"); +} + +SAW_TEST("Macroscopic / the field holds velocity, not momentum"){ + using namespace kel; + using Desc = sch::D2Q9; + + // Density deliberately not 1, which is the only case where velocity and + // momentum differ numerically. + const double rho_in = 2.0; + const double ux_in = 0.1; + + lbm::data> rho; + rho.at({}).set(rho_in); + lbm::data> vel; + vel.at({{0u}}).set(ux_in); + vel.at({{1u}}).set(0.0); + + auto dfs = lbm::equilibrium(rho,vel); + + lbm::data> rho_out; + lbm::data> vel_out; + lbm::compute_rho_u(dfs,rho_out,vel_out); + + const double got = vel_out.at({{0u}}).get(); + + SAW_EXPECT(std::abs(got - ux_in) < 1e-12, "must be the velocity u"); + // rho*u would be 0.2 here. If the division by rho were dropped, this is + // the assertion that would catch it. + SAW_EXPECT(std::abs(got - rho_in * ux_in) > 1e-3, + "must NOT be the momentum rho*u"); +} + +SAW_TEST("Macroscopic / round trip D2Q9"){ + roundtrip(1.0, 0.05); + roundtrip(2.0, 0.05); +} + +SAW_TEST("Macroscopic / round trip D2Q5"){ + roundtrip>(1.0, 0.0); +} + +SAW_TEST("Macroscopic / round trip D3Q27"){ + // The 3D path divides by rho too -- it is the same loop. + roundtrip(1.0, 0.05); + roundtrip(1.7, 0.03); +} + +SAW_TEST("Macroscopic / a quiescent field has zero velocity at any density"){ + using namespace kel; + using Desc = sch::D2Q9; + + for(double r : {0.5, 1.0, 3.0}){ + lbm::data> rho; + rho.at({}).set(r); + lbm::data> vel; + vel.at({{0u}}).set(0.0); + vel.at({{1u}}).set(0.0); + + auto dfs = lbm::equilibrium(rho,vel); + + lbm::data> rho_out; + lbm::data> vel_out; + lbm::compute_rho_u(dfs,rho_out,vel_out); + + SAW_EXPECT(std::abs(rho_out.at({}).get() - r) < 1e-12, "density preserved"); + SAW_EXPECT(std::abs(vel_out.at({{0u}}).get()) < 1e-12, "no x velocity at rest"); + SAW_EXPECT(std::abs(vel_out.at({{1u}}).get()) < 1e-12, "no y velocity at rest"); + } +} +} diff --git a/modules/core/tests/mask.cpp b/modules/core/tests/mask.cpp index dfb5ef8..f67cc78 100644 --- a/modules/core/tests/mask.cpp +++ b/modules/core/tests/mask.cpp @@ -15,7 +15,8 @@ using T = Float64; constexpr uint64_t D = 2u; auto make_one(double radius){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(1.0); return kel::lbm::create_spheroid_particle_group(r,d,{32u}); @@ -209,7 +210,8 @@ SAW_TEST("Mask / porosity agrees with the analytic sphere"){ kel::lbm::data> eps; eps.at({}).set(1.5); - auto rad = p.template get<"collision">().template get<"radius">(); + // The analytic porosity works on raw lengths, as the solvers do. + auto rad = p.template get<"collision">().template get<"radius">().handle(); double worst = 0.0; for(int i = 0; i <= 40; ++i){ diff --git a/modules/core/tests/particle_3d.cpp b/modules/core/tests/particle_3d.cpp index 8c3e579..a169f10 100644 --- a/modules/core/tests/particle_3d.cpp +++ b/modules/core/tests/particle_3d.cpp @@ -26,7 +26,8 @@ constexpr uint64_t D = 3u; template auto make_group(double radius, double density, uint64_t res = 12u){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(density); return kel::lbm::create_spheroid_particle_group(r,d,{res}); diff --git a/modules/core/tests/particle_consistency.cpp b/modules/core/tests/particle_consistency.cpp index ae40aa8..7e84ab5 100644 --- a/modules/core/tests/particle_consistency.cpp +++ b/modules/core/tests/particle_consistency.cpp @@ -19,7 +19,8 @@ constexpr uint64_t Cap = 16u; template auto make_group(double radius, uint64_t res = 24u){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(1.0); return kel::lbm::create_spheroid_particle_group(r,d,{res}); diff --git a/modules/core/tests/particle_edges.cpp b/modules/core/tests/particle_edges.cpp index cbc1bab..e39817b 100644 --- a/modules/core/tests/particle_edges.cpp +++ b/modules/core/tests/particle_edges.cpp @@ -17,7 +17,8 @@ constexpr uint64_t D = 2u; template auto make_group(double radius){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(1.0); return kel::lbm::create_spheroid_particle_group(r,d,{8u}); diff --git a/modules/core/tests/particle_gaps.cpp b/modules/core/tests/particle_gaps.cpp index 3db831e..f7213a6 100644 --- a/modules/core/tests/particle_gaps.cpp +++ b/modules/core/tests/particle_gaps.cpp @@ -18,7 +18,8 @@ constexpr uint64_t D = 2u; template auto make_group(double radius, uint64_t res = 32u){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(1.0); return kel::lbm::create_spheroid_particle_group(r,d,{res}); diff --git a/modules/core/tests/particles.cpp b/modules/core/tests/particles.cpp index 2105abc..1bc5d46 100644 --- a/modules/core/tests/particles.cpp +++ b/modules/core/tests/particles.cpp @@ -26,7 +26,8 @@ SAW_TEST("Verlet step 2D - Planar"){ acc.at({{0}}).set({1.0}); - kel::lbm::data> dt; + // A timestep is a lattice time. + kel::lbm::data>> dt; dt.at({}).set(0.5); lbm::verlet_step_lambda(particle,dt); @@ -131,7 +132,8 @@ SAW_TEST("Moving particles 2D"){ acc.at({{0u}}) = -0.1; } - kel::lbm::data> dt; + // A timestep is a lattice time. + kel::lbm::data>> dt; dt.at({}).set(0.5); bool has_collided = false; for(uint64_t i = 0u; i < 32u; ++i){ @@ -284,7 +286,8 @@ SAW_TEST("Spheroid Particle / AABB"){ constexpr uint64_t D = 2u; constexpr uint64_t PC = 1u; - kel::lbm::data> radi,dense; + kel::lbm::data>> radi; + kel::lbm::data> dense; radi.at({}) = 2.2f; dense.at({}) = 1.0f; auto spheroid_pgrp = lbm::create_spheroid_particle_group(radi,dense,{8u}); @@ -331,7 +334,8 @@ SAW_TEST("Spheroid Particle / AABB per particle"){ constexpr uint64_t D = 2u; constexpr uint64_t PC = 3u; - kel::lbm::data> radi,dense; + kel::lbm::data>> radi; + kel::lbm::data> dense; radi.at({}) = 2.2f; dense.at({}) = 1.0f; auto pgrp = lbm::create_spheroid_particle_group(radi,dense,{8u}); @@ -390,7 +394,8 @@ SAW_TEST("Spheroid Particle / per-particle inertial fan-out"){ constexpr uint64_t D = 2u; constexpr uint64_t PC = 4u; - kel::lbm::data> radi,dense; + kel::lbm::data>> radi; + kel::lbm::data> dense; radi.at({}) = 2.0; dense.at({}) = 3.0; auto pgrp = lbm::create_spheroid_particle_group(radi,dense,{8u}); @@ -466,7 +471,8 @@ SAW_TEST("Particle / force accumulates then integrates"){ - kel::lbm::data> radi,dense; + kel::lbm::data>> radi; + kel::lbm::data> dense; radi.at({}) = 1.0; dense.at({}) = 1.0 / 3.14159265358979323846; // gives mass == 1 auto pgrp = lbm::create_spheroid_particle_group(radi,dense,{4u}); @@ -502,7 +508,8 @@ SAW_TEST("Particle / zero sub steps still advances"){ - kel::lbm::data> radi,dense; + kel::lbm::data>> radi; + kel::lbm::data> dense; radi.at({}) = 1.0; dense.at({}) = 1.0 / 3.14159265358979323846; auto pgrp = lbm::create_spheroid_particle_group(radi,dense,{4u}); @@ -530,7 +537,8 @@ constexpr uint64_t D = 2u; template auto make_group(double radius, double density){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(density); return kel::lbm::create_spheroid_particle_group(r,d,{4u}); diff --git a/modules/core/tests/rotation.cpp b/modules/core/tests/rotation.cpp index e1e8c46..a4bab33 100644 --- a/modules/core/tests/rotation.cpp +++ b/modules/core/tests/rotation.cpp @@ -18,7 +18,8 @@ constexpr uint64_t D = 2u; template auto make_group(double radius, double density){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(density); return kel::lbm::create_spheroid_particle_group(r,d,{16u}); diff --git a/modules/core/tests/rotation_3d.cpp b/modules/core/tests/rotation_3d.cpp new file mode 100644 index 0000000..4bfb067 --- /dev/null +++ b/modules/core/tests/rotation_3d.cpp @@ -0,0 +1,254 @@ +#include + +#include + +#include "../c++/particle/particle.hpp" +#include "../c++/particle/integrate.hpp" + +namespace { +namespace sch { +using namespace kel::lbm::sch; +using T = Float64; +} + +constexpr uint64_t D = 3u; + +/** + * The fixed-axis rotor: a body pinned in place, free to spin about one axis. + * + * particle_angular_supported<3> is false and stays false -- general 3D spin + * needs a quaternion and an inertia tensor. These cover the constrained case + * instead, which is exact rather than approximate because the axis never + * moves. See particle_fixed_axis_supported for the argument. + */ + +auto make_rotor(double radius = 2.0, double density = 1.0){ + kel::lbm::data>> r; + kel::lbm::data> d; + r.at({}).set(radius); + d.at({}).set(density); + return kel::lbm::create_spheroid_particle_group(r,d,{8u}); +} + +template +auto& body(G& g){ + return g.template get<"particles">().at({0u}).template get<"rigid_body">(); +} + +template +void place_at_rest(G& g, double x, double y, double z){ + auto& rb = body(g); + auto& pos = rb.template get<"position">(); + auto& old = rb.template get<"position_old">(); + const double v[3] = {x,y,z}; + for(uint64_t i = 0u; i < 3u; ++i){ + pos.at({{i}}).set(v[i]); + // position_old == position is what makes the pin exact. + old.at({{i}}).set(v[i]); + } + auto& rot = rb.template get<"rotation">(); + auto& rot_old = rb.template get<"rotation_old">(); + auto& ang = rb.template get<"angular_acceleration">(); + for(uint64_t i = 0u; i < 3u; ++i){ + rot.at({{i}}).set(0); + rot_old.at({{i}}).set(0); + ang.at({{i}}).set(0); + } +} + +kel::lbm::data> vec3(double x, double y, double z){ + kel::lbm::data> v; + v.at({{0u}}).set(x); + v.at({{1u}}).set(y); + v.at({{2u}}).set(z); + return v; +} + +template +void set_torque(G& g, double x, double y, double z){ + auto& t = body(g).template get<"torque">(); + t.at({{0u}}).set(x); + t.at({{1u}}).set(y); + t.at({{2u}}).set(z); +} + +SAW_TEST("Rotor 3D / fixed axis is supported where general spin is not"){ + using namespace kel; + static_assert(not lbm::particle_angular_supported<3u>, + "General 3D spin must stay gated off"); + static_assert(lbm::particle_fixed_axis_supported<3u>, + "Fixed axis spin must be available in 3D"); + static_assert(lbm::particle_fixed_axis_supported<2u>, + "Fixed axis spin must also cover 2D"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Rotor 3D / position is pinned bitwise under load"){ + using namespace kel; + + auto g = make_rotor(); + place_at_rest(g, 7.0, 11.0, 13.0); + + auto& rb = body(g); + // A force far larger than anything the fluid would produce. + auto& f = rb.template get<"force">(); + f.at({{0u}}).set(1.0e6); + f.at({{1u}}).set(-5.0e5); + f.at({{2u}}).set(2.5e5); + + set_torque(g, 0.0, 3.0, 0.0); + + auto axis = vec3(0.0,1.0,0.0); + auto& pi = g.template get<"particles">().at({0u}); + + for(uint64_t s = 0u; s < 64u; ++s){ + lbm::integrate_particle_fixed_axis(pi, axis, {4u}); + } + + auto& pos = rb.template get<"position">(); + // Exact equality, not a tolerance: 2x - x_old + 0 reproduces x bitwise. + SAW_EXPECT(pos.at({{0u}}).get() == 7.0, "x must not drift"); + SAW_EXPECT(pos.at({{1u}}).get() == 11.0, "y must not drift"); + SAW_EXPECT(pos.at({{2u}}).get() == 13.0, "z must not drift"); +} + +SAW_TEST("Rotor 3D / only the on-axis torque component spins it"){ + using namespace kel; + + auto g = make_rotor(); + place_at_rest(g, 0.0, 0.0, 0.0); + + // Mostly off-axis: only the y component should drive a y-axis rotor. + set_torque(g, 9.0, 2.0, -4.0); + + auto axis = vec3(0.0,1.0,0.0); + auto& pi = g.template get<"particles">().at({0u}); + lbm::integrate_particle_fixed_axis(pi, axis, {1u}); + + auto& ang = body(g).template get<"angular_acceleration">(); + const double inertia = body(g).template get<"inertia">().at({}).get(); + + SAW_EXPECT(std::abs(ang.at({{0u}}).get()) < 1e-12, "No spin off the axis in x"); + SAW_EXPECT(std::abs(ang.at({{2u}}).get()) < 1e-12, "No spin off the axis in z"); + SAW_EXPECT(std::abs(ang.at({{1u}}).get() - (2.0 / inertia)) < 1e-12, + "On-axis component must give tau/I"); +} + +SAW_TEST("Rotor 3D / rotation stays parallel to the axis"){ + using namespace kel; + + auto g = make_rotor(); + place_at_rest(g, 0.0, 0.0, 0.0); + + // A deliberately tilted, non-unit axis: the component must normalise it. + auto axis = vec3(1.0, 2.0, -2.0); + auto& pi = g.template get<"particles">().at({0u}); + + for(uint64_t s = 0u; s < 128u; ++s){ + set_torque(g, 0.5, 1.0, 0.25); + lbm::integrate_particle_fixed_axis(pi, axis, {2u}); + } + + auto& rot = body(g).template get<"rotation">(); + + // |rot| must equal |rot . axis_hat|: no component ever grows off-axis. + const double n = std::sqrt(1.0 + 4.0 + 4.0); + const double ax[3] = {1.0/n, 2.0/n, -2.0/n}; + + double along = 0.0; + double len2 = 0.0; + for(uint64_t i = 0u; i < 3u; ++i){ + along += rot.at({{i}}).get() * ax[i]; + len2 += rot.at({{i}}).get() * rot.at({{i}}).get(); + } + const double len = std::sqrt(len2); + + SAW_EXPECT(len > 1e-9, "The rotor should actually have turned"); + SAW_EXPECT(std::abs(len - std::abs(along)) < 1e-9, + "Rotation must stay parallel to the spin axis"); +} + +SAW_TEST("Rotor 3D / constant torque spins up linearly"){ + using namespace kel; + + auto g = make_rotor(); + place_at_rest(g, 0.0, 0.0, 0.0); + + auto axis = vec3(0.0,0.0,1.0); + auto& pi = g.template get<"particles">().at({0u}); + auto& rb = body(g); + + const double inertia = rb.template get<"inertia">().at({}).get(); + const double tau = 3.0; + + // One sub step, so a step is a unit of time and omega is rot - rot_old. + auto omega_z = [&](){ + return rb.template get<"rotation">().at({{2u}}).get() + - rb.template get<"rotation_old">().at({{2u}}).get(); + }; + + for(uint64_t s = 0u; s < 8u; ++s){ + set_torque(g, 0.0, 0.0, tau); + lbm::integrate_particle_fixed_axis(pi, axis, {1u}); + } + const double w8 = omega_z(); + + for(uint64_t s = 0u; s < 8u; ++s){ + set_torque(g, 0.0, 0.0, tau); + lbm::integrate_particle_fixed_axis(pi, axis, {1u}); + } + const double w16 = omega_z(); + + SAW_EXPECT(w8 > 0.0, "A positive torque must spin it positively"); + // Constant angular acceleration: omega is linear in step count, so + // doubling the steps doubles omega. + SAW_EXPECT(std::abs(w16 - 2.0 * w8) < 1e-9 * std::abs(w16), + "Angular velocity must grow linearly under constant torque"); + SAW_EXPECT(std::abs(w8 - 8.0 * (tau / inertia)) < 1e-9, + "Angular velocity must match tau/I per step"); +} + +SAW_TEST("Rotor 3D / no torque means no spin"){ + using namespace kel; + + auto g = make_rotor(); + place_at_rest(g, 0.0, 0.0, 0.0); + set_torque(g, 0.0, 0.0, 0.0); + + auto axis = vec3(0.0,1.0,0.0); + auto& pi = g.template get<"particles">().at({0u}); + + for(uint64_t s = 0u; s < 32u; ++s){ + lbm::integrate_particle_fixed_axis(pi, axis, {4u}); + } + + auto& rot = body(g).template get<"rotation">(); + for(uint64_t i = 0u; i < 3u; ++i){ + SAW_EXPECT(rot.at({{i}}).get() == 0.0, "A torque free rotor must not turn"); + } +} + +SAW_TEST("Rotor 3D / a zero axis is inert rather than NaN"){ + using namespace kel; + + auto g = make_rotor(); + place_at_rest(g, 0.0, 0.0, 0.0); + set_torque(g, 1.0, 1.0, 1.0); + + // math::normalize returns a zero vector unchanged, so this must produce a + // body that never spins -- not one that poisons the run with NaN. + auto axis = vec3(0.0,0.0,0.0); + auto& pi = g.template get<"particles">().at({0u}); + + for(uint64_t s = 0u; s < 8u; ++s){ + lbm::integrate_particle_fixed_axis(pi, axis, {2u}); + } + + auto& rot = body(g).template get<"rotation">(); + for(uint64_t i = 0u; i < 3u; ++i){ + const double v = rot.at({{i}}).get(); + SAW_EXPECT(v == v, "Rotation must not be NaN"); + SAW_EXPECT(std::abs(v) < 1e-12, "A zero axis must not spin the body"); + } +} +} diff --git a/modules/core/tests/unit.cpp b/modules/core/tests/unit.cpp new file mode 100644 index 0000000..1c187c0 --- /dev/null +++ b/modules/core/tests/unit.cpp @@ -0,0 +1,343 @@ +#include + +#include +#include + +#include "../c++/abstract/unit.hpp" +#include "../c++/abstract/unit_print.hpp" +#include "../c++/lbm_unit.hpp" +#include "../c++/particle/schema.hpp" + +namespace { +namespace sch { +using namespace kel::lbm::sch; +using T = Float64; +} + +using kel::lbm::si_type::meter; +using kel::lbm::si_type::second; +using lbm_meter = kel::lbm::lbm_type::meter; + +template +using Met = sch::UnitElement; + +template +using Sec = sch::UnitElement; + +template using U = sch::Unit; + +template +using Mul = typename kel::lbm::unit_multiplication::Schema; + +template +using Div = typename kel::lbm::unit_division::Schema; + +/** + * The unit algebra had no direct coverage: converter.cpp asserts one + * conversion and args.cpp exercises unit_division once. Nothing pinned + * multiplication, exponent cancellation, or the scaling comparison -- and the + * scaling path could not even be instantiated, because unit_multiplication, + * unit_division and the three print patterns all matched the two argument + * form, which means scale 0 after the default argument is substituted. + */ + +SAW_TEST("Unit / multiplication folds same base and same scale"){ + static_assert(std::is_same_v>, U>>, U>>, + "m * m must fold to m^2"); + static_assert(std::is_same_v>, U>>, U>>, + "km * km must fold to km^2, keeping the scale"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Unit / different scales do not fold"){ + // Same base quantity, different prefix: the factors must both survive + // rather than silently combining, because nothing rescales the value. + using Mixed = Mul>, U>>; + static_assert(!std::is_same_v>>, "must not fold to m^2"); + static_assert(!std::is_same_v>>, "must not fold to km^2"); + static_assert(std::is_same_v, Met<1,3>>>, + "both factors must survive, in order"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Unit / division negates the exponent but keeps the scale"){ + // (km)^-1 is scale 3 exponent -1. Negating the scale too would quietly + // mean (mm)^-1. + static_assert(std::is_same_v>, U>>, U>>, + "km^2 / km must be km"); + static_assert(std::is_same_v, U>>, U>>, + "1 / km must be km^-1, not mm^-1"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Unit / cancelling exponents drop out entirely"){ + // m/m must be genuinely dimensionless, not m^0. + static_assert(std::is_same_v>, U>>, sch::Pure>, + "m / m must reduce to Pure"); + static_assert(std::is_same_v>, U>>, sch::Pure>, + "km / km must reduce to Pure"); + // But only when the scales agree. + static_assert(!std::is_same_v>, U>>, sch::Pure>, + "km / m must not cancel"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Unit / lattice and SI metres never fold"){ + // This is what actually separates lattice from SI -- distinct base tag + // types, not the scale. + using LbmMet = sch::UnitElement; + using Mixed = Mul>, U>; + static_assert(!std::is_same_v>>, "must not fold"); + // Canonical order, not encounter order: "meter_lbm" sorts before "meter_si". + static_assert(std::is_same_v>>, "both survive, sorted"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Unit / existing aliases are unchanged"){ + // Regression pin: widening the patterns must not have altered any + // composition that lbm_unit.hpp already produced. + static_assert(std::is_same_v, U>>, "SiMeter"); + static_assert(std::is_same_v, U, Sec<-1>>>, + "SiVelocity"); + static_assert( + std::is_same_v, U, Sec<-1>>>, + "SiKinematicViscosity"); + static_assert( + std::is_same_v, sch::SiSecond>, + sch::SiVelocity>, + "m / s must be the velocity alias exactly"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Unit / a scaled unit prints with its prefix"){ + std::stringstream plain, kilo, milli; + + kel::lbm::data>> p{2.0}; + kel::lbm::data>> k{2.0}; + kel::lbm::data>> m{2.0}; + + plain << p; + kilo << k; + milli << m; + + SAW_EXPECT(plain.str() == "2 [m_si]", "unscaled prints bare"); + SAW_EXPECT(kilo.str() == "2 [km_si]", "kilo prints a k prefix"); + SAW_EXPECT(milli.str() == "2 [mm_si]", "milli prints an m prefix"); + // The whole point: a scaled unit must not render identically to an + // unscaled one, or the output lies about the value. + SAW_EXPECT(plain.str() != kilo.str(), "scaled must differ from unscaled"); +} + +SAW_TEST("Unit / an off table scale prints explicitly"){ + std::stringstream o; + kel::lbm::data>> odd{2.0}; + o << odd; + // No SI letter exists for 10^4, so it must say so rather than drop it. + SAW_EXPECT(o.str() == "2 [10^(4)*m_si]", "unknown scale prints as a power"); +} +} + +namespace { +/** + * The particle position is unit typed. These pin the two things that are easy + * to break without noticing. + */ +SAW_TEST("Unit / a united wrapper is layout compatible with its base"){ + // The whole device path depends on this and nothing else checks it: the + // particle group crosses to the GPU through sycl_copy_helper, + // which is a bulk h.copy of the element block and never recurses into the + // element schema. A unit wrapper that changed the size would corrupt every + // particle silently rather than failing to compile. + using Raw = kel::lbm::data>; + using United = kel::lbm::data>>; + + static_assert(sizeof(Raw) == sizeof(United), + "A unit wrapper must not change the layout"); + static_assert(std::is_trivially_copyable_v, + "A unit wrapper must stay memcpy-able"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Unit / particle position carries lattice metres"){ + kel::lbm::data> rb; + + auto &pos = rb.template get<"position">(); + auto &pos_old = rb.template get<"position_old">(); + auto &acc = rb.template get<"acceleration">(); + + static_assert( + std::is_same_v, + kel::lbm::data>>>, + "position must be a lattice length"); + static_assert( + std::is_same_v, + std::remove_cvref_t>, + "position_old must match position"); + static_assert( + std::is_same_v, + kel::lbm::data>>>, + "acceleration must be a lattice acceleration"); + + // Element access still reaches the raw component. + pos.at({{0u}}).set(3.0); + SAW_EXPECT(pos.at({{0u}}).get() == 3.0, "element access must still work"); + + // A difference of two lengths is a length; its dot product is an area. + auto delta = pos - pos_old; + static_assert(std::is_same_v>, + "length minus length is a length"); + + auto area = kel::lbm::math::dot(delta, delta); + static_assert( + std::is_same_v, + sch::UnitElement>>>, + "dot of two lengths must be an area"); + SAW_EXPECT(area.at({}).get() == 9.0, "and the value must be right"); +} +} + +namespace { +/** + * Commutativity at the type level. + * + * m * s and s * m are the same physical unit. If the reduction preserves + * encounter order rather than imposing a canonical one, they become distinct + * C++ types and the same quantity reached two different ways stops matching. + */ +SAW_TEST("Unit / multiplication is commutative in the type"){ + using A = Mul>, U>>; + using B = Mul>, U>>; + static_assert(std::is_same_v, "m*s and s*m must be the same type"); + SAW_EXPECT(true, "static assert carries this one"); +} +} + +namespace { +/** + * An alias written by hand must already be in canonical order, or it will not + * match the same unit arrived at by arithmetic. + * + * Multiplying by a dimensionless unit runs the alias back through the + * reduction and the sort, so if its factors were declared out of order the + * round trip changes the type and this fails. + */ +template +inline constexpr bool is_canonical = + std::is_same_v, Alias>, Alias>; + +SAW_TEST("Unit / division is commutative in the type too"){ + // a/b reached two ways must agree. + using A = Div>, U>>, U>>; + using B = Div>, U>>, U>>; + static_assert(std::is_same_v, "operand order must not change the type"); + SAW_EXPECT(true, "static assert carries this one"); +} + +SAW_TEST("Unit / three factors sort canonically whatever the order"){ + using Kv = sch::UnitElement; + using Sl = sch::UnitElement; + + using One = Mul>, U>, U>; + using Two = Mul, U>>, U>; + using Three = Mul, U>, U>>; + + static_assert(std::is_same_v, "any permutation must agree"); + static_assert(std::is_same_v, "any permutation must agree"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Unit / every lbm_unit alias is already canonical"){ + static_assert(is_canonical>, "SiMeter"); + static_assert(is_canonical>, "LbmMeter"); + static_assert(is_canonical>, "SiSecond"); + static_assert(is_canonical>, "LbmSecond"); + static_assert(is_canonical>, "SiVelocity"); + static_assert(is_canonical>, "LbmVelocity"); + static_assert(is_canonical>, "SiAcceleration"); + static_assert(is_canonical>, "LbmAcceleration"); + static_assert(is_canonical>, "SiKinematicViscosity"); + static_assert(is_canonical>, "LbmKinematicViscosity"); + SAW_EXPECT(true, "static asserts carry this one"); +} +} + +namespace { +/** + * Velocity is typed through the kinematics and unwrapped at the force law, + * because force needs a mass base unit that does not exist yet. + */ +SAW_TEST("Unit / a length over a time is a velocity"){ + using Vel = Div, sch::LbmSecond>; + static_assert(std::is_same_v>, + "m / s must be exactly the LbmVelocity alias"); + + // And a velocity times a time is back to a length. + using Len = Mul, sch::LbmSecond>; + static_assert(std::is_same_v>, + "velocity * time must be a length again"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Unit / a normalised vector is Pure, not merely unannotated"){ + kel::lbm::data>> v; + v.at({{0u}}).set(3.0); + v.at({{1u}}).set(4.0); + v.at({{2u}}).set(0.0); + + auto dir = kel::lbm::math::normalize(v); + static_assert( + std::is_same_v>>>, + "a direction must be Pure"); + + SAW_EXPECT(std::abs(dir.at({{0u}}).get() - 0.6) < 1e-12, "x component"); + SAW_EXPECT(std::abs(dir.at({{1u}}).get() - 0.8) < 1e-12, "y component"); + + // The length keeps the unit it measured. + auto len = kel::lbm::math::norm(v); + static_assert(std::is_same_v>>>, + "a length must stay a length"); + SAW_EXPECT(std::abs(len.at({}).get() - 5.0) < 1e-12, "norm value"); +} +} + +namespace { +/** + * sqrt halves the exponents, so the root of an area is a length. This is what + * lets `pos_rel / sqrt(distance_squared)` come out as a dimensionless + * direction rather than an unlabelled vector. + */ +SAW_TEST("Unit / sqrt of an area is a length"){ + using Area = Mul>, sch::LbmMeter>>; + + kel::lbm::data a; + a.at({}).set(9.0); + + auto len = kel::lbm::math::sqrt(a); + static_assert( + std::is_same_v>>>, + "sqrt(m^2) must be m"); + SAW_EXPECT(std::abs(len.at({}).get() - 3.0) < 1e-12, "and the value must be right"); + + // A length divided by a length is a direction scale: dimensionless. + using Ratio = Div>, sch::LbmMeter>>; + static_assert(std::is_same_v>>, + "m / m must be Pure"); + SAW_EXPECT(true, "static asserts carry the rest"); +} + +SAW_TEST("Unit / sqrt refuses an odd exponent"){ + // m^3 has no expressible square root in this system. The static_assert in + // math::sqrt is what stops it, so this test records the intent rather + // than invoking it -- instantiating it would fail the build by design. + using Cube = Mul>, + sch::LbmMeter>>, + sch::LbmMeter>>; + static_assert(std::is_same_v, + sch::UnitElement>>, + "m*m*m must be m^3"); + SAW_EXPECT(true, "sqrt of this would not compile, by design"); +} +} diff --git a/modules/core/tests/voxel_narrow.cpp b/modules/core/tests/voxel_narrow.cpp index 5cffc76..08c1c1f 100644 --- a/modules/core/tests/voxel_narrow.cpp +++ b/modules/core/tests/voxel_narrow.cpp @@ -15,7 +15,8 @@ using T = Float64; constexpr uint64_t D = 2u; auto make_pair(double radius, uint64_t res = 32u){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(1.0); return kel::lbm::create_spheroid_particle_group(r,d,{res}); diff --git a/modules/core/tests/vtk_write.cpp b/modules/core/tests/vtk_write.cpp index b2a5682..61fb548 100644 --- a/modules/core/tests/vtk_write.cpp +++ b/modules/core/tests/vtk_write.cpp @@ -31,7 +31,7 @@ using CellStruct = Struct< template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member >; @@ -47,7 +47,7 @@ SAW_TEST("VTK Write test example"){ kel::lbm::data, 2>> cells{{{2u,2u}}}; auto& cell_0 = cells.at({{{0,0}}}); - cell_0.template get<"momentum">()= {{0.5,-0.1}}; + cell_0.template get<"velocity">()= {{0.5,-0.1}}; cell_0.template get<"pressure">().set(1.1); auto eov = lbm::impl::lbm_vtk_writer, 2>>::apply(sstream, cells); @@ -74,7 +74,7 @@ SAW_TEST("VTK Write raw test example"){ auto meta = cells.dims(); auto& cell_0 = cells.at({{{0,0}}}); - cell_0.template get<"momentum">()= {{0.5,-0.1}}; + cell_0.template get<"velocity">()= {{0.5,-0.1}}; cell_0.template get<"pressure">().set(1.1); auto eov = lbm::impl::lbm_vtk_writer_raw,2u>::apply(sstream, &cell_0, meta); -- cgit v1.3.1