Practical III
Assignment 01
Solving 1D Advection Method
Submitted by
Ramesh Ghimerey
Sandesh Darjee
Submitted to
Dr. Rupak
CONTENT
1. The Linear Advection Method
2. Backward Difference Method
3. Central Differential Method
4. Forward Differential Method
5. Bacward Difference Method + RK4
6. Central Difference Method + RK4
7. Forward Difference Method + RK4
1
The Linear Advection Equation
Mathematical Formulation
The one-dimensional linear advection equation is given by:
∂u ∂u
+c =0
∂t ∂x
where:
• u(x, t) is the quantity being transported,
• c is the constant advection speed,
• x is the spatial coordinate,
• t is time.
Applications
The linear advection equation is used in:
• Transport of pollutants in fluids,
• Heat transfer modeling,
• Traffic flow simulations,
• Weather prediction models.
Boundary Condition
u(0, t) = u(L, t) = 0
For initial Condition
u(x, t) = f (x − ct)
2
• Let us consider initial condition as,
u(x, 0) = sin x
u(x, t) = sin(x − ct)
1 Python Code Implementation
Python Code
2 import numpy as np
3 import matplotlib.pyplot as plt
4
5 c = 1.0
6 x = np.linspace(0, 2*np.pi,)
7 u0 = np.sin(x)
8
9 times = [0.00, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40]
10
11 plt.figure(figsize=(10,5))
12
13 for t in times:
14 u = np.sin(x - c * t)
15 plt.plot(x, u, label=f"t = {t:.2f}")
16
17 plt.xlabel("x")
18 plt.ylabel("u(x, t)")
19 plt.title("Analytical Solution of Linear Advection Equation")
20 plt.legend()
21 plt.grid(True)
22 plt.ylim(-1.00, 1.00)
23 plt.show()
24
3
PLOT OF 1D ADVECTION EQUATION
1D Advection Equation
1 t = 0.00
t = 0.05
t = 0.10
t = 0.15
0.5 t = 0.20
t = 0.25
t = 0.30
t = 0.35
u(x, t)
0 t = 0.40
−0.5
−1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6
x
Figure 1: Sine wave solution for multiple time steps
4
Finite Difference Discretization
We discretize the domain using:
xi = i ∆x f ori = 0, 1, 2, . . . , N
tn = n ∆t f orn = 0, 1, 2, . . . , T
uni = u(xi , tn )
Forward Difference in Time
∂u un+1 − uni
≈ i (1)
∂t ∆t
Backward Difference in Space
∂u un − uni−1
≈ i (2)
∂x ∆x
Combined Discretized Form
Substituting the finite difference approximations into the PDE:
un+1
i − uni un − uni−1
+c i =0 (3)
∆t ∆x
FTBS Update Formula
Rearranging the above gives the explicit update formula:
∆t n
un+1
i = uni − c ui − uni−1 (4)
∆x
5
PYTHON CODE IMPLEMENTATION
Python Code
1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 # Parameters
5 c = 1.0 # Wave speed
6 L = 10.0 # Domain length
7 Nx = 100 # Number of spatial points
8 dx = L / (Nx - 1) # Spatial step size
9 dt = 0.005 # Time step size
10 T = 2.5 # Total simulation time
11
12
13
14 x = np.linspace(0, L, Nx)
15 u = np.sin(x)
16 u_all = [u.copy()]
17
18
19 nt = int(T / dt)
20 for n in range(nt):
21 u_new = u.copy()
22 for i in range(1, Nx):
23 u_new[i] = u[i] - c * dt / dx * (u[i] - u[i-1])
24
25
26 u_new[0] = u[0] - c * dt / dx * (u[0] - u[-1])
27 u = u_new
28 if n % 50 == 0:
29 u_all.append(u.copy())
30
31
32
33 # Plotting
34 plt.figure(figsize=(10,5))
35 for idx, snapshot in enumerate(u_all):
36 plt.plot(x, snapshot, label=f"t={idx*50*dt:.2f}")
37 plt.xlabel("x")
38 plt.ylabel("u")
39 plt.title("Linear Advection using Backward Difference (FTBS)")
40 plt.legend()
41 plt.grid(True)
42 plt.show()
6
Figure 2: FTBS
PLOT OF FTBS EQUATION
7
FTFS Scheme for 1D Linear Advection
Discretization
Let the spatial and temporal domains be discretized as: xi = i ∆x f ori = 0, 1, 2, . . . , N
tn = n ∆t f orn = 0, 1, 2, . . . , T
n n
ui ≈ u(xi , t )
Finite Difference Approximations
We approximate the partial derivatives using forward differences:
Time Derivative (Forward Difference)
∂u un+1
i − uni
≈ (5)
∂t ∆t
Space Derivative (Forward Difference)
∂u un − uni
≈ i+1 (6)
∂x ∆x
Substitution into the PDE
Substituting the finite difference approximations into the advection equation:
un+1
i − uni un − uni
+ c · i+1 =0 (7)
∆t ∆x
Rearranged Update Formula
Solving for un+1
i :
c∆t n
un+1
i = uni − (u − uni ) (8)
∆x i+1
Interpretation
This is the Forward Time Forward Space (FTFS) scheme:
• It uses explicit time stepping and forward spatial differencing.
• It is first-order accurate in both time and space.
• It is generally unstable for the advection equation, especially for c > 0, due to its
non-upwind nature.
• It tends to amplify numerical errors unless very strict conditions are met.
8
PYTHON CODE IMPLEMENTATION
Python Code
1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 c = 1.0 # Wave speed
5 L = 10.0 # Domain length
6 Nx = 100 # Number of spatial points
7 dx = L / (Nx - 1) # Spatial step size
8 dt = 0.005 # Time step size
9 T = 2.5 # Total simulation time
10
11
12 x = np.linspace(0, L, Nx)
13 u = np.sin(x)
14 u_all = [u.copy()]
15
16
17
18 nt = int(T / dt)
19 for n in range(nt):
20 u_new = u.copy()
21 for i in range(1, Nx):
22 u_new[i] = u[i] - c * dt / dx * (u[i] - u[i-1])
23
24 u_new[0] = u[0] - c * dt / dx * (u[0] - u[-1])
25 u = u_new
26 if n % 50 == 0:
27 u_all.append(u.copy())
28
29 plt.figure(figsize=(10,5))
30 for idx, snapshot in enumerate(u_all):
31 plt.plot(x, snapshot, label=f"t={idx*50*dt:.2f}")
32 plt.xlabel("x")
33 plt.ylabel("u")
34 plt.title("Linear Advection using Pure Forward Difference (FTFS)")
35 plt.legend()
36 plt.grid(True)
37 plt.show()
38
article [utf8]inputenc graphicx caption tcolorbox skins, breakable
FTFS Method Visualization
9
FTFS Plot
Linear Advection using Pure Forward Difference (FTFS)
Interpretation
The FTFS method shows clear numerical dispersion. As time progresses, the wave loses
its original shape, highlighting the instability of this scheme for advection-dominated
problems.
Derivation of the FTCS Scheme for 1D Linear Advec-
tion
Discretization
Let the spatial and temporal domains be discretized as: xi = i ∆x f ori = 0, 1, . . . , N
tn = n ∆t f orn = 0, 1, . . . , T
n n
ui ≈ u(xi , t )
Finite Difference Approximations
Time Derivative (Forward Difference)
∂u un+1 − uni
≈ i (9)
∂t ∆t
Space Derivative (Central Difference)
∂u un − uni−1
≈ i+1 (10)
∂x 2∆x
10
Substitution into the PDE
Substituting the finite difference approximations into the advection equation:
un+1
i − uni un − uni−1
+ c · i+1 =0 (11)
∆t 2∆x
Multiplying through by ∆t:
c∆t n
un+1
i = uni − (ui+1 − uni−1 ) (12)
2∆x
Remarks
This is the Forward Time Central Space (FTCS) scheme:
• Second-order accurate in space, first-order in time.
• Symmetric stencil in space.
• Conditionally unstable for the advection equation.
• May produce non-physical oscillations unless the Courant number is very small.
11
PYTHON CODE IMPLEMENTATION
Python Code
1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 # Parameters
5 c = 1.0 # Wave speed
6 L = 10.0 # Domain length
7 Nx = 100 # Number of spatial points
8 dx = L / (Nx - 1) # Spatial step size
9 dt = 0.005 # Time step size
10 T = 2.5 # Total simulation time
11
12
13
14 x = np.linspace(0, L, Nx)
15 u = np.sin(x)
16 u_all = [u.copy()]
17
18 nt = int(T / dt)
19 for n in range(nt):
20 u_new = u.copy()
21 for i in range(1, Nx-1):
22 u_new[i] = u[i] - 0.5 * c * dt / dx * (u[i+1] - u[i-1])
23
24
25 # Periodic boundary conditions
26 u_new[0] = u[0] - 0.5 * c * dt / dx * (u[1] - u[-1])
27 u_new[-1] = u[-1] - 0.5 * c * dt / dx * (u[0] - u[-2])
28 u_new[1] = u[1] - 0.5 * c * dt / dx * (u[2] - u[0])
29
30 u = u_new
31 if n % 50 == 0:
32 u_all.append(u.copy())
33
34 # Plotting
35 plt.figure(figsize=(10,5))
36 for idx, snapshot in enumerate(u_all):
37 plt.plot(x, snapshot, label=f"t={idx*50*dt:.2f}")
38 plt.xlabel("x")
39 plt.ylabel("u")
40 plt.title("Linear Advection using Central Difference (FTCS)")
41 plt.legend()
42 plt.grid(True)
43 plt.show()
12
width=0.9
Figure 4: Numerical simulation of the 1D linear advection equation using the Forward
Time Centered Space (FTCS) scheme. The plot illustrates the evolution of the solution
u(x, t) over time, with curves corresponding to discrete time steps from t = 0.00 to
t = 2.50. As time progresses, the scheme exhibits characteristic numerical instability and
oscillations, especially due to the non-dissipative nature of the central difference in space.
This highlights the limitations of FTCS for pure advection problems.
13
2 Runge - Kutta 4th Order Method
Overview
The Runge-Kutta 4th Order Method (RK4) is a widely used numerical technique for
solving ordinary differential equations (ODEs) of the form:
du
= f (t, u), u(t0 ) = u0 (13)
dt
RK4 provides a fourth-order accurate estimate of the solution by evaluating the slope
at multiple points within each time step.
RK4 Formulation
Given the current value un at time tn , the RK4 method computes the next value un+1
using the
following intermediate
slopes: k1 = f (tn , un )
k2 = f tn + ∆t2
, un + ∆t
2 1
k
k3 = f tn + ∆t
2
, un + ∆t
2 2
k
n n
k4 = f (t + ∆t, u + ∆tk3 )
The update formula is then:
∆t
un+1 = un + (k1 + 2k2 + 2k3 + k4 ) (14)
6
Features
• Fourth-order accuracy: local error O(∆t5 ), global error O(∆t4 )
• No need for higher derivatives
• Stable and efficient for smooth problems
Application to PDEs
For partial differential equations like the 1D linear advection equation:
∂u ∂u
+c =0 (15)
∂t ∂x
RK4 is used to integrate in time, while spatial derivatives (e.g., ∂u
∂x
) are approximated
using finite difference methods such as central or upwind schemes.
14
Central Difference + RK4 Method
PYTHON CODE IMPLEMENTATION
Python Code
1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 # Parameters
5 c = 1.0 # Wave speed
6 L = 10.0 # Domain length
7 Nx = 100 # Number of spatial points
8 dx = L / (Nx - 1) # Spatial step size
9 dt = 0.005 # Time step size
10 T = 2.5 # Total simulation time
11
12 x = np.linspace(0, L, Nx)
13 u = np.sin(x)
14 u_all = [u.copy()]
15
16 nt = int(T / dt)
17
18 def spatial_derivative(u):
19 """Central difference with periodic boundaries"""
20 dudx = np.zeros_like(u)
21 dudx[1:-1] = (u[2:] - u[:-2]) / (2 * dx)
22 dudx[0] = (u[1] - u[-1]) / (2 * dx)
23 dudx[-1] = (u[0] - u[-2]) / (2 * dx)
24 return dudx
25
26 for n in range(nt):
27 k1 = -c * spatial_derivative(u)
28 k2 = -c * spatial_derivative(u + 0.5 * dt * k1)
29 k3 = -c * spatial_derivative(u + 0.5 * dt * k2)
30 k4 = -c * spatial_derivative(u + dt * k3)
31
32 u = u + (dt / 6.0) * (k1 + 2*k2 + 2*k3 + k4)
33
34 if n % 50 == 0:
35 u_all.append(u.copy())
36
37 # Plotting
38 plt.figure(figsize=(10,5))
39 for idx, snapshot in enumerate(u_all):
40 plt.plot(x, snapshot, label=f"t={idx*50*dt:.2f}")
41 plt.xlabel("x")
42 plt.ylabel("u")
15
43 plt.title("Linear Advection using RK4 + Central Difference")
44 plt.legend()
45 plt.grid(True)
46 plt.show()
width=0.9
Figure 5: Simulation of the 1D linear advection equation using the Runge-Kutta 4th
order (RK4) method for time integration combined with central difference for spatial dis-
cretization. The plot shows the evolution of the solution u(x, t) over time, with snapshots
from t = 0.00 to t = 2.50 in increments of 0.25. This method offers improved stability
and accuracy compared to simpler schemes like FTCS, and the wave propagation remains
smooth and well-resolved throughout the simulation.
16
Forward Difference + RK4 Method
PYTHON CODE IMPLEMENTATION
Python Code
1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 # Parameters
5 c = 1.0 # Wave speed
6 L = 10.0 # Domain length
7 Nx = 100 # Number of spatial points
8 dx = L / (Nx - 1) # Spatial step size
9 dt = 0.005 # Time step size
10 T = 2.5 # Total simulation time
11
12 x = np.linspace(0, L, Nx)
13 u = np.sin(x)
14 u_all = [u.copy()]
15
16 nt = int(T / dt)
17
18 def spatial_derivative_forward(u):
19 """Forward difference with periodic boundaries"""
20 dudx = np.zeros_like(u)
21 dudx[:-1] = (u[1:] - u[:-1]) / dx
22 dudx[-1] = (u[0] - u[-1]) / dx # Periodic boundary
23 return dudx
24
25 for n in range(nt):
26 k1 = -c * spatial_derivative_forward(u)
27 k2 = -c * spatial_derivative_forward(u + 0.5 * dt * k1)
28 k3 = -c * spatial_derivative_forward(u + 0.5 * dt * k2)
29 k4 = -c * spatial_derivative_forward(u + dt * k3)
30
31 u = u + (dt / 6.0) * (k1 + 2*k2 + 2*k3 + k4)
32
33 if n % 50 == 0:
34 u_all.append(u.copy())
35
36 # Plotting
37 plt.figure(figsize=(10,5))
38 for idx, snapshot in enumerate(u_all):
39 plt.plot(x, snapshot, label=f"t={idx*50*dt:.2f}")
40 plt.xlabel("x")
41 plt.ylabel("u")
42 plt.title("Linear Advection using RK4 + Forward Difference")
17
43 plt.legend()
44 plt.grid(True)
45 plt.show()
46
Figure 6: Linear advection using RK4 with forward difference. The solution u(x, t) is
shown at multiple time steps.
18
Backward Difference + RK4 Method
3 PYTHON CODE IMPLEMENTATION
Python Code
1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 # Parameters
5 c = 1.0 # Wave speed
6 L = 10.0 # Domain length
7 Nx = 100 # Number of spatial points
8 dx = L / (Nx - 1) # Spatial step size
9 dt = 0.005 # Time step size
10 T = 2.5 # Total simulation time
11
12 x = np.linspace(0, L, Nx)
13 u = np.sin(x)
14 u_all = [u.copy()]
15
16 nt = int(T / dt)
17
18 def spatial_derivative_backward(u):
19 """Backward difference with periodic boundaries"""
20 dudx = np.zeros_like(u)
21 dudx[1:] = (u[1:] - u[:-1]) / dx
22 dudx[0] = (u[0] - u[-1]) / dx # Periodic boundary
23 return dudx
24
25 for n in range(nt):
26 k1 = -c * spatial_derivative_backward(u)
27 k2 = -c * spatial_derivative_backward(u + 0.5 * dt * k1)
28 k3 = -c * spatial_derivative_backward(u + 0.5 * dt * k2)
29 k4 = -c * spatial_derivative_backward(u + dt * k3)
30
31 u = u + (dt / 6.0) * (k1 + 2*k2 + 2*k3 + k4)
32
33 if n % 50 == 0:
34 u_all.append(u.copy())
35
36 # Plotting
37 plt.figure(figsize=(10,5))
38 for idx, snapshot in enumerate(u_all):
39 plt.plot(x, snapshot, label=f"t={idx*50*dt:.2f}")
40 plt.xlabel("x")
41 plt.ylabel("u")
42 plt.title("Linear Advection using RK4 + Backward Difference")
19
43 plt.legend()
44 plt.grid(True)
45 plt.show()
46
Figure 7: Linear Advection using RK4 + Backward Difference. This plot illustrates the
evolution of the solution u(x, t) over time, from t = 0.00 to t = 2.50 in increments of
0.25. Each curve represents a snapshot at a specific time step, highlighting the method’s
behavior in capturing wave propagation.
20
ACKNOWLEDGEMENT
We would like to express our sincere gratitude to Rupak Sir for his invaluable guidance
and support throughout our exploration of numerical methods for partial differential
equations. His clear explanations and insightful feedback have deepened my understand-
ing of schemes such as FTBS, FTFS, FTCS, and RK4, and have helped me appreciate
the nuances of stability, accuracy, and implementation.
His encouragement to approach problems analytically and document them profes-
sionally using tools like Python and LaTex has been instrumental in shaping both my
technical skills and our appreciation for reproducible scientific computing. The clarity
and structure he instills in his teaching have made even complex topics approachable and
engaging.
I am truly grateful for his mentorship and the learning environment he fosters.
21