• Svein Linge
• Hans Petter Langtangen
Open Access
Chapter
Part of the Texts in Computational Science and Engineering book series (TCSE, volume 16)

## Abstract

Wave (Chap. 2) and diffusion (Chap. 3) equations are solved reliably by finite difference methods. As soon as we add a first-order derivative in space, representing advective transport (also known as convective transport), the numerics gets more complicated and intuitively attractive methods no longer work well. We shall show how and why such methods fail and provide remedies. The present chapter builds on basic knowledge about finite difference methods for diffusion and wave equations, including the analysis by Fourier components, truncation error analysis (Appendix B), and compact difference notation.

Wave (Chap. ) and diffusion (Chap. ) equations are solved reliably by finite difference methods. As soon as we add a first-order derivative in space, representing advective transport (also known as convective transport), the numerics gets more complicated and intuitively attractive methods no longer work well. We shall show how and why such methods fail and provide remedies. The present chapter builds on basic knowledge about finite difference methods for diffusion and wave equations, including the analysis by Fourier components, truncation error analysis (Appendix B), and compact difference notation.

### Remark on terminology

It is common to refer to movement of a fluid as convection, while advection is the transport of some material dissolved or suspended in the fluid. We shall mostly choose the word advection here, but both terms are in heavy use, and for mass transport of a substance the PDE has an advection term, while the similar term for the heat equation is a convection term.

Much more comprehensive discussion of dispersion analysis for advection problems can be found in the book by Duran . This is a an excellent resource for further studies on the topic of advection PDEs, with emphasis on generalizations to real geophysical problems. The book by Fletcher  also has a good overview of methods for advection and convection problems.

## 4.1 One-Dimensional Time-Dependent Advection Equations

We consider the pure advection model
$$\frac{\partial u}{\partial t}+v\frac{\partial u}{\partial x} =0,\quad x\in(0,L),\ t\in(0,T],$$
(4.1)
$$u(x,0) =I(x),\quad x\in(0,L),$$
(4.2)
$$u(0,t) =U_{0},\quad t\in(0,T].$$
(4.3)
In (4.1), v is a given parameter, typically reflecting the transport velocity of a quantity u with a flow. There is only one boundary condition (4.3) since the spatial derivative is only first order in the PDE (4.1). The information at x = 0 and the initial condition get transported in the positive x direction if v > 0 through the domain.
It is easiest to find the solution of (4.1) if we remove the boundary condition and consider a process on the infinite domain $$(-\infty,\infty)$$. The solution is simply
$$u(x,t)=I(x-vt)\thinspace.$$
(4.4)
This is also the solution we expect locally in a finite domain before boundary conditions have reflected or modified the wave.
A particular feature of the solution (4.4) is that
$$u(x_{i},t_{n+1})=u(x_{i-1},t_{n}),$$
(4.5)
if $$x_{i}=i\Delta x$$ and $$t_{n}=n\Delta t$$ are points in a uniform mesh. We see this relation from
\begin{aligned}\displaystyle u(i\Delta x,(n+1)\Delta t)&\displaystyle=I(i\Delta x-v(n+1)\Delta t)\\ \displaystyle&\displaystyle=I((i-1)\Delta x-vn\Delta t-v\Delta t+\Delta x)\\ \displaystyle&\displaystyle=I((i-1)\Delta x-vn\Delta t)\\ \displaystyle&\displaystyle=u((i-1)\Delta x,n\Delta t),\end{aligned}
provided $$v=\Delta x/\Delta t$$. So, whenever we see a scheme that collapses to
$$u^{n+1}_{i}=u_{i-1}^{n},$$
(4.6)
for the PDE in question, we have in fact a scheme that reproduces the analytical solution, and many of the schemes to be presented possess this nice property!

Finally, we add that a discussion of appropriate boundary conditions for the advection PDE in multiple dimensions is a challenging topic beyond the scope of this text.

### Method

A first attempt to solve a PDE like (4.1) will normally be to look for a time-discretization scheme that is explicit so we avoid solving systems of linear equations. In space, we anticipate that centered differences are most accurate and therefore best. These two arguments lead us to a Forward Euler scheme in time and centered differences in space:
$$[D_{t}^{+}u+vD_{2x}u=0]^{n}_{i}\thinspace.$$
(4.7)
Written out, we see that this expression implies that
$$u^{n+1}=u^{n}-\frac{1}{2}C(u^{n}_{i+1}-u_{i-1}^{n}),$$
with C as the Courant number
$$C=\frac{v\Delta t}{\Delta x}\thinspace.$$

### Implementation

A solver function for our scheme goes as follows.

### Test cases

The typical solution u has the shape of I and is transported at velocity v to the right (if v > 0). Let us consider two different initial conditions, one smooth (Gaussian pulse) and one non-smooth (half-truncated cosine pulse):
$$u(x,0) =Ae^{-\frac{1}{2}\left(\frac{x-L/10}{\sigma}\right)^{2}},$$
(4.8)
$$u(x,0) =A\cos\left(\frac{5\pi}{L}\left(x-\frac{L}{10}\right)\right),\quad x<\frac{L}{5}\hbox{ else }0\thinspace.$$
(4.9)
The parameter A is the maximum value of the initial condition.
Before doing numerical simulations, we scale the PDE problem and introduce $$\bar{x}=x/L$$ and $$\bar{t}=vt/L$$, which gives
$$\frac{\partial\bar{u}}{\partial\bar{t}}+\frac{\partial\bar{u}}{\partial\bar{x}}=0\thinspace.$$
The unknown u is scaled by the maximum value of the initial condition: $$\bar{u}=u/\max|I(x)|$$ such that $$|\bar{u}(\bar{x},0)|\in[0,1]$$. The scaled problem is solved by setting v = 1, L = 1, and A = 1. From now on we drop the bars.

