Numerical methods
One character of code
Two particle simulations, the same swirling field, the same step size. One reads x1 instead of x in a single line, and its dye stays exactly as even as it started, however long it runs. The other clumps.
What you’re watching
Same field, same step, one line apart
Each panel is a square of fluid that wraps around at its edges, filled with 262,144 particles on a desktop GPU (65,536 on phones and small screens). Both panels start from the same random cloud and are stirred by the same field, a few sine waves whose phases drift so the swirls wander. The dye colour is fixed by where each particle started, so the stripes stretch into filaments as the flow mixes them.
The only difference is the second line of the update. Forward Euler reads the up-down push at the particle’s old x; the shear update reads it at the new x1. Switch to Density to see the clumps and voids without the dye, change the step size, or restart both panels from the same even cloud.
In our test runs on a desktop at the default settings, forward Euler’s clumping index passed 1,000 within about 900 steps (15 seconds) and kept swinging between roughly 800 and 2,800. The shear panel’s stayed between 0.96 and 1.04 for the whole run of 2,800 steps.
Explainer
Why one character keeps the dye even
Pick a level with the switch on the diagram: Curious has no symbols, Builder has the code and the algebra, Mathematician has the statements. The pictures are the same at every level.
The question
These are the same two simulations as above, small enough to watch side by side. Both stir the same coloured dots with the same swirling pattern and the same size of step. The red number under each measures clumping: 1 means the dots are as evenly spread as if you had sprinkled them at random.
On the left the number creeps up and the dots bunch into bright threads. On the right it stays at 1. The two programs differ by one character. Why should that matter?
The stir
This is the stirring pattern. Each arrow shows which way the water at that spot is pushed. Split it into two pushes. The sideways push is the same all along any row: it only changes as you go up or down. The up-and-down push is the same all along any column.
Now look at the highlighted box. Whatever is pushed in through its left side is pushed out of its right side at exactly the same rate, because both sides sit in the same rows. Top and bottom work the same way. Nothing piles up inside. That is what it means for water to be incompressible, one box at a time.
Euler’s step
A computer can’t move the dots smoothly; it moves them in small jumps. The obvious recipe is to look at both pushes where the dot is now, then jump.
Follow the white grain. It reads the sideways push at its starting point, reads the up-and-down push at its starting point too, and jumps by both at once.
The leak
Now follow a tiny square of water through ten of those jumps. It tilts and stretches, which is fine: real water does that. But watch the purple number. The square’s area changes. At each jump some squares grow a little and others shrink.
Run that for thousands of jumps over hundreds of thousands of dots and they drain out of the growing places and pile into the shrinking ones. That is the clumping on the left.
One character
Here is the fix, and it really is tiny. Do the jump in two halves. First slide the grain sideways, exactly as before. Then, for the up-and-down slide, read the push at the place the grain has just moved to, not where it started.
Same pushes, same size of jump, same amount of work. The grain lands a little away from where the obvious recipe would put it (the faint ring), and that small difference turns out to be everything.
A deck of cards
Why does that work? Look at the first half-step on its own. Every row slides sideways by its own amount, like pushing a deck of cards so that it leans. The deck changes shape, but each card keeps its length, so the deck covers exactly the same area. The second half-step does the same with columns. Watch the purple number.
Mixing without squeezing
So the shear version can stir as hard as it likes. The dye folds into finer and finer threads, but the dots never bunch: the red number hugs 1 for as long as you watch.
Mixing and squeezing turn out to be different things. Real stirring mixes without squeezing, and now the simulation does too.
Name it
That’s the whole idea. Sliding rows can’t change area, sliding columns can’t either, and doing one after the other can’t, however big the slides. In the jargon: shears preserve area exactly.
Why physics engines do this
The trick is everywhere. Swing a pendulum in a game using the obvious recipe and it slowly gains energy until it whirls over the top. With the two-half-steps recipe it keeps swinging steadily. Each loop on the stage is one pendulum, drawn as its position against its speed.
The theorem
Shears preserve area, exactly
Let carry Lebesgue measure . For Borel-measurable 1-periodic define the shears
and . (The demo’s update is the case , which are again Borel and 1-periodic.)
(a) is a bijection of and for every Borel set .
(b) If are maps of this form, each with its own , and are independent and uniform on , then for every the points are again independent and uniform.
(c) By contrast, let for a smooth 1-periodic , and let with be one forward-Euler step. Then with , and if then for every with the map (mod ) is injective on but does not preserve area. For the demo’s field one has and , so Euler’s Jacobian is .
Read the proof
(a) Shears preserve area
is well defined on because is 1-periodic, so the formula does not depend on the representative of , and replacing by changes the output by . It is a bijection with inverse , well defined for the same reason. Likewise is a bijection with inverse . Both maps and their inverses are Borel measurable, being compositions of the Borel map with continuous addition.
Let be a Borel set and, for , let be its horizontal slice. The horizontal slice of at height is , a translate of on the circle. Translation preserves Lebesgue measure on , and is Borel, so by Tonelli’s theorem
The same argument with vertical slices gives . Hence . No smallness of or and no differentiability is needed.
(b) Uniform clouds stay uniform
Let . By (a) applied repeatedly, is a Borel bijection with for every Borel . If is uniform then , so is uniform. The maps are deterministic, so for Borel , using independence of the ,
So the images are independent and uniform.
(c) Forward Euler does not
The Jacobian matrix of is with . For any 2 × 2 matrix, . Here and , so .
Injectivity. Lift to by the same formula; is -periodic. For , . If with , then by periodicity of , so . Hence is injective on .
Positive Jacobian. For every and unit vector , , so and . Since , Cayley–Hamilton gives , so . Therefore everywhere.
Area is not preserved. Suppose . By continuity has a constant nonzero sign on an open disc of diameter less than 1 around a lift of ; its projection is an isometric copy, so . The lifted is and injective on with Jacobian , so by the change-of-variables formula , which is strictly larger or strictly smaller than according to the sign of . By the injectivity on the torus, the projection is injective on (if with then , and because has diameter less than 1), so . is open by the inverse function theorem, hence Borel. Take : injectivity gives , so , and does not preserve area in the sense of (a).
For the demo’s field, with of mean zero is for with , periodic. Then , , , so , which is not identically zero for any non-constant and .
Sources
Further reading
- Cavalieri’s principle: solids whose slices have equal areas have equal volumes; in the plane, equal slice lengths give equal areas.
- E. Hairer, C. Lubich and G. Wanner, Geometric Numerical Integration: Structure-Preserving Algorithms for Ordinary Differential Equations, 2nd ed., Springer, 2006. Symplectic Euler (chapter VI) and backward error analysis (chapter IX).
- Semi-implicit (symplectic) Euler method.
More demos
Fluid dynamics
A vortex that blows up
The swirling core from the September 2026 Navier–Stokes blow-up construction, run on the GPU in its own similarity coordinates.
Number theory
The golden angle
Why sunflowers use 137.5°, and the theorem that says points on a circle only ever leave three gap sizes.
Optimal transport
Particles that never collide
Thousands of particles fly in straight lines between shapes. A one-line inequality proves no two ever meet.