How to read the vtu files with python?

Hey everyone,

I have a question on how to read the vtu files. Can someone help me with it? Thanks!

I created some simple vtu files using this mfx file.
vtu.mfx (11.8 KB)

I tried reading the vtu files that contains information about flow velocity along the x-axis, and transform the information into a matrix for my machine learning algorithm. However, when I tried reading the vtu files of the simulation results, I got an error saying that my file was empty. Here is the python code that I used to read the vtk.


import numpy as np
import vtk
from vtk.util.numpy_support import vtk_to_numpy

reader = vtk.vtkXMLUnstructuredGridReader()
reader.SetFileName(“BACKGROUND_IC_0005.vtu”)
reader.Update()
data = reader.GetOutput()

u = vtk_to_numpy(data.GetPointData().GetArray(2))


Can anyone let me know if I had a mistake in my python code or if I had some misunderstand about how mfix creates vtu files?

Thanks,
Jack

I think it is caused because you did not give the keyword.
Please find the attachment and this is my previous work. read-vtu-file.py (1.7 KB)

2 Likes

Thank you Yuanhe! Sorry I still don’t understand what the keyword is though. Did you mean keyword in the python script or in the mfx file?

Processing: ENTIRE_DOMAIN_0372.vtu… in mfix file.

Thanks Yuanhe! Did you intend to attach a link or a file? I could not open it for some reason.

Please see Saving data as .csv files - #12 by cgw