Sie sind auf Seite 1von 12

Computational Fluid Dynamics I

Computational Fluid Dynamics I



http://users.wpi.edu/~gretar/me612.html! Objectives:!

Introduce the basic concepts needed to solve a partial


Elementary! differential equation using finite difference methods. !

Numerical Analysis:! Discuss basic time integration methods, ordinary and


partial differential equations, finite difference
Finite Difference approximations, accuracy. !

Approximations-II! Show the implementation of numerical algorithms into


actual computer codes.!

Grétar Tryggvason!
Spring 2010!

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Outline!

•  Solving partial differential equations!


! !Finite difference approximations!
! !The linear advection-diffusion equation!
! !Matlab code!
! !Accuracy and error quantification!
Finite Difference
! !Stability!
! !Consistency!
Approximations of
! !Multidimensional problems!
! !Steady state! the Derivatives!

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Finite Difference Approximations! Finite Difference Approximations!

Derive a numerical approximation to the governing

!
equation, replacing a relation between the derivatives by
a relation between the discrete nodal values.

The!
Δt

f (t + 2Δt) Time Derivative!
f (t + Δt)
Δt
f(t,x-h) f(t,x) f(t,x+h) !
f (t)
h
h

Computational Fluid Dynamics I
Computational Fluid Dynamics I

Finite Difference Approximations! Finite Difference Approximations!

Time derivative!
The Time Derivative is found using a FORWARD

using a Taylor series !


EULER method. The approximation can be found by
f (t + Δt) = f (t) +
∂f (t)
∂t
Δt +
∂ 2 f (t) Δt 2
∂t 2 2
+

Solving this equation for the time derivative gives:!


f(t+Δt,x)

Δt
f(t,x)! ∂f (t) f (t + Δt) − f (t) ∂ 2 f (t) Δt
= − +
h
h
∂t Δt ∂t 2 2

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Apply this to the ODE from last lecture:!

The original equation at time level n is:!


⎛ df ⎞
n
Solving a!
⎝ dt ⎠ = g
n

And using the approximation just derived for the partial differential
time derivative results in:!
f n+1 − f n
= g n + O(Δt)
equation!
Δt
which is exactly the first order forward Euler method. !

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Model Equations! Model Equations!

For initial conditions of the form:!


We will use the model equation:!
f (x, t = 0) = Asin(2πkx)
∂f ∂f ∂2 f
+U =D 2 It can be verified by direct substitution that the solution
∂t ∂x ∂x is given by:!
f (x, t) = e − Dk t sin(2πk(x − Ut))
2

to demonstrate how to solve a partial equation numerically.!

Although this equation is much simpler than the full


Navier Stokes equations, it has both an advection term which is a decaying
and a diffusion term. ! traveling wave!
Before attempting to solve the equation, it is useful to
understand how the analytical solution behaves.!
Computational Fluid Dynamics I
Computational Fluid Dynamics I

Finite Difference Approximations! Finite Difference Approximations!

When using FINITE DIFFERENCE approximations,


the values of f are stored at discrete points.!
The Spatial! f(x-h) f(x) f(x+h) !

First Derivative! h
h

The derivatives of the function are approximated


using a Taylor series!

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Finite Difference Approximations! Finite Difference Approximations!

Start by expressing the value of f(x+h) and f(x-h) in terms of f(x)


The result is:!
∂f (x) ∂ 3 f (x) h 3
∂f (x) ∂ 2 f (x) h 2 ∂ 3 f (x) h 3 ∂ 4 f (x) h 4 f (x + h) − f (x − h) = 2 h+2 +
f (x + h) = f (x) + h+ + + + ∂x ∂x 3 6
∂x ∂x 2 2 ∂x 3 6 ∂x 4 24
∂f (x) ∂ 2 f (x) h 2 ∂ 3 f (x) h 3 ∂ 4 f (x) h 4 Rearranging this equation to isolate the first derivative:!
f (x − h) = f (x) − h+ − + −
∂x ∂x 2 2 ∂x 3 6 ∂x 4 24

Subtracting the second equation from the first:! ∂f (x) f (x + h) − f (x − h) ∂ 3 f (x) h 2


= − +
∂x 2h ∂x 3 6
∂f (x) ∂ 3 f (x) h 3
f (x + h) − f (x − h) = 2 h+2 +
∂x ∂x 3 6

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Finite Difference Approximations! Finite Difference Approximations!

Start by expressing the value of f(x+h) and f(x-h) in terms of f(x)


