every part builds on the one before. New to the topic? Start at part 01.
How gradients flow backwards through a simulation, and how a coarse fluid solver and a correction network are trained together
Post 14 closed the Navier–Stokes series with turbulence. Until now, solvers and networks were kept apart: a solver, such as the heat update of Post 2 or the fluid loop of Post 13, produces data, and a network learned from that data.
Can a solver take part in training itself? Yes. Every solver step is ordinary arithmetic, so backpropagation flows through it like through the layers of a network. Part 1 shows how, on the heat equation. Part 2 uses it to train a network together with a fluid solver.
For a fixed input and target, a network's loss is a function of its weights. Gradient descent needs the gradient of the loss with respect to each individual weight: how much the loss changes when that one weight is nudged a tiny bit. It then moves every weight a small step against its gradient, so the loss shrinks. The gradient update is often times averaged over a batch of inputs, but the principle is the same.
Backpropagation computes all of these gradients in one pass, from the loss backwards. A network is a chain of simple operations, and each operation knows its local derivative: how much its output moves when one of its inputs is nudged. The gradient of the loss with respect to any value then follows from the chain rule: multiply the local derivatives along the route from that value to the loss, and if there are several routes, add them up. This works as long as every operation has a local derivative, which includes nonlinear activations such as ReLU. For a step-by-step refresher, Andrej Karpathy's video The spelled-out intro to neural networks and backpropagation builds it up from scratch.
Post 2's explicit heat update, with its constants collected into one number
Where
What we want to know. A solver has no weights to train. We differentiate through it, to reach whatever goes into it: the starting temperatures, a parameter such as
Writing out one cell. Take
The last line shows what one heat step really is: cell 2's new temperature is a weighted average of three old temperatures, 0.25 of its left neighbour, 0.5 of itself and 0.25 of its right neighbour. The weights add up to 1.
Reading off the derivatives. In the last line, each old temperature appears exactly once, multiplied by a fixed number. Adding one degree to
Where each fraction denotes how much cell 2's new temperature moves per degree added to one of the three old temperatures, with the other two held fixed. So an extra degree in cell 2 also raises cells 1 and 3 by 0.25 each: 0.5 stays, 0.25 goes to each neighbour, and nothing is lost.
A simulation of
To train anything through the solver, the chain needs a loss at its end. The solver's final temperatures are compared with an observation in the form of measured temperatures in every cell. The solver's result is the prediction here. A standard choice is the squared loss, which sums the squares of the differences between prediction and observation:
Where
This already solves a backwards problem. The temperatures were observed at step
Conceptually it is network training without a network: the unknown start plays the role of the weights, and the solver plays the role of fixed layers.
Take one heat step on a rod of 6 cells (
| in 1 | in 2 | in 3 | in 4 | in 5 | in 6 | |
|---|---|---|---|---|---|---|
| out 1 | 0.5 | 0.25 | 0 | 0 | 0 | 0 |
| out 2 | 0.25 | 0.5 | 0.25 | 0 | 0 | 0 |
| out 3 | 0 | 0.25 | 0.5 | 0.25 | 0 | 0 |
| out 4 | 0 | 0 | 0.25 | 0.5 | 0.25 | 0 |
| out 5 | 0 | 0 | 0 | 0.25 | 0.5 | 0.25 |
| out 6 | 0 | 0 | 0 | 0 | 0.25 | 0.5 |
Each row lists what one output cell listens to: itself and its two neighbours, the stencil. Row 2 is exactly Equation (3). Each column shows where a nudge to one input cell ends up: 0.25, 0.5, 0.25. The zeros say that cells which are not neighbours do not affect each other within one step. This table is the Jacobian of one step.
Over many steps, every cell does influence every other, but that is a different table. The whole-simulation table, from start to end, is the one-step table multiplied with itself once per step: a matrix product multiplies along every route through the intermediate step and adds over the routes. For two steps:
| in 1 | in 2 | in 3 | in 4 | in 5 | in 6 | |
|---|---|---|---|---|---|---|
| out 1 | 0.3125 | 0.25 | 0.0625 | 0 | 0 | 0 |
| out 2 | 0.25 | 0.375 | 0.25 | 0.0625 | 0 | 0 |
| out 3 | 0.0625 | 0.25 | 0.375 | 0.25 | 0.0625 | 0 |
| out 4 | 0 | 0.0625 | 0.25 | 0.375 | 0.25 | 0.0625 |
| out 5 | 0 | 0 | 0.0625 | 0.25 | 0.375 | 0.25 |
| out 6 | 0 | 0 | 0 | 0.0625 | 0.25 | 0.3125 |
Output 1 now feels input 3, along the single route 3 → 2 → 1: 0.25 · 0.25 = 0.0625. With every further step the nonzero band widens by one cell, until no zeros are left. For a 3D room with 128³ ≈ 2.1 million cells, the whole-simulation table would have about 4.4 · 10¹² entries, around 17 TB.
However, Guess-and-correct never needs it. So don't worry if you don't understand the whole-simulation table. This just shows how effective backpropagation is compared to computing the whole-simulation table. We are not interested in how each output cell reacts to each input cell, but only in the gradient of the loss with respect to each starting cell. The loss is one number, so what we need is only one gradient per starting cell.
Backpropagation gets these numbers without the table. It walks back one step at a time and keeps only one number per cell: how the loss reacts to it. Cell 3 at step
Where
Read Equation (5) one term at a time. Take the first term, 0.25 times the gradient at cell 2 one step later:
Cell 3 feeds three cells at the next step, so there are three routes, and Equation (5) adds them up.
Backpropagation repeats this backwards, one step at a time:
The whole-simulation table is never needed: each step only uses its own local gradients and the gradients just computed for the step after it.
Doing this for all cells of one step at once is called the vector–Jacobian product: the vector is the row of loss gradients at step
The backward pass can be obtained in two ways.
Automatic differentiation is what deep learning frameworks such as PyTorch do. During the forward pass, the framework records every single operation of the solver, and the backward pass walks back through all of them. This needs no extra work from the programmer, but it is expensive: every intermediate value of every step has to be kept in memory until the backward pass needs it. For a long simulation, this record can easily exceed the available memory. The heat step shows how wasteful it can be: its local gradients are the constants 0.25, 0.5 and 0.25, so the backward pass needs none of the stored values, yet automatic differentiation keeps them anyway.
The adjoint method is the classical alternative from engineering, where it has been used for decades, for example to optimize the shape of an aircraft wing. Someone derives the backward formula for a whole solver step once, on paper, and then programs it. Equation (5) is exactly that formula for the heat step. The adjoint computes only the gradients that are actually needed and does not record every small operation, so it saves a lot of memory and computation. Deriving adjoints for real solvers is a deep topic of its own and beyond this post.
Nils Thuerey, a professor at the Technical University of Munich whose group works on combining physics simulations with deep learning, describes differentiable physics as roughly "adjoint method + deep learning". The adjoint part is the old idea of walking backwards through a simulation. The new part is feeding those gradients into the training of a neural network. His group develops PhiFlow, an open-source simulation library whose solvers are differentiable, built on top of PyTorch, JAX and TensorFlow.
A coarse grid is cheap but wrong, because it cannot see small swirls, and Post 14 showed how expensive a fine enough grid gets. The idea: a coarse solver and a correction network take turns. The solver moves the flow one time step forward, and the network fixes what the coarse grid got wrong.
The key point is that the solver runs during training too, with the network inside, for several time steps in a row, and backpropagation flows through the whole chain with the machinery of Part 1. This gives the network two things: it trains on states it produced itself, which are exactly the states it meets when it is used, and a correction it makes early on gets feedback about what it caused later. This setup is called the solver in the loop, after a NeurIPS 2020 paper by Um, Brand, Fei, Holl and Thuerey from Thuerey's group.
Take swirling water in a box, simulated on a fine 256 × 256 grid and on a coarse 64 × 64 grid. Each coarse cell covers 4 × 4 fine cells and holds only one velocity, roughly their average, so swirls smaller than a coarse cell cannot be represented. They still act on the large flow (Post 14), so the coarse simulation drifts away from the fine one. The reference is the fine simulation, run once ahead of time and averaged down to the coarse grid.
The network does not predict the small swirls, and it does not upscale to 256 × 256. It only predicts their effect on the coarse velocities. That is enough, because what we usually care about is the large-scale flow, such as where the smoke goes or the force on a car, and the small swirls matter only through their effect on it.
This is learnable because small swirls are not random: they form where the large flow is torn apart. Two neighbouring cells at 2 m/s and −2 m/s, like two lanes of a river flowing against each other, constantly produce swirls that mix them, a bit like extra friction. Two cells at 1.0 and 1.1 m/s produce almost none. So the coarse field does tell where small swirls are likely and what they typically do.
The network still cannot know which exact swirls hide in a cell, and the same coarse pattern can hide slightly different swirls each time. The information comes from the reference: the network's corrected field is compared with it, and the mismatch is the missing effect of the small swirls, plus the coarse stencils' own errors. With a squared loss, the best the network can do is predict the average effect for each coarse pattern, because the average is the value with the smallest squared distance to all cases. From the network's point of view:
The solver I work with uses big cells, so it can't see swirls smaller than a cell. But I've learned that small swirls don't pop up at random. They form wherever the big flow gets torn apart, for example where two neighbouring cells flow in opposite directions. So every time the solver takes a step, I look at its big cells, spot the places where small swirls are probably forming, and nudge those cells the way the swirls usually would, often by mixing the clashing velocities a bit. I learned these nudges by comparing myself many times against a very detailed simulation that could see every little swirl. I can't know the exact swirls hiding in a cell, so I apply their average effect. That's not perfect, but it's much better than pretending they aren't there.
Nobody tells the network this rule. It learns whichever rule from coarse pattern to nudge makes the mismatch with the reference small.
Where
The plus is the skip connection of a ResNet: the solver already gets most of the flow right, so the network only learns the small fix, and a network that outputs zeros gives back the plain coarse solver. The next turn starts from the corrected field, so every nudge changes everything after it. This decides how training must work.
The obvious recipe: take the reference at time step
As a loop: take the reference at
At inference, when the trained network is used, the coarse solver starts from the network's own corrected field from the second turn on. That field is close to the reference but never exact, so the network sees inputs unlike anything it trained on. Its nudges get worse, the next input is even more unfamiliar, and the errors snowball: Post 4's rollout problem.
It is like learning to drive with an instructor who puts the car back into the middle of the lane after every second. You get very good at small adjustments from the middle, but you never practise getting back from a drift. On your first solo drive you drift a little, end up somewhere you have never practised, and drift further.
Don't reset: continue from the corrected field for a few turns. In the car picture, the instructor stops putting you back in the middle. Two objections come up naturally.
"The network has no memory, so why would a reset matter?" It indeed only sees the current field in each turn. That is exactly why its training inputs matter: it can only be good at moments it has practised.
"Without a reset, won't it learn its own errors?" No, because the input and the comparison are two different things. The input is the network's own, slightly drifted field. The comparison is still the reference, at every turn. So the network learns: from this drifted state, push back towards the truth.
Why the solver must run during training. These drifted fields depend on the network's current weights, which change with every update. They cannot be generated ahead of time: they only exist once the current network has made them.
One training iteration starts from the reference at some time step
Where
Why "unrolling". Written out like this, the loop is one long chain, like a recurrent network unrolled in time: solver layers with fixed weights alternating with network layers with trainable weights. To the backward pass, it is simply a deep network.
Why only a few turns. The backward pass must store and walk back through every turn. A few turns keep that affordable, and many different starting points cover the whole simulation.
The network appears
Where
Why adding is right. It is the argument from Equation (2), where cell 2's old temperature reached its new one along two routes and the routes added. Nudging
The network still has no memory: every copy sees only its current field. Its good behaviour over long rollouts comes from training, through two separate effects:
In each turn the solver comes first and the network after it. So the gradient seems to flow through the network rather than through the solver. How does the network ever get a signal through the solver?
For turn 1, it does not need to. Solver step 1 starts from the fixed reference, and nothing trainable lies behind it.
But copy 1's corrected field is the input of solver step 2. So copy 1 also influences
| Copy | Gets feedback from | Route back passes through |
|---|---|---|
| copy 1 | solver steps 2–4 and copies 2–4 | |
| copy 2 | solver steps 3–4 and copies 3–4 | |
| copy 3 | solver step 4 and copy 4 | |
| copy 4 | nothing, it feeds its loss directly |
Suppose copy 1 is the only one to make a mistake: it adds the effect of a swirl that is not there. The later copies make no new errors, they just carry the drifted field on. The later losses stay above zero, although copies 2 to 4 did nothing wrong. Had copy 1 added a little less of the fake swirl's effect, the field after turn 1 would be closer to the reference, and so would every field after it, so all four losses would be smaller. That is what the gradient through the solver tells copy 1: its nudge is blamed for the whole chain.
A tug of war. The copies can disagree about which way a shared weight should move. A negative gradient means the loss falls when the weight rises, a positive one that it rises. Take
Equation (8) gives
Copy 1 is not automatically the loudest. Its gradient is only large if the solver carries the error forward or makes it grow, as in turbulence. In a smooth flow dominated by friction, an early error fades and copy 1's contributions from the later losses are small. The gradient through the solver reports whichever is true.
Without a differentiable solver, the gradient would be cut at every solver step. The network would still practise on its own drifted fields, but each copy would only hear its own turn's loss. The gradient through the solver turns "be right at this turn" into "make nudges that keep the whole rollout on track".
The reverse question: we observe a flow at a later time and want the starting velocity field that caused it. Guess-and-correct answers this for one observation, but needs a full optimization loop for every new one. Instead, a network takes the observation and outputs a guessed starting field, the solver runs
Where
This is the key difference from the forward setup. In the inverse case, the solver is only needed during training, and inference is one network pass. In the forward case, the solver and the network keep taking turns at inference too. Thuerey's group calls this inverse setup end-to-end training.
The widget below runs both setups on a heated rod, with a real network, a real heat solver and real backpropagation. In the inverse view, a network guesses the rod's starting temperatures from a few thermometers. In the forward view, a coarse heat solver and a correcting network take
| Solver only produces data | Guess-and-correct | Inverse network | Forward solver in the loop | |
|---|---|---|---|---|
| Question | predict the future | find the cause of one observation | find the cause of any observation | predict the future |
| What is learned | correction network | one starting field, no network | network: observation → starting field | correction network |
| Compared with | the reference, after one turn | the observation | the same observation | the reference, after every turn |
| Solver during training? | no, only before | yes | yes | yes, |
| Gradient through the solver? | no | yes | yes, through all | yes, from each copy into all later turns |
| Solver at inference? | yes, takes turns with the network | no inference: every observation needs a new loop | no, one network pass | yes, takes turns with the network |
Only the first column keeps the solver out of training, and that is exactly why it fails: the network never practises on drifted fields. In the other three, the solver is part of what gets differentiated. They differ only in what sits in front of the solver and receives the gradient:
Once a solver is differentiable, gradients can reach anything that feeds into it. The next post introduces variational autoencoders and how Thuerey's group uses one, Tadpole, as a foundation model for 3D fluid simulations, whose paper names coupling with a differentiable solver as future work.