You are on page 1of 26

Dr Edmondo Minisci EngAn3-CFD

Engineering Analysis 3
2013-2014 / 2nd semester

Introduction to Computational Fluid


Dynamics ( CFD )
Dr Edmondo MINISCI
edmondo.minisci@strath.ac.uk
Twitter: https://twitter.com/edmondo_minisci https://twitter.com/MAE_Strath
1
Dr Edmondo Minisci EngAn3-CFD

Lecture 3

2
Dr Edmondo Minisci EngAn3-CFD

Class exercise
dT
For T ( x)  Sin(x / 2) , obtain (x) at x=0.5 with x  0.1 using
dx
dT ( x) T ( xi 1 )  T ( xi )
1.  = 1.0191e+00 Err = 8.2486e-02
dx x

dT ( x) T ( xi 1 )  T ( xi 1 )
2.  = 1.1062e+00 Err = 4.1073e-03
dx 2x

dT ( x) T ( xi  2 )  8T ( xi 1 )  8T ( xi 1 )  T ( xi  2 )
3.  = 1.1107e+00
dx 12x
Err = 2.0234e-05

compare the different approximations with exact values of the derivatives.

 Cos  x / 2  / 2
dT ( x)
= 1.1107e+00
dx
3
Dr Edmondo Minisci EngAn3-CFD

Truncation Error, Consistency, Order of Approximation [1]

f f ( xi  x )  f ( xi )
  T .E .
x i x

Truncation Error of discretization of a partial derivative: contains the rest of


the series dropped in the finite difference approximation and shows the
difference between the exact expression and the finite difference
approximation.
f f ( xi  x )  f ( xi )
In this case: T .E.  O x    O x 
x i x

Only the lowest-order term of the truncation error is important.


Its order is called order of truncation error or order of approximation of the finite
difference scheme.
It defines how fast the error of approximation decreases with the grid step.

4
Dr Edmondo Minisci EngAn3-CFD

Truncation Error, Consistency, Order of Approximation [2]

Does the finite difference equation approach the partial differential equation in
the limit of zero Δx (or Δt)?

Consistency of a finite difference approximation: the approximation makes


sense only if the truncation error vanishes at Δx → 0 (or Δt → 0) (i.e., if the
scheme has, at least, the first order of approximation).

A scheme satisfying this requirement is consistent.

Rule : Grid spacing should be chosen to minimize discretization error

5
Dr Edmondo Minisci EngAn3-CFD

Class exercise

1. Using the central difference, derive finite differences for the following derivatives

 d  and 
d   xi 
1
x 1
    where x
i
1 x  xi  x
 dx  x  dx  x 1
2 2 i
1
2
1 i 2
i 2
2

2. Using finite differences derived in question 1 derive a finite difference to the


following derivative
 d 2 
 2 
 dx  xi

6
Dr Edmondo Minisci EngAn3-CFD

The finite difference method (FD) [5b] (from previous lecture)


examples of Finite Differences

2x 3

f ( x  x)  f ( x  x)  2xf ' ( x)  f ( x)  ...


( 3)

3!
from which

f ( x  x)  f ( x  x) x 2 ( 3)
 f ' ( x)  f ( x)  ...
2x 3!
f ( x  x)  f ( x  x)
2x
 
 f ' ( x)  O x 2

f ( xi 1 )  f ( xi 1 ) f second order
 central difference
2x x i

7
Dr Edmondo Minisci EngAn3-CFD

Class exercise

1. Using the central difference, derive finite differences for the following derivatives

 d  and 
d   xi 
1
x 1
    where x
i
1 x  xi  x
 dx  x  dx  x 1
2 2 i
1
2
1 i 2
i 2
2

 d   i 1   i  i 1   i  d   i   i 1  i   i 1
       
 dx  x 1
xi 1  xi x  dx  x xi  xi 1 x
i 1
2 i
2

8
Dr Edmondo Minisci EngAn3-CFD

Class exercise

2. Using finite differences derived in question 1 derive a finite difference to the


following derivative 2
d 
 2 
 dx  xi

 d   d 
      d   i 1   i
 d 2  d  d    dx  xi1 / 2  dx  xi1 / 2   
 2        dx  x x
 dx  xi dx  dx  xi  xi xi 1/ 2  xi 1/ 2 i
1

 d   i   i 1
2

and using previous solution:   


 dx  x x
 i 1   i  i   i 1
1
i

 d 2   2

   x x   2 i   i 1
 i 1
 dx 2  x x 2
  xi
9
Dr Edmondo Minisci EngAn3-CFD

Class exercise:
The three point asymmetric formula

Let us search for an approximation to the first derivative


at position xi that has the following form :

