Skip to content

Worked examples

These examples develop a complete application from its data layout to the host loop and a check of the result. Start with the data examples below for scientific analysis, or heat diffusion for a first simulation. Every script is included with its explanation and available as a download; it needs no separate examples repository.

The scripts target the current Tack checkout. They use NumPy and the selected backend. The simulation tutorials use VTK for optional figures; the new data comparisons use Matplotlib. Install from source as in Getting Started, then run from the repository root:

uv run python docs/examples/heat.py --arch cpu --check
uv run --with vtk python docs/examples/heat.py --output heat.png

In a checkout, uv sync --extra cpu --extra interop (with your other extras) installs VTK from the lock instead, and uv run python ... --output then needs no --with vtk.

With an installed Tack environment, download a script and use python heat.py. Install VTK separately if you want its figure. --check uses smaller inputs and checks a numerical reference or independent properties; it is not a performance benchmark. --arch requires the named backend to initialize.

If you use a local VTK build, make its Python site-packages directory available through PYTHONPATH and use uv run python ... without --with vtk. The build must match your Python version. These figures use ordinary VTK image, geometry, rendering and PNG-writing modules; they do not require DLPack support. Offscreen geometry and image-panel rendering needs a working VTK rendering backend.

Example What you will learn Prerequisites
Heat diffusion Stencils, boundaries, two buffers, host timesteps Arrays and kernels
Advecting dye Vector arrays, interpolation, device functions Vectors and loops
Trail-following agents Random state, templates, scatter and diffusion Vectors and atomics
Marching squares Count, scan, allocation and ordered output Local arrays and scans
Particle-to-grid simulation Matrix arrays, interpolation and atomic scatter Vectors, matrices and atomics
Tiled N-body forces Shared memory and barriers, with a CPU path Sequential loops and workgroups

Datasets and visualization examples

The programs that develop the dataset API, each with an analytic check, are in the Visualization Guide's worked examples.

Images from the examples

Heat diffusing over time

Dye rotating over time

Trail-following agents

Contour segments

A falling MPM block

Bodies and their accelerations

These figures were generated by running the scripts on CPU. GPU results may differ in low floating-point bits, random-direction transcendental rounding, or long simulation trajectories. See Numerical checks.

VTK displays the downloaded image and geometry snapshots. For an example sharing storage between Tack and VTK, see the complete CPU exchange.

Origins and licenses

The heat example adapts an existing Tack example. Dye, Physarum, marching squares and MPM adapt public Taichi examples; N-body adapts NVIDIA Warp's tile example. Each page links to the original source and explains the teaching changes. The adapted Taichi and Warp scripts retain Apache-2.0; other scripts use Tack's BSD-3-Clause license. See the notices and Apache license text.