Você está na página 1de 102

Numerical Solution of Reaction-Diffusion Equations

by the Finite Difference Method

Antonis Karantonis
Chemist, PhD

Saitama, 2001
Contents

1 Introduction 3

2 Solution of the one dimensional diffusion equation 5


2.1 Discretisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Dirichlet boundary conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3 Neumann boundary conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3.1 NBC in a single end . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3.2 NBC in both ends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.4 Thomas algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.5 Example 1: Solution of 1D diffusion equation with DBC . . . . . . . . . . . . . . . . . . . . . . . 10
2.6 Example 2: Solution of 1D diffusion equation with NBC at one end . . . . . . . . . . . . . . . . . 23

3 Solution of a one dimensional reaction-diffusion equation 31


3.1 Dirichlet boundary conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.2 Neumann boundary conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.3 Example 3: Solution of 1D reaction-diffusion equation for NBC . . . . . . . . . . . . . . . . . . . 32

4 Solution of systems of reaction-diffusion equations with one spatial coordinate 39


4.1 Example 4: Solution of a system of reaction-diffusion equations in one spatial coordinate . . . . . . 41

5 Solution of systems of reaction-migration, reaction-diffusion equations 53


5.1 Example 5: Solution of the reaction-diffusion / reaction- migration problem with Neumann bound-
ary conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

6 Solution of diffusion equation in two spatial dimensions 65


6.1 ADI formulation: Peaceman-Rachford algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
6.1.1 Dirichlet boundary conditions for the ADI method . . . . . . . . . . . . . . . . . . . . . . 66
6.1.2 Example 6: Solution of the diffusion equation in two dimensions and Dirichlet boundary
conditions with ADI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.2 -formulation: Douglas-Gunn algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
6.2.1 Dirichlet boundary conditions for Douglas-Gunn method . . . . . . . . . . . . . . . . . . . 77

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

7 Solution of reaction-diffusion equations in two spatial dimensions 99


Chapter 1

Introduction

The simplest reation-diffusion equation is the diffusion equation itself,


u
= 2 u, (1.1)
t
2
where u = u(x, t) is the state variable (e.g. concentration) at time t and position x. The operator 2 = x2 +
2 2
y 2 + z 2 is often called Laplacian. If Eq. (1.1) holds for every x then it my be solved by the Fourier transform
method, once the initial conditions are defined,

u(x, 0) = u0 (x). (1.2)

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

Solution of the one dimensional diffusion equation

2.1 Discretisation

The diffusion equation in one spatial dimension (1D) is written,

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 .

2.2 Dirichlet boundary conditions

Let as assume Dirichlet boundary conditions (DBC),

u(xmin , t) = b1 (t), (2.5)


u(xmax , t) = b2 (t), (2.6)

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.,

un1 = bn1 , (2.7)


unjmax = bn2 . (2.8)

Thus, for j = 2, 3, ..., jmax 1, Eq. (3) is written,

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

un2 2(1 )sun2 + (1 )sun1 + (1 )sun3 + sun+1



1
un3 2(1 )sun3 + (1 )sun2 + (1 )sun4



...
d= . (2.14)


unj 2(1 )sunj + (1 )sunj1 + (1 )sunj+1

...


unjmax 1 2(1 )sunjmax 1 + (1 )sunjmax 2 + (1 )sunjmax + sun+1
jmax .

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.

2.3 Neumann boundary conditions

The case of Neumann boundary conditions (NBC) must be treated with care because the value of the derivative is
known at the boundaries.

2.3.1 NBC in a single end

Let us assume that we have NBC at x = xmin and DBC at x = xmax


u
x = c(t) at x = xmin
(2.15)
u = b(t) at x = xmax

Introduce a fictitious node j = 0 and approximate the NBC as,

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)

Combining Eqs.(16-17) we eliminate un+1


0 and obtain for the unknown boundary un+1
1 ,

(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

Equation (19) can be written in the form of Eq. (11) where,

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).

2.3.2 NBC in both ends

Let us assume that we have NBC at x = xmin and at x = xmax


u
x = c1 (t) at x = xmin
u
(2.23)
x = c2 (t) at x = xmax

Introduce a fictitious node j = jmax + 1 and approximate the NBC as,

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.,

(1 + 2s)un+1 n+1 n+1 n


jmax sujmax 1 sujmax +1 = ujmax . (2.25)

Combining Eqs.(24-25) we eliminate un+1 n+1


jmax +1 and obtain for the unknown boundary u jmax ,

(1 + 2s)un+1 n+1 n n+1


jmax 2sujmax 1 = ujmax + 2sxc2 . (2.26)

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

Equation (27) can be written in the form of Eq. (11) where,


T
u = (un+1
1 , un+1
3 , ..., un+1
jmax ) (2.28)

1 + 2s 2s 0 0 ... ...

s 1 + 2s s 0 ... ...
A=
...
(2.29)
... ... ... ... ...

... ... ... 0 2s 1 + 2s

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 .

2.4 Thomas algorithm

Thomas algorithm is a special application of LU decomposition for tridiagonal systems. Let,



b1 c1 0 ... ... ... ... ... ... x1 d1

a 2 b2 c2 0 ... ... ... ... ... x2 d 2


0 a 3 b3 c3 0 . . . . . . . . . . . . x3 d 3



... ... = ... (2.31)

. . . . . . . . . 0 a j bj cj 0 . . . xj d j



... ... ...

. . . . . . . . . . . . . . . . . . 0 a N bN xN dN
Normalize row 1 by dividing by b1 . Then, multiply row 1 by a2 and subtract from row 2. As a result we get,

b01 = b1 /b1 = 1, c01 = c1 /b1


a02 = a2 b01 a2 = 0, b02 = b2 c1 a2 /b1 = b2 c01 a2
d01 = d1 /b1 , d02 = d2 d01 a2

Now, normalize row 2 by dividing with b02 to get,

b02 = 1, c02 = c2 /(b2 c01 a2 ), d02 = (d2 d01 a2 )/(b2 c01 a2 )

Repeating this procedure we get the iteration formulas,

c01 = c1 /b1 , c0j = cj /(bj c0j1 aj ) (2.32)


d01 = d1 /b1 , d0j = (dj d0j1 aj )/(bj c0j1 aj ) (2.33)
10 Chapter 2. Solution of the one dimensional diffusion equation

for j = 2, 3, ..., N 1. The factorized form of Eq. (31) is,



1 c01 . . . . . . ... x1 d01
0

0 1 c2 . . . . . . x2
d0
2
... ... = ... (2.34)



. . . . . . . . . 1 c0 d0
N 1 xN 1

N 1
... ... ... ... 1 xN d0N

Equation (34) is solved with back substitution, i.e.,

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.

2.5 Example 1: Solution of 1D diffusion equation with DBC

In this example we solve the one dimensional diffusion equation for Dirichlet boundary conditions on both ends.

u(0, t) = u(1, t) = 100, (2.37)


m max
X 400 2 2
u(x, t0 ) = 100 sin[(2m 1)x]ea(2m1) t (2.38)
m=1
(2m 1)

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

C SOLVES THE DIFFUSION EQUATION USING


C CRANK-NICOLSON FORMULA
C AND DIRICHLET B.C.

DOUBLE PRECISION SUM,AVS,RMS,DSQRT,DMP


DIMENSION DUM(165),TE(141),A(5,165),D(165)
DIMENSION ELX(3),EMX(3),TOL(141),TN(141),TD(141)
C DIMENSION X(141)

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)

C File c-n.dat inputs the parametes of the system.


C These are:
C IPR output control
C JMAX number of points in x direction
C MAXEX number of terms in the exact solution
C NMAX maxumum number of time steps
C ALPH diffusion coefficient
C S = ALPH*DT/DX**2
C TMAX maximum time
C TST initial time

C File c-n.out writes the output in every time step


