Skip to content

Instantly share code, notes, and snippets.

@RemDelaporteMathurin
Created March 8, 2025 03:18
Show Gist options
  • Save RemDelaporteMathurin/5dfb8e2ff99f9b3ce7c89bd4d2752bbf to your computer and use it in GitHub Desktop.
Save RemDelaporteMathurin/5dfb8e2ff99f9b3ce7c89bd4d2752bbf to your computer and use it in GitHub Desktop.
read unstructured vtk grid into dolfinx
import pyvista as pv
import numpy as np
import ufl
import basix
from dolfinx.mesh import create_mesh
from mpi4py import MPI
import dolfinx
# Define the points
points = [
[1.0, 1.0, 1.0],
[1.0, -1.0, -1.0],
[-1.0, 1.0, -1.0],
[-1.0, -1.0, 1.0],
[2.0, 2.0, -1.0], # Additional point for the second tetrahedron
]
# Define the cells (two tetrahedra sharing a face)
cells = [4, 0, 1, 2, 3, 4, 0, 1, 2, 4] # First tetrahedron # Second tetrahedron
# Define the cell types
celltypes = [pv.CellType.TETRA, pv.CellType.TETRA]
grid = pv.UnstructuredGrid(cells, celltypes, points)
# grid.plot(show_edges=True, show_axes=True)
grid.cell_data["mean"] = np.arange(grid.n_cells)
# save to vtk file
grid.save("original.vtk")
# Extract connectivity information
cell_connectivity = grid.cells_dict[10]
degree = 1 # Set polynomial degree
cell = ufl.Cell("tetrahedron")
mesh_element = basix.ufl.element("Lagrange", cell.cellname(), degree, shape=(3,))
# Create dolfinx Mesh
mesh_ufl = ufl.Mesh(mesh_element)
dolfinx_mesh = create_mesh(MPI.COMM_WORLD, cell_connectivity, grid.points, mesh_ufl)
function_space = dolfinx.fem.functionspace(dolfinx_mesh, ("DG", 0))
u = dolfinx.fem.Function(function_space)
u.x.array[:] = grid.cell_data["mean"][dolfinx_mesh.topology.original_cell_index]
writer = dolfinx.io.VTXWriter(MPI.COMM_WORLD, "u_as_dg0.bp", u, "BP5")
writer.write(t=0)
@RemDelaporteMathurin
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment