∂u

Partial Differential Equations

Your Progress in this Chapter

0%

0 / 12 units completed

Chapter Overview

Solution Of PDE by Method of Lines

Section 10.2 of 435 min6 code examples

The Method of Lines (MOL) is a powerful numerical technique used to solve partial differential equations (PDEs), particularly those that are time-dependent (evolutionary).

Instead of discretizing all dimensions(space and time) simultaneously, the core idea is to discretize the spatial variables while leaving the time variable continuous. This transforms a single PDE into a system of coupled Ordinary Differential Equations(ODEs).

How It Works: The 3-Step Process

Imagine you are solving the heat equation:

ut=α2ux2\frac{\partial u}{\partial t} = \alpha \frac{\partial^2 u}{\partial x^2}
  1. Spatial Discretization: You divide the spatial domain into a grid of points(e.g., x_1, x_2, \cdot, x_n). You replace the spatial derivatives (\frac{\partial^2 u}{\partial x^2}) with finite difference approximations, such as:
2ux2=ui+12ui+ui1(Δx)2\frac{\partial^2 u}{\partial x^2} = \frac{u_{i+1} - 2u_i + u_{i-1}}{(\Delta x)^2}
  1. Conversion to ODEs: By applying this at every grid point, the PDE becomes a set of ODEs, one for each point :
ui(t)tαui+1(t)2ui(t)+ui1(t)(Δx)2\frac{\partial u_i(t)}{\partial t} \approx \alpha \frac{u_{i+1}(t) - 2u_i(t) + u_{i-1}(t)}{(\Delta x)^2}
  1. Temporal Integration: Now that you have a system of ODEs, you can use standard, high-performance ODE solvers like Runge-Kutta or Euler’s method to step forward in time.

## Why Use It? (Advantages)

  • Leverages Existing Tech: You can use sophisticated, pre-built ODE solvers(like Ode45/Ode45a in SepalSolver or ode45 in MATLAB) that automatically handle error control and adaptive time-stepping.
  • Flexibility: You can use different spatial discretization methods, such as finite differences, finite elements, or spectral methods, depending on the geometry of your problem
  • Simplification: It breaks a complex multi-dimensional problem into a more manageable "line-by-line" temporal evolution.

## Limitations

  • Stiffness: The resulting system of ODEs is often "stiff," meaning you may need implicit solvers to avoid tiny, inefficient time steps.
  • Not for All PDEs: It is designed for "evolutionary" problems (parabolic and hyperbolic). It cannot be used directly for purely "steady-state" elliptic equations(like Laplace’s equation) without adding a "pseudo-time" variable.
FeatureStandard Finite Difference(FDM)Method of Lines(MOL)
Time TreatmentDiscretized at the startKept continuous initially
Solving LogicSolves the whole grid at onceSolves ODEs along "lines" of time
SoftwareRequires custom time-steppingUses off-the-shelf ODE solvers

In sepalsolver, the nnumerical solution of pde (Pdepe) is built on Ode45a: our differential algebraic equation solver that relies on L-stable diagonally implicit Runge Kutta. This allows it to handle the boundary conditions implicitly and also correct the egde values in situation where the initial condition is not consitent with the boundary condition

Example 1: Solving the Heat Equation

Consider the one-dimensional heat equation:

ut=α2ux2\frac{\partial u}{\partial t} = \alpha \frac{\partial^2 u}{\partial x^2}

with initial condition u(x,0) = \sin(\pi x) and boundary conditions u(0,t) = u(1,t) = 0.

Example 1C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Code is ready to run
OutputFrom the book
Temperature.png

Example 2: Solving Reactive Diffusion Equation

The Fisher-KPP reaction-diffusion model transformed into a cylindrical coordinate system (m = 1). In cylindrical coordinates, the model represents radial population dispersion or chemical wavefront propagation outward from a central core (e.g., cell growth in a Petri dish or cylindrical tissue scaffold).

ut=D1rr(rur)+rgu(1u)\frac{\partial u}{\partial t} = D\frac{1}{r}\frac{\partial}{\partial r}\left(r\frac{\partial u}{\partial r} \right) + r_g \cdot u(1 - u)

initial condition