C if IPR =1 or only the final step if IPR>1.

C File diffusion.dat writes the output in every time


C step for ploting with gnuplot.

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,//)

4 FORMAT( DIFFUSION EQUATION: Crank-Nicolson,//)

C IMPLICIT PARAMETERS

C PARAMETER BETA FOR C-N FORMULA


BET=0.5

C OPERATOR MX=(0,1.,0) FOR C-N FORMULA


C FINITE DIFFERENCES
EMX(1)=0.
EMX(2)=1.
EMX(3)=0.

C KNOWN MATRIX ELEMENTS A, B, C


AD=-BET*S
BD=1.+2.*BET*S
CD=-BET*S

C OPERATOR LX=(1.,-2.,1.) FOR C-N FORMULA


ELX(1)=1.
ELX(2)=-2.
ELX(3)=1.

JMAF=JMAX-2

C OBTAIN INITIAL CONDITIONS FROM THE EXACT SOLUTION


C TN(J) is scaled.
C TD(J) is unscaled

T = TST
CALL INIT(JMAX,MAXEX,DELX,PI,ALPH,T,TE)

C Uncomment also the last DIMENSION line


2.5. Example 1: Solution of 1D diffusion equation with DBC 13

C if you want to use this I.C.


C DO J=1,JMAX
C AJ=J-1
C X(J)=DELX*AJ
C TE(J)=100.*COS(2.*PI*2.*X(J))
C WRITE(3,FMT=(11F8.2)) TE(J)
C END DO
C WRITE(3,*)

DO J=2, JMAP
TN(J)=TE(J)/100.
END DO

C SET BOUNDARY CONDITIONS (Dirichlet)


TN(1)=1.
TN(JMAX)=1.

TD(1)=100.*TN(1)
TD(JMAX)=100.*TN(JMAX)
N=0

C EACH TIME STEP STARTS AT 11

11 N=N+1

C SET UP THE TRIDIAGONAL SYSTEM OF EQUATIONS

DO J=2,JMAP
JM=J-1
IF(N.GT.1)GOTO 12

C This is the matrix of the known parameters.


C The form of this matrix is,
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 |............................................................|
14 Chapter 2. Solution of the one dimensional diffusion equation

C | A(2,i) A(3,i) A(4,i) |


C |............................................................|
C | A(2,N) A(3,N)|
C
C where,
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.
12 D(JM)=0.
DO K=1,3
KJ=J-2+K

C CRANK-NICOLSON FORMULA FOR VECTOR D


C The vector D for j=2,3,...,jmax-1 is,
C u(j+1)-2*(1-beta*s)*u(j+1)+(1-beta)*s*u(j)+(1-beta)*s*u(j+1)
C For j=1 and j=jmax, the vector is partially calculated
C and completed after the loop.

D(JM)=D(JM)+EMX(K)*TN(KJ)+
$ ELX(K)*S*(1.-BET)*TN(KJ)

END DO

END DO

C FIRST LINE OF C-N FORMULA (Dirichlet BC)


C The first line must be modified because,
C u(2)-2*(1-beta*s)*u(2)+(1-beta)*s*u(1)+(1-beta)*s*u(3)+beta*s*u(1)
C i.e. D(1) + beta*s*u(1)
C where u(1) comes from the B.C.
2.5. Example 1: Solution of 1D diffusion equation with DBC 15

D(1)=D(1)+BET*S*TN(1)

C LAST LINE OF C-N FORMULA (Dirichlet BC)


C The last line must be modified because,
C u(jmax-1)-2*(1-beta*s)*u(jmax-1)+
C +(1-beta)*s*u(jmax-1)+(1-beta)*s*u(jmax)+beta*s*u(jmax)
C i.e. D(JMAF) + beta*s*u(jmax)
C where u(jmax) comes from the B.C.
C NOTE that the number of the unknowns is JMAF = JMAX-2
C because u(1) and u(jmax) are known form the BC.

D(JMAF)=D(JMAF)+BET*S*TN(JMAX)

C SOLVE BANDED SYSTEM OF EQUATIONS


C Subroutine is called only once because the
C matrix A is independent of time and the factorization
C holds.

IF(N.EQ.1)CALL LU(A,JMAF)

C Subroutine must be called in every time step because


C the vector D is updated by the us.

CALL LUSOLVE(D,DUM,A,JMAF)

C Get the solution


DO J=2,JMAP
TN(J)=DUM(J-1)
END DO

C Scale up
DO J=2,JMAP
TD(J)=100.*TN(J)
END DO

C Next time step


T=T+DELT
16 Chapter 2. Solution of the one dimensional diffusion equation

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

C IF MAXIMUM TIME OR MAXIMUM NUMEBR OF TIME-STEPS


C EXCEEDED EXIT FROM LOOP

IF(N.GE.NMAX)GOTO 18
IF(T.LT.TMAX)GOTO 11

C The next chapter is valid only when the


C exact solution is known.
C In other cases, hope...

C OBTAIN EXACT SOLUTION AND COMPARE

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,//)

C RMS IS THE RMS ERROR


AVS=SUM/(1.+AJM)
RMS=DSQRT(AVS)
WRITE(2,21)RMS
21 FORMAT( RMS DIF=,D11.4,//)

CLOSE(1)
2.5. Example 1: Solution of 1D diffusion equation with DBC 17

CLOSE(2)
CLOSE(3)
STOP
END

The initial and boundary conditions are in subroutine INIT.

SUBROUTINE INIT(JMAX,MAXEX,DELX,PI,ALPH,T,TE)
C C.A.J. Fletcher,
C Computational Techniques in fluid dynamics, vol.1

C EXACT SOLUTION OF THE TRANSIENT HEAT


C CONDUCTION PROBLEM

C This subroutine calculates the exact solution


C of the diffusion equation with Dirichlet B.C.
C This solution is used as an initial condition
C and for error calculation.
C
C The exact solution is given by a series from m=1 to MAXEX,
C
C u(x,t)=100-SUM{[400/(2m-1)]sin[(2m-1)pix]exp[-a(2m-1)^2pi^2t]}
C
C See Fletcher, p.67.

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

C LIMIT THE ARGUMENT SIZE OF EXP(DTM)


IF(DTM.LT.-25.)DTM=-25.0
18 Chapter 2. Solution of the one dimensional diffusion equation

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

The Thomas algorithm is in subroutines LU and LUSOLVE.

SUBROUTINE LU(B,N)
C C.A.J. Fletcher,
C Computational Techniques in fluid dynamics, vol.1

C FACTORIZES BAND MATRIX ARISING FROM


C LINEAR OR QUADRATIC ELEMENTS INTO LU

C This routine uses Thomas algorithm


C for the factorization of a tridiagonal
C matrix to upper triangular form.
C The Thomas algorithm is:
C Let a matrix be,
C
C |b(1) c(1) |
C |a(2) b(2) c(2) |
C |.....................................|
C | a(i) b(i) c(i) |
C |.....................................|
C | a(N) b(N)|
C
C Then, multiply row1 by a(2)/b(1) and
C substruct row1 from row 2. The new elements
C are,
C
C b(1)= 1
C c(1)= c(1)/b(1)
C a(2)= 0
C b(2)= b(2)-a(2)*c(1)/b(1) = b(2)-a(2)*c(1)
C
2.5. Example 1: Solution of 1D diffusion equation with DBC 19

C Normalizing row2, multiplying by a(2) etc


C we get,
C
C c(i) = c(i)/(b(i)-a(i)*c(i-1)/b(i-1)) =
C = c(i)/(b(i)-a(i)*c(i-1))
C
C Also, the components of the vector of
C the unknowns must be manipulated
C simultaneusly. Direct inspection shows that,
C
C d(1)= d(1)/b(1),
C d(i)= (d(i)-a(i)*d(i-1)/b(i-1))/(b(i)-a(i)*c(i)) =
C = (d(i)-a(i)*d(i-1))/(b(i)-a(i)*c(i))
C
C The diagonal elements are all 1 and
C the rest of the elements are zero.
C
C The values of elements with 1 or 0 value
C do not have to be stored. Thus, the following
C routine calculates the normalization factor
C b(i)-a(i)*c(i-1)/b(i-1) and stores it
C in the diagonal elements.
C The routine doesnt manipulate the ds but it
C calculates the factor a(i)/b(i-1) and
C stores it to the lower diagonal elements.
C Clever...
C Note that, in this routine, the tridiagonal
C matrix is,
C
C |B(3,1) B(4,1) |
C |B(2,2) B(3,2) B(4,2) |
C | B(2,3) B(3,3) B(4,3) |
C |............................................................|
C | B(2,i) B(3,i) B(4,i) |
C |............................................................|
C | B(2,N) B(3,N)|

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

C Form the factor a(i)/b(i-1) and store in a(i)

B(2,JP)=B(2,JP)/B(3,J)

C Form the factor b(i)-a(i)*c(i-1)/b(i-1) and store in b(i)

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

C USES LU FACTORIZATION TO SOLVE FOR X, GIVEN R

C This routine uses the factorization information


C of the subroutine LU to solve a tridiagonal
C system of equations.
C Recall that X is the vector of the unknowns,
C R is the known vector and B is the factorization
C information form the LU subroutine.
C Thus,
C
C B(2,J) = a(j)/b(j-1)
C B(3,J) = b(j)-a(j)*c(j-1)/b(j-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

C Form the term d(i)-a(i)*d(i-1) = d(i)-a(i)*d(i-1)/b(i-1)


C Note that the factor a(i)/b(i-1) is B(2,i)

R(JP)=R(JP)-B(2,JP)*R(J)

END DO

C Solve trivially for the last row x(N)=d(N)=


C = (d(N)-a(N)*d(N-1))/(b(N)-a(N)*c(N-1))
C But (d(N)-a(N)*d(N-1))=R(N), already calculated above
C and b(N)-a(N)*c(N-1)=B(3,N) from the LU subroutine.

X(N)=R(N)/B(3,N)

C Solve for the rest of the unknowns by back substitution.


C x(i)=d(i)-c(i)*x(i+1)
C Note that B(4,JA)/B(3,JA)=c(i)

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,

u(x, t0 ) = 100 cos(3x), (2.39)

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

2.6 Example 2: Solution of 1D diffusion equation with NBC at one end

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)

The initial condition is,


u(x, 0) = 2x + 4 cos(0.5x). (2.42)

The basic module of this program is in DIFIM.

PROGRAM DIFIM
C C.A.J. Fletcher,
C Computational Techniques in fluid dynamics, vol.1
C and Srininas and Fletcher -solution manual

C SOLVES THE DIFFUSION EQUATION USING


C CRANK-NICOLSON FORMULA
C AND NEUMANN B.C. at one end

DOUBLE PRECISION SUM,AVS,RMS,DSQRT,DMP


DIMENSION DUM(165),TE(141),A(5,165),D(165)
DIMENSION ELX(3),EMX(3),TOL(141),TN(141),TD(141)

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)

C IPR output control


C JMAX number of points in x direction
C MAXEX number of terms in the exact solution
C NMAX maxumum number of time steps
C ALPH diffusion coefficient
C S = ALPH*DT/DT**2
C TMAX maximum time
C TST initial time
24 Chapter 2. Solution of the one dimensional diffusion equation

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

C OPERATOR MX=(0,1.,0) FOR C-N FORMULA


C FINITE DIFFERENCES
EMX(1)=0.
EMX(2)=1.
EMX(3)=0.

C KNOWN MATRIX ELEMENTS A, B, C


AD=-BET*S
BD=1.+2.*BET*S
CD=-BET*S

C OPERATOR LX=(1.,-2.,1.) FOR C-N FORMULA


ELX(1)=1.
ELX(2)=-2.
2.6. Example 2: Solution of 1D diffusion equation with NBC at one end 25

ELX(3)=1.

JMAF=JMAX-2

C OBTAIN INITIAL CONDITIONS FROM THE EXACT SOLUTION


T = TST
CALL INIT(JMAX,MAXEX,DELX,PI,ALPH,T,TE)

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

C SET BOUNDARY CONDITIONS (Dirichlet)


TN(1)=TE(1)
TN(JMAX)=TE(JMAX)
C TN(1)=1.
C TN(JMAX)=1.
TD(1)=TN(1)
TD(JMAX)=TN(JMAX)
N=0

C EACH TIME STEP STARTS AT 11


11 N=N+1

C SET UP THE TRIDIAGONAL SYSTEM OF EQUATIONS


DO J=2,JMAP

C Now the counter JM starts from 2 (in contrast with the


C Dirichlet B.C. which starts with 1). The boundary
C value u(1) is also an unknown.
JM=J

IF(N.GT.1)GOTO 12
26 Chapter 2. Solution of the one dimensional diffusion equation

C The structure of the matrix of the unkowns is,


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(2,1) = -2.*S
C
C and for i=2,3,...,N
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.

C The first line of the system must be modified to


C meet the Neumann B.C. The implicit scheme used
C can be found in Fletcher p.273, Eq.~(7.36)
A(1,1)=0.
A(2,1)=0.
A(3,1)=1.+2.*S
A(4,1)=-2.*S
A(5,1)=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

C FIRST LINE OF C-N FORMULA (Neumann BC)


C The component of vector D in the first line
C must be modified due to the Neumann B.C.
C This is,
C
C d(1)=u(1)-2*s*Dx*c(n+1)
C
C where n+1 indicates the next TIME step.
C See, Fletcher, p.237, Eq.~(7.36)
C Thus, D(1) calculated above is replaced by the
C following commands.
T=T+DELT
CN=2.-2.*PI*SIN(0.05*PI)*EXP(-ALPH*PI*PI*T/4.)
D(1)=TN(1)-2.*S*DELX*CN

C LAST LINE OF C-N FORMULA (Dirichlet BC)


C The last line of d is modified according to
C the Dirichlet B.C. as in program c-n.f
C Now, the total number of unknowns is JMAP=JMAX-1
C because u(1) at the boundary is also an unknown...
D(JMAP)=D(JMAP)-A(4,JMAP)*TN(JMAX)

C SOLVE BANDED SYSTEM OF EQUATIONS


C The second argument in the calling statements
C is JMAP=JMAX-1 because u(1) at the boundary is
C also an unknown...
IF(N.EQ.1)CALL LU(A,JMAP)
CALL LUSOLVE(D,DUM,A,JMAP)
28 Chapter 2. Solution of the one dimensional diffusion equation

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

C IF MAXIMUM TIME OR MAXIMUM NUMEBR OF TIME-STEPS


C EXCEEDED EXIT FROM LOOP
IF(N.GE.NMAX)GOTO 18
IF(T.LT.TMAX)GOTO 11

C OBTAIN EXACT SOLUTION AND COMPARE


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)
2.6. Example 2: Solution of 1D diffusion equation with NBC at one end 29

