Load many files, collect, and animate

The generated travelling-wave examples represent the same positive density wave at successive times. These analytic fixtures demonstrate file handling and visualization; they are not numerical simulation results. Each file has its own frame number and time metadata.

Download the example bundle. The zero-padded names (travelling_wave_000.gkyl through frame 015) make lexicographic sorting match time order. The script uses sorted(Path.glob(...)); the CLI accepts the quoted wildcard. Use sort with your own files if their names or input order do not match time order.

collect(frames) makes a single dataset whose leading grid axis is time. A 1-D sequence therefore becomes a 2-D space–time plot. Its timestamps are sorted using the files’ metadata. Interpolate before collecting.

animate(frames) instead draws the original individual frames in sequence. To play a collected space–time dataset, use animate([collected_data], collected=True). The example keeps a fixed value range across all frames, so a changing color does not merely reflect a changing scale. The second animation shows a 2-D travelling wave. saveframes also saves each frame as a PNG; fps controls playback speed, independently of the physical times stored in the files.

Both GIFs below are generated and checked frame by frame during the build. Use the browser’s image controls or open the images separately to inspect them.

Python script

python examples/scripts/07_collect_animate.py
"""Load numbered frames, assemble an x–t diagram, and animate 1D and 2D data."""

import numpy as np
import postgkyl as pg
from _example_paths import TEST_DATA, prepare_output_dir

OUTPUT_DIR = prepare_output_dir()
paths = sorted((TEST_DATA / "generated").glob("travelling_wave_*.gkyl"))
frames = [pg.load(path).interpolate() for path in paths]
assert len(frames) == 16

# collect adds a leading time coordinate, taken from each file's metadata.
history = pg.collect(frames)
np.testing.assert_allclose(history.grid[0], [d.ctx["time"] for d in frames])
np.testing.assert_allclose(history.values, np.stack([d.values for d in frames]))
history.plot(title="Travelling wave: space–time diagram",
             xlabel="Time",
             ylabel="x",
             clabel="Density [normalized]",
             no_show=True,
             saveas=OUTPUT_DIR / "07_collect.png",
             dpi=100)

# animate takes the individual frames, not the dataset returned by collect.
# saveframes also keeps every PNG for analysis or a presentation.
pg.animate(frames,
           saveframes=str(OUTPUT_DIR / "07_wave_frame"),
           saveas=str(OUTPUT_DIR / "07_wave.gif"),
           fps=8,
           dpi=90,
           figsize=(6, 4),
           no_show=True)

surface_paths = sorted((TEST_DATA / "generated").glob("wave_surface_*.gkyl"))
surface_frames = [pg.load(path).interpolate() for path in surface_paths]
assert len(surface_frames) == 12
pg.animate(surface_frames,
           saveframes=str(OUTPUT_DIR / "07_surface_frame"),
           saveas=str(OUTPUT_DIR / "07_surface.gif"),
           fps=6,
           dpi=90,
           figsize=(6, 4),
           no_show=True)

Equivalent CLI commands

Run from the extracted example bundle (or repository root). Create an output directory first:

mkdir -p output
pgkyl 'tests/test_data/generated/travelling_wave_*.gkyl' interpolate collect plot --title \
    'Travelling wave: space–time diagram' --xlabel Time --ylabel x --clabel \
    'Density [normalized]' --no_show --dpi 100 --saveas output/07_collect.png

07_collect.png: Identical pixels (1 frame(s)) and timing.

Python and CLI outputs
Python output for 07_collect.png CLI output for 07_collect.png
pgkyl 'tests/test_data/generated/travelling_wave_*.gkyl' interpolate animate --saveframes \
    output/07_wave_frame --fps 8 --dpi 90 --figsize 6 4 --no_show --saveas \
    output/07_wave.gif

07_wave.gif: Identical pixels (16 frame(s)) and timing.

Python and CLI outputs
Python output for 07_wave.gif CLI output for 07_wave.gif
pgkyl 'tests/test_data/generated/wave_surface_*.gkyl' interpolate animate --saveframes \
    output/07_surface_frame --fps 6 --dpi 90 --figsize 6 4 --no_show --saveas \
    output/07_surface.gif

07_surface.gif: Identical pixels (12 frame(s)) and timing.

Python and CLI outputs
Python output for 07_surface.gif CLI output for 07_surface.gif

Animation controls

Animation accepts the plotting controls for contours, quiver, streamlines, scatter, line styling, labels, legends, colorbars, axis transforms, logarithmic scales, aspect ratio, and mesh edges. subplots=True gives each dataset in a frame its own panels; squeeze=True overlays components in one panel. group=0 or group=1 draws field lineouts along that coordinate.

xlim, ylim, and zlim accept pairs (or comma-separated strings) and supersede individual minimum/maximum bounds. Each component panel has its own fixed value or color range spanning all frames, including value shifts and scaling. Datasets drawn in the same panel share a range; subplots=True keeps their ranges separate, and squeeze=True combines the components’ ranges. variable_range=True recomputes each panel’s range over the blocks in that frame, while respecting explicit bounds. cutoffglobalrange selects a central fraction of each panel’s dataset extrema, between zero and one.

The generated CLI uses the public API’s names: the former --float control is --variable_range, --nsubplotrow / --nsubplotcol are --num_subplot_row / --num_subplot_col, and --fix-aspect is --fixaspect. Use --no_legend, --no_colorbar, --no_showgrid, and --no_show to disable default display features. --forcelegend forces a legend. All options are listed by pgkyl animate --help.

use selects a tag; grouptags=True produces separate animations with tag suffixes on output filenames. multiblock=True combines blocks with the same frame index. saveframes writes numbered PNG files; nproc selects the number of frame workers and tmpdir places temporary frames. GIF, WebP, and APNG output use Pillow; MP4, MOV, AVI, and MKV require ffmpeg.

Video encoders on clusters

Finding an ffmpeg executable is not enough: it must contain an encoder for the requested video format. Animation checks encoders before generating frames. It prefers software H.264 (libx264 or libopenh264) in the executable on PATH, then tries the executable supplied by imageio-ffmpeg. If neither provides software H.264, it uses mpeg4. Hardware encoders are used only when explicitly requested.

Use --codec mpeg4 (Python: codec="mpeg4") to choose MPEG-4 explicitly, or --codec libx264 to require that encoder. Explicit choices produce a clear error if unavailable. For example:

pgkyl "frames_*.gkyl" interpolate animate --saveas movie.mp4 --nproc 10 --codec mpeg4

See Installation for ffmpeg setup. GIF, WebP, and APNG require no ffmpeg installation. Video export reports the chosen executable and encoder if encoding fails, uses a noninteractive canvas when compiling saved frames, and pads odd-sized H.264/MPEG-4 frames to even dimensions.