Hello,
I would like to create a mesh with given by a cube with a spherical hole in it. I can do this with the following code
import meshio
import gmsh
import pygmsh
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("resolution")
args = parser.parse_args()
resolution = (float)(args.resolution)
geometry = pygmsh.occ.Geometry()
model = geometry.__enter__()
L1 = 1
L2 = 1
L3 = 1
r = 0.25
c_r = [0.5, 0.5, 0.5]
box = model.add_box([0, 0, 0], [L1, L2, L3], mesh_size=resolution)
ball = model.add_ball(c_r, r, mesh_size=resolution)
difference = model.boolean_difference(box, ball)
model.synchronize()
model.add_physical( difference, "difference" )
geometry.generate_mesh(dim=3)
gmsh.write("solution/mesh.msh")
model.__exit__()
def create_mesh(mesh, cell_type, prune_z=False):
cells = mesh.get_cells_type(cell_type)
cell_data = mesh.get_cell_data("gmsh:physical", cell_type)
out_mesh = meshio.Mesh(points=mesh.points, cells={
cell_type: cells}, cell_data={"name_to_read": [cell_data]})
return out_mesh
mesh_from_file = meshio.read("solution/mesh.msh")
tetrahedron_mesh = create_mesh(mesh_from_file, "tetra", True)
meshio.write("solution/tetrahedron_mesh.xdmf", tetrahedron_mesh)
which works
$clear; clear; rm -r solution; mkdir solution; python3 generate_3dmesh_box_ball.py 0.1
and mesh.msh looks like this:
I can tag the volume given by the difference between the cube and the ball with model.add_physical( difference, "difference" )
which is tagged with name_to_read = 1
.
I would like to tag the ball surface, only the surface, not its volume, but I don’t know how to do it. Do you know how to do this ?
Note that I tried multiple solutions: this shows how to tag a sphere with its volume, which is not what I want. This allowed me to extract the surfaces
with surfaces = gmsh.model.occ.getEntities(dim=2)
but then it is unclear how to tag them: The webpage uses gmsh.model.addPhysicalGroup
while in the code above I use model.add_physical
, which does not work with argument surfaces[0]
.
Thank you