20 FORMAT(/, T=,F5.2, TE=,100F8.2,//)

C RMS IS THE RMS ERROR


AVS=SUM/(1.+AJM)
RMS=DSQRT(AVS)
WRITE(2,21)RMS
21 FORMAT( RMS DIF=,D11.4,//)
CLOSE(1)
CLOSE(2)
CLOSE(3)
STOP
END

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

An example of the solution of this problem is shown in Fig.3.


30 Chapter 2. Solution of the one dimensional diffusion equation

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

Solution of a one dimensional reaction-diffusion equation

A one dimensional reaction-diffusion equation is written,


u 2u
= a 2 + f (u, ), (3.1)
t x
where f (u, ) can be a nonlinear fuction of u and is a parameter of the system. If f (u, ) does not contain any
spatial derivatives, the solution of Eq. (43) is very similar with that of the simple diffusion equation.
Let us consider a specific example,
u 2u
= + u(1 u)(u ). (3.2)
t x2
Hence, the disretization of Eq. (44) gives,
un+1
j unj h unj1 2unj + unj+1 un+1 n+1
j1 2uj + un+1
j+1
i
(1 ) + = unj (1 unj )(unj ), (3.3)
t x2 x2
where = 0.5. The algorithm obtained form Eq. (45) is,

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.

3.1 Dirichlet boundary conditions

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.

3.2 Neumann boundary conditions

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.

3.3 Example 3: Solution of 1D reaction-diffusion equation for NBC

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 SOLVES A 1D RD EQUATION USING


C CRANK-NICOLSON FORMULA
C AND NEUMANN B.C.

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)

C JMAX number of points in x direction


C NMAX maxumum number of time steps
C ALPH diffusion coefficient
C S = ALPH*DT/DX**2
C TMAX maximum time
C TST initial time

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

C PARAMETER BETA FOR C-N FORMULA


BET=0.5

C OPERATOR MX=(0,1.,0) FOR C-N FORMULA


C FINITE DIFFERENCES
EMX(1)=0.
EMX(2)=1.
EMX(3)=0.

C KNOWN MATRIX ELEMENTS A, B, C


AD=-BET*S
BD=1.+2.*BET*S
CD=-BET*S
34 Chapter 3. Solution of a one dimensional reaction-diffusion equation

C OPERATOR LX=(1.,-2.,1.) FOR C-N FORMULA


ELX(1)=1.
ELX(2)=-2.
ELX(3)=1.

C OBTAIN INITIAL CONDITIONS

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

C EACH TIME STEP STARTS AT 11

11 N=N+1

C SET UP THE TRIDIAGONAL SYSTEM OF EQUATIONS

DO J=2,JMAP

C The counter JM starts from 2 (in contrast with the


C Dirichlet B.C. which starts with 1). The boundary
C values u(1) an u(N) are also an unknowns.
JM=J

IF(N.GT.1)GOTO 12

C The structure of the matrix of the unkowns is,


3.3. Example 3: Solution of 1D reaction-diffusion equation for NBC 35

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.

C The first and last line of the system must be modified to


C meet the Neumann B.C. The implicit scheme used
C can be found in Fletcher p.273, Eq.~(7.36)

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

C CRANK-NICOLSON FORMULA FOR VECTOR D

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

C FIRST LINE OF C-N FORMULA (Neumann BC)


C The component of vector D in the first line
C must be modified due to the Neumann B.C.
C Thus, D(1) calculated above is replaced by the
C following commands.

T=T+DELT

CN=0.

D(1)=TN(1)-2.*S*DELX*CN
$ +DELT*TN(1)*(1.-TN(1))*(TN(1)-PMU)

C LAST LINE OF C-N FORMULA (Neumann BC)


C The last line of d is modified according to
C the Neuman B.C.
C Now, the total number of unknowns is JMAX
3.3. Example 3: Solution of 1D reaction-diffusion equation for NBC 37

C because u(1) and u(N) at the boundary is also unknown...

D(JMAX)=TN(JMAX)+2.*S*DELX*CN
$ +DELT*TN(JMAX)*(1.-TN(JMAX))*(TN(JMAX)-PMU)

C SOLVE BANDED SYSTEM OF EQUATIONS

C The second argument in the calling statements


C is JMAX because u(1) and u(N) at the boundary are
C also unknown...

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

C IF MAXIMUM TIME OR MAXIMUM NUMEBR OF TIME-STEPS


C EXCEEDED EXIT FROM LOOP

IF(N.GE.NMAX)GOTO 18
IF(T.LT.TMAX)GOTO 11

18 CLOSE(1)
STOP
END

The following subroutine is the initial conditions for Eq. (43).


38 Chapter 3. Solution of a one dimensional reaction-diffusion equation

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

Solution of systems of reaction-diffusion equations with one spatial coordinate

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

In Eqs.(51-52) we have two dynamical variables and one spatial dimension.


Let us introduce a forward Euler discretization formula for the time derivatives and a Crank-Nicolson scheme
for the spatial derivatives. Eqs.(51-52) become,

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

From Eqs.(53-54) the following algorithm is introduced,

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

tions at the other end, i.e.


u v
= = 0, at x = 0, (4.7)
x x
u = v = 0, at x = xmax (4.8)

Equation (57) can be written,


un+1
2 un+1
0
= 0. (4.9)
2x
Introducing also a full implicit scenarion for the interion at the boundary we have,
un+1
1 un1 un+1 2un+1 + un+1
2 1 0
= [1 (un1 )2 (v1n )2 )](un1 3v1n ). (4.10)
t x2
Combining Eqs.(53-54) we obtain,