∂f (x) ∂ 2 f (x) h 2 ∂ 3 f (x) h 3 ∂ 4 f (x) h 4


f (x + h) = f (x) + h+ + + +
The Spatial! ∂x ∂x 2 2 ∂x 3 6 ∂x 4 24
∂f (x) ∂ 2 f (x) h 2 ∂ 3 f (x) h 3 ∂ 4 f (x) h 4
Second Derivative! f (x − h) = f (x) −
∂x
h+
∂x 2 2

∂x 3 6
+
∂x 4 24
−

Adding the second equation to the first:!

∂f 2 (x) h 2 ∂ 4 f (x) h 4
f (x + h) + f (x − h) = 2 f (x) + 2 +2 +
∂x 2
2 ∂x 4 24
Computational Fluid Dynamics I
Computational Fluid Dynamics I

Finite Difference Approximations! Finite Difference Approximations!

The result is:!

∂f 2 (x) h 2 ∂ 4 f (x) h 4
f (x + h) + f (x − h) = 2 f (x) + 2 +2 +
∂x 2
2 ∂x 4 24 Solving the partial
Rearranging this equation to isolate the second derivative:!
differential equation!
∂ 2 f (x) f (x + h) − 2 f (x) + f (x − h) ∂ 4 f (x) h 2
= − +
∂x 2 h2 ∂x 4 12

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Finite Difference Approximations! Finite Difference Approximations!

We already introduced the notation! f = f (t) A numerical approximation to!


n

For space and time we will use:! ∂f ∂f ∂2 f


+U =D 2
∂t ∂x ∂x
f jn = f (t, x j ) Is found by replacing the derivatives by the following
f jn+1 approximations!
f jn+1 = f (t + Δt, x j ) ⎛∂ 2 f ⎞
n
⎛ ∂f ⎞ ⎛ ∂f ⎞
n n

f jn−1 f jn f jn+1 Δt +U = D⎜ 2
f jn+1 = f (t, x j + h) ⎝ ∂t ⎠ j ⎝ ∂x ⎠ j ⎝ ∂x ⎠ j
f jn−1 = f (t, x j − h)
h
h

f jn+1

f jn−1 f jn f jn+1 Δt
h
h

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Finite Difference Approximations! Finite Difference Approximations!

Using our shorthand ! Substituting these approximations into:!


gives!
notation! ∂f ∂f ∂2 f
⎛ ∂f ⎞
n
f n+1 − f jn +U =D 2
= j + O(Δt) ∂t ∂x ∂x
f jn = f (t, x j ) ⎝ ∂t ⎠ j Δt
gives!
f jn+1 = f (t + Δt, x j )
⎛ ∂f ⎞
n
f n − f jn−1
f jn+1 = f (t, x j + h) = j +1 + O(h2 ) f jn+1 − fjn f jn+1 − fjn−1 fjn+1 − 2 f jn + f jn−1
⎝ ∂x ⎠ j 2h +U =D + O(Δt, h 2 )
Δt 2h h2
f jn−1 = f (t, x j − h) n
⎜⎛ ∂ f ⎞ = f j +1 − 2 f j + f j −1 + O(h2 )
2 n n n

⎝ ∂x 2 ⎠ j Solving for the new value and dropping the error terms yields!
h2
UΔt n DΔt
f jn+1 = f jn − ( f j +1 − f jn−1) + 2 ( f jn+1 − 2 f jn + f jn−1)
2h h
Computational Fluid Dynamics I
Computational Fluid Dynamics I

Finite Difference Approximations!

Thus, given f at one time (or time level), f at the


next time level is given by:!

UΔt n DΔt
f jn+1 = f jn − ( f − f jn−1) + 2 ( f jn+1 − 2 f jn + f jn−1)
2h j +1 h Example!
The value of every point at level n+1 is given
explicitly in terms of the values at the level n!

f jn+1

f jn−1 f jn f jn+1 Δt
h
h

Computational Fluid Dynamics I


Computational Fluid Dynamics I

EX2!

% one-dimensional advection-diffusion by the FTCS scheme!


n=21; nstep=100; length=2.0; h=length/(n-1); dt=0.05; D=0.05;!
f=zeros(n,1); y=zeros(n,1); ex=zeros(n,1); time=0.0;!
A short MATLAB program! for i=1:n, f(i)=0.5*sin(2*pi*h*(i-1)); end;
for m=1:nstep, m, time!
% initial conditions!

for i=1:n, ex(i)=exp(-4*pi*pi*D*time)*...!


