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
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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
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;
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
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
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
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
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
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)
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.
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)
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