Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Traffic Flow Capstone

In the second module you encountered different aspects of numerical problems and solution techniques that interact with each other. You have the interaction of spatial and time discretization operating together, and the implications for stability of the numerical solution. You saw the effect of nonlinearity in the convection equation, and the interplay between diffusion and convection in Burgers’ equation. This is now a broader foundation to build your knowledge of numerical techniques.

In this module capstone, you will put all this to work in a new problem setting: a model of traffic flow. Picture a stretch of one-lane road that starts with light traffic followed by a short patch of higher density. As the patch moves downstream, how does it change? This traffic model will allow you to estimate minimum and average vehicle speeds and investigate how much the computed results depend on the grid and time step.

Vehicles along a stretch of road encounter a high-density patch.

Figure 1:Vehicles along a stretch of road encounter a high-density patch of traffic. How can we predict the minimum and average vehicle speeds?

Engineering brief

A traffic modeling team wants to use a one-lane road model to compare how a dense patch evolves under two sets of traffic conditions. The team needs minimum and average vehicle speeds at selected times, along with enough evidence to decide whether the computed profiles support what they see happening to the patch.

The engineering question is:

Can the team report the minimum and average vehicle speeds with confidence, and which apparent changes in the patch might be caused by the grid and time step?

Start with the 51-point calculation as a provisional result. Compare it with finer calculations at the same physical times. Your recommendation should say which numbers are stable under refinement and what the profiles reveal about where the patch moves and how its edges change.

From density to traffic flux

Let xx measure distance downstream and tt measure time. The traffic density ρ(x,t)\rho(x,t) is the number of cars per kilometer of road. We model the local vehicle speed uu as a function of density alone. At very low density, drivers travel near the free-flow speed umax⁡u_{\max}. At the maximum density ρmax⁡\rho_{\max}, traffic is bumper to bumper and comes to a stop. Assume that the speed decreases linearly between these limits:

u(ρ)=umax⁡(1−ρρmax⁡).u(\rho)=u_{\max}\left(1-\frac{\rho}{\rho_{\max}}\right).

The traffic flux FF is the number of cars passing a point per hour. Multiplying density in cars per kilometer by vehicle speed in kilometers per hour gives

F(ρ)=ρu(ρ)=umax⁡ρ(1−ρρmax⁡).F(\rho)=\rho u(\rho) =u_{\max}\rho\left(1-\frac{\rho}{\rho_{\max}}\right).

The flux is zero on an empty road because there are no cars to pass, and zero at bumper-to-bumper density because the cars are stationary.

Vehicle speed decreases linearly to zero, while the flux rises to a peak and falls back to zero.

Figure 2:Vehicle speed uu and traffic flux FF as functions of density ρ\rho. The flux reaches its maximum at ρ⋆=ρmax⁡/2\rho^\star=\rho_{\max}/2.

How density changes

There are no ramps or other sources of cars along this road. The number of cars on any stretch increases when more cars enter than leave. As in the convection lesson, we express this balance with a conservation equation.

∂ρ∂t+∂F(ρ)∂x=0.\frac{\partial \rho}{\partial t}+\frac{\partial F(\rho)}{\partial x}=0.

Where the density is smooth, the chain rule gives ρt+a(ρ)ρx=0\rho_t+a(\rho)\rho_x=0, with

a(ρ)=dFdρ=umax⁡(1−2ρρmax⁡).a(\rho)=\frac{dF}{d\rho} =u_{\max}\left(1-\frac{2\rho}{\rho_{\max}}\right).

Both uu and aa have units of kilometers per hour: uu describes how fast the cars move, while aa, the characteristic speed, describes how a small change in density travels through traffic. It’s the speed at which information, such as a green light or a braking car, propagates on the road. For densities below ρmax⁡/2\rho_{\max}/2, this change travels downstream because a>0a>0. We will use the conservation form in Equation 3 to compute the evolution of the patch, whose initial density jumps at its two edges.

Problem setup: dense traffic patch, two scenarios