To run our test cases and plot the solution, we make the function

### Bug?

Running either of the test cases, the plot becomes a mess, and the printout of u values in the plot function reveals that u grows very quickly. We may reduce $$\Delta t$$ and make it very small, yet the solution just grows. Such behavior points to a bug in the code. However, choosing a coarse mesh and performing one time step by hand calculations produces the same numbers as the code, so the implementation seems to be correct. The hypothesis is therefore that the solution is unstable.

### 4.1.2 Analysis of the Scheme

It is easy to show that a typical Fourier component
$$u(x,t)=B\sin(k(x-ct))$$
is a solution of our PDE for any spatial wave length $$\lambda=2\pi/k$$ and any amplitude B. (Since the PDE to be investigated by this method is homogeneous and linear, B will always cancel out, so we tend to skip this amplitude, but keep it here in the beginning for completeness.)
A general solution may be viewed as a collection of long and short waves with different amplitudes. Algebraically, the work simplifies if we introduce the complex Fourier component
$$u(x,t)=A_{\mbox{\footnotesize e}}e^{ikx},$$
with
$$A_{\mbox{\footnotesize e}}=Be^{-ikv\Delta t}=Be^{-iCk\Delta x}\thinspace.$$
Note that $$|A_{\mbox{\footnotesize e}}|\leq 1$$.
It turns out that many schemes also allow a Fourier wave component as solution, and we can use the numerically computed values of $$A_{\mbox{\footnotesize e}}$$ (denoted A) to learn about the quality of the scheme. Hence, to analyze the difference scheme we have just implemented, we look at how it treats the Fourier component
$$u_{q}^{n}=A^{n}e^{ikq\Delta x}\thinspace.$$
Inserting the numerical component in the scheme,
$$[D_{t}^{+}Ae^{ikq\Delta x}+vD_{2x}Ae^{ikq\Delta x}=0]^{n}_{q},$$
and making use of (A.25) results in
$$\left[e^{ikq\Delta x}\left(\frac{A-1}{\Delta t}+v\frac{1}{\Delta x}i\sin(k\Delta x)\right)=0\right]^{n}_{q},$$
which implies
$$A=1-iC\sin(k\Delta x)\thinspace.$$

The numerical solution features the formula A n . To find out whether A n means growth in time, we rewrite A in polar form: $$A=A_{r}e^{i\phi}$$, for real numbers A r and ϕ, since we then have $$A^{n}=A_{r}^{n}e^{i\phi n}$$. The magnitude of A n is $$A_{r}^{n}$$. In our case, $$A_{r}=(1+C^{2}\sin^{2}(kx))^{1/2}> 1$$, so $$A_{r}^{n}$$ will increase in time, whereas the exact solution will not. Regardless of $$\Delta t$$, we get unstable numerical solutions.

### Method

Another explicit scheme is to do a ‘‘leapfrog’’ jump over $$2\Delta t$$ in time and combine it with central differences in space:
$$[D_{2t}u+vD_{2x}u=0]_{i}^{n},$$
which results in the updating formula
$$u^{n+1}_{i}=u^{n-1}_{i}-C(u_{i+1}^{n}-u_{i-1}^{n})\thinspace.$$
A special scheme is needed to compute u 1, but we leave that problem for now. Anyway, this special scheme can be found in advec1D.py .

### Implementation

We now need to work with three time levels and must modify our solver a bit:

### Running a test case

Let us try a coarse mesh such that the smooth Gaussian initial condition is represented by 1 at mesh node 1 and 0 at all other nodes. This triangular initial condition should then be advected to the right. Choosing scaled variables as $$\Delta t=0.1$$, T = 1, and C = 1 gives the plot in Fig. 4.1, which is in fact identical to the exact solution (!). Fig. 4.1 Exact solution obtained by Leapfrog scheme with $$\Delta t=0.1$$ and C = 1

### Running more test cases

We can run two types of initial conditions for C = 0.8: one very smooth with a Gaussian function (Fig. 4.2) and one with a discontinuity in the first derivative (Fig. 4.3). Unless we have a very fine mesh, as in the left plots in the figures, we get small ripples behind the main wave, and this main wave has the amplitude reduced. Fig. 4.2 Advection of a Gaussian function with a leapfrog scheme and C = 0.8, $$\Delta t=0.001$$ (left) and $$\Delta t=0.01$$ (right) Fig. 4.3 Advection of half a cosine function with a leapfrog scheme and C = 0.8, $$\Delta t=0.001$$ (left) and $$\Delta t=0.01$$ (right)

Advection of the Gaussian function with a leapfrog scheme, using C = 0.8 and $$\Delta t=0.01$$ can be seen in a movie file 1. Alternatively, with $$\Delta t=0.001$$, we get this movie file 2.

Advection of the cosine hat function with a leapfrog scheme, using C = 0.8 and $$\Delta t=0.01$$ can be seen in a movie file 3. Alternatively, with $$\Delta t=0.001$$, we get this movie file 4.

### Analysis