0.5*sin(2*pi*(h*(i-1)-time)); end; % exact solution !
The evolution of a sine wave is followed as it hold off; plot(f,'linewidt',2); axis([1 n -2.0, 2.0]); % plot solution!
is advected and diffused. Two waves of the hold on; plot(ex,'r','linewidt',2);pause; % plot exact solution!
y=f; % store the solution!
infinite wave train are simulated in a domain of for i=2:n-1,!
length 2. To model the infinite train, periodic f(i)=y(i)-0.5*(dt/h)*(y(i+1)-y(i-1))+...!
boundary conditions are used. Compare the ! D*(dt/h^2)*(y(i+1)-2*y(i)+y(i-1)); % advect by centered differences!
end;!
numerical results with the exact solution.! f(n)=y(n)-0.5*(dt/h)*(y(2)-y(n-1))+...!
! D*(dt/h^2)*(y(2)-2*y(n)+y(n-1)); % do endpoints for!
f(1)=f(n); % periodic boundaries!
time=time+dt;!
end;!

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Summary!

Evolution
for!
U=1;
Finite difference approximations by Taylor
D=0.05;
expansion!
k=1
Movie: adv1!

Approximating a partial differential


N=21
equation!
Δt=0.05

Exact!

Numerical!
Computational Fluid Dynamics I
Computational Fluid Dynamics I

http://users.wpi.edu/~gretar/me612.html! Outline!

•  Solving partial differential equations!


Elementary! ! !Finite difference approximations!
! !The linear advection-diffusion equation!
Numerical Analysis:! ! !Matlab code!
! !Accuracy and error quantification!
Finite Difference ! !Stability!
! !Consistency!
Approximations-III! ! !Multidimensional problems!
! !Steady state!

Grétar Tryggvason!
Spring 2010!

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Evolution
for!
U=1;

D=0.05;

k=1
Movie: adv1!
Accuracy!
N=21

Δt=0.05

Exact!

Numerical!

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Accuracy!

It is clear that although the numerical solution is


qualitatively similar to the analytical solution, there Evolution m=11; time=0.50!
are significant quantitative differences.! for!
U=1;
2

The derivation of the numerical approximations for D=0.05;


1.5

the derivatives showed that the error depends on k=1


1

the size of h and Δt. !


0.5
N=21

First we test for different Δt. ! Δt=0.05
0

-0.5

Number of time steps= T/Δt


-1
Exact!
-1.5

Numerical! -2
2 4 6 8 10 12 14 16 18 20
Computational Fluid Dynamics I
Computational Fluid Dynamics I

Accuracy! Accuracy!

Repeat with m=21; time=0.50! Repeat with m=41; time=0.50!


a smaller a smaller
time-step! 2 time-step! 2

Δt=0.025
1.5
Δt=0.0125
1.5

1 1
N=21
N=21

0.5 0.5

0 0

-0.5 -0.5

-1 -1
Exact! Exact!
-1.5 -1.5

Numerical! -2
Numerical! -2
2 4 6 8 10 12 14 16 18 20 2 4 6 8 10 12 14 16 18 20

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Accuracy!

Very fine spatial


How accurate solution can resolution and a m=1001; time=0.50!
we obtain?! small time step!
2
U=1;

Take ! D=0.05;
1.5

Δt = 0.0005 ! k=1
1

and ! 0.5
N=200! N=200
0
Δt=0.0005

-0.5

-1
Exact!
-1.5

Numerical! -2
20 40 60 80 100 120 140 160 180 200

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Accuracy!

Examine the spatial accuracy by taking a


Quantifying the Error! very small time step, Δt = 0.0005 and vary
the number of grid points, N, used to
Order of Accuracy! resolve the spatial direction. !

The grid size is h = L/N where L = 1 for our


case!
Computational Fluid Dynamics I
Computational Fluid Dynamics I

Accuracy! Accuracy!

Exact! Accuracy. Effect of!


time=0.50!
time=0.50! spatial resolution!
100
Numerical!
dt=0.0005!
N=11; E = 0.1633! N=11 to N=161! 10-1
N =21; E = 0.0403!
N =41; E = 0.0096!
-2
N =61; E = 0.0041! E
10
N =81; E = 0.0022!
N =101; E = 0.0015! 10-3
N N =121; E = 0.0011!
E=h ∑( f
j =1
j − fexact )2 N =161; E = 9.2600e-04! 10-4 0
10 10
1
10
2

