You are on page 1of 8

# Computational Fluid Dynamics

http://www.nd.edu/~gtryggva/CFD-Course/!

Warm-up:!
One-Dimensional
Conservation
Equation!

Objectives:!

## Introduce the basic concepts needed to solve a partial

differential equation using finite difference methods. !
!
Discuss basic time integration methods, ordinary and
partial differential equations, finite difference
approximations, accuracy. !
!
Show the implementation of numerical algorithms into
actual computer codes.!

Grtar Tryggvason!
Spring 2013!

Outline!

## Solving partial differential equations!

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

Model Equations!

f
f
2 f
+U
=D 2
t
x
x

## Computational Fluid Dynamics

Model Equations!
For initial conditions of the form:!

f (x, t = 0) = Asin(2kx)
It can be verified by direct substitution that the solution
is given by:!

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
and a diffusion term. !
Before attempting to solve the equation, it is useful to
understand how the analytical solution behaves.!

which is a decaying
traveling wave!

## Computational Fluid Dynamics

Conservation equations!

## Computational Fluid Dynamics

Conservation equations!

## Most physical laws are based on CONSERVATION

principles: In the absence of explicit sources or sinks, f
is neither created nor destroyed.!
!
Consider a simple one dimensional pipe of uniform
diameter with a given velocity !

f (t)

## To predict the evolution of f everywhere in the pipe,

we assume that f is conserved. Thus, for any section
of the pipe:!
Amount of f
in a control
volume
after a
given time
interval t!

U (x)

x
Given f at the inlet as a function of time (as well as
everywhere at time zero), how do we predict the f(t,x)? !

Amount of f in
the control
volume at the
beginning of
the time
interval t!

Amount of
f that
flows into
the control
volume
during t!

Amount of
f that flows
out of the
control
volume
during t!

U (x)

f (t)

## Computational Fluid Dynamics

Conservation equations!
How much does the total amount of f in
the Control Volume change during a short
time t? Restating the conservation law
from the last slide, we have!

## Computational Fluid Dynamics

Conservation equations!
We can also state the conservation principle
in differential form. From last slide!

Approximate:!

fTotal =

f dx f av x

## Denote the flow rate of f into the control volume by F then:!

()

f av t + t f av t x = t Fin Fout

## Computational Fluid Dynamics

Conservation equations!

()

f av t + t f av t x = t Fin Fout

Divide by tx!
!

()

f av t + t f av t

= Fout Fin
t
x
or:!

f av
F
=
t
x

Taking the
limit, we get:!

f F
+
=0
t x
F: Flux of f!

## The general form of the one-dimensional

conservation equation is:!

f F
+
=0
t x
Taking the flux to be the sum of advective and
diffusive fluxes:!

F = Uf F

f
x

## Gives the advection diffusion equation!

f
f
2 f
+U
=D 2
t
x
x

Finite Difference
Approximations of
the Derivatives!

## Computational Fluid Dynamics

Finite Difference Approximations!

## Computational Fluid Dynamics

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.

f (t + 2t)
t

t

The!
Time Derivative!

f (t + t)
f(t,x-h) f(t,x) f(t,x+h) !
h

f (t)

## Computational Fluid Dynamics

Finite Difference Approximations!

## Computational Fluid Dynamics

Finite Difference Approximations!

Time derivative!
The Time Derivative is found using a FORWARD
EULER method. The approximation can be found by
using a Taylor series

f(t+t,x)

t

f(t,x)!
h

## Computational Fluid Dynamics

Finite Difference Approximations!

The Spatial!
First Derivative!

f (t + t) = f (t) +

f (t)
2 f (t) t 2
t +
+
t
t 2 2

=

+
t
t
t 2 2

## Computational Fluid Dynamics

Finite Difference Approximations!

## When using FINITE DIFFERENCE approximations,

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

## The derivatives of the function are approximated

using a Taylor series!

## Computational Fluid Dynamics

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+
+
+
+
x
x 2 2
x 3 6
x 4 24
f (x h) = f (x)

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

