Loading VTK data into Python

An update to this discussion in response to a related question in the form post “Using Paraview or Matlab to do calculations with output fields”.

Rather than use the VTK commands directly, I’ve found that using PyVista provides a much more convenient way of loading data into python, and also providing a wide range of useful functionality for manipulating and visualizing the data. PyVista is largely built on VTK, and if preferred you can use both PyVista and direct VTK functionality.

There are now two examples for how to use PyVista for loading and visualizing ASPECT data in the repository, and another workflow example is provide below. At the bottom of the post are some suggestions for how to install pyvista in anaconda.

If you have any further questions on PyVista or related topics, please feel free to post them here!

Example PyVista Workflow

# Import PyVista
import pyvista as pv

# Read solution data into a variable named mesh, which contains all of the solution data. Replace the name of the pvtu with the full or relative path and name of the specific file you wish to load.
mesh = pv.read(‘solution-00000.pvtu’)

# Show all variables (data) contained with the objected mesh.point_data (you can also cell-wise data via mesh.cell_data. The output will include a list of the fields (T, p, etc) contained within the vtu file..
mesh.point_data

# Load point (x,y,z) data into a unique variable (numpy ndarray)
points = mesh.points

# Load temperature data into a unique variable. Note that the same thing can be accomplished with temperature = mesh.point_data['T']
temperature = mesh[‘T’]

PyVista Installation Instructions

  1. While PyVista can be installed through Anaconda or PIP, the most straightforward way to ensure it works and does not produce conflicts with other python libraries is to create an anaconda environment with the package dependencies found at pyvista/environment.yml at main · pyvista/pyvista · GitHub.

  2. After downloading this file, create a new anaconda environment using this file with conda env create -f environment.yml.

  3. Once that environment has been created, activate it with conda activate pyvista-env and then install pyvist with conda install -c conda-forge pyvista.

  4. You can check to see if pyvista is installed with import pyvista as pv.