Page MenuHomec4science

meshing.py
No OneTemporary

File Metadata

Created
Thu, Aug 15, 09:39

meshing.py

import gmsh
import os
import sys
"""
function to mesh given .step file
geom_dir: location of the step file
name: name of the file
ext: extension of the file
mesh_dir: location to save .msh files
"""
def meshing(geom_dir, name, ext, mesh_dir):
#initialize gmesh
gmsh.initialize()
gmsh.model.add(name)
#read the .step file
gmsh.model.occ.importShapes(geom_dir + name + ext)
#get solid part of the model, determine the edges of the top surfaces
solids = gmsh.model.occ.getEntities(dim=3)
lines_upper = []
for solid in solids:
x_min,y_min, z_min, x_max, y_max, z_max = gmsh.model.occ.getBoundingBox(solid[0],solid[1])
lines = gmsh.model.occ.getEntitiesInBoundingBox(x_min,y_min, z_min, x_max, y_max, z_max, dim=1)
#print(x_min,y_min, z_min, x_max, y_max, z_max)
for line in lines:
_, _, z_l, _, _, z_u = gmsh.model.occ.getBoundingBox(line[0], line[1])
if z_l > 0.9*z_max and z_u > 0.9*z_max:
lines_upper.append(line)
#activate
gmsh.model.occ.synchronize()
#create field based on distance from top edges
gmsh.model.mesh.field.add('Distance', 1)
gmsh.model.mesh.field.setNumbers(1, 'CurvesList', [i[1] for i in lines_upper])
gmsh.model.mesh.field.setNumber(1, 'NumPointsPerCurve', 800)
#create threshold field based on the distance field
lc = 0.5
gmsh.model.mesh.field.add('Threshold', 2)
gmsh.model.mesh.field.setNumber(2, 'InField', 1)
gmsh.model.mesh.field.setNumber(2, "SizeMin", lc / 10)
gmsh.model.mesh.field.setNumber(2, "SizeMax", lc)
gmsh.model.mesh.field.setNumber(2, "DistMin", 0.15)
gmsh.model.mesh.field.setNumber(2, "DistMax", 0.5)
#set threshold field as the background mesh
gmsh.model.mesh.field.setAsBackgroundMesh(2)
#prevent over-refinement due to small meshes on the boundary
gmsh.option.setNumber("Mesh.MeshSizeExtendFromBoundary", 0)
gmsh.option.setNumber("Mesh.MeshSizeFromPoints", 0)
gmsh.option.setNumber("Mesh.MeshSizeFromCurvature", 0)
#generate 3D mesh
gmsh.model.mesh.generate(3)
#create directory to save
try:
os.mkdir(mesh_dir)
except FileExistsError:
pass
#write the mesh file
extension = '.msh'
gmsh.write(mesh_dir + name + extension)
#launch the GUI to see the results:
#gmsh.fltk.run()
#close gmesh
gmsh.finalize()

Event Timeline