You will use this model to follow a dense patch of traffic as it moves along a one-lane road. You’ll run two scenarios that differ in free-flow speed and background density, then use the computed density to estimate minimum and average vehicle speeds at selected times. You’ll start with a coarse grid and repeat the calculations with finer grids and smaller time steps. By comparing both the speed summaries and the shape of the patch, you’ll judge which features persist under refinement and which may be due to numerical smearing.

The table gives the model parameters, baseline discretization, and speeds to report for each scenario.

SettingScenario AScenario B
Road length11 km11 km
Maximum density ρmax⁡\rho_{\max}250 cars/km250 cars/km
Free-flow speed umax⁡u_{\max}80 km/h136 km/h
Background and left inflow density10 cars/km20 cars/km
Dense-patch density50 cars/km50 cars/km
Baseline grid and time step51 points, 0.001 h51 points, 0.001 h
Speeds to reportMinimum at 0 min, average at 3 min, minimum at 6 minMinimum at 0 min, average and minimum at 3 min

In both scenarios, the road starts at x=0x=0. Set the initial density to the dense-patch value from 2.2 km up to, but excluding, 4.4 km. Use the background density everywhere else. The comparison between scenarios reflects changes in both the free-flow speed and the background density.

The baseline grid includes both endpoints, so Δx=0.22 km\Delta x=0.22\ \mathrm{km} and the patch occupies the Python slice 10:20. Each time you double the number of intervals, double both slice indices to keep the patch in the same place and preserve its width.

Boundary conditions

The initial densities in both scenarios lie below half the maximum density. We will work in this range, where information enters at the left end of the road. Hold ρ(0,t)\rho(0,t) at the scenario’s background density throughout the run. The right end is an outflow boundary. Its density evolves with the solution.

Speeds to report

Use kilometers and hours in the calculation. You will evaluate the solution at the specified times (3 minutes is 0.05 h and 6 minutes is 0.10 h), and convert the vehicle speeds to meters per second for reporting.

For the minimum, use the smallest vehicle speed on the grid. For the average, take the arithmetic mean of the vehicle speeds at all grid points, including the endpoints. This is a spatial sample average. Each grid point carries the same weight regardless of the local number of cars.

Derive the conservative update

Plot the traffic model

From a place on the road to a point on the curve

The model curves above answer “What speed or flux does the model predict at this density?” Their horizontal axis is density. A snapshot of the road answers “What is the density at this location?” Its horizontal axis is distance.

Start at a labeled location on the left, read its density, then use that density on the horizontal axis on the right. Locations P and R are kilometers apart, but they have the same initial density in Scenario A, so they map to the same point on the speed curve. Location Q is inside the patch and maps to a lower speed. You can read the flux curve in the same way, using the density at the location you picked.

Advance and check the traffic model

Compare your discrete update with the supplied step

We apply a backward difference to the flux in Equation 3 and solve the forward difference in time for the new density.

ρin+1=ρin−ΔtΔx[F(ρin)−F(ρi−1n)],i=1,…,N.\rho_i^{n+1}=\rho_i^n-\frac{\Delta t}{\Delta x} \left[F(\rho_i^n)-F(\rho_{i-1}^n)\right], \qquad i=1,\ldots,N.

The two slices of the old flux array, flux[1:] and flux[:-1], pair each updated node with its left neighbor. The first density stays fixed, and the last density is updated by the same rule as the interior. The step returns the old outlet flux minus the old inlet flux, so the time loop can accumulate the net number of cars that left.

For the chosen densities, the characteristic speed is positive and largest at the smallest density. To see why the initial range is preserved, factor the flux difference. The factor multiplying ρin−ρi−1n\rho_i^n-\rho_{i-1}^n is umax⁡[1−(ρin+ρi−1n)/ρmax⁡]u_{\max}[1-(\rho_i^n+\rho_{i-1}^n)/\rho_{\max}]. This is the characteristic speed evaluated at the average of the two densities. If amax⁡Δt/Δx≤1a_{\max}\Delta t/\Delta x\le 1, the update is a weighted average of the two old densities, with nonnegative weights that sum to one. The fixed inlet lies in the same range, so the initial density bounds and the same CFL limit apply at each step.

Check one step against your notes

