Flow past a cylinder: a channel with an obstacle
Flow past a cylinder: a channel with an obstacle
The lid-driven cavity was a flow in a box. Most flows that matter go around something: air over a wing, water past a bridge pier, blood past a stent. This lesson solves the classic first test of such a flow, a cylinder in a channel, known as the DFG benchmark 2D-1. Two things are new. The domain has a hole in it, built with the Composite domain block. And the three Navier-Stokes equations are written as three custom residuals that couple the network's outputs, so the whole problem is built in the Studio, with no editing of the generated script.
After 30,000 Adam epochs and an L-BFGS polish on a Colab T4 GPU, the network gives a drag coefficient of 4.99 and a pressure drop of 0.103, about 11 % and 13 % below the reference values 5.58 and 0.118. It gets the lift wrong, sign included. The flow field looks right, but a mass check shows the network loses about a tenth of the fluid on its way down the channel, and that loss is the most likely reason the drag and pressure drop are low. Section 6 goes through the evidence.
#studio #navier-stokes #cfd #coupled-equations #composite-domain #benchmark · 50 min read
Before you start. You should have done the lid-driven cavity, which introduces the Navier-Stokes equations and a network with three outputs, and read Complex geometries in the Studio Tour. Open the Studio in another tab. To train the network you need Python with PyTorch and, in practice, a GPU (Colab is one way to get one). The files are listed at the end of this page.
1. The problem
A channel 2.2 m long and 0.41 m high carries a viscous fluid from left to right. A circular cylinder of diameter $D = 0.1$ m sits near the inlet, centred at $(0.2, 0.2)$, a little below the middle of the channel so that the flow is not exactly symmetric. The fluid enters with a parabolic velocity profile, peak $U_m = 0.3$ m/s, and has density $\rho = 1$ and kinematic viscosity $\nu = 10^{-3}$. The walls and the cylinder are no-slip. The setting was fixed in 1996 by Schäfer and Turek so that different flow solvers could be compared on exactly the same problem.
The Reynolds number, with the mean inflow speed $\bar U = \tfrac23 U_m = 0.2$ and the diameter, is
$$ Re = \frac{\bar U D}{\nu} = \frac{0.2 \times 0.1}{0.001} = 20 . $$
At this Reynolds number the flow is steady: two small eddies sit behind the cylinder and do not shed. Three numbers summarise it, and published high-accuracy values exist for all three:
| Quantity | Meaning | Reference |
|---|---|---|
| $C_D$ | drag coefficient: the force pushing the cylinder downstream | 5.57953523384 |
| $C_L$ | lift coefficient: the sideways force, small because the cylinder is nearly centred | 0.010618948146 |
| $\Delta p$ | pressure at the front of the cylinder minus pressure at the back, in Pa | 0.11752016697 |
These are the numbers the network is judged by. Unlike the cavity, there is no field-by-field reference here, but a force on a body is a demanding test: it depends on the pressure and on velocity gradients right at the wall.
Making it dimensionless
We solve the problem in units of the cylinder and the flow: lengths in diameters, velocities in $\bar U$, pressure in $\rho \bar U^2$. Then every quantity the network outputs is of order one, and the equations carry a single number, $1/Re = 0.05$:
$$ \begin{aligned} u\,u_x + v\,u_y + p_x - \tfrac{1}{Re}(u_{xx} + u_{yy}) &= 0, \\ u\,v_x + v\,v_y + p_y - \tfrac{1}{Re}(v_{xx} + v_{yy}) &= 0, \\ u_x + v_y &= 0 . \end{aligned} $$
| Physical | In the Studio | |
|---|---|---|
| Channel | 2.2 × 0.41 m | 22 × 4.1 |
| Cylinder | centre (0.2, 0.2), radius 0.05 | centre (2, 2), radius 0.5 |
| Inflow | $u = 4 U_m y(0.41 - y)/0.41^2$, peak 0.3 m/s | $u = 6y(4.1 - y)/4.1^2$, peak 1.5 |
| Pressure | Pa | multiply by $\rho\bar U^2 = 0.04$ to get Pa |
Why bother? In metres and m/s the velocities are about 0.3 and the pressure differences about 0.1, and the viscous term is a thousandth of the others. A network can learn that, but its outputs and the terms of its loss would live on very different scales (see chapter 10). The dimensionless form costs nothing and removes the problem.
The outlet
The benchmark lets the flow leave through a "do-nothing" outlet, $\tfrac{1}{Re}\,\partial_x \mathbf u - p\,\mathbf n = 0$, a condition that mixes velocity and pressure. A Boundary condition block prescribes one output at a time, so we use the standard approximation instead: $p = 0$, $\partial u/\partial x = 0$ and $\partial v/\partial x = 0$. Twenty diameters downstream of the cylinder the flow is back to a parabola, $\partial u/\partial x$ is almost zero there, and the two conditions agree. Since $\Delta p$ is a difference and the forces do not depend on the pressure level, fixing $p = 0$ rather than letting the outlet set it changes none of the three numbers.
2. The plan: one block per decision
| Decision | Block | Setting |
|---|---|---|
| Where the flow is | Composite domain | a 22 × 4.1 rectangle minus a circle |
| The equations | 3 × PDE | custom residuals for x-momentum, y-momentum, continuity |
| What happens at the edges | 11 × Boundary condition | inlet profile, no slip on walls and cylinder, outlet |
| The unknowns | Network | 3 outputs: $u_1 = u$, $u_2 = v$, $u_3 = p$ |
| Training | Loss, Optimiser, Train | Adam 30,000 epochs, then L-BFGS |
| Output | Visualisation, Report | field plots, loss curve, residual map, results table |
3. Step by step in the Studio
Step 1: A new notebook. In the Lab, start from the empty canvas and name it Flow past a cylinder.
Step 2: The domain. From the Domain family, place a Composite domain block and set:
| Slot | Shape | Operation | Numbers |
|---|---|---|---|
| Shape 1 · base | Rectangle | x min 0, x max 22, y min 0, y max 4.1 | |
| Shape 2 | Circle | Subtract (cut out) | Centre x 2, Centre y 2, Radius 0.5 |
| Shape 3, Shape 4 | None |
Leave Time dimension off: the flow is steady. Set Interior points to 20000 and Interior distribution to Sobol sequence. Leave Default boundary points at 200, and in Points per boundary give BC3 and BC4 (the walls, 22 long) 600 each and BC5 (the cylinder) 400.
Press Detect boundaries. It should report five boundaries: BC1 the left edge $x = 0$ (inlet), BC2 the right edge $x = 22$ (outlet), BC3 the bottom wall $y = 0$, BC4 the top wall $y = 4.1$, BC5 the rim of the cut circle. Press Plot geometry and points and check the hole is at (2, 2), close to the inlet, with its normals pointing into the hole.
Step 3: The network. Place a Network block. Set Network outputs to 3, Hidden layers to 64, 64, 64, 64, 64, Activation tanh, Initialisation xavier. Wire the domain's Coords to the Network's Coords.
Step 4: The equations. Place three PDE blocks. In each, set Equation type to Elliptic and Equation to Custom residual, then:
| Block | Residual expression | Applies to network output |
|---|---|---|
| x-momentum | u1*u1_x + u2*u1_y + u3_x - 0.05*(u1_xx + u1_yy) |
1 |
| y-momentum | u1*u2_x + u2*u2_y + u3_y - 0.05*(u2_xx + u2_yy) |
2 |
| continuity | u1_x + u2_y |
3 |
Wire the domain's Coords and Interior into each of the three. The loss log calls them PDE1, PDE2 and PDE3, but that numbering is the Studio's own and need not follow the order you placed them in. Look at the residual_pde1 … residual_pde3 functions in the generated script to see which is which. In the run below, PDE1 is continuity, PDE2 is x-momentum and PDE3 is y-momentum.
Step 5: The conditions. Place eleven Boundary condition blocks. Each takes one domain port and fixes one output:
| Domain port | Condition type | Value | Applies to network output | Meaning |
|---|---|---|---|---|
| BC1 | Dirichlet | 6*y*(4.1 - y)/4.1**2 |
1 | inflow profile, $u$ |
| BC1 | Dirichlet | 0 |
2 | inflow, $v = 0$ |
| BC2 | Dirichlet | 0 |
3 | outlet, $p = 0$ |
| BC2 | Neumann | 0 |
1 | outlet, $\partial u/\partial x = 0$ |
| BC2 | Neumann | 0 |
2 | outlet, $\partial v/\partial x = 0$ |
| BC3 | Dirichlet | 0 |
1 | bottom wall, $u = 0$ |
| BC3 | Dirichlet | 0 |
2 | bottom wall, $v = 0$ |
| BC4 | Dirichlet | 0 |
1 | top wall, $u = 0$ |
| BC4 | Dirichlet | 0 |
2 | top wall, $v = 0$ |
| BC5 | Dirichlet | 0 |
1 | cylinder, $u = 0$ |
| BC5 | Dirichlet | 0 |
2 | cylinder, $v = 0$ |
A port can feed several BC blocks: drag a second wire from the same port.
Step 6: The loss. Place a Loss block, Term norm MSE, Weighting scheme Fixed, weights left at 1. Wire the Network's output into Network, the three PDE blocks into PDE residual, and all eleven BC blocks into BC / IC.
Step 7: The optimiser. Place an Optimiser block: Adam, Learning rate 0.001, Epochs 30000, Scheduler Step with Step size 10000 and Decay factor 0.5. Tick Second stage: L-BFGS, Iterations 5000, Learning rate 1.0.
Step 8: Training. Place a Train block: Random seed 0, Log every 500, Device auto. Wire the Loss and the Optimiser into it.
Step 9: Output. Place a Visualisation block (Field plot on, Field style Auto (by dimension) (on a 2-D domain that is a filled contour), Grid resolution 300, Loss vs. epochs on, Residual map on, Save to disk on, DPI 150 for checking or 300 for figures you will publish) and a Report block (Rows grid, Grid points per axis 200, Residual column on). Wire the Train block's Model into both.
Step 10: Check and generate. The Graph check should be clean. Press Generate code, then Download .py, and save it as flow_past_a_cylinder.py. The header of the script is a quick check that the Studio understood the graph: it should say Geometry : Composite, list shape 1 : base rectangle [0, 22] x [0, 4.1] and shape 2 : subtract circle centre (2, 2), radius 0.5, give Coordinates : (x, y) and Conditions : 11 boundary, 0 initial.
If you would rather not wire 21 blocks by hand, open the finished notebook in the Lab instead. The Graph check should say nothing to flag.
4. Running it
The run below used a Colab T4 GPU. A CPU will do the same work, but it is slow enough to be impractical: each epoch takes second derivatives of three outputs at 20,000 points.
On Colab, set the runtime to a GPU, upload flow_past_a_cylinder.py and cylinder_forces.py, and run
python flow_past_a_cylinder.py
python cylinder_forces.py flow_past_a_cylinder.py model.pt --json forces.json
The first command trains and writes model.pt, model.history.json, the figures and results.csv. The second reads the trained network and prints the three benchmark numbers next to their reference values, plus two checks that need no reference.
Upload the script under exactly the name you use in the command. A browser download that turns underscores into hyphens gives No such file or directory. If the first command fails, there is no model.pt, and the second command fails with it.
Read the first log line before you walk away. It should list three PDE terms and eleven BC terms, such as BC1 dirichlet (u1), and a total loss of order 1. A total near $10^{-11}$ within a few epochs means the problem is trivial: no domain was wired, so there are no conditions and the network satisfies three empty equations. Check that the script's header says DIM = 2.
5. How the forces are computed
The force of the fluid on the cylinder, per unit depth, is the integral of the stress over its surface. With $\mathbf n$ the unit normal pointing from the cylinder into the fluid and the stress written as the benchmark writes it, $\boldsymbol\sigma = \tfrac{1}{Re}\nabla\mathbf u - p\,\mathbf I$,
$$ \mathbf F = \oint_{\text{cylinder}} \boldsymbol\sigma\,\mathbf n \; ds, \qquad C_D = 2F_x, \qquad C_L = 2F_y . $$
The factor 2 is the definition $C = 2F/(\rho \bar U^2 D)$ in dimensionless units. cylinder_forces.py evaluates the integrand at 4000 points round the rim, with the derivatives taken by automatic differentiation of the trained network, exactly as the residuals were during training. The pressure drop is $\Delta p = 0.04\,\bigl(p(1.5, 2) - p(2.5, 2)\bigr)$, the factor 0.04 being $\rho\bar U^2$ in Pa.
Two further checks need no reference. Mass: the volume flowing through any vertical line must equal what enters, $\int_0^{4.1} u\,dy = 4.1$, at every $x$. On a line that crosses the cylinder ($x = 2$), only the fluid part counts: inside the solid the network was never trained and its output means nothing. No slip: the speed on the walls and the cylinder must be zero. The script prints both.
6. Results
One run, seed 0, on the notebook exactly as built above.
The three numbers
| Quantity | PINN | Reference | Error |
|---|---|---|---|
| $C_D$ | 4.986 | 5.5795 | −10.6 % |
| $C_L$ | −0.0128 | 0.0106 | wrong sign; absolute error 0.023 |
| $\Delta p$ | 0.1028 | 0.1175 | −12.5 % |
Drag and pressure drop are both about a tenth too low. That is a usable first answer, but an ordinary CFD solver on a modest mesh gets these numbers to well under 1 %, so the network is not competitive here. Lift is a different matter: the network's value is of the right size but the wrong sign. A relative error means little for a number this small; the useful measure is the absolute error, 0.023, which is 0.4 % of the drag. The lift comes from a pressure imbalance of that order round the rim, and the network's pressure is not that accurate (see What this run does not show).
The flow
The horizontal velocity has the right shape. The flow divides round the cylinder and accelerates in the two gaps to about 1.8, more than the inlet peak of 1.5, because the same flow has to pass through less height. Behind the cylinder there is a slow wake. The lowest value on the colour bar is slightly below zero, so the network has a small region of backflow there, as the real flow does at $Re = 20$. A plot at this resolution cannot show how long that region is. The wake closes gradually, and by $x \approx 12$ the profile is a parabola again.
Look at the peak of that parabola. With 4.1 units of fluid per unit time, a parabolic profile across 4.1 must peak at 1.5, as it does at the inlet. Downstream, the colour bar puts it nearer 1.3 to 1.4. That is the first sign of the mass loss measured below.
The vertical velocity shows the flow moving out of the way of the cylinder, up above the centre line and down below it, and then closing back in behind it. It is zero almost everywhere else, as it should be in a straight channel.
The pressure is highest at the front stagnation point, where the flow is brought to rest, and lowest just behind the cylinder. That front-to-back difference is $\Delta p$, and its pull on the cylinder is most of the drag. Away from the cylinder the pressure falls steadily along the channel, which is the pressure gradient that pushes a viscous fluid down a pipe.
The residual map shows where the equations are least well satisfied: in a thin ring round the cylinder and in faint streaks just upstream and downstream of it. That is where the velocity changes fastest, and also where the drag is decided. Downstream the residual is small, but small is not the same as zero, as the next check shows.
Mass and no slip
| Value | Exact | Error | |
|---|---|---|---|
| Flow rate at $x = 0$ (inlet) | 4.093 | 4.1 | −0.2 % |
| Flow rate at $x = 10$ | 3.749 | 4.1 | −8.6 % |
| Flow rate at $x = 22$ (outlet) | 3.643 | 4.1 | −11.2 % |
| Largest speed on walls and cylinder | 0.014 | 0 | 1.4 % of the mean inflow speed |
The boundary conditions are well met. The inflow is right to 0.2 %, and the fluid sticks to the walls and the cylinder to about 1 % of the flow speed. The interior is the weak point: about a tenth of the fluid that enters never leaves.
That can look strange when the continuity loss is only $2.9 \times 10^{-4}$. The reason is that the flow-rate check integrates the continuity error. Because $v = 0$ on both walls,
$$ \frac{d}{dx}\int_0^{4.1} u\,dy = \int_0^{4.1} (u_x + v_y)\,dy , $$
so the flow rate changes along the channel by exactly the integral of the continuity residual across it. A drop of 0.45 over a length of 22 needs an average residual of only about $-0.005$, while the root mean square of the residual here is $\sqrt{2.9\times10^{-4}} \approx 0.017$. The residual is small at every point, but it leans one way, and over a long channel that adds up. This is the general lesson of the example: a small loss does not guarantee a conserved quantity. If it matters, check it directly.
The mass deficit and the low forces are probably linked. A flow that carries about 10 % less fluid past the cylinder pushes less on it, and $C_D$ and $\Delta p$ are low by 11 % and 13 %. This run cannot prove that link: the flow rate right at the cylinder is not in the table, only at $x = 0$, 10 and 22. But it is the first thing to fix (see Try it yourself).
The training record
| Term | Epoch 1,000 | Epoch 10,000 | Epoch 30,000 | After L-BFGS |
|---|---|---|---|---|
| Total | $3.4\times10^{-2}$ | $7.0\times10^{-3}$ | $7.35\times10^{-4}$ | $7.04\times10^{-4}$ |
| PDE1 (continuity) | $2.4\times10^{-2}$ | $3.8\times10^{-3}$ | $3.0\times10^{-4}$ | $2.9\times10^{-4}$ |
| PDE2 (x-momentum) | $2.0\times10^{-3}$ | $1.4\times10^{-3}$ | $2.1\times10^{-4}$ | $2.1\times10^{-4}$ |
| PDE3 (y-momentum) | $1.6\times10^{-3}$ | $9.1\times10^{-4}$ | $1.4\times10^{-4}$ | $1.4\times10^{-4}$ |
| BC1 inflow $u$ | $4.2\times10^{-3}$ | $2.9\times10^{-4}$ | $3.8\times10^{-6}$ | $6.5\times10^{-6}$ |
| BC5 cylinder $u$ | $8.2\times10^{-4}$ | $1.1\times10^{-4}$ | $1.5\times10^{-5}$ | $1.2\times10^{-5}$ |
| BC5 cylinder $v$ | $1.8\times10^{-4}$ | $7.7\times10^{-5}$ | $3.7\times10^{-5}$ | $3.0\times10^{-5}$ |
Four things stand out.
- The boundaries were learned first and best. Within 1,000 epochs the inflow term fell from 1.6 to $4\times10^{-3}$. By the end every boundary term is between $10^{-7}$ and $3\times10^{-5}$, ten to a thousand times below the PDE terms. The remaining error is in the interior equations, with continuity the largest. That is the same picture the mass check gave.
- The cylinder is the hardest boundary. Its two terms are the largest boundary terms at the end. A curved wall in the middle of the flow, with the steepest gradients in the problem, is harder than a straight wall.
- Adam spikes. From about epoch 9,000 the total loss jumps by one or two orders of magnitude for a few epochs and then recovers. The spikes are most frequent between epochs 10,000 and 20,000 and smaller after the second learning-rate cut. They are typical of Adam on a stiff, many-term loss. They did no lasting harm here, but they show that a learning rate of $10^{-3}$, and even $5\times10^{-4}$, is on the high side for this problem.
- L-BFGS did almost nothing. It took the total from $7.35\times10^{-4}$ to $7.04\times10^{-4}$, a 4 % improvement. In other problems in this book the second stage gains an order of magnitude. PyTorch's L-BFGS stops early when its line search makes no further progress, and the saved history does not record how many of the 5,000 iterations it used. Either way, the polish was not where this run's accuracy came from.
7. What to report
If you write this example up, say plainly what was done and what came out:
Steady flow past a cylinder, DFG 2D-1 ($Re = 20$), solved with a PINN built in the Scientific ML Studio: a fully connected tanh network, 5 × 64, with 3 outputs $(u, v, p)$; 20,000 Sobol interior points, 2,200 boundary points; outlet approximated by $p = 0$, $\partial_x u = \partial_x v = 0$; unit loss weights; Adam for 30,000 epochs (learning rate $10^{-3}$, halved every 10,000), then L-BFGS. One run, seed 0, Colab T4 GPU. Result: $C_D = 4.99$ (−10.6 %), $\Delta p = 0.103$ (−12.5 %), $C_L = -0.013$ (reference 0.0106; wrong sign). The flow rate falls from 4.09 at the inlet to 3.64 at the outlet (exact 4.1), and the largest wall speed is 0.014.
Report the training time as well, read from the script's finished in … line. Report the mass check next to the forces: it tells the reader why the forces are off, which the forces alone cannot.
What this run does not show
- Lift is the hardest number. It is about 500 times smaller than the drag and comes from the slight asymmetry of the cylinder's position. Small errors in the pressure round the rim, harmless for the drag, can be as large as the lift itself.
- The points are uniform. A mesh-based solver puts small cells near the cylinder, where the gradients are; the Studio samples uniformly over the whole channel. Most of the 20,000 points are far downstream, where little happens.
- One run, one seed. A second seed could move the drag by a few percent either way. Nothing here measures that spread.
- The outlet is approximate. The condition used here is not the benchmark's do-nothing outlet. Twenty diameters downstream the two should agree closely, but this run does not test that.
8. If something goes wrong
| Symptom | Likely cause | What to try |
|---|---|---|
| Graph check: unknown symbol 'u3_x' | the Network has fewer than 3 outputs | set Network outputs to 3 |
| Graph check: No condition on BC5 | the cylinder BC blocks are on the wrong port | hover the domain ports; BC5 is Shape 2 (cut circle) rim |
| Drag negative, or the flow goes right to left | the inflow profile was given to output 2, or with the wrong sign | output 1 is $u$; the profile is positive |
| Flow rate far from 4.1 downstream, drag far too small | the inflow has not yet been carried down the channel: the network is undertrained, not wrong. In a test run on fewer points, the flow rate at x = 10 went from 0.33 after 3,000 epochs to 1.10 after 6,000 | more Adam epochs before L-BFGS; check that one of the three PDE blocks is u1_x + u2_y |
| Loss stalls high, fields smooth but wrong | the boundary terms dominate | check the loss log term by term; a weight of 10 on the cylinder blocks is a reasonable next try |
| Loss low, forces about 10 % low, flow rate falling along the channel | continuity is satisfied only on average; the error builds up downstream (the run above) | weight continuity more heavily by writing its residual as 3.16*(u1_x + u2_y), or add more interior points; see Try it yourself |
| Total loss near $10^{-11}$ after a few epochs | no domain was wired: the script is 1-D, with no conditions | check the script's header: Geometry : Composite, DIM = 2 |
| Sharp spikes in the loss curve | Adam's learning rate is high for this loss | they usually recover; if not, start at $5\times10^{-4}$ |
9. Try it yourself
- Fix the mass loss. The Loss block's PDE weight scales all three equations together, so to weight continuity alone, scale its residual: write it as
3.16*(u1_x + u2_y). The loss squares the residual, so this counts continuity $3.16^2 \approx 10$ times as heavily, and the equation itself is unchanged. Run again. Does the outlet flow rate get closer to 4.1, and do $C_D$ and $\Delta p$ move with it? This tests the link suggested in section 6. - Move the cylinder to the exact centre, $(2, 2.05)$. The lift should vanish. How close to zero does the network get?
- Halve the viscosity term to
0.025: that is $Re = 40$, still steady. The eddies behind the cylinder grow. - Put the cylinder at $(4, 2)$ and add a second one at $(8, 2)$ with Shape 3.
- Give BC5 1000 points instead of 400 and compare $C_D$.
10. Recap
- The Composite domain block turns "a channel with a cylinder" into two slots: a rectangle, and a circle cut out of it.
- Custom residuals that name the outputs
u1,u2,u3write a coupled system as one PDE block per equation. - A body force is a sharp test of a flow solution: it needs pressure and wall gradients to be right on a curve the network never saw except through its boundary points.
- In this run the network meets its boundary conditions to better than 1 %, yet gives a drag 11 % low and lift of the wrong sign. The flow-rate check finds the likely cause, mass lost along the channel. A low loss is not a verified solution: check conserved quantities directly.
Files: flow_past_a_cylinder.omega-notebook.json (the notebook, to open in the Lab), cylinder_forces.py (drag, lift, pressure drop and the checks, from a trained network).
Reference: M. Schäfer and S. Turek, "Benchmark computations of laminar flow around a cylinder", in E.H. Hirschel (ed.), Flow Simulation with High-Performance Computers II, Notes on Numerical Fluid Mechanics 52, Vieweg, 1996, pp. 547–566. Reference values from the FEATFLOW benchmark page for DFG 2D-1.