NavierStokes2D: the two-dimensional Navier–Stokes equations on a periodic square, by a Fourier pseudo-spectral method

Vorticity computed by run_NS.m with its own parameters (1024 × 1024 Fourier modes, viscosity ν = 10⁻⁴) on the
square −π ⩽ x, y ⩽ π, at t = 0, 2, 3.5 and 5.1 (the last frame of the run), with one colour scale for all panels,
from −1.01 (wine) to 1.01 (navy). The lattice of vortices made by the three Fourier modes of the initial vorticity (a)
is sheared into tilted bands (b), which thin and curl © into filaments and hooks (d). Since the vorticity is carried
by the flow and diffuses only slowly at this viscosity, its largest absolute value barely changes, from 1.0098 to
1.0050.
NavierStokes2D is a short MATLAB code, written for teaching, that solves the incompressible Navier–Stokes equations in
two space dimensions, in vorticity form, on a square with periodic boundary conditions in both directions. Derivatives,
the Laplacian and the velocity are computed in Fourier space and the advection term on the grid (a Fourier
pseudo-spectral method), the upper half of the wavenumbers being switched off against aliasing; the vorticity is
advanced in time by the adaptive Adams–Bashforth–Moulton integrator ode113 of MATLAB and drawn after every 0.1 units
of time. No article accompanies the code, so the software itself is cited (see How to cite).
The problem
The fluid is viscous and incompressible and the flow two-dimensional. All quantities are dimensionless, the viscosity
ν being the inverse of a Reynolds number (the code calls it “1/Re or viscosity”). With the velocity (u, v), the
vorticity ω = ∂v/∂x − ∂u/∂y and the stream function ψ, the code solves
∂ω/∂t + u ∂ω/∂x + v ∂ω/∂y = ν Δω, Δ = ∂²/∂x² + ∂²/∂y²,
u = ∂ψ/∂y, v = −∂ψ/∂x, −Δψ = ω,
for −π ⩽ x ⩽ π, −π ⩽ y ⩽ π and t ⩾ 0, every field being 2π-periodic in x and in y, and ψ having zero mean. The
parameters are set at the top of run_NS.m:
nu = 1e-4 viscosity ν
Lx, Ly = pi, pi half-lengths of the square
Nx, Ny = 1024, 1024 Fourier modes in x and in y (multiples of 4)
Tf, ds = 5, 0.1 final time and output interval
RelTol = AbsTol = 1e-10 tolerances of ode113
The initial vorticity, set by InitCond.m, is made of three Fourier modes with random amplitudes:
ω(x, y, 0) = C Re[ a₁ exp(4iξ) + a₂ exp(i(ξ + η)) + a₃ exp(3iη) ], ξ = x + π − h, η = y + π − h,
a₁ = −0.64901 + 1.18117 i, a₂ = −0.75845 − 1.10961 i, a₃ = −0.84555 − 0.57266 i, C ≈ 0.27196.
Here h = 2π/Nx is the grid step, and ξ and η are measured from the first grid point (−π + h, −π + h); the real and
imaginary parts of a₁, a₂ and a₃ are the first six numbers drawn by randn from the Mersenne Twister with seed 1,
which run_NS.m sets; and C makes the largest value of ω on the grid equal to 1 (the smallest is −1.0098).
The square is sampled at the N × N points x = −π + jh and y = −π + lh (1 ⩽ j, l ⩽ N), where N = Nx = Ny = 1024, and
the fields are expanded on the integer wavenumbers 0, 1, …, N/2, −N/2 + 1, …, −1 in each direction. Derivatives, the
Laplacian and the solution of −Δψ = ω are computed in Fourier space with fft2 and ifft2, and the products u ∂ω/∂x
and v ∂ω/∂y on the grid.
Against aliasing, the wavenumbers larger than N/4 in absolute value (257 to 512 for N = 1024) are replaced by zero in
the vectors of wavenumbers from which these operators are built. The N² values of ω on the grid then obey a system of
ordinary differential equations, which ode113 (variable step and variable order, Adams–Bashforth–Moulton) integrates
over successive intervals of length 0.1, the vorticity being drawn after each. The loop while (t < Tf) adds 0.1 to t
in floating point and therefore stops after 51 intervals, at t = 5.1.
Quick start
In MATLAB, from the folder matlab/ (the script adds tools/ to the path itself):
cd matlab
run_NS
The script draws the initial vorticity in a window, then integrates the equations and redraws the vorticity after
every interval of 0.1, with a text progress bar during each interval, up to t = 5.1. With the shipped parameters the
run takes about three minutes on one processor core (MATLAB R2025b). The initial vorticity is set in InitCond.m, the
right-hand side of the equations in RHS.m and the drawing in Plot.m.
A check of the run
Along every solution, the kinetic energy E = ½⟨u² + v²⟩ and the enstrophy Z = ½⟨ω²⟩, where ⟨·⟩ is the mean over the
square, satisfy dE/dt = −2νZ. The run of the figure gives the following values.
E Z
t = 0 0.0209430215 0.0862715442
t = 5.1 0.0208554789 0.0852753710
E(0) − E(5.1) = 8.754264 × 10⁻⁵
2ν ∫ Z dt (0 to 5.1) = 8.754262 × 10⁻⁵ (trapezoidal rule over the 52 frames)
The energy lost thus matches the dissipation to seven digits. The grid resolves this run completely: at t = 5.1 the
squared moduli of the Fourier coefficients of ω, summed over rings of radius |k|, have fallen from the largest (at
|k| = 1) by a factor 10¹⁰ at |k| = 64 and by a factor 10³² at the anti-aliasing cut-off |k| = 256.
Animations of longer runs
Three animations of longer runs made with this code are on YouTube; they show the vorticity as Plot.m draws it
(click a picture to watch).