(1 + 2s)un+1
1 2sun+1
2 = un1 + t[1 (un1 )2 (v1n )2 )](un1 3v1n ). (4.11)

Following a similar procedure for the other variable we get,

(1 + 2s)v1n+1 2sv2n+1 = v1n + t[1 (un1 )2 (v1n )2 )](3un1 + v1n ). (4.12)

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

+ t[1 (unjmax 1 )2 (vjnmax 1 )2 ](unjmax 1 3vjnmax 1 ) + 0.5sujmax ,


(4.13)

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

un1 + t[1 (un1 )2 (v1n )2 )](un1 3v1n )


v1n + t[1 (un1 )2 (v1n )2 )](3un1 + v1n )






...

uj suj + 0.5suj1 + 0.5suj+1 + t[1 (unj )2 (vjn )2 ](unj 3vjn )
n n n n


d=

vjn svjn + 0.5svj1
n n
+ 0.5svj+1 + t[1 (unj )2 (vjn )2 ](3unj + vjn )



...

ujmax 1 suj + 0.5sujmax 2 + 0.5sujmax + t[1 (unjmax 1 )2 (vjnmax 1 )2 ](unjmax 1 3vjnmax 1 ) + 0.5sujmax
n n n n

vjnmax 1 svjn + 0.5svjnmax 2 + 0.5svjnmax + t[1 (unjmax 1 )2 (vjnmax 1 )2 ](3unjmax 1 + vjnmax 1 ) + 0.5svjmax
(4.17)

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.

4.1 Example 4: Solution of a system of reaction-diffusion equations in one spatial coordinate

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

C SOLVES A 2D SYSTEM OF RD EQUATIONS USING


C CRANK-NICOLSON FORMULA
C AND NEUMANN B.C. AT X=0.

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)

C IPR output control


C JMAX number of points in x direction
C MAXEX number of terms in the exact solution
C NMAX maxumum number of time steps
42 Chapter 4. Solution of systems of reaction-diffusion equations with one spatial coordinate

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.

C ALPH diffusion coefficient


C S = ALPH*DT/DX**2
C TMAX maximum time
C TST initial time

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

C PARAMETER BETA FOR C-N FORMULA


BET=0.5

C KNOWN MATRIX ELEMENTS A, B, C


AD=-BET*S
BD=1.+2.*BET*S
CD=-BET*S

C OBTAIN INITIAL CONDITIONS

T = TST
CALL INIT(JMAX,MAXEX,DELX,PI,ALPH,T,TE)

C WRITE INITIAL CONDITION


C IF(IPR.EQ.1) THEN
C DO J=1,JMAX
C WRITE(1,FMT=(F15.8)) TE(J,1)
C END DO
C WRITE(1,*)
C END IF

DO J=1, JMAX
TN(J,1)=TE(J,1)
TN(J,2)=TE(J,2)
END DO

N=0

C EACH TIME STEP STARTS AT 11

11 N=N+1