Run the same three-point example you worked through on paper. First compare each updated density with your hand result. Then check whether the change in the number of cars agrees with what you predicted from the inlet and outlet fluxes.

To estimate the total number of cars on the grid, multiply each updated node’s density (cars/km) by the grid spacing (km) and add: M=Δx∑i=1NρiM=\Delta x\sum_{i=1}^N\rho_i. This is the same sum over nodes 1 through NN that you used in the paper balance. The fixed inlet at node 0 contributes through its flux.

The cell computes the change in MM from the old and new densities. Your paper calculation gives another way to find that change: use the old fluxes and multiply the inlet flux minus the outlet flux by the time step. The balance residual is the difference between these two estimates, in cars. How close is it to zero?

See what a constant-state check misses

A constant density is a useful first test. If every point, including the inlet, starts at the same density, the flux is the same everywhere, so the density should stay unchanged. How much does passing this test tell us about the update?

To find out, we will test the supplied conservative step alongside a version with one deliberate mistake. The wrong step replaces the flux difference in Equation 5 with a density difference multiplied by the local vehicle speed, u(ρin)u(\rho_i^n). The grid, time step, and inlet are the same for both versions.

  1. Start with constant density. Both versions start with [10,10,10][10,10,10] cars/km. Predict what each will do when both the flux differences and the density differences are zero. The first two lines of output tell you whether each version leaves that state unchanged.

  2. Return to the hand example. Both versions start with [10,50,20][10,50,20] cars/km. Compare the wrong update’s densities with your hand calculation and the conservative result above. Because the inlet and outlet fluxes differ in this example, you also have an independent prediction for the change in the discrete vehicle total. Use the same prediction for both versions: Δt\Delta t times the old inlet flux minus the old outlet flux, with both fluxes inside the time-step factor. A balance residual measures how far the computed change is from that prediction.

Compare the two residuals and the two updated arrays with your notes. Why does a constant state give both updates the same result? Which checks distinguish them on the nonuniform example? A small balance residual checks the net change in the discrete total. The hand calculation also checks how the change is distributed among the nodes.

Set up and run the baseline calculation

The initializer uses the number of intervals, NN, so the array contains N+1N+1 points. On grids with NN divisible by five, integer slice indices put the patch boundaries at one-fifth and two-fifths of the road length.

The driver advances to a single requested time T>0T>0. It rounds T/Δtmax⁡T/\Delta t_{\max} up to choose the number of steps and divides TT by that count to get the actual step. It checks the CFL number before advancing. The driver is for the positive characteristic speeds in this capstone, with the leftmost initial value held fixed. It saves the final array, the accumulated net outflow, and the step information needed to compare runs.

Begin with 50 intervals and the baseline maximum step of 0.001 h. Each call starts from the same initial state for its scenario. Store the returned profiles so you can compare them with finer grids later. The six numbers are baseline estimates. The refinement investigation will show how sensitive they are to the grid and step.

Check the baseline runs before refining

At each observation time, check that the inlet is unchanged, that the densities stay within their starting range, and that the reported CFL number satisfies the restriction. Then check the cumulative vehicle balance. Let Mn=Δx∑i=1NρinM^n=\Delta x\sum_{i=1}^{N}\rho_i^n be the discrete total over the updated nodes. The balance residual is

Rn=Mn−M0+∑k=0n−1Δt[F(ρNk)−F(ρ0k)].R^n=M^n-M^0+\sum_{k=0}^{n-1}\Delta t \left[F(\rho_N^k)-F(\rho_0^k)\right].

The solver already accumulated the last term using the old boundary fluxes at each step. Compute the change in MM from the initial and final arrays. Compare the residual with a tolerance scaled to the totals and accumulated flux, allowing for floating-point rounding over many updates.

Look at the density along the road

Plot density against distance for the initial state and the saved observation times. Both panels use the same axes. The dashed line shows the initial density patch. For the later snapshots, the markers show the computed values at the grid points and the lines connect them to guide your eye.

