| import 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 |
|
|
| gmsh.initialize() |
| gmsh.option.setNumber("General.Terminal", 0) |
| geo = gmsh.model.geo |
|
|
| def P(x, y): return geo.addPoint(x, y, 0.0) |
|
|
| O = P(0, 0) |
| DE, DN, DW, DS = P(a,0), P(0,a), P(-a,0), P(0,-a) |
| CE, CN, CW, CS = P(R,0), P(0,R), P(-R,0), P(0,-R) |
| B1, B2, B3, B4 = P(x_in,y_lo), P(x_out,y_lo), P(x_out,y_hi), P(x_in,y_hi) |
|
|
| |
| 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) |
|
|
| |
| def block(d, r2, arc, r1): |
| cl = geo.addCurveLoop([d, r2, -arc, -r1]) |
| s = geo.addPlaneSurface([cl]); return s |
|
|
| sEN = block(dEN, rN, aEN, rE) |
| sNW = block(dNW, rW, aNW, rN) |
| sWS = block(dWS, rS, aWS, rW) |
| sSE = block(dSE, rE, aSE, rS) |
| annulus = [sEN, sNW, sWS, sSE] |
|
|
| 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) |
| 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) |
| 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) |
|
|
| |
| def pg(dim, tags, name): gmsh.model.addPhysicalGroup(dim, tags, name=name) |
| pg(1,[dEN,dNW,dWS,dSE],"cube"); pg(1,[aEN,aNW,aWS,aSE],"interface") |
| pg(1,[oL],"inlet"); pg(1,[oR],"outlet"); pg(1,[oB,oT],"sides") |
| pg(2,annulus,"corotating"); pg(2,[sOuter],"static") |
|
|
| gmsh.model.mesh.generate(2) |
|
|
| |
| ntype, ntags = {}, gmsh.model.mesh.getElements(2) |
| types, etags, enodes = ntags |
| import numpy as np |
| node_tags, coords, _ = gmsh.model.mesh.getNodes() |
| xyz = {int(t): coords[3*i:3*i+2] for i,t in enumerate(node_tags)} |
| nq = nt = 0 |
| import matplotlib; matplotlib.use("Agg") |
| import matplotlib.pyplot as plt |
| from matplotlib.collections import PolyCollection |
| polys=[] |
| for et, ets, ens in zip(types, etags, enodes): |
| npe = {2:3,3:4}.get(et) |
| if not npe: continue |
| ens = ens.reshape(-1,npe) |
| for row in ens: |
| polys.append([xyz[int(n)] for n in row]) |
| if et==3: nq+=len(ets) |
| if et==2: nt+=len(ets) |
| print(f"2D cells: {nq+nt} (quads={nq}, tris={nt}, quad fraction={nq/(nq+nt):.3f})") |
|
|
| fig, axs = plt.subplots(1,2,figsize=(14,4.2)) |
| for ax,(xl,yl,ttl) in zip(axs,[((x_in,x_out),(y_lo,y_hi),"full section"), |
| ((-1.6,1.6),(-1.6,1.6),"near-cube O-grid")]): |
| pc=PolyCollection(polys, facecolors="none", edgecolors="#1f4e79", linewidths=0.25) |
| ax.add_collection(pc); ax.set_xlim(*xl); ax.set_ylim(*yl); ax.set_aspect("equal") |
| ax.set_title(ttl,fontsize=10) |
| th=np.linspace(0,2*np.pi,200) |
| ax.plot(R*np.cos(th),R*np.sin(th),"--",color="#c00000",lw=1.0) |
| import os |
| OUT = os.path.dirname(os.path.abspath(__file__)) |
| plt.tight_layout(); plt.savefig(os.path.join(OUT,"section2d.png"),dpi=130) |
| print("saved preview") |
| gmsh.write(os.path.join(OUT,"section2d.msh")) |
| gmsh.finalize() |