C SET UP THE TRIDIAGONAL SYSTEM OF EQUATIONS

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 FIRST LINE BLOCKS


B(1,1,1)=1.+2.*S
B(1,1,2)=0.
B(1,2,1)=0.
B(1,2,2)=1.+2.*S

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.

C CRANK-NICOLSON FORMULA FOR VECTOR D

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

C FIRST LINE OF C-N FORMULA (Neumann BC)

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))

C LAST LINE OF C-N FORMULA (Dirichlet BC)

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))

C SOLVE BANDED SYSTEM OF EQUATIONS


46 Chapter 4. Solution of systems of reaction-diffusion equations with one spatial coordinate

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

C IF MAXIMUM TIME OR MAXIMUM NUMEBR OF TIME-STEPS


C EXCEEDED EXIT FROM LOOP

IF(N.GE.NMAX)GOTO 18
IF(T.LT.TMAX)GOTO 11

18 CLOSE(1)
STOP
END

Initial conditions subroutine,

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

Routines for the modified Thomas algorithm.

SUBROUTINE BLTRI(N,A,B,C,D,X)

C SOLVES NBLxNBL BLOCK TRIDIAGONAL SYSTEM


C OF EQUATIONS

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

C GAUSS ELIMINATION WITH PARTIAL PIVOTING

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 CHECK FOR ZERO PIVOT


IF (ABS(S).LT.1.E-05)GOTO 6
50 Chapter 4. Solution of systems of reaction-diffusion equations with one spatial coordinate

C CALULATE MULTIPLIERS
DO I=KP,N
A(I,K)=-A(I,K)/S
END DO

C INTERCHANGE AND ELIMINATE BY COLUMNS


DO J=KP,N
S=A(L,J)
A(L,J)=A(K,J)
A(K,J)=S
IF(ABS(S).LT.1.E-15)GOTO 4
DO I=KP,N
A(I,J)=A(I,J)+A(I,K)*S
END DO
4 END DO
END DO
RETURN
6 JPVT(N)=-1
PAUSE Zero pivot
RETURN
END

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

Solution of systems of reaction-migration, reaction-diffusion equations

Spatio-temporal begavior on an electrode surface may be modeled by systems of reaction-migration / reaction-


diffusion equations. We will see that, in principle, for such models the numerical solution is exactly analogous with
the solution of systems of reaction-diffusion equations.
As a working example, let us consider the following system,

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

Figure 5.1: Solution of Eq. (69-71) jmax = 250.

2.5

1.5

0.5

0
0 0.2 0.4 0.6 0.8 1

Figure 5.2: Nullclines of Eq. (69-71) for the excitable case.


56 Chapter 5. Solution of systems of reaction-migration, reaction-diffusion equations

1.8

1.6

1.4

1.2

0.8

0.6

0.4
0 50 100 150 200 250

Figure 5.3: Excitable pulse of Eq. (69-71).

PROGRAM RDE

C SOLVES A 2D SYSTEM OF RDE EQUATIONS USING


C CRANK-NICOLSON FORMULA
C AND NEUMANN B.C. AT BOTH ENDS

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 IPR output control


C JMAX number of points in x direction
C MAXEX number of terms in the exact solution
C NMAX maxumum number of time steps
C ALPH diffusion coefficient
5.1. Example 5: Solution of the reaction-diffusion / reaction- migration problem with Neumann boundary conditions 57

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

C PARAMETER BETA FOR C-N FORMULA


BET=0.5

C KNOWN MATRIX ELEMENTS A, B, C


AD1=-BET*S1
BD1=1.+2.*BET*S1
CD1=-BET*S1
AD2=-BET*S2
BD2=1.+2.*BET*S2
CD2=-BET*S2

C OBTAIN INITIAL CONDITIONS

T = TST
CALL INIT(JMAX,MAXEX,DELX,PI,ALPH,T,TE)

C WRITE INITIAL CONDITION


C IF(IPR.EQ.1) THEN
C DO J=1,JMAX
C WRITE(1,FMT=(F15.8)) TE(J,1)
C END DO
C WRITE(1,*)
C END IF

DO J=1, JMAX
TN(J,1)=TE(J,1)
TN(J,2)=TE(J,2)
END DO

N=0

C EACH TIME STEP STARTS AT 11

11 N=N+1
5.1. Example 5: Solution of the reaction-diffusion / reaction- migration problem with Neumann boundary conditions 59

C SET UP THE TRIDIAGONAL SYSTEM OF EQUATIONS

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 NEUMAN BOUNDARY CONDITION AT J=1


B(1,1,1)=1.+2.*S1
B(1,1,2)=0.
B(1,2,1)=0.
B(1,2,2)=1.+2.*S2

C(1,1,1)=-2.*S1
C(1,1,2)=0.
C(1,2,1)=0.
C(1,2,2)=-2.*S2

C NEUMAN BOUNDARY CONDITION AT J=JMAX


B(JMAX,1,1)=1.+2.*S1
B(JMAX,1,2)=0.
60 Chapter 5. Solution of systems of reaction-migration, reaction-diffusion equations

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.

C CRANK-NICOLSON FORMULA FOR VECTOR D

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

C FIRST LINE OF C-N FORMULA (Neumann BC)


C NEUMANN B.C. AT J=1

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)))

C LAST LINE OF C-N FORMULA (NEUMANN BC)


C NEUMANN B.C. AT J=NMAX

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 LAST LINE OF C-N FORMULA (Dirichlet BC)


C DIRICHELT B.C. AT J=NMAX

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 SOLVE BANDED SYSTEM OF EQUATIONS


C JMAP=JMAX-1 DUE TO D.B.C.

C JMAX DUE TO NBC


CALL BLTRI(JMAX,A,B,C,D,X)

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

C IF MAXIMUM TIME OR MAXIMUM NUMEBR OF TIME-STEPS


C EXCEEDED EXIT FROM LOOP

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

Solution of diffusion equation in two spatial dimensions

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

6.1 ADI formulation: Peaceman-Rachford algorithm

Introduce an FTCS sceme for the first time step n + 1/2, of the form,

uj,k unj,k uj1,k 2uj,k + uj+1,k unj,k1 2unj,k + unj,k+1


ax a y = 0, (6.2)
0.5t x2 y 2

or, in a compact form,


uj,k unj,k
ax Lxxuj,k ay Lyy unj,k = 0. (6.3)
0.5t
Notice that, in this first half-step, only variations at the x-direction are unknown, i.e. constant k, j = 2, 3, ..., j max
1.
Equation (82) is written as an algorithm,

0.5sxuj1,k + (1 + sx )uj,k 0.5sxuj+1,k = 0.5sy unj,k1 + (1 sy )unj,k + 0.5sy unj,k+1 , (6.4)

for j = 2, 3, ..., jmax 1 and k constant. Also, sx = ax t/x2 and sy = ay t/y 2 .


For the second half-step introduce the following FTCS sceme,

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

or, in a compact form,


un+1
j,k uj,k
ax Lxxuj,k ay Lyy un+1
j,k = 0. (6.6)
0.5t
Notice that, in this first second-step, only variations at the y-direction are unknown, i.e. constant j, k = 2, 3, ..., k max
1.

65
66 Chapter 6. Solution of diffusion equation in two spatial dimensions

Equation (85) is written as an algorithm,

0.5sy un+1 n+1 n+1


j,k1 + (1 + sy )uj,k 0.5sy uj,k+1 = 0.5sx uj1,k + (1 sx )uj,k + 0.5sx uj+1,k , (6.7)

for k = 2, 3, ..., kmax 1 and j constant.

6.1.1 Dirichlet boundary conditions for the ADI method

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,

u1,k = 0.5(nk + n+1


k ) 0.25tay Lyy (n+1
k nk ). (6.12)

Similarly for the boundary at j = jmax ,