For each scenario, compare the initial patch with the three-minute snapshot. Where has the region of highest density moved? How have its peak and its two edges changed? For Scenario A, follow those changes through six minutes. Use the model speed curve to explain how a change in the peak density affects the minimum vehicle speed.

The baseline profiles show how the patch evolves in the 51-point calculation. To tell which features depend on the discretization, compare profiles at the same physical time with the same initial patch on finer grids. Choose one feature to track, such as the height of the peak or the width of an edge, and write down what evidence would make you trust the baseline picture of that feature.

Investigate space–time refinement

Plan the grid and time-step study

Return to the feature you chose in the baseline plots. Write down what you expect to change as the grid gets finer. If you halve both Δx\Delta x and the time step, how do you expect the CFL number and the number of point updates to change? Keep these predictions in your notes.

Use the supplied solver to compare grids with 50, 100, 200, 400, 800, and 1,600 intervals. These have 51 through 1,601 points. Start with a maximum time step of 0.001 h and halve it each time you double the interval count. Keep the same physical patch and compare at the same observation times: 3 and 6 minutes for A, and 3 minutes for B.

Decide what to measure

For each adjacent pair of grids, find the absolute change in each requested speed summary at a positive time. Also compare the full vehicle-speed profiles at every positive observation time. Convert to m/s before comparing. Every second fine-grid point coincides with a coarse-grid point, including both endpoints. At those shared points, take the absolute speed differences d0,…,dNd_0,\ldots,d_N and compute their trapezoidal average over the road.

D=ΔxL(12d0+∑i=1N−1di+12dN).D=\frac{\Delta x}{L}\left(\tfrac12 d_0+\sum_{i=1}^{N-1}d_i+\tfrac12 d_N\right).

Here Δx\Delta x is the coarse spacing. Take absolute differences before averaging so that differences of opposite sign cannot cancel. A small whole-road average can still conceal a large change near one edge.

Set the decision rule before running

For a given scenario, mark a grid pair as passing if every applicable speed summary change and every profile average DD is below 0.1 m/s. Use unrounded values. The candidate is the finer grid in the second of the first two consecutive passing pairs.

On that candidate grid, halve the maximum time step and repeat the runs at the same observation times. Check that the requested summary changes and the profile averages between the two time-step choices are also below 0.1 m/s. For this comparison the grids are identical, so use all points directly. If either stage leaves you without a passing result, report which measure is unresolved within the tested runs. The threshold describes sensitivity to these changes in grid and step.

Record the agent task brief

Audit before running

Before executing, trace one comparison through the proposed code. Check the scenario parameters, patch alignment, actual observation time, and the pairing of shared positions. Check that the endpoints receive half weight in the profile average and that work counts the updated nodes times the actual number of steps. If something differs from your brief, request a focused correction and inspect it before running.

Check one result independently

Return to the feature you chose in the baseline plots. Overlay two profiles at the same time and mark where they differ near that feature. Record what the local comparison adds to the summary changes and whole-road average. Keep a short agent record with your brief, material revisions, and the checks you performed.

Write your engineering verdict

Write a short verdict in your working notebook. Use the tables, the comparison you reproduced, and the profile overlay you already made.

  • For each scenario, state which grid and actual time step support your reported speeds under the sensitivity rule. Give the speed estimates in m/s and cite the comparisons and work counts that support your choice. If the evidence leaves a claim unresolved, say which measure or check limits it.

  • Point to the feature you tracked in the overlay. Explain what changed with refinement and what the whole-road average or the minimum alone would have missed.

  • Name one consequential check you used to assess the agent’s experiment. Explain what the evidence supports within this idealized model and what you would need to compare the prediction with traffic on a real road.

Completion rubric

LevelEvidence in your work
4 — Complete with distinctionThe core evidence is complete, and you use it to explain a consequential uncertainty or limitation, such as a local change concealed by a stable whole-road average.
3 — CompleteYou can explain your paper update and verification checks, show how you audited the refinement experiment, and defend your verdict from the evidence.
2 — DevelopingYou can explain a substantial attempt, identify a gap that weakens your claim, and propose a concrete revision.
1 — Not assessableEssential evidence is missing or you cannot explain a consequential part of the work.