We can perform a Fourier analysis again. Inserting the numerical Fourier component in the Leapfrog scheme, we get
$$A^{2}-i2C\sin(k\Delta x)A-1=0,$$
and
$$A=-iC\sin(k\Delta x)\pm\sqrt{1-C^{2}\sin^{2}(k\Delta x)}\thinspace.$$
Rewriting to polar form, $$A=A_{r}e^{i\phi}$$, we see that $$A_{r}=1$$, so the numerical component is neither increasing nor decreasing in time, which is exactly what we want. However, for C > 1, the square root can become complex valued, so stability is obtained only as long as C ≤ 1.

### Stability

For all the working schemes to be presented in this chapter, we get the stability condition C ≤ 1:
$$\Delta t\leq\frac{\Delta x}{v}\thinspace.$$
This is called the CFL condition and applies almost always to successful schemes for advection problems. Of course, one can use Crank-Nicolson or Backward Euler schemes for increased and even unconditional stability (no $$\Delta t$$ restrictions), but these have other less desired damping problems.
We introduce $$p=k\Delta x$$. The amplification factor now reads
$$A=-iC\sin p\pm\sqrt{1-C^{2}\sin^{2}p},$$
and is to be compared to the exact amplification factor
$$A_{\mbox{\footnotesize e}}=e^{-ikv\Delta t}=e^{-ikC\Delta x}=e^{-iCp}\thinspace.$$
Section 4.1.9 compares numerical amplification factors of many schemes with the exact expression.

### 4.1.4 Upwind Differences in Space

Since the PDE reflects transport of information along with a flow in positive x direction, when v > 0, it could be natural to go (what is called) upstream and not downstream in the spatial derivative to collect information about the change of the function. That is, we approximate
$$\frac{\partial u}{\partial x}(x_{i},t_{n})\approx[D^{-}_{x}u]^{n}_{i}=\frac{u^{n}_{i}-u^{n}_{i-1}}{\Delta x}\thinspace.$$
This is called an upwind difference (the corresponding difference in the time direction would be called a backward difference, and we could use that name in space too, but upwind is the common name for a difference against the flow in advection problems). This spatial approximation does magic compared to the scheme we had with Forward Euler in time and centered difference in space. With an upwind difference,
$$[D^{+}_{t}u+vD^{-}_{x}u=0]^{n}_{i},$$
(4.10)
written out as
$$u^{n+1}_{i}=u^{n}_{i}-C(u^{n}_{i}-u^{n}_{i-1}),$$
gives a generally popular and robust scheme that is stable if C ≤ 1. As with the Leapfrog scheme, it becomes exact if C = 1, exactly as shown in Fig. 4.1. This is easy to see since C = 1 gives the property (4.6). However, any C < 1 gives a significant reduction in the amplitude of the solution, which is a purely numerical effect, see Fig. 4.4 and 4.5. Experiments show, however, that reducing $$\Delta t$$ or $$\Delta x$$, while keeping C reduces the error. Fig. 4.4 Advection of a Gaussian function with a forward in time, upwind in space scheme and C = 0.8, $$\Delta t=0.01$$ (left) and $$\Delta t=0.001$$ (right) Fig. 4.5 Advection of half a cosine function with a forward in time, upwind in space scheme and C = 0.8, $$\Delta t=0.001$$ (left) and $$\Delta t=0.01$$ (right)

Advection of the Gaussian function with a forward in time, upwind in space scheme, using C = 0.8 and $$\Delta t=0.01$$ can be seen in a movie file 5. Alternatively, with $$\Delta t=0.005$$, we get this movie file 6.

Advection of the cosine hat function with a forward in time, upwind in space scheme, using C = 0.8 and $$\Delta t=0.01$$ can be seen in a movie file 7. Alternatively, with $$\Delta t=0.001$$, we get this movie file 8.

The amplification factor can be computed using the formula (A.23),
$$\frac{A-1}{\Delta t}+\frac{v}{\Delta x}(1-e^{-ik\Delta x})=0,$$
which means
$$A=1-C(1-\cos(p)-i\sin(p))\thinspace.$$
For C < 1 there is, unfortunately, non-physical damping of discrete Fourier components, giving rise to reduced amplitude of $$u^{n}_{i}$$ as in Fig. 4.4 and 4.5. The damping seen in these figures is quite severe. Stability requires C ≤ 1.

### Interpretation of upwind difference as artificial diffusion

One can interpret the upwind difference as extra, artificial diffusion in the equation. Solving
$$\frac{\partial u}{\partial t}+v\frac{\partial u}{\partial x}=\nu\frac{\partial^{2}u}{\partial x^{2}},$$
by a forward difference in time and centered differences in space,
$$D^{+}_{t}u+vD_{2x}u=\nu D_{x}D_{x}u]^{n}_{i},$$
actually gives the upwind scheme (4.10) if $$\nu=v\Delta x/2$$. That is, solving the PDE $$u_{t}+vu_{x}=0$$ by centered differences in space and forward difference in time is unsuccessful, but by adding some artificial diffusion $$\nu u_{xx}$$, the method becomes stable:
$$\frac{\partial u}{\partial t}+v\frac{\partial u}{\partial x}=\left(\alpha+\frac{v\Delta x}{2}\right)\frac{\partial^{2}u}{\partial x^{2}}\thinspace.$$

### 4.1.5 Periodic Boundary Conditions

