import gmsh
import csv
def write_faces(element_types, element_tags, element_node_tags):
# Write faces to CSV
with open('beam_faces.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
for i, elem_node_tags_current in enumerate(element_node_tags):
for j in range(0, len(elem_node_tags_current), 8): # 8 nodes per hexahedron
face = elem_node_tags_current[j:j+8]
writer.writerow(face)
def write_nodes(node_tags, node_coords, _):
# Write nodes to CSV
with open('beam_nodes.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
for i in range(len(node_tags)):
writer.writerow([node_tags[i], node_coords[3*i], node_coords[3*i+1], node_coords[3*i+2]])
w = 4
h = 8
d = 120
gmsh.initialize()
gmsh.logger.start()
# Create a hexahedral box
box_tag = gmsh.model.occ.add_box(0, 0, 0, w, h, d)
gmsh.model.occ.synchronize()
# Recombine tetrahedrons to hexahedrons
gmsh.option.setNumber("Mesh.RecombinationAlgorithm", 2)
gmsh.option.setNumber("Mesh.RecombineAll", 2)
gmsh.option.setNumber("Mesh.CharacteristicLengthFactor", 1)
gmsh.model.mesh.generate(3)
element_types, element_tags, element_node_tags = gmsh.model.mesh.getElements(dim=3) # 3D elements
node_tags, node_coords, _ = gmsh.model.mesh.getNodes(dim=3) # 3D nodes
write_faces(element_types, element_tags, element_node_tags)
write_nodes(node_tags, node_coords, _)
gmsh.write('getnodes_getfaces.vtk')
print('done...')
import pyvista as pv
# Inspect the VTK file using PyVista
vtk_mesh = pv.read("getnodes_getfaces.vtk")
# Get the cell types
cell_types = vtk_mesh.celltypes
# Check if the cell type is HEXAHEDRON
if pv.CellType.HEXAHEDRON in cell_types:
print("The mesh contains hexahedron cells.")
else:
print("The mesh does not contain hexahedron cells.")
PyVista declared that the mesh does not contain hexahedron cells. So far it is my intent that the mesh only contains hexahedron cells.
https://docs.fenicsproject.org/dolfinx/v0.4.1/python/demos/demo_gmsh.html
# Recombine tetrahedrons to hexahedrons
gmsh.option.setNumber("Mesh.RecombinationAlgorithm", 2)
gmsh.option.setNumber("Mesh.RecombineAll", 2)
gmsh.option.setNumber("Mesh.CharacteristicLengthFactor", 1)
What is going wrong here? It seems someplace along the way there is an issue creating the hexahedron. Is gmsh not recombining properly to hexahedron or is PyVista having some type of difficulty reading the hexahedron VTK? How can I create the mesh as hexahedron? Is there a way to view the hexahedron VTK with PyVista?
I tried many different ways to use gmsh.option.setNumber(). So far none of the ways that I had tried had resulted in PyVista reading the mesh as one of hexahedral cells.
Francesco Amatto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.