17、各向异性背景网格
import gmsh
import math
import os
import sys
gmsh.initialize()
gmsh.model.add("t17")
gmsh.model.occ.addRectangle(-2, -2, 0, 4, 4)
gmsh.model.occ.synchronize()
path = os.path.dirname(os.path.abspath(__file__))
gmsh.merge(os.path.join(path, os.pardir, 't17_bgmesh.pos'))
#设置背景网格字段:
bg_field = gmsh.model.mesh.field.add("PostView")
gmsh.model.mesh.field.setNumber(bg_field, "ViewIndex", 0)
gmsh.model.mesh.field.setAsBackgroundMesh(bg_field)
#设置网格生成选项
gmsh.option.setNumber("Mesh.SmoothRatio", 3)
gmsh.option.setNumber("Mesh.AnisoMax", 1000)
gmsh.option.setNumber("Mesh.Algorithm", 7)
gmsh.model.mesh.generate(2)
gmsh.write("t17.msh")
if '-nopopup' not in sys.argv:
gmsh.fltk.run()
gmsh.finalize()
18、周期网格
import gmsh
import math
import os
import sys
gmsh.initialize()
gmsh.model.add("t18")
gmsh.model.occ.addBox(0, 0, 0, 1, 1, 1, 1)
gmsh.model.occ.synchronize()
gmsh.model.mesh.setSize(gmsh.model.getEntities(0), 0.1)
gmsh.model.mesh.setSize([(0, 1)], 0.02)
translation = [1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
gmsh.model.mesh.setPeriodic(2, [2], [1], translation)
gmsh.model.mesh.setPeriodic(2, [6], [5],
[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1])
gmsh.model.mesh.setPeriodic(2, [4], [3],
[1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1])
gmsh.model.occ.addBox(2, 0, 0, 1, 1, 1, 10)
x = 2 - 0.3
y = 0
z = 0
gmsh.model.occ.addSphere(x, y, z, 0.35, 11)
gmsh.model.occ.addSphere(x + 1, y, z, 0.35, 12)
gmsh.model.occ.addSphere(x, y + 1, z, 0.35, 13)
gmsh.model.occ.addSphere(x, y, z + 1, 0.35, 14)
gmsh.model.occ.addSphere(x + 1, y + 1, z, 0.35, 15)
gmsh.model.occ.addSphere(x, y + 1, z + 1, 0.35, 16)
gmsh.model.occ.addSphere(x + 1, y, z + 1, 0.35, 17)
gmsh.model.occ.addSphere(x + 1, y + 1, z + 1, 0.35, 18)
out, _ = gmsh.model.occ.fragment([(3, 10)], [(3, i) for i in range(11, 19)])
gmsh.model.occ.synchronize()
gmsh.option.setNumber("Geometry.OCCBoundsUseStl", 1)
eps = 1e-3
vin = gmsh.model.getEntitiesInBoundingBox(2 - eps, -eps, -eps, 2 + 1 + eps,
1 + eps, 1 + eps, 3)
for v in vin:
out.remove(v)
gmsh.model.removeEntities(out, True)
p = gmsh.model.getBoundary(vin, False, False, True)
gmsh.model.mesh.setSize(p, 0.1)
p = gmsh.model.getEntitiesInBoundingBox(2 - eps, -eps, -eps, 2 + eps, eps, eps,
0)
gmsh.model.mesh.setSize(p, 0.001)
sxmin = gmsh.model.getEntitiesInBoundingBox(2 - eps, -eps, -eps, 2 + eps,
1 + eps, 1 + eps, 2)
for i in sxmin:
xmin, ymin, zmin, xmax, ymax, zmax = gmsh.model.getBoundingBox(i[0], i[1])
sxmax = gmsh.model.getEntitiesInBoundingBox(xmin - eps + 1, ymin - eps,
zmin - eps, xmax + eps + 1,
ymax + eps, zmax + eps, 2)
for j in sxmax:
xmin2, ymin2, zmin2, xmax2, ymax2, zmax2 = gmsh.model.getBoundingBox(
j[0], j[1])
xmin2 -= 1
xmax2 -= 1
if (abs(xmin2 - xmin) < eps and abs(xmax2 - xmax) < eps and
abs(ymin2 - ymin) < eps and abs(ymax2 - ymax) < eps and
abs(zmin2 - zmin) < eps and abs(zmax2 - zmax) < eps):
#周期网格的相关设置
gmsh.model.mesh.setPeriodic(2, [j[1]], [i[1]], translation)
gmsh.model.mesh.generate(3)
gmsh.write("t18.msh")
if '-nopopup' not in sys.argv:
gmsh.fltk.run()
gmsh.finalize()
19、穿透、圆角、管道、曲率网格尺寸
import gmsh
import math
import os
import sys
gmsh.initialize()
gmsh.model.add("t19")
gmsh.model.occ.addCircle(0, 0, 0, 0.5, 1)
gmsh.model.occ.addCurveLoop([1], 1)
gmsh.model.occ.addCircle(0.1, 0.05, 1, 0.1, 2)
gmsh.model.occ.addCurveLoop([2], 2)
gmsh.model.occ.addCircle(-0.1, -0.1, 2, 0.3, 3)
gmsh.model.occ.addCurveLoop([3], 3)
gmsh.model.occ.addThruSections([1, 2, 3], 1)
gmsh.model.occ.synchronize()
gmsh.model.occ.addCircle(2 + 0, 0, 0, 0.5, 11)
gmsh.model.occ.addCurveLoop([11], 11)
gmsh.model.occ.addCircle(2 + 0.1, 0.05, 1, 0.1, 12)
gmsh.model.occ.addCurveLoop([12], 12)
gmsh.model.occ.addCircle(2 - 0.1, -0.1, 2, 0.3, 13)
gmsh.model.occ.addCurveLoop([13], 13)
gmsh.model.occ.addThruSections([11, 12, 13], 11, True, True)
gmsh.model.occ.synchronize()
out = gmsh.model.occ.copy([(3, 1)])
gmsh.model.occ.translate(out, 4, 0, 0)
gmsh.model.occ.synchronize()
e = gmsh.model.getBoundary(gmsh.model.getBoundary(out), False)
gmsh.model.occ.fillet([out[0][1]], [abs(i[1]) for i in e], [0.1])
gmsh.model.occ.synchronize()
nturns = 1.
npts = 20
r = 1.
h = 1. * nturns
p = []
for i in range(0, npts):
theta = i * 2 * math.pi * nturns / npts
gmsh.model.occ.addPoint(r * math.cos(theta), r * math.sin(theta),
i * h / npts, 1, 1000 + i)
p.append(1000 + i)
gmsh.model.occ.addSpline(p, 1000)
gmsh.model.occ.addWire([1000], 1000)
gmsh.model.occ.addDisk(1, 0, 0, 0.2, 0.2, 1000)
gmsh.model.occ.rotate([(2, 1000)], 0, 0, 0, 1, 0, 0, math.pi / 2)
gmsh.model.occ.addPipe([(2, 1000)], 1000, 'DiscreteTrihedron')
gmsh.model.occ.remove([(2, 1000)])
gmsh.option.setNumber("Geometry.NumSubEdges", 1000)
gmsh.model.occ.synchronize()
gmsh.option.setNumber("Mesh.MeshSizeFromCurvature", 20)
gmsh.option.setNumber("Mesh.MeshSizeMin", 0.001)
gmsh.option.setNumber("Mesh.MeshSizeMax", 0.3)
gmsh.model.mesh.generate(3)
gmsh.write("t19.msh")
if '-nopopup' not in sys.argv:
gmsh.fltk.run()
gmsh.finalize()
20、STEP导入和操作、几何分割
import gmsh
import math
import os
import sys
gmsh.initialize()
gmsh.model.add("t20")
#导入几何体
path = os.path.dirname(os.path.abspath(__file__))
v = gmsh.model.occ.importShapes(os.path.join(path, os.pardir, 't20_data.step'))
xmin, ymin, zmin, xmax, ymax, zmax = gmsh.model.occ.getBoundingBox(
v[0][0], v[0][1])
N = 5
dir = 'X' #
surf = False
dx = (xmax - xmin)
dy = (ymax - ymin)
dz = (zmax - zmin)
L = dz if (dir == 'X') else dx
H = dz if (dir == 'Y') else dy
s = []
s.append((2, gmsh.model.occ.addRectangle(xmin, ymin, zmin, L, H)))
if dir == 'X':
gmsh.model.occ.rotate([s[0]], xmin, ymin, zmin, 0, 1, 0, -math.pi/2)
elif dir == 'Y':
gmsh.model.occ.rotate([s[0]], xmin, ymin, zmin, 1, 0, 0, math.pi/2)
tx = dx / N if (dir == 'X') else 0
ty = dy / N if (dir == 'Y') else 0
tz = dz / N if (dir == 'Z') else 0
gmsh.model.occ.translate([s[0]], tx, ty, tz)
for i in range(1, N-1):
s.extend(gmsh.model.occ.copy([s[0]]))
gmsh.model.occ.translate([s[-1]], i * tx, i * ty, i * tz)
gmsh.model.occ.fragment(v, s)
gmsh.model.occ.remove(gmsh.model.occ.getEntities(2), True)
gmsh.model.occ.synchronize()
if surf:
eps = 1e-4
s = []
for i in range(1, N):
xx = xmin if (dir == 'X') else xmax
yy = ymin if (dir == 'Y') else ymax
zz = zmin if (dir == 'Z') else zmax
s.extend(gmsh.model.getEntitiesInBoundingBox(
xmin - eps + i * tx, ymin - eps + i * ty, zmin - eps + i * tz,
xx + eps + i * tx, yy + eps + i * ty, zz + eps + i * tz, 2))
dels = gmsh.model.getEntities(2)
for e in s:
dels.remove(e)
gmsh.model.removeEntities(gmsh.model.getEntities(3))
gmsh.model.removeEntities(dels)
gmsh.model.removeEntities(gmsh.model.getEntities(1))
gmsh.model.removeEntities(gmsh.model.getEntities(0))
gmsh.option.setNumber("Mesh.MeshSizeMin", 3)
gmsh.option.setNumber("Mesh.MeshSizeMax", 3)
gmsh.model.mesh.generate(3)
gmsh.write("t20.msh")
if '-nopopup' not in sys.argv:
gmsh.fltk.run()
gmsh.finalize()
21、网格划分
import gmsh
import math
import os
import sys
gmsh.initialize()
gmsh.model.add("t17")
gmsh.model.occ.addRectangle(-2, -2, 0, 4, 4)
gmsh.model.occ.synchronize()
path = os.path.dirname(os.path.abspath(__file__))
gmsh.merge(os.path.join(path, os.pardir, 't17_bgmesh.pos'))
bg_field = gmsh.model.mesh.field.add("PostView")
gmsh.model.mesh.field.setNumber(bg_field, "ViewIndex", 0)
gmsh.model.mesh.field.setAsBackgroundMesh(bg_field)
gmsh.option.setNumber("Mesh.SmoothRatio", 3)
gmsh.option.setNumber("Mesh.AnisoMax", 1000)
gmsh.option.setNumber("Mesh.Algorithm", 7)
gmsh.model.mesh.generate(2)
gmsh.write("t17.msh")
if '-nopopup' not in sys.argv:
gmsh.fltk.run()
gmsh.finalize()