Convolutions and U-Nets
A learned stencil slid over a grid, at several scales, with shortcuts for fine detail.
Outcome: Explain a convolution as a learned stencil, and work out how pooling grows the receptive field; build a two-level U-Net with skip connections in PyTorch; say when a grid model is the wrong choice.
Builds on Grids, meshes and point clouds, Overfitting and regularisation
When the geometry and the field live on a regular grid, a network can do what every finite-difference solver does: combine each cell with the cells around it. The aircraft’s surface is not a grid, so this lesson’s example is a 2D airfoil on a grid.
Convolution
A convolution slides a small window, the kernel, over the grid.
DefinitionConvolution
A 3×3 kernel has nine weights; a finite-difference Laplacian is one particular choice of them. A convolutional layer learns its weights from data, and it learns many kernels at once: each produces one output channel, a new grid of numbers. The input channels are the given grids (geometry, inflow velocity); the hidden channels are features the network learns.
Conv2d computes this sum without flipping the kernel, so strictly it is a cross-correlation.Because the weights are shared across the grid, a convolution has few parameters (a 3×3 layer from 32 to 64 channels has 18,432 weights plus 64 biases, whatever the grid size) and it treats the same flow pattern the same way wherever it appears.
Pooling and the receptive field
The receptive field of a cell is the patch of the input it can be influenced by. One 3×3 layer sees 3×3 cells; two see 5×5; ten see 21×21. That grows too slowly to connect a leading edge to a wake 100 cells away. Pooling speeds this up: a 2×2 max-pool keeps the largest of every four cells and halves the grid in each direction. After three poolings, a 3×3 kernel on the coarse grid spans about 24×24 cells of the original one.
The U-Net
A U-Net (Ronneberger, Fischer and Brox, 2015) is an encoder that convolves and pools down to a coarse grid, followed by a decoder that upsamples back to full resolution. The coarse levels see much further but have lost fine detail, so skip connections copy each encoder level across to the decoder level of the same size, where the two are concatenated. The network combines global context with local detail, much like a multigrid solver.
On the way up, a transposed convolution (ConvTranspose2d) reverses pooling's change of size, not its loss of detail: each coarse cell spreads into a 2×2 block through learned weights, doubling the grid in each direction.
One early study of this kind is Thuerey et al. (2020), who trained a U-Net (Figure 5-3) on RANS solutions around about 1,500 airfoil shapes. The input was three 128×128 channels: the two components of the freestream velocity and a mask marking the airfoil. The output was pressure and the two velocity components. On 30 airfoils left out of training, the paper reports for one of its networks a mean relative error over the three outputs of 2.6 %, but 14.76 % for pressure alone, where many small values inflate a pointwise relative error (see measuring error).
import torch
from torch import nn
def block(c_in, c_out):
# Two 3x3 convolutions: a learned stencil, twice.
return nn.Sequential(
nn.Conv2d(c_in, c_out, 3, padding=1), nn.SiLU(),1
nn.Conv2d(c_out, c_out, 3, padding=1), nn.SiLU())
class UNet(nn.Module):
def __init__(self, c_in=3, c_out=3, w=32):
super().__init__()
self.enc1 = block(c_in, w)
self.enc2 = block(w, 2 * w)
self.mid = block(2 * w, 4 * w) # coarsest level
self.pool = nn.MaxPool2d(2) # halve H and W
# Learned upsampling: double H and W.
self.up2 = nn.ConvTranspose2d(4 * w, 2 * w, 2, stride=2)2
# 4w channels in = 2w (upsampled) + 2w (skip)
self.dec2 = block(4 * w, 2 * w)
self.up1 = nn.ConvTranspose2d(2 * w, w, 2, stride=2)
self.dec1 = block(2 * w, w)
# A 1x1 convolution: channels to output fields.
self.out = nn.Conv2d(w, c_out, 1)
def forward(self, x): # (B, c_in, H, W)
e1 = self.enc1(x) # (B, w, H, W)
e2 = self.enc2(self.pool(e1)) # (B, 2w, H/2, W/2)
m = self.mid(self.pool(e2)) # (B, 4w, H/4, W/4)
# Skip connections: concatenate e2, then e1.
d2 = self.dec2(torch.cat([self.up2(m), e2], 1))3
d1 = self.dec1(torch.cat([self.up1(d2), e1], 1))
return self.out(d1) # (B, c_out, H, W)
# Channels: inflow u, inflow v, airfoil mask.
x = torch.randn(8, 3, 128, 128)
print(UNet()(x).shape) # (8, 3, 128, 128)
padding=1adds one cell around the grid, so a 3×3 convolution keeps H and W unchanged.- Kernel 2, stride 2: each coarse cell spreads into its own 2×2 block, with no overlap, which doubles H and W exactly.
- Concatenation along axis 1, the channels. The two grids must have the same H and W, so H and W must be divisible by 4 for two poolings.
Example 5-3 prints torch.Size([8, 3, 128, 128]): three output fields on the same grid as the input. It is trained as in Example 4-5, with grids in place of designs.
Limits
In 3D, memory grows as , with the number of cells along each side: halving the cell size multiplies it by eight, so 3D grids that fit in GPU memory are usually coarse next to the near-wall cells of an industrial mesh. And a U-Net is trained for one grid spacing, which neural operators relax.
An unstructured CFD mesh must be resampled onto the grid, and anything thinner than a cell can be lost. Surface values are read back by interpolation from cells cut by the wall, often where accuracy matters most.