flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
gsv56.py
Go to the documentation of this file.
1"""G-SV5 / G-SV6 instrument for the sharp viscous interface (doc/vof_sharp_viscous_stress.md ยง11,
2WO-5): a fixed number of production steps, the final fields written to one .npz, one summary line.
3
4 python gsv56.py --case p1 --dh 16 --steps 20 --mode sharp --out p1_sharp_np1.npz
5 mpirun --bind-to none -np 4 python gsv56.py ... (distributed; rank 0 writes the gathered fields)
6
7Cases:
8 p1 the P1 bubble (Loisy E1: Ar 29.9, Bo 2, rho ratio 1e-3, mu ratio 1e-2, triply periodic box
9 L = 4d, from rest, C = 1 is the gas) as validation/highratio/pec/run_hr_scheme.py sets it up
10 (VoF momentum, upwind scheme, CSF, PCG 1e-8), dt = 0.25 min(cfl_dt, capillary_dt) per step.
11 wall the G-SV1 geometry class: unit box, no-slip walls on all six faces, a planar interface with
12 normal n(22.5, 22.5 deg) through the centre offset 0.3 h along n, mu ratio 1e-2 (C = 1 the
13 low phase), uniform rho, Stokes (advection off), a body force 0.1 along the tangent t1,
14 dt at D_hi = mu_hi dt/(rho h^2) = 100. --n cells per axis (default 24).
15--mu-uniform: mu = LinearMix(C) with equal phase viscosities (the closure stays; G-SV5 uniform mu).
16--sigma S (wall): surface tension S; any S > 0 sets the planes' interfacial threshold to the CSF
17wisp guard (1e-8) instead of 0 (the sigma = 0 default of VofCurvature::interfaceEps).
18Compare two .npz with python gsv56.py --compare A.npz B.npz (prints max|diff| per field and
19BITWISE / DIFFER).
20"""
21import argparse
22import hashlib
23import math
24import sys
25
26import numpy as np
27
28from gsv1 import tie_init, tie_read, tie_str # G-SV12 owner-tie census (WO-9.4)
29
30
31def parse():
32 p = argparse.ArgumentParser()
33 p.add_argument("--case", choices=("p1", "wall"))
34 p.add_argument("--dh", type=int, default=16)
35 p.add_argument("--n", type=int, default=24)
36 p.add_argument("--steps", type=int, default=20)
37 p.add_argument("--mode", choices=("sharp", "averaged"), default="averaged")
38 p.add_argument("--mu-uniform", action="store_true")
39 p.add_argument("--sigma", type=float, default=0.0,
40 help="wall case: surface tension (a tiny value puts the planes on the CSF wisp threshold)")
41 p.add_argument("--out")
42 p.add_argument("--compare", nargs=2)
43 return p.parse_args()
44
45
46def compare(a, b):
47 A, B = np.load(a), np.load(b)
48 worst, same = {}, True
49 for k in ("C", "u", "v", "w", "p"):
50 d = float(np.max(np.abs(A[k] - B[k])))
51 worst[k] = d
52 same = same and np.array_equal(A[k], B[k])
53 print(("BITWISE " if same else "DIFFER ") + " ".join(f"{k}={v:.3e}" for k, v in worst.items()))
54 return 0 if same else 1
55
56
57def sphere_fractions(n, c, r, sub=8):
58 off = (np.arange(sub) + 0.5) / sub
59 g = (np.arange(n)[:, None] + off[None, :]).ravel()
60 out = np.zeros((n, n, n))
61 lo, hi = int(math.floor(c - r)) - 1, int(math.ceil(c + r)) + 1
62 for k in range(max(lo, 0), min(hi, n)):
63 zs = k + off
64 X, Y, Z = np.meshgrid(g, g, zs, indexing="ij")
65 ins = ((X - c) ** 2 + (Y - c) ** 2 + (Z - c) ** 2 < r * r).astype(np.float64)
66 out[:, :, k] = ins.reshape(n, sub, n, sub, sub).mean(axis=(1, 3, 4))
67 return np.asfortranarray(out)
68
69
70def plane_fractions(n, nrm, x0, sub=8):
71 """Cell fractions of the half-space nrm.(x - x0) < 0, x in cells, sub^3 samples."""
72 off = (np.arange(sub) + 0.5) / sub
73 g = (np.arange(n)[:, None] + off[None, :]).ravel()
74 out = np.zeros((n, n, n))
75 for k in range(n):
76 X, Y, Z = np.meshgrid(g, g, k + off, indexing="ij")
77 ins = (nrm[0] * (X - x0[0]) + nrm[1] * (Y - x0[1]) + nrm[2] * (Z - x0[2]) < 0).astype(np.float64)
78 out[:, :, k] = ins.reshape(n, sub, n, sub, sub).mean(axis=(1, 3, 4))
79 return np.asfortranarray(out)
80
81
82def main():
83 a = parse()
84 if a.compare:
85 sys.exit(compare(*a.compare))
86 import peclet.flow as pf
87 comm = None
88 try:
89 from mpi4py import MPI
90 if MPI.COMM_WORLD.Get_size() > 1:
91 comm = MPI.COMM_WORLD
92 except ImportError:
93 pass
94 n = round(4 * a.dh) if a.case == "p1" else a.n
95 if comm is None:
96 org, siz = (0, 0, 0), (n, n, n)
97 else:
98 org, siz = pf.mpi_block(n, n, n)
99 sl = tuple(slice(o, o + m) for o, m in zip(org, siz))
100
101 if a.case == "p1":
102 AR, BO, RR, MR = 29.9, 2.0, 1e-3, 1e-2
103 D, RHO_L, G = 1.0e-3, 1000.0, 9.81
104 RHO_G = RR * RHO_L
105 MU_L = math.sqrt(RHO_L * (RHO_L - RHO_G) * G * D ** 3) / AR
106 MU_G = MU_L if a.mu_uniform else MR * MU_L
107 SIGMA = (RHO_L - RHO_G) * G * D ** 2 / BO
108 L = 4 * D
109 h = L / n
110 s = pf.Solver(siz, extent=(L, L, L)) if comm is None else \
111 pf.Solver(siz, extent=(L, L, L), global_cells=(n, n, n))
112 if comm is not None:
113 s.init_mpi(n, n, n)
114 s.set_rho(RHO_L)
115 s.set_mu(MU_L)
116 s.set_pressure_geometry(np.full(siz, 1.0, order="F"))
117 s.enable_vof()
118 s.set_property_model("rho", "linear", "C", [RHO_L, RHO_G - RHO_L])
119 s.set_property_model("mu", "linear", "C", [MU_L, MU_G - MU_L])
120 s.set_advection(True)
121 s.set_surface_tension(SIGMA)
122 s.set_pressure_pcg(True, 800, 1e-8)
123 Cg = sphere_fractions(n, 0.5 * L / h, 0.5 * D / h)
124 s.set_vof(np.asfortranarray(Cg[sl]))
125 s.enable_vof_momentum(RHO_L, RHO_G)
126 alpha = float(np.mean(Cg))
127 rho_av = RHO_L + (RHO_G - RHO_L) * alpha
128 s.set_property_model("force_x", "linear", "C", [-(RHO_L - rho_av) * G, -(RHO_G - RHO_L) * G])
129 s.set_superficial_velocity(True, "x", 0.0)
130 else:
131 L, h = 1.0, 1.0 / n
132 MU_HI, MU_LO = 1.0, (1.0 if a.mu_uniform else 1e-2)
133 s = pf.Solver(siz, extent=(L, L, L)) if comm is None else \
134 pf.Solver(siz, extent=(L, L, L), global_cells=(n, n, n))
135 if comm is not None:
136 s.init_mpi(n, n, n)
137 for f in ("-x", "+x", "-y", "+y", "-z", "+z"):
138 s.set_domain_bc(f, "wall")
139 s.set_rho(1.0)
140 s.set_mu(MU_HI)
141 s.set_pressure_geometry(np.full(siz, 1.0, order="F"))
142 s.enable_vof()
143 b, g = math.radians(22.5), math.radians(22.5)
144 nrm = (math.cos(b) * math.cos(g), math.cos(b) * math.sin(g), math.sin(b))
145 x0 = [0.5 * n + 0.3 * c for c in nrm]
146 s.set_vof(np.asfortranarray(plane_fractions(n, nrm, x0)[sl]))
147 s.set_property_model("mu", "linear", "C", [MU_HI, MU_LO - MU_HI])
148 s.set_advection(False)
149 s.set_body_force((-0.1 * math.sin(g), 0.1 * math.cos(g), 0.0))
150 s.set_pressure_pcg(True, 800, 1e-10)
151 if a.sigma > 0:
152 s.set_surface_tension(a.sigma)
153 if a.mode == "sharp" or hasattr(s, "set_vof_viscous_interface"): # absent on a pre-sharp build
154 s.set_vof_viscous_interface(a.mode) # explicit both ways: the default 'auto' is sharp here
155 cen = {"band_cells": 0, "virtual_cells": 0, "film_cells": 0, "fit_fallbacks": 0}
156 tie = tie_init()
157 for _ in range(a.steps):
158 if a.case == "p1":
159 lm = s.vof_step_limits()
160 s.set_dt(0.25 * min(lm["cfl_dt"], lm["capillary_dt"]))
161 else:
162 s.set_dt(100.0 * h * h / MU_HI)
163 s.step()
164 if hasattr(s.diagnostics, "vof_viscous_interface_census"): # absent on a pre-sharp build
165 c = s.diagnostics.vof_viscous_interface_census()
166 for k in cen:
167 cen[k] += c[k]
168 tie_read(s, tie, comm)
169 if comm is not None:
170 for k in cen:
171 cen[k] = comm.allreduce(cen[k])
172 loc = {"C": s.get_vof(), "u": s.get_u(), "v": s.get_v(), "w": s.get_w(), "p": s.get_field("p")}
173 if comm is not None:
174 parts = comm.gather((sl, loc), root=0)
175 if comm.Get_rank() != 0:
176 return
177 glob = {k: np.zeros((n, n, n), order="F") for k in loc}
178 for psl, pl in parts:
179 for k in loc:
180 glob[k][psl] = pl[k]
181 loc = glob
182 np.savez(a.out, **loc)
183 hs = hashlib.sha256(b"".join(np.ascontiguousarray(loc[k]).tobytes() for k in sorted(loc))).hexdigest()[:16]
184 npr = 1 if comm is None else comm.Get_size()
185 print(f"GSV56 case={a.case} mode={a.mode} mu_uniform={a.mu_uniform} np={npr} steps={a.steps} "
186 f"census/step band={cen['band_cells'] / a.steps:.1f} virt={cen['virtual_cells'] / a.steps:.1f} "
187 f"film={cen['film_cells'] / a.steps:.1f} fallbacks={cen['fit_fallbacks']} {tie_str(tie)} "
188 f"max|u|={float(np.max(np.abs(loc['u']))):.6e} sha={hs}")
189
190
191if __name__ == "__main__":
192 main()
plane_fractions(n, nrm, x0, sub=8)
Definition gsv56.py:70
main()
Definition gsv56.py:82
compare(a, b)
Definition gsv56.py:46
sphere_fractions(n, c, r, sub=8)
Definition gsv56.py:57
parse()
Definition gsv56.py:31
static std::vector< double > slice(const std::vector< double > &g, int ox, int oy, int oz, int lnx, int lny, int lnz)
static double mean(const std::vector< double > &a)