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.

Burgers’ equation

So far in this module, we have learned about the finite-difference solution for the linear and non-linear convection equations and the diffusion equation. It’s time to combine all these into one: Burgers’ equation.

Burgers’ equation in one spatial dimension looks like this:

∂u∂t+u∂u∂x=ν∂2u∂x2\frac{\partial u}{\partial t} + u \frac{\partial u}{\partial x} = \nu \frac{\partial ^2u}{\partial x^2}

Here, ν>0\nu>0 is the viscosity. The nonlinear convection term transports the velocity field, while the diffusion term smooths its spatial variations.

Convection and diffusion in one problem

In the convection problems, information traveled across the domain. In the diffusion problem, spatial variations became smoother. Burgers’ equation lets these two processes act together: the local velocity uu determines how fast the field is transported, while viscosity ν\nu smooths its gradients. A region with larger velocity can catch a slower region ahead of it, steepening the profile as diffusion works to smooth it.

Before investigating the formation of a steep front, we need a calculation whose answer we can check. We will begin with a smooth, periodic solution that is known exactly. It travels around the domain and gradually smooths, giving us a reference against which to measure numerical error.

We will also return to a question from Lesson 6: does discretizing uuxu u_x give the same numerical behavior as discretizing (u2/2)x(u^2/2)_x? The two expressions agree for a smooth function, but their discrete updates need not preserve the same properties. Here we will look for preservation of the mean velocity and agreement with the exact profile. A computation can pass one check without passing the other.

Periodic domain

Imagine that the right end of the interval connects back to its left end. A profile leaving one side reappears on the other. The periodic boundary condition is

u(x+L,t)=u(x,t).u(x+L,t)=u(x,t).

There is no separate inlet or outlet. For a smooth periodic Burgers solution, the spatial mean stays constant: integrating the conservative form of the equation around the domain leaves no net convective or diffusive boundary flux.

We store each physical point just once. With NN points, the spacing is Δx=L/N\Delta x=L/N, and the final stored point is L−ΔxL-\Delta x. The point at x=Lx=L coincides with x=0x=0 and is excluded using endpoint=False. This differs from our earlier grids with two distinct fixed endpoints.

Reference solution

Let k=2π/Lk=2\pi/L, q(t)=ae−νk2tq(t)=a e^{-\nu k^2t}, and θ=k(x−Ut)\theta=k(x-Ut). With these parameters, our reference solution is

ϕ=1+qcos⁡θ,u(x,t)=U−2νϕxϕ=U+2νkqsin⁡θ1+qcos⁡θ.\phi=1+q\cos\theta,\qquad u(x,t)=U-2\nu\frac{\phi_x}{\phi} =U+\frac{2\nu k q\sin\theta}{1+q\cos\theta}.

Here UU is the mean velocity, while u(x,t)u(x,t) is the evolving field. Taking 0<a<10<a<1 keeps the denominator positive. The expression is periodic and satisfies the viscous Burgers equation. Its shape translates at speed UU, and its variation about UU decays.

The parameter aa controls the initial shape. It is dimensionless; UU has units of velocity, and ν\nu has units of length squared per time. The factors kxkx and νk2t\nu k^2t are therefore dimensionless.

This expression is more complicated than the sine-decay solution in Lesson 8, but it has a similar role: it supplies an independent reference at any position and time. We will evaluate the reference solution directly in a small function. We use L=2πL=2\pi, U=1U=1, ν=0.1\nu=0.1, and a=0.95a=0.95. We will inspect the resulting initial profile before choosing the convection discretization.

The initial condition for this benchmark is the same expression evaluated at t=0t=0. Note that changing viscosity in this formula also changes that initial profile.

First import NumPy and Matplotlib, choose the parameters, and construct the unique periodic grid. The four observation times will let us follow the profile through almost one circuit of the domain.

Evaluate the exact solution

The function exact_solution() implements the formula above. It accepts an array of positions, so NumPy evaluates the entire profile at once. We keep the parameters explicit: the same function will provide the initial array and the reference at each later time.

Inspect the initial profile

Evaluate the exact solution at t=0t=0 to construct u0, then plot it to see the data we will evolve. First check that 0<a<10<a<1 and that viscosity is positive, as assumed in the reference formula.

For plotting only, append the point at x=Lx=L with the value at x=0x=0 to close the periodic curve. The solver will still use the original array of unique points.

The profile lies above zero; its smallest sampled velocity is approximately 0.392. Confirm this at every grid point with np.all(u0 > 0.0): the comparison produces one Boolean value per point, and np.all returns true only if every comparison is true.

These positive initial velocities give us the setting for backward differences in convection. Later, the time-step argument will show why both numerical updates keep their values within the initial range, preserving positivity throughout the calculation.

Two ways to discretize convection

