Introduction¶
Partial Differential Equations (PDEs) are fundamental tools in the mathematical modeling of various physical phenomena. Unlike Ordinary Differential Equations (ODEs), which involve functions of a single variable and their derivatives, PDEs involve functions of multiple variables and their partial derivatives. This distinction makes PDEs particularly powerful in describing systems where changes occur in more than one dimension, such as in space and time.
A PDE is an equation that relates the partial derivatives of a multivariable function. In general form, a PDE can be written as:
where is the unknown function, and denotes the partial derivatives of with respect to the variables .
PDEs are essential in modeling continuous systems where the state of the system depends on multiple variables. They appear in various fields such as physics, engineering, finance, and biology, describing phenomena like heat conduction, wave propagation, fluid dynamics, and quantum mechanics.
Definition and Significance in Modeling Continuous Systems¶
PDEs provide a framework for formulating problems involving functions of several variables and their rates of change. They are indispensable in describing the behavior of physical systems where spatial and temporal variations are intrinsic. Examples include:
Advection Equation: Models a quantity, e.g., density, moves with velocity :
Heat Equation: Models the distribution of heat (or temperature) in a given region over time.
Wave Equation: Describes the propagation of waves, such as sound or electromagnetic waves, through a medium.
Laplace’s Equation: Represents steady-state solutions where the system does not change over time, such as electric potential in a region devoid of charge.
The ability to model such diverse phenomena underscores the versatility and importance of PDEs in scientific and engineering disciplines.
Finite Difference Methods (FDM)¶
Solving PDEs numerically is essential for modeling complex physical systems that lack closed-form analytical solutions. Among the various numerical methods available, Finite Difference Methods (FDM) are particularly popular due to their simplicity and ease of implementation. They approximate the derivatives in PDEs by using difference on a discretized grid. This approach transforms continuous PDEs into discrete algebraic equations that can be solved iteratively. FDM is widely used in engineering and scientific computations due to its straightforward application to regular grids and its compatibility with existing numerical solvers.
Forward Time Centered Space¶
The Forward Time Centered Space (FTCS) scheme is one of the simplest explicit finite difference methods used to solve time-dependent PDEs. It approximates the time derivative using a forward difference and the spatial derivatives using centered differences.
Consider the linear advection equation that models the transport of a quantity with constant speed :
Let denote the numerical approximation of at spatial index and time level . The FTCS scheme discretizes the advection equation as follows:
Solving for :
This explicit update rule allows the computation of the solution at the next time step based on the current and neighboring spatial points.
Here is a simple python implementation:
# Parameters
c = 1.0 # advection speed
l = 1.0 # domain size
dt = 0.005 # time step
nx = 100 # number of spatial points
nt = 200 # number of time stepsimport numpy as np
X, dx = np.linspace(0, l, nx, endpoint=False, retstep=True) # spatial grid
U0 = np.sin(2*np.pi * X) # initial condition# Forward Time Centered Space (FTCS) scheme
def FTCS(c, U0, dx, dt, n):
sigma = c * dt / dx
U = [U0]
for _ in range(n):
U0 = U[-1]
U1 = U0 - sigma * (np.roll(U0,-1) - np.roll(U0,1)) / 2
U.append(U1)
return np.array(U)U = np.sin(2*np.pi * (X - c*dt*nt)) # analytical solution
UFTCS = FTCS(c, U0, dx, dt, nt) # numerical solutionLet’s now plot the result! After , the solution should match the initial condition exactly.
from matplotlib import pyplot as plt
plt.plot(X, U0, label='Initial Condition')
plt.plot(X, U, ':', label='Exact Solution')
plt.plot(X, UFTCS[-1], '.-', label='FTCS Scheme')
plt.xlabel('x')
plt.ylabel('u')
plt.legend()However, the numerical solution is oscillating. This looks like a numerical artifact. Let’s inspect it with a movie.
from matplotlib.animation import ArtistAnimation
from IPython.display import HTML
from tqdm import tqdm
def animate(X, U):
fig, ax = plt.subplots(1,1)
ax.set_xlabel('x')
ax.set_ylabel('y')
frames = []
for n in tqdm(range(len(U))):
f = ax.plot(X, U[n], 'C0.-', animated=True)
frames.append(f)
plt.close()
return ArtistAnimation(fig, frames, interval=50)anim = animate(X, UFTCS)
HTML(anim.to_html5_video()) # display animation
# anim.save('FTCS.mp4') # save animationThe oscillation grows slowly as the solution evolve. This looks like numerical instability that we discussed in ODE integrator.
# HANDSON: change `c`, `dt`, `dx`, or other parameters to observe how
# the numerical solution changes.
# HANDSON: change the initial condition to, e.g., a boxcar function,
# and observe how the numerical solution changes
To understand the oscillation, we will perform a stability analysis.
Von Neumann Stability Analysis¶
Von Neumann stability analysis is a mathematical technique used to assess the stability of finite difference schemes applied to (linear) PDEs. This method involves decomposing the numerical solution into Fourier modes and analyzing the growth or decay of these modes over time. If all Fourier modes remain bounded, the numerical scheme is considered stable. Otherwise, the scheme is unstable and may produce erroneous results.
To perform von Neumann stability analysis, we assume a solution of the form:
where:
is the numerical approximation of at spatial index and time level .
is the amplification factor (recall our ODE stability analysis).
is the wave number.
is the spatial position of the -th grid point.
The goal is to determine whether the magnitude of the amplification factor remains less than or equal to 1 for all possible wave numbers . If for all , the numerical scheme is stable.
Now, consider the FTCS scheme applied to the linear advection equation
where the FTCS update rule reads
Assumed a single Fourier mode solution,
Substituting it into the FTCS update rule,
Simplify using , we have
Using Euler’s formula :
where is the Courant number.
Calculating the magnitude of , we obtain:
Since for any , the FTCS scheme is unconditionally unstable for the linear advection equation.
To overcome this limitation, we will introduce two more robust finite difference methods: the Upwind Scheme and the Lax-Wendroff Scheme. These methods enhance stability and accuracy, making them more suitable for solving advection-dominated problems.
Upwind Scheme¶
The Upwind Scheme is a finite difference method specifically designed to handle advection-dominated problems more effectively than symmetric schemes like FTCS. By incorporating the direction of wave propagation into the discretization of spatial derivatives, the upwind method enhances numerical stability and reduces non-physical oscillations.
In advection processes, information propagates in a specific direction determined by the flow velocity . The upwind scheme leverages this directional information to bias the spatial derivative approximation, ensuring that the numerical flux aligns with the physical transport direction. This directional bias significantly improves the stability of the numerical solution, especially when dealing with sharp gradients or discontinuities.
The upwind scheme discretizes the spatial derivative based on the sign of the advection speed :
For (flow to the right):
For (flow to the left):
Assuming for this implementation, the upwind scheme update rule becomes:
where:
is the numerical approximation of at spatial index and time level ,
and are the time and spatial step sizes, respectively.
The upwind scheme is actually simplier than the FTCS scheme. The following Python code implements it to solve the linear advection equation.
# Parameters
c = 1.0 # advection speed
l = 1.0 # domain size
dt = 0.005 # time step
nx = 100 # number of spatial points
nt = 200 # number of time stepsX, dx = np.linspace(0, l, nx, endpoint=False, retstep=True) # spatial grid
U0 = np.sin(2*np.pi * X) # initial condition# Upwind scheme
def upwind(c, U0, dx, dt, n):
sigma = c * dt / dx
U = [U0]
for _ in range(n):
U0 = U[-1]
U1 = U0 - sigma * (U0 - np.roll(U0,1))
U.append(U1)
return np.array(U)U = np.sin(2*np.pi * (X - c*dt*nt)) # analytical solution
Uupwind = upwind(c, U0, dx, dt, nt) # numerical solutionplt.plot(X, U0, label='Initial Condition')
plt.plot(X, U, ':', label='Exact Solution')
plt.plot(X, Uupwind[-1], '.-', label='Upwind Scheme')
plt.xlabel('x')
plt.ylabel('u')
plt.legend()anim = animate(X, Uupwind)
HTML(anim.to_html5_video()) # display animation
# anim.save('FTCS.mp4') # save animationAlthough the amplitude decrease as time evolve, the upwind scheme is at least stable!
# HANDSON: change `c`, `dt`, `dx`, or other parameters to observe how
# the numerical solution changes.
# HANDSON: change the initial condition to, e.g., a boxcar function,
# and observe how the numerical solution changes
Von Neumann Stability Analysis¶
Assuming again the numerical solution can be expressed as , the upwind update equation becomes
where is again the Courant number.
Divide both sides by ,
Using Euler’s formula and separate the real and imaginary parts
Kdx = np.linspace(-np.pi, np.pi, 65)
def absG(sigma):
return np.sqrt(1 - 2 * sigma + 2 * sigma*sigma + 2*(1-sigma)*sigma*np.cos(Kdx))
for sigma in [0.5, 1, 1.5]:
plt.plot(Kdx, absG(sigma), label=r"$\sigma={}$".format(sigma))
plt.xlabel(r"$k\Delta x$")
plt.ylabel(r"|G|")
plt.legend()Therefore, the upwind scheme is stable as provided that the Courant number satisfies:
# HANDSON: given what we know about stability now, change `c`, `dt`,
# `dx`, or other parameters to observe how the numerical
# solution changes.
Modified Equation Method¶
We observed the amplitude of the wave decreases when using the upwind scheme. This decay in amplitude is resemble of viscosity: in physical systems, viscosity acts to dissipate energy and smooth out oscillations. Similarly, many stable numerical schemes effectively remove “energy” from the discrete system in order to maintain stability. This is not surprising that the upwind method damps out .
But how exactly does this damping arise from the discretization? To answer this, we turn to another powerful tool called the Modified Equation Method.
Whereas von Neumann stability analysis focuses on whether solutions grow or decay in time, modified equation analysis asks a different question:
What continuous differential equation is the numerical method really solving, up to leading-order truncation error?
By expanding the discrete update scheme in a Taylor series, we can write down an equivalent modified PDE. This PDE makes the numerical errors explicit, showing whether the scheme introduces artificial diffusion (numerical viscosity) or artificial dispersion (phase errors).
Modified Equation of Upwind Advection¶
Express the Upwind Scheme Using Taylor Series Expansions
Start with the upwind update equation:
in addition to the grid point that we want to update, , there are two additional grid points. They are and .
Expand and around the point using Taylor series:
Using the shorthands and , the above series can be written as
Substitute the Taylor Expansions into the Upwind Scheme
Substitute the expansions into the upwind update equation:
Because the derivatives are exact (instead of numerical), we can drop the functions’ evaluations at the dicrete points and . Simplify the equation, we have:
Rearrange and Substitute the Original PDE
Taking the spatial and temporal derivatives of the original advection equation , we have
Combine, we obtain the two-way wave equation:
Substituting the wave equation into the modified equation, we obtain:
Combine Like Terms
Put back the definition of and rearrange,
Define
be the numerical diffusion coefficient, the above equation reduces to
From the modified equation analysis, we found that the Upwind scheme introduces an additional term of the form
where depends on both the grid spacing and the chosen time step.
This term looks exactly like a viscous diffusion term, even though the original advection equation contains no viscosity at all. Hence the interpretation: the upwind scheme introduces “numerical viscosity/diffusion” to suppress instabilities.
Why numerical viscosity/diffusion matters?
Stabilizing Effect: Artificial viscosity is not necessarily a flaw. It is precisely what makes the Upwind Scheme stable, by damping out non-physical oscillations that can arise from discretization errors.
Trade-off with Accuracy: The downside is that this numerical viscosity smears out sharp features, such as wavefronts, contact discontinuities, or shocks. While this can improve stability, it comes at the cost of accuracy, especially in problems where sharp interfaces are physically meaningful.
Quantification via Modified Equation Analysis: The modified equation allows us to measure how much numerical diffusion is introduced and compare across schemes.
Thus, modified equation analysis connects stability theory (von Neumann analysis) and physical interpretation, showing how schemes balance between damping (diffusion) and oscillations (dispersion).
The artificial viscosity term also highlights the importance of the CFL condition:
When , the artificial viscosity remains positive, ensuring stability.
When , the coefficient becomes negative, meaning the scheme adds anti-diffusion rather than diffusion, which destabilize the solution.
Therefore, the CFL condition directly controls the magnitude and sign of the artificial viscosity introduced by the scheme.
Modified Equation of FTCS Advection¶
To determine how the FTCS scheme modifies the original linear advection equation by introducing additional terms that represent numerical errors, specifically focusing on artificial diffusion or dispersion.
Start with the FTCS Update Equation
Recalling the FTCS scheme for the linear advection equation is given by:
where:
is the numerical approximation of at spatial index and time level ,
is the constant advection speed,
and are the time and spatial step sizes, respectively,
is the Courant number.
Expand the Temporal and Spatial Terms Using Taylor Series
Expand around :
Undefined control sequence: \dddot at position 126: …Delta t^3}{6} {\̲d̲d̲d̲o̲t̲{u}\,}_i^n + \m… \begin{align} u_i^{n+1} = u_i^n + \Delta t \dot{u}_i^n + \frac{\Delta t^2}{2} \ddot{u}_i^n + \frac{\Delta t^3}{6} {\dddot{u}\,}_i^n + \mathcal{O}(\Delta t^3) \end{align}Expand and around :
Substitute the Taylor Expansions into the FTCS Scheme
Substitute the expansions into the FTCS update equation:
Undefined control sequence: \dddot at position 110: …Delta t^3}{6} {\̲d̲d̲d̲o̲t̲{u}\,}_i^n \\ … \begin{align} u_i^n &+ \Delta t \dot{u}_i^n + \frac{\Delta t^2}{2} \ddot{u}_i^n + \frac{\Delta t^3}{6} {\dddot{u}\,}_i^n \\ &\approx u_i^n - \frac{c \Delta t}{2 \Delta x} \left[ \left( u_i^n + \Delta x {u'}_i^n + \frac{\Delta x^2}{2} {u''}_i^n + \frac{\Delta x^3}{6} {u'''}_i^n \right) - \left( u_i^n - \Delta x {u'}_i^n + \frac{\Delta x^2}{2} {u''}_i^n - \frac{\Delta x^3}{6} {u'''}_i^n \right) \right] \end{align}Simplify,
Undefined control sequence: \dddot at position 89: …Delta t^2}{6} {\̲d̲d̲d̲o̲t̲{u}\,}_i^n … \begin{align} \dot{u}_i^n + \frac{\Delta t}{2}\ddot{u}_i^n + \frac{\Delta t^2}{6} {\dddot{u}\,}_i^n &\approx - c\left[ {u'}_i^n + \frac{\Delta x^2}{6} {u'''}_i^n \right] \end{align}
Substitute and Rearrange
Similar to the upwind screen, we recall that the advection equation implies the wave equation .
Taking an additional time derivative, we obtain:
Substitute this into the modified equation, we obtain
Rearrange and drop the indices and , the final modified equation is:
The modified equation of the FTCS advection scheme has a second-order “anti-diffusion” term and a third-order “dispersive” term. These terms affect the numerical solution in several ways:
Numerical Instability: Unlike the Upwind Scheme, which introduces artificial diffusion to enhance stability, the FTCS scheme has an anti-diffusion term that is unstable.
Introduction of Dispersive Errors: The (higher order) term represents a dispersive error that causes different wave components to travel at slightly different speeds. This leads to phase errors where waveforms become distorted over time, deviating from the exact solution.
Impact on Solution Accuracy: The introduced dispersive term does not counteract the amplification of numerical errors. Instead, it modifies the original equation in a way that can exacerbate inaccuracies, especially for higher-frequency components of the solution. Over time, these errors accumulate, leading to significant deviations from the true solution, as evidenced by the von Neumann stability analysis.
Reinforcement of Von Neumann Stability Findings: The modified equation analysis complements the von Neumann stability analysis by providing a physical interpretation of why the FTCS scheme is unstable. The introduced dispersive errors contribute to the amplification of numerical oscillations, aligning with the conclusion that for any .
Practical Considerations: Practitioners must recognize that the FTCS scheme not only fails to maintain stability but also introduces errors that distort the solution without providing any compensatory benefits.
Consequently, the FTCS scheme is unsuitable for solving advection-dominated PDEs where both stability and accuracy in capturing wave propagation are essential.
Lax-Wendroff Scheme¶
First-order schemes like the upwind method are simple to implement but often introduce significant numerical diffusion, which can blur important features of the solution. To address these limitations, higher-order schemes have been developed to provide more accurate and reliable results.
The Lax-Wendroff Scheme is a second-order accurate finite difference method designed to solve hyperbolic PDEs, such as the linear advection equation. Unlike first-order methods, the Lax-Wendroff scheme incorporates both temporal and spatial derivatives up to the second order. This allows it to capture wave propagation more accurately while minimizing numerical diffusion and dispersion.
The main advantage of the Lax-Wendroff Scheme is its ability to maintain higher accuracy without compromising stability. By extending the Taylor series expansion to include second-order terms, the scheme reduces the smearing effect seen in first-order methods. Additionally, it better preserves the shape and speed of waves, making it suitable for problems where precise wave behavior is crucial.
We begin by expanding in time around using a Taylor series up to second order:
Substitute the advection equation and the wave equation into the Taylor series expansion:
and approximate the spatial derivatives using centered finite differences:
we obtain:
Simplify the equation to isolate , we obtain the Lax-Wendroff scheme for the linear advection equation
# Parameters
c = 1.0 # advection speed
l = 1.0 # domain size
dt = 0.005 # time step
nx = 101 # number of spatial points
nt = 200 # number of time stepsX, dx = np.linspace(0, l, nx, endpoint=False, retstep=True) # spatial grid
U0 = np.sin(2*np.pi * X) # initial condition# Lax-Wendroff scheme
def LW(c, U0, dx, dt, n):
sigma = c * dt / dx
U = [U0]
for _ in range(n):
U0 = U[-1]
U1 = U0 - sigma * (np.roll(U0,-1) - np.roll(U0,1)) / 2 \
+ sigma*sigma * (np.roll(U0,-1) - 2*U0 + np.roll(U0,1)) / 2
U.append(U1)
return np.array(U)U = np.sin(2*np.pi * (X - c*dt*nt)) # analytical solution
ULW = LW(c, U0, dx, dt, nt) # numerical solutionplt.plot(X, U0, label='Initial Condition')
plt.plot(X, U, ':', label='Exact Solution')
plt.plot(X, ULW[-1], '.-', label='Lax-Wendroff')
plt.xlabel('x')
plt.ylabel('u')
plt.legend()anim = animate(X, ULW)
HTML(anim.to_html5_video()) # display animation
# anim.save('FTCS.mp4') # save animationVon Neumann Stability Analysis¶
Following previous derivation, it is straightforward to show
Therefore,
Hence, for all iff . This lead to the same CFL condition
# HANDSON: Perform the von Neumann stability analysis to the
# Lax-Wendroff Scheme.
Note that this is the same as the FTCS modified equation but without the antidiffusion term.
Note also that when , the term vanishes at leading order. When , the term create phase (dispersion) error. For , there is also weak dissipation from higher-order terms.
# HANDSON: Perform modified equation analysis to the Lax-Wendroff
# Scheme.