u(r,0)={0.8if r<0.5,0otherwiseu(r,0) = \begin{cases} 0.8 & \text{if } r < 0.5, \\ 0 & \text{otherwise} \end{cases}

boundary condition

ur(0,t)=0,ur(5,t)=0\left.\frac{\partial u}{\partial r}\right|_{(0,t)} = 0, \quad \left.\frac{\partial u}{\partial r}\right|_{(5,t)} = 0

Example 2C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Code is ready to run
OutputFrom the book
Cylindrical_FisherKPP.png
Cylindrical_FisherKPP.png

It is important to note that pdepe can be invoked with a shothand form as shown in the example below

Example 3C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Code is ready to run

Example 3: System of Partial Differential Equations

Here is a classic engineering problem: Coupled Transient Heat and Mass Transfer in a Cylindrical Reactor / Packed Bed (with m = 1). This models fluid flowing axially through a cylindrical pipe where a chemical reaction occurs, generating heat and consuming reactant along both the radial direction r and over time t.

Governing System of Equations For radial coordinate r \in [r_0, R] (or x \in [0, R]) and time t > 0, we track two coupled variables:

  1. Temperature T(r,t)
  2. Reactant Concentration C(r,t)
ρCpTt=1rr(rkTr)+(ΔH)k0CeEaRgTCt=1rr(rDCr)k0CeEaRgT\begin{aligned} \rho C_p \frac{\partial T}{\partial t} &= \frac{1}{r} \frac{\partial}{\partial r} \left( r \cdot k \frac{\partial T}{\partial r} \right) + (-\Delta H) \cdot k_0 C e^{-\frac{E_a}{R_g T}} \\ \frac{\partial C}{\partial t} &= \frac{1}{r} \frac{\partial}{\partial r} \left( r \cdot D \frac{\partial C}{\partial r} \right) - k_0 C e^{-\frac{E_a}{R_g T}} \end{aligned}

Standard Pdepe Vector Mapping

In terms of the pdepe flux-source balance equation with cylindrical symmetry (m = 1):

c(x,t,u,ux)ut=x1x(x1f(x,t,u,ux))+s(x,t,u,ux)c\left(x,t,\mathbf{u},\frac{\partial \mathbf{u}}{\partial x}\right) \frac{\partial \mathbf{u}}{\partial t} = x^{-1} \frac{\partial}{\partial x}\left(x^1 \mathbf{f}\left(x,t,\mathbf{u},\frac{\partial \mathbf{u}}{\partial x}\right)\right) + \mathbf{s}\left(x,t,\mathbf{u},\frac{\partial \mathbf{u}}{\partial x}\right)

We map state vector \mathbf{u} = \begin{bmatrix} T \\ C \end{bmatrix} (where u_1 = T, u_2 = C and spatial coordinate x = r):

-Capacities \mathbf{c}:

c=[ρCp1]c = \begin{bmatrix} \rho C_p \\ 1 \end{bmatrix}

Fluxes \mathbf{f}:

f=[kTrDCr]f = \begin{bmatrix} k \frac{\partial T}{\partial r} \\ D \frac{\partial C}{\partial r} \end{bmatrix}

Sources \mathbf{s}:

s=[(ΔH)k0CeEaRgTk0CeEaRgT]s = \begin{bmatrix} (-\Delta H) \cdot k_0 C e^{-\frac{E_a}{R_g T}} \\ -k_0 C e^{-\frac{E_a}{R_g T}} \end{bmatrix}

Example 4C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
Code is ready to run
OutputFrom the book
System_of_PDE.png

Example 4: Higher Order in time: Wave Equation 1D

This is haneld using the system of PDE just like we use system of ode to handle higher oder odes. We first convert the higher oder derivative in time to system of first order time pdes.

Lets look as example of a string fixed at the right end and exited at the left end according to 0.5 Sin(10*t).

2ut2=c22ux2\frac{\partial^2 u}{\partial t^2} = c^2\frac{\partial^2 u}{\partial x^2}

initial condition

u(x,0)=0u(x,0) = 0

boundary condition

u(0,t)=0.5sin(20t)u(0, t) = 0.5\sin(20t)
u(L,t)=0u(L, t) = 0

To solve this, we set v = \cfrac{\partial u}{\partial t} This means we also have to differentiate the boundary conditions. And then we have

ut=vvt=c22ux2\begin{align} \frac{\partial u}{\partial t} = v \\ \frac{\partial v}{\partial t} = c^2\frac{\partial^2 u}{\partial x^2} \end{align}

initial condition

u(x,0)=0v(x,0)=0\begin{align} u(x,0) = 0\\ v(x,0) = 0 \end{align}

boundary condition

u(0,t)=0.5sin(20t)v(0,t)=10cos(20t)\begin{align} u(0, t) = 0.5\sin(20t)\\ v(0, t) = 10\cos(20t) \end{align}
u(L,t)=0v(L,t)=0\begin{align} u(L, t) = 0\\ v(L, t) = 0 \end{align}

Example 5C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Code is ready to run

Example 5: Higher Dimension: Wave Equation Example

For higher dimensions, the same method can be applied. This will be demonstrated using wave equation assume c > 0

2ut2=c2(2ux2+2uy2)\frac{\partial^2 u}{\partial t^2} = c^2\left(\frac{\partial^2 u}{\partial x^2} + \frac{\partial^2 u}{\partial y^2} \right)

initial condition

u(x,y,0)={0.5(1+cos(πr(x,y)R))if r(x,y)<R,0otherwiseu(x,y,0) = \begin{cases} 0.5\left(1 + \cos\left(\cfrac{\pi r(x, y)}{R} \right) \right) & \text{if } r(x,y) < R, \\ 0 & \text{otherwise} \end{cases}

where r = \sqrt{(x - x_c)^2 + (y - y_c)^2}

boundary condition

u(±L/2,y,t)=0 for y[L/2,L/2]u(\pm L/2, y, t) = 0~\text{for}~y \in [-L/2, L/2]
u(x,±L/2,t)=0 for x[L/2,L/2]u(x, \pm L/2, t) = 0~\text{for}~x \in [-L/2, L/2]

Step 1: Convert to system of first order PDEs in time

ut=v\frac{\partial u}{\partial t} = v

and hence

vt=c2(2ux2+2uy2)\frac{\partial v}{\partial t} = c^2\left(\frac{\partial^2 u}{\partial x^2} + \frac{\partial^2 u}{\partial y^2} \right)

Step 2: discretize the spatial part of the equation as:

2ux2=u(i+1,j)2u(i,j)+u(i1,j)(Δx)2\frac{\partial^2 u}{\partial x^2} = \frac{u(i+1, j) - 2u(i, j) + u(i-1, j)}{(\Delta x)^2}
2uy2=u(i,j+1)2u(i,j)+u(i,j1)(Δy)2\frac{\partial^2 u}{\partial y^2} = \frac{u(i, j+1) - 2u(i, j) + u(i, j-1)}{(\Delta y)^2}

Step 3: Assemble the system of coupled ODEs

u(i,j)t=v(i,j)\frac{\partial u(i, j)}{\partial t} = v(i, j)
v(i,j)t=c2(u(i+1,j)2u(i,j)+u(i1,j)(Δx)2+u(i,j+1)2u(i,j)+u(i,j1)(Δy)2)\frac{\partial v(i, j)}{\partial t} = c^2\left(\frac{u(i+1, j) - 2u(i, j) + u(i-1, j)}{(\Delta x)^2} + \frac{u(i, j+1) - 2u(i, j) + u(i, j-1)}{(\Delta y)^2} \right)

We were given boundary conditions for u but we introduce v, so we have to create the boubdary condition for that by differentiating the given boundary condition.

ut(±L/2,y,t)=0 for y[L/2,L/2]\frac{\partial u}{\partial t}(\pm L/2, y, t) = 0~\text{for}~y \in [-L/2, L/2]
ut(x,±L/2,t)=0 for x[L/2,L/2]\frac{\partial u}{\partial t}(x, \pm L/2, t) = 0~\text{for}~x \in [-L/2, L/2]

we can do so for \partial u/\partial t too

vt(±L/2,y,t)=0 for y[L/2,L/2]\frac{\partial v}{\partial t}(\pm L/2, y, t) = 0~\text{for}~y \in [-L/2, L/2]
vt(x,±L/2,t)=0 for x[L/2,L/2]\frac{\partial v}{\partial t}(x, \pm L/2, t) = 0~\text{for}~x \in [-L/2, L/2]

Example 6C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Code is ready to run