1/ h

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Accuracy! Accuracy!

If the error is of second order:! time=0.50!


−2 Accuracy. Effect of! 100
⎛1
E = Ch 2 = C ⎞ spatial resolution!
⎝h ⎠ 10-1

Taking the log:!


-2
E
10 2!
⎛ ⎛ 1 ⎞−2 ⎞ ⎛1
ln E = ln⎜ C ⎟ = lnC − 2ln ⎞ 1!
⎝ ⎝ h ⎠ ⎠ ⎝h ⎠ 10-3
dt=0.0005!
On a log-log plot, the E versus (1/h) curve 10-4 0 1 2
10 10 10
should therefore have a slope -2! N=11 to N=161!
1/ h

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Accuracy! Summary!

Why is does the error deviate from the line for the
highest values of N?!
Showed, by numerical experiments, that
accuracy increases as the resolution is
Truncation! increased!
Error!

Showed that the error behaves in a way


that should be predictable!
Total!

Roundoff!

Number of Steps, N = 1/h!


Computational Fluid Dynamics I
Computational Fluid Dynamics I

http://users.wpi.edu/~gretar/me612.html! Outline!

•  Solving partial differential equations!


! !Finite difference approximations!
Elementary! ! !The linear advection-diffusion equation!
! !Matlab code!
Numerical Analysis:! ! !Accuracy and error quantification!
! !Stability!
Finite Difference ! !Consistency!
! !Multidimensional problems!
Approximations-IV! ! !Steady state!

Grétar Tryggvason!
Spring 2010!

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Stability! Stability!

Evolution
As long as accuracy is reasonable, integration at for!
larger time steps is more efficient and desirable. ! U=1;

D=0.05;

Can we increase the time step indefinitely?! k=1

Letʼs repeat the 1-D advection-diffusion equation N=21

with larges time step.! Δt=0.2

Use Δt = 0.2, instead of Δt = 0.05!
Exact!

Numerical!

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Stability!

Instead of decaying as it should, the


amplitude of the numerical solution keeps
increasing. !
Ordinary Differential Equation!
Indeed, if we continued the calculations, we Stability!
would eventually produce numbers larger
than the computer can handle. This results
in an “overflow” or “NaN” (Not a Number).!
Computational Fluid Dynamics I
Computational Fluid Dynamics I

ODE—Example! ODE—Example!
5

Take:! f n+1 = (1 − Δt) f n 4


df
= −f with initial condition! f (0) = 1
3

dt Δt=0.5!
2
1
The exact solution is!
Δt=1.5! 0

f (t ) = e −t Δt=2.5! -1
-2

Forward Euler! -3
-4
f n+1
= f − f Δt
n n
f n+1
= (1 − Δt) f n
-5
0 5 10 15 20
n +1 n +1
f f
≤1 only if! Δt ≤ 2 ≤1 only if! Δt ≤ 2
fn fn

Computational Fluid Dynamics I


Computational Fluid Dynamics I

ODE—Example!

However!
f n +1 = (1− Δt) f n = (1− Δt) 2 f n−1
= ………… = (1− Δt) n f 1
Obviously, f oscillates unless! Δt ≤ 1 Stability Analysis of the!
Advection-Diffusion Equation:!
Backward Euler! Von Neumann Method!
f n +1 1
f n+1 = f n − f n+1 Δt =
fn (1+ Δt)
f n +1
≤1 for all Δt

fn

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Stability! Stability!

Consider the 1-D advection-diffusion equation:!


Generally, stability analysis of the full nonlinear system of
∂f ∂f ∂2f
equations is too involved to be practical, and we study a
+U =D 2
model problem that in some way mimics the full equations.! ∂t ∂x ∂x
The linear advection-diffusion equation is one such model
equation, and we will apply von Neumann's method to In finite-difference form:!
check the stability of a simple finite difference
approximation to that equation.!
f jn +1 − f jn f jn+1 − f jn−1 f jn+1 − 2 f jn + f jn−1
+U =D
Δt 2h h2
Look at the evolution of a small perturbation!
f jn = ε nj
Computational Fluid Dynamics I
Computational Fluid Dynamics I

Stability! Stability!

The evolution of the perturbation is governed by:! The error at node j is:!
ikx j
n +1 ε nj = ε n e
ε j −ε n
j ε n
j +1 −ε n
j −1 ε n
j +1 − 2ε + ε
n
j
n
j −1
+U =D
Δt h2
2h The error at j+1 and j -1 can be written as!
ikx j +1 ik (x j + h) ikx j ikh
Write the error as a wave (expand as a Fourier series):! ε nj +1 = ε n e = εne = εne e

