Sei sulla pagina 1di 22

Numerical Methods in Chemical Engineering

- 1 -
8 Partial Differential Equations


8 Partial Differential Equations........................................................................1
8.1 Overview..................................................................................................1
8.2 Types of partial differential equations.....................................................2
8.3 Solving PDEs numerically.......................................................................5
8.4 The method of lines (parabolic PDEs).....................................................6
8.4.1 Unsteady heat conduction in a slab...................................................6
8.4.2 The computer code..........................................................................13
8.4.3 The solution.....................................................................................15
8.5 Stability of the numerical solution.........................................................18
8.6 Summary................................................................................................22

8.1 Overview
In this handout, we will look at how we can compute numerical solutions to
partial differential equations. The standard types of differential equation will be
discussed (elliptic, parabolic, hyperbolic). Our basic approach will be to reduce
the PDE to a system of ODEs or algebraic equations. In fact, this approach was
used previously in Handout 4, where we solved laplace's equation.

Useful reading:
Chapter 6 of "Numerical Methods for Chemical Engineers with Matlab
Applications" by Constantinides and Mostoufi


Numerical Methods in Chemical Engineering
- 2 -

8.2 Types of partial differential equations
For a general 2nd Order PDE of the form

u
x
u
t
u
function
x
u
c
x t
u
b
t
u
a , , 2
2
2 2
2
2
(8-1)
We classify the PDE as follows
0 4
2
< ac b - Elliptic
0 4
2
= ac b - Parabolic
0 4
2
> ac b - Hyperbolic
These classifications are a bit abstract, but they do provide a useful grouping.
For computational purposes, it is also useful to look at the independent variables
and decide whether they are one or two-way coordinates
1
.

Parabolic equations
e.g. the equation which governs heat conduction in one dimension
0
2
2
=

x
T
C
k
t
T
p

(8-2)
we can identify t and x as the independent variables. The equation contains a
second derivative in x (i.e. a diffusive term), which means that the value of T at a
given point in space depends on the temperatures surrounding the point in both
directions. Space is a two-way coordinate. By contrast, there is only a first
derivative in time; the current temperature only depends on the past temperature,
not the future temperature. Time is a one-way coordinate. We will see later that
this means we can march the solution forward in time. Parabolic equations
are usually initial value problems!

1
See "Numerical heat transfer and fluid flow", by Patankar S.V, (1980)
Numerical Methods in Chemical Engineering
- 3 -
o In general, a parabolic problem will contain a one-way co-ordinate.
Elliptic Equations
e.g. Laplace's equation for steady state heat conduction in two dimensions.
0
2
2
2
2
=

y
T
x
T
(8-3)
Both the independent variables, x and y are now two-way coordinates. We
solved this equation in lecture 4, on a rectangular grid. We had to solve for all
values simultaneously, rather than being able to march forward along a
particular co-ordinate. You should recall that the temperatures were specified on
all 4 boundaries (the boundaries which enclosed the domain). Elliptic equations
often arise in boundary value problems.

Hyperbolic equations
e.g. the wave equation
0
2
2
2
2
=

x
u
c
t
u

We will not look at hyperbolic problems in the course. However, you can see
that all the co-ordinates are two-way. There are in-fact, characteristic directions
along which the problem will behave as if it had a one-way coordinate (e.g.
waves).







Numerical Methods in Chemical Engineering
- 4 -


Question: can space be a one-way co-ordinate?

The answer is (approximately) yes. Recall the way in which you solved the
equations for conversion of a species in a plug flow reactor with a first order
reaction:

V
kC
dz
dC
=
C =concentration
V =velocity of fluid (m/s)
k =first order rate constant
With this kind of equation, we could use the conditions at the start of the reactor,
and march forward to the end of the reactor. We have of course neglected
dispersion. Thus, when convection becomes more important than axial
dispersion, space becomes a one-way co-ordinate system.











Numerical Methods in Chemical Engineering
- 5 -
8.3 Solving PDEs numerically
In order to solve a PDE, we must first reduce the PDE to either a system of
equations (possibly non-linear), or a system of ODEs, which can be marched
forward in time. To accomplish this, the domain of the solution must be
discretised. In lecture 4, we turned the 2D Laplace problem into a set of linear
equations using finite differences. There are 3 methods that are generally used to
discretise the spatial domain:

