M0.1 6 min read Oct 2026

M0.1 Counting Gate Crossings: Divergence and Flux

Builds on1 note

We cannot see inside the station

You manage a station with no interior camera. Counters record entries and exits at four gates.

Is the population inside increasing or decreasing, and at what rate?

Adding entries and subtracting exits works only if every crossing is counted. Trains can carry people through another part of the boundary.

How can we formalize boundary flow and then localize it to a small region?

DiscussionQ1

What are the region, boundary, measured quantity, and desired quantity?

Show the reasoning

Before subtracting counts, identify the objects.

The region Ω\Omega is the station interior. Its total population is N(t)N(t), and we seek dN/dtdN/dt.

The boundary ∂Ω\partial\Omega separates inside and outside. Gates are traversable parts; walls have no crossing flow.

Counters measure net people crossing a boundary segment per time, with a chosen outward-positive sign convention.

Conservation connects crossings to enclosed change: no one appears or disappears inside. Complete boundary accounting is also required.

A sprinkler, a drain, and a corridor

To count people, speed alone is insufficient. Define the flow field j=ρvj=\rho v, multiplying density by velocity. Its arrows represent transported amount.

Outward arrows around a sprinkler give positive net outflow; inward arrows around a drain give negative net outflow. Uniform corridor flow has equal inflow and outflow.

Shrink a surrounding region to each point and measure net outflow per area. The resulting signed local quantity is divergence.

Source, sink, and uniform through-flow, with their local net outward fluxes.

Figure: positive divergence at a source, negative at a sink, and zero for uniform through-flow.

Express the local counting mathematically

Flux. In two dimensions, jj has units people per length per time. For a boundary element dsds with outward unit normal nn, j⋅n dsj\cdot n\,ds counts outward flow. Only the normal component crosses:

Φ(Ω)=∮∂Ωj⋅n ds,\Phi(\Omega)=\oint_{\partial\Omega}j\cdot n\,ds ,

This is outward-positive flux. Conservation gives

dNdt=− Φ(Ω):\frac{dN}{dt}=-\,\Phi(\Omega):

Enclosed change equals net inflow, provided all changes arise from boundary crossings and there are no internal source terms.

Divergence. Consider a small rectangle. Horizontal outflow minus inflow is

[jx ⁣(x+Δx2,y)−jx ⁣(x−Δx2,y)]Δy.\left[j_x\!\left(x+\frac{\Delta x}{2},y\right) -j_x\!\left(x-\frac{\Delta x}{2},y\right)\right]\Delta y.

Divide by area, add the vertical contribution, and take the limit:

∇ ⁣⋅j  =  ∂jx∂x+∂jy∂y  =  lim⁡∣Ω∣→0Φ(Ω)∣Ω∣.\nabla\!\cdot j\;=\;\frac{\partial j_x}{\partial x}+\frac{\partial j_y}{\partial y} \;=\;\lim_{|\Omega|\to0}\frac{\Phi(\Omega)}{|\Omega|}.

Divergence adds directional outflow differences. ∇⋅j\nabla\cdot j tracks mass transport; ∇⋅v\nabla\cdot v instead measures local volume expansion. Population density requires the former.

Divergence theorem. Tile the region with small cells. Shared internal edges contribute equal and opposite flows, canceling and leaving only the external boundary:

∮∂Ωj⋅n ds  =  ∫Ω∇ ⁣⋅j  dA.\oint_{\partial\Omega}j\cdot n\,ds\;=\;\int_\Omega\nabla\!\cdot j\;dA .

For sufficiently regular fields and boundaries, integrated interior divergence equals boundary flux.

DetailsWhy do internal boundaries cancel exactly?

The same edge flow is +F+F for one cell and −F-F for its neighbor. Their sum is exactly zero. Taking a sufficiently regular continuum limit yields the divergence theorem.

State the assumptions behind the intuitive answer

The result. Population change equals negative outward flux. This is an exact conservation balance.

The assumptions. Include every traversable boundary and any internal sources. Train doors, staff exits, and emergency doors can invalidate a calculation based only on fare gates. A net gate inflow can coexist with a falling station population if a train carries more people away.

What divergence adds. Boundary counts describe total change. The density-weighted flow field allows local density-change calculations. Disagreement between them can indicate missing channels, measurement error, or incorrect assumptions.

Calculate the balance

The grid example verifies exact cancellation, then adds an unrecorded removal:

import numpy as np
n = 40; rho = np.full((n, n), 5.0)
Fx = 0.3 + 0.1*np.random.randn(n, n+1)
Fy = 0.1*np.random.randn(n+1, n)
inside = (slice(10, 30), slice(10, 30))
for leak in (0.0, 0.5):
    N0 = rho[inside].sum()
    div = (Fx[:, 1:] - Fx[:, :-1]) + (Fy[1:, :] - Fy[:-1, :])
    rho_new = rho - div; rho_new[20, 20] -= leak
    flux_out = Fx[10:30, 30].sum() - Fx[10:30, 10].sum() + Fy[30, 10:30].sum() - Fy[10, 10:30].sum()
    print(f"actual {rho_new[inside].sum()-N0:+.3f}; boundary {-flux_out:+.3f}")

Without the removal, the numbers agree. With it, they differ by 0.5. In this grid code the extra removal is represented as a source/sink term rather than an explicit boundary edge.

Check your understanding

Check your understanding

With no internal sources, where is crowd density increasing fastest?

Check your understanding

Reliable gate counters show persistent inflow, but population remains constant. What should you check?

Check your understanding

What follows from a smooth mass-flow field having zero divergence throughout a region?

References

  1. Strang, G. Calculus. Wellesley-Cambridge Press; MIT OpenCourseWare RES.18-001
  2. Feynman, R. P., Leighton, R. B., Sands, M. The Feynman Lectures on Physics, Vol. II, Ch. 3: Vector Integral Calculus.
  3. Marsden, J. E., Tromba, A. J. Vector Calculus. W. H. Freeman.