Skip to main content

vicflow

PyPI Python Downloads per month Total downloads CI License GitHub stars

GPU-accelerated 2D vortex-in-cell fluid simulator for generative art. High-Reynolds flows on CUDA, ROCm and Apple Silicon. Based on Mark J. Stock's vic2d.

Status: early development. What exists today is the solver on periodic domains and between no-slip walls, with density, buoyancy and color, validated against vic2d, with a NumPy reference backend and a PyTorch backend (CUDA, Apple Silicon, CPU) whose advection runs as a fused native kernel on GPUs (Triton, Metal). Obstacles, open boundaries and scene files come next.

Installation

pip install vicflow            # NumPy backend (CPU)
pip install "vicflow[torch]"   # adds the PyTorch backend: CUDA, Apple Silicon (MPS), CPU

vicflow needs Python 3.11 or later. With an NVIDIA driver older than 580 (CUDA 12.x), install PyTorch from https://download.pytorch.org/whl/cu126 before vicflow: the PyTorch wheels on PyPI need CUDA 13.

Method

The flow is described by its vorticity ω. Each time step:

  1. diffuses ω (explicit 5-point scheme),
  2. advects it semi-Lagrangially: every grid node traces its fluid back with RK4 and interpolates ω there with the 4th-order M4′ kernel,
  3. recovers the velocity by solving ∇²ψ = −ω with FFTs, u = ∂ψ/∂y, v = −∂ψ/∂x.

Each axis is periodic or bounded by no-slip walls. Walls carry ψ = 0 (a sine transform, done as an FFT of the odd extension), and create vorticity: at every step the wall vorticity is set from the slip velocity next to it, then diffuses into the fluid.

This is vic2d's scheme; its low numerical diffusion is what makes Reynolds numbers in the millions possible.

Example

import numpy as np
from vicflow import Grid, Simulation

grid = Grid.square(256)
x, y = grid.coords()
vorticity = np.sin(2 * np.pi * x) * np.sin(2 * np.pi * y)

sim = Simulation(grid, vorticity, viscosity=1e-4, dt=0.01)
sim.run(100)
print(sim.time, sim.kinetic_energy())

Fields have shape (nx, ny) and are indexed [ix, iy], with y pointing up, like vic2d. Images and matplotlib put rows (y) first and y down, so plot a field transposed: plt.imshow(sim.vorticity.T, origin="lower"). The domain's longest side has length 1.

Fields can be replaced between steps, e.g. to add a vortex in the middle of a run: sim.vorticity = sim.vorticity + extra. The velocity is recomputed from the new vorticity. Changing a field in place (sim.vorticity[...] += ...) does not update the velocity: assign the whole field.

A box with walls all round, or a channel periodic in x:

box = Grid.square(256, "wall")  # 257 x 257 nodes, wall nodes included
channel = Grid(512, 128, "periodic", "wall")  # 512 x 129 nodes