So far, we have given the value on the left boundary, $$u_{0}^{n}$$, and used the scheme to propagate the solution signal through the domain. Often, we want to follow such signals for long time series, and periodic boundary conditions are then relevant since they enable a signal that leaves the right boundary to immediately enter the left boundary and propagate through the domain again.

The periodic boundary condition is
$$u(0,t)=u(L,t),\quad u_{0}^{n}=u_{N_{x}}^{n}\thinspace.$$
It means that we in the first equation, involving $$u_{0}^{n}$$, insert $$u_{N_{x}}^{n}$$, and that we in the last equation, involving $$u^{n+1}_{N_{x}}$$ insert $$u^{n+1}_{0}$$. Normally, we can do this in the simple way that u_1 is updated as u_1[Nx] at the beginning of a new time level.

In some schemes we may need $$u^{n}_{N_{x}+1}$$ and $$u^{n}_{-1}$$. Periodicity then means that these values are equal to $$u^{n}_{1}$$ and $$u^{n}_{N_{x}-1}$$, respectively. For the upwind scheme, it is sufficient to set u_1=u_1[Nx] at a new time level before computing u. This ensures that u becomes right and at the next time level u at the current time level is correctly updated. For the Leapfrog scheme we must update u and u[Nx] using the scheme:

### Test condition

Analytically, we can show that the integral in space under the $$u(x,t)$$ curve is constant:
\begin{aligned}\displaystyle\int_{0}^{L}\left(\frac{\partial u}{\partial t}+v\frac{\partial u}{\partial x}\right)dx&\displaystyle=0\\ \displaystyle\frac{\partial}{\partial t}\int_{0}^{L}udx&\displaystyle=-\int_{0}^{L}v\frac{\partial u}{\partial x}dx\\ \displaystyle\frac{\partial u}{\partial t}\int_{0}^{L}udx&\displaystyle=[vu]_{0}^{L}=0\end{aligned}
as long as $$u(0)=u(L)=0$$. We can therefore use the property
$$\int_{0}^{L}u(x,t)dx=\hbox{const}$$
as a partial verification during the simulation. Now, any numerical method with C ≠ 1 will deviate from the constant, expected value, so the integral is a measure of the error in the scheme. The integral can be computed by the Trapezoidal integration rule

if u is an array holding the solution.

### Solving a specific problem

We need to call up the solver function in some kind of administering problem solving function that can solve specific problems and make appropriate visualization. The function below makes both static plots, screen animation, and hard copy videos in various formats.

The complete code is found in the file advec1D.py .

### 4.1.7 A Crank-Nicolson Discretization in Time and Centered Differences in Space

Another obvious candidate for time discretization is the Crank-Nicolson method combined with centered differences in space:
$$[D_{t}u]^{n}_{i}+v\frac{1}{2}([D_{2x}u]^{n+1}_{i}+[D_{2x}u]^{n}_{i})=0\thinspace.$$
It can be nice to include the Backward Euler scheme too, via the θ-rule,
$$[D_{t}u]^{n}_{i}+v\theta[D_{2x}u]^{n+1}_{i}+v(1-\theta)[D_{2x}u]^{n}_{i}=0\thinspace.$$
When θ is different from zero, this gives rise to an implicit scheme,
$$u^{n+1}_{i}+\frac{\theta}{2}C(u^{n+1}_{i+1}-u^{n+1}_{i-1})=u^{n}_{i}-\frac{1-\theta}{2}C(u^{n}_{i+1}-u^{n}_{i-1})$$
for $$i=1,\ldots,N_{x}-1$$. At the boundaries we set u = 0 and simulate just to the point of time when the signal hits the boundary (and gets reflected).
$$u^{n+1}_{0}=u^{n+1}_{N_{x}}=0\thinspace.$$
The elements on the diagonal in the matrix become:
$$A_{i,i}=1,\quad i=0,\ldots,N_{x}\thinspace.$$
On the subdiagonal and superdiagonal we have
$$A_{i-1,i}=-\frac{\theta}{2}C,\quad A_{i+1,i}=\frac{\theta}{2}C,\quad i=1,\ldots,N_{x}-1,$$
with $$A_{0,1}=0$$ and $$A_{N_{x}-1,N_{x}}=0$$ due to the known boundary conditions. And finally, the right-hand side becomes
\begin{aligned}\displaystyle b_{0}&\displaystyle=u^{n}_{N_{x}}\\ \displaystyle b_{i}&\displaystyle=u^{n}_{i}-\frac{1-\theta}{2}C(u^{n}_{i+1}-u^{n}_{i-1}),\quad i=1,\ldots,N_{x}-1\\ \displaystyle b_{N_{x}}&\displaystyle=u^{n}_{0}\thinspace.\end{aligned}
The dispersion relation follows from inserting $$u^{n}_{q}=A^{n}e^{ikx}$$ and using the formula (A.25) for the spatial differences:
$$A=\frac{1-(1-\theta)iC\sin p}{1+\theta iC\sin p}\thinspace.$$

### Movie 1

Crank-Nicolson in time, centered in space, C = 0.8, $$\Delta t=0.005$$. https://raw.githubusercontent.com/hplgit/fdm-book/master/doc/pub/book/html/mov-advec/gaussian/CN/C08_dt0005/movie.ogg

### Movie 2

Backward-Euler in time, centered in space, C = 0.8, $$\Delta t=0.005$$. https://raw.githubusercontent.com/hplgit/fdm-book/master/doc/pub/book/html/mov-advec/cosinehat/BE/C_08_dt005.ogg

