flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
state_hash.py
Go to the documentation of this file.
1#!/usr/bin/env python
2"""Byte gate for the peclet.flow public surface: SHA-256 of the final state of one fixed-seed run
3per public entry path, at one thread.
4
5Run: OMP_NUM_THREADS=1 OMP_PROC_BIND=false PYTHONPATH=<build> python tests/regression/state_hash.py
6 OMP_NUM_THREADS=1 PYTHONPATH=<build> mpirun -np 2 python tests/regression/state_hash.py mpi
7
8Entry paths: the staggered periodic sphere bed (set_solid, cut-cell pressure), the collocated
9solver under each of its four schemes ('ghost', 'gauge-exact', 'plain', 'embed'), the collocated
10ADVECTING paths -- periodic (explicit SOU/Koren) and inflow/outflow (implicit FOU + deferred
11correction), the two branches that read the projected face field as the advecting velocity and the
12only ones the four scheme cases above miss (they are Stokes: set_advection(False)) -- an
13inflow/wall/outflow channel with advection, a VoF droplet under surface tension, Boussinesq
14scalar transport, the porous (volume-averaged) continuity with implicit drag, an analytic scene
15with a moving instance (set_instance_motion + set_instance_transform + rebuild_geometry), the
16collocated variable-rho balanced-force projection under the single-rank MG-PCG, and the
17distributed step at np = 2 (flow.mpi_block + init_mpi). Each prints one line: the case, the
18SHA-256 over the per-field hashes, and the first 12 hex digits of each field's hash (get_u/v/w/p,
19plus get_vof and the scalar where present), taken over the contiguous float64 bytes.
20
21Every run here is reproducible run-to-run at one thread (verified 2026-09-10 on host-openmp); a
22refactor of the bindings or the solver that is not meant to change numerics must reproduce every
23line bitwise (QUALITY_PLAN F gate). The hashes recorded at each package-F milestone are in the
24commit messages.
25"""
26import hashlib
27import sys
28
29import numpy as np
30
31import peclet.flow as pf
32
33CASES = ("staggered_bed", "colocated_ghost", "colocated_gauge_exact", "colocated_plain",
34 "colocated_embed", "colocated_advect", "colocated_advect_bc", "channel", "vof_droplet",
35 "scalar", "porous", "scene_moving", "colocated_bfp")
36
37
38def sha(a):
39 return hashlib.sha256(np.ascontiguousarray(np.asarray(a), dtype=np.float64).tobytes()).hexdigest()
40
41
42def packing_sdf(N, rfrac=0.18):
43 R = rfrac * N
44 cs = np.array([0.25 * N, 0.75 * N])
45 xs = np.arange(N) + 0.5
46 X, Y, Z = np.meshgrid(xs, xs, xs, indexing="ij")
47 best = np.full((N, N, N), 1e30)
48 for sx in cs:
49 for sy in cs:
50 for sz in cs:
51 dx = X - sx; dx -= N * np.round(dx / N)
52 dy = Y - sy; dy -= N * np.round(dy / N)
53 dz = Z - sz; dz -= N * np.round(dz / N)
54 best = np.minimum(best, np.sqrt(dx * dx + dy * dy + dz * dz) - R)
55 return np.asfortranarray(best)
56
57
58def sphere_fractions(shape, R, c, sub=4):
59 n = np.array(shape)
60 off = (np.arange(sub) + 0.5) / sub
61 out = np.zeros(shape)
62 for ox in off:
63 for oy in off:
64 for oz in off:
65 X, Y, Z = np.meshgrid(np.arange(n[0]) + ox, np.arange(n[1]) + oy,
66 np.arange(n[2]) + oz, indexing="ij")
67 out += ((X - c[0]) ** 2 + (Y - c[1]) ** 2 + (Z - c[2]) ** 2 <= R * R)
68 return np.asfortranarray(out / sub ** 3)
69
70
71def stokes_bed(cls, N=16, steps=5, scheme=None):
72 s = cls(N, N, N)
73 s.set_rho(1.0); s.set_mu(0.1); s.set_dt(60.0); s.set_body_force((1e-3, 0.0, 0.0))
74 s.set_advection(False)
75 s.diagnostics.set_velocity_solver_params(80)
76 s.set_pressure_multigrid(True, 3)
77 s.set_pressure_pcg(True, 200, 1e-9)
78 if scheme is not None:
79 s.set_collocated_scheme(scheme)
80 s.set_solid(packing_sdf(N), cutcell_pressure=True)
81 for _ in range(steps):
82 s.step()
83 return s
84
85
87 return stokes_bed(pf.Solver)
88
89
91 return stokes_bed(pf.SolverColocated, scheme="ghost")
92
93
95 return stokes_bed(pf.SolverColocated, scheme="gauge-exact")
96
97
99 return stokes_bed(pf.SolverColocated, scheme="plain")
100
101
103 return stokes_bed(pf.SolverColocated, scheme="embed")
104
105
106def _tg_state(N, nz, U0=1.0):
107 """A divergence-free (to O(h^2)) cell-centred Taylor-Green pair: a non-trivial advecting
108 field with no body force, so the hash reflects the advection + projection only."""
109 k = 2.0 * np.pi / N
110 ix = np.arange(N)
111 X, Y = np.meshgrid(ix, ix, indexing="ij")
112 u = np.repeat((U0 * np.sin(k * X) * np.cos(k * Y))[:, :, None], nz, axis=2)
113 v = np.repeat((-U0 * np.cos(k * X) * np.sin(k * Y))[:, :, None], nz, axis=2)
114 return (np.asfortranarray(u), np.asfortranarray(v),
115 np.asfortranarray(np.zeros((N, N, nz))))
116
117
119 """Collocated + advection, triply periodic: hasBc_ false, so advection is EXPLICIT (Koren in
120 the RHS) and the advecting velocity is the projected face field of the previous step."""
121 N, nz = 16, 8
122 s = pf.SolverColocated(N, N, nz)
123 s.set_rho(1.0); s.set_mu(0.05); s.set_dt(0.5)
124 s.set_advection(True); s.set_advection_scheme("koren")
125 s.diagnostics.set_velocity_solver_params(80)
126 s.set_pressure_pcg(True, 200, 1e-9)
127 s.set_solid(np.asfortranarray(np.full((N, N, nz), 1e3)), cutcell_pressure=True)
128 s.set_state(*_tg_state(N, nz))
129 for _ in range(5):
130 s.step()
131 return s
132
133
135 """Collocated + advection + domain BCs: hasBc_ true, so implicitAdv() holds and the IMPLICIT
136 FOU operator (buildAdvStencil) plus the explicit (SOU - FOU) deferred correction both read
137 the advecting velocity -- they must read the SAME one."""
138 nx, ny, nz = 24, 12, 8
139 s = pf.SolverColocated(nx, ny, nz)
140 s.set_rho(1.0); s.set_mu(0.05); s.set_dt(0.5)
141 s.set_advection(True); s.set_advection_scheme("koren")
142 s.set_domain_bc("-x", "inflow", (1.0, 0.0, 0.0))
143 s.set_domain_bc("+x", "outflow")
144 s.set_domain_bc("-y", "wall"); s.set_domain_bc("+y", "wall")
145 s.set_pressure_geometry(np.asfortranarray(np.full((nx, ny, nz), 10.0)))
146 s.set_pressure_pcg(True, 200, 1e-9)
147 for _ in range(5):
148 s.step()
149 return s
150
151
153 nx, ny, nz = 24, 12, 8
154 s = pf.Solver(nx, ny, nz)
155 s.set_rho(1.0); s.set_mu(0.05); s.set_dt(0.5)
156 s.set_advection(True); s.set_advection_scheme("koren")
157 s.set_domain_bc("-x", "inflow", (1.0, 0.0, 0.0))
158 s.set_domain_bc("+x", "outflow")
159 s.set_domain_bc("-y", "wall"); s.set_domain_bc("+y", "wall")
160 s.set_pressure_geometry(np.asfortranarray(np.full((nx, ny, nz), 10.0)))
161 s.set_pressure_pcg(True, 200, 1e-9)
162 for _ in range(5):
163 s.step()
164 return s
165
166
168 n, R = 16, 4.0
169 s = pf.Solver(n, n, n)
170 s.set_advection(False)
171 s.set_rho(1.0); s.set_mu(0.1)
172 s.set_pressure_geometry(np.asfortranarray(np.full((n, n, n), 10.0)))
173 s.set_pressure_pcg(True, 500, 1e-12)
174 s.enable_vof()
175 s.set_vof(sphere_fractions((n, n, n), R, (n / 2 + 0.13, n / 2 + 0.27, n / 2 + 0.11)))
176 s.set_property_model("rho", "linear", "C", [1.0, 0.0])
177 s.set_surface_tension(1.0)
178 s.set_dt(0.25 * s.capillary_dt())
179 for _ in range(5):
180 s.step()
181 return s
182
183
185 """Collocated variable-rho (V8) VoF drop at ratio 1000 under a constant curvature (an exact
186 equilibrium), the balanced-force projection ON (its V8 default) and the single-rank MG-PCG: the
187 one path where the resident PCG stops on the caller's stop reference (WO-X1, setStopReference)."""
188 n, R = 16, 4.0
189 s = pf.SolverColocated(n, n, n)
190 s.set_advection(False)
191 s.set_rho(1000.0); s.set_mu(0.1); s.set_dt(1.0)
192 s.set_pressure_geometry(np.asfortranarray(np.full((n, n, n), 10.0)))
193 s.enable_vof()
194 s.set_vof(sphere_fractions((n, n, n), R, (n / 2 + 0.13, n / 2 + 0.27, n / 2 + 0.11)))
195 s.set_property_model("rho", "linear", "C", [1.0, 999.0])
196 s.set_surface_tension(1.0)
197 s.diagnostics.set_vof_kappa_constant(1.0 / R)
198 s.set_dt(0.5 * s.capillary_dt())
199 s.set_balanced_force_projection(True)
200 s.set_pressure_pcg(True, 500, 1e-10)
201 for _ in range(5):
202 s.step()
203 return s
204
205
207 N, nz = 16, 4
208 s = pf.Solver(N, N, nz)
209 # pre-flip default (Stokes) kept; NS physics: CHANGELOG "advection default"
210 s.set_advection(False)
211 s.set_rho(1.0); s.set_mu(0.05); s.set_dt(8.0)
212 s.set_implicit_advection(True); s.diagnostics.set_outer_iterations(2)
213 for f in ("-x", "+x", "-y", "+y"):
214 s.set_domain_bc(f, "wall", (0.0, 0.0, 0.0))
215 s.set_pressure_geometry(np.asfortranarray(np.full((N, N, nz), 10.0)))
216 alpha = 0.05 / 0.71
217 s.add_scalar("T", diffusivity=alpha, scheme="koren", iters=50)
218 s.set_scalar_bc("T", "-x", "dirichlet", 1.0); s.set_scalar_bc("T", "+x", "dirichlet", 0.0)
219 s.set_scalar_bc("T", "-y", "neumann", 0.0); s.set_scalar_bc("T", "+y", "neumann", 0.0)
220 coeff = 1e4 * 0.05 ** 2 / (0.71 * N ** 3)
221 s.set_property_model("force_y", "boussinesq", "T", [1.0, coeff, 1.0, 0.5])
222 x = np.arange(N)
223 T0 = np.repeat((1.0 - (x + 0.5) / N)[:, None, None], N, 1).repeat(nz, 2)
224 s.set_field("T", np.asfortranarray(T0.astype(np.float64)))
225 for _ in range(5):
226 s.step()
227 return s
228
229
231 N = 16
232 s = pf.Solver(N, N, N)
233 s.set_rho(1.0); s.set_mu(1.0); s.set_dt(0.5)
234 s.set_advection(False)
235 s.set_pressure_geometry(np.asfortranarray(np.full((N, N, N), 10.0)))
236 s.enable_drag()
237 s.set_porous_continuity(True)
238 s.set_field("eps", np.asfortranarray(np.full((N, N, N), 0.6)))
239 s.set_field("drag_beta", np.asfortranarray(np.full((N, N, N), 4.0)))
240 s.diagnostics.exchange_field("eps"); s.diagnostics.exchange_field("drag_beta")
241 s.sync_porous_prev()
242 s.set_body_force((0.0, 0.0, 0.2))
243 for _ in range(5):
244 s.step()
245 return s
246
247
249 N = 16
250 s = pf.Solver(N, N, N)
251 s.set_rho(1.0); s.set_mu(0.1); s.set_dt(2.0); s.set_body_force((1e-3, 0.0, 0.0))
252 s.set_advection(False)
253 s.set_pressure_pcg(True, 200, 1e-9)
254 node_ints = np.array([1, -1, -1], dtype=np.int32) # kSphere
255 node_reals = np.zeros(16); node_reals[0] = 3.0; node_reals[14] = 1.0; node_reals[15] = 1.0
256 ii = np.array([[0, -1]], dtype=np.int32)
257 ir = np.zeros((1, 18)); ir[0, 0:3] = (N / 2, N / 2, N / 2); ir[0, 6] = 1.0; ir[0, 7] = 1.0
258 s.set_scene(node_ints, node_reals, ii.ravel(), ir.ravel(), periodic=True)
259 s.set_instance_transform(0, [N / 2, N / 2, N / 2], [0.0, 0.0, 0.0, 1.0])
260 s.set_instance_motion(0, lin_vel=[0.02, 0.0, 0.0], ang_vel=[0.0, 0.0, 0.005])
261 s.set_solid_from_scene(True)
262 for k in range(5):
263 if k == 3:
264 s.set_instance_transform(0, [N / 2 + 0.1, N / 2, N / 2], [0.0, 0.0, 0.0, 1.0])
265 s.rebuild_geometry()
266 s.step()
267 return s
268
269
270def fields(s):
271 d = {"u": s.get_u(), "v": s.get_v(), "w": s.get_w(), "p": s.get_p()}
272 if s.has_field("C"):
273 d["C"] = s.get_vof()
274 if s.has_field("T"):
275 d["T"] = s.get_field("T")
276 return d
277
278
279def report(name, d):
280 h = hashlib.sha256()
281 parts = []
282 for k in sorted(d):
283 hk = sha(d[k]); h.update(hk.encode()); parts.append(f"{k}={hk[:12]}")
284 print(f"{name:24s} {h.hexdigest()} " + " ".join(parts), flush=True)
285
286
288 from mpi4py import MPI
289 comm = MPI.COMM_WORLD
290 N = 16
291 gsdf = packing_sdf(N)
292 origin, bsize = pf.mpi_block(N, N, N)
293 ox, oy, oz = origin; lnx, lny, lnz = bsize
294 lsdf = np.asfortranarray(gsdf[ox:ox + lnx, oy:oy + lny, oz:oz + lnz])
295 s = pf.Solver(lnx, lny, lnz)
296 s.init_mpi(N, N, N)
297 s.set_rho(1.0); s.set_mu(0.1); s.set_dt(60.0); s.set_body_force((1e-3, 0.0, 0.0))
298 s.set_advection(False)
299 s.diagnostics.set_velocity_solver_params(80)
300 s.set_pressure_multigrid(True, 3)
301 s.set_pressure_pcg(True, 200, 1e-9)
302 s.set_solid(lsdf, cutcell_pressure=True)
303 for _ in range(5):
304 s.step()
305 loc = {k: np.ascontiguousarray(v, dtype=np.float64) for k, v in fields(s).items()}
306 allf = comm.gather(loc, root=0)
307 if comm.Get_rank() == 0:
308 d = {k: np.concatenate([np.asarray(r[k]).ravel() for r in allf]) for k in loc}
309 report(f"mpi_np{comm.Get_size()}", d)
310
311
312if __name__ == "__main__":
313 want = [a for a in sys.argv[1:] if not a.startswith("-")] or list(CASES)
314 for c in want:
315 if c == "mpi":
316 case_mpi()
317 else:
318 report(c, fields(globals()["case_" + c]()))
case_colocated_bfp()
case_scene_moving()
report(name, d)
case_colocated_advect()
case_colocated_advect_bc()
case_colocated_embed()
packing_sdf(N, rfrac=0.18)
Definition state_hash.py:42
_tg_state(N, nz, U0=1.0)
case_staggered_bed()
Definition state_hash.py:86
stokes_bed(cls, N=16, steps=5, scheme=None)
Definition state_hash.py:71
case_colocated_gauge_exact()
Definition state_hash.py:94
case_colocated_ghost()
Definition state_hash.py:90
case_colocated_plain()
Definition state_hash.py:98
sphere_fractions(shape, R, c, sub=4)
Definition state_hash.py:58