x
x 2 2
x 3 6
x 4 24

## Subtracting the second equation from the first:!

f (x + h) f (x h) = 2

f (x)
3 f (x) h 3
h+2
+
x
x 3 6

## Computational Fluid Dynamics

Finite Difference Approximations!
The result is:!

f (x + h) f (x h) = 2

f (x)
3 f (x) h 3
h+2
+
x
x 3 6

## Rearranging this equation to isolate the first derivative:!

f (x) f (x + h) f (x h) 3 f (x) h 2
=

+
x
2h
x 3 6

## Computational Fluid Dynamics

Finite Difference Approximations!

## Computational Fluid Dynamics

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

The Spatial!
Second Derivative!

f (x + h) = f (x) +

f (x)
2 f (x) h 2 3 f (x) h 3 4 f (x) h 4
h+
+
+
+
x
x 2 2
x 3 6
x 4 24

f (x h) = f (x)

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

x
x 2 2
x 3 6
x 4 24

## Adding the second equation to the first:!

f (x + h) + f (x h) = 2 f (x) + 2

## Computational Fluid Dynamics

Finite Difference Approximations!

f 2 (x) h 2
4 f (x) h 4
+2
+
2
x
2
x 4 24

## Computational Fluid Dynamics

Finite Difference Approximations!

## The result is:!

f (x + h) + f (x h) = 2 f (x) + 2

f 2 (x) h 2
4 f (x) h 4
+2
+
2
x
2
x 4 24

=

+
x 2
h2
x 4 12

## Solving the partial

differential equation!

## Computational Fluid Dynamics

Finite Difference Approximations!

## Computational Fluid Dynamics

Finite Difference Approximations!
A numerical approximation to!

## Use the notation! f n = f (t)

For space and time we will use:!

f
f
2 f
+U
=D 2
t
x
x

f j = f (t, x j )
f jn+1
f jn+1

## Is found by replacing the derivatives by the following

approximations!

f jn+1

= f (t + t, x j )

f jn1

= f (t, x j + h)

f jn f jn+1
h

f jn1 = f (t, x j h)

n
n
2 f
f
f
+U
= D 2
t j
x j
x j

f jn+1
f jn1

f jn f jn+1
h

## Computational Fluid Dynamics

Finite Difference Approximations!

## Computational Fluid Dynamics

Finite Difference Approximations!
Substituting these approximations into:!

## Using the shorthand !

notation!

gives!

f fj
f
= j
+ O(t)
t j
t
n

f jn = f (t, x j )
f jn+1 = f (t + t, x j )

n+1

f
f
2 f
+U
=D 2
t
x
x

gives!

f f j 1
f
= j +1
+ O(h2 )
x j
2h
n

f jn+1 = f (t, x j + h)
f jn1 = f (t, x j h)

f jn+1 fjn
f jn+1 fjn1
fjn+1 2 f jn + f jn1
+U
=D
+ O(t, h 2 )
t
2h
h2

2 f
f jn+1 2 f jn + f jn1
2
=
+ O(h )
2
x j
h2

Solving for the new value and dropping the error terms yields!

fj

## Computational Fluid Dynamics

Finite Difference Approximations!

n+1

= fj
n

Ut n
Dt n
n
n
n
( f f j 1) + 2 ( f j +1 2 f j + f j 1)
2h j +1
h

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

next time level is given by:!

f jn+1 = f jn

Ut n
Dt
( f j +1 f jn1) + 2 ( f jn+1 2 f jn + f jn1)
2h
h

## 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 jn1

f jn f jn+1
h

Example!

## A short MATLAB program!

The evolution of a sine wave is followed as it
is advected and diffused. Two waves of the
infinite wave train are simulated in a domain of
length 2. To model the infinite train, periodic
boundary conditions are used. Compare the
numerical results with the exact solution.!