ujmax ,k = 0.5(bnk + bn+1


k ) 0.25tay Lyy (bn+1
k bnk ). (6.13)

Under these considerations, Eq. (84) is written in a matrix form of Eq. (11), where,

u = (u2,k , u3,k , . . . , ujmax 1,k ), (6.14)



1 + sx 0.5sx 0 ...
1 + sx 0 ...

0.5sx 0.5sx

...
A= (6.15)



... 0 0.5sx 1 + sx 0.5sx

...


... 0 0.5sx 1+s
and
0.5sy un2,k1 + (1 sy )un2,k + 0.5sy un2,k+1 + 0.5sx u1,k
0.5sy un3,k1 + (1 sy )un3,k + 0.5sy un3,k+1



...
d= (6.16)

0.5sy unj,k1 + (1 sy )unj,k + 0.5sy unj,k+1



...


0.5sy unjmax 1,k1 + (1 sy )unjmax 1,k + 0.5sy unjmax 1,k+1 + 0.5sx ujmax ,k
6.1. ADI formulation: Peaceman-Rachford algorithm 67

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,

u = (un+1 n+1 n+1


j,2 , uj,3 , . . . , uj,kmax 1 ), (6.17)

1 + sy 0.5sy 0 ...
1 + sy 0 ...

0.5sy 0.5sy

...
A= (6.18)



... 0 0.5sy 1 + sy 0.5sy

...


... 0 0.5sy 1 + sy
and
0.5sx uj1,2 + (1 sx )uj,2 + 0.5sx uj+1,2 + 0.5sy un+1

j,1
0.5sx uj1,3 + (1 sx )uj,3 + 0.5sx uj+1,3



...
d= (6.19)



0.5sx uj1,k + (1 sx )uj,k + 0.5sx uj+1,k

...


0.5sx uj1,kmax 1 + (1 sx )uj,kmax 1 + 0.5sx uj+1,kmax 1 + 0.5sy un+1
j,kmax

where un+1 n+1


j,1 and uj,kmax in the first and last line of vector d are given by the conditions at this boundary, e.g.
un+1 n+1
j,1 = cj and un+1 n+1
j,kmax = dj . Obviously, all the above systems are tridiagonal and can be solved with Thomas
algorithm.

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,

u(0, y, t) = 20 + 80y, (6.20)


2
u(1, y, t) = 20 + 80[y e0.5a t sin(0.5y)], (6.21)
u(x, 0, t) = 20, (6.22)
0.5a 2 t
u(x, 1, t) = 20 + 80[1 e sin(0.5x)] (6.23)

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

C SOLVES THE DIFFUSION EQUATION


68 Chapter 6. Solution of diffusion equation in two spatial dimensions

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

Figure 6.1: Solution of the 2D diffusion equation for DBC.

C IN TWO DIMENSIONS USING


C THE ADI METHOD AND DIRICHLET B.C.

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)

C JMAX number of points in x direction


C KMAX number of points in y direction
C NMAX maxumum number of time steps
C ALPHX diffusion coefficient in x
6.1. ADI formulation: Peaceman-Rachford algorithm 69

C ALPHY diffusion coefficient in y


C SX = ALPHX*DT/DX**2
C SY = ALPHY*DT/DY**2
C TMAX maximum time
C TST initial time

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

C OBTAIN INITIAL CONDITIONS

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

C SET BOUNDARY CONDITIONS (Dirichlet)


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

N=0

C EACH TIME STEP STARTS AT 11

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

C SET UP THE TRIDIAGONAL SYSTEM OF EQUATIONS


C The number of unknowns is JAF=JMAX-2
C i.e. j=2,3,..., jmax
DO J=2,JMAP
JM=J-1

IF(N.GT.0)GOTO 12

C Matrix element for A


A1(1,JM)=0.
A1(2,JM)=-0.5*SX
A1(3,JM)=1.+SX
6.1. ADI formulation: Peaceman-Rachford algorithm 71

A1(4,JM)=-0.5*SX
A1(5,JM)=0.

C Vector elements for d


12 D1(JM)=0.5*SY*TN(J,K-1)+(1.-SY)*TN(J,K)+0.5*TN(J,K+1)

END DO

C FIRST LINE OF VECTOR D1 (Dirichlet BC)

C Modifed boundary condition


TH(1,K)= 0.5*(BCX1(N,K,DELT,DELY)+BCX1(N+1,K,DELT,DELY))
$ -0.25*SY*(BCX1(N+1,K-1,DELT,DELY)-
$ 2.*BCX1(N+1,K,DELT,DELY)+BCX1(N+1,K+1,DELT,DELY))
$ +0.25*SY*(BCX1(N,K-1,DELT,DELY)-
$ 2.*BCX1(N,K,DELT,DELY)+BCX1(N,K+1,DELT,DELY))

D1(1)=D1(1)+0.5*SX*TH(1,K)

C LAST LINE OF VECTOR D1 (Dirichlet BC)

C Modified boundary condition


TH(JMAX,K)=0.5*(BCX2(N,K,DELT,DELY)
$ +BCX2(N+1,K,DELT,DELY))
$ -0.25*SY*(BCX2(N+1,K-1,DELT,DELY)-
$ 2.*BCX2(N+1,K,DELT,DELY)+BCX2(N+1,K+1,DELT,DELY))
$ +0.25*SY*(BCX2(N,K-1,DELT,DELY)-
$ 2.*BCX2(N,K,DELT,DELY)+BCX2(N,K+1,DELT,DELY))

D1(JMAF)=D1(JMAF)+0.5*SX*TH(JMAX,K)

C SOLVE BANDED SYSTEM OF EQUATIONS


C Subroutine is called only once because the
C matrix A is independent of time and the factorization
C holds. Notice, the number of unknowns is JMAF.
IF(N.EQ.0)CALL LU(A1,JMAF)

C Subroutine must be called in every time step because


72 Chapter 6. Solution of diffusion equation in two spatial dimensions

C the vector D is updated by the us.


CALL LUSOLVE(D1,DUM,A1,JMAF)

C Get the solution


C This nodal values are the values for the first
C half-step
DO J=2,JMAP
TH(J,K)=DUM(J-1)
END DO

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

C SET UP THE TRIDIAGONAL SYSTEM OF EQUATIONS

DO K=2,KMAP
KM=K-1

IF(N.GT.1)GOTO 13

C Matrix elements for A


A2(1,KM)=0.
A2(2,KM)=-0.5*SY
A2(3,KM)=1.+SY
A2(4,KM)=-0.5*SY
A2(5,KM)=0.

C Vector elements for d


13 D2(KM)=0.5*SX*TH(J-1,K)
6.1. ADI formulation: Peaceman-Rachford algorithm 73

$ +(1.-SX)*TH(J,K)+0.5*TH(J+1,K)

END DO

C FIRST LINE OF VECTOR D2 (Dirichlet BC)


C TN(J,1)= BCY1(N+1,J,DELT,DELX)
TN(J,1)= BCY1(N,J,DELT,DELX)

D2(1)=D2(1)+0.5*SY*TN(J,1)

C LAST LINE OF VECTOR D2 (Dirichlet BC)


C TN(J,KMAX)=BCY2(N+1,J,DELT,DELX)
TN(J,KMAX)=BCY2(N,J,DELT,DELX)

D2(KMAF)=D2(KMAF)+0.5*SY*TN(J,KMAX)

C SOLVE BANDED SYSTEM OF EQUATIONS


C Subroutine is called only once because the
C matrix A is independent of time and the factorization
C holds.
IF(N.EQ.1)CALL LU(A2,KMAF)

C Subroutine must be called in every time step because


C the vector D is updated by the us.
CALL LUSOLVE(D2,DUM,A2,KMAF)

C Get the solution


DO K=2,KMAP
TN(J,K)=DUM(K-1)
END DO
END DO

C Update boundary conditions


