Saving data as .csv files

Hi, Justin, May I ask a question? I have used the code obtaining the information about “Diameter” , but how can I obtain the coordinate of each particle from vtp files? The coordinate is not a component name.

you could store the particle x y z coordinates as 3 distinct DES_USR_VAR that can thus be saved as vtk along with velocity, diameter etc

Thanks very much. But in this case, I will recalculate all the cases.

The points (particle locations) are stored in the vtp files differently then the variable arrays. You can access the points like this:

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

particle_reader= vtk.vtkXMLPolyDataReader()
particle_reader.SetFileName(fname)
particle_reader.Update()
data = particle_reader.GetOutput()

# get the positions!
particle_positions = vtk_to_numpy(data.GetPoints().GetData())

FYI, I learned most of this from reading the VTK docs and the examples.

Thanks very much for your valuable instructions. It really helps me.

@onlyjus How can I get the velocity of the particles? I can get the positions of the particles by the piece of code you provided, but I am not able to get the velocity?

If you are saving the particle velocity in the VTK files, you can access that variable for each particle.

To see the array names, you can do something like this (untested):

particle_reader = (
        vtk.vtkXMLPPolyDataReader()
        if path.suffix == ".pvtp"
        else vtk.vtkXMLPolyDataReader()
    )
particle_reader.SetFileName(path)
particle_reader.Update()

data = particle_reader.GetOutput()
point_data = data.GetPointData()

for i in range(point_data.GetNumberOfArrays()):
    print(point_data.GetArrayName(i))

Then to get the actual data array, do something like this (with the array name you want, printed with the above code):

array = point_data.GetAbstractArray(name)