Performance Benchmarks / Run Benchmarks (push) Canceled after 0s
CI / Format Check (push) Canceled after 0s
CI / Clippy Check (push) Canceled after 0s
CI / Build (macos-latest) (push) Canceled after 0s
CI / Build (ubuntu-latest) (push) Canceled after 0s
CI / Test (macos-latest) (push) Canceled after 0s
CI / Test (ubuntu-latest) (push) Canceled after 0s
CI / Build CPU-Only (Explicit) (push) Canceled after 0s
CI / Python Bindings (maturin) (macos-latest) (push) Canceled after 0s
CI / Python Bindings (maturin) (ubuntu-latest) (push) Canceled after 0s
CI / WASM Build + Size Check (push) Canceled after 0s
CI / Distributed Training Tests (push) Canceled after 0s
CI / CI Success (push) Canceled after 0s
Documentation / Build API Documentation (push) Canceled after 0s
Documentation / Build User Guide (push) Canceled after 0s
Clears the rest of the quarantine. All three crates now run 558 tests
with 0 failures and no `#[ignore]` markers.
SIMPLE could not converge, and the reason was not slow convergence but
wrong physics.
The pressure correction equation used a bare Laplacian, 1/dx^2 and
1/dy^2, while the velocity correction divided by a_p = rho dx dy / dt.
SIMPLE requires these to be each other's inverse: substituting the
corrected velocities into continuity must reproduce the pressure
equation, which fixes a_E = rho d dy/dx with d = dV/a_p. The two
disagreed by roughly 1/(h^2 dt) -- about 2e4 on a 16x16 cavity -- so the
pressure correction was that many times too weak to enforce continuity.
The consequence was visible and specific. A lid-driven cavity at Re=100
produced a monotonic profile rising from 0 at the floor to 1 at the lid:
Couette flow, with no recirculation anywhere, and a peak pressure of
1.6e-4 against the rho U^2 scale of 1. The return flow in a cavity is
driven entirely by the pressure gradient, so with the pressure pinned
near zero there was nothing to turn the flow around. With the
coefficients made consistent the profile recirculates, the peak pressure
is 2.9, and the solver converges.
Also in SIMPLE:
- `p'` was never reset between outer iterations. It is a correction
that `pressure_update_step` folds into `p`, so carrying it forward
applied the same correction twice.
- The convergence measure was the inner Gauss-Seidel residual, which
goes to zero whether or not the flow satisfies continuity. Now the
mass imbalance.
- The velocity correction used only the transient part of a_p,
`rho dV/dt`, rather than the diagonal the momentum equation was
actually solved with.
- All four convective face fluxes were computed from a single
cell-centred velocity, so `fe` and `fw` were the same number, as were
`fn` and `fs`. Upwinding then picked the same direction on opposite
faces of the control volume. Now interpolated per face on the
staggered grid.
Not claimed: agreement with Ghia, Ghia & Shin (1982). The vortex centre
moves toward their y = 0.4531 under refinement (0.400 at 16^2, 0.419 at
32^2, 0.460 at 64^2) but the minimum centreline velocity reaches only
-0.130 against their -0.2109, and the converged field still depends
slightly on the pseudo-time step, which a true steady state cannot. The
cavity test therefore asserts what is established -- convergence,
recirculation, vortex position, and an O(1) pressure field -- and the
remaining gap is recorded in omni-cortex/docs/solver_status.md rather
than papered over with a loose tolerance.
LBM bounce-back was doing neither of the things its name claims. It was
written as assignment (`f[2] = f[4]`) rather than a swap, discarding the
population being reflected -- bounce-back is a permutation and conserves
mass exactly, so the domain leaked 0.013% of its mass every 100 steps and
would have kept draining. And the pairs used were 5<->8 and 6<->7, which
reverse only the wall-normal component: that is specular reflection, a
free-slip wall, so the no-slip condition the walls were supposed to
impose never held.
Mesh quality:
- Quadrilateral aspect ratio included the diagonals in the maximum but
not the minimum, so it could never return 1: a unit square reported
sqrt(2) and a 2:1 rectangle sqrt(5).
- Triangle aspect ratio used longest-over-shortest edge, which does not
detect the failure mode that matters. A sliver with vertices (0,0),
(10,0), (5,0.1) scores 2.0 -- indistinguishable from a healthy 2:1
triangle -- while its area is a twentieth of what its edges suggest.
Now the radius ratio R/2r, which is 1 for equilateral and 1250 for
that sliver, and which also fixes the quality histogram.
- StructuredMesh aspect ratio took bounding-box extents and guarded the
z-extent with `.max(1e-10)`. On a 2-D mesh the depth is exactly zero,
so the guard became the minimum and a unit square reported 2e10.
Mesh refinement produced meshes that failed their own validation.
`subdivide_triangle` reserved midpoint ids as `next_node_id + k`, then
advanced the counter by 3, after which `refine_cells` called `add_node`
and advanced it three more -- so every refined cell referenced vertices
three ids away from the ones actually created. Separately, the position
lookup selected by slot rather than by id ("This is simplified, should
look up correct midpoint"), so three of four sub-triangles had their
areas computed from the wrong points; the quadrilateral version mapped
every new id to the cell centre.
Fixtures corrected rather than tolerances loosened: a structured mesh
test asserted 0.16 for the average cell volume while the comment beside
it computed 0.25 from the node-count convention the code actually uses;
the Zou-He pressure test built a *velocity* boundary at u = 1.2, far
above the lattice speed of sound, making the density negative; and the
cavity-setup test required the lid to influence the domain centre 16
rows away in 10 steps, which exceeds the lattice propagation speed.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
207 lines
6.8 KiB
Rust
207 lines
6.8 KiB
Rust
//! Tests for LBM boundary conditions
|
|
|
|
use approx::assert_relative_eq;
|
|
use nalgebra::Vector2;
|
|
use rtx_cfd::solvers::lbm::boundary::{
|
|
BounceBackBc, BoundaryOrientation, LbmBoundaryCondition, ZouHeBc,
|
|
};
|
|
use rtx_cfd::solvers::lbm::{D2Q9Parameters, D2Q9Solver};
|
|
|
|
#[test]
|
|
fn test_bounce_back_boundary_mass_conservation() {
|
|
let nx = 16;
|
|
let ny = 16;
|
|
let mut solver = D2Q9Solver::new(nx, ny, D2Q9Parameters::default());
|
|
|
|
// Initialize with uniform density and some velocity
|
|
solver.initialize_uniform(1.0, Vector2::new(0.05, 0.02));
|
|
|
|
let initial_mass = solver.total_mass();
|
|
|
|
// Run simulation with bounce-back boundaries
|
|
for _ in 0..100 {
|
|
solver.step(); // Uses bounce-back internally
|
|
}
|
|
|
|
let final_mass = solver.total_mass();
|
|
|
|
// Mass should be conserved with bounce-back
|
|
assert_relative_eq!(final_mass, initial_mass, epsilon = 1e-10);
|
|
}
|
|
|
|
#[test]
|
|
fn test_bounce_back_no_slip_condition() {
|
|
let nx = 16;
|
|
let ny = 8;
|
|
let mut solver = D2Q9Solver::new(nx, ny, D2Q9Parameters::default());
|
|
|
|
// Initialize with uniform flow
|
|
solver.initialize_uniform(1.0, Vector2::new(0.1, 0.0));
|
|
|
|
// Run for sufficient time to establish boundary layer
|
|
for _ in 0..500 {
|
|
solver.step();
|
|
}
|
|
|
|
// Check that velocity is approximately zero at walls
|
|
for x in 0..nx {
|
|
// Bottom wall
|
|
let vars_bottom = solver.macroscopic_variables_at(x, 0);
|
|
assert!(
|
|
vars_bottom.velocity.norm() < 1e-3,
|
|
"Bottom wall velocity should be small: {}",
|
|
vars_bottom.velocity.norm()
|
|
);
|
|
|
|
// Top wall
|
|
let vars_top = solver.macroscopic_variables_at(x, ny - 1);
|
|
assert!(
|
|
vars_top.velocity.norm() < 1e-3,
|
|
"Top wall velocity should be small: {}",
|
|
vars_top.velocity.norm()
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_bounce_back_velocity_reversal() {
|
|
let mut solver = D2Q9Solver::new(5, 5, D2Q9Parameters::default());
|
|
|
|
// Initialize equilibrium state
|
|
solver.initialize_uniform(1.0, Vector2::zeros());
|
|
|
|
// Create specific distribution functions for testing
|
|
let mut f = vec![0.0; 9];
|
|
f[0] = 4.0 / 9.0; // Rest particle
|
|
f[1] = 1.0 / 18.0; // East velocity (should bounce back to west)
|
|
f[2] = 1.0 / 18.0; // North velocity (should bounce back)
|
|
f[3] = 1.0 / 18.0; // West velocity
|
|
f[4] = 1.0 / 18.0; // South velocity
|
|
f[5] = 1.0 / 72.0; // Northeast velocity
|
|
f[6] = 1.0 / 72.0; // Northwest velocity
|
|
f[7] = 1.0 / 72.0; // Southwest velocity
|
|
f[8] = 1.0 / 72.0; // Southeast velocity
|
|
|
|
// Set this distribution at a wall cell
|
|
solver.set_distribution_at(0, 2, &f);
|
|
|
|
// Store original values
|
|
let f_original = solver.distribution_at(0, 2);
|
|
|
|
// Apply bounce-back boundary conditions
|
|
solver.apply_bounce_back_boundaries();
|
|
|
|
let f_after = solver.distribution_at(0, 2);
|
|
|
|
// Check bounce-back behavior for wall boundaries
|
|
// Bottom wall: north-facing velocities should bounce back
|
|
// The specific behavior depends on which wall we're testing
|
|
assert!(f_after[0] == f_original[0]); // Rest particle unchanged
|
|
}
|
|
|
|
#[test]
|
|
fn test_zou_he_velocity_boundary() {
|
|
let bc = ZouHeBc::new(0.1); // Velocity boundary
|
|
|
|
// Create test distribution
|
|
let mut f = vec![1.0 / 9.0; 9]; // Start with uniform distribution
|
|
|
|
// Apply Zou-He boundary condition
|
|
bc.apply(&mut f, 0, 0);
|
|
|
|
// Check that the distribution is modified
|
|
// (The exact check depends on implementation details)
|
|
assert!(f.iter().sum::<f64>() > 0.0);
|
|
}
|
|
|
|
#[test]
|
|
fn test_zou_he_pressure_boundary() {
|
|
// A pressure boundary prescribes the density, so it must be built with
|
|
// the pressure constructor.
|
|
//
|
|
// This previously used `ZouHeBc::new(1.2)`, which is the legacy
|
|
// constructor for a *velocity* boundary. That imposed a lattice velocity
|
|
// of 1.2 — well above the lattice speed of sound, 1/sqrt(3) — and the
|
|
// Zou-He density relation `rho = (...) / (1 - u)` then divides by -0.2 and
|
|
// returns a negative density. The distribution came back negative, which
|
|
// is what the "sum > 0" assertion was detecting.
|
|
let target_density = 1.2;
|
|
let bc = ZouHeBc::pressure(target_density, BoundaryOrientation::Left);
|
|
|
|
let mut f = vec![1.0 / 9.0; 9];
|
|
bc.apply(&mut f, 0, 0);
|
|
|
|
// The contract of a pressure boundary is the density it produces, so
|
|
// assert that rather than merely that the result is positive.
|
|
assert_relative_eq!(f.iter().sum::<f64>(), target_density, epsilon = 1e-12);
|
|
}
|
|
|
|
#[test]
|
|
fn test_boundary_condition_trait() {
|
|
let bounce_back = BounceBackBc;
|
|
let zou_he = ZouHeBc::new(0.05);
|
|
|
|
let mut f1 = vec![1.0 / 9.0; 9];
|
|
let mut f2 = f1.clone();
|
|
|
|
// Both should implement the trait
|
|
bounce_back.apply(&mut f1, 0, 0);
|
|
zou_he.apply(&mut f2, 0, 0);
|
|
|
|
// Results should be different
|
|
assert_ne!(f1, f2);
|
|
}
|
|
|
|
#[test]
|
|
fn test_lid_driven_cavity_setup() {
|
|
let nx = 32;
|
|
let ny = 32;
|
|
let mut solver = D2Q9Solver::new(nx, ny, D2Q9Parameters::default());
|
|
|
|
// Initialize with zero velocity
|
|
solver.initialize_uniform(1.0, Vector2::zeros());
|
|
|
|
// Run a few steps to establish flow
|
|
for _ in 0..10 {
|
|
solver.step_with_boundaries(|s| {
|
|
// Apply no-slip on walls
|
|
s.apply_bounce_back_boundaries();
|
|
|
|
// Apply moving lid condition (simplified - just set velocity)
|
|
let lid_velocity = 0.1;
|
|
for x in 1..(nx - 1) {
|
|
let density = 1.0;
|
|
let velocity = Vector2::new(lid_velocity, 0.0);
|
|
let f_eq = s.equilibrium_distribution(density, &velocity);
|
|
s.set_distribution_at(x, ny - 1, &f_eq);
|
|
}
|
|
});
|
|
}
|
|
|
|
// Populations move exactly one lattice cell per step, so after 10 steps
|
|
// the lid can only have influenced the 10 rows beneath it. Flow must have
|
|
// developed there.
|
|
let steps = 10;
|
|
let near_lid = solver.macroscopic_variables_at(nx / 2, ny - 2);
|
|
assert!(
|
|
near_lid.velocity.norm() > 0.0,
|
|
"no flow beneath the lid after {steps} steps"
|
|
);
|
|
|
|
// ...and must not have appeared beyond the front. The domain centre is
|
|
// 16 rows from the lid, so it is still exactly at rest.
|
|
//
|
|
// The assertion here previously demanded non-zero velocity at the centre
|
|
// after 10 steps, which would require information to travel 16 cells in
|
|
// 10 -- faster than the lattice permits. Asserting the propagation front
|
|
// in both directions tests something the solver could actually get wrong.
|
|
let centre = solver.macroscopic_variables_at(nx / 2, ny / 2);
|
|
assert!(
|
|
centre.velocity.norm() < 1e-12,
|
|
"the lid influenced the domain centre {} rows away in only {steps} steps, \
|
|
which exceeds the lattice propagation speed: {}",
|
|
ny - 1 - ny / 2,
|
|
centre.velocity.norm()
|
|
);
|
|
}
|