43 from stab
import blocks, rho_face
44 M.TAU_RULE =
"mixed-avg"
45 case = M.Case(N, ang, off, muA, 1.0,
"couette")
46 rf = rho_face(case, *blocks(case,
"facef",
"arith",
None,
None)[3:], rA, 1.0)
49 K2 = blocks(case,
"facef",
"arith",
None, {
"xx",
"yy",
"yu",
"xv"})[0]
50 Kc = blocks(case,
"facef",
"arith",
None, {
"xx",
"yy"})[0]
52 At, G, D, _, _ = blocks(case,
"sharp",
"facef",
"fit",
None)
54 K2 = blocks(case,
"arith",
"arith",
None, {
"xx",
"yy",
"yu",
"xv"})[0]
55 Kc = blocks(case,
"arith",
"arith",
None, {
"xx",
"yy"})[0]
56 At, G, D, _, _ = blocks(case,
"arith",
"arith",
None,
None)
59 _, _, _, uidx, mm = blocks(case,
"arith",
"arith",
None, {
"xx"})
62 Kcf = np.zeros((nv, nv))
63 Kcb = np.zeros((nv, nv))
66 f = case.frac_cell(i * h, j * h)
67 w = 2.0 * (f * muA + (1.0 - f) * 1.0) / h**2
68 for ip, im
in ((mm.iu(i + 1, j), mm.iu(i, j)), (mm.iv(i, j + 1), mm.iv(i, j))):
74 Kcf += w * np.outer(g, g)
75 if 1e-8 < f < 1.0 - 1e-8:
76 Kcb += w * np.outer(g, g)
77 chk = float(np.max(np.abs(Kcf - Kc)) / np.max(np.abs(Kc)))
78 assert chk < 1e-10, chk
82 dt = Dn * case.h**2 * (1.0
if muA < 1
else rA / muA)
85 P = np.eye(len(rf)) - Mi @ G @ np.linalg.pinv(D @ Mi @ G) @ D
86 for nm, K
in ((
"K2", K2), (
"K1", K1), (
"Kb", Kb)):
87 out[(nm, Dn)] = float(np.max(np.abs(np.linalg.eigvals(P @ np.linalg.solve(Mm + K, Mm + K - At)))))
97 from multiprocessing
import Pool
98 geoms = ((0.0, 0.6), (0.0, 0.2), (22.5, 0.0), (22.5, 0.3), (45.0, 0.3), (67.5, 0.5))
99 pairs = ((1e-3, 1e-4), (1e-2, 1e-3), (1e2, 1e3), (1e3, 1e4))
100 Ds = (0.1, 1.0, 10.0, 100.0, 1e4)
101 jobs = [(a.N, m, r, g, o, sch, Ds)
for sch
in (
"sharp",
"averaged")
for (m, r)
in pairs
for (g, o)
in geoms]
102 with Pool(a.procs)
as p:
103 res = p.map(_harness_job, jobs)
104 for (N, m, r, g, o, sch, _), out
in res:
105 print(
"HARN N=%d %-8s mu=%g rho=%g geom=(%g,%g) | %s" % (N, sch, m, r, g, o,
" | ".join(
106 "%s: %s" % (nm,
" ".join(
"%.4f" % out[(nm, d)]
for d
in Ds))
for nm
in (
"K2",
"K1",
"Kb"))), flush=
True)
107 for sch
in (
"sharp",
"averaged"):
108 for nm
in (
"K2",
"K1",
"Kb"):
109 w = [max(out[(nm, d)]
for (j, out)
in res
if j[5] == sch)
for d
in Ds]
110 print(
"HARN-WORST N=%d %s %s rho(G) at D_hi=%s: %s" % (a.N, sch, nm, Ds,
" ".join(
"%.4f" % x
for x
in w)))
133 case = M.Case(N, a.ang, a.off, 1.0, 1.0,
"couette")
135 C2 = np.array([[case.frac_cell(i * h, j * h)
for j
in range(N)]
for i
in range(N)])
136 return np.asfortranarray(np.repeat(C2[:, :,
None], Nz, axis=2))
137 if a.geom ==
"pplane":
141 from gsv1
import layered_fractions
142 m = np.array(a.m
if a.m
else (2.0, 1.0, 1.0), float)
143 nrm = m / np.linalg.norm(m)
144 P = 1.0 / float(np.linalg.norm(m))
145 C = layered_fractions(nrm, float(nrm @ np.full(3, 0.5)) + 0.3 / N, P, a.frac * P, 0.0, N)
147 C = np.roll(C, a.roll, axis=(0, 1, 2))
148 return np.asfortranarray(C)
151 C = np.zeros((N, N, N))
if lowA
else np.ones((N, N, N))
152 C[N // 2, :, :] = 1.0
if lowA
else 0.0
153 return np.asfortranarray(C)
154 b = g = math.radians(22.5)
155 n = np.array([math.cos(b) * math.cos(g), math.cos(b) * math.sin(g), math.sin(b)])
157 n = np.array(a.m, float) / np.linalg.norm(a.m)
158 x0 = np.array([0.5 * N + 0.3 * c
for c
in n])
160 sys.path.insert(0, HERE)
164 C = np.zeros((N, N, N))
168 C[i, j, k] =
cube_frac(n, float(n @ (x0 - np.array([i, j, k]))))
169 return np.asfortranarray(C)
174 from scipy.sparse.linalg
import ArpackNoConvergence, LinearOperator, eigs
176 Nz = a.Nz
if a.geom ==
"ext" else N
178 muA, rA = a.muA, a.rA
179 dt = a.D * h * h * (1.0
if muA < 1
else rA / muA)
180 s =
pf.Solver((N, N, Nz), extent=(1.0, 1.0, Nz * h))
182 for f
in (
"-x",
"+x",
"-y",
"+y"):
183 s.set_domain_bc(f,
"periodic" if a.geom ==
"pplane" else "wall")
184 for f
in (
"-z",
"+z"):
185 s.set_domain_bc(f,
"periodic" if a.geom
in (
"ext",
"pplane")
else "wall")
188 s.set_pressure_geometry(np.full((N, N, Nz), 1.0, order=
"F"))
192 s.set_property_model(
"mu",
"linear",
"C", [1.0, muA - 1.0])
193 s.set_property_model(
"rho",
"linear",
"C", [1.0, rA - 1.0])
194 s.set_advection(
False)
196 s.set_vof_viscous_interface(a.mode)
197 dg.set_incremental_pressure(bool(a.incremental))
199 s.set_pressure_pcg(
True, 800, 1e-12)
201 {
"pcg": s.set_pressure_pcg,
"fcg": s.set_pressure_fcg,
"cheb": s.set_pressure_chebyshev}[a.pdriver](
203 dg.set_velocity_solver(
"gauss_seidel")
205 dg.set_velocity_solver_params(a.mom_fixed, 0.0, 2)
207 dg.set_velocity_solver_params(200000, a.mom_tol, 2)
211 s.set_velocity_residual_tolerance(0.0)
212 zper = a.geom ==
"ext"
214 lo = [0, 0, 0]
if a.geom ==
"pplane" else [1, 1, 0
if zper
else 1]
215 sls = [tuple(
slice(lo[c],
None)
if d == c
else slice(
None)
for d
in range(3))
for c
in range(3)]
216 sizes = [int(np.prod([n - (lo[c]
if d == c
else 0)
for d, n
in enumerate(shp)]))
for c
in range(3)]
217 npc = N * N * Nz
if a.incremental
else 0
218 nd = sum(sizes) + npc
220 stats = {
"n": 0,
"pit": 0,
"leak": 0.0,
"msw": 0}
225 if a.geom ==
"pplane":
226 rho_c = 1.0 + (rA - 1.0) * C0
227 wts = [(0.5 * (rho_c + np.roll(rho_c, 1, axis=c))).ravel(order=
"F")
for c
in range(3)]
228 offs = np.cumsum([0] + sizes)
231 x = np.array(x, dtype=float, copy=
True)
233 seg = x[offs[c]:offs[c + 1]]
234 seg -= float(wts[c] @ seg) / float(np.sum(wts[c]))
237 deflate =
lambda x: x
243 A[sls[c]] = x[o:o + sizes[c]].reshape(A[sls[c]].shape, order=
"F")
248 def pack(arrs, P=None):
249 out = [np.asarray(arrs[c])[sls[c]].ravel(order=
"F")
for c
in range(3)]
251 P = np.asarray(P).ravel(order=
"F")
252 out.append(P - P.mean())
253 return np.concatenate(out)
257 u = [np.repeat(u[0].
mean(axis=2, keepdims=
True), Nz, 2), np.repeat(u[1].
mean(axis=2, keepdims=
True), Nz, 2),
262 x = deflate(np.real(np.asarray(x)).ravel())
263 sc = amp / max(np.max(np.abs(x)), 1e-300)
264 for c, A
in enumerate(unpack(x * sc)):
265 s.set_velocity(c, np.asfortranarray(A))
267 P = x[nd - npc:] * sc
268 s.set_field(
"p", np.asfortranarray((P - P.mean()).reshape(shp, order=
"F")))
272 stats[
"pit"] = max(stats[
"pit"], dg.last_pressure_iterations())
273 stats[
"msw"] = max(stats[
"msw"], int(dg.last_step_timers()[
"momentum_sweeps"]))
274 y = pack([s.get_u(), s.get_v(), s.get_w()], s.get_field(
"p")
if npc
else None) / sc
277 stats[
"leak"] = max(stats[
"leak"], float(np.linalg.norm(y - yp) / max(np.linalg.norm(y), 1e-300)))
281 rng = np.random.default_rng(7)
282 v0 = deflate(rng.standard_normal(nd))
285 x1, x2 = deflate(rng.standard_normal(nd)), deflate(rng.standard_normal(nd))
287 x1, x2 = proj(x1), proj(x2)
288 cpu0 = time.process_time()
289 lin = np.linalg.norm(mv(0.3 * x1 + 0.7 * x2) - 0.3 * mv(x1) - 0.7 * mv(x2)) / np.linalg.norm(mv(x1))
290 cpu_mv = (time.process_time() - cpu0) / 4.0
299 xs = y / np.linalg.norm(y)
303 dg.set_velocity_solver_params(2 * a.mom_fixed, 0.0, 2)
305 dg.set_velocity_solver_params(a.mom_fixed, 0.0, 2)
307 du = max(du, float(np.max(np.abs(d[:nu]))) / max(float(np.max(np.abs(y1[:nu]))), 1e-300))
309 dp = max(dp, float(np.max(np.abs(d[nu:]))) / max(float(np.max(np.abs(y1[nu:]))), 1e-300))
310 dbl =
" dbl_u=%.1e dbl_p=%.1e mom_fixed=%d cpu_per_mv=%.3f" % (du, dp, a.mom_fixed, cpu_mv)
313 dg.set_velocity_solver_params(a.ref_check, 0.0, 2)
315 dg.set_velocity_solver_params(a.mom_fixed, 0.0, 2)
317 print(
"REFCHK lin=%.1e%s | fixed %d vs %d: du %.2e dp %.2e" % (
318 lin, dbl, a.mom_fixed, a.ref_check, float(np.max(np.abs(d[:nu]))) / float(np.max(np.abs(yr[:nu]))),
319 float(np.max(np.abs(d[nu:]))) / float(np.max(np.abs(yr[nu:])))
if npc
else 0.0), flush=
True)
321 if max(du, dp) > 1e-12:
322 print(
"PROD %s geom=%s N=%d D_hi=%g | rho=nan | top: - | matvecs=%d max_pcg_it=%d lin=%.1e kz0_leak=0.0e+00 "
323 "solver=%s conv=0 ncv=%d max_momentum_sweeps=%d%s dbl_fail=1" % (
324 a.mode, a.geom, N, a.D, stats[
"n"], stats[
"pit"], lin, dg.velocity_solver(), a.ncv, stats[
"msw"],
327 op = LinearOperator((nd, nd), matvec=mv, dtype=float)
330 vals = eigs(op, k=a.k, which=
"LM", v0=v0, ncv=a.ncv, tol=1e-9, maxiter=a.maxiter,
331 return_eigenvectors=
False)
332 except ArpackNoConvergence
as ex:
333 conv, vals = 0, ex.eigenvalues
334 vals = sorted(vals, key=
lambda z: -abs(z))
if len(vals)
else [complex(
"nan")]
335 tag =
"%s geom=%s%s N=%d Nz=%d mu=%g rho=%g D_hi=%g incr=%d prodsolve=%d kz0=%d p=%s mom_tol=%g" % (
336 a.mode, a.geom,
"(%g,%g)" % (a.ang, a.off)
if a.geom ==
"ext" else ((
"/sub8" if a.sub8
else "") + (
337 "/m=%s" % (a.m,)
if a.m
else "") + (
"/frac=%g" % a.frac
if a.geom ==
"pplane" else "") + (
338 "/roll=%s" % (a.roll,)
if a.roll
else "") + (
"/deflated" if a.geom ==
"pplane" else "")), N, Nz,
339 muA, rA, a.D, a.incremental, a.prodsolve, a.kz0, a.pdriver, a.mom_tol)
340 print(
"PROD %s | rho=%.6f | top: %s | matvecs=%d max_pcg_it=%d lin=%.1e kz0_leak=%.1e solver=%s "
341 "conv=%d ncv=%d max_momentum_sweeps=%d" % (
342 tag, abs(vals[0]),
" ".join(
"%.5f%+.5fi" % (z.real, z.imag)
for z
in vals[:4]), stats[
"n"],
343 stats[
"pit"], lin, stats[
"leak"], dg.velocity_solver(), conv, a.ncv, stats[
"msw"]) + dbl, flush=
True)