Figure 4.6 depicts a numerical solution for C = 0.8 and the Crank-Nicolson with severe oscillations behind the main wave. These oscillations are damped as the mesh is refined. Switching to the Backward Euler scheme removes the oscillations, but the amplitude is significantly reduced. One could expect that the discontinuous derivative in the initial condition of the half a cosine wave would make even stronger demands on producing a smooth profile, but Fig. 4.7 shows that also here, Backward-Euler is capable of producing a smooth profile. All in all, there are no major differences between the Gaussian initial condition and the half a cosine condition for any of the schemes. Fig. 4.6 Crank-Nicolson in time, centered in space, Gaussian profile, C = 0.8, $$\Delta t=0.01$$ (left) and $$\Delta t=0.005$$ (right) Fig. 4.7 Backward-Euler in time, centered in space, half a cosine profile, C = 0.8, $$\Delta t=0.01$$ (left) and $$\Delta t=0.005$$ (right)

### 4.1.8 The Lax-Wendroff Method

The Lax-Wendroff method is based on three ideas:
1. 1.

Express the new unknown $$u^{n+1}_{i}$$ in terms of known quantities at $$t=t_{n}$$ by means of a Taylor polynomial of second degree.

2. 2.

Replace time-derivatives at $$t=t_{n}$$ by spatial derivatives, using the PDE.

3. 3.

Discretize the spatial derivatives by second-order differences so we achieve a scheme of accuracy $$\mathcal{O}(\Delta t^{2})+\mathcal{O}(\Delta x^{2})$$.

Let us follow the recipe. First we have the three-term Taylor polynomial,
$$u^{n+1}_{i}=u^{n}_{i}+\Delta t\left(\frac{\partial u}{\partial t}\right)^{n}_{i}+\frac{1}{2}\Delta t^{2}\left(\frac{\partial^{2}u}{\partial t^{2}}\right)^{n}_{i}\thinspace.$$
From the PDE we have that temporal derivatives can be substituted by spatial derivatives:
$$\frac{\partial u}{\partial t}=-v\frac{\partial u}{\partial x},$$
and furthermore,
$$\frac{\partial^{2}u}{\partial t^{2}}=v^{2}\frac{\partial^{2}u}{\partial x^{2}}\thinspace.$$
Inserted in the Taylor polynomial formula, we get
$$u^{n+1}_{i}=u^{n}_{i}-v\Delta t\left(\frac{\partial u}{\partial x}\right)^{n}_{i}+\frac{1}{2}\Delta t^{2}v^{2}\left(\frac{\partial^{2}u}{\partial x^{2}}\right)^{n}_{i}\thinspace.$$
To obtain second-order accuracy in space we now use central differences:
$$u^{n+1}_{i}=u^{n}_{i}-v\Delta t[D_{2x}u]^{n}_{i}+\frac{1}{2}\Delta t^{2}v^{2}[D_{x}D_{x}u]^{n}_{i},$$
or written out,
$$u^{n+1}_{i}=u^{n}_{i}-\frac{1}{2}C(u^{n}_{i+1}-u^{n}_{i-1})+\frac{1}{2}C^{2}(u^{n}_{i+1}-2u^{n}_{i}+u^{n}_{i-1})\thinspace.$$
This is the explicit Lax-Wendroff scheme.

### Lax-Wendroff works because of artificial viscosity

From the formulas above, we notice that the Lax-Wendroff method is nothing but a Forward Euler, central difference in space scheme, which we have shown to be useless because of chronic instability, plus an artificial diffusion term of strength $$\frac{1}{2}\Delta tv^{2}$$. It means that we can take an unstable scheme and add some diffusion to stabilize it. This is a common trick to deal with advection problems. Sometimes, the real physical diffusion is not sufficiently large to make schemes stable, so then we also add artificial diffusion.

From an analysis similar to the ones carried out above, we get an amplification factor for the Lax-Wendroff method that equals
$$A=1-iC\sin p-2C^{2}\sin^{2}(p/2)\thinspace.$$
This means that $$|A|=1$$ and also that we have an exact solution if C = 1!

### 4.1.9 Analysis of Dispersion Relations

We have developed expressions for $$A(C,p)$$ in the exact solution $$u_{q}^{n}=A^{n}e^{ikq\Delta x}$$ of the discrete equations. Note that the Fourier component that solves the original PDE problem has no damping and moves with constant velocity v. There are two basic errors in the numerical Fourier component: there may be damping and the wave velocity may depend on C and $$p=k\Delta x$$.

The shortest wavelength that can be represented is $$\lambda=2\Delta x$$. The corresponding k is $$k=2\pi/\lambda=\pi/\Delta x$$, so $$p=k\Delta x\in(0,\pi]$$.

Given a complex A as a function of C and p, how can we visualize it? The two key ingredients in A is the magnitude, reflecting damping or growth of the wave, and the angle, closely related to the velocity of the wave. The Fourier component
$$D^{n}e^{ik(x-ct)}$$
has damping D and wave velocity c. Let us express our A in polar form, $$A=A_{r}e^{-i\phi}$$, and insert this expression in our discrete component $$u_{q}^{n}=A^{n}e^{ikq\Delta x}=A^{n}e^{ikx}$$:
$$u^{n}_{q}=A_{r}^{n}e^{-i\phi n}e^{ikx}=A_{r}^{n}e^{i(kx-n\phi)}=A_{r}^{n}e^{i(k(x-ct))},$$
for
$$c=\frac{\phi}{k\Delta t}\thinspace.$$
Now,
$$k\Delta t=\frac{Ck\Delta x}{v}=\frac{Cp}{v},$$
so
$$c=\frac{\phi v}{Cp}\thinspace.$$
An appropriate dimensionless quantity to plot is the scaled wave velocity c ∕ v:
$$\frac{c}{v}=\frac{\phi}{Cp}\thinspace.$$