ε nj = ε n ( x j ) = ∑ε
k = −∞
n ikx j
ke ε nj −1 = ε n e
ikx j −1 ik (x j −h)
= ε ne = ε ne
ikx j
e −ikh

Dropping the subscript!


ikx j
ε nj = ε n e
Recall:!
e ikx = cos kx + isin kx

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Stability! Stability!

Substituting! The equation for the error is:!


ε =ε e
n
j
n ikx j
ε n
j +1 =ε e
n ikx j
e
ikh
ε n
j −1 =ε en ikx j
e −ikh
ε n+1 − ε n ε n ikh − ikh ε n ikh −ikh
+U (e − e ) = D 2 (e − 2 + e )
into! Δt 2h h
ε n+1 − ε nj ε n − ε nj −1 ε n − 2ε nj + ε nj −1
+ U j +1 = D j +1
j

Δt 2h h2
yields! Solving for the ratio of the errors:!
ε n+1e − ε ne εn
ikx j ikx j
+ U (eikhe ikx j − e− ikheikx j ) =
Δt 2h ε n +1 UΔt ikh −ikh DΔt ikh
= 1− (e − e ) + 2 (e − 2 + e −ikh )
εn εn 2h h
D 2 (e ikheikx j − 2e ikx j + e − ikhe ikx j )
h

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Stability! Stability!

Dividing by the error amplitude at n:! The ratio of the error amplitude at n+1 and n is:!
amplification factor! ε n +1 DΔt h UΔt
= 1− 4 2 sin 2 k − i sin kh
ε n +1
UΔt ikh −ikh DΔt ikh εn h 2 h
= 1− (e − e ) + 2 (e − 2 + e −ikh )
εn 2h h Stability requires that!

UΔt DΔt ε n+1


≤1
= 1− isin kh + 2 2(cos kh −1) εn
h h
DΔt 2 h UΔt Since the amplification factor is a complex number, and k,
= 1− 4 2 sin k − i sin kh the wave number of the error, can be anything, the
h 2 h determination of the stability limit is slightly involved.!
Using:!
We will look at two special cases: (a) U = 0 and (b) D = 0
e ikh + e−ikh = 2cos kh; e ikh − e−ikh = i2sin kh; 2sin 2 θ = 1− cos2θ
Computational Fluid Dynamics I
Computational Fluid Dynamics I

Stability! Stability!

(a) Consider first the case when U = 0, so the problem (b) Consider now the other limit where D = 0 and
reduces to a pure diffusion! we have a pure advection problem.!
ε n+1 DΔt h ε n +1 UΔt
= 1− 4 2 sin2 k = 1− i sin kh
εn h 2 εn h
2
Since sin () ≤ 1 the amplification factor is always ε n+1
Since the amplification
less than 1, and we find that it is bigger than -1 if!
factor has the form 1+i() εn
DΔt the absolute value of this
−1≤ 1− 4 ≤1
h2 complex number is always UΔt
larger than unity and the i sin kh
DΔt 1 method is unconditionally h
2 ≤
h 2 unstable for this case.!
1

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Stability! Stability!

For a two-dimensional problem, assume an error of the form!


i( kxi +ly j )
For the general case we must investigate the stability ε i,n j = ε n e
condition in more detail. We will not do so here, but
simply quote the results:! A stability analysis gives:!

ΔtD 1 U 2 Δt DΔt 1 (| U | + | V |) 2 Δt
≤ and! ≤2 ≤ and! ≤4
h2 2 D h2 4 D

Notice that high velocity and low viscosity lead to For a three-dimensional problem we get:!
instability according to the second restriction.!
DΔt 1 (|U | + |V | + |W |) 2 Δt
≤ and! ≤8
h2 6 D

Computational Fluid Dynamics I


Computational Fluid Dynamics I

Stability! Summary!

Stability – Now you know!!


Convergence – the solution to the finite-difference
equation approaches the true solution to the PDE having Introduced a formal method to examine
the same initial and boundary conditions as the mesh is whether a given finite difference
refined.! approximation is stable or not!

Laxʼs Equivalence Theorem! Introduced Laxʼs equivalent theorem.!


Given a properly posed initial value problem and a finite-
difference approximation to it that satisfies the consistency
condition, stability is the necessary and sufficient condition
for convergence.!

Das könnte Ihnen auch gefallen