Write λ=Δt/Δx\lambda=\Delta t/\Delta x and r=νΔt/Δx2r=\nu\Delta t/\Delta x^2. The original pointwise update is

uin+1=uin−λuin(uin−ui−1n)+r(ui+1n−2uin+ui−1n).u_i^{n+1}=u_i^n-\lambda u_i^n(u_i^n-u_{i-1}^n) +r(u_{i+1}^n-2u_i^n+u_{i-1}^n).

For a smooth solution, uux=(u2/2)xu u_x=(u^2/2)_x. Discretizing this flux derivative instead gives the conservative update:

uin+1=uin−λ[f(uin)−f(ui−1n)]+r(ui+1n−2uin+ui−1n),f(u)=u22.u_i^{n+1}=u_i^n-\lambda\left[f(u_i^n)-f(u_{i-1}^n)\right] +r(u_{i+1}^n-2u_i^n+u_{i-1}^n),\qquad f(u)=\frac{u^2}{2}.

np.roll(u, 1) supplies every left neighbor, including across the periodic seam; np.roll(u, -1) supplies every right neighbor. All terms read the old state. Backward flux differencing here is used only for positive velocities.

The pointwise update

burgers_step() combines the backward difference of uu multiplied by the local velocity with the centered second difference. We retain it as the comparison method from our earlier convection work.

The conservative update

The function conservative_burgers_step() changes only the convection term: it subtracts neighboring values of the flux f(u)=u2/2f(u)=u^2/2. The diffusion term and periodic neighbors are identical. This isolates the effect of the convection discretization.

Why the conservative update preserves the mean

Define uˉn=N−1∑iuin\bar u^n=N^{-1}\sum_i u_i^n, counting each periodic point once. Summing the conservative update gives

uˉn+1−uˉn=−λN∑i[f(uin)−f(ui−1n)]+rN∑i[ui+1n−2uin+ui−1n]=0.\bar u^{n+1}-\bar u^n= -\frac{\lambda}{N}\sum_i[f(u_i^n)-f(u_{i-1}^n)] +\frac{r}{N}\sum_i[u_{i+1}^n-2u_i^n+u_{i-1}^n]=0.

Each shifted sum contains exactly the same values: both differences telescope around the periodic grid. Conservation follows from the discrete algorithm, up to floating-point roundoff.

For the pointwise scheme, the identity

∑iui(ui−ui−1)=12∑i(ui−ui−1)2\sum_i u_i(u_i-u_{i-1})=\frac12\sum_i(u_i-u_{i-1})^2

instead gives

uˉn+1−uˉn=−λ2N∑i(uin−ui−1n)2.\bar u^{n+1}-\bar u^n=-\frac{\lambda}{2N}\sum_i(u_i^n-u_{i-1}^n)^2.

It loses mean whenever the old state is nonconstant. This is a property of the discretization, not an indexing error.

Derive a common time-step bound

For the conservative scheme, factor the flux difference and define

bi=λ2(uin+ui−1n).b_i=\frac{\lambda}{2}(u_i^n+u_{i-1}^n).

Then

uin+1=(bi+r)ui−1n+(1−bi−2r)uin+rui+1n.u_i^{n+1}=(b_i+r)u_{i-1}^n+(1-b_i-2r)u_i^n+r u_{i+1}^n.

If 0≤uin≤M0\le u_i^n\le M, then 0≤bi≤λM0\le b_i\le\lambda M. Thus all three weights are nonnegative and sum to one whenever

Δt(MΔx+2νΔx2)≤1.\Delta t\left(\frac{M}{\Delta x}+\frac{2\nu}{\Delta x^2}\right)\le1.

Choose MM as the largest initial velocity. Under the time-step restriction above, both schemes update each point by taking a weighted average of old neighboring values. Neither scheme can therefore create a value outside the initial range. The largest initial velocity remains an upper bound at every later step, so we can keep the same time-step limit throughout the calculation. Since the initial velocities are all positive, the computed velocities remain positive as well.

Compare your hand-calculated values with the expected arrays in the assertions below before executing the cell. The checks also confirm that the input array is unchanged and that both schemes preserve a constant state.

Integrate both methods to the same times

Use the initial maximum M=max⁡iui0M=\max_i u_i^0 in the time-step bound and multiply the permitted step by the safety factor 0.8. The range-preservation argument lets us use this bound throughout the run.

For each observation interval, np.ceil rounds the required number of steps upward. Dividing the interval by that count gives a step no larger than the target and lands exactly at the requested time. Both methods receive that same step.

The dictionaries states and snapshots keep the two calculations separate, with each starting from its own copy of u0. We retain only the four requested profiles. Within the loop, assertions check for nonfinite values and departures from the initial range. We also record the largest mean drift over all steps and check that the conservative scheme’s drift stays below 10-12 for this run.

