Page MenuHomec4science

meshing.py
No OneTemporary

File Metadata

Created
Mon, Mar 3, 05:19

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.option.setNumber("Mesh.MshFileVersion", 2.2)
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 = z_max/3
gmsh.model.mesh.field.add('Threshold', 2)
gmsh.model.mesh.field.setNumber(2, 'InField', 1)
gmsh.model.mesh.field.setNumber(2, "SizeMin", lc)
gmsh.model.mesh.field.setNumber(2, "SizeMax", lc)
gmsh.model.mesh.field.setNumber(2, "DistMin", 0.1)
gmsh.model.mesh.field.setNumber(2, "DistMax", 2)
"""
#h_max = 1
#h_min = 0.085
#a = (h_min-h_max)/z_max**2
#b = 2*(h_min-h_max)/z_max
#c = h_max
#gmsh.model.mesh.field.setString(3, "F", "-((2-0.1)/2)*z*z+((2-0.1)/2)*{}*{}+0.01".format(z_max, z_max))
#gmsh.model.mesh.field.setString(3, "F", "{}*z*z + {}".format(a,c))
h_max = 1
h_min = 0.07
z_max_bar = 2
m = -(h_max-h_min)/z_max_bar
#gmsh.model.mesh.field.setString(3, "F", "-((2-0.1)/2)*z*z+((2-0.1)/2)*{}*{}+0.01".format(z_max, z_max))
#gmsh.model.mesh.field.setString(3, "F", "{}*z*z + {}".format(a,c))
if z_max < 3*h_min:
gmsh.model.mesh.field.add("Box", 3)
gmsh.model.mesh.field.setNumber(3, "VIn", z_max/3)
gmsh.model.mesh.field.setNumber(3, "VOut", z_max/3)
gmsh.model.mesh.field.setNumber(3, "XMin", x_min)
gmsh.model.mesh.field.setNumber(3, "XMax", x_max)
gmsh.model.mesh.field.setNumber(3, "YMin", y_min)
gmsh.model.mesh.field.setNumber(3, "YMax", y_max)
gmsh.model.mesh.field.setNumber(3, "ZMin", z_min)
gmsh.model.mesh.field.setNumber(3, "ZMax", z_max)
gmsh.model.mesh.field.setAsBackgroundMesh(3)
else:
gmsh.model.mesh.field.add('MathEval',3)
gmsh.model.mesh.field.setString(3, "F", "{}*(z-{}) + {}".format(m,z_max, h_min))
#set threshold field as the background mesh
gmsh.model.mesh.field.setAsBackgroundMesh(3)
#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)
#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