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/macroscopic.cpp | 113 +++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 modules/core/tests/macroscopic.cpp (limited to 'modules/core/tests/macroscopic.cpp') 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"); + } +} +} -- cgit v1.3.1