Numerical Methods · interactive worked example

Potential Flow Over an Irregular Conduit

Laplace's equation on a rectangle is easy to mesh — every interior point looks the same. Cut a slanted wall into the domain and suddenly the mesh itself has to be generated automatically, node by node, before you can even write the equations.

The problem

A perfect (inviscid, incompressible) fluid flows through a flat conduit that's 1.6 m long and 1.2 m tall — but it isn't a plain rectangle. The lower-left corner is cut away by a straight diagonal wall, so the conduit narrows on the inlet side:

0.6 m 1.2 m 0.4 m 1.6 m entrance exit diagonal wall

Fluid enters along the top free surface at a steady velocity, and on the entrance (left) and exit (right) faces the velocity is zero at the wall and ramps linearly up to the free-surface value — a no-slip-like profile, not the free-stream value applied everywhere. Every other edge — the diagonal wall and the short wall segments — is a solid boundary at zero velocity. The question: what does the full velocity field look like everywhere inside?

The same setup, in the grid units the solver actually uses (each step = 0.2 m): the boundary is just the ordered list of corners (1,7)→(9,7)→(9,1)→(7,1)→(4,4)→(1,4)→(1,7). You can edit that list yourself in the panel below — it's prefilled with this exact shape, but any closed polygon on the grid works.

∇²u = 0 → u[i−1,j]+u[i+1,j]+u[i,j−1]+u[i,j+1]−4u[i,j] = 0
Same Laplace equation as steady conduction or electrostatics — here u is a potential-flow velocity. The 5-point stencil is identical everywhere; only the boundary handling has to adapt to the shape.

Velocity field u(x,y) — solved live on your domain

Darker = slower · Brighter = faster
Unknown nodes solved
Max interior velocity
Grid size

What you're seeing

Entrance velocity (free surface)

10

Exit velocity (free surface)

10

Define the domain

One "i,j" grid corner per line, in order around the boundary. Top edge = free surface, leftmost vertical edge = entrance, rightmost vertical edge = exit, everything else = wall.

How the mesh gets built automatically

From geometry to matrix, step by step

  1. Read the boundary as a list of points. The conduit's outline is just an ordered list of grid corners: (1,7)→(9,7)→(9,1)→(7,1)→(4,4)→(1,4)→back to (1,7). No formula for the shape is needed — just the points.
  2. Classify every grid node. A node is either outside the domain, on the boundary (value given), or interior (value unknown) — found automatically with a point-in-polygon test against whatever boundary you typed in, plus an edge walk that marks every lattice point lying exactly on the boundary itself.
  3. Write the same 5-point Laplace stencil everywhere. Every interior node gets the identical finite-difference equation; the geometry only changes which neighbors are "boundary" (known) vs "interior" (unknown) for a given node.
  4. Assemble into a matrix. Known boundary values move to the right-hand side; unknown-to-unknown coefficients build a sparse matrix — solved here by direct elimination since the system is small.
  5. Solve and plot. The same linear-algebra step as the Gauss-Seidel lab — this lab uses direct Gaussian elimination since 23 unknowns solves instantly, but the same matrix could just as easily be handed to an iterative solver.

None of this changes if you swap "potential flow velocity" for "steady-state temperature" or "electrostatic potential" — Laplace's equation doesn't know which physics it's describing. The geometry-handling machinery is the actual reusable skill.

Reading the result

With both free-surface velocities equal, entrance and exit are balanced — try making the exit velocity larger than the entrance instead: the flow accelerates through the narrowing/widening geometry, and the contour bands compress near the slanted wall where the domain changes shape fastest.

Try editing the boundary list itself — delete the diagonal-cut points (4,4) and (1,4) and replace them with a plain rectangle, or add your own notch or step. The mesh, the matrix, and the solve all rebuild automatically from whatever closed shape you type in.

EngineeringCandy · Mesh generated and matrix solved live, from raw boundary points · the slanted wall is just data, not a special case in the code