nummeth handout 8

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

Upload: edwincaribegonsales

Post on 30-Apr-2017

235 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: NumMeth Handout 8

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

Page 2: NumMeth Handout 8

Numerical Methods in Chemical Engineering

- 2 -

8.2 Types of partial differential equations

For a general 2nd Order PDE of the form

⎟⎠⎞

⎜⎝⎛

∂∂

∂∂

=∂∂

+∂∂

∂+

∂∂ u

xu

tufunction

xuc

xtub

tua ,,2 2

22

2

2

(8-1)

We classify the PDE as follows

042 <− acb - Elliptic

042 =− acb - Parabolic

042 >− acb - 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 coordinates1.

Parabolic equations

e.g. the equation which governs heat conduction in one dimension

02

2

=∂∂

+∂∂

xT

Ck

tT

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)

Page 3: NumMeth Handout 8

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.

02

2

2

2

=∂∂

+∂∂

yT

xT (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

02

2

2

2

=∂∂

−∂∂

xuc

tu

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

Page 4: NumMeth Handout 8

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:

VkC

dzdC

−=

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.

Page 5: NumMeth Handout 8

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

Page 6: NumMeth Handout 8

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

TtT 2∇=

∂∂ α , (8-4)

where α is the thermal diffusivity. In two dimensions (and Cartesian co-

ordinates) this can be written as:

⎟⎟⎠

⎞⎜⎜⎝

⎛∂∂

+∂∂

=∂∂

2

2

2

2

yT

xT

tT α (8-5)

We are going to solve this problem on the following domain,

T = Tb3 T = Tb4

T = Tb1

T = Tb2

Initially, the temperature is uniform at To

y

x

Page 7: NumMeth Handout 8

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 TNx+1 TNx+2 TNx+3 ... T2Nx

j = 1 T1 T2 T3 ... TNx

i = 1 i = 2 i = 3 i = Nx

The index of the general node is

k = i +Nx ×(j – 1) (8-6)

So that Ti,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.

⎟⎟

⎜⎜

∂∂

+∂∂

=jiji

ji

yT

xT

dtdT

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

Page 8: NumMeth Handout 8

Numerical Methods in Chemical Engineering

- 8 -

Then we can estimate the second derivative as

( ) ( )

xxTT

xTT

xxT

xT

xT

jijijiji

ii

∆∆

−−

∆∂∂

−∂∂

≈∂∂

−+

−+

,1,,,1

2/12/12

2

2

,1,,1 2

x

TTT jijiji

+−=

−+ (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. Ti, j) as,

⎟⎟⎠

⎞⎜⎜⎝

+−+

+−= −+−+

21,,1,

2,1,,1, 22

y

TTT

x

TTTdt

dT jijijijijijiji α (8-10)

or in terms of the node indices

⎟⎟⎠

⎞⎜⎜⎝

∆+−

+∆

+−= −+−+

2211 22

yTTT

xTTT

dtdT NxkkNxkkkkk α . (8-11)

i

i - 1 i + 1

Ti

Ti - 1

Ti + 1 T

x∆x∆x

Page 9: NumMeth Handout 8

Numerical Methods in Chemical Engineering

- 9 -

An aside:

To recover the steady state problem, we set

dt

dTk = 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

dtdTboundary = 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.

Page 10: NumMeth Handout 8

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

)(xFdt

xd= ,

You can solve

)(xFdt

xdM =

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.

Page 11: NumMeth Handout 8

Numerical Methods in Chemical Engineering

- 11 -

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

A : Nodes not on border (∆y=∆x)

( )NxkkNxkkkkk TTTTTT

xdtdT

−+−+ +−++−= 22 112∆α

(8-12)

To eliminate boundary conditions we note that

Node on border 1 -> (Tk-Nx = Tb1)

Node on border 2 -> (Tk+Nx = Tb2)

Node on border 3 -> (Tk-1 = Tb3)

Node on border 4 -> (Tk+1 = Tb4)

So for example at B;

( ) 1211222 bkNxkkkk

k Tx

TTTTTxdt

dT∆α

∆α

+−+−+= +−+

j = Ny TNx(Ny-1)+1 TNx(Ny-1)+2 T2Nx

j = 2 TNx+1 T2Nx

j = 1 T1 TNx+2 TNx

i = 1 i = 2 i = Nx

A

B 4

2

1

3

Page 12: NumMeth Handout 8

Numerical Methods in Chemical Engineering

- 12 -

etc.....

So the equations can be written in the form

bTAdtTd

+= (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

⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢

+

+

+

+

+

⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢

−−

−−

−−

−−

−−

−−

=

⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢

42

2

32

4

3

4

3

41

1

31

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

410100000000141010000000014001000000100410100000010141010000001014001000000100410100000010141010000001014001000000100410000000010141000000001014

bb

b

bb

b

b

b

b

bb

b

bb

TTT

TTT

TT

TTT

TTT

x

TTTTTTTTTTTT

x

TTTTTTTTTTTT

dtd

∆α

∆α

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:

function DT = dT_dt(t,T) %give this routine access to the global variables A and b global A global b %calculate the rates of change DT = A*T +b;

k = Nx + 1

Page 13: NumMeth Handout 8

Numerical Methods in Chemical Engineering

- 13 -

return

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 function HeatConduction(Tb1,Tb2, Tb3, Tb4,To,alpha) %solves the transient heat conduction equation on a rectangular grid %the domain is bounded by 4 constant temperature boundaries (Tb1,Tb2,Tb3,Tb4) % % Tb2 % ------------- % ! ! % Tb3 ! ! Tb4 % ! ! % ! ! % ------------- % Tb1 %The initial temperature is To. The grid is assumed to be equally spaced %alpha = thermal diffusivity

global Nx global Ny global d global Tscale %the grid size Nx=10; %number of points along x direction Ny=10; %number of points in the y direction d = 0.1; %the grid spacing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %set the matrix A and the Right hand side vector (place them in global %memory to stop us having to continually reallocate them) global A e = ones(Nx*Ny,1); A = spdiags([e,e,-4*e,e,e],[-Nx,-1,0,1,Nx],Nx*Ny,Nx*Ny); A = A*alpha/d^2; global b b = linspace(0,0,Nx*Ny)'; for i = 1:Nx %border 1 j = 1; k = i + (Nx)*(j-1); b(k) = b(k) + Tb1*alpha/d^2;

Page 14: NumMeth Handout 8

Numerical Methods in Chemical Engineering

- 14 -

%border 2 j = Ny; k = i + (Nx)*(j-1); b(k) = b(k) + Tb2*alpha/d^2; end for j = 1:Ny %border 3 i = 1; k = i + (Nx)*(j-1); b(k) = b(k) + Tb3*alpha/d^2; if (k-1>0) A(k,k-1) = 0; end %border 4 i = Nx; k = i + (Nx)*(j-1); b(k) = b(k) + Tb4*alpha/d^2; if (k+1<Nx*Ny) A(k,k+1) = 0; end end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %the heating of the slab has a characteristic time time_constant = ((Nx+1)*d/2)^2/alpha; %the initial temperature vector InitialTemp = linspace(To,To,Nx*Ny)'; %set some options for the odesolver (comment the line below to turn off %plotting) options = odeset('OutputFcn',@Plotting); %a temperature scale used by the plotting routine Tscale = max([Tb1,Tb2,Tb3,Tb4]); %call the ODEsovler [t,T]=ode45(@dT_dt,[0,3*time_constant],InitialTemp,options) return

Page 15: NumMeth Handout 8

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

α

τ2L

=

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

appropriate)

[ ]( )α

∆τ22/1 xNx +

=

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

Page 16: NumMeth Handout 8

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

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

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

Page 17: NumMeth Handout 8

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

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

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

Page 18: NumMeth Handout 8

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

bTAdtTd

+= . (8-13)

In Handout 6, we saw that it was the Jacobian 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 Jacobian with the largest magnitude which

determines the largest time step you can take.

The Jacobian for the system of equations given by (8-13) is simply

J = A

where

⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢

−−

−−

−−

−−

−−

−−

=

410100000000141010000000014001000000100410100000010141010000001014001000000100410100000010141010000001014001000000100410000000010141000000001014

2xA

∆α

Page 19: NumMeth Handout 8

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. NkkNkkkkk aaaaaa ,,,,,, 11221 +++++≤− −−Kλ (4-21)

This means that all the eigenvalues should be within a radius of 4α/∆x2 units

from the point (-4α/∆x2,0) on an Argand diagram.

The worst possible case should be an eigenvalue of -8α/∆x2. (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α/∆x2).

Re(λ)

Im(λ)

(-4α/∆x2,0)

Radius = 4α/∆x2

(-8α/∆x2,0)

Page 20: NumMeth Handout 8

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 2xdt < .

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

Page 21: NumMeth Handout 8

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 options for the odesolver (comment the line below to turn off plotting) options = odeset('OutputFcn',@Plotting,'Jacobian',A); %call the ODEsovler [t,T]=ode15s(@dT_dt,[0,3*time_constant],InitialTemp,options)

Page 22: NumMeth Handout 8

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 Jacobians

o The eigenvalues of the Jacobian 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.