( xi )  a ( xi )  b ( xi 1 )  c ( xi  2 )  ( x m )
x
1-) Use Taylor expansions of  ( xi ) , ( xi 1 ) , ( xi  2 ) to show that
a ( xi )  b ( xi 1 )  c ( xi  2 )  ( a  b  c) ( xi ) 
  bx 2 c(2x) 2   2
(bx  c 2x) ( xi )  
  
 x 2 ( xi )  ...
x  2 2 
2-) Compare the left and right sides of this equation to obtain the
coefficients a, b, c and prove that the approximation is
3 1
 ( xi )  2( xi 1 )  ( xi  2 )
 2 2
( xi )   (x 2 )
x x 10
Dr Edmondo Minisci EngAn3-CFD

Class exercise:
The three point asymmetric formula

Let us search for an approximation to the first derivative


at position xi that has the following form :

( xi )  a ( xi )  b ( xi 1 )  c ( xi  2 )  ( x m )
x
Taylor expansions of  ( xi ) , ( xi 1 ) , ( xi  2 ) are

 ( xi )   ( xi )
 x 2  2  x 3  3
( xi 1 )  ( xi )  x ( xi )  ( xi )  ( xi )  ...
x 2 x 2
6 x 3

 4x  
2 2
8x 3  3
 ( xi  2 )   ( xi )  2x ( xi )  ( xi )  ( xi )  ..
x 2 x 2
6 x 3

If we multiply the first equation by a, the second by b and the third by c,


we obtain:
11
Dr Edmondo Minisci EngAn3-CFD

Class exercise:
The three point asymmetric formula

a ( xi )  a ( xi )

 x 2  2 
b ( xi 1 )  b ( xi )  bx ( xi )  b ( xi ) 
x 2 x 2
x 3  3
b ( xi )  ...
6 x 3

 4x 2  2 
c ( xi  2 )  c ( xi )  c 2x ( xi )  c ( xi ) 
x 2 x 2

8x 3  3
c ( xi )  ...
6 x 3

If we sum the three equations


12
Dr Edmondo Minisci EngAn3-CFD

Class exercise:
The three point asymmetric formula

a ( xi )  b ( xi 1 )  c ( xi  2 )  ( a  b  c ) ( xi ) 

(bx  c 2x ) ( xi ) 
x
 bx 2 c ( 2x ) 2   2 

 2  
 ( xi ) 
 x
2
 2
 bx 3 c ( 2x ) 3   3

 6  
 x 3 ( xi )  ...
 6 
If we want


( xi )  a( xi )  b( xi 1 )  c( xi  2 )  (x m )
x
then

13
Dr Edmondo Minisci EngAn3-CFD

Class exercise:
The three point asymmetric formula

 3
  a  
a  b  c  0 2x

 
b  2c x  1
2
b 
b  x
4c
  0  1
2 2 c  
 2x

3 1
  ( xi )  2 ( xi 1 )   ( xi  2 )
 2 2 x 2
 3

( xi )   ( xi )  ...
x x 3 x 3

3 1
 ( xi )  2( xi 1 )  ( xi  2 )
 2 2
( xi )   (x 2 )
x x
14
Dr Edmondo Minisci EngAn3-CFD

Boundary and initial conditions

 before solving any partial differential equation, boundary conditions and/or initial
conditions have to be prescribed. These provide values of the unknown function at
some initial position and time (usually the boundary of the domain)

xL T ( xi ) xR
 Dirichlet boundary conditions: some values of the function are known at the edges

T ( xL )  TL T ( xL )  TR

 Neumann boundary conditions: values of the gradient (flux) are known at the edge
dT dT
( xL )  dT
( xR ) 
dT
dx dx L dx dx R
 Mixed boundary conditions
dT dT
T ( xL )  TL ( xR ) 
dx dx R
15
Dr Edmondo Minisci EngAn3-CFD

Numerical solution to 1-dimensional steady


state heat conduction equation [1]

 for a 1-dimensional conduction the PDE describing temperature within the domain is

 2T ph
 T  TLS 
x 2
Ak

 discretise the second derivative involved using the second central finite difference

f ( xi 1 )  2 f ( xi )  f ( xi 1 ) 2 f

x 2
x 2
to get
i

Ti 1  2Ti  Ti 1 ph
 (Ti  TLS )
x 2
Ak
16
Dr Edmondo Minisci EngAn3-CFD

Numerical solution to 1-dimensional steady


state heat conduction equation [2]
x1 x3
 using Dirichlet boundary conditions : x0
temperature value is known at the edge.
x2 x4
T0 and T4 are known

For
T2  2T1  T0
i 1
ph
 (T1  TLS )