1 - finite differences (e.g. lecture 4 and this lecture)
2 - finite volumes (next handout)
3 - finite elements













Numerical Methods in Chemical Engineering
- 6 -
8.4 The method of lines (parabolic PDEs)
In the method of lines, we discretise the spatial domain of our PDE, to produce a
set of ODEs which govern the temperature at each point in the solution domain.
Here we will use finite differences to accomplish this.

8.4.1 Unsteady heat conduction in a slab
Lets return the problem we solved in handout 4 (Laplace's equation for steady
state heat conduction), and this time look at unsteady heat conduction.
The unsteady heat conduction equation is
T
t
T
2
=

, (8-4)
where is the thermal diffusivity. In two dimensions (and Cartesian co-
ordinates) this can be written as:

2
2
2
2
y
T
x
T
t
T
(8-5)
We are going to solve this problem on the following domain,

T =T
b3

T =T
b4
T =T
b1
T =T
b2
Initially, the
temperature is
uniform at T
o

y
x
Numerical Methods in Chemical Engineering
- 7 -
To proceed, we place a grid over our solution domain, and track the temperature
at each point on the grid. For simplicity, we will use a regular, equally spaced
grid (x =y).
j =Ny (Ny-1)Nx +1
..
j =3
j =2 T
Nx+1
T
Nx+2
T
Nx+3
... T
2Nx

j =1 T
1
T
2
T
3
... T
Nx

i =1 i =2 i =3 i =Nx
The index of the general node is
k =i +Nx (j 1) (8-6)
So that T
i,j
=T
k =i +Nx (j 1)
(8-7)

We can now discretise (8-5) to find an ODE which governs the temperature at
node k.

=
j i j i
j i
y
T
x
T
dt
dT
,
2
2
,
2
2
,
(8-8)

We need estimates of the second differentials at node k. These can be obtained
from finite differences. We can assume a piece-wise linear profile in
temperature, i.e.
Numerical Methods in Chemical Engineering
- 8 -

Then we can estimate the second derivative as

( ) ( )
x
x
T T
x
T T
x
x
T
x
T
x
T
j i j i j i j i
i i

+
+
, 1 , , , 1
2 / 1 2 / 1
2
2


2
, 1 , , 1
2
x
T T T
j i j i j i

+
=
+
(8-9)

If we make a similar approximation for the second derivative in the y direction,
we can write the rate of change of temperature at node k (i.e. T
i, j
) as,

+
+

+
=
+ +
2
1 , , 1 ,
2
, 1 , , 1 ,
2 2
y
T T T
x
T T T
dt
dT
j i j i j i j i j i j i j i
(8-10)
or in terms of the node indices

+
+

+
=
+ +
2 2
1 1
2 2
y
T T T
x
T T T
dt
dT
Nx k k Nx k k k k k
. (8-11)

i

i - 1
i +1
T
i

T
i - 1

T
i +1
T
x
x
x
Numerical Methods in Chemical Engineering
- 9 -

An aside:
To recover the steady state problem, we set

dt
dT
k
=0.
This will then result in a discretised form of Laplace's Equation (elliptic),
identical to the problem we met in handout 4.

The boundary nodes have a fixed temperature, so don't obey (8-11). There are
three ways to proceed to take account of the boundary nodes:
1. Eliminate the temperatures at boundary nodes from the set of equations
given by (8-11).
2. The nodes at the boundary have a constant temperature, so write
dt
dT
boundary
=0. Each boundary node will then remain the initial
temperature supplied as an initial condition.
3. Include the boundary node equations as a set of algebraic equations
which must be solved alongside the set of ODEs produced by (8-11).

Here we will use option (1)
2
. However, it should be noted that the implicit
solvers in Matlab can be made to solve systems of coupled ODEs and Algebraic
equations.




2
Note that in Handout 4, method (3) was used. The equations for the boundary nodes were just solved alongside
the equations for the internal nodes.
Numerical Methods in Chemical Engineering
- 10 -

