flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_open_face_plane_mpi.py
Go to the documentation of this file.
1#!/usr/bin/env python
2"""Gate: the high-side face plane under MPI -- a distributed run equals the serial one (both grids).
3
4Why this test exists. The fix that keeps the HIGH-side domain face of a MAC face field across the
5ghost fills (doc/uf_outlet_fix.md, test_open_face_plane.py) acts only on the rank that owns that
6global face (touchesGlobalFace); every other rank must take the plain halo exchange. Nothing else
7registered covers that branch: the mpi_np2 state hash is fully periodic. Here the domain is split
8along x only (asserted), so the +x face belongs to one rank and, at np = 3, the middle rank touches
9neither open face.
10
11Cases (each on SolverColocated and Solver, distributed vs a serial solver on rank 0):
12 A. forward channel (-x inflow, +x outflow), developed 30 steps, then a c = 1 scalar (D = 0, Koren)
13 for 3 steps: c and u equal serial to 1e-12, and max|c - 1| <= 1e-10 (a wrapped outlet plane
14 moves the outlet column by O(1), on every rank count);
15 B. the channel driven backwards (fluid enters through the +x outflow face): the backflow census
16 max_reverse per step equals serial to 1e-12;
17 C. a HIGH-side inflow (+x inflow, -x outflow), 60 steps: u equals serial to 1e-12.
18The distributed-vs-serial differences measured 2026-10-22 were <= 1.8e-14 at np 1/2/3/4/6.
19
20Run: OMP_NUM_THREADS=2 OMP_PROC_BIND=false PYTHONPATH=<build> \
21 mpirun --bind-to none -np 3 python tests/python/test_open_face_plane_mpi.py
22Exit 0 pass, 1 fail, 77 skipped (no module, no MPI build, no mpi4py, or a single rank).
23"""
24import sys
25
26import numpy as np
27
28NX, NY, NZ = 48, 16, 4
29MU, DT = 0.1, 0.5
30TOL_SERIAL = 1e-12
31TOL_C = 1e-10
32
33
34def make(pf, cls, u_in, low, blk, dist):
35 lx, ly, lz = blk[3:]
36 s = cls(lx, ly, lz)
37 if dist:
38 s.init_mpi(NX, NY, NZ)
39 s.set_rho(1.0); s.set_mu(MU); s.set_dt(DT)
40 s.set_advection(True); s.set_advection_scheme("koren")
41 if cls is pf.SolverColocated:
42 s.set_collocated_scheme("gauge-exact")
43 if low == "inflow":
44 s.set_domain_bc("-x", "inflow", (u_in, 0.0, 0.0)); s.set_domain_bc("+x", "outflow")
45 else:
46 s.set_domain_bc("-x", "outflow"); s.set_domain_bc("+x", "inflow", (u_in, 0.0, 0.0))
47 s.set_domain_bc("-y", "wall"); s.set_domain_bc("+y", "wall")
48 s.set_pressure_geometry(np.asfortranarray(np.full((lx, ly, lz), 10.0)))
49 s.set_pressure_pcg(True, 400, 1e-11)
50 return s
51
52
53def run_cases(pf, blk, dist):
54 """The three cases on one block; returns {name: local array} (census: the per-step series)."""
55 res = {}
56 for cls in (pf.SolverColocated, pf.Solver):
57 n = cls.__name__
58 s = make(pf, cls, 1.0, "inflow", blk, dist)
59 for _ in range(30):
60 s.step()
61 s.add_scalar("c", diffusivity=0.0, scheme="koren", iters=1)
62 s.set_scalar_bc("c", "-x", "dirichlet", 1.0); s.set_scalar_bc("c", "+x", "neumann", 0.0)
63 s.set_field("c", np.asfortranarray(np.ones(blk[3:])))
64 for _ in range(3):
65 s.step()
66 res[n + "_A_c"] = np.array(s.get_field("c"), dtype=np.float64)
67 res[n + "_A_u"] = np.array(s.get_u(), dtype=np.float64)
68 s = make(pf, cls, -1.0, "inflow", blk, dist)
69 cen = []
70 for _ in range(10):
71 s.step()
72 cen.append(s.diagnostics.outflow_backflow()["max_reverse"])
73 res[n + "_B_census"] = np.array(cen)
74 s = make(pf, cls, -1.0, "outflow", blk, dist)
75 for _ in range(60):
76 s.step()
77 res[n + "_C_u"] = np.array(s.get_u(), dtype=np.float64)
78 return res
79
80
81def main():
82 try:
83 import peclet.flow as pf
84 from mpi4py import MPI
85 except ImportError as e:
86 print(f"SKIP: {e}")
87 return 77
88 if not hasattr(pf, "mpi_block"):
89 print("SKIP: peclet.flow built without PECLET_FLOW_MPI")
90 return 77
91 comm = MPI.COMM_WORLD
92 if comm.size < 2:
93 print("SKIP: run under mpirun with np >= 2")
94 return 77
95 (ox, oy, oz), (lx, ly, lz) = pf.mpi_block(NX, NY, NZ)
96 blk = (ox, oy, oz, lx, ly, lz)
97 xsplit = comm.allreduce(int((oy, oz, ly, lz) == (0, 0, NY, NZ)), op=MPI.MIN)
98 owners = comm.allreduce(int(ox + lx == NX), op=MPI.SUM)
99
100 local = run_cases(pf, blk, True)
101 parts = comm.gather((blk, local), root=0)
102 ok = True
103 if comm.rank == 0:
104 ref = run_cases(pf, (0, 0, 0, NX, NY, NZ), False) # the serial reference, on rank 0 alone
105 try:
106 assert xsplit == 1 and owners == 1, (
107 f"np {comm.size}: the decomposition is not an x-only split with one owner of +x "
108 f"(xsplit {xsplit}, owners {owners})")
109 line = [f"np {comm.size}"]
110 for k in sorted(ref):
111 if k.endswith("census"):
112 d = float(np.abs(parts[0][1][k] - ref[k]).max())
113 else:
114 g = np.zeros_like(ref[k])
115 for (bx, by, bz, bl, bm, bn), p in parts:
116 g[bx:bx + bl, by:by + bm, bz:bz + bn] = p[k]
117 d = float(np.abs(g - ref[k]).max())
118 if k.endswith("_A_c"):
119 cdev = float(np.abs(g - 1.0).max())
120 assert cdev <= TOL_C, f"{k}: distributed c = 1 moved by {cdev:.3e}"
121 line.append(f"{k} {d:.1e}")
122 assert d <= TOL_SERIAL, f"{k}: distributed vs serial {d:.3e} > {TOL_SERIAL:.0e}"
123 for n in ("SolverColocated", "Solver"):
124 cdev = float(np.abs(ref[n + "_A_c"] - 1.0).max())
125 assert cdev <= TOL_C, f"{n}_A_c: serial c = 1 moved by {cdev:.3e}"
126 print(" " + " ".join(line))
127 except AssertionError as e:
128 print(f"FAIL: {e}")
129 ok = False
130 ok = comm.bcast(ok, root=0)
131 if not ok:
132 return 1
133 if comm.rank == 0:
134 print("OK: the high-side face plane is kept on its owning rank only; distributed == serial")
135 return 0
136
137
138if __name__ == "__main__":
139 sys.exit(main())
make(pf, cls, u_in, low, blk, dist)