Read VTK or transform VTK to XML (or h5) function data

Hi

I have legacy field data living in format of .vtk. I want to use it as the initial condition as my simulation. I definitely see answers about xml and h5. But did not find discussion about vtk

Thanks
Victor

1 Like

So you need to load a mesh and field data from VTK? I’ve used this workflow:

You’d have to convert the mesh first. Try meshio to create a DOLFIN XML mesh, or use for instance gmsh to save the VTK mesh in gmsh’s MSH format, then dolfin-convert (apparently still comes with FEnICS) to convert MSH to XML. The XML mesh can be converted to XDMF or HDF5 with the “standard methods”.

Given the mesh the VTK data fields are defined for, reading the data is easy. The data should be ordered by degree-of-freedom. In a FEniCS script, set

parameters['reorder_dofs_serial'] = False

at the beginning, read mesh, create a function space and receiving function. Then just open the VTK file, jump to the section where the data is given, read all data lines, convert to a numpy array.
Finally, set u.vector()[:] = data (or possibly data.T.flatten() if reading vector data).

Hope this helps!

2 Likes

Hi,

Thanks for the reply, this is just about what I had in mind if there’s no direct way to read in vtk. I guess I’d just go with this path :joy:

Thanks,
Victor

Hi dajuno,
many thanks for providing the instructions, but can you please provide a sample code where you implement these steps?

parameters['reorder_dofs_serial'] = False

at the beginning, read mesh, create a function space and receiving function. Then just open the VTK file, jump to the section where the data is given, read all data lines, convert to a numpy array.
Finally, set u.vector()[:] = data (or possibly data.T.flatten() if reading vector data).

cause usually I got lost whenever I try to implement it!

Thanks in advance,
Best,
Mahmoud