Label

Method

FE

Forward Euler in time, centered difference in space

LF

Leapfrog in time, centered difference in space

UP

Forward Euler in time, upwind difference in space

CN

Crank-Nicolson in time, centered difference in space

LW

Lax-Wendroff’s method

BE

Backward Euler in time, centered difference in space

The total damping after some time $$T=n\Delta t$$ is reflected by $$A_{r}(C,p)^{n}$$. Since normally $$A_{r}<1$$, the damping goes like $$A_{r}^{1/\Delta t}$$ and approaches zero as $$\Delta t\rightarrow 0$$. The only way to reduce damping is to increase C and/or the mesh resolution.

We can learn a lot from the dispersion relation plots. For example, looking at the plots for C = 1, the schemes LW, UP, and LF has no amplitude reduction, but LF has wrong phase velocity for the shortest wave in the mesh. This wave does not (normally) have enough amplitude to be seen, so for all practical purposes, there is no damping or wrong velocity of the individual waves, so the total shape of the wave is also correct. For the CN scheme, see Fig. 4.6, each individual wave has its amplitude, but they move with different velocities, so after a while, we see some of these waves lagging behind. For the BE scheme, see Fig. 4.7, all the shorter waves are so heavily dampened that we cannot see them after a while. We see only the longest waves, which have slightly wrong velocity, but visible amplitudes are sufficiently equal to produce what looks like a smooth profile.

Another feature was that the Leapfrog method produced oscillations, while the upwind scheme did not. Since the Leapfrog method does not dampen the shorter waves, which have wrong wave velocities of order 10 percent, we can see these waves as noise. The upwind scheme, however, dampens these waves. The same effect is also present in the Lax-Wendroff scheme, but the damping of the intermediate waves is hardly present, so there is visible noise in the total signal.

We realize that, compared to pure truncation error analysis, dispersion analysis sheds more light on the behavior of the computational schemes. Truncation analysis just says that Lax-Wendroff is better than upwind, because of the increased order in time, but most people would say upwind is the better one when looking at the plots.

## 4.2 One-Dimensional Stationary Advection-Diffusion Equation

Now we pay attention to a physical process where advection (or convection) is in balance with diffusion:
$$v\frac{du}{dx}=\alpha\frac{d^{2}u}{dx^{2}}\thinspace.$$
(4.11)
For simplicity, we assume v and α to be constant, but the extension to the variable-coefficient case is trivial. This equation can be viewed as the stationary limit of the corresponding time-dependent problem
$$\frac{\partial u}{\partial t}+v\frac{\partial u}{\partial x}=\alpha\frac{\partial^{2}u}{\partial x^{2}}\thinspace.$$
(4.12)

Equations of the form (4.11) or (4.12) arise from transport phenomena, either mass or heat transport. One can also view the equations as a simple model problem for the Navier-Stokes equations. With the chosen boundary conditions, the differential equation problem models the phenomenon of a boundary layer, where the solution changes rapidly very close to the boundary. This is a characteristic of many fluid flow problems, which makes strong demands to numerical methods. The fundamental numerical difficulty is related to non-physical oscillations of the solution (instability) if the first-derivative spatial term dominates over the second-derivative term.

### 4.2.1 A Simple Model Problem

We consider (4.11) on $$[0,L]$$ equipped with the boundary conditions $$u(0)=U_{0}$$, $$u(L)=U_{L}$$. By scaling we can reduce the number of parameters in the problem. We scale x by $$\bar{x}=x/L$$, and u by
$$\bar{u}=\frac{u-U_{0}}{U_{L}-U_{0}}\thinspace.$$
Inserted in the governing equation we get
$$\frac{v(U_{L}-U_{0})}{L}\frac{d\bar{u}}{d\bar{x}}=\frac{\alpha(U_{L}-U_{0})}{L^{2}}\frac{d^{2}\bar{u}}{d\bar{x}^{2}},\quad\bar{u}(0)=0,\ \bar{u}(1)=1\thinspace.$$
Dropping the bars is common. We can then simplify to
$$\frac{du}{dx}=\epsilon\frac{d^{2}u}{dx^{2}},\quad u(0)=0,\ u(1)=1\thinspace.$$
(4.13)

There are two competing effects in this equation: the advection term transports signals to the right, while the diffusion term transports signals to the left and the right. The value $$u(0)=0$$ is transported through the domain if ϵ is small, and u ≈ 0 except in the vicinity of x = 1, where $$u(1)=1$$ and the diffusion transports some information about $$u(1)=1$$ to the left. For large ϵ, diffusion dominates and the u takes on the ‘‘average’’ value, i.e., u gets a linear variation from 0 to 1 throughout the domain.

