| """ |
| extrude3d.py -- extrude the 2D x-y cross-section (section2d) into the 3D |
| semi-structured mesh for the rotating-cube case. |
| |
| 3D shape subtlety (see CLAUDE.md): |
| The cube is finite in z: it occupies |z| < c/2 only. In that mid z-band the |
| x-y section has the diamond "hole" (cube is solid); above and below it the |
| section is FULL (fluid), and the cube top/bottom faces (z = +/- c/2) are walls. |
| |
| Strategy: |
| Mesh the full section ONCE -- including the diamond interior -- and extrude it |
| in THREE z-bands so node-planes land exactly on z = +/- c/2. Every band |
| extrudes the *identical* surface set, so the mesh is automatically conformal. |
| Only the mid-band diamond volume is tagged as a separate "cube" cellZone; a |
| later subsetMesh removes it, exposing the cube walls. This avoids any fragile |
| per-band surface bookkeeping. |
| |
| Output (variant a, single connected mesh for the static-cube validation): |
| section3d.msh -- physical volumes: fluid, cube ; physical surfaces: inlet, |
| outlet, sides, zMin, zMax. (interface circle + cube walls |
| stay internal here; the AMI split is variant b, later.) |
| """ |
| import os, math, gmsh |
|
|
| |
| c = 1.0 |
| a = c / math.sqrt(2.0) |
| R = 1.0 * c |
| x_in, x_out = -2.5*c, 5.0*c |
| y_lo, y_hi = -1.5*c, 1.5*c |
| n_tang = 26 |
| n_rad = 18 |
| prog = 1.14 |
|
|
| |
| zc = c / 2.0 |
| z_far = 3.5 * c |
| nz_mid = 16 |
| nz_band = 26 |
| z_ratio = 1.12 |
|
|
| gmsh.initialize() |
| gmsh.option.setNumber("General.Terminal", 1) |
| geo = gmsh.model.geo |
|
|
| def P(x, y, z=0.0): return geo.addPoint(x, y, z) |
|
|
| |
| z0 = -z_far |
| O = P(0, 0, z0) |
| DE, DN, DW, DS = P(a,0,z0), P(0,a,z0), P(-a,0,z0), P(0,-a,z0) |
| CE, CN, CW, CS = P(R,0,z0), P(0,R,z0), P(-R,0,z0), P(0,-R,z0) |
| B1, B2, B3, B4 = P(x_in,y_lo,z0), P(x_out,y_lo,z0), P(x_out,y_hi,z0), P(x_in,y_hi,z0) |
|
|
| |
| dEN, dNW, dWS, dSE = (geo.addLine(DE,DN), geo.addLine(DN,DW), |
| geo.addLine(DW,DS), geo.addLine(DS,DE)) |
| aEN = geo.addCircleArc(CE, O, CN); aNW = geo.addCircleArc(CN, O, CW) |
| aWS = geo.addCircleArc(CW, O, CS); aSE = geo.addCircleArc(CS, O, CE) |
| rE, rN = geo.addLine(DE,CE), geo.addLine(DN,CN) |
| rW, rS = geo.addLine(DW,CW), geo.addLine(DS,CS) |
| oB, oR = geo.addLine(B1,B2), geo.addLine(B2,B3) |
| oT, oL = geo.addLine(B3,B4), geo.addLine(B4,B1) |
|
|
| |
| dia_loop = geo.addCurveLoop([dEN, dNW, dWS, dSE]) |
| sDia = geo.addPlaneSurface([dia_loop]) |
|
|
| |
| def block(d, r2, arc, r1): |
| cl = geo.addCurveLoop([d, r2, -arc, -r1]) |
| return geo.addPlaneSurface([cl]) |
|
|
| sEN = block(dEN, rN, aEN, rE) |
| sNW = block(dNW, rW, aNW, rN) |
| sWS = block(dWS, rS, aWS, rW) |
| sSE = block(dSE, rE, aSE, rS) |
|
|
| for ln in (dEN,dNW,dWS,dSE, aEN,aNW,aWS,aSE): |
| geo.mesh.setTransfiniteCurve(ln, n_tang) |
| for ln in (rE,rN,rW,rS): |
| geo.mesh.setTransfiniteCurve(ln, n_rad, "Progression", prog) |
| geo.mesh.setTransfiniteSurface(sDia, "Left", [DE, DN, DW, DS]) |
| for s, corners in ((sEN,[DE,DN,CN,CE]),(sNW,[DN,DW,CW,CN]), |
| (sWS,[DW,DS,CS,CW]),(sSE,[DS,DE,CE,CS])): |
| geo.mesh.setTransfiniteSurface(s, "Left", corners) |
| for s in (sDia, sEN, sNW, sWS, sSE): |
| geo.mesh.setRecombine(2, s) |
|
|
| |
| outer_loop = geo.addCurveLoop([oB, oR, oT, oL]) |
| hole_loop = geo.addCurveLoop([aEN, aNW, aWS, aSE]) |
| sOuter = geo.addPlaneSurface([outer_loop, hole_loop]) |
| geo.mesh.setRecombine(2, sOuter) |
| geo.synchronize() |
|
|
| |
| fd = gmsh.model.mesh.field |
| f_dist = fd.add("Distance"); fd.setNumbers(f_dist, "CurvesList", [aEN,aNW,aWS,aSE]) |
| f_thr = fd.add("Threshold") |
| fd.setNumber(f_thr,"InField",f_dist); fd.setNumber(f_thr,"SizeMin",0.030) |
| fd.setNumber(f_thr,"SizeMax",0.28); fd.setNumber(f_thr,"DistMin",0.05); fd.setNumber(f_thr,"DistMax",2.2) |
| f_box = fd.add("Box") |
| fd.setNumber(f_box,"VIn",0.06); fd.setNumber(f_box,"VOut",0.4) |
| fd.setNumber(f_box,"XMin",0.4); fd.setNumber(f_box,"XMax",x_out) |
| fd.setNumber(f_box,"YMin",-1.0); fd.setNumber(f_box,"YMax",1.0); fd.setNumber(f_box,"Thickness",0.4) |
| f_min = fd.add("Min"); fd.setNumbers(f_min,"FieldsList",[f_thr,f_box]); fd.setAsBackgroundMesh(f_min) |
| gmsh.option.setNumber("Mesh.MeshSizeExtendFromBoundary",0) |
| gmsh.option.setNumber("Mesh.MeshSizeFromCurvature",0) |
| gmsh.option.setNumber("Mesh.Algorithm",8) |
| gmsh.option.setNumber("Mesh.RecombinationAlgorithm",1) |
|
|
| |
| |
| base = [(2, s) for s in (sDia, sEN, sNW, sWS, sSE, sOuter)] |
|
|
| def graded(n, ratio, fine_at_end): |
| """Return (numElements, cumHeights) for n single-element layers whose |
| thickness grows geometrically; fine_at_end clusters thin layers near 1.0.""" |
| th = [ratio**i for i 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 i, (d, t) in enumerate(out): |
| if d == 3: |
| vols.append(t); tops.append(out[i-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) |
| |
| v2, top2 = extrude_band(top1, 2*zc, [nz_mid], [1.0]) |
| |
| nE, hs = graded(nz_band, z_ratio, fine_at_end=False) |
| v3, top3 = extrude_band(top2, z_far - zc, nE, hs) |
| geo.synchronize() |
|
|
| |
| |
| cube_vol = v2[0] |
| 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") |
|
|
| |
| def classify_boundary(): |
| tol = 1e-6 |
| groups = {"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_in) < tol: groups["inlet"].append(t) |
| elif abs(x1-x0) < tol and abs(x0-x_out) < tol: groups["outlet"].append(t) |
| elif abs(y1-y0) < tol and (abs(y0-y_lo) < tol or abs(y0-y_hi) < tol): groups["sides"].append(t) |
| elif abs(zz1-zz0) < tol and abs(zz0-(-z_far)) < tol: groups["zMin"].append(t) |
| elif abs(zz1-zz0) < tol and abs(zz0-( z_far)) < tol: groups["zMax"].append(t) |
| return groups |
|
|
| groups = classify_boundary() |
| for name, tags in groups.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) |
| import numpy as np |
| counts = {4:"tets",5:"hexes",6:"prisms",7:"pyramids"} |
| tot = 0 |
| print("--- 3D element counts ---") |
| for et, ets in zip(types, etags): |
| n = len(ets); tot += n |
| print(f" {counts.get(et,et):8s}: {n}") |
| print(f" TOTAL 3D cells (incl. cube block to be removed): {tot}") |
| print(f" boundary faces: " + ", ".join(f"{k}={len(v)}" for k,v in groups.items())) |
| print(f" z bands: [-{z_far},-{zc}] x{nz_band} | [-{zc},{zc}] x{nz_mid} | [{zc},{z_far}] x{nz_band}") |
|
|
| gmsh.write(os.path.join(OUT, "section3d.msh")) |
| print("wrote section3d.msh") |
| gmsh.finalize() |
|
|