flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_scalar_inflow.py
Go to the documentation of this file.
1#!/usr/bin/env python
2"""Gate: a transported scalar enters through a Dirichlet inflow face with the prescribed flux.
3
4Why this test exists. advanceScalars froze c^n into cOld BEFORE filling c's ghosts, and the
5explicit advection reads cOld's ghosts. set_field (and the coupling drivers, and the VoF energy
6transport) write inner cells only, so the first step after such a write upwinded the inflow from
7stale values -- zeros on a fresh scalar: c = 1 at a Dirichlet-1 inlet fell to 0.5 in one step
8(doc/uf_outlet_fix.md section B).
9
10Checks:
11 1. a c == 1 scalar with a Dirichlet-1 inlet stays 1 in the INLET column after one step, both
12 grids, first-order upwind and Koren;
13 2. a 1-D plug: uniform flow U through a box periodic in y and z, c = 0 initially, c = 1 at the
14 inlet. Until the front reaches the outlet the inventory is exactly U t (the inflow carries
15 U * 1 per unit area), and the c = 1/2 crossing sits at x = U t to within a cell.
16
17Run: OMP_NUM_THREADS=4 OMP_PROC_BIND=false PYTHONPATH=<build> \
18 python tests/python/test_scalar_inflow.py
19Exit 0 pass, 1 fail, 77 skipped (no module).
20"""
21import sys
22
23import numpy as np
24
25MU, DT = 0.1, 0.5
26
27
28def box(pf, cls, nx, ny, nz, walls):
29 s = cls(nx, ny, nz)
30 s.set_rho(1.0); s.set_mu(MU); s.set_dt(DT)
31 s.set_advection(True); s.set_advection_scheme("koren")
32 if cls is pf.SolverColocated:
33 s.set_collocated_scheme("gauge-exact")
34 s.set_domain_bc("-x", "inflow", (1.0, 0.0, 0.0))
35 s.set_domain_bc("+x", "outflow")
36 if walls:
37 s.set_domain_bc("-y", "wall"); s.set_domain_bc("+y", "wall")
38 s.set_pressure_geometry(np.asfortranarray(np.full((nx, ny, nz), 10.0)))
39 s.set_pressure_pcg(True, 400, 1e-11)
40 return s
41
42
43def check_inlet_column(pf, cls, scheme):
44 nx, ny, nz = 24, 12, 4
45 s = box(pf, cls, nx, ny, nz, walls=True)
46 for _ in range(5):
47 s.step()
48 s.add_scalar("c", diffusivity=0.0, scheme=scheme, iters=1)
49 s.set_scalar_bc("c", "-x", "dirichlet", 1.0)
50 s.set_scalar_bc("c", "+x", "neumann", 0.0)
51 s.set_field("c", np.asfortranarray(np.ones((nx, ny, nz))))
52 s.step()
53 c = np.asarray(s.get_field("c"))
54 err = float(np.abs(c[: nx - 1] - 1.0).max()) # the outlet column is test_open_face_plane's
55 assert err <= 1e-10, f"{cls.__name__}/{scheme}: c = 1 moved by {err:.3e} upstream of the outlet"
56 return err
57
58
59def check_plug(pf, cls, scheme, steps=40):
60 nx, ny, nz = 64, 4, 4
61 s = box(pf, cls, nx, ny, nz, walls=False)
62 s.add_scalar("c", diffusivity=0.0, scheme=scheme, iters=1)
63 s.set_scalar_bc("c", "-x", "dirichlet", 1.0)
64 s.set_scalar_bc("c", "+x", "neumann", 0.0)
65 s.set_field("c", np.asfortranarray(np.zeros((nx, ny, nz))))
66 for _ in range(steps):
67 s.step()
68 c = np.asarray(s.get_field("c")).mean(axis=(1, 2))
69 t = steps * DT
70 inv = float(c.sum()) # cell units: inventory per unit cross-section
71 k = int(np.argmax(c < 0.5)) # first cell below 1/2
72 xf = k - 0.5 + (c[k - 1] - 0.5) / (c[k - 1] - c[k]) * 1.0 if k > 0 else 0.0
73 assert abs(inv - t) <= 1e-9, f"{cls.__name__}/{scheme}: inventory {inv:.12f} != U t = {t}"
74 assert abs(xf - t) <= 1.0, f"{cls.__name__}/{scheme}: c = 1/2 at x = {xf:.3f}, U t = {t}"
75 return inv - t, xf - t
76
77
78def main():
79 try:
80 import peclet.flow as pf
81 except ImportError:
82 print("SKIP: peclet.flow not importable (set PYTHONPATH to the build tree)")
83 return 77
84 try:
85 for cls in (pf.SolverColocated, pf.Solver):
86 for scheme in ("fou", "koren"):
87 e = check_inlet_column(pf, cls, scheme)
88 di, dx = check_plug(pf, cls, scheme)
89 print(f" {cls.__name__:16s} {scheme:5s} inlet |c-1| {e:.2e} plug inventory - Ut "
90 f"{di:+.2e} front - Ut {dx:+.3f}")
91 except AssertionError as e:
92 print(f"FAIL: {e}")
93 return 1
94 print("OK: the scalar enters with the prescribed flux")
95 return 0
96
97
98if __name__ == "__main__":
99 sys.exit(main())
check_plug(pf, cls, scheme, steps=40)
box(pf, cls, nx, ny, nz, walls)
check_inlet_column(pf, cls, scheme)
static double mean(const std::vector< double > &a)