Compare the profiles

The extra point at x=Lx=L below is added only for plotting, by copying the value at x=0x=0. The numerical solver always uses the unique periodic grid.

All panels share axis limits, so changes in amplitude can be compared directly. The exact solution is drawn in ink, the pointwise result in gray, and the conservative result in red. Line styles distinguish the curves as well as color.

Measure what the plot cannot establish

The curves are close enough that visual agreement alone is difficult to judge. Compute the maximum error over the grid and the change in the discrete mean:

E∞(t)=max⁡i∣uicomputed−u(xi,t)∣,Δuˉ(t)=1N∑iuicomputed−1N∑iui0.E_\infty(t)=\max_i|u_i^{\mathrm{computed}}-u(x_i,t)|, \qquad \Delta\bar u(t)=\frac1N\sum_i u_i^{\mathrm{computed}}-\frac1N\sum_i u_i^0.

The first quantity measures agreement with the exact reference. The second measures whether the algorithm preserves its initial discrete mean; it uses the sampled initial mean rather than assuming that a finite grid integrates the exact profile perfectly. Neither diagnostic replaces the other.

At t=6t=6, the maximum error is about 0.0216 for the pointwise scheme and 0.0101 for the conservative scheme. The pointwise mean has decreased by about 0.00453, while the conservative mean is preserved to roundoff. The negative drift agrees with the sum-of-squares expression derived above.

For these parameters and this grid, the conservative scheme both preserves the mean and has a smaller maximum error. Conservation does not guarantee a smaller error in every comparison, and a single grid does not establish convergence. We have checked an important discrete property and measured accuracy for one smooth problem.

Convection steepens and diffusion smooths

The reference problem let us check the algorithms against a known solution. Now use the conservative update to investigate the competition in Burgers’ equation. Can a smooth wave develop a steep front before diffusion smooths it away?

Choose a new initial condition on the same periodic domain:

u(x,0)=U+Asin⁡(kx),k=2π/L.u(x,0)=U+A\sin(kx),\qquad k=2\pi/L.

Keep U=1U=1 and L=2πL=2\pi, and choose A=0.75A=0.75. The velocity initially ranges from 0.25 to 1.75. Unlike our reference family, this initial profile does not depend on viscosity. We can change ν\nu while keeping the initial data fixed, so differences between the calculations reveal the effect of diffusion.

On the descending side of the wave, larger velocities lie behind smaller ones. The faster part catches the slower part, compressing the profile and making its slope more negative. On the ascending side, faster values lie ahead and pull away. Diffusion opposes the formation of sharp spatial variations. Which effect dominates depends on the viscosity, the profile, and the length scale being examined.

We will compare ν=0.5\nu=0.5 and ν=0.05\nu=0.05 at the same physical times. With positive viscosity the front remains continuous: a thin, steep transition is often called a viscous shock. It is not a discontinuity.

Reuse the conservative update

The function below organizes repeated calls to conservative_burgers_step(); it introduces no new numerical method. As before, it chooses a step from the initial maximum, adjusts it to reach each requested time, and saves only those profiles. We will reuse it for both viscosities and for the grid check. The requested times start at zero and increase.

Hold the initial data fixed

Use 512 unique periodic points for the first comparison. The two runs begin with the same sine array and finish at the same observation times. Their time steps can differ: each must satisfy the combined convection–diffusion restriction for its viscosity. Reducing viscosity does not remove the convection contribution to that restriction.

The panels use common axes. Gray shows the larger viscosity; red shows the smaller one. At zero time the curves coincide because their initial conditions are identical. A duplicated endpoint closes each curve for plotting only.

The larger viscosity rapidly reduces the wave’s variation. With the smaller viscosity, the descending side becomes much steeper while the ascending side spreads out. Convection and diffusion act in both calculations; the smaller viscosity allows a thinner transition to develop.

This is the behavior we wanted to observe, but the plot raises a numerical question: is the computed thickness of the front set by physical viscosity, or partly by the grid? A conservative scheme can preserve the mean while smoothing a front too much. The exact-solution comparison verified behavior for that benchmark; it did not certify this new calculation at every viscosity and resolution.

Check the thinner front on finer grids

Keep ν=0.05\nu=0.05, the initial sine function, and the final time t=2t=2 fixed. Compare 256, 512, and 1024 points, recomputing the permitted time step on each grid. Reuse the existing 512-point run.

We will measure the steepest descending slope using a centered difference and compare neighboring-grid solutions at their shared points. These grids are nested: every second point of a fine grid coincides with a point of the next coarser grid. The slice [::2] selects those matching points.

The slope tells us whether refinement changes the front’s sharpness. The maximum difference between grids tells us how much the final profile changes; it is not an error against an exact solution.