An aside: Solving systems of ODEs and algebraic equations, simultaneously, is
not a difficult as it sounds, since an implicit solver must solve a set of equations
to get the update at each time step. Solving a few extra, coupled, algebraic
equations is no big deal. Have a look in the help file of Matlab for ODE15s (in
particular, the mass matrix).
Instead of solving
) (x F
dt
x d
= ,
You can solve
) (x F
dt
x d
M =
where M is a mass matrix, which pre-multiplies the rate of change vector. For
most problems, the mass matrix is M =I, which is the default setting used by
Matlab. To specify an algebraic equation, the row of the mass matrix which
corresponds to that equation will contain all zeros.

Since we only want to march the internal nodes forward in time, the nodes can
be renumbered as shown below. The white nodes are internal, whilst the black
nodes are on the boundary and not modelled. With this notation Ny and Nx are
now the number of internal nodes in the x and y direction, respectively.






Numerical Methods in Chemical Engineering
- 11 -













Equation (8-11) give us a set of linear ODEs

A : Nodes not on border (y=x)

( )
Nx k k Nx k k k k
k
T T T T T T
x dt
dT
+ +
+ + + = 2 2
1 1
2

(8-12)
To eliminate boundary conditions we note that
Node on border 1 ->(T
k-Nx
=T
b1
)
Node on border 2 ->(T
k+Nx
=T
b2
)
Node on border 3 ->(T
k-1
=T
b3
)
Node on border 4 ->(T
k+1
=T
b4
)
So for example at B;
( )
1
2
1 1
2
2 2
b k Nx k k k k
k
T
x
T T T T T
x dt
dT

+ + + =
+ +





j = Ny T
Nx(Ny-1)+1
T
Nx(Ny-1)+2
T
2Nx

j =2 T
Nx+1
T
2Nx

j =1 T
1
T
Nx+2
T
Nx


i =1 i =2 i =Nx

A
B
4
2
1
3
Numerical Methods in Chemical Engineering
- 12 -
etc.....
So the equations can be written in the form
b T A
dt
T d
+ = (8-13)
where b is a vector which is zero for internal nodes, but contains the boundary
temperatures for nodes which are next to the artificial boundary we have
imposed. These are written out in full for Nx =3 and Ny =4


+
+
+
+
+

4 2
2
3 2
4
3
4
3
4 1
1
3 1
2
12
11
10
9
8
7
6
5
4
3
2
1
2
12
11
10
9
8
7
6
5
4
3
2
1
0
0
4 1 0 1 0 0 0 0 0 0 0 0
1 4 1 0 1 0 0 0 0 0 0 0
0 1 4 0 0 1 0 0 0 0 0 0
1 0 0 4 1 0 1 0 0 0 0 0
0 1 0 1 4 1 0 1 0 0 0 0
0 0 1 0 1 4 0 0 1 0 0 0
0 0 0 1 0 0 4 1 0 1 0 0
0 0 0 0 1 0 1 4 1 0 1 0
0 0 0 0 0 1 0 1 4 0 0 1
0 0 0 0 0 0 1 0 0 4 1 0
0 0 0 0 0 0 0 1 0 1 4 1
0 0 0 0 0 0 0 0 1 0 1 4
b b
b
b b
b
b
b
b
b b
b
b b
T T
T
T T
T
T
T
T
T T
T
T T
x
T
T
T
T
T
T
T
T
T
T
T
T
x
T
T
T
T
T
T
T
T
T
T
T
T
dt
d


So to solve this problem, all we need to do is write a function which returns the
rate of change of temperature at each node:
f unct i on DT = dT_dt ( t , T)
%gi ve t hi s r out i ne access t o t he gl obal var i abl es A and b
gl obal A
gl obal b

%cal cul at e t he r at es of change
DT = A*T +b;

k =Nx +1
Numerical Methods in Chemical Engineering
- 13 -
r et ur n
This function is called many times during the solution, so we don't want to have
to reallocate A and b each time the function is called (this would be very
inefficient, given that A and b are constant). We can set A and b as global
variables in the main program, before we call the ODE solver.
8.4.2 The computer code
f unct i on Heat Conduct i on( Tb1, Tb2, Tb3, Tb4, To, al pha)
%sol ves t he t r ansi ent heat conduct i on equat i on on a r ect angul ar gr i d
%t he domai n i s bounded by 4 const ant t emper at ur e boundar i es
( Tb1, Tb2, Tb3, Tb4)
%
% Tb2
% - - - - - - - - - - - - -
% ! !
%Tb3 ! ! Tb4
% ! !
% ! !
% - - - - - - - - - - - - -
% Tb1
%The i ni t i al t emper at ur e i s To. The gr i d i s assumed t o be equal l y spaced
%al pha = t her mal di f f usi vi t y

gl obal Nx
gl obal Ny
gl obal d
gl obal Tscal e

%t he gr i d si ze
Nx=10; %number of poi nt s al ong x di r ect i on
Ny=10; %number of poi nt s i n t he y di r ect i on
d = 0. 1; %t he gr i d spaci ng

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%set t he mat r i x A and t he Ri ght hand si de vect or ( pl ace t hemi n gl obal
%memor y t o st op us havi ng t o cont i nual l y r eal l ocat e t hem)

gl obal A
e = ones( Nx*Ny, 1) ;
A = spdi ags( [ e, e, - 4*e, e, e] , [ - Nx, - 1, 0, 1, Nx] , Nx*Ny, Nx*Ny) ;
A = A*al pha/ d^2;

gl obal b
b = l i nspace( 0, 0, Nx*Ny) ' ;
f or i = 1: Nx
%bor der 1
j = 1;
k = i + ( Nx) *( j - 1) ;
b( k) = b( k) + Tb1*al pha/ d^2;
Numerical Methods in Chemical Engineering
- 14 -
%bor der 2
j = Ny;
k = i + ( Nx) *( j - 1) ;
b( k) = b( k) + Tb2*al pha/ d^2;
end

f or j = 1: Ny
%bor der 3
i = 1;
k = i + ( Nx) *( j - 1) ;
b( k) = b( k) + Tb3*al pha/ d^2;
i f ( k- 1>0)
A( k, k- 1) = 0;
end

%bor der 4
i = Nx;
k = i + ( Nx) *( j - 1) ;
b( k) = b( k) + Tb4*al pha/ d^2;
i f ( k+1<Nx*Ny)
A( k, k+1) = 0;
end

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%t he heat i ng of t he sl ab has a char act er i st i c t i me
t i me_const ant = ( ( Nx+1) *d/ 2) ^2/ al pha;

%t he i ni t i al t emper at ur e vect or
I ni t i al Temp = l i nspace( To, To, Nx*Ny) ' ;

%set some opt i ons f or t he odesol ver ( comment t he l i ne bel ow t o t ur n of f
%pl ot t i ng)
opt i ons = odeset ( ' Out put Fcn' , @Pl ot t i ng) ;

%a t emper at ur e scal e used by t he pl ot t i ng r out i ne
Tscal e = max( [ Tb1, Tb2, Tb3, Tb4] ) ;

%cal l t he ODEsovl er
[ t , T] =ode45( @dT_dt , [ 0, 3*t i me_const ant ] , I ni t i al Temp, opt i ons)

r et ur n

Numerical Methods in Chemical Engineering
- 15 -
8.4.3 The solution
If we heat up a slab by applying constant temperatures to each of its faces, the
slab will reach an equilibrium (see handout 4). The time constant for reaching
this equilibrium is

2
L
=

where L is characteristic length scale for conduction (a value of half the width is
appropriate)

[ ] ( )

2
2 / 1 x Nx +
=

This time constant gives us an idea over how long we should run the simulation
for. Now lets set the initial temperature of the slab to 0 C, the bottom to be a
constant temperature of 5 C and the 3 remaining sides to a constant temperature
of 10 C.

t = 0.0001










0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
2
3
4
5
6
7
8
9
Numerical Methods in Chemical Engineering
- 16 -


t = 0.1

















t = 0.5















0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
2
3
4
5
6
7
8
9
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
2
3
4
5
6
7
8
9
Numerical Methods in Chemical Engineering
- 17 -
t = 1













t =


















Question: How can you easily compute the temperature profile at t = ?
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
2
3
4
5
6
7
8
9
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
2
3
4
5
6
7
8
9
Numerical Methods in Chemical Engineering
- 18 -
8.5 Stability of the numerical solution
In the previous section, we reduced a PDE to set of ODEs of the form
b T A
dt
T d
+ = . (8-13)
In Handout 6, we saw that it was the J acobian of the RHS of the system of ODEs
which determined whether or not the numerical solution was stable. In
particular, it is the eigenvalue of the J acobian with the largest magnitude which
determines the largest time step you can take.

The J acobian for the system of equations given by (8-13) is simply
J = A
where

=
4 1 0 1 0 0 0 0 0 0 0 0
1 4 1 0 1 0 0 0 0 0 0 0
0 1 4 0 0 1 0 0 0 0 0 0
1 0 0 4 1 0 1 0 0 0 0 0
0 1 0 1 4 1 0 1 0 0 0 0
0 0 1 0 1 4 0 0 1 0 0 0
0 0 0 1 0 0 4 1 0 1 0 0
0 0 0 0 1 0 1 4 1 0 1 0
0 0 0 0 0 1 0 1 4 0 0 1
0 0 0 0 0 0 1 0 0 4 1 0
0 0 0 0 0 0 0 1 0 1 4 1
0 0 0 0 0 0 0 0 1 0 1 4
2
x
A







Numerical Methods in Chemical Engineering
- 19 -
If we recall from Handout 4:
Gershgorin's theorem states
for a square matrix and row k, an eigenvalue is located on the complex plane
within a radius equal to the sum of the moduli of the off-diagonal elements of
that row.

i.e.
N k k N k k k k k
a a a a a a , , , , , ,
1 1 2 2 1
+ + + + +

K (4-21)

This means that all the eigenvalues should be within a radius of 4/x
2
units
from the point (-4/x
2
,0) on an Argand diagram.

The worst possible case should be an eigenvalue of -8/x
2
. (If you run the
transient heat conduction problem from section 8.3 and stop the program half
way through, you can use the eigs(A) command to calculate the largest
magnitude eigenvalues; you will find they are very close to -8/x
2
).
Re()
Im()
(-4/x
2
,0)
Radius =4/x
2

(-8/x
2
,0)
Numerical Methods in Chemical Engineering
- 20 -

Now suppose we had used the explicit Euler method to step forward in time. In
handout 4 it was shown that the Euler scheme is only stable if
2 < dt

8
2
2
x
dt < .
This rather simple result highlights a problem we face when solving PDEs. If we
use an explicit time stepping scheme then there will generally be maximum
stepsize we can take in order to produce a stable solution. The smaller the grid
spacing, the smaller this step size, and thus the more time steps the solver will
have to take.
o The magnitude of the eigenvalues of the J acobian depend on the grid
spacing.
o Reducing the grid spacing, to improve the quality of the solution can
result in the solution becoming unstable.
o An implicit time stepping scheme we won't be constrained by a limiting
time step.

Although there is no restriction on the time step for stability when using an
implicit scheme, the time step still has to be small enough to ensure that the
solution is physically sensible.






Numerical Methods in Chemical Engineering
- 21 -
In section 88.4.2 ODE45, which uses an explicit routine, was used. This routine
will automatically reduce the time step until it has produced a stable solution.
There are two problems in doing this:
1) a smaller time step means more time steps have to be taken (so the
solution takes a long time to produce),
2) There may be conditions where the time step can be reduced to within
the finite precision of the computer.

Question: How could the code be changed so that it uses ODE15s efficiently?
%set some opt i ons f or t he odesol ver ( comment t he l i ne bel ow t o
t ur n of f pl ot t i ng)
opt i ons = odeset ( ' Out put Fcn' , @Pl ot t i ng, ' J acobi an' , A) ;

%cal l t he ODEsovl er
[ t , T] =ode15s( @dT_dt , [ 0, 3*t i me_const ant ] , I ni t i al Temp, opt i ons)
Numerical Methods in Chemical Engineering
- 22 -
8.6 Summary
We have seen in this lecture that we can solve a parabolic PDE numerically by
discretising the spatial domain to produce a set of ODEs.

o PDEs can be turned into a set of ODEs by discretisation
o PDEs produce systems with large, sparse J acobians
o The eigenvalues of the J acobian depend on the grid spacing. If the grid
spacing is made too small, the ODEs become stiff and an implicit ODE
solver might be needed.

Potrebbero piacerti anche