## Computational Fluid Dynamics

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;!
for i=1:n, f(i)=0.5*sin(2*pi*h*(i-1)); end;
% initial conditions!
for m=1:nstep, m, time!
for i=1:n, ex(i)=exp(-4*pi*pi*D*time)*...!
0.5*sin(2*pi*(h*(i-1)-time)); end;
% exact solution !
hold off; plot(f,'linewidt',2); axis([1 n -2.0, 2.0]); % plot solution!
hold on; plot(ex,'r','linewidt',2);pause;
% plot exact solution!
y=f;
% store the solution!
for i=2:n-1,!
f(i)=y(i)-0.5*(dt/h)*(y(i+1)-y(i-1))+...!
! D*(dt/h^2)*(y(i+1)-2*y(i)+y(i-1)); % advect by centered differences!
end;!
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;!

Evolution
for!
U=1;

D=0.05;

k=1

N=21

t=0.05

Accuracy!

Movie: adv1!

Exact!
Numerical!

## It is clear that although the numerical solution is

qualitatively similar to the analytical solution, there
are significant quantitative differences.!
!
The derivation of the numerical approximations for
the derivatives showed that the error depends on
the size of h and t. !
!
First we test for different t. !
!
Number of time steps= T/t

Accuracy!

Evolution
for!
U=1;

D=0.05;

k=1

N=21

t=0.05

m=11; time=0.50!
2

1.5
1

0.5

0
-0.5

Exact!

-1
-1.5

Numerical!

-2

10

12

14

16

18

20

Accuracy!

Accuracy!

m=21; time=0.50!

Repeat with
a smaller
time-step!
t=0.025

N=21

m=41; time=0.50!

Repeat with
a smaller
time-step!
t=0.0125

N=21

1.5

1.5

0.5

0.5

-0.5

-0.5

-1

Exact!

-1

Exact!

-1.5

Numerical!

-2

-1.5

Numerical!
2

10

12

14

16

18

20

we obtain?!
!
Take !
t = 0.0005 !
and !
N=200!

-2

10

12

14

16

18

20

## Computational Fluid Dynamics

Accuracy!
Very fine spatial
resolution and a
small time step!

m=1001; time=0.50!
2

U=1;

D=0.05;

k=1

N=200

t=0.0005

1.5
1

0.5

0
-0.5

-1

Exact!

-1.5

Numerical!

## Quantifying the Error!

Order of Accuracy!

-2

20

40

60

80

100

120

140

Accuracy!

## Examine the spatial accuracy by taking a

very small time step, t = 0.0005 and vary
the number of grid points, N, used to
resolve the spatial direction. !
!
The grid size is h = L/N where L = 1 for our
case!

160

180

200

## Computational Fluid Dynamics

Accuracy!
Exact!

time=0.50!

Numerical!

E=h

( f
j =1

N=11; E = 0.1633!
N =21; E = 0.0403!
N =41; E = 0.0096!
N =61; E = 0.0041!
N =81; E = 0.0022!
N =101; E = 0.0015!
N =121; E = 0.0011!
N =161; E = 9.2600e-04!

fexact )2

## Computational Fluid Dynamics

Accuracy!
Accuracy. Effect of!
spatial resolution!
dt=0.0005!
N=11 to N=161!

time=0.50!
100
10-1
-2
E
10

10-3
10-4 0
10

10

10

10

1/ h

Accuracy!

Accuracy!
time=0.50!

2

1
E = Ch 2 = C
h

## Accuracy. Effect of!

spatial resolution!

10-1

## Taking the log:!

-2
E
10

1 2
1
ln E = ln C = lnC 2ln

h
h

## On a log-log plot, the E versus (1/h) curve

should therefore have a slope -2!

## Computational Fluid Dynamics

Accuracy!
Why is does the error deviate from the line for the
highest values of N?!

Error!

Truncation!

Total!
Roundoff!
Number of Steps, N = 1/h!

100

2!
1!

10-3

dt=0.0005!
!
N=11 to N=161!

10-4 0
10

10

1/ h

Summary!

## Finite difference approximations by Taylor

expansion!
!
Approximating a partial differential
equation!
!
Showed, by numerical experiments, that
accuracy increases as the resolution is
increased!
!
Showed that the error behaves in a way
that should be predictable!