1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! SIMPLE-style implicit pressure-correction solver.
//!
//! This module adds an *implicit* pressure/diffusion correction on top of the
//! existing explicit [`crate::Step`] scheme. After a provisional explicit
//! momentum update (`u*`, `v*`) we build the pressure Poisson equation
//! `∇²p = (ρ/dt)·∇·u*`, solve it with the sparse conjugate-gradient solver from
//! `tpt-sci-grid`, and subtract its gradient to obtain a divergence-free
//! velocity field. This is the core of a SIMPLE/PISO pressure-projection
//! method, run *alongside* (not replacing) the explicit fractional-step solver.
use tpt_sci_grid::sparse::{CsrMatrix, conjugate_gradient};
use crate::{CfdError, CollocatedGrid};
/// A SIMPLE-style pressure-correction solver on a uniform collocated grid.
///
/// The recommended workflow per timestep is [`SimpleSolver::predict`] (explicit
/// momentum to obtain the provisional `u*`,`v*`), then
/// [`SimpleSolver::correct`] (solve the pressure Poisson equation with CG and
/// subtract its gradient), both wrapped by [`SimpleSolver::advance`]. The
/// provisional field may also be supplied directly via
/// [`SimpleSolver::set_provisional`] for testing or for coupling to an external
/// momentum scheme.
#[derive(Debug, Clone)]
pub struct SimpleSolver {
grid: CollocatedGrid,
/// Provisional `x`-velocity `u*` (before pressure correction).
pub u_star: Vec<f64>,
/// Provisional `y`-velocity `v*` (before pressure correction).
pub v_star: Vec<f64>,
/// Corrected `x`-velocity `u`.
pub u: Vec<f64>,
/// Corrected `y`-velocity `v`.
pub v: Vec<f64>,
/// Pressure field `p` (defined up to an additive constant).
pub p: Vec<f64>,
nu: f64,
rho: f64,
dt: f64,
}
impl SimpleSolver {
/// Construct a solver with zero velocity/pressure fields and the given
/// kinematic viscosity `ν`, density `ρ`, and timestep `dt`.
#[must_use]
pub fn new(grid: CollocatedGrid, nu: f64, rho: f64, dt: f64) -> Self {
let n = grid.len();
Self {
grid,
u_star: vec![0.0; n],
v_star: vec![0.0; n],
u: vec![0.0; n],
v: vec![0.0; n],
p: vec![0.0; n],
nu,
rho,
dt,
}
}
/// Replace the provisional velocity field (`u*`,`v*`) used by the pressure
/// correction.
///
/// # Errors
///
/// Returns [`CfdError::DimensionMismatch`] if either vector's length does
/// not match the number of grid cells.
pub fn set_provisional(&mut self, u: Vec<f64>, v: Vec<f64>) -> Result<(), CfdError> {
let n = self.grid.len();
if u.len() != n || v.len() != n {
return Err(CfdError::DimensionMismatch {
expected: n,
actual: u.len(),
});
}
self.u_star = u;
self.v_star = v;
Ok(())
}
fn clamp(i: isize, n: usize) -> usize {
i.clamp(0, n as isize - 1) as usize
}
/// Discrete divergence `∇·u` of a velocity field, using the *forward*
/// (face-flux) difference `(u[i+1] − u[i]) / dx` per axis with a clamped
/// (zero-flux) boundary: the last cell per axis returns zero, so a uniform
/// field is exactly divergence-free. Its exact negative adjoint is the
/// one-sided pressure gradient used by [`SimpleSolver::correct`], and the
/// Poisson matrix in [`SimpleSolver::build_poisson_matrix`] is the
/// block-form product `A = FₓFₓᵀ + F_yF_yᵀ` of the per-axis divergence
/// operators — together they form a consistent, symmetric
/// pressure-projection scheme.
#[must_use]
pub fn divergence(&self, u: &[f64], v: &[f64]) -> Vec<f64> {
let g = &self.grid;
let (dx, dy) = (g.dx, g.dy);
let mut div = vec![0.0; g.len()];
for j in 0..g.ny {
for i in 0..g.nx {
let c = g.idx(i, j);
let dux = if i < g.nx - 1 {
(u[g.idx(i + 1, j)] - u[c]) / dx
} else {
0.0
};
let dvy = if j < g.ny - 1 {
(v[g.idx(i, j + 1)] - v[c]) / dy
} else {
0.0
};
div[c] = dux + dvy;
}
}
div
}
/// Build the pressure Poisson matrix `A = -∇²` on the structured collocated
/// grid.
///
/// The operator is assembled as the block-form product `A = FₓFₓᵀ + F_yF_yᵀ`,
/// where `Fₓ`/`F_y` are the per-axis forward (face-flux) divergence
/// operators of [`SimpleSolver::divergence`] acting on the `u`/`v` fields —
/// the standard symmetric 5-point Neumann Laplacian with a constant-only
/// nullspace. Because `A` is built from the exact adjoint pair
/// `(F, −Fᵀ)`, the projection in [`SimpleSolver::correct`] is
/// self-consistent: the discrete divergence of the corrected field
/// vanishes up to the conjugate-gradient tolerance whenever the provisional
/// field's net flux through the clamped boundaries balances. The
/// right-hand side is mean-projected for compatibility. Only `∇p` feeds
/// the velocity correction, so the additive gauge is immaterial.
#[must_use]
pub fn build_poisson_matrix(&self) -> CsrMatrix {
let g = &self.grid;
let n = g.len();
let mut rows: Vec<Vec<(usize, f64)>> = vec![Vec::new(); n];
for j in 0..g.ny {
for i in 0..g.nx {
// The two-field collocated projection requires the *block*
// operator `A = FₓFₓᵀ + F_yF_yᵀ`, where `Fₓ`/`F_y` are the
// per-axis forward-difference divergence matrices acting on
// the `u`/`v` fields respectively. Each axis therefore
// contributes its own outer product below. Combining both
// axes into a single summed column before the outer product
// would add spurious `1/(dx·dy)` cross terms — precisely at
// edge/corner cells where both axes have boundary-clamped
// fluxes — which was the historical source of the O(1)
// residual divergence at corner cells.
let mut col_x: Vec<(usize, f64)> = Vec::with_capacity(2);
if i < g.nx - 1 {
col_x.push((g.idx(i, j), -1.0 / g.dx));
}
if i > 0 {
col_x.push((g.idx(i - 1, j), 1.0 / g.dx));
}
let mut col_y: Vec<(usize, f64)> = Vec::with_capacity(2);
if j < g.ny - 1 {
col_y.push((g.idx(i, j), -1.0 / g.dy));
}
if j > 0 {
col_y.push((g.idx(i, j - 1), 1.0 / g.dy));
}
for &(r, v1) in &col_x {
for &(s, v2) in &col_x {
rows[r].push((s, v1 * v2));
}
}
for &(r, v1) in &col_y {
for &(s, v2) in &col_y {
rows[r].push((s, v1 * v2));
}
}
}
}
CsrMatrix::from_rows(n, n, &rows)
}
/// Solve the pressure Poisson equation `∇²p = (ρ/dt)·∇·u*`, returning the
/// pressure field.
///
/// The matrix from [`SimpleSolver::build_poisson_matrix`] is the symmetric
/// Neumann Laplacian: its only nullspace vector is the constant field, and
/// the additive gauge of `p` is immaterial because only `∇p` feeds the
/// velocity correction. The right-hand side is therefore mean-projected
/// for compatibility (a net flux imbalance through the clamped boundaries
/// cannot be represented by a pressure gradient), and conjugate gradient
/// from zero yields the mean-free solution.
#[must_use]
pub fn solve_pressure(&self) -> Vec<f64> {
// The divergence is the exact negative adjoint of the gradient used in
// [`SimpleSolver::correct`] and assembled into
// [`SimpleSolver::build_poisson_matrix`] (`A = FₓFₓᵀ + F_yF_yᵀ`), so
// the discrete equation is exactly `A·p = -(ρ/dt)·∇·u*` with a
// symmetric `A`.
let mut b: Vec<f64> = self
.divergence(&self.u_star, &self.v_star)
.iter()
.map(|d| -(self.rho / self.dt) * d)
.collect();
let mean = b.iter().sum::<f64>() / b.len() as f64;
for v in &mut b {
*v -= mean;
}
let n = self.grid.len();
let a = self.build_poisson_matrix();
conjugate_gradient(&a, &b, None, 1e-12, 30 * n)
}
/// Explicit advection–diffusion momentum update of the provisional field
/// (`u*`,`v*`), analogous to [`crate::Step::momentum`].
pub fn predict(&mut self) {
let g = &self.grid;
let (dx, dy, dt, nu) = (g.dx, g.dy, self.dt, self.nu);
let u0 = self.u.clone();
let v0 = self.v.clone();
let mut u = vec![0.0; g.len()];
let mut v = vec![0.0; g.len()];
for j in 0..g.ny {
for i in 0..g.nx {
let c = g.idx(i, j);
let im = g.idx(Self::clamp(i as isize - 1, g.nx), j);
let ip = g.idx(Self::clamp(i as isize + 1, g.nx), j);
let jm = g.idx(i, Self::clamp(j as isize - 1, g.ny));
let jp = g.idx(i, Self::clamp(j as isize + 1, g.ny));
let dudx = (u0[ip] - u0[im]) / (2.0 * dx);
let dudy = (u0[jp] - u0[jm]) / (2.0 * dy);
let dvdx = (v0[ip] - v0[im]) / (2.0 * dx);
let dvdy = (v0[jp] - v0[jm]) / (2.0 * dy);
let lap_u = (u0[ip] - 2.0 * u0[c] + u0[im]) / (dx * dx)
+ (u0[jp] - 2.0 * u0[c] + u0[jm]) / (dy * dy);
let lap_v = (v0[ip] - 2.0 * v0[c] + v0[im]) / (dx * dx)
+ (v0[jp] - 2.0 * v0[c] + v0[jm]) / (dy * dy);
u[c] = u0[c] + dt * (-(u0[c] * dudx + v0[c] * dudy) + nu * lap_u);
v[c] = v0[c] + dt * (-(u0[c] * dvdx + v0[c] * dvdy) + nu * lap_v);
}
}
self.u_star = u;
self.v_star = v;
}
/// Pressure correction: solve the Poisson equation and subtract `∇p` from
/// the provisional velocity so the result is (approximately) divergence
/// free.
///
/// The gradient here is the exact negative adjoint `−Fᵀ` of the divergence
/// `F` used for the right-hand side (and assembled into
/// [`SimpleSolver::build_poisson_matrix`] as `A = FFᵀ`): a backward
/// difference at interior cells with one-sided differences at the boundary
/// cells, so the projection is self-consistent — the discrete divergence of
/// the corrected field vanishes up to the conjugate-gradient tolerance.
pub fn correct(&mut self) {
let p = self.solve_pressure();
self.p = p.clone();
let g = &self.grid;
let (dx, dy, dt, rho) = (g.dx, g.dy, self.dt, self.rho);
let mut u = vec![0.0; g.len()];
let mut v = vec![0.0; g.len()];
for j in 0..g.ny {
for i in 0..g.nx {
let c = g.idx(i, j);
// −Fᵀ per axis: backward difference at interior cells,
// one-sided at the boundary cells.
let dpdx = if i == 0 {
p[c] / dx
} else if i < g.nx - 1 {
(p[c] - p[g.idx(i - 1, j)]) / dx
} else {
-(p[g.idx(i - 1, j)]) / dx
};
let dpdy = if j == 0 {
p[c] / dy
} else if j < g.ny - 1 {
(p[c] - p[g.idx(i, j - 1)]) / dy
} else {
-(p[g.idx(i, j - 1)]) / dy
};
u[c] = self.u_star[c] - dt / rho * dpdx;
v[c] = self.v_star[c] - dt / rho * dpdy;
}
}
self.u = u;
self.v = v;
}
/// Perform one full SIMPLE step (predict + correct).
///
/// Returns `false` if any velocity or pressure component became
/// non-finite (the scheme went unstable); `true` otherwise.
pub fn advance(&mut self) -> bool {
self.predict();
self.correct();
self.u
.iter()
.chain(&self.v)
.chain(&self.p)
.all(|&x| x.is_finite())
}
/// Maximum absolute discrete divergence ([`SimpleSolver::divergence`], the
/// exact negative transpose of the projection's gradient) of the corrected
/// velocity field — a quality metric that should be near zero after
/// correction, since the pressure correction zeroes exactly this operator
/// up to the conjugate-gradient tolerance.
#[must_use]
pub fn max_divergence(&self) -> f64 {
self.divergence(&self.u, &self.v)
.iter()
.fold(0.0_f64, |a, d| a.max(d.abs()))
}
/// Borrow the underlying grid.
#[must_use]
pub fn grid(&self) -> &CollocatedGrid {
&self.grid
}
}
#[cfg(test)]
mod tests {
use super::*;
use approx::assert_abs_diff_eq;
use std::f64::consts::PI;
// `sin(πx)sin(πy)` is a smooth manufactured pressure. The provisional
// velocity `u* = -(dt/ρ)∇p` (built from the solver's own discrete
// gradient) has divergence `(dt/ρ)∇²p`, so the Poisson solve must recover
// `p` exactly up to the additive gauge.
#[test]
fn manufactured_poisson_recovers_pressure() {
let g = CollocatedGrid::new(40, 40, 1.0, 1.0).unwrap();
let mut solver = SimpleSolver::new(g.clone(), 1e-2, 1.0, 1e-3);
let (nx, ny) = (g.nx, g.ny);
let (dx, dy) = (g.dx, g.dy);
let dt = solver.dt;
let rho = solver.rho;
let mut p_true = vec![0.0; g.len()];
for j in 0..ny {
for i in 0..nx {
let x = (i as f64 + 0.5) * dx;
let y = (j as f64 + 0.5) * dy;
p_true[g.idx(i, j)] = (PI * x).sin() * (PI * y).sin();
}
}
// Build the provisional field from the solver's own *discrete*
// adjoint gradient (`−Fᵀ`: backward differences at interior cells,
// one-sided at boundaries) of the sampled pressure, so the manufactured
// relationship `u* = -(dt/ρ)∇p` holds exactly in the discrete operators
// and the Poisson solve is exact (up to the additive gauge and the
// conjugate-gradient tolerance).
let mut ustar = vec![0.0; g.len()];
let mut vstar = vec![0.0; g.len()];
for j in 0..ny {
for i in 0..nx {
let c = g.idx(i, j);
let dpdx = if i == 0 {
p_true[c] / dx
} else if i < nx - 1 {
(p_true[c] - p_true[g.idx(i - 1, j)]) / dx
} else {
-p_true[g.idx(i - 1, j)] / dx
};
let dpdy = if j == 0 {
p_true[c] / dy
} else if j < ny - 1 {
(p_true[c] - p_true[g.idx(i, j - 1)]) / dy
} else {
-p_true[g.idx(i, j - 1)] / dy
};
ustar[c] = -(dt / rho) * dpdx;
vstar[c] = -(dt / rho) * dpdy;
}
}
solver.set_provisional(ustar, vstar).unwrap();
let p = solver.solve_pressure();
// The recovered pressure satisfies `p + p_true` = constant exactly
// (the constant nullspace of the Neumann problem; the solve pins the
// mean to zero). Gauge-fix, then check the interior deviation.
let mut shift = 0.0;
let mut count = 0usize;
for c in 0..g.len() {
let i = c % nx;
let j = c / nx;
if i > 0 && i < nx - 1 && j > 0 && j < ny - 1 {
shift += p[c] + p_true[c];
count += 1;
}
}
shift /= count as f64;
let mut max_dev = 0.0_f64;
for c in 0..g.len() {
let i = c % nx;
let j = c / nx;
if i > 0 && i < nx - 1 && j > 0 && j < ny - 1 {
max_dev = max_dev.max((p[c] + p_true[c] - shift).abs());
}
}
assert!(max_dev < 1e-9, "recovered pressure deviates by {max_dev}");
}
#[test]
fn pressure_correction_reduces_divergence() {
let g = CollocatedGrid::new(24, 24, 1.0, 1.0).unwrap();
let mut solver = SimpleSolver::new(g.clone(), 1e-2, 1.0, 1e-3);
let mut u = vec![0.0; g.len()];
let v = vec![0.0; g.len()];
// A mass-compatible provisional field: the same nonzero value at both
// ends of each x-line means the clamped forward divergence sums to zero
// over the grid (no net flux imbalance), as a closed box requires.
for j in 0..g.ny {
for i in 0..g.nx {
u[g.idx(i, j)] = 0.05 * (2.0 * PI * i as f64 / (g.nx - 1) as f64).cos() + 0.05;
}
}
solver.set_provisional(u.clone(), v.clone()).unwrap();
let before = solver
.divergence(&u, &v)
.iter()
.map(|d| d.abs())
.fold(0.0, f64::max);
solver.correct();
let after = solver.max_divergence();
assert!(
after < before * 0.1,
"pressure correction should reduce divergence (before={before}, after={after})"
);
}
/// The Poisson matrix must equal the block-form operator product
/// `A = FₓFₓᵀ + F_yF_yᵀ`, the exact negative-adjoint pair consumed by the
/// two-field projection in [`SimpleSolver::correct`]. Assembling combined
/// per-cell columns instead adds spurious `1/(dx·dy)` cross terms at
/// edge/corner cells, which was the historical cause of O(1) residual
/// divergence there.
#[test]
fn poisson_matrix_is_exact_adjoint_of_divergence() {
let g = CollocatedGrid::new(24, 24, 1.0, 1.0).unwrap();
let solver = SimpleSolver::new(g.clone(), 1e-2, 1.0, 1e-3);
let a = solver.build_poisson_matrix();
let zeros = vec![0.0; g.len()];
for k in [0usize, 5, 23, 24, 100, 275, 300, 551, 575] {
// Independent construction of column `k` of `A`:
// col_k(A) = Fₓ(Fₓᵀ e_k) + F_y(F_yᵀ e_k),
// where Fₓᵀ e_k is row `k` of Fₓ (the coefficients of cell `k`'s
// value in its own and its neighbour's x-divergence).
let mut wx = vec![0.0; g.len()];
let i = k % g.nx;
if i < g.nx - 1 {
wx[k] -= 1.0 / g.dx;
wx[k + 1] += 1.0 / g.dx;
}
let mut wy = vec![0.0; g.len()];
let j = k / g.nx;
if j < g.ny - 1 {
wy[k] -= 1.0 / g.dy;
wy[k + g.nx] += 1.0 / g.dy;
}
let expected = {
let cx = solver.divergence(&wx, &zeros);
let cy = solver.divergence(&zeros, &wy);
cx.iter().zip(&cy).map(|(x, y)| x + y).collect::<Vec<_>>()
};
let mut e = vec![0.0; g.len()];
e[k] = 1.0;
let a_col = a.mul_vec(&e);
let err: f64 = a_col
.iter()
.zip(&expected)
.map(|(x, y)| (x - y).abs())
.fold(0.0, f64::max);
assert!(err < 1e-9, "column {k}: |A e − (FₓFₓᵀ+F_yF_yᵀ) e| = {err}");
}
}
#[test]
fn advance_stays_finite_and_divergence_free() {
let grid = CollocatedGrid::new(16, 16, 1.0, 1.0).unwrap();
let mut solver = SimpleSolver::new(grid, 0.01, 1.0, 0.002);
// Seed a small uniform flow so the explicit step is stable.
solver.u.fill(0.5);
assert!(solver.advance());
assert!(solver.max_divergence() < 1e-6);
}
#[test]
fn set_provisional_rejects_wrong_length() {
let grid = CollocatedGrid::new(8, 8, 1.0, 1.0).unwrap();
let mut solver = SimpleSolver::new(grid, 1.0, 1.0, 1.0);
let r = solver.set_provisional(vec![0.0; 5], vec![0.0; 64]);
assert!(matches!(r, Err(CfdError::DimensionMismatch { .. })));
}
#[test]
fn uniform_provisional_stays_divergence_free() {
let grid = CollocatedGrid::new(12, 12, 1.0, 1.0).unwrap();
let mut solver = SimpleSolver::new(grid, 1.0, 1.0, 1e-3);
solver.u_star.fill(0.7);
// Divergence of a uniform field is exactly zero.
assert_abs_diff_eq!(solver.max_divergence(), 0.0, epsilon = 1e-12);
}
}