Contents
The folder matlab/ holds the script run_NS.m (parameters, grid, operators and time loop) and the functions it
calls: InitCond.m (the initial vorticity), RHS.m (the right-hand side ν Δω − u ∂ω/∂x − v ∂ω/∂y on the grid) and
Plot.m (one frame); the folder matlab/tools/ holds the text progress bar shown during each integration. The folder
docs/figures/ holds the two scripts that make the figure of this page: ns2d_readme_data.m (MATLAB or GNU Octave)
repeats the computation of run_NS.m (in MATLAB their last frames agree to 2 × 10⁻¹¹) and writes the vorticity every
0.5 time units, with the kinetic energy and the enstrophy of every frame, to ns2d_vorticity.mat, and
plot_readme_figure.py (Python 3 with NumPy, SciPy and Matplotlib, and the style file beside it) draws the figure from
that file. Both are run from docs/figures/, in this order.
Requirements
MATLAB, with no toolbox (checked with R2025b). GNU Octave cannot run run_NS.m as it stands, since it has neither
RandStream (line 74) nor ode113 (line 100). The script docs/figures/ns2d_readme_data.m runs in both (checked with
GNU Octave 11.1): without RandStream it builds the same initial vorticity from the six numbers given above, and
without ode113 it integrates with ode45 at the same tolerances; at t = 0.5 its vorticity agrees with that of MATLAB
to 3 × 10⁻⁸.
How to cite
No article accompanies this code, so please cite the software itself:
D. Dutykh, NavierStokes2D: the two-dimensional Navier–Stokes equations on a periodic square, by a Fourier
pseudo-spectral method, version 1.0.0 (2026).
https://github.com/dutykh/NavierStokes2D
@software{Dutykh_NavierStokes2D,
author = {Dutykh, Denys},
title = {{NavierStokes2D}: the two-dimensional {Navier--Stokes} equations on a periodic square, by a {Fourier} pseudo-spectral method},
year = {2026},
version = {1.0.0},
url = {https://github.com/dutykh/NavierStokes2D},
license = {GPL-3.0-only}
}
The button “Cite this repository” in the right-hand column of this page gives the same reference, read from
CITATION.cff.
Licence
GNU General Public License, version 3 (see LICENSE). The progress bar of matlab/tools/ comes from the submission
Text progress bar for ODEs by
David Romero-Antequera on the File Exchange of MATLAB Central, whose odetpbar.m calls textprogressbar.m by Paul
Proteus; these two files remain the work of their authors and are not covered by the licence of this repository.
Author
Denys Dutykh (Mathematics Department, Khalifa University of Science and Technology, Abu Dhabi, UAE).
https://www.denys-dutykh.com/
评论