flow 0.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
collocated_zh_hybrid.py
Go to the documentation of this file.
1"""Z&H drag for the collocated cutcell-ghost HYBRID modes (set_face_interp 9/10): mode-0's
2aperture projection with the directional gpCenterGrad -grad(P)/cell correction (9), plus the
3open-centroid wall-aware constraint quadrature (10). Question: how much of the mode-0
4first-order drag was the O(1/h) gradient defect alone (9), and does the a-priori-O(h^2)
5open-centroid flux quadrature finally pay off once paired with a telescoping 2nd-order force
6(10)? Baselines: mode-0 +1.00/+0.69/+0.60/+0.40/+0.30 %, ghost (1,2)
7-0.175/-0.084/-0.056/-0.029/-0.018 % at N=32..128."""
8import os
9import sys
10import time
11
12sys.path.insert(0, os.path.abspath(os.environ.get("SDFLOW_BUILD", "build_cuda2")))
13import numpy as np
14from peclet import flow
15
16
17def lattice_sdf(N, phi=0.125):
18 R = (3 * phi / (4 * np.pi)) ** (1 / 3) * N
19 g = np.arange(N) + 0.5
20 X, Y, Z = np.meshgrid(g, g, g, indexing="ij")
21 dx = X - 0.5 * N
22 dx -= N * np.round(dx / N)
23 dy = Y - 0.5 * N
24 dy -= N * np.round(dy / N)
25 dz = Z - 0.5 * N
26 dz -= N * np.round(dz / N)
27 return np.sqrt(dx * dx + dy * dy + dz * dz) - R, R
28
29
30def drag(N, mode, mu=0.1, F=1e-3, dt=80.0, warm_tol=1e-7, tail=40, max_steps=4000):
31 sdf, R = lattice_sdf(N)
32 s = flow.SolverColocated(N, N, N)
33 s.set_rho(1.0)
34 s.set_mu(mu)
35 s.set_dt(dt)
36 s.set_body_force(F, 0, 0)
37 s.set_advection(False)
38 s.set_velocity_solver_params(200)
39 s.set_pressure_multigrid(True, levels=max(2, int(np.log2(N)) - 1))
40 s.set_pressure_pcg(True, 400, 1e-10)
41 if mode:
42 s.set_face_interp(mode)
43 s.set_solid(sdf, cutcell_pressure=True, pressure_coarse="rediscretized")
44 prev, warm, um = 0.0, None, []
45 for it in range(max_steps):
46 s.step()
47 m = float(s.get_u().mean())
48 um.append(m)
49 if warm is None:
50 if it % 10 == 9:
51 if it > 10 and abs(m - prev) < warm_tol * (abs(m) + 1e-30):
52 warm = it
53 prev = m
54 elif it - warm >= tail:
55 break
56 K = F * N**3 / (6 * np.pi * mu * R * np.mean(um[-tail:]))
57 return K, s.last_pressure_iterations()
58
59
60if __name__ == "__main__":
61 kref = 4.2920
62 M0 = {32: +1.004, 48: +0.685, 64: +0.598, 96: +0.397, 128: +0.299}
63 MG = {32: -0.175, 48: -0.084, 64: -0.056, 96: -0.029, 128: -0.018}
64 print(f"Z&H K={kref}. Collocated hybrids: 9 = aperture proj + gpCenterGrad;"
65 f" 10 = + open-centroid flux quadrature.", flush=True)
66 print(f"{'N':>4} | {'mode0':>7} | {'ghost':>7} | {'hyb9 err%':>9} {'ord':>6} |"
67 f" {'hyb10 err%':>10} {'ord':>6}", flush=True)
68 prev = {}
69 for N in (32, 48, 64, 96, 128):
70 t0 = time.time()
71 K9, i9 = drag(N, 9)
72 K10, i10 = drag(N, 10)
73 e9 = 100 * (K9 - kref) / kref
74 e10 = 100 * (K10 - kref) / kref
75 o9 = np.log(abs(prev["9"]) / abs(e9)) / np.log(N / prev["N"]) if prev else float("nan")
76 o10 = np.log(abs(prev["10"]) / abs(e10)) / np.log(N / prev["N"]) if prev else float("nan")
77 print(f"{N:>4} | {M0[N]:>+7.3f} | {MG[N]:>+7.3f} | {e9:>+9.3f} {o9:>6.2f} |"
78 f" {e10:>+10.3f} {o10:>6.2f} ({time.time() - t0:.0f}s)", flush=True)
79 prev = {"9": e9, "10": e10, "N": N}
drag(N, mode, mu=0.1, F=1e-3, dt=80.0, warm_tol=1e-7, tail=40, max_steps=4000)