DO K=1,KMAX
TN(1,K)=BCX1(N,K,DELT,DELY)
TN(JMAX,K)=BCX2(N,K,DELT,DELY)
END DO
74 Chapter 6. Solution of diffusion equation in two spatial dimensions

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

C Compare with exact solution


WRITE(1,FMT=(A10)) ERROR
DO K=1, KMAX
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))
WRITE(1,FMT=(F15.3,$))ABS(TE(J,K)-TN(J,K))
END DO
WRITE(1,*)
END DO

C IF MAXIMUM TIME OR MAXIMUM NUMEBR OF TIME-STEPS


C EXCEEDED EXIT FROM LOOP

IF(N.GE.NMAX)GOTO 18
IF(T.LT.TMAX)GOTO 11

C Plot contour plot and surface plot


C for the last step
6.1. ADI formulation: Peaceman-Rachford algorithm 75

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

Routine for the boundary conditions

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

6.2 -formulation: Douglas-Gunn algorithm

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

where un+1 n+1 n n+1


j,k = uj,k uj,k . In order to manipulate Eq. (104) to give an algorithm for the unknown u j,k let us
expand the term un+1
j,k around the n-th time level, i.e.,

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

Introducing Eq. (106) into Eq. (104) we get,


un+1
j,k
(ax Lxx unj,k + ay Lyy unj,k ) (ax Lxx un+1 n+1
j,k + ay Lyy uj,k ) = 0. (6.27)
t
Rearanging Eq. (107) we obtain,

[1 t(ax Lxx + ay Lyy )]un+1 n


j,k = t(ax Lxx + ay Lyy )uj,k . (6.28)

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,

(1 tax Lxx )(1 tay Lyy )un+1 n


j,k = t(ax Lxx + ay Lyy )uj,k . (6.29)

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,

(1 tax Lxx )uj,k = t(ax Lxx + ay Lyy )unj,k , (6.30)


(1 tay Lyy )un+1
j,k = uj,k . (6.31)

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.

6.2.1 Dirichlet boundary conditions for Douglas-Gunn method

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,

u1,k = (1 tay Lyy )un+1


1,k , (6.32)

or, by using Eq. (88),


u1,k = (1 tay Lyy )n+1
k . (6.33)
Similarly for the other boundary,
ujmax ,k = (1 tay Lyy )bn+1
k . (6.34)
Thus, at the first stage (constant k) we must a tridiagonal equation of the form of Eq. (11) where,

u = (u2,k , u3,k , . . . , ujmax 1,k ), (6.35)



1 + 2sx sx 0 ...
1 + 2sx 0 ...

sx sx

...
A=



... 0 sx 1 + 2sx sx

...


... 0 sx 1 + 2sx
(6.36)
78 Chapter 6. Solution of diffusion equation in two spatial dimensions

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,

u = (un+1 n+1 n+1


j,2 , uj,3 , . . . , uj,kmax 1 ), (6.38)

1 + 2sy sx 0 ...
1 + 2sy 0 ...

sy sy

...
A= (6.39)


... 0 sy 1 + 2sy sy

...


... 0 sy 1 + 2sy
and
uj,2 + sy cn+1

j
uj,3



...
d= (6.40)



uj,k

...


uj,kmax 1 + sy dn+1
j

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

C SOLVES THE DIFFUSION EQUATION


C IN TWO DIMENSIONS USING
C THE ADI METHOD AND DIRICHLET B.C.
6.2. -formulation: Douglas-Gunn algorithm 79

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 JMAX number of points in x direction


C KMAX number of points in y direction
C NMAX maxumum number of time steps
C ALPHX diffusion coefficient in x
C ALPHY diffusion coefficient in y
C SX = ALPHX*DT/DX**2
80 Chapter 6. Solution of diffusion equation in two spatial dimensions

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

C OBTAIN INITIAL CONDITIONS

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

C SET BOUNDARY CONDITIONS (Dirichlet)


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

C EACH TIME STEP STARTS AT 11

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

C SET UP THE TRIDIAGONAL SYSTEM OF EQUATIONS


C The number of unknowns is JAF=JMAX-2
C i.e. j=2,3,..., jmax
DO J=2,JMAP
82 Chapter 6. Solution of diffusion equation in two spatial dimensions

JM=J-1

IF(N.GT.0)GOTO 12

C Matrix element for A


A1(1,JM)=0.
A1(2,JM)=-0.5*SX
A1(3,JM)=1.+SX
A1(4,JM)=-0.5*SX
A1(5,JM)=0.

C Vector elements for d


12 D1(JM)= RR(J,K)

END DO

C FIRST LINE OF VECTOR D1 (Dirichlet BC)

C Modifed boundary condition


DT(1,K)=BCX1(N+1,K,DELT,DELY)-BCX1(N,K,DELT,DELY)
DT(1,K-1)=BCX1(N+1,K-1,DELT,DELY)-BCX1(N,K-1,DELT,DELY)
DT(1,K+1)=BCX1(N+1,K+1,DELT,DELY)-BCX1(N,K+1,DELT,DELY)

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))

C LAST LINE OF VECTOR D1 (Dirichlet BC)

C Modified boundary condition


DT(JMAX,K)=BCX2(N+1,K,DELT,DELY)-BCX2(N,K,DELT,DELY)
DT(JMAX,K-1)=BCX2(N+1,K-1,DELT,DELY)-BCX2(N,K-1,DELT,DELY)
DT(JMAX,K+1)=BCX2(N+1,K+1,DELT,DELY)-BCX2(N,K+1,DELT,DELY)

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))

C SOLVE BANDED SYSTEM OF EQUATIONS


C Subroutine is called only once because the
6.2. -formulation: Douglas-Gunn algorithm 83

C matrix A is independent of time and the factorization


C holds. Notice, the number of unknowns is JMAF.
IF(N.EQ.0)CALL LU(A1,JMAF)

C Subroutine must be called in every time step because


C the vector D is updated by the us.
CALL LUSOLVE(D1,DUM,A1,JMAF)

C Get the solution


C This nodal values are the values for the first
C half-step
DO J=2,JMAP
RR(J,K)=DUM(J-1)
END DO

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

C SET UP THE TRIDIAGONAL SYSTEM OF EQUATIONS

DO K=2,KMAP
KM=K-1

IF(N.GT.1)GOTO 13

C Matrix elements for A


A2(1,KM)=0.
A2(2,KM)=-0.5*SY
A2(3,KM)=1.+SY
84 Chapter 6. Solution of diffusion equation in two spatial dimensions

A2(4,KM)=-0.5*SY
A2(5,KM)=0.

C Vector elements for d


13 D2(KM)=RR(J,K)

END DO

C FIRST LINE OF VECTOR D2 (Dirichlet BC)

DT(J,1)= BCY1(N,J,DELT,DELX)-BCY1(N-1,J,DELT,DELX)

D2(1)=D2(1)+0.5*SY*DT(J,1)

C LAST LINE OF VECTOR D2 (Dirichlet BC)

DT(J,KMAX)=BCY2(N,J,DELT,DELX)-BCY2(N-1,J,DELT,DELX)

D2(KMAF)=D2(KMAF)+0.5*SY*DT(J,KMAX)

C SOLVE BANDED SYSTEM OF EQUATIONS


C Subroutine is called only once because the
C matrix A is independent of time and the factorization
C holds.
IF(N.EQ.1)CALL LU(A2,KMAF)

C Subroutine must be called in every time step because


C the vector D is updated by the us.
CALL LUSOLVE(D2,DUM,A2,KMAF)

C Get the solution


DO K=2,KMAP
DT(J,K)=DUM(K-1)
TN(J,K)=TN(J,K)+DT(J,K)
END DO
END DO

C Update boundary conditions


6.2. -formulation: Douglas-Gunn algorithm 85

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.

C Compare with exact solution


