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#
pyvisfile.vtk.VTK_LAGRANGE_CURVE#
pyvisfile.vtk.VTK_LAGRANGE_TRIANGLE#
pyvisfile.vtk.VTK_LAGRANGE_QUADRILATERAL#
pyvisfile.vtk.VTK_LAGRANGE_TETRAHEDRON#
pyvisfile.vtk.VTK_LAGRANGE_HEXAHEDRON#
pyvisfile.vtk.VTK_LAGRANGE_WEDGE#

Building blocks#

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

XML elements#

class pyvisfile.vtk.XMLElement(tag, **attributes)[source]#
write(file)[source]#

XML generators#

class pyvisfile.vtk.InlineXMLGenerator(compressor=None, vtk_file_version=None)[source]#
__init__(compressor=None, vtk_file_version=None)[source]#
Parameters:

vtk_file_version

a string "x.y" with the desired VTK XML file format version. Relevant versions are as follows:

  • "0.1" is the original version.

  • "1.0" added support for 64-bit indices and offsets, as described here.

  • "2.0" added support for ghost array data, as described here.

  • "2.1": added support for writing additional information attached to a DataArray using information keys.

  • "2.2": changed the node numbering of the hexahedron, as described here.

__call__(vtkobj)[source]#

Return an XMLElement.

class pyvisfile.vtk.AppendedDataXMLGenerator(compressor=None, vtk_file_version=None)[source]#
__call__(vtkobj)[source]#

Return an XMLElement.

class pyvisfile.vtk.ParallelXMLGenerator(pathnames)[source]#
__call__(vtkobj)[source]#

Return an XMLElement.

Convenience functions#

pyvisfile.vtk.write_structured_grid(file_name, mesh, cell_data=None, point_data=None, overwrite=False)[source]#

Examples#

Writing a structured mesh#

# contributed by Luke Olson

import numpy as np

n = 50
x, y = np.meshgrid(np.linspace(-1, 1, n),
np.linspace(-1, 1, n))

u = np.exp(-50 * (x**2 + y**2))

from pyvisfile.vtk import write_structured_grid
mesh = np.rollaxis(np.dstack((x, y)), 2)
write_structured_grid("test.vts", mesh,
        point_data=[("u", u[np.newaxis, :, :])])

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

Writing a collection of points#

Note

Observe that this is written as a ‘unstructured grid’, even though there is not much grid here. However, by supplying connectivity data, it is possible to generalize from this to actual unstructured meshes.

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.)