flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
probe_conserve.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""Case A G5 diagnosis: does ONE velocity-form step conserve momentum when every force sums to zero?
3
4With g = 0, sigma = 0 and the superficial closure off, every force of the staggered velocity-form
5predictor is a periodic divergence (pressure, viscous flux), so the predictor + projection must
6leave PRED = sum_f rho^n_f (u^{n+1} - u^n) / dt = 0 to the solver tolerances (the density change
7of the step is a separate, physical term and is not in PRED). Initial field: a divergence-free
8shear u_x = A sin(2 pi y / L) + a uniform U, through the case-A sphere (rho and mu ratios
9selectable). PRED is reported in units of <rho> g (g = 9.81, as in probe_budget.py).
10
11Usage: OMP_NUM_THREADS=4 OMP_PROC_BIND=false PYTHONPATH=<build> \\
12 python tests/study/case_a/probe_conserve.py --n 32
13"""
14
15import argparse
16import os
17import sys
18
19import numpy as np
20
21sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
22import run_case_a as ca
23
24
25def one(n, rho_ratio, mu_ratio, stress, steps, adv, dtf, grav=False, sigma="none"):
26 L = ca.L_OVER_D * ca.D
27 h = L / n
28 rl, rg, ml, mg = ca.RHO_L, rho_ratio * ca.RHO_L, ca.MU_L, mu_ratio * ca.MU_L
29 s = ca.pf.Solver((n, n, n), extent=(L, L, L))
30 s.set_rho(rl)
31 s.set_mu(ml)
32 s.set_pressure_geometry(np.full((n,) * 3, 1.0, order="F"))
33 s.enable_vof()
34 C = ca.sphere_fractions(n, 0.5 * L / h, 0.5 * ca.D / h)
35 s.set_vof(C)
36 s.set_property_model("rho", "linear", "C", [rl, rg - rl])
37 s.set_property_model("mu", "linear", "C", [ml, mg - ml])
38 s.set_viscous_stress(stress)
39 if adv:
40 s.set_advection(True)
41 if sigma != "none":
42 s.set_surface_tension(ca.SIGMA)
43 s.set_pressure_pcg(True, 800, 1e-8)
44 if sigma == "blk":
45 s.set_vof(np.zeros((n,) * 3, order="F"))
46 s.enable_vof_blocks([(0.5 * L / h,) * 3 + (0.5 * ca.D / h,)])
47 s.enable_vof_block_csf()
48 C = np.array(s.get_vof())
49 if grav:
50 al = C.mean()
51 rav_ = rl + (rg - rl) * al
52 s.set_property_model(
53 "force_x", "linear", "C", [-(rl - rav_) * ca.G, -(rg - rl) * ca.G]
54 )
55 U, A = 0.02, 0.07
56 dt = dtf * 0.25 * h / (U + A)
57 s.set_dt(dt)
58 y = (np.arange(n) + 0.5) * h
59 ux = np.asfortranarray(
60 np.broadcast_to(
61 (U + A * np.sin(2 * np.pi * y / L))[None, :, None], (n, n, n)
62 ).copy()
63 )
64 s.set_velocity(0, ux)
65 s.update_properties()
66 rav = rl + (rg - rl) * C.mean()
67 out = []
68 for _ in range(steps):
69 Cn = np.array(s.get_vof())
70 un = [np.array(g()) for g in (s.get_u, s.get_v, s.get_w)]
71 s.step()
72 u1 = [np.array(g()) for g in (s.get_u, s.get_v, s.get_w)]
73 rho = rl + (rg - rl) * Cn
74 pr = []
75 for c in range(3):
76 rf = 0.5 * (rho + np.roll(rho, 1, axis=c)) # get_u()[i] = face (i-1, i)
77 pr.append(float(np.sum(rf * (u1[c] - un[c]))) / dt / n**3 / (rav * ca.G))
78 out.append(pr)
79 o = np.array(out)
80 return (
81 f"CONSERVE n={n} rho_ratio={rho_ratio:g} mu_ratio={mu_ratio:g} stress={stress} "
82 f"adv={adv} grav={grav} sigma={sigma} dt={dt:.3e} steps={steps} PRED/<rho>g first=({o[0, 0]:+.3e},{o[0, 1]:+.3e},"
83 f"{o[0, 2]:+.3e}) mean_x={o[:, 0].mean():+.3e} max|.|={np.abs(o).max():.3e}"
84 )
85
86
87def main():
88 p = argparse.ArgumentParser()
89 p.add_argument("--n", type=int, default=32)
90 p.add_argument("--steps", type=int, default=5)
91 p.add_argument("--set", default="base", choices=("base", "forces"))
92 a = p.parse_args()
93 cfgs = [
94 (1.0, 1.0, "laplacian", False, 1.0),
95 (0.1, 1.0, "laplacian", False, 1.0),
96 (1.0, 0.1, "laplacian", False, 1.0),
97 (1.0, 0.1, "full", False, 1.0),
98 (0.1, 0.1, "laplacian", False, 1.0),
99 (0.1, 0.1, "full", False, 1.0),
100 (0.1, 0.1, "full", False, 0.5),
101 (0.1, 0.1, "full", True, 1.0),
102 ]
103 if (
104 a.set == "forces"
105 ): # the case-A forces one at a time (capillary dt binds with sigma)
106 for grav, sig, dtf in (
107 (True, "none", 0.5),
108 (False, "single", 0.1),
109 (False, "blk", 0.1),
110 (True, "blk", 0.1),
111 ):
112 print(
113 one(a.n, 0.1, 0.1, "full", a.steps, False, dtf, grav, sig), flush=True
114 )
115 return
116 for rr, mr, st, adv, dtf in cfgs:
117 print(one(a.n, rr, mr, st, a.steps, adv, dtf), flush=True)
118
119
120if __name__ == "__main__":
121 main()
one(n, rho_ratio, mu_ratio, stress, steps, adv, dtf, grav=False, sigma="none")