x 2
Ak
T3  2T2  T1

ph
(T2  TLS ) i2
x 2
Ak
T4  2T3  T2

ph
(T3  TLS ) i 3
x 2
Ak
17
Dr Edmondo Minisci EngAn3-CFD

Numerical solution to 1-dimensional steady


state heat conduction equation [2b]
x1 x3
 using Dirichlet boundary conditions : x0
temperature value is known at the edge.
x2 x4
T0 and T4 are known

For

  2  x

2 ph 

Ak
T1  T2   x

2 ph

Ak
TLS  T0 i 1
 ph 
i2
2 ph
T1    2  x 2 T2  T3   x TLS
 Ak  Ak

 ph  2 ph
T2    2  x 2

T3   x
Ak  Ak
TLS  T4
i 3
18
Dr Edmondo Minisci EngAn3-CFD

Numerical solution to 1-dimensional steady


state heat conduction equation [3]
 the algebraic equations to be solved are then better written in matrix form

   1  TLS  T0 
2
 2   1
2 0 T
  1 2   2  1  T    2T 
   2
 LS

 0  1 2    T3   TLS  T4 
2
2

 ph 
M Y  B   x  
2

 Ak 
2

3-diagonal Matrix 19
Dr Edmondo Minisci EngAn3-CFD

Numerical solution to 1-dimensional steady


state heat conduction equation [4]
x1 x3
 with Neumann boundary x0
conditions (a value of the x2 x4
flux is known at a given position)
T 

( x  x4 )  Q4
x


dT Ti  Ti 1
use first order difference Q   Ak   Ak
to discretise the flux dx x

 T3  T4 
replace equation at Ak  Q 4
the specified positions x
20
Dr Edmondo Minisci EngAn3-CFD

Numerical solution to 1-dimensional steady


state heat conduction equation [5]

the algebraic equations to be solved when Neumann


Boundary condition is used are written as:

2  
2
1 0 0  T1   TLS  T0 
2

 1 0 
T   2 
 2   1
2

  2  TLS 
T3  
 2T 
 0 1 2    1
2
 LS 
 0  T   x  
 0 1 1   4  Ak Q4 
Is the use of first order difference methodologically correct?

21
Dr Edmondo Minisci EngAn3-CFD

Introduction to Matlab (part 3)

Loosely based on material for “Introduction to Matlab”, Luke Dickens, Imperial College London

22
Dr Edmondo Minisci EngAn3-CFD

Introduction to Matlab [28]


The diary Function

All or part of a MATLAB session (commands and output) can be recorded in text
format, with the diary command.

To do so type what follows at the beginning of the session you wish to record:
>> diary <filename>

and, at the end:


>> diary off

This stores your commands in the le <filename>.

23
Dr Edmondo Minisci EngAn3-CFD

Introduction to Matlab [29]


save and load

The commands save and load can be used to save and load variables in files called
a Mat-files. Mat-files are binary format and always have the .mat extension.

Examples of usage are below:


save somedata.mat x y Saves variables x and y in the le somedata.mat
save moredata x y z Saves variables x, y and z in the file
moredata.mat
save xdata.dat x -ascii Saves x in 8-digit ASCII format in the file
xdata.mat
save xdata.dat x -ascii -double Saves x in 16-digit ASCII format in the file
xdata.mat
save Saves entire workspace in the file matlab.mat
load somedata Loads variables stored in the somedata.mat
file.
load Loads variables stored in the matlab.mat file.
The command load can also be used to load ASCII data les, provided the data file
contains only a rectangular matrix of numbers.
24
Dr Edmondo Minisci EngAn3-CFD

Introduction to Matlab [30]


M-files

Sequences of commands can be grouped in a file, and call be called from within
MATLAB. Called m-files, their filename extension must be “.m”.
There are two types:

Script files
A sequence of MATLAB commands that are executed (in order) once the script is
called. E.g., for the script file compute.m , executing the command compute will
cause the statements in that file to be executed.

Function files
Allow user's to define commands with input and/or output. These have the same
status as other MATLAB commands.

25
Dr Edmondo Minisci EngAn3-CFD

Introduction to Matlab [31]


Flow Control: Loops

For scripts and functions, we may need some flow control.


for blocks have the form:

for i = m:n for i = m:n


... a(i)=b(i)-c(i-1);
end end

This iterates over elements i in the vector [m : n].


while loops are similar and have the form:
i=m;
while (a(i)<10)
while (boolean_statement)
i=i+1;
...
a(i)=b(i)-c(i-1);
end
end
And will loop until the statement returns false.

Flow control can be used at the command prompt, but is particularly useful in m-
files.
26

You might also like