WRITE(1,FMT=(A10)) ERROR
DO K=1, KMAX
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,*)
86 Chapter 6. Solution of diffusion equation in two spatial dimensions

END DO
RMST=SQRT(SUMT/((JMAX-2)*(KMAX-2)))
WRITE(1,FMT=(A8,F15.5)) ERROR:,RMST

C IF MAXIMUM TIME OR MAXIMUM NUMEBR OF TIME-STEPS


C EXCEEDED EXIT FROM LOOP

IF(N.GE.NMAX)GOTO 18
IF(T.LT.TMAX)GOTO 11

C Plot contour plot and surface plot


C for the last step
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

6.2.3 Neumann boundary conditions for Douglas-Gunn method

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,

un+1 n+1 n+1


j+1,k = uj1,k + 2xgk , (6.45)
un+1 n+1 n+1
j,k+1 = uj,k1 + 2yhj , (6.46)

where,

gkn+1 = gkn+1 gkn , (6.47)


hn+1
j = hn+1
j hnj . (6.48)

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)

where, the new operators are,


2uj1,k 2uj,k
Lm
xx = , (6.51)
x2
2uj,k1 2uj,k
Lm
yy = . (6.52)
y 2

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

C SOLVES THE DIFFUSION EQUATION


C IN TWO DIMENSIONS USING
C THE ADI METHOD DIRICHLET & NEUMANN B.C.

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)

C JMAX number of points in x direction


C KMAX number of points in y direction
C NMAX maxumum number of time steps
C ALPHX diffusion coefficient in x
C ALPHY diffusion coefficient in y
C SX = ALPHX*DT/DX**2
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
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

C OBTAIN INITIAL CONDITIONS


90 Chapter 6. Solution of diffusion equation in two spatial dimensions

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

C SET BOUNDARY CONDITIONS (NEUMANN)


DO J=1,JMAX
TN(J,KMAX+1)=TN(J,KMAX-1)+2.*DELY*BCY2(N,J,DELT,DELX)
END DO
DO K=1,KMAX
TN(JMAX+1,K)=TN(JMAX-1,K)+2.*DELX*BCX2(N,K,DELT,DELY)
END DO

N=0

C EACH TIME STEP STARTS AT 11

C Right side vector components


11 DO K=2,KMAX
DO J=2,JMAX
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 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

C SET UP THE TRIDIAGONAL SYSTEM OF EQUATIONS


6.2. -formulation: Douglas-Gunn algorithm 91

C The number of unknowns is JAF=JMAX-2


C i.e. j=2,3,..., jmax
DO J=2,JMAX
JM=J-1

IF(N.GT.0)GOTO 12

C Matrix element for A


A1(1,JM)=0.
A1(2,JM)=-0.5*SX
A1(3,JM)=1.+SX
A1(4,JM)=-0.5*SX
A1(5,JM)=0.

A1(2,1)=0.
A1(2,JMAP)=-SX
A1(4,JMAP)=0.

C Vector elements for d


12 D1(JM)= RR(J,K)

END DO

C FIRST LINE OF VECTOR D1 (Dirichlet BC)

C Modifed boundary condition


DT(1,K)=BCX1(N+1,K,DELT,DELY)-BCX1(N,K,DELT,DELY)
DT(1,K-1)=BCX1(N+1,K-1,DELT,DELY)-BCX1(N,K-1,DELT,DELY)
DT(1,K+1)=BCX1(N+1,K+1,DELT,DELY)-BCX1(N,K+1,DELT,DELY)

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))

C LAST LINE OF VECTOR D1 (NEUMANN BC)

C Modified boundary condition

D1(JMAP)=D1(JMAP)
92 Chapter 6. Solution of diffusion equation in two spatial dimensions

DT(JMAX+1,K)=DT(JMAX-1,K)

C SOLVE BANDED SYSTEM OF EQUATIONS


C Subroutine is called only once because the
C matrix A is independent of time and the factorization
C holds. Notice, the number of unknowns is JMAF.
IF(N.EQ.0)CALL LU(A1,JMAP)

C Subroutine must be called in every time step because


C the vector D is updated by the us.
CALL LUSOLVE(D1,DUM,A1,JMAP)

C Get the solution


C This nodal values are the values for the first
C half-step
DO J=2,JMAX
RR(J,K)=DUM(J-1)
END DO

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

C SET UP THE TRIDIAGONAL SYSTEM OF EQUATIONS

DO K=2,KMAX
KM=K-1

IF(N.GT.1)GOTO 13
6.2. -formulation: Douglas-Gunn algorithm 93

C Matrix elements for A


A2(1,KM)=0.
A2(2,KM)=-0.5*SY
A2(3,KM)=1.+SY
A2(4,KM)=-0.5*SY
A2(5,KM)=0.

A2(2,KMAP)=-SY
A2(2,1)=0.
A2(4,KMAP)=0.

C Vector elements for d


13 D2(KM)=RR(J,K)

END DO

C FIRST LINE OF VECTOR D2 (Dirichlet BC)

DT(J,1)= BCY1(N,J,DELT,DELX)-BCY1(N-1,J,DELT,DELX)

D2(1)=D2(1)+0.5*SY*DT(J,1)

C LAST LINE OF VECTOR D2 (Neumann BC)

D2(KMAP)=D2(KMAP)
DT(J,KMAX+1)=DT(J,KMAX-1)

C SOLVE BANDED SYSTEM OF EQUATIONS


C Subroutine is called only once because the
C matrix A is independent of time and the factorization
C holds.
IF(N.EQ.1)CALL LU(A2,KMAP)

C Subroutine must be called in every time step because


C the vector D is updated by the us.
CALL LUSOLVE(D2,DUM,A2,KMAP)

C Get the solution


94 Chapter 6. Solution of diffusion equation in two spatial dimensions

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)

C Update boundary conditions


DO K=1,KMAX
TN(1,K)=BCX1(N,K,DELT,DELY)
C TN(JMAX,K)=BCX2(N,K,DELT,DELY)
TN(JMAX+1,K)=TN(JMAX-1,K)+2.*DELX*BCX2(N,K,DELT,DELY)
END DO

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.

C Compare with exact solution


WRITE(1,FMT=(A10)) ERROR
DO K=1, KMAX
6.2. -formulation: Douglas-Gunn algorithm 95

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

C IF MAXIMUM TIME OR MAXIMUM NUMEBR OF TIME-STEPS


C EXCEEDED EXIT FROM LOOP

IF(N.GE.NMAX)GOTO 18
IF(T.LT.TMAX)GOTO 11

C Plot contour plot and surface plot


C for the last step
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)
96 Chapter 6. Solution of diffusion equation in two spatial dimensions

CLOSE(2)
CLOSE(3)
STOP
END

The boundary conditions module is,

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

Solution of reaction-diffusion equations in two spatial dimensions

The solution of reaction-diffusion equations in two spatial dimensions does not involve many difficulties is the
reaction term does not contain spatial derivatives.

Thus, in the general case,

u 2u 2u
= ax 2 + ay 2 + f (u, ). (7.1)
t x y

Equation (135) is written in a two-level implicit scheme as follows,

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

Following the procedures of Sec.5.2 we arrive to the following algorithm,

(1 tax Lxx )(1 tay Lyy )un+1 n n


j,k = t(ax Lxx + ay Lyy )uj,k + tf (uj,k , ). (7.3)

Equation (137) can be splitted in two equations each evolving in only one spatial direction,

(1 tax Lxx )uj,k = t(ax Lxx + ay Lyy )unj,k + tf (unj,k , ), (7.4)


(1 tay Lyy )un+1
j,k = uj,k . (7.5)

Equations (138-139) can be solved in a similar manner as in Secs.5.2.1 and 5.2.3.

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).

[7] J.A. Sherratt, Appl. Math. Lett. 10 (1997) 1.

[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

Você também pode gostar