Tutorial 6
Tutorial 6
Description of problem
𝜕𝐶 𝜕𝐶 𝜕2 𝐶
+𝑢 −𝛤 =𝑆 (1)
𝜕𝑡 𝜕𝑥 𝜕𝑥 2
where u is the velocity, 𝛤 is the diffusion coefficient and S is the source term. Using
finite difference method, numerically solving the equation in a computational domain
𝑥 ∈ [0, 𝐿] with the following boundary conditions,
• At x = 0, C = 1
• At x = L, 𝐶 = 0
At the initial step, C = 0 in the entire domain.
A reminder
𝜕𝐶 𝜕2 𝐶
• Central scheme (central scheme for both 𝜕𝑥 and 𝜕𝑥 2)
𝑢 𝛤 𝛤 𝑢 𝛤
(− 2𝑖 − ∆𝑥𝑖 ) 𝐶𝑖−1 + 2 ∆𝑥𝑖 𝐶𝑖 + ( 2𝑖 − ∆𝑥𝑖 ) 𝐶𝑖+1 = ∆𝑥𝑆𝑖 (2)
𝛤 𝛤 𝛤
(𝑚𝑖𝑛(−𝑢𝑖 , 0) − ∆𝑥𝑖 ) 𝐶𝑖−1 + (2 ∆𝑥𝑖 + 𝑎𝑏𝑠(𝑢𝑖 )) 𝐶𝑖 + (𝑚𝑖𝑛(0, 𝑢𝑖 ) − ∆𝑥𝑖 ) 𝐶𝑖+1 = ∆𝑥𝑆𝑖 (3)
Please be noted that when deriving these equations, we have multiplied both sides of
𝜕𝐶 𝜕2 𝐶
the equations by ∆𝑥. In the other words, directly replacing the derivatives in 𝑢 𝜕𝑥 − 𝛤 𝜕𝑥 2 =
𝑆 using finite difference schemes, it leads to
1 𝑢𝑖 𝛤 1 2𝛤 1 𝑢 𝛤
(− − ∆𝑥𝑖 ) 𝐶𝑖−1 + ∆𝑥 ( ∆𝑥𝑖 ) 𝐶𝑖 + ∆𝑥 ( 2𝑖 − ∆𝑥𝑖 ) 𝐶𝑖+1 = 𝑆𝑖 (4)
∆𝑥 2
and
1 𝛤 1 2𝛤 1 𝛤
(𝑚𝑖𝑛(−𝑢𝑖 , 0) − ∆𝑥𝑖 ) 𝐶𝑖−1 + ∆𝑥 ( ∆𝑥𝑖 + 𝑎𝑏𝑠(𝑢𝑖 )) 𝐶𝑖 + ∆𝑥 (𝑚𝑖𝑛(0, 𝑢𝑖 ) − ∆𝑥𝑖 ) 𝐶𝑖+1 = 𝑆𝑖 (5)
∆𝑥
Compared with the 1D stead advection-diffusive equation, the unsteady one has an
additional term. Accordingly, one only needs to add one additional term into the above
𝜕𝐶
discretised equation. This additional term is the finite difference scheme for 𝜕𝑡 , i.e.
𝜕𝐶 𝐶𝑖𝑘+1 −𝐶𝑖𝑘
= (6)
𝜕𝑡 ∆𝑡
𝐶𝑖𝑘+1−𝐶𝑖𝑘 1 𝑢𝑘 Γ𝑘 𝑘 1 2Γ𝑘 1 𝑢𝑘 Γ𝑘
+ ∆𝑥 (− 𝑖
− ∆𝑥𝑖 ) 𝐶𝑖−1 + ∆𝑥 ( ∆𝑥𝑖 ) 𝐶𝑖𝑘 + ∆𝑥 ( 2𝑖 − ∆𝑥𝑖 ) 𝐶𝑖+1
𝑘
= S𝑖𝑘 (8)
∆𝑡 2
for the explicit Euler’s method. For your convenience, all changes compared with the
Eq. (4) is highlighted in red. Generally, the implicit Euler’s method has better stability
and thus is used more popular than the Explicit one. Moving all terms in Eq. (7) related
to k-th time step to the right-hand side, multiplying ∆𝑥, it leads to
Compared with Eq. (2), the changes are highlighted in blue. Similarly, Eq. (8) can be
rearranged to
∆𝑡 𝑢𝑘 Γ𝑘 ∆𝑡 2Γ𝑘 ∆𝑡 𝑢𝑘 Γ𝑘
𝐶𝑖𝑘+1 = − ∆𝑥 (− 𝑖 𝑘
− ∆𝑥𝑖 ) 𝐶𝑖−1 − ∆𝑥 ( ∆𝑥𝑖 ) 𝐶𝑖𝑘 − ∆𝑥 ( 2𝑖 − ∆𝑥𝑖 ) 𝐶𝑖+1
𝑘
+ ∆𝑡S𝑖𝑘 + 𝐶𝑖𝑘 (10)
2
You can make minor change to the code you have developed in Tutorial 5 to solve the
unsteady problem. The following is the code for the central scheme, in which the
changes to accommodate the time derivative are highlighted in red, those due to the
change of the boundary condition at i = n is highlighted in orange.
dt = (te-ts)/nStep;
ts = ts:dt:ts;
% leading to A F = B
A = zeros(n);
B(1:n) = 0;
for i = 1:n
if i == 1 % left boundary
A(i,i) = 1.0;
B(i) = f0;
elseif i == n % right boundary
A(i,i) = 1.0;
B(i) = fL;
else
A(i,i-1) = - u/2.0 - Tau/dx;
A(i,i) = 2*Tau/dx + dx/dt;
A(i,i+1) = u/2.0 - Tau/dx;
B(i) = dx*S + dx/dt*fs(k,i);
end
end
% solving the linear system of equation
fs(k+1,:) = A\B';
end
end