rtx-cfd: overset P4-0 — the O-grid around the Turek–Hron rigid body (cylinder + flag), gated
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
Documentation / Build API Documentation (push) Canceled after 0s
Documentation / Build User Guide (push) Canceled after 0s
CI / Python Bindings (maturin) (ubuntu-latest) (push) Canceled after 0s
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 / WASM Build + Size Check (push) Canceled after 0s
CI / Distributed Training Tests (push) Canceled after 0s
CI / CI Success (push) Canceled after 0s

patch_gen::{cylinder_flag_outline, cylinder_flag_patch, winslow_smooth, respace_rays}
(+ convex_hull, offset_convex_polygon, nearest_on_polyline): the outline CCW then
reversed to clockwise (tip semicircle 16 cells, junction fillets of a FIXED radius
with 3 cells, straights graded 0.3 h → h, cylinder arc at h); the outer ring the
6 h normal offset of the body's convex hull; initial pairing by the inner point's
normal offset projected onto the hull offset (an arclength-proportional pairing
folded the transfinite grid at the tip: rays crossed where the curvatures differ);
Winslow (TTM) smoothing of the interior with the outer nodes SLIDING along the
hull offset (each re-placed at the nearest point to the extrapolated ray), then
re-spacing along the smoothed rays to the across stretch. Gates
(tests/patch_cylinder_flag.rs): ny = 41/62/82 → 143×12 / 183×12 / 225×12 cells,
positive, wall row 0.23 h (fillet max 0.37 / 0.43 / 0.50 h), worst
non-orthogonality 76.6 / 69.1 / 63.3° at the concave fillets (structural: a
concave arc's normals converge at its centre), classification of the benchmark
background with both donor invariants. P0 MMS on these meshes
(tests/cylinder_flag_mms.rs, exact acceptors, line-implicit): Stokes orders 2.17 /
2.13, upwind 2.00 / 1.86 (cell Péclet ≈ 0.1), divergence ≤ 9e-14 — the fillet skew
costs nothing measurable. Rule: a refinement ladder's geometry must be fixed in
physical units — with fillet = h/2 the Stokes orders read 1.74 → 1.31, the O(h)
boundary perturbation masquerading as a scheme defect; the fillet is a parameter
(5 mm across the ladder). Also: the P3b knock-outs H1/H2 on the balanced default
(no effect), the P3 §5.10 record in the falsifier's header.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01X2GmJXeQ2njUecEKiJZ1G2
This commit is contained in:
Omar Sobh
2026-09-05 19:50:14 -07:00
co-authored by Claude Fable 5.1
parent 62df6bd628
commit 45ff34da8f
4 changed files with 763 additions and 0 deletions
@@ -243,3 +243,415 @@ pub fn stadium(
outer.push(outer[0]);
transfinite(&inner, &outer, nn, stretch, Some([0.0, 0.0]))
}
#[allow(dead_code)]
/// Cumulative arclength fractions of a closed polyline (`pts[0]` repeated
/// as the implicit last point): `frac[i]` for `i = 0..=n`, `frac[n] = 1`.
fn arclength_fractions(pts: &[[f64; 2]]) -> Vec<f64> {
let n = pts.len();
let mut cum = Vec::with_capacity(n + 1);
let mut s = 0.0;
cum.push(0.0);
for i in 0..n {
let (a, b) = (pts[i], pts[(i + 1) % n]);
s += ((b[0] - a[0]).powi(2) + (b[1] - a[1]).powi(2)).sqrt();
cum.push(s);
}
cum.iter().map(|c| c / s).collect()
}
#[allow(dead_code)]
/// Point at arclength fraction `f` of the closed polyline `pts`.
fn point_at_fraction(pts: &[[f64; 2]], cum: &[f64], f: f64) -> [f64; 2] {
let n = pts.len();
let f = f.clamp(0.0, 1.0);
// cum has n+1 entries, cum[i] .. cum[i+1] is segment i.
let mut i = match cum.binary_search_by(|c| c.partial_cmp(&f).unwrap()) {
Ok(k) => k.min(n - 1),
Err(k) => k.saturating_sub(1).min(n - 1),
};
while i + 1 < cum.len() - 1 && cum[i + 1] < f {
i += 1;
}
let (a, b) = (pts[i], pts[(i + 1) % n]);
let seg = cum[i + 1] - cum[i];
let t = if seg > 0.0 { (f - cum[i]) / seg } else { 0.0 };
[a[0] + t * (b[0] - a[0]), a[1] + t * (b[1] - a[1])]
}
/// Nearest point on the closed polyline `pts` to `q` (segment projection).
fn nearest_on_polyline(pts: &[[f64; 2]], q: [f64; 2]) -> [f64; 2] {
let n = pts.len();
let (mut best, mut best_d) = (pts[0], f64::INFINITY);
for i in 0..n {
let (a, b) = (pts[i], pts[(i + 1) % n]);
let (dx, dy) = (b[0] - a[0], b[1] - a[1]);
let l2 = dx * dx + dy * dy;
let t = if l2 > 0.0 {
(((q[0] - a[0]) * dx + (q[1] - a[1]) * dy) / l2).clamp(0.0, 1.0)
} else {
0.0
};
let p = [a[0] + t * dx, a[1] + t * dy];
let d = (p[0] - q[0]).powi(2) + (p[1] - q[1]).powi(2);
if d < best_d {
best_d = d;
best = p;
}
}
best
}
/// Points on the circular arc of centre `c`, radius `r`, from angle `a0` to
/// `a1` (radians, signed sweep), `k` segments, EXCLUDING the end point.
fn arc_points(c: [f64; 2], r: f64, a0: f64, a1: f64, k: usize) -> Vec<[f64; 2]> {
(0..k)
.map(|m| {
let th = a0 + (a1 - a0) * m as f64 / k as f64;
[c[0] + r * th.cos(), c[1] + r * th.sin()]
})
.collect()
}
/// The TurekHron rigid body — the cylinder of radius `r` at `centre` with
/// the flag of half-thickness `t` reaching to `x_tip` — as a closed
/// COUNTER-CLOCKWISE outline: the flag tip a semicircle of radius `t`, the
/// two concave junctions filleted with radius `fillet` (`k_fillet` cells
/// each), straights graded from the tip/fillet spacing to `d_straight`
/// (ratio `grade`), the cylinder arc at ≈ `d_straight`, the tip arc with
/// `k_tip` cells. Starts at the tip's topmost point.
#[allow(clippy::too_many_arguments)]
pub fn cylinder_flag_outline(
centre: [f64; 2],
r: f64,
t: f64,
x_tip: f64,
fillet: f64,
k_fillet: usize,
k_tip: usize,
d_straight: f64,
grade: f64,
) -> Vec<[f64; 2]> {
let (cx, cy) = (centre[0], centre[1]);
let (y_top, y_bot) = (cy + t, cy - t);
let tip_c = [x_tip - t, cy];
// Junction x on the circle, and the fillet centre F (in the fluid,
// tangent to the flag line and externally to the cylinder).
let x_f = cx + ((r + fillet).powi(2) - (t + fillet).powi(2)).sqrt();
let f_top = [x_f, y_top + fillet];
let f_bot = [x_f, y_bot - fillet];
// Tangent points: on the flag line straight below/above F; on the circle
// along C → F.
let t1_top = [x_f, y_top];
let dir_top = [
(f_top[0] - cx) / (r + fillet),
(f_top[1] - cy) / (r + fillet),
];
let t2_top = [cx + r * dir_top[0], cy + r * dir_top[1]];
let t1_bot = [x_f, y_bot];
let dir_bot = [
(f_bot[0] - cx) / (r + fillet),
(f_bot[1] - cy) / (r + fillet),
];
let t2_bot = [cx + r * dir_bot[0], cy + r * dir_bot[1]];
let d0 = (PI * t / k_tip as f64).min(fillet * PI / 2.0 / k_fillet as f64);
let mut pts: Vec<[f64; 2]> = Vec::new();
// 1. Top edge, from the tip top (x_tip t, y_top) to the fillet tangent
// point (x_f, y_top), moving x (CCW: body on the left).
let len_top = (x_tip - t) - x_f;
let fr = graded_fractions(len_top, d0, d_straight, grade);
for &f in &fr[..fr.len() - 1] {
pts.push([(x_tip - t) - f * len_top, y_top]);
}
// 2. Top fillet, from T1 (angle 90° about F) to T2 (angle of C F),
// the short way.
let a_t1 = -PI / 2.0;
let mut a_t2 = (t2_top[1] - f_top[1]).atan2(t2_top[0] - f_top[0]);
while a_t2 - a_t1 > PI {
a_t2 -= 2.0 * PI;
}
while a_t2 - a_t1 < -PI {
a_t2 += 2.0 * PI;
}
pts.extend(arc_points(f_top, fillet, a_t1, a_t2, k_fillet));
// 3. Cylinder arc, CCW from angle(T2_top) to angle(T2_bot) + 2π.
let th_top = (t2_top[1] - cy).atan2(t2_top[0] - cx);
let th_bot = (t2_bot[1] - cy).atan2(t2_bot[0] - cx) + 2.0 * PI;
let arc_len = (th_bot - th_top) * r;
let k_arc = ((arc_len / d_straight).round() as usize).max(8);
pts.extend(arc_points(centre, r, th_top, th_bot, k_arc));
// 4. Bottom fillet, from T2_bot to T1_bot (angle +90° about F_bot).
let a_b2 = (t2_bot[1] - f_bot[1]).atan2(t2_bot[0] - f_bot[0]);
let mut a_b1 = PI / 2.0;
while a_b1 - a_b2 > PI {
a_b1 -= 2.0 * PI;
}
while a_b1 - a_b2 < -PI {
a_b1 += 2.0 * PI;
}
pts.extend(arc_points(f_bot, fillet, a_b2, a_b1, k_fillet));
// 5. Bottom edge, from (x_f, y_bot) to the tip bottom, moving +x.
for &f in &fr[..fr.len() - 1] {
pts.push([x_f + f * len_top, y_bot]);
}
// 6. Tip semicircle, from 90° to +90° about the tip centre (CCW).
pts.extend(arc_points(tip_c, t, -PI / 2.0, PI / 2.0, k_tip));
let _ = (t1_top, t1_bot);
pts
}
/// The 2-D convex hull (Andrew's monotone chain), counter-clockwise.
fn convex_hull(mut pts: Vec<[f64; 2]>) -> Vec<[f64; 2]> {
pts.sort_by(|a, b| a.partial_cmp(b).unwrap());
pts.dedup();
if pts.len() < 3 {
return pts;
}
let cross = |o: [f64; 2], a: [f64; 2], b: [f64; 2]| {
(a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])
};
let mut lower: Vec<[f64; 2]> = Vec::new();
for &p in &pts {
while lower.len() >= 2 && cross(lower[lower.len() - 2], lower[lower.len() - 1], p) <= 0.0 {
lower.pop();
}
lower.push(p);
}
let mut upper: Vec<[f64; 2]> = Vec::new();
for &p in pts.iter().rev() {
while upper.len() >= 2 && cross(upper[upper.len() - 2], upper[upper.len() - 1], p) <= 0.0 {
upper.pop();
}
upper.push(p);
}
lower.pop();
upper.pop();
lower.extend(upper);
lower
}
/// The normal offset by `d` of a convex counter-clockwise polygon, as a
/// fine polyline (`per_vertex` points on each rounded corner).
fn offset_convex_polygon(hull: &[[f64; 2]], d: f64, per_vertex: usize) -> Vec<[f64; 2]> {
let n = hull.len();
let mut out = Vec::new();
for i in 0..n {
let prev = hull[(i + n - 1) % n];
let cur = hull[i];
let next = hull[(i + 1) % n];
// Outward normals of the incoming and outgoing edges (CCW polygon:
// outward = right-hand normal (dy, dx)).
let n_in = {
let (dx, dy) = (cur[0] - prev[0], cur[1] - prev[1]);
let l = (dx * dx + dy * dy).sqrt().max(1e-300);
[dy / l, -dx / l]
};
let n_out = {
let (dx, dy) = (next[0] - cur[0], next[1] - cur[1]);
let l = (dx * dx + dy * dy).sqrt().max(1e-300);
[dy / l, -dx / l]
};
let a0 = n_in[1].atan2(n_in[0]);
let mut a1 = n_out[1].atan2(n_out[0]);
while a1 < a0 {
a1 += 2.0 * PI;
}
// Rounded corner around `cur` from n_in to n_out (CCW sweep ≤ π).
let k = if a1 - a0 > 1e-9 { per_vertex } else { 1 };
for m in 0..k {
let th = a0 + (a1 - a0) * m as f64 / k as f64;
out.push([cur[0] + d * th.cos(), cur[1] + d * th.sin()]);
}
}
out
}
/// Winslow (TTM) smoothing of the interior nodes of a periodic structured
/// grid `x[k][i]`, `k = 0..=nn` (row 0 fixed; row nn fixed, or SLIDING on
/// the closed curve `outer_curve` when given: after each interior sweep
/// every outer node is re-placed at the nearest point of the curve to the
/// extrapolated ray from rows nn2, nn1 — the distribution on the outer
/// ring follows the interior instead of dictating it), `i = 0..ns`
/// (column `ns` mirrors column 0). GaussSeidel until the largest node
/// move is below `tol` or `max_sweeps`. Returns `(sweeps, last move)`.
pub fn winslow_smooth(
x: &mut [Vec<[f64; 2]>],
ns: usize,
tol: f64,
max_sweeps: usize,
outer_curve: Option<&[[f64; 2]]>,
) -> (usize, f64) {
let nn = x.len() - 1;
let mut sweeps = 0;
let mut moved = f64::INFINITY;
while sweeps < max_sweeps && moved > tol {
sweeps += 1;
moved = 0.0;
if let Some(curve) = outer_curve {
for i in 0..ns {
let (a, b) = (x[nn - 2][i], x[nn - 1][i]);
let cand = [2.0 * b[0] - a[0], 2.0 * b[1] - a[1]];
let p = nearest_on_polyline(curve, cand);
let d = ((p[0] - x[nn][i][0]).powi(2) + (p[1] - x[nn][i][1]).powi(2)).sqrt();
moved = moved.max(d);
x[nn][i] = p;
}
x[nn][ns] = x[nn][0];
}
for k in 1..nn {
for i in 0..ns {
let ip = (i + 1) % ns;
let im = (i + ns - 1) % ns;
let (xe, xw, xn, xs) = (x[k][ip], x[k][im], x[k + 1][i], x[k - 1][i]);
let (xne, xnw, xse, xsw) = (x[k + 1][ip], x[k + 1][im], x[k - 1][ip], x[k - 1][im]);
let xi = [0.5 * (xe[0] - xw[0]), 0.5 * (xe[1] - xw[1])];
let eta = [0.5 * (xn[0] - xs[0]), 0.5 * (xn[1] - xs[1])];
let g11 = xi[0] * xi[0] + xi[1] * xi[1];
let g22 = eta[0] * eta[0] + eta[1] * eta[1];
let g12 = xi[0] * eta[0] + xi[1] * eta[1];
let denom = 2.0 * (g11 + g22);
if denom <= 1e-300 {
continue;
}
let mut new = [0.0; 2];
for c in 0..2 {
let cross = 0.25 * (xne[c] - xnw[c] - xse[c] + xsw[c]);
new[c] =
(g22 * (xe[c] + xw[c]) + g11 * (xn[c] + xs[c]) - 2.0 * g12 * cross) / denom;
}
let d = ((new[0] - x[k][i][0]).powi(2) + (new[1] - x[k][i][1]).powi(2)).sqrt();
moved = moved.max(d);
x[k][i] = new;
}
x[k][ns] = x[k][0];
}
}
(sweeps, moved)
}
/// Re-space the nodes of every column (ray) of `x` along its polyline to the
/// fractions `eta` (keeps the smoothed ray SHAPES, restores the across-body
/// stretch Winslow equidistributes away).
pub fn respace_rays(x: &mut [Vec<[f64; 2]>], ns: usize, eta: &[f64]) {
let nn = x.len() - 1;
for i in 0..=ns {
let ray: Vec<[f64; 2]> = (0..=nn).map(|k| x[k][i]).collect();
let mut cum = vec![0.0; nn + 1];
for k in 1..=nn {
cum[k] = cum[k - 1]
+ ((ray[k][0] - ray[k - 1][0]).powi(2) + (ray[k][1] - ray[k - 1][1]).powi(2))
.sqrt();
}
let total = cum[nn];
for (k, &e) in eta.iter().enumerate().skip(1).take(nn - 1) {
let target = e * total;
let mut seg = 0;
while seg + 1 < nn && cum[seg + 1] < target {
seg += 1;
}
let l = cum[seg + 1] - cum[seg];
let tt = if l > 0.0 {
(target - cum[seg]) / l
} else {
0.0
};
x[k][i] = [
ray[seg][0] + tt * (ray[seg + 1][0] - ray[seg][0]),
ray[seg][1] + tt * (ray[seg + 1][1] - ray[seg][1]),
];
}
}
}
/// The O-grid around the TurekHron rigid body (`docs/overset_metal_campaign.md`
/// §5.11): inner ring = `cylinder_flag_outline` (reversed to CLOCKWISE, the
/// right-handed frame), outer ring = the `offset` normal offset of the
/// body's convex hull sampled at the inner ring's arclength fractions,
/// transfinite start with the geometric `stretch` across, Winslow smoothing
/// of the interior, then re-spacing along each ray to the stretch. Returns
/// the mesh and `(winslow sweeps, last move)`.
#[allow(clippy::too_many_arguments)]
pub fn cylinder_flag_patch(
centre: [f64; 2],
r: f64,
t: f64,
x_tip: f64,
h: f64,
fillet: f64,
offset: f64,
nn: usize,
stretch: f64,
winslow_sweeps: usize,
) -> CfdResult<(PatchMesh, (usize, f64))> {
// The fillet radius is a GEOMETRY parameter, fixed across a refinement
// ladder (a radius ∝ h is an O(h) boundary error: measured Stokes orders
// 1.74 → 1.31 with fillet = h/2 on the ny = 41/62/82 ladder).
let mut inner = cylinder_flag_outline(centre, r, t, x_tip, fillet, 3, 16, h, 1.15);
inner.reverse(); // clockwise
let ns = inner.len();
// Outer ring: hull offset, sampled at the inner ring's arclength
// fractions, starting from the point nearest the inner start's normal
// offset.
let mut hull_src: Vec<[f64; 2]> = arc_points(centre, r, 0.0, 2.0 * PI, 256);
hull_src.extend(arc_points(
[x_tip - t, centre[1]],
t,
-PI / 2.0,
PI / 2.0,
64,
));
hull_src.push([x_tip - t, centre[1] + t]);
hull_src.push([x_tip - t, centre[1] - t]);
let hull = convex_hull(hull_src);
let outer_poly = offset_convex_polygon(&hull, offset, 24);
// Initial outer ring: the inner point pushed along its outward normal
// (the ring is clockwise, so the outward normal is the LEFT-hand normal
// of the direction of travel) and projected onto the hull offset —
// exact on the convex parts, bunched across the concave junctions (the
// swallowtail), which the sliding Winslow rows then spread out.
let outer: Vec<[f64; 2]> = (0..=ns)
.map(|i| {
let i0 = i % ns;
let prev = inner[(i0 + ns - 1) % ns];
let cur = inner[i0];
let next = inner[(i0 + 1) % ns];
let (dx, dy) = (next[0] - prev[0], next[1] - prev[1]);
let l = (dx * dx + dy * dy).sqrt().max(1e-300);
let normal = [-dy / l, dx / l];
nearest_on_polyline(
&outer_poly,
[cur[0] + offset * normal[0], cur[1] + offset * normal[1]],
)
})
.collect();
let mut inner_closed = inner.clone();
inner_closed.push(inner[0]);
// Transfinite start.
let eta = stretched_fractions(nn, stretch);
let mut x: Vec<Vec<[f64; 2]>> = eta
.iter()
.map(|&e| {
(0..=ns)
.map(|i| {
[
(1.0 - e) * inner_closed[i][0] + e * outer[i][0],
(1.0 - e) * inner_closed[i][1] + e * outer[i][1],
]
})
.collect()
})
.collect();
let report = winslow_smooth(&mut x, ns, 1e-10 * h, winslow_sweeps, Some(&outer_poly));
respace_rays(&mut x, ns, &eta);
let mut xs = Vec::with_capacity((nn + 1) * (ns + 1));
let mut ys = Vec::with_capacity(xs.capacity());
for row in &x {
for p in row {
xs.push(p[0]);
ys.push(p[1]);
}
}
let mesh = PatchMesh::from_nodes(ns, nn, xs, ys, Some([0.0, 0.0]))?;
Ok((mesh, report))
}