It turns out that we can find an exact solution to the differential equation problem and also to many of its discretizations. This is one reason why this model problem has been so successful in designing and investigating numerical methods for mixed convection/advection and diffusion. The exact solution reads
$$u_{\mbox{\footnotesize e}}(x)=\frac{e^{x/\epsilon}-1}{e^{1/\epsilon}-1}\thinspace.$$
The forthcoming plots illustrate this function for various values of ϵ.

### 4.2.2 A Centered Finite Difference Scheme

The most obvious idea to solve (4.13) is to apply centered differences:
$$[D_{2x}u=\epsilon D_{x}D_{x}u]_{i}$$
for $$i=1,\ldots,N_{x}-1$$, with $$u_{0}=0$$ and $$u_{N_{x}}=1$$. Note that this is a coupled system of algebraic equations involving $$u_{0},\ldots,u_{N_{x}}$$.
Written out, the scheme becomes a tridiagonal system
$$A_{i-1,i}u_{i-1}+A_{i,i}u_{i}+A_{i+1,i}u_{i+1}=0,$$
for $$i=1,\ldots,N_{x}-1$$
\begin{aligned}\displaystyle A_{0,0}&\displaystyle=1,\\ \displaystyle A_{i-1,i}&\displaystyle=-\frac{1}{\Delta x}-\epsilon\frac{1}{\Delta x^{2}},\\ \displaystyle A_{i,i}&\displaystyle=2\epsilon\frac{1}{\Delta x^{2}},\\ \displaystyle A_{i,i+1}&\displaystyle=\frac{1}{\Delta x}-\epsilon\frac{1}{\Delta x^{2}},\\ \displaystyle A_{N_{x},N_{x}}&\displaystyle=1\thinspace.\end{aligned}
The right-hand side of the linear system is zero except $$b_{N_{x}}=1$$.
Figure 4.14 shows reasonably accurate results with $$N_{x}=20$$ and $$N_{x}=40$$ cells in x direction and a value of ϵ = 0.1. Decreasing ϵ to 0.01 leads to oscillatory solutions as depicted in Fig. 4.15. This is, unfortunately, a typical phenomenon in this type of problem: non-physical oscillations arise for small ϵ unless the resolution N x is big enough. Exercise 4.1 develops a precise criterion: u is oscillation-free if
$$\Delta x\leq\frac{2}{\epsilon}\thinspace.$$
If we take the present model as a simplified model for a viscous boundary layer in real, industrial fluid flow applications, $$\epsilon\sim 10^{-6}$$ and millions of cells are required to resolve the boundary layer. Fortunately, this is not strictly necessary as we have methods in the next section to overcome the problem! Fig. 4.14 Comparison of exact and numerical solution for ϵ = 0.1 and $$N_{x}=20,40$$ with centered differences Fig. 4.15 Comparison of exact and numerical solution for ϵ = 0.01 and $$N_{x}=20,40$$ with centered differences

### Solver

A suitable solver for doing the experiments is presented below.

### 4.2.3 Remedy: Upwind Finite Difference Scheme

The scheme can be stabilized by letting the advective transport term, which is the dominating term, collect its information in the flow direction, i.e., upstream or upwind of the point in question. So, instead of using a centered difference
$$\frac{du}{dx}_{i}\approx\frac{u_{i+1}-u_{i-1}}{2\Delta x},$$
we use the one-sided upwind difference
$$\frac{du}{dx}_{i}\approx\frac{u_{i}-u_{i-1}}{\Delta x},$$
in case v > 0. For v < 0 we set
$$\frac{du}{dx}_{i}\approx\frac{u_{i+1}-u_{i}}{\Delta x},$$
On compact operator notation form, our upwind scheme can be expressed as
$$[D^{-}_{x}u=\epsilon D_{x}D_{x}u]_{i}$$
provided v > 0 (and ϵ > 0).
We write out the equations and implement them as shown in the program in Sect. 4.2.2. The results appear in Fig. 4.16 and 4.17: no more oscillations! Fig. 4.16 Comparison of exact and numerical solution for ϵ = 0.1 and $$N_{x}=20,40$$ with upwind difference Fig. 4.17 Comparison of exact and numerical solution for ϵ = 0.01 and $$N_{x}=20,40$$ with upwind difference

We see that the upwind scheme is always stable, but it gives a thicker boundary layer when the centered scheme is also stable. Why the upwind scheme is always stable is easy to understand as soon as we undertake the mathematical analysis in Exercise 4.1. Moreover, the thicker layer (seemingly larger diffusion) can be understood by doing Exercise 4.2.

### Exact solution for this model problem

It turns out that one can introduce a linear combination of the centered and upwind differences for the first-derivative term in this model problem. One can then adjust the weight in the linear combination so that the numerical solution becomes identical to the analytical solution of the differential equation problem at any mesh point.

## 4.3 Time-dependent Convection-Diffusion Equations

Now it is time to combine time-dependency, convection (advection) and diffusion into one equation:
$$\frac{\partial u}{\partial t}+v\frac{\partial u}{\partial x}=\alpha\frac{\partial^{2}u}{\partial x^{2}}\thinspace.$$
(4.14)

### Analytical insight

The diffusion is now dominated by convection, a wave, and diffusion, a loss of amplitude. One possible analytical solution is a traveling Gaussian function
$$u(x,t)=B\exp{\left(-\left(\frac{x-vt}{4at}\right)\right)}\thinspace.$$
This function moves with velocity v > 0 to the right (v < 0 to the left) due to convection, but at the same time we have a damping $$e^{-16a^{2}t^{2}}$$ from diffusion.

