Escolar Documentos
Profissional Documentos
Cultura Documentos
Antonis Karantonis
Chemist, PhD
Saitama, 2001
Contents
1 Introduction 3
1
2 Contents
6.2.2 Example 7: Solution of the diffusion equation in two dimensions and Dirichlet boundary
conditions with Douglas-Gunn algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
6.2.3 Neumann boundary conditions for Douglas-Gunn method . . . . . . . . . . . . . . . . . . 86
6.2.4 Example 8: Solution of the diffusion equation in two dimensions with Dirichlet and Neu-
mann boundary conditions with Douglas-Gunn algorithm . . . . . . . . . . . . . . . . . . . 87
Introduction
On the other hand, is Eq. (1.1) holds in some subspace then we must impose some boundary conditions upon u.
Equation (1.1) is obviously a linear equation.
Consider now the following equation,
u
= 2 u + f (u). (1.3)
t
Here, a reaction term f (u) is added on the diffusion equation. The reaction term can be nonlinear therefore Eq. (1.3)
in gereral can be nonlinear.
The main question asked is, if we know the space distribution of u at time t = 0, how will it evolve through
time? In the rest of these notes we will present some numerical techniques in order to solve reaction-diffusion
equations, that is to find the evolution of the state variable u in time.
The reader who is interested in other features of reaction-diffusion equations, such as stability and pattern
formation, may consult books such as P. Glindrod The Theory and Applications of Reaction-Diffusion Equations:
Patterns and Waves.
3
4 Chapter 1. Introduction
Chapter 2
2.1 Discretisation
u 2u
a 2 = 0. (2.1)
t x
Using a forward Euler discretisation for the time derivative and a Crank- Nicolson scheme for the spatial derivative
we get,
un+1
j unj h unj1 2unj + unj+1 un+1 n+1
j1 2uj + un+1
j+1
i
a (1 ) 2
+ 2
= 0, (2.2)
t x x
where = 0.5.
Splitting the unknowns from the knows in Eq. (2) we obtain the following algorithm,
sun+1 n+1
j1 + (1 + 2s)uj sun+1 n n n n
j+1 = uj 2(1 )suj + (1 )suj1 + (1 )suj+1 , (2.3)
where s = at/x2 , j = 2, 3, ..., jmax 1, and jmax is the maximum number of nodal points in the spatial
coordinate.
Equation (3) can be written in a more compact form as,
sun+1 n+1
j1 + (1 + 2s)uj sun+1 n n
j+1 = M u + (1 )L u , (2.4)
where u = (uj1 , uj , uj+1 )T , the operators M and L are, M = (0, 1, 0) and L = (1, 2, 1).
The problem now is to solve Eq. (3) or (4) for the unknowns un+1
j , j = 1, 2, ..., jmax .
5
6 Chapter 2. Solution of the one dimensional diffusion equation
where b1 (t) and b2 (t) are known functions. Due to Eqs.(5-6) the values of u at the nodes 1 and j max are known,
i.e.,
sun+1
1 + (1 + 2s)un+1
2 sun+1
3 = un2 2(1 )sun2 + (1 )sun1 + (1 )sun3 ,
sun+1
2 + (1 + 2s)un+1
3 sun+1
4 = un3 2(1 )sun3 + (1 )sun2 + (1 )sun4 ,
... (2.9)
sun+1
jmax 2 + (1 + 2s)un+1
jmax 1 sun+1
jmax = unjmax 1 2(1 )sunjmax 1 + (1 )sunjmax 2 +
(1 )sunjmax .
But, due to the DBC, u1 and ujmax are known elements (for any n) in Eq. (9). Placing the unknowns on the left side
we obtain,
(1 + 2s)un+1
2 sun+1
3 = un2 2(1 )sun2 + (1 )sun1 + (1 )sun3 + sun+1
1 ,
sun+1
2 + (1 + 2s)un+1
3 sun+1
4 = un3 2(1 )sun3 + (1 )sun2 + (1 )sun4 ,
... (2.10)
sun+1 n+1 n n n
jmax 2 + (1 + 2s)ujmax 1 = ujmax 1 2(1 )sujmax 1 + (1 )sujmax 2 +
(1 )sunjmax + sun+1
jmax .
Equation (10) is a system of linear algebraic equations for jmax 2 unknowns (i.e. un+1
j , j = 2, 3, ..., jmax 1).
This equation can be written in a matrix form,
A u = d, (2.11)
where,
T
u = (un+1
2 , un+1
3 , ..., un+1
jmax 1 ) (2.12)
1 + 2s s 0 0 ... ...
s 1 + 2s s 0 ... ...
A= 0 s 1 + 2s s 0 ... (2.13)
... ... ... ... ... ...
... ... ... 0 s 1 + 2s
2.3. Neumann boundary conditions 7
and
From the structure of A we observe that the system Eq. (10) is tridiagonal. The dimension of A is (j max 2)
(jmax 2) and the dimension of the vectors u and d is 1 (jmax 2). The tridiagonal system Eq. (11) can be
solved with Thomas algorithm.
The case of Neumann boundary conditions (NBC) must be treated with care because the value of the derivative is
known at the boundaries.
un+1
2 un+1
0
= cn+1 . (2.16)
2x
Now, assume a fully implicit scheme for the interior, i.e.,
(1 + 2s)un+1
1 sun+1
0 sun+1
2 = un1 . (2.17)
(1 + 2s)un+1
1 2sun+1
2 = un1 2sxcn+1 . (2.18)
Equation (18) will be the first line of the system of the algebraic equations. Thus, Eq. (10) is written,
(1 + 2s)un+1
1 2sun+1
2 = un1 2sxcn+1
sun+1
1 + (1 + 2s)un+1
2 sun+1
3 = un2 2(1 )sun2 + (1 )sun1 + (1 )sun3 ,
... (2.19)
sun+1 n+1 n n n
jmax 2 + (1 + 2s)ujmax 1 = ujmax 1 2(1 )sujmax 1 + (1 )sujmax 2 +
(1 )sunjmax + sun+1
jmax .
8 Chapter 2. Solution of the one dimensional diffusion equation
T
u = (un+1
1 , un+1
3 , ..., un+1
jmax 1 ) (2.20)
1 + 2s 2s 0 0 ... ...
s 1 + 2s s 0 ... ...
A=
...
(2.21)
... ... ... ... ...
... ... ... 0 s 1 + 2s
and
un1 2sxcn+1
...
d= unj 2(1 )sunj + (1 )sunj1 + (1 )sunj+1 . (2.22)
...
unjmax 1 2(1 )sunjmax 1 + (1 )sunjmax 2 + (1 )sunjmax + sun+1
jmax .
Clearly, the system is again tridiagonal and can be solved with Thomas algorithm. The dimension of A is
(jmax 1) (jmax 1) and the dimension of the vectors u and d is 1 (jmax 1).
un+1 n+1
jmax +1 ujmax 1
= cn+1
2 . (2.24)
2x
Now, assume a fully implicit scheme for the interior, i.e.,
Equation (26) will be the last line of the system of the algebraic equations. Thus, Eq. (10) is written,
(1 + 2s)un+1
1 2sun+1
2 = un1 2sxcn+1
1
sun+1
1 + (1 + 2s)un+1
2 sun+1
3 = un2 2(1 )sun2 + (1 )sun1 + (1 )sun3 ,
(2.27)
...
2sun+1 n+1 n n+1
jmax 1 + (1 + 2s)ujmax = ujmax + 2xc2 .
2.4. Thomas algorithm 9
and
un1 2sxcn+1
1
...
d = uj 2(1 )suj + (1 )sunj1 + (1 )sunj+1
n n . (2.30)
...
ujmax + 2sxcn+1
n
2
Clearly, the system is again tridiagonal and can be solved with Thomas algorithm. The dimension of A is
jmax jmax and the dimension of the vectors u and d is 1 jmax .
xN = d0N (2.35)
xj = d0j c0j xj+1 (2.36)
The computational tricks for the implementation of Thomas algorithm are shown in the listing of the specific
examples below.
In this example we solve the one dimensional diffusion equation for Dirichlet boundary conditions on both ends.
The iteration formulas are located in in the main body of the program DIFIM.
PROGRAM DIFIM
C C.A.J. Fletcher,
C Computational Techniques in fluid dynamics, vol.1
OPEN(1,FILE=~/programs/Crank-Nicolson/c-n.dat,
$ STATUS=UNKNOWN)
OPEN(2,FILE=~/programs/Crank-Nicolson/c-n.out,
2.5. Example 1: Solution of 1D diffusion equation with DBC 11
$ STATUS=UNKNOWN)
OPEN(3,FILE=~/programs/Crank-Nicolson/diffusion.dat,
$ STATUS=UNKNOWN)
READ(1,*)IPR,JMAX,MAXEX,NMAX,ALPH,S,TMAX,TST
PI=3.1415927
JMAP=JMAX-1
AJM=JMAP
C Spatial step
DELX=1./AJM
C Temporal step
DELT=DELX*DELX*S/ALPH
WRITE(2,4)
WRITE(2,2)JMAX,MAXEX,NMAX,TMAX,TST
2 FORMAT( JMAX=,I5, MAXEX=,I5, NMAX=,I5,
$ TMAX=,F8.2, TST=,F5.2)
12 Chapter 2. Solution of the one dimensional diffusion equation
WRITE(2,3)S,ALPH,DELT,DELX
3 FORMAT( S=,F5.3, ALPH=, E10.3, DELT=,E10.3,
$ DELX=,E10.3,//)
C IMPLICIT PARAMETERS
JMAF=JMAX-2
T = TST
CALL INIT(JMAX,MAXEX,DELX,PI,ALPH,T,TE)
DO J=2, JMAP
TN(J)=TE(J)/100.
END DO
TD(1)=100.*TN(1)
TD(JMAX)=100.*TN(JMAX)
N=0
11 N=N+1
DO J=2,JMAP
JM=J-1
IF(N.GT.1)GOTO 12
A(1,JM)=0.
A(2,JM)=AD
A(3,JM)=BD
A(4,JM)=CD
A(5,JM)=0.
12 D(JM)=0.
DO K=1,3
KJ=J-2+K
D(JM)=D(JM)+EMX(K)*TN(KJ)+
$ ELX(K)*S*(1.-BET)*TN(KJ)
END DO
END DO
D(1)=D(1)+BET*S*TN(1)
D(JMAF)=D(JMAF)+BET*S*TN(JMAX)
IF(N.EQ.1)CALL LU(A,JMAF)
CALL LUSOLVE(D,DUM,A,JMAF)
C Scale up
DO J=2,JMAP
TD(J)=100.*TN(J)
END DO
IF(IPR.EQ.1) THEN
WRITE(2,17)T,(TD(J),J=1,JMAX)
17 FORMAT( T=,F5.2, TD=,100F8.2)
DO J=1, JMAX
WRITE(3,FMT=(11F8.2)) TD(J)
END DO
WRITE(3,*)
END IF
IF(N.GE.NMAX)GOTO 18
IF(T.LT.TMAX)GOTO 11
18 CALL INIT(JMAX,MAXEX,DELX,PI,ALPH,T,TE)
SUM=0.
DO J=1,JMAX
DMP=TE(J)-TD(J)
SUM=SUM+DMP*DMP
END DO
IF(IPR.NE.1)WRITE(2,17)T,(TD(J),J=1,JMAX)
WRITE(2,20)T,(TE(J),J=1,JMAX)
20 FORMAT(/, T=,F5.2, TE=,100F8.2,//)
CLOSE(1)
2.5. Example 1: Solution of 1D diffusion equation with DBC 17
CLOSE(2)
CLOSE(3)
STOP
END
SUBROUTINE INIT(JMAX,MAXEX,DELX,PI,ALPH,T,TE)
C C.A.J. Fletcher,
C Computational Techniques in fluid dynamics, vol.1
DIMENSION X(141),TE(141)
DO J=1,JMAX
AJ=J-1
X(J)=DELX*AJ
TE(J)=100.
DO M=1,MAXEX
AM=M
DAM=(2.*AM-1.)
DXM=DAM*PI*X(J)
DTM=-ALPH*DAM*DAM*PI*PI*T
DTM=EXP(DTM)
IF(DTM.LT.1.0E-10)GOTO 2
TE(J)=TE(J)-400./DAM/PI*SIN(DXM)*DTM
END DO
2 END DO
RETURN
END
SUBROUTINE LU(B,N)
C C.A.J. Fletcher,
C Computational Techniques in fluid dynamics, vol.1
DIMENSION B(5,165)
20 Chapter 2. Solution of the one dimensional diffusion equation
C TRIDIAGONAL SYSTEM
NP=N-1
DO J=1,NP
JP=J+1
B(2,JP)=B(2,JP)/B(3,J)
B(3,JP)=B(3,JP)-B(2,JP)*B(4,J)
END DO
RETURN
END
SUBROUTINE LUSOLVE(R,X,B,N)
C C.A.J. Fletcher,
C Computational Techniques in fluid dynamics, vol.1
DIMENSION R(165),X(165),B(5,165)
2.5. Example 1: Solution of 1D diffusion equation with DBC 21
C TRIDIAGONAL SYSTEM
NP = N-1
DO J=1, NP
JP=J+1
R(JP)=R(JP)-B(2,JP)*R(J)
END DO
X(N)=R(N)/B(3,N)
DO J=1,NP
JA=N-J
X(JA)=(R(JA)-B(4,JA)*X(JA+1))/B(3,JA)
END DO
RETURN
END
An example of the solution of the problem based on the initial conditions Eq. (38) is Fig.1. A second example
for different initial conditions,
is shown in Fig.2
22 Chapter 2. Solution of the one dimensional diffusion equation
100
90
80
70
60
50
40
30
20
10
90
80
70
60
0 5 10 50
15 20 40
25 30 30
20
35 40 10
45 50 0
Figure 2.1: Solution of the 1D diffusion equation for initial conditions Eq. (38), a = 0.01,t0 = 4.5, jmax = 51, s = 1,
t = 0.25 and DBC, Eq. (37).
100
50
0
-50
-100
100
0 5 10 50
15 20
25 30
35 40
45 50 0
Figure 2.2: Solution of the 1D diffusion equation for initial conditions Eq. (39), a = 0.01, t0 = 4.5, jmax = 51, s = 1,
t = 0.25 and DBC, Eq. (37).
2.6. Example 2: Solution of 1D diffusion equation with NBC at one end 23
In this example the one dimensional diffusion equation is solved for NBC at one end and DBC at the other end,
u
= 2 2 sin(0.05) exp(0.5a 2 t) at x = 0.1 (2.40)
x
u=2 at x = 1 (2.41)
PROGRAM DIFIM
C C.A.J. Fletcher,
C Computational Techniques in fluid dynamics, vol.1
C and Srininas and Fletcher -solution manual
OPEN(1,FILE=~/programs/Crank-Nicolson/c-n-neumann.dat,
$ STATUS=UNKNOWN)
OPEN(2,FILE=~/programs/Crank-Nicolson/c-n-neumann.out,
$ STATUS=UNKNOWN)
OPEN(3,FILE=~/programs/Crank-Nicolson/dif-neumann.dat,
$ STATUS=UNKNOWN)
READ(1,*)IPR,JMAX,MAXEX,NMAX,ALPH,S,TMAX,TST
PI=3.1415927
JMAP=JMAX-1
AJM=JMAP
XINT=0.1
XEND=1.
DELX=(XEND-XINT)/(JMAX-1)
DELT=DELX*DELX*S/ALPH
WRITE(2,4)
WRITE(2,2)JMAX,MAXEX,NMAX,TMAX,TST
2 FORMAT( JMAX=,I5, MAXEX=,I5, NMAX=,I5,
$ TMAX=,F8.2, TST=,F5.2)
WRITE(2,3)S,ALPH,DELT,DELX
3 FORMAT( S=,F5.3, ALPH=, E10.3, DELT=,E10.3,
$ DELX=,E10.3,//)
4 FORMAT( DIFFUSION EQUATION: Crank-Nicolson,//)
C IMPLICIT PARAMETERS
C PARAMETER BETA FOR C-N FORMULA
BET=0.5
ELX(3)=1.
JMAF=JMAX-2
IF(IPR.EQ.1) THEN
WRITE(2,17)T,(TE(J),J=1,JMAX)
WRITE(2,*)
END IF
DO J=2, JMAP
TN(J)=TE(J)
END DO
IF(N.GT.1)GOTO 12
26 Chapter 2. Solution of the one dimensional diffusion equation
A(1,JM)=0.
A(2,JM)=AD
A(3,JM)=BD
A(4,JM)=CD
A(5,JM)=0.
12 D(JM)=0.
2.6. Example 2: Solution of 1D diffusion equation with NBC at one end 27
DO K=1,3
KJ=J-2+K
C CRANK-NICOLSON FORMULA FOR VECTOR D
D(JM)=D(JM)+EMX(K)*TN(KJ)+
$ ELX(K)*S*(1.-BET)*TN(KJ)
END DO
END DO
DO J=1,JMAP
TN(J)=DUM(J)
END DO
DO J=1,JMAP
TD(J)=TN(J)
END DO
C T=T+DELT
IND=IND+1
IF(IPR.EQ.1) THEN
IF(IND.EQ.50) THEN
WRITE(2,17)T,(TD(J),J=1,JMAX)
17 FORMAT( T=,F5.2, TD=,100F8.2)
DO J=1, JMAX
WRITE(3,FMT=(11F8.2)) TD(J)
END DO
WRITE(3,*)
IND=0
END IF
END IF
The initial condition for this problem is in subroutine INIT, whereas LU and LUSOLVE subroutines remain the
same.
SUBROUTINE INIT(JMAX,MAXEX,DELX,PI,ALPH,T,TE)
C EXACT SOLUTION OF THE TRANSIENT HEAT
C CONDUCTION PROBLEM
DIMENSION X(141),TE(141)
XINT=0.1
XEND=1.
DO J=1,JMAX
X(J)=XINT+(XEND-XINT)*(J-1)/(JMAX-1)
TE(J)=2.*X(J)+4.*COS(PI*X(J)/2.)*EXP(-ALPH*PI*PI*T/4.)
END DO
RETURN
END
3.5
3
2.5
2
1.5
1
0.5
0
15
0 10
5
10 5
15 0
20
Figure 2.3: Solution of the 1D diffusion equation for the bounday conditions, Eq. (40-41). a = 0.01, t = 1.65, s = 1 and
jmax = 21.
Chapter 3
sun+1 n+1
j1 +(1+2s)uj sun+1 n n n n n n n
j+1 = uj 2(1)suj +(1)suj1 +(1)suj+1 +tuj (1uj )(uj )
(3.4)
2
where s = t/x , j = 2, 3, ..., jmax 1, and jmax is the maximum number of nodal points in the spatial
coordinate. Equation (46) is very similar to Eq. (3). The only difference lies on the right side, i.e. the vector of the
known values of the resulting algebraic system.
In the case of DBC, the vector u and the matrix A are exactly the same as in Eqs.(12) and (13). The vector d though
has to be modified. That is,
un2 2(1 )sun2 + (1 )sun1 + (1 )sun3 + sun+1
1 + tf (un2 , )
un3 2(1 )sun3 + (1 )sun2 + (1 )sun4 + tf (un3 , )
...
d= .
n n
uj 2(1 )suj + (1 )suj1 + (1 )sunj+1 + tf (unj , )
n
...
unjmax 1 2(1 )sunjmax 1 + (1 )sunjmax 2 + (1 )sunjmax + sun+1 n
jmax + tf (ujmax 1 , )
(3.5)
31
32 Chapter 3. Solution of a one dimensional reaction-diffusion equation
The resulting system remains tridiagonal and can be solved with Thomas algorithm.
In the case of NBC the form of the vector u and the matrix A remain as in Eqs.(28) and (29). The vector d has to
be modified. The modified version of this vector is,
un1 2sxcn+1
1 + tf (un1 , )
...
d = uj 2(1 )suj + (1 )suj1 + (1 )sunj+1 + tf (unj , )
n n n . (3.6)
...
n+1
n
ujmax + 2sxc2 + tf (unjmax , )
In this case also, the resulting system is tridiagonal and can be solved with Thomas algorithm.
In this example, we solve Eq. (43) with the following boundary and initial conditions,
u
= 0, x = 0, 10 (3.7)
x
(
1 if 0 x < 5
u(x, 0) = (3.8)
0 if 5 x 10
An example for = 0.3 is shown in Fig.4.
PROGRAM RD1D
C C.A.J. Fletcher,
C Computational Techniques in fluid dynamics, vol.1
C and Srinivas and Fletcher -solution manual
C Based on P.Grindrod
C The theory and applications of reaction-diffusion
C equations: Patterns and waves, p.15
DIMENSION DUM(165),TE(165),A(5,165),D(165)
DIMENSION ELX(3),EMX(3),TN(165)
3.3. Example 3: Solution of 1D reaction-diffusion equation for NBC 33
OPEN(1,FILE=~/programs/RD/rd1D-nbc.dat,
$ STATUS=UNKNOWN)
JMAX=51
XINT=0.
XEND=10.
DELX=(XEND-XINT)/(JMAX-1)
DELT=0.004
NMAX=10000
ALPH=1.
S=ALPH*DELT/(DELX**2)
TMAX=10000.
TST=0.
JMAP=JMAX-1
PMU = 0.3
C IMPLICIT PARAMETERS
T = TST
CALL INIT(JMAX,DELX,ALPH,T,TE)
DO J=1,JMAX
WRITE(1,FMT=(F15.5)) TE(J)
END DO
WRITE(1,*)
DO J=1, JMAX
TN(J)=TE(J)
END DO
N=0
11 N=N+1
DO J=2,JMAP
IF(N.GT.1)GOTO 12
C
C |A(3,1) A(4,1) |
C |A(2,2) A(3,2) A(4,2) |
C | A(2,3) A(3,3) A(4,3) |
C |............................................................|
C | A(2,i) A(3,i) A(4,i) |
C |............................................................|
C | A(2,N) A(3,N)|
C
C where,
C
C A(3,1) = 1.+2.*S
C A(4,1) = -2.*S
C
C A(2,N) = -2.*S
C A(3,N) = 1.+2.*S
C
C and for i=2,3,...,N-1
C
C A(2,i) = -beta*s
C A(3,i) = 1+2*beta*s
C A(4,i) = -beta*s
A(1,JM)=0.
A(2,JM)=AD
A(3,JM)=BD
A(4,JM)=CD
A(5,JM)=0.
A(1,1)=0.
A(2,1)=0.
A(3,1)=1.+2.*S
A(4,1)=-2.*S
A(5,1)=0.
36 Chapter 3. Solution of a one dimensional reaction-diffusion equation
A(1,JMAX)=0.
A(2,JMAX)=-2.*S
A(3,JMAX)=1.+2.*S
A(4,JMAX)=0.
A(5,JMAX)=0.
12 D(JM)=0.
DO K=1,3
KJ=J-2+K
D(JM)=D(JM)+EMX(K)*TN(KJ)+
$ ELX(K)*S*(1.-BET)*TN(KJ)
$ +DELT*EMX(K)*(TN(KJ)*(1.-TN(KJ))*(TN(KJ)-PMU))
END DO
END DO
T=T+DELT
CN=0.
D(1)=TN(1)-2.*S*DELX*CN
$ +DELT*TN(1)*(1.-TN(1))*(TN(1)-PMU)
D(JMAX)=TN(JMAX)+2.*S*DELX*CN
$ +DELT*TN(JMAX)*(1.-TN(JMAX))*(TN(JMAX)-PMU)
IF(N.EQ.1)CALL LU(A,JMAX)
CALL LUSOLVE(D,DUM,A,JMAX)
DO J=1,JMAX
TN(J)=DUM(J)
END DO
IND=IND+1
IF(IND.EQ.100) THEN
DO J=1, JMAX
WRITE(1,FMT=(F15.8)) TN(J)
END DO
WRITE(1,*)
IND=0
END IF
IF(N.GE.NMAX)GOTO 18
IF(T.LT.TMAX)GOTO 11
18 CLOSE(1)
STOP
END
0.5
0
100
0 5 10 50
15 20
25 30
35 40
45 50 0
Figure 3.1: Solution of Eq. (43) for = 0.3, jmax = 51, t = 0.004 and Neumann boundary conditions at both ends.
SUBROUTINE INIT(JMAX,DELX,ALPH,T,TE)
C INITIAL CONDITION FOR THE 1D RD PROBLEM
C WITH DBC
DIMENSION TE(165)
XINT=0.
XEND=10.
DO J=1,(JMAX-1)/2
TE(J) = 1.
END DO
DO J=1+(JMAX-1)/2, JMAX
TE(J)=0.
END DO
RETURN
END
Chapter 4
In the case of systems of coupled reaction-diffusion equations a special strategy must be followed. The resulting
equations must be solved for each variable at every node simultaneously and thus Thomas algorithm cannot be used
as it is.
Systems of reaction-diffusion systems lead to systems of algebraic equations which are block tridiagonal. Thus,
once Thomas algorithm is modified for block tridiagonal system, the general concepts for the solution of these
problems remain the same.
In this chapter will be focus on a specific working example. Let us consider the system,
u 2u
= + (1 u2 v 2 )(u 3v), (4.1)
t x2
v 2v
= + (1 u2 v 2 )(3u + v). (4.2)
t x2
un+1
j unj unj1 2unj + unj+1 un+1
j1 2uj
n+1
+ un+1
j+1
= + + [1 (unj )2 (vjn )2 ](unj 3vjn ), (4.3)
t 2x2 2x2
vjn+1 vjn n
vj1 2vjn + vj+1
n n+1
vj1 n+1
2vjn+1 + vj+1
= + + [1 (unj )2 (vjn )2 ](3unj + vjn ). (4.4)
t 2x2 2x2
n 2 n 2
0.5sun+1 n+1
j1 + (1 + s)uj 0.5sun+1 n n n n n n
j+1 = uj suj + 0.5suj1 + 0.5suj+1 + t[1 (uj ) (vj ) ](uj 3vj ),
(4.5)
n+1
0.5svj1 + (1 + s)vjn+1 n+1
0.5svj+1 = vjn svjn + n
0.5svj1 + n
0.5svj+1 + t[1 (unj )2 (vjn )2 ](3unj + vjn ),
(4.6)
where s = t/x2 , j = 2, 3, ..., jmax 1, and jmax is the maximum number of nodal points in the spatial
coordinate.
Let us also assume Neumann boundary conditions for both variables at one end and Dirichelt boundary condi-
39
40 Chapter 4. Solution of systems of reaction-diffusion equations with one spatial coordinate
(1 + 2s)un+1
1 2sun+1
2 = un1 + t[1 (un1 )2 (v1n )2 )](un1 3v1n ). (4.11)
Equation (61-62) will be the first two lines of the system of algebraic equations. The last two lines are modified
according to the DBC, i.e.,
0.5sun+1 n+1 n n n n
jmax 2 + (1 + s)ujmax 1 = ujmax 1 suj + 0.5sujmax 2 + 0.5sujmax
0.5svjn+1
max 2
+ (1 + s)vjn+1
max 1
= vjnmax 1 svjn + 0.5svjnmax 2 + 0.5svjnmax
+ t[1 (unjmax 1 )2 (vjnmax 1 )2 ](3unjmax 1 + vjnmax 1 ) + 0.5svjmax .
(4.14)
The resulting algebraic system is written in the form of Eq. (11), where,
u = (un+1
1 , v1n+1 , un+1
2 , v2n+1 , . . . , un+1 n+1
jmax 1 , vjmax 1 ) (4.15)
1 + 2s 0 2s 0 ... ... ... ... ... ... ... ...
0 1 + 2s 0 2s 0 ... ... ... ... ... ... ...
0.5s
0 1+s 0 0.5s 0 ... ... ... ... ... ...
0 0.5s 0 1+s 0 0.5s ... ... ... ... ... ...
A= 0 0 0.5s 0 1+s 0 0.5s 0 ... ... ... ...
0 0 0 0.5s 0 1+s 0 0.5s ... ... ... ...
...
0 0 0 0 0 0 0 0 0.5s 0 1+s 0
0 0 0 0 0 0 0 0 0 0.5s 0 1+s
(4.16)
4.1. Example 4: Solution of a system of reaction-diffusion equations in one spatial coordinate 41
and
From Eq. (66) we observe that the system is block tridiagonal. In order to solve the resulting system, we
introduce a modification of Thomas algorithm, as shown in Example 4.
Equations (51-52) are solved under the conditions Eq. (57-58) and also,
u = v = 0.01, at x = 0, t = 0 (4.18)
The main iteration procedures are located in RDSYS. The driver for the modified Thomas algorithm is BLTRI
and FACT and SOLVE are the subroutines for LU factorization. A solution is shown in Fig. 5.
PROGRAM RDSYS
DIMENSION TE(250,5),A(250,5,5),D(250,5)
DIMENSION B(250,5,5),C(250,5,5),X(250,5)
DIMENSION ELX(3),EMX(3),TN(250,5)
OPEN(1,FILE=~/programs/RD/rd-sys-graph.dat,
$ STATUS=UNKNOWN)
0.5
-0.5
-1
15
0 10
50
100 5
150
200 0
Figure 4.1: Solution of Eqs.(51-52), under the conditions Eqs.(57),(58) and (68). jmax = 250, t = 0.004, x = 0.4. The u
variable is shown.
IPR=1
JMAX=250.
MAXEX=10
NMAX=10000
ALPH=1.
DELT=0.004
DELX=0.4
S=ALPH*DELT/(DELX**2)
TMAX=10000.
TST=0.
JMAP=JMAX-1
C IMPLICIT PARAMETERS
4.1. Example 4: Solution of a system of reaction-diffusion equations in one spatial coordinate 43
T = TST
CALL INIT(JMAX,MAXEX,DELX,PI,ALPH,T,TE)
DO J=1, JMAX
TN(J,1)=TE(J,1)
TN(J,2)=TE(J,2)
END DO
N=0
11 N=N+1
DO J=2,JMAP
JM=J
44 Chapter 4. Solution of systems of reaction-diffusion equations with one spatial coordinate
IF(N.GT.1)GOTO 12
B(JM,1,1)=BD
B(JM,1,2)=0.
B(JM,2,1)=0.
B(JM,2,2)=BD
C(JM,1,1)=CD
C(JM,1,2)=0.
C(JM,2,1)=0.
C(JM,2,2)=CD
A(JM,1,1)=AD
A(JM,1,2)=0.
A(JM,2,1)=0.
A(JM,2,2)=AD
C(1,1,1)=-2.*S
C(1,1,2)=0.
C(1,2,1)=0.
C(1,2,2)=-2.*S
12 D(JM,1)=0.
D(JM,2)=0.
D(JM,1)= TN(JM,1)-2.*(1.-BET)*S*TN(JM,1)+
$ (1.-BET)*S*TN(JM-1,1)+
4.1. Example 4: Solution of a system of reaction-diffusion equations in one spatial coordinate 45
$ (1.-BET)*S*TN(JM+1,1)+
$ DELT*(1.-(TN(JM,1)**2)-(TN(JM,2)**2))*
$ (TN(JM,1)-3.*TN(JM,2))
D(JM,2)= TN(JM,2)-2.*(1.-BET)*S*TN(JM,2)+
$ (1.-BET)*S*TN(JM-1,2)+
$ (1.-BET)*S*TN(JM+1,2)+
$ DELT*(1.-(TN(JM,1)**2)-(TN(JM,2)**2))*
$ (3.*TN(JM,1)+TN(JM,2))
END DO
T=T+DELT
CN=0.
D(1,1)=TN(1,1)-2.*S*DELX*CN
$ +DELT*(1.-TN(1,1)**2-TN(1,2)**2)*
$ (TN(1,1)-3.*TN(1,2))
D(1,2)=TN(1,2)-2.*S*DELX*CN
$ +DELT*(1.-TN(1,1)**2-TN(1,2)**2)*
$ (3.*TN(1,1)-TN(1,2))
D(JMAP,1)=D(JMAP,1)+BET*S*TN(JMAX,1)
$ +DELT*(1.-TN(JMAP,1)**2-TN(JMAP,2)**2)*
$ (TN(JMAP,1)-3.*TN(JMAP,2))
D(JMAP,2)=D(JMAP,2)+BET*S*TN(JMAX,2)
$ +DELT*(1.-TN(JMAP,1)**2-TN(JMAP,2)**2)*
$ (3.*TN(JMAP,1)-TN(JMAP,2))
CALL BLTRI(JMAP,A,B,C,D,X)
DO J=1,JMAP
TN(J,1)=X(J,1)
TN(J,2)=X(J,2)
END DO
IF (N.GT.1000) THEN
IND=IND+1
IF(IND.EQ.500) THEN
DO J=1, JMAX
WRITE(1,FMT=(F15.8)) TN(J,1)
END DO
WRITE(1,*)
IND=0
END IF
END IF
IF(N.GE.NMAX)GOTO 18
IF(T.LT.TMAX)GOTO 11
18 CLOSE(1)
STOP
END
SUBROUTINE INIT(JMAX,MAXEX,DELX,PI,ALPH,T,TE)
C EXACT SOLUTION OF THE TRANSIENT HEAT
C CONDUCTION PROBLEM
DIMENSION X(250),TE(250,5)
XINT=0.
XEND=10.
4.1. Example 4: Solution of a system of reaction-diffusion equations in one spatial coordinate 47
TE(1,1)=0.01
TE(1,2)=0.01
DO J=2,JMAX
C AJ=J-1
C X(J)=DELX*AJ
C TE(J) = 0.01*SIN(2.*PI*0.05*X(J))
TE(J,1)=0.
TE(J,2)=0.
END DO
RETURN
END
SUBROUTINE BLTRI(N,A,B,C,D,X)
PARAMETER(NBL=2)
DIMENSION A(250,5,5),B(250,5,5),C(250,5,5)
DIMENSION D(250,5),DD(5),X(250,5),CD(250,5,6)
DIMENSION BD(250,50),JPVT(250),R(250)
NBP=NBL+1
DO K=1,NBP
DO J=1,NBL
CD(1,J,K)=0.
END DO
END DO
C FORWARD SWEEP
DO L=1,N
48 Chapter 4. Solution of systems of reaction-diffusion equations with one spatial coordinate
DO J=1,NBL
DD(J)=D(L,J)
DO K=1,NBL
BD(J,K)=B(L,J,K)
DO I=1,NBL
IF(K.EQ.NBL) DD(J)=DD(J)-A(L,J,I)*CD(L,I,NBP)
BD(J,K)=BD(J,K)-A(L,J,I)*CD(L,I,K)
END DO
C PRINT *,BD(J,K)
END DO
END DO
CALL FACT(NBL,BD,JPVT)
KST=1
IF(L.EQ.N) KST=NBP
DO K=KST,NBP
DO J=1,NBL
IF(K.LE.NBL) R(J)=C(L,J,K)
IF(K.EQ.NBP) R(J)=DD(J)
END DO
CALL SOLVE(NBL,BD,JPVT,R)
DO J=1,NBL
CD(L+1,J,K)=R(J)
END DO
END DO
END DO
C BACKWARD SWEEP
DO J=1,NBL
X(N,J)=CD(N+1,J,NBP)
END DO
DO L=2,N
LA=N+1-L
DO J=1,NBL
4.1. Example 4: Solution of a system of reaction-diffusion equations in one spatial coordinate 49
X(LA,J)=CD(LA+1,J,NBP)
DO K=1,NBL
X(LA,J)=X(LA,J)-CD(LA+1,J,K)*X(LA+1,K)
END DO
END DO
END DO
RETURN
END
SUBROUTINE FACT(N,A,JPVT)
C FACTORISES A INTO PERMUTED LU SO THAN
C PERM*A=L*U
C JPVT(K) GIVES INDEX OF KTH PIVOT ROW
C SETS JPVT(N)=-1 IF ZERO PIVOT OCCURS
DIMENSION A(250,50),JPVT(250)
NM=N-1
DO K=1,NM
KP=K+1
C SELECT PIVOT
L=K
DO I=KP,N
IF(ABS(A(I,K)).GT.ABS(A(L,K)))L=I
END DO
JPVT(K)=L
S=A(L,K)
A(L,K)=A(K,K)
A(K,K)=S
C CALULATE MULTIPLIERS
DO I=KP,N
A(I,K)=-A(I,K)/S
END DO
SUBROUTINE SOLVE(N,A,JPVT,B)
C SOLVES LINEAR SYSTEM A*X=B
C ASSUMES A IS FACTORISED INTO L.U FORM
C RETURNS THE SOLUTION, X, IN B
DIMENSION A(250,5),JPVT(250),B(250)
C FORWARD ELIMINATION
NM=N-1
DO K=1,NM
KP=K+1
L=JPVT(K)
S=B(L)
B(L)=B(K)
B(K)= S
DO I=KP,N
B(I)=B(I)+A(I,K)*S
4.1. Example 4: Solution of a system of reaction-diffusion equations in one spatial coordinate 51
END DO
END DO
C BACK SUBSTITUTION
DO KA=1,NM
KM=N-KA
K=KM+1
B(K)=B(K)/A(K,K)
S=-B(K)
DO I=1,KM
B(I)=B(I)+A(I,K)*S
END DO
END DO
B(1)=B(1)/A(1,1)
RETURN
END
52 Chapter 4. Solution of systems of reaction-diffusion equations with one spatial coordinate
Chapter 5
2 v
=d 2 + uf (), (5.1)
t x r
u 2u
= + 1 u uf (), (5.2)
t x2
f () = 3 2 + . (5.3)
This model consides a potentiostatic situation where is the surface potential, u the surface concentration, v the
applied potential, r the ohmic resistance, 0 < 1 the capacitance of the double layer and d > 1 the screening of
the potential due to the supporting electrolyte. Also f () is the kinetic expression of the faradaic current.
Equations (69-71) are of the reaction-diffusion type and thus can be solved with the method presented in Sec.
3. Only some minor modifications are necessary. Thus, Eq. (69) is written,
2 hv i
= a 2 + 1 uf () , (5.4)
t x r
where a = d/. Equations (72-73) is descritized similarly to Eqs.(54-55),
n+1
j nj nj1 2nj + nj+1 n+1 n+1
j1 2j + n+1
j+1 1
h v n
j n n
i
=a + a + u j f ( j ) , (5.5)
t 2x2 2x2 r
n+1 n+1 n+1 n+1
uj unj unj1 2unj + unj+1 uj1 2uj + uj+1
= + + 1 unj unj f (nj ). (5.6)
t 2x2 2x2
Thus, the following algorithm holds,
h v n i
j
0.5s1 n+1 n+1
j1 + (1 + s1 )j 0.5s1 n+1 n n n n
j+1 = j s1 j + 0.5s1 j1 + 0.5s1 j+1 + t
1
unj f (nj ) ,
r
(5.7)
0.5s2 un+1 n+1
j1 + (1 + s2 )uj 0.5s2 un+1 n n n n n n n
j+1 = uj s2 uj + 0.5s2 uj1 + 0.5s2 uj+1 + t[1 uj uj f (j )],
(5.8)
53
54 Chapter 5. Solution of systems of reaction-migration, reaction-diffusion equations
where s1 = at/x2 and s2 = t/x2 . Now, following a procedure exactly similar to Sec. 3 we observe that
the solution method will be exactly the same keeping in mind that the vector d has to be modified and that the s in
matrix A are altenating by row. Thus, for Neumann boundary conditions we will have,
u = (n+1
1 , un+1
1 , n+1
2 , un+1
2 , . . . , n+1 n+1
jmax , ujmax ) (5.9)
1 + 2s1 0 2s1 0 ... ... ... ... ... ... ... ...
0 1 + 2s2 0 2s2 0 ... ... ... ... ... ... ...
0.5s
1 0 1 + s1 0 0.5s1 0 ... ... ... ... ... ...
0 0.5s2 0 1 + s2 0 0.5s2 ... ... ... ... ... ...
A= 0 0 0.5s1 0 1 + s1 0 0.5s1 0 ... ... ... ...
0 0 0 0.5s2 0 1 + s2 0 0.5s2 ... ... ... ...
...
0 0 0 0 0 0 0 0 2s1 0 1 + s1 0
0 0 0 0 0 0 0 0 0 0.5s2 0 1 + s2
(5.10)
and
h i
vn
n1 + t1 r
1
un1 f (n1 )
un1 + t[1 un1 un1 f (n1 )]
...
vn
n h i
j snj + 0.5snj1 + 0.5snj+1 + t1 r j unj f (nj )
d= (5.11)
unj sunj + 0.5sunj1 + 0.5sunj+1 + t[1 unj unj f (nj )]
...
h n i
n 1 vjmax n n
j max
+ t r u j max
f ( j max
)
unjmax + t[1 unjmax unjmax f (njmax )]
The problem is stil block tridiagonal and can be solved with the methods of Sec.3.
5.1 Example 5: Solution of the reaction-diffusion / reaction- migration problem with Neumann boundary
conditions
In this example we solve Eqs.(69-71) for the excitable case for v = 1.9, d = 5, = 2, = 0.01 and = = 8.
The bounday conditions are,
u
= =0 (5.12)
x x
and = 1.823 and u = 0.898 for any x as an initial condition, expect of the point (0, 0) = 1.5. An example of
the excitable response is shown in Fig.6.
In Fig. 7 the nullclines of the system are presented and in Fig.8 one excitable pulse. The routines for the
computation are given below.
5.1. Example 5: Solution of the reaction-diffusion / reaction- migration problem with Neumann boundary conditions 55
1.5
0.5
0 25
20
0 15
50 10
100
150 5
200 0
2.5
1.5
0.5
0
0 0.2 0.4 0.6 0.8 1
1.8
1.6
1.4
1.2
0.8
0.6
0.4
0 50 100 150 200 250
PROGRAM RDE
DIMENSION TE(250,5),A(250,5,5),D(250,5)
DIMENSION B(250,5,5),C(250,5,5),X(250,5)
DIMENSION ELX(3),EMX(3),TN(250,5)
OPEN(1,FILE=~/programs/RDE/rd-graph1.dat,
$ STATUS=UNKNOWN)
OPEN(2,FILE=~/programs/RDE/rd-graph2.dat,
$ STATUS=UNKNOWN)
OPEN(3,FILE=~/programs/RDE/null.dat,
$ STATUS=UNKNOWN)
C S = ALPH*DT/DX**2
C TMAX maximum time
C TST initial time
EPSI=0.01
IPR=1
JMAX=250
MAXEX=10
NMAX=300
ALPH1=5./EPSI
ALPH2=1.
DELT=0.004
DELX=0.4
S1=ALPH1*DELT/(DELX**2)
S2=ALPH2*DELT/(DELX**2)
TMAX=1000.
TST=0.
VAPP=1.9
C -----------
C EXCITABLE
C -----------
R=0.75
C -----------
C OSCILLATORY
C -----------
C R=1.
AC=2.
BC=8.
CC=8.
CALL NULL(AC,BC,CC,R,VAPP)
JMAP=JMAX-1
58 Chapter 5. Solution of systems of reaction-migration, reaction-diffusion equations
C IMPLICIT PARAMETERS
T = TST
CALL INIT(JMAX,MAXEX,DELX,PI,ALPH,T,TE)
DO J=1, JMAX
TN(J,1)=TE(J,1)
TN(J,2)=TE(J,2)
END DO
N=0
11 N=N+1
5.1. Example 5: Solution of the reaction-diffusion / reaction- migration problem with Neumann boundary conditions 59
DO J=2,JMAP
JM=J
IF(N.GT.1)GOTO 12
B(JM,1,1)=BD1
B(JM,1,2)=0.
B(JM,2,1)=0.
B(JM,2,2)=BD2
C(JM,1,1)=CD1
C(JM,1,2)=0.
C(JM,2,1)=0.
C(JM,2,2)=CD2
A(JM,1,1)=AD1
A(JM,1,2)=0.
A(JM,2,1)=0.
A(JM,2,2)=AD2
C(1,1,1)=-2.*S1
C(1,1,2)=0.
C(1,2,1)=0.
C(1,2,2)=-2.*S2
B(JMAX,2,1)=0.
B(JMAX,2,2)=1.+2.*S2
A(JMAX,1,1)=-2.*S1
A(JMAX,1,2)=0.
A(JMAX,2,1)=0.
A(JMAX,2,2)=-2.*S2
12 D(JM,1)=0.
D(JM,2)=0.
D(JM,1)= TN(JM,1)-2.*(1.-BET)*S1*TN(JM,1)+
$ (1.-BET)*S1*TN(JM-1,1)+
$ (1.-BET)*S1*TN(JM+1,1)+
$ (DELT/EPSI)*((VAPP-TN(JM,1))/R-
$ TN(JM,2)*(AC*TN(JM,1)**3-
$ BC*TN(JM,1)**2+CC*TN(JM,1)))
D(JM,2)= TN(JM,2)-2.*(1.-BET)*S2*TN(JM,2)+
$ (1.-BET)*S2*TN(JM-1,2)+
$ (1.-BET)*S2*TN(JM+1,2)+
$ DELT*(1.-TN(JM,2)-
$ TN(JM,2)*(AC*TN(JM,1)**3-
$ BC*TN(JM,1)**2+CC*TN(JM,1)))
END DO
T=T+DELT
CN=0.
5.1. Example 5: Solution of the reaction-diffusion / reaction- migration problem with Neumann boundary conditions 61
D(1,1)=TN(1,1)-2.*S1*DELX*CN+
$ (DELT/EPSI)*((VAPP-TN(1,1))/R-
$ TN(1,2)*(AC*TN(1,1)**3-
$ BC*TN(1,1)**2+CC*TN(1,1)))
D(1,2)=TN(1,2)-2.*S2*DELX*CN+
$ DELT*(1.-TN(1,2)-
$ TN(1,2)*(AC*TN(1,1)**3-
$ BC*TN(1,1)**2+CC*TN(1,1)))
D(JMAX,1)=TN(JMAX,1)-2.*S1*DELX*CN+
$ (DELT/EPSI)*((VAPP-TN(JMAX,1))/R-
$ TN(JMAX,2)*(AC*TN(JMAX,1)**3-
$ BC*TN(JMAX,1)**2+CC*TN(JMAX,1)))
D(JMAX,2)=TN(JMAX,2)-2.*S2*DELX*CN+
$ DELT*(1.-TN(JMAX,2)-
$ TN(JMAX,2)*(AC*TN(JMAX,1)**3-
$ BC*TN(JMAX,1)**2+CC*TN(JMAX,1)))
C D(JMAP,1)=D(JMAP,1)+BET*S1*TN(JMAX,1)
C $ +(DELT/EPSI)*((VAPP-TN(JMAX,1))/R-
C $ TN(JMAX,2)*(AC*TN(JMAX,1)**3-
C $ BC*TN(JMAX,1)**2+CC*TN(JMAX,1)))
C D(JMAP,2)=D(JMAP,2)+BET*S2*TN(JMAX,2)
62 Chapter 5. Solution of systems of reaction-migration, reaction-diffusion equations
C $ +DELT*(1.-TN(JMAX,2)-
C $ TN(JMAX,2)*(AC*TN(JMAX,1)**3-
C $ BC*TN(JMAX,1)**2+CC*TN(JMAX,1)))
C JMAX NBC
DO J=1,JMAX
TN(J,1)=X(J,1)
TN(J,2)=X(J,2)
END DO
C IF (N.GT.6000) THEN
IND=IND+1
IF(IND.EQ.10) THEN
DO J=1, JMAX
WRITE(1,FMT=(F15.8)) TN(J,1)
WRITE(2,FMT=(F15.8)) TN(J,2)
END DO
WRITE(1,*)
WRITE(2,*)
IND=0
END IF
C END IF
IF(N.GE.NMAX)GOTO 18
IF(T.LT.TMAX)GOTO 11
5.1. Example 5: Solution of the reaction-diffusion / reaction- migration problem with Neumann boundary conditions 63
18 CLOSE(1)
CLOSE(2)
CLOSE(3)
STOP
END
64 Chapter 5. Solution of systems of reaction-migration, reaction-diffusion equations
Chapter 6
In the present chapter we will presend the ADI method for the solution of the diffusion equation in two spatial
dimensions.
Consider the 2D diffusion equation,
u 2u 2u
ax 2 ay 2 = 0. (6.1)
t x y
Introduce an FTCS sceme for the first time step n + 1/2, of the form,
un+1
j,k uj,k uj1,k 2uj,k + uj+1,k un+1 n+1 n+1
j,k1 2uj,k + uj,k+1
ax a y = 0, (6.5)
0.5t x2 y 2
65
66 Chapter 6. Solution of diffusion equation in two spatial dimensions
How about boundary conditions for the first half-step? Let us assume DBC, i.e,
un1,k = nk , (6.8)
unjmax ,k = bnk . (6.9)
From Eq. (83) and (86) we have for the boundary values and j = 1,
u1,k un1,k
ax Lxx u1,k ay Lyy un1,k = 0, (6.10)
0.5t
un+1
1,k u1,k
ax Lxx u1,k ay Lyy un+1
1,k = 0. (6.11)
0.5t
Substracting Eq. (91) from (92) and using Eqs.(88-89) we get,
Under these considerations, Eq. (84) is written in a matrix form of Eq. (11), where,
where u1,k and ujmax ,k in the first and last line of vector d are given by Eqs.(92-93).
Equation(87) is also written in matrix form where,
6.1.2 Example 6: Solution of the diffusion equation in two dimensions and Dirichlet boundary conditions
with ADI
In the present example we will solve diffusion equation in two spatial dimensions and Dirichlet boundary conditions,
An example of the solution at t = 16000 is shown in Fig.9 for jmax = kmax = 51, t = 40 and ax = ay =
5
10 .
The routines for the above computation are given below. The module ADI is the main iteration routine. Routine
ADIBC is the Dirichlet boundary conditions. Routines LU and LUSOLVE remain the same.
PROGRAM ADI
100
90
80
70
60
50
40
30
20
50
45
40
35
30
0 5 10 25
15 20 20
25 30 15
10
35 40 5
45 50 0
DIMENSION TE(141,141),A1(5,165),D1(165),D2(165)
DIMENSION TN(141,141),DUM(165),A2(5,165)
DIMENSION TH(141,141)
OPEN(1,FILE=~/programs/ADI/ADI-out.dat,
$ STATUS=UNKNOWN)
OPEN(2,FILE=~/programs/ADI/ADI-graph.mtv,
$ STATUS=UNKNOWN)
OPEN(3,FILE=~/programs/ADI/ADI-graph.dat,
$ STATUS=UNKNOWN)
JMAX=51
KMAX=51
ALPHX=1.E-05
ALPHY=1.E-05
TMAX=0.160E+05
NMAX=400.
C DELT=0.4E+04
DELT=40.
XMIN=0.
XMAX=1.
YMIN=0.
YMAX=1.
DELX=(XMAX-XMIN)/(JMAX-1)
DELY=(YMAX-YMIN)/(KMAX-1)
SX=ALPHX*DELT/(DELX**2)
SY=ALPHY*DELT/(DELY**2)
TST=0.
PI=3.1415927
JMAP=JMAX-1
KMAP=KMAX-1
JMAF=JMAX-2
KMAF=KMAX-2
T = TST
CALL INIT(JMAX,KMAX,ALPHX,DELX,DELY,T,TE)
DO J=2, JMAP
DO K=2,KMAP
TN(J,K)=TE(J,K)
70 Chapter 6. Solution of diffusion equation in two spatial dimensions
END DO
END DO
DO J=1,JMAX
TN(J,1)=BCY1(N,J,DELT,DELX)
TN(J,KMAX)=BCY2(N,J,DELT,DELX)
END DO
N=0
C FIRST HALF-STEP
C Solve the system of equations for the first
C half-step. Solve for j=2,3,...,jmax-1 for
C each k.
C Fixed k
11 DO K=2,KMAP
IF(N.GT.0)GOTO 12
A1(4,JM)=-0.5*SX
A1(5,JM)=0.
END DO
D1(1)=D1(1)+0.5*SX*TH(1,K)
D1(JMAF)=D1(JMAF)+0.5*SX*TH(JMAX,K)
END DO
N=N+1
C SECOND HALF-STEP
C Solve the system of equations for the second
C half-step. Solve for k=2,3,...,kmax-1 for
C each j.
C Fixed j
DO J=2,JMAP
DO K=2,KMAP
KM=K-1
IF(N.GT.1)GOTO 13
$ +(1.-SX)*TH(J,K)+0.5*TH(J+1,K)
END DO
D2(1)=D2(1)+0.5*SY*TN(J,1)
D2(KMAF)=D2(KMAF)+0.5*SY*TN(J,KMAX)
DO J=1,JMAX
TN(J,1)=BCY1(N,J,DELT,DELY)
TN(J,KMAX)=BCY2(N,J,DELT,DELY)
END DO
T=T+DELT
WRITE(1,FMT=(A10,G8.3)) Time:,T
DO K=1, KMAX
DO J=1,JMAX
WRITE(1,FMT=(F15.3,$)) TN(J,K)
END DO
WRITE(1,*)
END DO
IF(N.GE.NMAX)GOTO 18
IF(T.LT.TMAX)GOTO 11
18 WRITE(2,FMT=(A20)) $DATA=CONTOUR
WRITE(2,FMT=(A20)) %CONTSTYLE=2
WRITE(2,FMT=(A20,I5)) %NX=,JMAX
WRITE(2,FMT=(A20,I5)) %NY=,KMAX
DO K=1, KMAX
DO J=1,JMAX
WRITE(2,FMT=(F15.3,$)) TN(J,K)
WRITE(3,FMT=(F15.3)) TN(J,K)
END DO
WRITE(2,*)
WRITE(3,*)
END DO
CLOSE(1)
CLOSE(2)
CLOSE(3)
STOP
END
FUNCTION BCX1(N,K,DELT,DELY)
C Boundary condition at x=0, i.e. u(1,k)
T=N*DELT
Y=DELY*(K-1)
BCX1=20.+80.*Y
RETURN
END
FUNCTION BCX2(N,K,DELT,DELY)
C Boundary condition at x=1, i.e. u(jmax,k)
T=N*DELT
Y=DELY*(K-1)
PI=3.1415927
ALPHX=1.E-5
BCX2=20.
$ +80.*(Y-EXP(-0.5*ALPHX*PI**2*T)*SIN(0.5*PI*Y))
RETURN
76 Chapter 6. Solution of diffusion equation in two spatial dimensions
END
FUNCTION BCY1(N,J,DELT,DELX)
C Boundary condition at y=0, i.e. u(j,1)
T=N*DELT
X=DELX*(J-1)
BCY1=20.
RETURN
END
FUNCTION BCY2(N,J,DELT,DELX)
C Boundary condition at y=1, i.e. u(j,kmax)
T=N*DELT
X=DELX*(J-1)
PI=3.1415927
ALPHX=1.E-05
BCY2=20.
$ +80.*(1.-EXP(-0.5*ALPHX*PI**2*T)*SIN(0.5*PI*X))
RETURN
END
The Douglas-Gunn algorithm is a -forumation of the approximate factorization method which belongs to the class
of ADI techniques.
Let us introduce a two-level implicit scheme for Eq. (81),
un+1
j,k
(1 )(ax Lxx unj,k + ay Lyy unj,k ) (ax Lxx un+1 n+1
j,k + ay Lyy uj,k ) = 0 (6.24)
t
u n
un+1 n
j,k = uj,k + t +... (6.25)
t j,k
or,
unj,k
un+1 n
j,k = uj,k + t +... (6.26)
t
6.2. -formulation: Douglas-Gunn algorithm 77
Still, Eq. (108) cannot written in a tridiagonal form. In order to do so, we introduce an approximate factorization
of Eq. (108) of the form,
Actually, in Eq. (109) an additional term is introduced, i.e. 2 t2 ax ay Lxx Lyy un+1
j,k , which is second order
in time and thus it does not alter the accuracy of the scheme.
Now, Eq. (109) can be solved in two steps, each one evolving in a single spatial dimension,
The scheme Eqs.(110-111) is more economical that the Peaceman-Rachford algorithm since it computes the spatial
terms on the right hand only one time per time-step.
Intermediate boundary conditions of the Dirichlet type can be implemented very easely when the Douglas-Gunn
algorithm is used. Thus, from Eq. (111) we have,
and
t(ax Lxx + ay Lyy )un2,k + sx (1 tay Lyy )n+1
k
t(ax Lxx + ay Lyy )un3,k
...
d= (6.37)
t(ax Lxx + ay Lyy )unj,k
...
t(ax Lxx + ay Lyy )unjmax 1,k + sx (1 tay Lyy )bn+1
k
At the second stage (constant j), Eq. (11) is in a very simple form, where,
where, cnj and dnj are the boundary conditions on these boundaries.
6.2.2 Example 7: Solution of the diffusion equation in two dimensions and Dirichlet boundary conditions
with Douglas-Gunn algorithm
In this example we will solve the same problem as Example 6 (Sec. 5.1.2) by using the Douglas-Gunn algorithm.
The modules ADINIT, ADIBC, LU and LUSOLVE remain the same. The driver program where the main iterations
take place is given below. An example for jmax = kmax = 6 and the rest parameters the same as in Sec. 5.1.2 is
shown in Fig. 10 for t = 24000 and = 0.5.
PROGRAM ADIDELTA
100
90
80
70
60
50
40
30
20
5
4
3
0
1 2
2 1
3
4 0
5
Figure 6.2: Solution of the 2D diffusion equation with the Douglas-Gunn algorithm and Dirichlet boundary conditions
DIMENSION TE(141,141),A1(5,165),D1(165),D2(165)
DIMENSION TN(141,141),DUM(165),A2(5,165)
DIMENSION RR(165,165),DT(141,141)
OPEN(1,FILE=~/programs/ADI/ADI-out.dat,
$ STATUS=UNKNOWN)
OPEN(2,FILE=~/programs/ADI/ADI-graph.mtv,
$ STATUS=UNKNOWN)
OPEN(3,FILE=~/programs/ADI/ADI-graph.dat,
$ STATUS=UNKNOWN)
C SY = ALPHY*DT/DY**2
C TMAX maximum time
C TST initial time
JMAX=6
KMAX=6
ALPHX=1.E-05
ALPHY=1.E-05
C TMAX=0.160E+05
TMAX=24000.
NMAX=400.
DELT=0.4E+04
C DELT=40.
XMIN=0.
XMAX=1.
YMIN=0.
YMAX=1.
DELX=(XMAX-XMIN)/(JMAX-1)
DELY=(YMAX-YMIN)/(KMAX-1)
SX=ALPHX*DELT/(DELX**2)
SY=ALPHY*DELT/(DELY**2)
TST=0.
PI=3.1415927
JMAP=JMAX-1
KMAP=KMAX-1
JMAF=JMAX-2
KMAF=KMAX-2
T = TST
CALL INIT(JMAX,KMAX,ALPHX,DELX,DELY,T,TE)
DO J=2, JMAP
DO K=2,KMAP
TN(J,K)=TE(J,K)
END DO
6.2. -formulation: Douglas-Gunn algorithm 81
END DO
N=0
DO J=1,JMAX
TN(J,1)=BCY1(N,J,DELT,DELX)
TN(J,KMAX)=BCY2(N,J,DELT,DELX)
END DO
C FIRST HALF-STEP
C Solve the system of equations for the first
C half-step. Solve for j=2,3,...,jmax-1 for
C each k.
11 DO K=2,KMAP
DO J=2,JMAP
RR(J,K)=SX*(TN(J-1,K)-2.*TN(J,K)+TN(J+1,K))
$ +SY*(TN(J,K-1)-2.*TN(J,K)+TN(J,K+1))
END DO
END DO
C Fixed k
DO K=2,KMAP
JM=J-1
IF(N.GT.0)GOTO 12
END DO
D1(1)=D1(1)+0.5*SX*(-0.5*SY*DT(1,K-1)
$ +(1.+SY)*DT(1,K)-0.5*SY*DT(1,K+1))
D1(JMAF)=D1(JMAF)+0.5*SX*(-0.5*SY*DT(JMAX,K-1)
$ +(1.+SY)*DT(JMAX,K)-0.5*SY*DT(JMAX,K+1))
END DO
N=N+1
C SECOND HALF-STEP
C Solve the system of equations for the second
C half-step. Solve for k=2,3,...,kmax-1 for
C each j.
C Fixed j
DO J=2,JMAP
DO K=2,KMAP
KM=K-1
IF(N.GT.1)GOTO 13
A2(4,KM)=-0.5*SY
A2(5,KM)=0.
END DO
DT(J,1)= BCY1(N,J,DELT,DELX)-BCY1(N-1,J,DELT,DELX)
D2(1)=D2(1)+0.5*SY*DT(J,1)
DT(J,KMAX)=BCY2(N,J,DELT,DELX)-BCY2(N-1,J,DELT,DELX)
D2(KMAF)=D2(KMAF)+0.5*SY*DT(J,KMAX)
DO K=1,KMAX
TN(1,K)=BCX1(N,K,DELT,DELY)
TN(JMAX,K)=BCX2(N,K,DELT,DELY)
END DO
DO J=1,JMAX
TN(J,1)=BCY1(N,J,DELT,DELX)
TN(J,KMAX)=BCY2(N,J,DELT,DELX)
END DO
T=T+DELT
WRITE(1,FMT=(A10,G8.3)) Time:,T
DO K=1, KMAX
DO J=1,JMAX
WRITE(1,FMT=(F15.3,$)) TN(J,K)
END DO
WRITE(1,*)
END DO
SUMT=0.
END DO
RMST=SQRT(SUMT/((JMAX-2)*(KMAX-2)))
WRITE(1,FMT=(A8,F15.5)) ERROR:,RMST
IF(N.GE.NMAX)GOTO 18
IF(T.LT.TMAX)GOTO 11
DO K=1, KMAX
DO J=1,JMAX
WRITE(2,FMT=(F15.3,$)) TN(J,K)
WRITE(3,FMT=(F15.3)) TN(J,K)
END DO
WRITE(2,*)
WRITE(3,*)
END DO
CLOSE(1)
CLOSE(2)
CLOSE(3)
STOP
END
The case of Neumann boundary conditions must be treated with special care when the Douglas-Gunn or any other
ADI method is used. The problem arises from the fact the intermediate values u j,k they do not have nessesary
any meaning and thus must be expressed in terms of unj,k . Additionally, when a -formulation is used, boundary
conditions must be expressed in terms of unj,k .
6.2. -formulation: Douglas-Gunn algorithm 87
Let us assume Neumann boundary conditions on two sides of the rectangular region under consideration, i.e.,
u
= g(y, t), (6.41)
x x=1,y,t
u
= h(x, t). (6.42)
y x,y=1,t
In descrete form, Eq. (121-122) are written for j = jmax and k = kmax , respectively
unj+1,k unj1,k
= gkn , (6.43)
2x
unj,k+1 unj,k1
= hnj . (6.44)
2y
Taking the difference between two succesive time-steps we get,
where,
It can be observed that when the functions g and h are independent of time the ficticius points u j+1,k and
uj,k+1 are given directely from the inner points un+1 n+1
j1,k and uj,k1 . In this case also, the intemediate points
uj+1,k are equal with zero (prove ?).
Under these considerations, the equations during the first stage are Eqs.(110-111), except of the last line (j =
jmax ) which is,
(1 ax tLm n
xx )uj,k = t(ax Lxx ay Ly y)uj,k . (6.49)
Similarly on the second stage Eqs.(110-111) hold except of the last line (k = k max ) which will be,
n+1
(1 ay tLm
yy )uj,k = uj,k , (6.50)
6.2.4 Example 8: Solution of the diffusion equation in two dimensions with Dirichlet and Neumann bound-
ary conditions with Douglas-Gunn algorithm
In this example we will solve the same problem as Example 6 (Sec. 5.1.2) by using the Douglas-Gunn algorithm.
The only difference is that Eqs.(101) and (103) are replaced by,
u
= 0, (6.53)
x x=1,y,t
u
= 80. (6.54)
y x,y=1,t
88 Chapter 6. Solution of diffusion equation in two spatial dimensions
100
90
80
70
60
50
40
30
20
5
4
3
0
1 2
2 1
3
4 0
5
Figure 6.3: Solution of the 2D diffusion equation with the Douglas-Gunn algorithm and Dirichlet/Neumann boundary conditions
The modules ADINIT, LU and LUSOLVE remain the same. The driver program ADI-DELTA-N where the
main iterations take place is given below. The subroutine ADIBCN incorporates the new boundary conditions. An
example for jmax = kmax = 6 and the rest parameters the same as in Sec. 5.1.2 is shown in Fig. 11 for t = 24000
and = 0.5.
PROGRAM ADIDELTAN
DIMENSION TE(141,141),A1(5,165),D1(165),D2(165)
DIMENSION TN(141,141),DUM(165),A2(5,165)
DIMENSION RR(165,165),DT(141,141)
OPEN(1,FILE=~/programs/ADI/ADI-out.dat,
$ STATUS=UNKNOWN)
OPEN(2,FILE=~/programs/ADI/ADI-graph.mtv,
$ STATUS=UNKNOWN)
6.2. -formulation: Douglas-Gunn algorithm 89
OPEN(3,FILE=~/programs/ADI/ADI-graph.dat,
$ STATUS=UNKNOWN)
JMAX=6
KMAX=6
ALPHX=1.E-05
ALPHY=1.E-05
TMAX=24000.
NMAX=400.
DELT=0.4E+04
XMIN=0.
XMAX=1.
YMIN=0.
YMAX=1.
DELX=(XMAX-XMIN)/(JMAX-1)
DELY=(YMAX-YMIN)/(KMAX-1)
SX=ALPHX*DELT/(DELX**2)
SY=ALPHY*DELT/(DELY**2)
TST=0.
PI=3.1415927
JMAP=JMAX-1
KMAP=KMAX-1
JMAF=JMAX-2
KMAF=KMAX-2
T = TST
CALL INIT(JMAX,KMAX,ALPHX,DELX,DELY,T,TE)
DO J=1, JMAX
DO K=1,KMAX
TN(J,K)=TE(J,K)
END DO
END DO
N=0
C FIRST HALF-STEP
C Solve the system of equations for the first
C half-step. Solve for j=2,3,...,jmax for
C each k.
C Fixed k
DO K=2,KMAX
IF(N.GT.0)GOTO 12
A1(2,1)=0.
A1(2,JMAP)=-SX
A1(4,JMAP)=0.
END DO
D1(1)=D1(1)+0.5*SX*(-0.5*SY*DT(1,K-1)
$ +(1.+SY)*DT(1,K)-0.5*SY*DT(1,K+1))
D1(JMAP)=D1(JMAP)
92 Chapter 6. Solution of diffusion equation in two spatial dimensions
DT(JMAX+1,K)=DT(JMAX-1,K)
END DO
N=N+1
C SECOND HALF-STEP
C Solve the system of equations for the second
C half-step. Solve for k=2,3,...,kmax-1 for
C each j.
C Fixed j
DO J=2,JMAX
DO K=2,KMAX
KM=K-1
IF(N.GT.1)GOTO 13
6.2. -formulation: Douglas-Gunn algorithm 93
A2(2,KMAP)=-SY
A2(2,1)=0.
A2(4,KMAP)=0.
END DO
DT(J,1)= BCY1(N,J,DELT,DELX)-BCY1(N-1,J,DELT,DELX)
D2(1)=D2(1)+0.5*SY*DT(J,1)
D2(KMAP)=D2(KMAP)
DT(J,KMAX+1)=DT(J,KMAX-1)
DO K=2,KMAX
DT(J,K)=DUM(K-1)
TN(J,K)=TN(J,K)+DT(J,K)
END DO
END DO
DT(JMAX+1,KMAX+1)=DT(JMAX-1,KMAX-1)
DO J=1,JMAX
TN(J,1)=BCY1(N,J,DELT,DELX)
C TN(J,KMAX)=BCY2(N,J,DELT,DELX)
TN(J,KMAX+1)=TN(J,KMAX-1)+2.*DELY*BCY2(N,K,DELT,DELX)
END DO
T=T+DELT
WRITE(1,FMT=(A10,G8.3)) Time:,T
DO K=1, KMAX
DO J=1,JMAX
WRITE(1,FMT=(F15.3,$)) TN(J,K)
END DO
WRITE(1,*)
END DO
SUMT=0.
DO J=1,JMAX
Y=DELY*(K-1)
X=DELX*(J-1)
TE(J,K)=20.+
$ 80.*(Y-EXP(-0.5*ALPHX*PI**2*T)
$ *SIN(0.5*PI*X)*SIN(0.5*PI*Y))
ERR=TN(J,K)-TE(J,K)
SUMT=SUMT+ERR**2
WRITE(1,FMT=(F15.3,$)) ERR
END DO
WRITE(1,*)
END DO
RMST=SQRT(SUMT/((JMAX-1)*(KMAX-1)))
WRITE(1,FMT=(A8,F15.5)) ERROR:,RMST
IF(N.GE.NMAX)GOTO 18
IF(T.LT.TMAX)GOTO 11
DO K=1, KMAX
DO J=1,JMAX
WRITE(2,FMT=(F15.3,$)) TN(J,K)
WRITE(3,FMT=(F15.3)) TN(J,K)
END DO
WRITE(2,*)
WRITE(3,*)
END DO
CLOSE(1)
96 Chapter 6. Solution of diffusion equation in two spatial dimensions
CLOSE(2)
CLOSE(3)
STOP
END
FUNCTION BCX1(N,K,DELT,DELY)
C Boundary condition at x=0, i.e. u(1,k)
T=N*DELT
Y=DELY*(K-1)
BCX1=20.+80.*Y
RETURN
END
FUNCTION BCX2(N,K,DELT,DELY)
C Derivative boundary condition at x=1
T=N*DELT
Y=DELY*(K-1)
PI=3.1415927
ALPHX=1.E-5
BCX2=0.
RETURN
END
FUNCTION BCY1(N,J,DELT,DELX)
C Boundary condition at y=0, i.e. u(j,1)
T=N*DELT
X=DELX*(J-1)
BCY1=20.
RETURN
END
FUNCTION BCY2(N,J,DELT,DELX)
C Derivative boundary condition at y=1
6.2. -formulation: Douglas-Gunn algorithm 97
T=N*DELT
X=DELX*(J-1)
PI=3.1415927
ALPHX=1.E-05
BCY2=80.
RETURN
END
98 Chapter 6. Solution of diffusion equation in two spatial dimensions
Chapter 7
The solution of reaction-diffusion equations in two spatial dimensions does not involve many difficulties is the
reaction term does not contain spatial derivatives.
u 2u 2u
= ax 2 + ay 2 + f (u, ). (7.1)
t x y
un+1
j,k
(1 )(ax Lxx unj,k + ay Lyy unj,k ) (ax Lxx un+1 n+1 n
j,k + ay Lyy uj,k ) = f (uj,k , ). (7.2)
t
Equation (137) can be splitted in two equations each evolving in only one spatial direction,
99
100 Chapter 7. Solution of reaction-diffusion equations in two spatial dimensions
Bibliography
[1] C.A.J. Fletcher, Computational Techniques for Fluid Dynamics. Fundamental and General Techniques Vol.I,
Springer (1991).
[2] C.A.J. Fletcher, Computational Techniques for Fluid Dynamics. Specific Techniques for Different Flow Cate-
gories Vol.II, Springer (1991).
[3] K. Srinivas and C.A.J. Fletcher, Computational Techniques for Fluid Dynamics. A Solutions Manual Vol.II,
Springer (1992).
[4] C. Hirsch, Numerical Computation of Internal and External Flows. Fundamentals of Numerical Discretization
Vol.1, Wiley (1988).
[5] W.H. Press et al., Numerical Recipes in Fortran 77. The Art of Scientific Computing, Cambridge University
Press (1992).
[6] P. Grindrod, The Theory and Applications of Reaction-Diffusion Equations. Patterns and Waves, Clarendon
Press (1996).
[8] M.T.M. Kopper and J.H. Sluyters, Electrochim. Acta 38 (1993) 1535.
[9] A.R. Mitchell and D.F. Griffiths, The Finite Difference Method in Partial Differential Equations, Wiley (1980).
101