A density field is carried by the flow and drives it through buoyancy (Boussinesq); with the default gravity, pointing down, heavy fluid sinks (vic2d's points up). At a sharp interface the M4′ interpolation overshoots and leaves grid-scale ripples; the default limiter="scalars" clips the advected density and color to their local range, and leaves the vorticity alone. Rayleigh-Taylor instability, heavy fluid over light:

grid = Grid(512, 256, "periodic", "wall")
x, y = grid.coords()
density = np.tanh((y - 0.25 - 0.01 * np.cos(2 * np.pi * 4 * x)) / 0.01)
sim = Simulation(
    grid,
    np.zeros(grid.shape),
    viscosity=1e-5,
    dt=0.002,
    density=density,
    diffusivity=1e-5,
    buoyancy=1.0,
)

Color is a stack of passive scalars of shape (channels, nx, ny), usually RGB, carried and diffused (color_diffusivity) in the same advection pass. vicflow.image reads and writes PNGs (8 or 16 bits per channel) with y pointing up, like vic2d; the image needs one pixel per node:

from vicflow.image import read_color, write_image

color = read_color("start.png")  # (3, width, height), values in [0, 1]
sim = Simulation(
    grid,
    vorticity,
    viscosity=1e-5,
    dt=0.002,
    color=color,
    color_diffusivity=1e-6,
)
sim.run(500)
write_image("frame.png", sim.color, bits=16)

The time step can follow the flow: with courant, dt becomes the largest step, and each step is shortened so that the fastest fluid moves at most courant cells. This suits runs that start at rest and speed up, such as Rayleigh-Taylor. run_until shortens the last steps to land exactly on a time, for frames evenly spaced in time:

sim = Simulation(
    grid,
    np.zeros(grid.shape),
    viscosity=1e-5,
    dt=0.01,  # the largest step
    density=density,
    courant=2.0,  # at most 2 cells per step
)
for frame in range(1, 101):
    sim.run_until(0.05 * frame)
    write_image(f"frame{frame:03d}.png", sim.density, vmin=-1, vmax=1)
print(sim.step_count, sim.last_dt)

Scenes, palettes and recipes

Experimental: these modules may still change between minor versions, and with them the images they make.

vicflow.recipes holds complete scenes with tested parameters. Each recipe returns the arguments of Simulation, grid included, so any of them can be overridden:

from vicflow import Simulation, recipes

scene = recipes.rayleigh_taylor(1024, palette="ink", seed=3)
sim = Simulation(**scene, backend="torch")
sim.run(round(recipes.DURATIONS["rayleigh_taylor"] / sim.dt))

The recipes are stripes (thin buoyant color stripes, after Mark J. Stock's run19 and run27 images), rayleigh_taylor and turbulence; the examples/ scripts render them.

vicflow.scenes has the building blocks: color bands and stripes, wavy interfaces and layers, vortex, dipole, shear_layer, random turbulence and smooth noise, and density_from_luminance. They return NumPy arrays to combine freely. Lengths are in domain units and random patterns depend on the seed only, so a scene starts the same at 256 cells as at 4096: the coarse run is a quick preview of the fine one while the flow is young, before turbulence makes them drift apart.

vicflow.palettes has the color lists they paint with: built-in palettes (palettes.names()), your own (["#15161d", "#23356b", ...] or RGB triples), or a matplotlib colormap (Palette.from_matplotlib("magma")). Colors are interpolated in Oklab, so gradients look even.

GPU

On a GPU, install the torch extra (pip install "vicflow[torch]") and pick the PyTorch backend; it uses CUDA or Apple's MPS when available, in float32, with a fused advection kernel (Triton on NVIDIA, Metal on Apple Silicon; AMD/ROCm should work through Triton but is untested).

sim = Simulation(grid, vorticity, viscosity=1e-4, dt=0.01, backend="torch")
sim.run(100)
w = sim.to_numpy(sim.vorticity)

Replaying a vic2d run

vicflow's defaults favour stable, artefact-free runs over matching vic2d bit for bit. To reproduce a vic2d run, pass its conventions explicitly:

sim = Simulation(
    grid,
    vorticity,
    viscosity=1e-4,
    dt=0.01,
    density=density,
    gravity=(0, 1),  # vic2d's gravity points up (only with a density field)
    vic2d_mean_flow=True,  # its mean-velocity convention, which depends on the grid origin
    time_order=1,  # velocity frozen during the backtrace (default 2: extrapolated)
    limiter="none",  # no limiter (default "scalars")
)

vic2d's -cn is a fixed dt = cn / n on an n-cell grid. Its -constcn uses the same units as courant (cells per step) but another rule to pick the step, so adaptive steps are not reproduced exactly.

time_order=1 is about 20 % faster, but its kinetic energy drifts upwards at large time steps and it can blow up next to walls.

Public API

Grid, Simulation and the vicflow.image module; vicflow.scenes, vicflow.palettes and vicflow.recipes are public but experimental. The other modules (vicflow.poisson, vicflow.advect, vicflow.backends, ...) are internal and may change between versions.

Development

Needs uv.

uv sync                 # create the environment
uv run pytest           # tests, including the comparison with vic2d
uv run ruff check .     # lint
uv run ruff format .    # format
uv run python benchmarks/step_time.py   # ms per step for every available backend

Work happens on dev; main only receives stable, tagged versions.

tests/reference/ holds the vic2d runs used as an oracle and the scripts that regenerate them. validation/turbulence_vs_legacy.py runs a larger case (decaying turbulence, 1024²) with both codes and draws them side by side.

Citing

If you use vicflow in your work, please cite it:

@misc{vicflow2026,
  author       = {Quentin Petit},
  title        = {vicflow: a {GPU}-accelerated {2D} vortex-in-cell fluid simulator for generative art},
  howpublished = {\url{https://github.com/petitquentin/vicflow}},
  year         = {2026}
}

Credits

The numerical method comes from vic2d by Mark J. Stock. If you use vicflow, please also cite it:

@Misc{vic2d2004,
  author =       {Mark J.~Stock},
  title =        {vic2d:  A command-line two-dimensional fluid simulator using a novel semi-Lagrangian advection technique},
  howpublished = {\url{https://github.com/markstock/vic2d}},
  year =         {2004}
}

License

GPL-3.0-or-later, like vic2d. See LICENSE.

Metadata

Release files for vicflow 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for vicflow 0.2.0
File Size Uploaded Provenance Actions
vicflow-0.2.0.tar.gz 56.4 kB With provenance Details for vicflow-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for vicflow 0.2.0
File Interpreter ABI Platform Actions
vicflow-0.2.0-py3-none-any.whl Python 3 none any Details for vicflow-0.2.0-py3-none-any.whl

Total release size: 117.3 kB

Release files / vicflow-0.2.0.tar.gz

Download URL vicflow-0.2.0.tar.gz
Size 56.4 kB
Tags Source
SHA-256 checksum
How to use checksums
1ccafd8ec5e70cdc7d34d83edff1c0a6e3b6ef359c9372005a3e429915049b18
BLAKE2b-256 checksum
How to use checksums
c7e11d13cebea2c901cc0ec5be7d9ce2a42be1980493985009ef221085acfbdc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 4, 2026.

Transparency log

Release files / vicflow-0.2.0-py3-none-any.whl

Download URL vicflow-0.2.0-py3-none-any.whl
Size 61.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6515b1dfe013d25261cd0231970049cd5f07d1f82d5573231618947157206136
BLAKE2b-256 checksum
How to use checksums
494fc5d6239538a8f4553ab0890382daa0fe8298242822db6b758f920ee9b97d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 4, 2026.

Transparency log

Security for vicflow 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for vicflow-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 4, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page