Couple velocity and pressure in Stokes flow¶
Goal: express coupled fields and choose a divergence-free velocity space. Read symbolic PDEs and global collocation first.
1. Model creeping flow between rotating cylinders¶
In an annulus with radii \(a=0.5\) and \(b=1\), solve
The inner wall rotates with angular speed one, so \(\boldsymbol u=(-y,x)\) there. The outer wall is fixed. There is no convective term in steady Stokes flow.
import numpy as np
import sympy as sp
import rbflab as rbf
from rbflab import geometry, meshgen
domain = geometry.Annulus(.5, 1.)
cloud = meshgen.generate(domain, interior=70,
boundary={"inner": 32, "outer": 48}, seed=42)
The two labels connect wall pieces to different vector boundary equations. The cloud has 70 interior and 80 boundary nodes.

2. Declare the fields and momentum equation¶
model = rbf.SymbolicStokes(2,vector_fields=("U",), scalar_fields=("p",))
U, p = model.fields
x, y = model.coordinates
mu = 1
momentum = -mu*model.laplacian(U) + model.gradient(p)
U is a two-component symbolic vector; p is a scalar symbolic function.
laplacian(U) acts component by component, while gradient(p) is a vector.
Their sum expresses the two momentum equations.
problem = model.stationary([sp.Eq(momentum, sp.zeros(2, 1))], boundary=[
model.bc("inner", sp.Eq(U, sp.ImmutableMatrix([-y, x]))),
model.bc("outer", sp.Eq(U, sp.zeros(2, 1))),
])
sp.zeros(2, 1) is a vector right-hand side. Boundary equations also compare
vectors. This velocity-Dirichlet problem does not prescribe pressure on the wall.
3. Choose the approximation spaces¶
spaces = rbf.MixedSpace({U: rbf.DivergenceFreeSpace(rbf.IMQ(3), 2),
p: rbf.PressureSpace(rbf.IMQ(3), 1)})
method = rbf.GlobalCollocation(spaces=spaces)
system = method.assemble(problem, cloud)
solution = system.solve()
The dictionary keys are the symbolic fields. Each value describes how that field is approximated. For velocity, the scalar radial kernel generates
Every column has zero divergence. Velocity polynomials are restricted to
divergence-free modes as well. Thus the equation list contains momentum, while
incompressibility belongs to the trial space. Adding an explicit
Eq(model.divergence(U), 0) is not how this space-based route is specified.
The velocity polynomial degree is two; the pressure degree is one. Pressure is represented modulo additive constants. This formulation handles the nullspace in its construction; it is not an arbitrary extra pressure row in a scalar block system. The space derivation explains the coupled kernel and its pressure representation.
4. Recover physical fields¶
query = np.array([[.75, 0.], [0., .75], [-.6, .2]])
velocity = solution.velocity(query) # shape (M, 2)
pressure_gradient = solution.pressure_gradient(query) # shape (M, 2)
pressure = solution.pressure(query, reference=([.75, 0.], 0.)) # shape (M,)
Velocity and pressure gradient have shape (M, 2); pressure has shape (M,).
The reference fixes the displayed pressure constant at \((0.75,0)\). It does not
alter velocity or the pressure gradient.
Here the analytic solution is
Constant pressure belongs to this creeping-flow model. Finite-inertia circular flow has a different momentum balance.
5. Display and modify the problem¶
from rbflab import viz
fig, ax = viz.plot_velocity(solution, domain=domain, title="Rotating inner wall")
- Change wall expressions to prescribe another velocity.
- Replace the zero momentum right-hand side with a vector body force.
- Change kernels independently in velocity and pressure spaces.
- For another domain, supply its corresponding boundary labels and data.
This lesson uses the Python global space assembler. Selecting a local C++ backend does not accelerate this dense system. LHI Stokes has different capabilities, including pressure-gradient reconstruction rather than a globally reconciled pressure field; see capabilities before changing routes.
A quick check¶
Compare velocity with the azimuthal formula at a few points. Pressure-gradient accuracy is a separate question; the existing validation record reports it independently.
Complete example¶
Run python -m examples.tutorials.stokes_construction from a source checkout.
Add --plot stokes.png to save a velocity figure.
Complete runnable script
"""Build steady annular Stokes flow through symbolic fields and kernel spaces."""
import argparse
import numpy as np
import sympy as sp
import rbflab as rbf
from rbflab import geometry, meshgen
def run(plot=None):
domain = geometry.Annulus(.5, 1.)
cloud = meshgen.generate(domain, interior=70,
boundary={"inner": 32, "outer": 48}, seed=42)
model = rbf.SymbolicStokes(2,vector_fields=("U",), scalar_fields=("p",))
U, p = model.fields
x, y = model.coordinates
mu = 1
momentum = -mu*model.laplacian(U) + model.gradient(p)
problem = model.stationary([sp.Eq(momentum, sp.zeros(2, 1))], boundary=[
model.bc("inner", sp.Eq(U, sp.ImmutableMatrix([-y, x]))),
model.bc("outer", sp.Eq(U, sp.zeros(2, 1))),
])
spaces = rbf.MixedSpace({U: rbf.DivergenceFreeSpace(rbf.IMQ(3), 2),
p: rbf.PressureSpace(rbf.IMQ(3), 1)})
method = rbf.GlobalCollocation(spaces=spaces)
system = method.assemble(problem, cloud)
solution = system.solve()
query = np.array([[.75, 0.], [0., .75], [-.6, .2]])
velocity = solution.velocity(query) # shape (M, 2)
pressure_gradient = solution.pressure_gradient(query) # shape (M, 2)
pressure = solution.pressure(query, reference=([.75, 0.], 0.)) # shape (M,)
factor = (1/np.sum(query*query, axis=1)-1)/3
truth = np.column_stack((-query[:, 1]*factor, query[:, 0]*factor))
error = float(np.max(np.abs(velocity-truth)))
print(f"velocity={velocity.shape}, pressure={pressure.shape}, pressure gradient={pressure_gradient.shape}")
print(f"sampled velocity error={error:.3e}")
if plot:
from rbflab import viz
fig, ax = viz.plot_velocity(solution, domain=domain, title="Stokes / rotating inner wall")
fig.savefig(plot, dpi=160)
return error
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--plot")
run(**vars(parser.parse_args()))
Next: Build a cavity algorithm, using a different scalar-space velocity-pressure discretization and explicit sparse blocks.