The steepest descending slope changes from approximately -3.49 to -3.84 to -4.06 as the grid is refined. For comparison, the initial sine profile has minimum slope -0.75. Thus steepening is clear on all three grids, but the computed front continues to sharpen with refinement.

The maximum difference drops from about 0.0326 between the first two grids to 0.0191 between the next two. This supports decreasing grid sensitivity, but it does not establish an exact error or a fully resolved front thickness. Our defensible conclusion is that the smaller viscosity permits a steep transition and that resolution still affects its computed shape. A quantitative claim about front thickness would need a stated tolerance and further refinement evidence.

Verification with an agent

Does the solver approach the exact solution?

The sine-wave experiment showed that the computed front changes under refinement. Without an exact answer for that initial condition, differences between grids measure grid sensitivity, not error. Return now to the smooth periodic reference, where we can measure the error directly.

Use the conservative solver to ask: does its error decrease at the rate suggested by the discretization? The repeated grid setup and bookkeeping are useful work to delegate. You will supply the numerical question, predict the behavior, inspect the agent’s proposed experiment, and decide what the evidence supports. The agent will draft code to organize the runs and measurements without changing the numerical method.

Step 1 — Predict before delegating

For a smooth solution, backward differencing of the convection flux is first order in space, centered diffusion is second order in space, and forward Euler is first order in time. The expected leading error contributions have the form

e≈C1Δx+C2Δx2+C3Δt,e\approx C_1\Delta x+C_2\Delta x^2+C_3\Delta t,

with coefficients that depend on the solution and comparison time. Our step restriction couples space and time. At fixed positive viscosity, sufficiently fine grids make the diffusion contribution to the restriction dominant, so Δt\Delta t scales like Δx2\Delta x^2. The first-order spatial term should then dominate the error, provided its coefficient is nonzero and the grid is fine enough.

Step 2 — Specify the measurements

For each grid, you will compare the computed final array with the exact reference evaluated at those same positions and at TT:

EN=max⁡i∣uicomputed−u(xi,T)∣,RN=ENE2N,pN=log⁡(RN)log⁡2.E_N=\max_i|u_i^{\mathrm{computed}}-u(x_i,T)|, \qquad R_N=\frac{E_N}{E_{2N}}, \qquad p_N=\frac{\log(R_N)}{\log 2}.

Here pNp_N is the observed order for a pair of grids. This is a combined space–time refinement study; it does not independently measure the temporal order. An error ratio near two gives an observed order near one.

Use these fixed problem data:

  • Reference parameters: L=2πL=2\pi, U=1U=1, ν=0.1\nu=0.1, and a=0.95a=0.95.

  • Unique periodic grid sizes: N=128,256,512,1024N=128,256,512,1024.

  • Final time: T=1T=1; use observation times [0.0, T].

  • Safety factor: 0.8 in the existing combined step restriction.

These are the reference initial data from exact_solution(), not the sine initial condition. Use distinct experiment variable names so that the preceding viscosity study cannot silently supply the wrong parameters.

Retain each grid’s positions, initial array, computed final array, exact final array, and measured error so you can audit a result. Request one table with grid size, spacing, time-step count, actual time step, maximum error, error ratio, and observed order. Put each ratio and order on the coarser row of its grid pair; leave them blank on the final row. The existing runner does not return step counts: the verification code can calculate them from the same stated step-selection formula without modifying the runner.

Step 3 — Write the brief and steer your agent

The staged request gives you a chance to catch a misunderstanding before it becomes code. For example, a proposal to compare against the next finer grid would answer a different question from the exact-error study specified here.

Step 4 — Audit, then execute

Inspect the proposed cells and notebook diff. Check that the agent:

  • uses endpoint=False and dx=L/N, with fresh reference initial data on every grid;

  • calls the existing functions unchanged, passes the correct parameters, and uses [0.0, T];

  • reports the adjusted step that reaches TT, rather than just the target step;

  • compares the final numerical array with the exact solution on the same grid at TT, using the maximum absolute difference;

  • forms coarse-error/fine-error ratios in the correct order and computes pp from those ratios;

  • retains the arrays and changes only the permitted cells.

If a check fails, ask for a correction confined to the added cells and inspect again. Once the code passes review, run it one cell at a time.

A plausible table is not enough if its measurements do not match the saved solutions. Conversely, an observed order away from one is a result to investigate, not permission to ask the agent to alter the calculation until it passes. First check the specification, implementation, and whether the tested grids appear to have reached the predicted refinement regime.

Step 5 — Make the numerical judgment

Note the limits of your conclusions. Conservation is a property of the discrete update, while agreement with a known solution under refinement supplies evidence about accuracy for the tested problem. A thin front at another viscosity still requires its own resolution evidence.