rotating-cube-cfd / code /validate_cube.py
aloeme's picture
Upload code/validate_cube.py with huggingface_hub
c720564 verified
Raw
History Blame Contribute Delete
5.14 kB
"""
validate_cube.py -- dedicated phase-1 validation mesh.
Goal: reproduce the paper's static-cube validation cd ~= 0.935 (bracketed by the
unbounded cube-particle correlations Hoelzer 0.854 / Haider-Levenspiel 1.122).
That benchmark needs a LOW-BLOCKAGE, FACE-FORWARD, axis-aligned cube -- NOT the
high-blockage 45-deg diamond of the production dataset.
Geometry: unit cube [-0.5,0.5]^3, face normal to the +x inflow (frontal area = c^2,
matching the paper's Aref = c^2). Large box (+/-8c laterally, 8c up / 16c down)
=> blockage < 1%. Cube is finite in z (free stream above/below), like production.
Mesh: 3x3 transfinite block grid in x-y around the central square, extruded in
3 z-bands (planes at z = +/-0.5). Axis-aligned => zero-skew pure hex. The central
column's mid z-band is tagged 'cube'; subsetMesh carves it out (same pipeline as
the production mesh).
"""
import os, gmsh
# ---- domain / cube ---------------------------------------------------------
X = [-8.0, -0.5, 0.5, 16.0] # x partition lines (cube faces at -0.5, 0.5)
Y = [-8.0, -0.5, 0.5, 8.0] # y partition lines
zc, z_far = 0.5, 8.0 # cube half-height ; outer z extent
# transfinite node counts per segment (upstream, central, downstream)
Nx = [22, 28, 28]
Ny = [20, 28, 20]
cx = [0.86, 1.0, 1.16] # progression: <1 fine at end, >1 fine at start
cy = [0.86, 1.0, 1.16]
nz_mid, nz_band, z_ratio = 28, 20, 1.16
gmsh.initialize()
gmsh.option.setNumber("General.Terminal", 1)
geo = gmsh.model.geo
z0 = -z_far
# 4x4 grid of points at z0
P = [[geo.addPoint(X[i], Y[j], z0) for j in range(4)] for i in range(4)]
# horizontal lines H[i][j]: from P[i][j] to P[i+1][j] (x-direction, segment i)
H = [[geo.addLine(P[i][j], P[i+1][j]) for j in range(4)] for i in range(3)]
# vertical lines V[i][j]: from P[i][j] to P[i][j+1] (y-direction, segment j)
V = [[geo.addLine(P[i][j], P[i][j+1]) for j in range(3)] for i in range(4)]
# 9 surfaces
S = [[0]*3 for _ in range(3)]
for i in range(3):
for j in range(3):
cl = geo.addCurveLoop([H[i][j], V[i+1][j], -H[i][j+1], -V[i][j]])
S[i][j] = geo.addPlaneSurface([cl])
# transfinite curves
for i in range(3):
for j in range(4):
if cx[i] == 1.0: geo.mesh.setTransfiniteCurve(H[i][j], Nx[i])
else: geo.mesh.setTransfiniteCurve(H[i][j], Nx[i], "Progression", cx[i])
for i in range(4):
for j in range(3):
if cy[j] == 1.0: geo.mesh.setTransfiniteCurve(V[i][j], Ny[j])
else: geo.mesh.setTransfiniteCurve(V[i][j], Ny[j], "Progression", cy[j])
for i in range(3):
for j in range(3):
geo.mesh.setTransfiniteSurface(S[i][j])
geo.mesh.setRecombine(2, S[i][j])
geo.synchronize()
# ---- z-band extrusion (identical surface set each band -> conformal) -------
base = [(2, S[i][j]) for i in range(3) for j in range(3)]
cube_base_idx = base.index((2, S[1][1])) # central square is the cube footprint
def graded(n, ratio, fine_at_end):
th = [ratio**k for k in range(n)]
if fine_at_end: th = th[::-1]
tot = sum(th); cum, acc = [], 0.0
for t in th:
acc += t/tot; cum.append(acc)
cum[-1] = 1.0
return [1]*n, cum
def extrude_band(surfs, dz, nE, hs):
out = geo.extrude(surfs, 0, 0, dz, numElements=nE, heights=hs, recombine=True)
vols, tops = [], []
for k, (d, t) in enumerate(out):
if d == 3:
vols.append(t); tops.append(out[k-1][1])
return vols, [(2, t) for t in tops]
nE, hs = graded(nz_band, z_ratio, fine_at_end=True)
v1, top1 = extrude_band(base, (-zc) - (-z_far), nE, hs) # bottom band
v2, top2 = extrude_band(top1, 2*zc, [nz_mid], [1.0]) # mid band (cube here)
nE, hs = graded(nz_band, z_ratio, fine_at_end=False)
v3, top3 = extrude_band(top2, z_far - zc, nE, hs) # top band
geo.synchronize()
cube_vol = v2[cube_base_idx]
fluid_vol = [t for t in (v1 + v2 + v3) if t != cube_vol]
gmsh.model.addPhysicalGroup(3, fluid_vol, name="fluid")
gmsh.model.addPhysicalGroup(3, [cube_vol], name="cube")
# exterior box faces by planar bbox
tol = 1e-6
grp = {"inlet": [], "outlet": [], "sides": [], "zMin": [], "zMax": []}
for (d, t) in gmsh.model.getEntities(2):
x0,y0,zz0,x1,y1,zz1 = gmsh.model.getBoundingBox(2, t)
if abs(x1-x0)<tol and abs(x0-X[0])<tol: grp["inlet"].append(t)
elif abs(x1-x0)<tol and abs(x0-X[3])<tol: grp["outlet"].append(t)
elif abs(y1-y0)<tol and (abs(y0-Y[0])<tol or abs(y0-Y[3])<tol): grp["sides"].append(t)
elif abs(zz1-zz0)<tol and abs(zz0-(-z_far))<tol: grp["zMin"].append(t)
elif abs(zz1-zz0)<tol and abs(zz0-( z_far))<tol: grp["zMax"].append(t)
for name, tags in grp.items():
if tags: gmsh.model.addPhysicalGroup(2, tags, name=name)
gmsh.model.mesh.generate(3)
OUT = os.path.dirname(os.path.abspath(__file__))
types, etags, _ = gmsh.model.mesh.getElements(3)
tot = sum(len(e) for e in etags)
print(f"3D cells (incl. cube block): {tot} | types={[int(t) for t in types]} (5=hex)")
print("boundary faces: " + ", ".join(f"{k}={len(v)}" for k,v in grp.items()))
gmsh.write(os.path.join(OUT, "validate_cube.msh"))
print("wrote validate_cube.msh")
gmsh.finalize()