flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
apriori1d.py
Go to the documentation of this file.
1"""A-priori operator test for doc/vof_momentum_second_order.md (numpy, host, seconds).
2
3One directional sweep of flow's mass-momentum-consistent update (src/vof/momentum_advect.hpp, the
4deviation form `u_new = u_old + dev/rho_new`) transporting a TRANSVERSE velocity v(x) through a sharp
5liquid slab at uniform Courant number a, periodic. In 1D the PLIC flux of a step is exact, so C is
6exact and v has the exact solution v0(x - T): every error printed is the momentum reconstruction's.
7
8Schemes: upwind (flow default) | muscl_mixed (flow's opt-in MinMod of the mixed CV velocity) |
9mc_mixed (MC of the mixed velocity, no cap) | muscl_mixed_capped (muscl_mixed + the mass-aware cap) |
10muscl_phase (the design: per-phase interface-biased MC slopes + the cap) | phase_central (Basilisk's
11interface-biased rule with an UNLIMITED central slope, no cap).
12
13Run: python -I apriori1d.py (prints the table quoted in the design note, section 2)
14"""
15import numpy as np
16
17A = 0.2
18
19
20def mm(p, q):
21 return np.where(p * q > 0, np.where(np.abs(p) < np.abs(q), p, q), 0.0)
22
23
24def mc(p, q):
25 s = np.where(p * q > 0, np.sign(p), 0.0)
26 return s * np.minimum(np.minimum(2 * np.abs(p), 2 * np.abs(q)), 0.5 * np.abs(p + q))
27
28
29def phase_est(dm, dp, f):
30 """Interface-biased slope estimate for the phase whose fraction is f (>= 1/2 = holds it)."""
31 fm, fp = np.roll(f, 1) >= 0.5, np.roll(f, -1) >= 0.5
32 est = np.where(fm & fp, 0.5 * (dm + dp), np.where(fm, dm, np.where(fp, dp, 0.0)))
33 return np.where(f >= 0.5, est, 0.0)
34
35
36def tvd(dm, dp, est):
37 s = np.where(dm * dp > 0, np.sign(dm), 0.0)
38 return s * np.minimum(np.minimum(2 * np.abs(dm), 2 * np.abs(dp)), np.abs(est))
39
40
41def run(N, ratio, scheme, v0, steps=None, slab=(0.3, 0.7), wisp=0.0):
42 h = 1.0 / N
43 x = (np.arange(N) + 0.5) * h
44 xl = np.arange(N) * h
45 C = np.clip((np.minimum(xl + h, slab[1]) - np.maximum(xl, slab[0])) / h, 0, 1)
46 rg, rl = 1.0, float(ratio)
47 v = v0(x) if callable(v0) else np.array(v0, dtype=float)
48 nst = steps if steps is not None else int(round(1.0 / (A * h)))
49 bites = 0
50 for _ in range(nst):
51 right = np.roll(C, -1) >= np.roll(C, 1) # liquid sits on the side of the fuller neighbour
52 F = np.where(right, np.minimum(A, C), np.maximum(0.0, C - (1 - A))) # + face, donor = cell
53 F = np.where((C <= wisp) | (C >= 1 - wisp), C * A, F)
54 dm, dp = v - np.roll(v, 1), np.roll(v, -1) - v
55 if scheme == 'upwind':
56 sl = sg = np.zeros_like(v)
57 elif scheme in ('muscl_mixed', 'muscl_mixed_capped'):
58 sl = sg = mm(dm, dp)
59 elif scheme == 'mc_mixed':
60 sl = sg = mc(dm, dp)
61 elif scheme == 'muscl_phase':
62 sl, sg = tvd(dm, dp, phase_est(dm, dp, C)), tvd(dm, dp, phase_est(dm, dp, 1 - C))
63 elif scheme == 'phase_central':
64 sl, sg = phase_est(dm, dp, C), phase_est(dm, dp, 1 - C)
65 if scheme.endswith('_capped') or scheme == 'muscl_phase':
66 # the cap of the note's section 4.3, 1D uniform a: the - face is inflow, no dilation
67 R = np.maximum(rg * (1 - C) + rl * C - (rg * (A - F) + rl * F), 0.0)
68 m = np.minimum(np.abs(dm), np.abs(dp))
69 ext = np.abs(0.5 * (1 - A) * (rg * (A - F) * sg + rl * F * sl))
70 th = np.where(ext > R * m, R * m / np.where(ext > 0, ext, 1.0), 1.0)
71 bites += int((th < 1).sum())
72 sl, sg = th * sl, th * sg
73 ul, ug = v + 0.5 * (1 - A) * sl, v + 0.5 * (1 - A) * sg
74 FM = np.roll(F, 1)
75 Cn = C + FM - F
76 rn = rg * (1 - Cn) + rl * Cn
77 dev = rg * ((A - FM) * (np.roll(ug, 1) - v) - (A - F) * (ug - v)) \
78 + rl * (FM * (np.roll(ul, 1) - v) - F * (ul - v))
79 v = v + dev / rn
80 C = Cn
81 return x, C, v, nst, bites
82
83
84def v0(s):
85 return np.sin(2 * np.pi * s) + 0.5 * np.cos(6 * np.pi * s)
86
87
88if __name__ == '__main__':
89 Ns = [32, 64, 128, 256, 512]
90 schemes = ['upwind', 'muscl_mixed', 'mc_mixed', 'muscl_mixed_capped', 'muscl_phase',
91 'phase_central']
92 print("L1 error of v after one period, a = 0.2; order = log2 of successive ratios")
93 for ratio in [1.0, 1e3, 1e4]:
94 for sch in schemes:
95 l1, bt = [], []
96 for N in Ns:
97 x, C, v, nst, b = run(N, ratio, sch, v0)
98 l1.append(np.abs(v - v0(x - nst * A / N)).mean())
99 bt.append(b)
100 print(f"ratio {ratio:5.0e} {sch:19s} " + " ".join(f"{q:.2e}" for q in l1) + " | ord "
101 + " ".join(f"{np.log2(l1[k] / l1[k + 1]):5.2f}" for k in range(len(Ns) - 1))
102 + (f" | cap bites {bt}" if sch.endswith('capped') or sch == 'muscl_phase' else ""))
103 print("noise: v = 1 + 1e-14 n(x), N = 64, 200 steps; identity: v = 1 exactly")
104 noise = 1e-14 * np.random.default_rng(7).standard_normal(64)
105 for ratio in [1e3, 1e4]:
106 for sch in schemes:
107 _, _, v, _, _ = run(64, ratio, sch, 1.0 + noise, steps=200)
108 _, _, w, _, _ = run(64, ratio, sch, np.ones(64), steps=200)
109 print(f"ratio {ratio:5.0e} {sch:19s} max|v-1| {np.abs(noise).max():.2e} -> "
110 f"{np.abs(v - 1).max():.2e} identity {np.abs(w - 1).max():.1e}")
mc(p, q)
Definition apriori1d.py:24
tvd(dm, dp, est)
Definition apriori1d.py:36
phase_est(dm, dp, f)
Definition apriori1d.py:29
run(N, ratio, scheme, v0, steps=None, slab=(0.3, 0.7), wisp=0.0)
Definition apriori1d.py:41
mm(p, q)
Definition apriori1d.py:20
static double mean(const std::vector< double > &a)