### 4.3.1 Forward in Time, Centered in Space Scheme

The Forward Euler for the diffusion equation is a successful scheme, but it has a very strict stability condition. The similar Forward in time, centered in space strategy always gives unstable solutions for the advection PDE. What happens when we have both diffusion and advection present at once?
$$[D_{t}u+vD_{2x}u=\alpha D_{x}D_{x}u+f]_{i}^{n}\thinspace.$$
We expect that diffusion will stabilize the scheme, but that advection will destabilize it.

Another problem is non-physical oscillations, but not growing amplitudes, due to centered differences in the advection term. There will hence be two types of instabilities to consider. Our analysis showed that pure advection with centered differences in space needs some artificial diffusion to become stable (and then it produces upwind differences for the advection term). Adding more physical diffusion should further help the numerics to stabilize the non-physical oscillations.

The scheme is quickly implemented, but suffers from the need for small space and time steps, according to this reasoning. A better approach is to get rid of the non-physical oscillations in space by simply applying an upwind difference on the advection term.

### 4.3.2 Forward in Time, Upwind in Space Scheme

A good approximation for the pure advection equation is to use upwind discretization of the advection term. We also know that centered differences are good for the diffusion term, so let us combine these two discretizations:
$$[D_{t}u+vD^{-}_{x}u=\alpha D_{x}D_{x}u+f]_{i}^{n},$$
(4.15)
for v > 0. Use $$vD^{+}u$$ if v < 0. In this case the physical diffusion and the extra numerical diffusion $$v\Delta x/2$$ will stabilize the solution, but give an overall too large reduction in amplitude compared with the exact solution.
We may also interpret the upwind difference as artificial numerical diffusion and centered differences in space everywhere, so the scheme can be expressed as
$$\left[D_{t}u+vD^{-}_{2x}u=\alpha\frac{v\Delta x}{2}D_{x}D_{x}u+f\right]_{i}^{n}\thinspace.$$
(4.16)

## 4.4 Applications of Advection Equations

There are two major areas where advection and convection applications arise: transport of a substance and heat transport in a fluid. To derive the models, we may look at the similar derivations of diffusion models in Sect. , but change the assumption from a solid to fluid medium. This gives rise to the extra advection or convection term $$\boldsymbol{v}\cdot\nabla u$$. We briefly show how this is done.

Normally, transport in a fluid is dominated by the fluid flow and not diffusion, so we can neglect diffusion compared to advection or convection. The end result is anyway an equation of the form
$$\frac{\partial u}{\partial t}+\boldsymbol{v}\cdot\nabla u=0\thinspace.$$

### 4.4.1 Transport of a Substance

The diffusion of a substance in Sect.  takes place in a solid medium, but in a fluid we can have two transport mechanisms: one by diffusion and one by advection. The latter arises from the fact that the substance particles are moved with the fluid velocity v such that the effective flux now consists of two and not only one component as in ():
$$\boldsymbol{q}=-\alpha\nabla c+\boldsymbol{v}\boldsymbol{c}\thinspace.$$
Inserted in the equation $$\nabla\cdot\boldsymbol{q}=0$$ we get the extra advection term $$\nabla\cdot(\boldsymbol{v}\boldsymbol{c})$$. Very often we deal with incompressible flows, $$\nabla\cdot\boldsymbol{v}=0$$ such that the advective term becomes $$\boldsymbol{v}\cdot\nabla c$$. The mass transport equation for a substance then reads
$$\frac{\partial c}{\partial t}+\boldsymbol{v}\cdot\nabla c=\alpha\nabla^{2}c\thinspace.$$
(4.17)

### 4.4.2 Transport of Heat in Fluids

The derivation of the heat equation in Sect.  is limited to heat transport in solid bodies. If we turn the attention to heat transport in fluids, we get a material derivative of the internal energy in (),
$$\frac{De}{dt}=-\nabla\cdot\boldsymbol{q},$$
and more terms if work by stresses is also included, where
$$\frac{De}{dt}=\frac{\partial e}{\partial t}+\boldsymbol{v}\cdot\nabla e,$$
v being the velocity of the fluid. The convective term $$\boldsymbol{v}\cdot\nabla e$$ must therefore be added to the governing equation, resulting typically in
$$\varrho c\left(\frac{\partial T}{\partial t}+\boldsymbol{v}\cdot\nabla T\right)=\nabla\cdot(k\nabla T)+f,$$
(4.18)
where f is some external heating inside the medium.

## 4.5 Exercises

### Exercise 4.1 (Analyze 1D stationary convection-diffusion problem)

Explain the observations in the numerical experiments from Sect. 4.2.2 and 4.2.3 by finding exact numerical solutions.

### Hint

The difference equations allow solutions on the form A i , where A is an unknown constant and i is a mesh point counter. There are two solutions for A, so the general solution is a linear combination of the two, where the constants in the linear combination are determined from the boundary conditions.

Filename: twopt_BVP_analysis1.

### Exercise 4.2 (Interpret upwind difference as artificial diffusion)

Consider an upwind, one-sided difference approximation to a term du ∕ dx in a differential equation. Show that this formula can be expressed as a centered difference plus an artificial diffusion term of strength proportional to $$\Delta x$$. This means that introducing an upwind difference also means introducing extra diffusion of order $$\mathcal{O}(\Delta x)$$.

Filename: twopt_BVP_analysis2.