Hello Dokken, I tried this but that gives me the wrong result. I will now share the full code and try to explain my approach. I have extracted Point Data from VTU file and mesh conversion via Meshio and then interpolated the Point Data into a Fenics function. Now I want to interpolate this function (called point_function
in the code) into a seperate mesh and for this I created another function in this mesh (called T
).
! pip3 install pandas
! pip3 install meshio
! pip3 install h5py
! pip install pyvista
! pip install panel
import numpy as np
import pyvista
import panel
import meshio
import ufl
import dolfinx as dfx
import numpy as np
from dolfinx.io import XDMFFile
from dolfinx import plot
from mpi4py.MPI import COMM_WORLD
# use meshio to read vtk mesh
filename = "new.vtu"
vtk_mesh = meshio.vtu.read(filename)
# cell data field as named in vtu (see paraview)
fieldname = "Temperature"
# use meshio to convert to and write a dolfinx mesh
dim = 3
ps=vtk_mesh.points[:,:dim]
cs = vtk_mesh.get_cells_type("tetra")
dolfinx_mesh = meshio.Mesh(points=ps, cells={"tetra": cs})
meshio.write("test.xdmf", dolfinx_mesh)
# read mesh with dolfinx
comm = COMM_WORLD
with XDMFFile(comm, "test.xdmf", "r") as file:
dolfinx_mesh = file.read_mesh(name="Grid")
# create the necessary grid functions
cell_element=ufl.FiniteElement("DG", dolfinx_mesh.ufl_cell(), 0)
point_element=ufl.FiniteElement("Lagrange", dolfinx_mesh.ufl_cell(), 1)
cell_function_space=dfx.fem.FunctionSpace(dolfinx_mesh, cell_element)
point_function_space=dfx.fem.FunctionSpace(dolfinx_mesh, point_element)
cell_function = dfx.fem.Function(cell_function_space)
point_function = dfx.fem.Function(point_function_space)
point_function.name = fieldname
# Global indexes owned locally
point_ids = dolfinx_mesh.geometry.input_global_indices
#cell_ids = dolfinx_mesh.geometry.input_cell_indices
# Map VTK data directy onto dolfinx vectors
#cell_function.x.array[:] = vtk_mesh.cell_data[fieldname][ids]
point_function.x.array[:] = vtk_mesh.point_data[fieldname][point_ids]
# create new mesh
test_mesh = dfx.mesh.create_box(COMM_WORLD, [np.array([0.4, -0.125, -0.3]), np.array([0.9, 0.125, 0.0])],
[50, 25, 25], cell_type=dfx.mesh.CellType.tetrahedron)
tdim = test_mesh.topology.dim
fdim = tdim - 1
test_mesh.topology.create_connectivity(fdim, tdim)
boundary_facets = dfx.mesh.exterior_facet_indices(test_mesh.topology)
pyvista.set_jupyter_backend("panel")
pyvista.start_xvfb()
topology, cell_types, geometry = plot.create_vtk_mesh(test_mesh, tdim)
grid = pyvista.UnstructuredGrid(topology, cell_types, geometry)
plotter = pyvista.Plotter()
plotter.add_mesh(grid, show_edges=True)
plotter.view_xy()
if not pyvista.OFF_SCREEN:
plotter.show()
else:
figure = plotter.screenshot("fundamentals_mesh.png")
#create new function in new mesh
V0 = ufl.FiniteElement("Lagrange", test_mesh.ufl_cell(), 1)
V = dfx.fem.FunctionSpace(test_mesh, V0)
T = dfx.fem.Function(V)
# this gives an error
from dolfinx.fem import create_nonmatching_meshes_interpolation_data
# this is what dokken suggests for interpolating function on old mesh in new mesh fo 0.6.0 dolfinx
T.interpolate(point_function)
T.x.scatter_forward()
u_topology, u_cell_types, u_geometry = plot.create_vtk_mesh(V)
u_grid = pyvista.UnstructuredGrid(u_topology, u_cell_types, u_geometry)
u_grid.point_data["T"] = T.x.array.real
u_grid.set_active_scalars("T")
u_plotter = pyvista.Plotter()
u_plotter.add_mesh(u_grid, show_edges=True, cmap="rainbow")
u_plotter.view_xy()
if not pyvista.OFF_SCREEN:
# result is not the same
u_plotter.show()
p_topology, p_cell_types, p_geometry = plot.create_vtk_mesh(point_function_space)
p_grid = pyvista.UnstructuredGrid(p_topology, p_cell_types, p_geometry)
p_grid.point_data["T"] = point_function.x.array.real
p_grid.set_active_scalars("T")
p_plotter = pyvista.Plotter()
p_plotter.add_mesh(p_grid, show_edges=True, cmap="rainbow")
p_plotter.view_xy()
if not pyvista.OFF_SCREEN:
# this is what it should look like
p_plotter.show()
Original Function:
Function (on new mesh) into which it should be interpolated - currently not being properly interpolated:
One thing I should have mentioned before is that the original mesh is unstructured and the new one is structured. Does that cause an issue?