Page MenuHomec4science

meshing.py
No OneTemporary

File Metadata

Created
Fri, Oct 18, 15:25

meshing.py

import gmsh
import os
import sys
import numpy as np
import argparse
"""
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 parse_args():
"""
Function to handle arguments
"""
parser = argparse.ArgumentParser(description='Run given layer')
parser.add_argument("layer_no", type=int, help='layer_number')
args = parser.parse_args()
return args
def main():
args = parse_args()
#initialize gmesh
layer_no = args.layer_no
name = 'temperature_coarse_'+str(layer_no).zfill(3)
gmsh.initialize()
gmsh.option.setNumber("Mesh.MshFileVersion", 2.2)
gmsh.model.add(name)
z_lim = 0.00801
x_lim = 0.055
#read the .step file
#gmsh.model.occ.importShapes(geom_dir + name + ext)
box_dim_tags = gmsh.model.occ.addBox(0, 0, z_lim, x_lim, 0.005, layer_no*0.00003 - z_lim)
#get solid part of the model, determine the edges of the top surfaces
solids = gmsh.model.occ.getEntities(dim=3)
lines_upper = []
faces_upper = []
x_max_dim = 0
y_max_dim = 0
z_max_dim = 0
x_min_dim = 0
y_min_dim = 0
z_min_dim = 0
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)
surfaces = gmsh.model.occ.getEntitiesInBoundingBox(x_min,y_min, z_min, x_max, y_max, z_max, dim=2)
x_min_dim = np.min([x_min_dim, x_min])
y_min_dim = np.min([y_min_dim, y_min])
z_min_dim = np.min([z_min_dim, z_min])
x_max_dim = np.max([x_max_dim, x_max])
y_max_dim = np.max([y_max_dim, y_max])
z_max_dim = np.max([z_max_dim, 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)
for face in surfaces:
_, _, z_l, _, _, z_u = gmsh.model.occ.getBoundingBox(face[0], face[1])
if z_l > 0.999*z_max and z_u > 0.999*z_max:
faces_upper.append(face)
#activate
gmsh.model.occ.synchronize()
if z_max_dim < 272*0.00003:
gmsh.model.mesh.field.add("Box", 1)
gmsh.model.mesh.field.setNumber(1, "VIn", 0.0002)
gmsh.model.mesh.field.setNumber(1, "VOut", 0.0002)
gmsh.model.mesh.field.setNumber(1, "XMin", x_min_dim)
gmsh.model.mesh.field.setNumber(1, "XMax", x_max_dim)
gmsh.model.mesh.field.setNumber(1, "YMin", y_min_dim)
gmsh.model.mesh.field.setNumber(1, "YMax", y_max_dim)
gmsh.model.mesh.field.setNumber(1, "ZMin", z_min_dim)
gmsh.model.mesh.field.setNumber(1, "ZMax", z_max_dim)
gmsh.model.mesh.field.setAsBackgroundMesh(1)
else:
gmsh.model.mesh.field.add('Distance', 2)
gmsh.model.mesh.field.setNumbers(2, 'SurfacesList', [i[1] for i in faces_upper])
gmsh.model.mesh.field.setNumber(2, 'NumPointsPerCurve', 2000)
gmsh.model.mesh.field.add('MathEval', 3)
gmsh.model.mesh.field.setString(3, "F", "0.25*F2+0.0002")
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'
directory = "/scratch/kubilay/mechanical_model/results/coarse/"
gmsh.write(directory+name+extension)
#launch the GUI to see the results:
#gmsh.fltk.run()
#close gmesh
gmsh.finalize()
if __name__=='__main__':
main()

Event Timeline