Hexahedrons are exported to XDMF:
import sys
import os
sys.path.append(os.getcwd())
import meshio
import numpy as np
import HexRegion
# A hexahedron region is built by extusion of a rectangular plane that consists of sub-rectangles
# all with the same winding order... 10 segments are extruded with each segment having a length of 10 units
# in the case cm...
hex_reg = HexRegion(10, 10, 'rect_cross_section.json', isShaderActive=False)
# Gather all hexahedrons
hexahedrons = []
materials = []
for hex_index in range(hex_reg.num_hexas()):
hexahedron = hex_reg.getHexaVerts(hex_index)
material = hex_reg.TAGs[hex_index]
hexahedrons.append(hexahedron)
materials.append(material)
# Convert to numpy array for meshio
hexahedrons = np.array(hexahedrons)
# Create a mapping from material types to integers
material_mapping = {material: idx for idx, material in enumerate(set(materials))}
encoded_materials = np.array([material_mapping[mat] for mat in materials])
# Flatten the hexahedron vertices array for meshio
points = hexahedrons.reshape(-1, 3)
cells = [("hexahedron", np.arange(hexahedrons.shape[0] * 8).reshape(-1, 8))]
# Create the mesh
mesh = meshio.Mesh(points=points, cells=cells)
# Add material data as cell data
mesh.cell_data["material"] = [encoded_materials]
# Export to a file format suitable for dolfinx
mesh.write("hexahedrons.xdmf")
# Print material mapping for reference
print("Material mapping:", material_mapping)
print("Export complete.")
The XDMF files:
importing the hexahedrons.xdmf:
import dolfinx
from dolfinx import mesh
from dolfinx.io import XDMFFile
from mpi4py import MPI
# Create an XDMFFile object for reading
with XDMFFile(MPI.COMM_WORLD, "hexahedrons.xdmf", "r") as file:
mesh_obj = mesh.create_mesh(MPI.COMM_WORLD, file.read_mesh())
print("Mesh created successfully!")
'''
Traceback (most recent call last):
File "/home/prusso/dolfinx-hexahedron/main.py", line 8, in <module>
mesh_obj = mesh.create_mesh(MPI.COMM_WORLD, file.read_mesh())
^^^^^^^^^^^^^^^^
File "/home/prusso/spack/var/spack/environments/fenicsx-env/.spack-env/view/lib/python3.11/site-packages/dolfinx/io/utils.py", line 258, in read_mesh
cell_shape, cell_degree = super().read_cell_type(name, xpath)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: <Grid> with name 'mesh' not found.
'''
It seems that are some additional things to take into account when preparing an XDMF for the purposes of importing it into dolfinx.
So far I would like to do things in a way that will satisfy both dolfinx and PyVista.PyVista seems to squack a bit about timeframe data. Why this traceback about Grid from doflinx? How can I resolve the issue and get the XDMF file imported into dolfinx?