Usage Reference for pyvisfile.vtk

Constants

Vector formats

pyvisfile.vtk.VF_LIST_OF_COMPONENTS

[[x0,y0,z0], [x1,y1,z1]

pyvisfile.vtk.VF_LIST_OF_VECTORS

[[x0,x1], [y0,y1], [z0,z1]]

Element types

pyvisfile.vtk.VTK_VERTEX
pyvisfile.vtk.VTK_POLY_VERTEX
pyvisfile.vtk.VTK_LINE
pyvisfile.vtk.VTK_POLY_LINE
pyvisfile.vtk.VTK_TRIANGLE
pyvisfile.vtk.VTK_TRIANGLE_STRIP
pyvisfile.vtk.VTK_POLYGON
pyvisfile.vtk.VTK_PIXEL
pyvisfile.vtk.VTK_QUAD
pyvisfile.vtk.VTK_TETRA
pyvisfile.vtk.VTK_VOXEL
pyvisfile.vtk.VTK_HEXAHEDRON
pyvisfile.vtk.VTK_WEDGE
pyvisfile.vtk.VTK_PYRAMID

Building blocks

class pyvisfile.vtk.DataArray(name, container, vector_padding=3, vector_format=0, components=None)
class pyvisfile.vtk.UnstructuredGrid(points, cells, cell_types)
add_pointdata(data_array)
add_celldata(data_array)
class pyvisfile.vtk.StructuredGrid(mesh)
add_pointdata(data_array)
add_celldata(data_array)

XML elements

class pyvisfile.vtk.XMLElement(tag, **attributes)
write(file)

XML generators

class pyvisfile.vtk.InlineXMLGenerator(compressor=None)
__call__(vtkobj)

Return an XMLElement.

class pyvisfile.vtk.AppendedDataXMLGenerator(compressor=None)
__call__(vtkobj)

Return an XMLElement.

class pyvisfile.vtk.ParallelXMLGenerator(pathnames)
__call__(vtkobj)

Return an XMLElement.

Convenience functions

pyvisfile.vtk.write_structured_grid(file_name, mesh, cell_data=[], point_data=[])

Examples

Writing a structured mesh

from pyvisfile.vtk import write_structured_grid

import numpy as np

angle_mesh = np.mgrid[1:2:10j, 0:2*np.pi:20j, 0:np.pi:30j]

r = angle_mesh[0, np.newaxis]
phi = angle_mesh[1, np.newaxis]
theta = angle_mesh[2, np.newaxis]
mesh = np.vstack((
    r*np.sin(theta)*np.cos(phi),
    r*np.sin(theta)*np.sin(phi),
    r*np.cos(theta),
    ))

from pytools.obj_array import make_obj_array
vec = make_obj_array([
    np.sin(theta)*np.cos(phi),
    np.sin(theta)*np.sin(phi),
    np.cos(theta),
    ])

write_structured_grid("yo.vts", mesh,
        point_data=[("phi", phi), ("vec", vec)])

(You can find this example as examples/vtk-structured.py in the PyVisfile source distribution.)

Writing an unstructured mesh

import numpy as np
from pyvisfile.vtk import ( 
    UnstructuredGrid, DataArray,
    AppendedDataXMLGenerator,
    VTK_VERTEX, VF_LIST_OF_VECTORS, VF_LIST_OF_COMPONENTS)

n = 5000
points = np.random.randn(n, 3)

data = [
        ("p", np.random.randn(n)),
        ("vel", np.random.randn(3, n)),
]
file_name = "points.vtu"
compressor = None

grid = UnstructuredGrid(
        (n, DataArray("points", points, vector_format=VF_LIST_OF_VECTORS)),
        cells=np.arange(n, dtype=np.uint32),
        cell_types=np.asarray([VTK_VERTEX] * n, dtype=np.uint8))

for name, field in data:
    grid.add_pointdata(DataArray(name, field,
        vector_format=VF_LIST_OF_COMPONENTS))

from os.path import exists
if exists(file_name):
    raise RuntimeError("output file '%s' already exists"
        % file_name)

outf = open(file_name, "w")
AppendedDataXMLGenerator(compressor)(grid).write(outf)
outf.close()

(You can find this example as examples/vtk-unstructured-points.py in the PyVisfile source distribution.)