MN Notes..... ,.... ,........
MN Notes..... ,.... ,........
11
1
References
• Chapra, S. & Canale, R.; Numerical methods for engineering;
7th edition; Mc Graw Hill Education; 2015
• Weber, H. & Arfken, G.; Essential mathematical methods for
physicist; Academic Press; 2003
• Ghaboussi, J. & Wu, X.; Numerical methods in computational
mechanics; CRC Press; 2017
• Akio Tomiyama, Kosuke Hayashi, Takeshi Seta, Tomohiro
Takaki, Michihisa Tsutahara
• Class of Akira Kageyama at Kobe University
• Any Fortran language & compiler user reference guide
• Any python guide: NumPy, SymPy, SciPy, pandas, matplotlib
Numerical Analysis
1. Introduction
2. Basic programming concepts
3. Error analysis
4. Roots of equations
5. Linear algebraic equations
6. Curve fitting
7. Ordinary differential equations
8. Partial differential equations
9. Finite element method
2
There are three main scientific approaches to
study a problem
* This Photo by Unknown Author is licensed under CC BY-SA * This Photo by Unknown Author is licensed under CC BY-SA
Numerical methods
* MH4 Laboratory, Kobe University
3
Fluid Mechanics & Heat Transfer equations
u 0
u 1
u u P 2 u g
t
u u u u 1 2u 2u 2u
u v w P 2 2 2 g
t x y z x y z
u u u
u 0 u uu p 0
t x t x r x x x
1 2 1 2 e u u u
e u e p u u ' u u 0
t r x
2 2 x x x x
f
t
c
x
1
f x ,t f x ,t f eq x ,t
ji 2 ui
Fi 2
x j t
1 ui u j
ij
2 x j xi
ij Cijkl kl
4
Numerical analysis involves some fields
• Numerical analysis: mathematics, computer science,
engineering and others that create and implement
algorithms for solving problems. NUMBERS IN A
COMPUTER
• E.g. numerical study of fluid dynamics (differential
equations): computer science (programming language)
and numerical analysis (discretization and solution
techniques)
• A computer can be viewed as a virtual laboratory
• Effect of computer hardware and techniques for
optimizing calculations
Software&Tools
• Programming: Fortran (Gfortran)
• Text editor: Notepad++
5
Text editor
• File extension can be changed: *.f90
• Colors appear depending on the programming language
• Different font colors for: functions, comments, commands, etc.
0visualizatecolorstexteditor.f90
WINDOWS
How to show file extensions
1. Explorer
2. View and then Options
3. Change folders and search
options
4. Uncheck hide extensions
6
Visualization application: ParaView
• Read files: *.vtk (special format, numbers)
• Color distribution of parameters: low (blue) and high (red)
• http://www.paraview.org/
Gnuplot folder:
scripts
screenshoots
Screen shots
7
Visualization application:
Graph digitizing system-GSYS
• Digitalize images for comparison
• Text files with selected format
• http://www.jcprg.org/gsys/
• Java
Numerical Analysis
1. Introduction
2. Basic programming concepts
3. Error analysis
4. Roots of equations
5. Linear algebraic equations
6. Curve fitting
7. Ordinary differential equations
8. Partial differential equations
9. Finite element method
8
Basic programming concepts
• Program: sequential instructions in a defined language [math,I/O]
• Language to tell the computer what to do [instructions]
• Interactive(short) and Script(long): *.f90 *.py (source code) [syntax]
• Structure: Red for Fortran Blue for Python
Fortran Python
main program (PROGRAM): starting point
subroutines: part of the code to perform a name() def name():
determined task. CALL name ……..
functions: part of the code to perform nv=name2(x) def name2(variable):
calculations and return a value for given calculation=f(variable)
arguments, new_variable= name2(x) return calculation
modules: usually used to separate parts of the import module_name
code and improve readability, e.g declare
common variables, libraries. use module_name
9
Why Fortran? Also why python?
• Scientific and numerical computing: normally taken from a
research group
• Could be free
• Programming can be a little tedious but it gives good
understanding from the mathematical point of view
• Easy for parallelization
• *.exe file is portable
• Not good for handling text and system interface but it focuses
on “numerical computing”, for this, python is better
• Since it is a compiled language like C, C++, Visual Basic, and
so on (opposite to interpreted language like MATLAB,
MATHEMATICA, python, etc.), it is very fast
10
Double vs. single precision floating point format
• It is useful for understanding use of computer memory
Double (8bytes or 64 bits)* Single (4bytes or 32 bits)
Exponent width (2n) 11 bit (consider 1 bit for sign) 8 bit (consider 1 bit for sign)
11
Discretization
Space and time are continuous
Infinite finite (discretize)
Space
Continuous
1D
Discrete
2D
Continuous Discrete
python: I/O
• Read for input and write for output, e.g. read ‘r’ and write ‘w’
• 6d : 1 integer number of width 6 digits
• 12.3f: 1 real number of width 12 with 3 decimal digits (no
exponent)
• 10.5E: 1 real number in exponent notation of width 10 and
5 decimal digits in the mantissa (exponent E)
• 10: string with 10 characters aligned left
6readmatrix.py
• >10: string with 10 characters aligned right
7readfromfile.py
• : spaces
8example read from input
• \t : tab
9grid
12
Tips for writing good codes
• Readable: avoid jumps to other statements
• Simple: to increase performance
• Add comments: collaboration with others, and use “!” “#”. At
the beginning with name and date, and when necessary
• Test: to confirm accuracy with a benchmark problem
• Improve efficiency: by testing optimization tools
• Declare all the variables: IMPLICIT NONE
• When getting “segmentation fault”, check arrays
• Parenthesis, exponentiation, multiplication, addition
• Fortran is not sensible to upper&lower cases, but python is
Calculation speed
• FLOPS: floating point operation per second
• For a human, multiply once two floating point numbers and
check the time 1FLO
10 3 FLOPS 1mFLOPS
1000s
• Human living only for this task, she or he could perform:
1mFLOPS *100 years 3*106 FLOP
• In a computer, this is easy
• Computer speed: GFLOPS (109)
• Supercomputer: TFLOPS(1012)-PFLOPS(1015) www.top500.org
• Walk ~ 1m/s, light velocity ~ 3*108m/s: 108
• Supercomputer vs. brain : 1018 Taken from Akira Kageyama
13
Tokyo Institute of Technology
Tsubame supercomputer 2.5
14
Numerical Analysis
1. Introduction
2. Basic programming concepts 0
3. Error analysis
4. Roots of equations
IB-FDLBM Measured
(Present) (ten Cate et al., 2002)
-0.1
5. Linear algebraic equations Case
1
Case
1
2 2
3 3
6. Curve fitting
4 4
-0.15
0 1 2 3 4
t [s]
7. Ordinary differential equations
8. Partial differential equations
9. Finite element method
Error analysis
• Perfect matching is IMPOSSIBLE
• Error: discrepancy between analytical, experimental and
numerical solution
• Round-off errors: finite number of digits in a computer
Chopping or rounding: 3.141592653589793238…2numbers.f90
• Truncation error: finite number of mathematical operations
in a computer
x1 x 2 x3 x 4 x5 x 6 Code this equation
e 1 ......
x and test diferent
1! 2! 3! 4! 5! 6! approximations
15
True and approximated errors
• Relative error is a tool for comparison or assessment of a
scientific technique
16
Total error
u/Uo
-1
17
Examples of error analysis: Streamlines of
driven cavity flows
Re = 100 Re = 100
U0
No-slip boundary
No-slip boundary
y Re = 10000 Re = 10000
x No-slip boundary
v/U0
No-slip boundary
No-slip boundary
0.6 0.6
0 0
y/L0
y/L0
0.4 0.4
H 0.2 0.2
0 Good predictions of -1 the
0 velocity profiles -1
-1 0 1 -1 0 1
u/U0
Re = 5000 Re = 10000
y 0 0.2 0.4 0.6 0.8 1 x/L0 0 0.2 0.4 0.6 0.8 1
1 FDM (Ertuk et al.)
1 1 FDM (Ertuk at al.)
1
FDLBM FDLBM
x No-slip boundary 0.8 0.8
v/U0
v/U0
0.6 0.6
H=201, Re=100 and 1000 0 0
y/L0
y/L0
0.4 0.4
H=601, Re=5000 and 10000 0.2 0.2
0 -1 0 -1
-1 0 1 u/U0 -1 0 1
18
Errors in arrays: Norms
Maximum
f IJpredicted f IJexact
l max
IJ domain f IJexact
L1-norm
f IJpredicted f IJexact
Norm
IJ domain f IJexact
L2-norm
2
f IJpredicted f IJexact
Norm 2
IJ domain f IJexact
L2-norm
f IJpredicted f IJexact
2
Norm 2 IJ domain
f
IJ domain
exact 2
IJ
19
Examples of error analysis: Circular Couette
flows
1.5
(Re, ) = (0.038, 20) Analytical Analytical Solution (Eq. (1.23))
SRT: (Re, ) =(0.079,10)
TRT: (Re, , )=(0.079,10,0.53)
solution
1
R R
out
u/U
uT Rout R
0.5
U Rin Rout
Rout Rin
0
Non-physical distortion
TRT agrees Outer Cylinder Fluid Inner Cylinder
Region Region Region
better than SRT -0.5
0 20 40 60 80 100
x
u/U 0 1
Axisymmetric
flow fields at
low Re
(Re,,) = (1.5,1,1) (0.17, 5, 0.56) (0.079, 10, 0.53) (0.038, 20, 0.51)
Maximum
20
Examples of error analysis: Circular Couette
flow-errors at different grid resolutions
0.5 Error
0.45 +=10; A=0.5
u predicted uexact
0.4 201x201 l max
IJ fluid u exact
401x401
0.35 801x801 1.5
Re
Re 0.038
0
0.3 10
Smaller error
0.25 with TRT
l
-1
10
0.2
0.15
l
10-2
0.1
SRT
0.05 TRT, =1/4
-3
10
0 -4 -3 -2 -1 0 1 2 3 10-1 100 101 102
10 10 10 10 10 10 10 10 for SRT, + for TRT
Read the given files and calculate the norm
(maximum error) in the velocity profiles
Numerical Analysis
1. Introduction
2. Basic programming concepts
3. Error analysis
4. Roots of equations
5. Linear algebraic equations
6. Curve fitting
7. Ordinary differential equations
8. Partial differential equations
9. Finite element method
21
Roots of equations
Find the solution of linear and non linear equations
1. Bracketing methods
2. Opened method
Graphical method
• Easy and it allows to identify the points
where a given a function is zero
• Low accuracy
• This result can be used as an input for more
accurate methods
gm
v
c
1 e ( c / m )t explicit
Try now to isolate c (implicit)
gm
f (c )
c
1 e(c / m)t v
Plot the function with g=9.81m/s2,c=1,v=40m/s, t=10s, and
determine the roots in a given interval m [0:50 kg]
g=9.81m/s2,m=70kg,v=40m/s, t =10s
22
Bisection method
• Function changes its sign,
that is the half that contains
the root
• Reduce width of the bracket
• Halve the increment and
check where the prediction is
• Run until finding a
convergent solution
23
False-point method
• Bisection: inefficient since the
magnitude of f is not considered
• Intersection with x is an
improved prediction
f ( xl ) f ( xu )
xr xl xr xu
f ( xu ) xl xu
xr xu
f ( xl ) f ( xu )
24
Roots of equations
1. Bracketing methods
2. Opened method
Introduction
• Bracketing methods are convergent, and they need an interval
• Opened methods require only a single starting value or two
values that do not necessarily include the root. Sometimes,
these diverge. However, when these converge, it is very fast
25
Newton-Raphson method
• Initial guess and a tangent to the x-axis
• The intersection with x-axis is the estimated root
f ( xi ) 0
f '( xi )
xi xi 1
f ( xi )
xi 1 xi
f '( xi )
Code the method
Example
e x x
Poor convergence
• Inflection points and oscillations
• Multiple roots
26
Secant method
• Derivative is an issue
• Derivative can be approximated by a backward FD
f ( xi )
xi 1 xi
f '( xi )
f ( xi 1 ) f ( xi )
f '( xi )
xi 1 xi
f ( xi ) xi 1 xi
xi 1 xi
f ( xi 1 ) f ( xi )
Secant method-continued
f ( xu ) xl xu
xr xu
f ( xl ) f ( xu )
f ( xi ) xi 1 xi
xi 1 xi
f ( xi 1 ) f ( xi )
• May diverge
• Fast convergence
Modification
f ( xi xi ) f ( xi )
f '( xi )
xi
xi f ( xi )
xi 1 xi
f ( xi xi ) f ( xi )
27
Application of roots of equations
Numerical Analysis
1. Introduction
2. Basic programming concepts
3. Error analysis
4. Roots of equations
5. Linear algebraic equations
6. Curve fitting
7. Ordinary differential equations
8. Partial differential equations
9. Finite element method
28
Linear algebraic equations
Direct methods:
1. Gauss elimination
2. LU decomposition and matrix inversion
29
Application: linear equation system (continued)
Numerical Analysis
1. Introduction
2. Basic programming concepts
3. Error analysis
4. Roots of equations
5. Linear algebraic equations
6. Curve fitting
7. Ordinary differential equations
8. Partial differential equations
9. Finite element method
30
Curve fitting
1. Least-squares regression
2. Interpolation
3. Fourier approximation
31
Numerical Analysis
1. Introduction
2. Basic programming concepts
3. Error analysis
4. Roots of equations
5. Linear algebraic equations
6. Curve fitting
7. Ordinary differential equations (Section taken from Prof. Tomiyama)
8. Partial differential equations
9. Finite element method
Integration
Integral What functions?
b
I f ( x)dx A continuous function
a
A discrete function
Summation Zero-degree (order) polynomial (constant)
f(x) f(x) f(x)
n=6 n=6
a b x a b x a b x
D D
32
Integration trapezoidal rule
First-degree polynomial (line) Slope straight line equation
b b
y1 mx B
I f ( x)dx f1 ( x)dx f (b) f (a )
a a f1 ( x) f (a ) x a
ba
Exact for linear functions
Multiple applications
improve the solution
f (a ) f (b)
I b a
2
n 1
f x0 2 f xi f xn
I b a i 1
2n
a D b
33
Integration Simpson’s 1/3 rule-multiple
applications
Higher-order polynomial
3 points: limits and midway
Second order polynomial
b b
I f ( x) dx f 2 ( x ) dx a b
a a
y2 f 2 x Ax 2 Bx C
f ( x0 ) 4 f ( x1 ) f ( x2 ) n=2
I b a
6
n1 n2
f (x0 ) 4 f (xi ) 2 f (xj ) f (xn ) ba
D
I b a
i1,3,5 j 2,4,6
n
3n n: even
Area?
FD dFD Pcos w sindA In 2D for a circle
A A
dA=dS*span
FL dFL Psinw cos dA dS=rd
dA=D/2d*span
A A
FD
CD Given values:
1
V Aprojected
2
: fluid density
2
V: freestream velocity
FL
CL A projected: depends on
1
V Aprojected
2
the body shape
2
Briefly explanation Paraview and file.vtk
34
Flows past a circular cylinder at a wide range of
Re: IBFDLBM
50D
Uniform inflow
at U0
(20D,20D)
40D
Cylinder
40Dx for D
y Nm = 126
x Continuous outflow
102 0.4
Measured (Wieselsberger, 1922) Measured (Roshko, 1954)
FDM (Lei et al., 2000) Measured (Schewe, 1983)
FDM (Matsumiya et al., 1993) FDM (Lei et al., 2000)
IB-FDLBM (Present) 0.3 FDM (Matsumiya et al., 1993)
IB-FDLBM (Present)
101
CD St 0.2
0
10
0.1
10-1 -1 0 1
10 100 101 102 103 104 105 10 102 103 104 105
High (High Re Low (Low High (High Re Low (Low
Re = 20
Re = 40
A Pair of stationary Vortex shedding Irregular vortex
vortices shedding
35
Shear stress in flow past a circular cylinder
n
v
v cos
u
u sin
t
Derivation
Derivative dy f xi Dx f xi
lim
dx Dx 0 Dx
36
Discretization (time and space)
Space and time are continuous
Infinite finite (discretize)
Continuous
Discrete
Time 1 Time 2
1D
space space
Time 1 Time 2
2D
space space
Taylor expansion
df xi d 2 f xi Dx 2 d 3 f xi Dx3
f xi Dx f xi xi Dx xi ...
dx dx 2 2 dx3 6
Constant f xi Dx f xi O Dx Order of Dx
Line df xi
f xi Dx f xi
dx
Dx O Dx 2 Order of Dx2
df xi d 2 f xi Dx 2
Capture
curvature
f xi Dx f xi
dx
Dx
dx 2
2
O Dx 3
Order of Dx3
37
Basic finite difference schemes
Discretization of space and time
xi iDx ,t n nDt f ( xi ,t n ) fin f ( xi ,t n 1 ) fi n 1 f ( xi 1 ,t n ) f in1
Difference approximation of derivative using Taylor expansion
1
f xi Dx ,t n f xi ,t n
f n
x i
Dx
2 f n Dx 2 3 f n Dx3
x 2 i 2
3
x i 6
O Dx 4
2
f xi Dx ,t n
f xi ,t n
f n
x i
Dx
2 f n Dx 2 3 f n Dx3
x 2 i 2
3
x i 6
O Dx 4 f(x)
f n f in1 fi n f n f in f in1
ODx ODx i-1 i i+1
x i Dx x i Dx
1 Forward (downwind) 2 Backward (upwind)
2
fi n1 2 f in fin1
f n f in1 f i n1
x i
2Dx
O Dx 2 f n
x 2
i
Dx 2
O Dx 2
1 - 2 Centered 1 + 2 Centered
f i 1 f i f i 2 2 f i 1 f i
f 'i Dx O Dx 2
Dx 2 Dx 2
f i 2 4 f i 1 3 fi
f 'i O Dx 2
2 Dx
38
High-accuracy differentiation formulas
Forward schemes
39
High-accuracy differentiation formulas
Centered schemes
Example
40
Example
Example
41
Taylor expansion: multivariable
For a two-dimensional function f (u,v)
f f
f ui 1 ,vi 1 f ui ,vi
ui 1 ui vi 1 vi
u v
1 f 2
f
2
2 f 2
2 ui 1 ui 2 ui 1 ui vi 1 vi 2 vi 1 vi ....
2
2 ! u uv v
Partial derivatives
For a two-dimensional function f (x,y)
f f x Dx, y f x Dx, y
x 2 Dx
f f x, y Dy f x, y Dy
y 2 Dy
Mixed partial derivative
2 f f
xy x y
f x Dx, y f x Dx, y
f
2
y y
xy 2Dx
f x Dx, y Dy f x Dx, y Dy f x Dx, y Dy f x Dx, y Dy
2 f 2 Dy 2Dy
O Dx , Dy
2 2
xy 2Dx
42
Initial value problem of ODE
Standard form of the initial value problem of ODE
df t
F f t , t I.C. (Initial condition) f 0 f
0
dt
df f d2 f
Ex.1: , f 0 f 0 Ex. 2: 2 f , f 0 f , f ' 0 f '
2 0 0
dt dt
t df
f1 f , f 2 f '
f f 0e dt
df1
f2 d f1 0 1 f1
:time constant dt
f 2 0 f
relaxation time df 2 dt 2 2
2 f1
f
0
f =5,=1 dt
df
Af , f 0 f 0
dt
time
Discretization of time
f n-1 fn
f0 f nmax
f n+1
43
Explicit and implicit Euler methods
Discretization of time
f t n f nDt f n , f t n 1 f t n Dt f n 1
Forward and backward finite differences for a derivative
n
df d 2 f n Dt 2 df n f n 1 f n
f t n Dt f t n Dt 2 ... Forward
dt dt 2 dt Dt
n Backward
df d 2 f n Dt 2 n
n 1
f t Dt f t
n
n n
Dt 2 ... df f f
dt dt 2 (no new
dt Dt information)
df
n 1
d2 f n 1
Dt 2 df n 1
f n 1 f n Backward
f t n f t n Dt Dt ...
dt dt 2 2 dt Dt
n n 1
df df
f n 1 n
f Dt Explicit Euler Implicit Euler f n 1 f n Dt
dt dt
Unstable
stop Dt 2 FAIL
IMPORTANT: Know-How
Dt in explicit method must be less than the time constant!
44
Round-off error
Limitation of the digit numbers computer
Dt 1, Dt 0.000001
f new 1 f old If
f new 0.999999 f old
If CPU can handle only 5 digits, 0.9999991, then f
new
f old
round-off
Mathematically Dt0 makes FDE solution converge to exact
solution
In reality, too small Dt results in increasing errors
Error
Dt
2nd-order Runge-Kutta
k4 F f n k3Dt ,t n 1
Dt
1
F f n , t n
n
f 2
fn
2
n 1
n 12 n 12
f f Dt F f , t
n
45
Other schemes for time derivation 2
f n 1 f n 55 F n 59 F n 1 37 F n 2 9 F n 3 24Dt
Predictor-corrector method
f n1 f n
F n F f * , t n1
Dt : implicit solution (corrector)
2
Exercise 25.4
y (0) 4
d2y dy
2
0.6 8 y 0 y '(0) 0
dx dx
x[0 : 5]
Numerical solution Dx 0.5
dy
z new
y old Dx z old
dx Explicit Euler y
dz
z new z old Dx 8 y old 0.6 z old
0.6 z 8 y 0
dx y0 4
z0 0
46
Exercise 25.16
dx
v Explicit Euler
x new x old Dt v old
dt
Dt
v new v old cv old kx old
dv
m cv kx 0 m
dt
x 0 1m
v 0 0m / s
47
Numerical solution-exercise 25.16 (implicit Euler
method)
dx
v Implicit Euler
x new x old Dt v new
dt
Dt
v new v old cv new kx new
dv
m cv kx 0 m
dt
Linear equation system
x new Dt v new x old
Dt k new Dt c new
x 1 v v
old
m m
1 Dt new
Dt k x x old
Dt c new old
1 v v
m m
2D damped oscillator
dx dy
u v x 0 1m
dt dt
du dv y 0 1m
m cu kx m cv ky
dt dt u 0 1m / s
v 0 0m / s
m 1kg
c 0.25 Ns / m
k 1N / m
48
Numerical solution of 2D damped oscillator
E Ekinetic E potential
1 1
E mV 2 (t ) kx 2 (t )
2 2
49
Examples of boundary conditions
Pipeline:Navier-Stokes equations
(u, P)
Numerical Analysis
1. Introduction
2. Basic programming concepts
3. Error analysis
4. Roots of equations
5. Linear algebraic equations
6. Curve fitting
7. Ordinary differential equations (Section taken from Prof. Tomiyama)
8. Partial differential equations
9. Finite element method
50
Partial differential equations
Order
f 2 f
5f 5f
t t 2
Linear or non-linear
2 f 2 f 2 f 2 f
S f2
x 2 y 2 x 2 y 2
B 2 AC 0 Elliptic
B 2 AC 0 Parabolic
B 2 AC 0 Hyperbolic
51
Typical equations
Diffusion equation
f 2 f : diffusion coefficient [m2/s] Parabolic
2
t x
Wave equation
2 f 2 f
2
c: phase velocity [m/s] Hyperbolic
c 0
t 2 x 2
Poisson equation
Laplace equation
2 f 2 f Elliptic
S S: source & sink
x 2 y 2
Navier- u u 2u 1 P Parabolic
u 2
Stokes eq. t x x x Hyperbolic
Elliptic
52
Advection and diffusion
Computational domain
Discretization technique
53
Discretization in 1D&time (point data)
f xi ,t n fi n ,
f xi ,t n 1 fi n 1 ,
f xi 1 ,t n 1 f i n11
f ni-1 f ni
f n f nimax
1 f ni+1
tn
x1 xi-1 xi xi+1 ximax
i=1 Dx i = imax
f n+1i+1 Dt
f n+11 f n+1
f n+1i imax
f n+1i-1
tn+1
Initial condition & Boundary conditions
j=1
x1 xi-1 xi xi+1 ximax
i=1 Dx i = imax
54
Discretization of partial differential equations by
using the finite difference method
Using the following functions
f xi fi ,
f xi ,t n f i n ,
f xi , y j ,t n fijn ,
f xi , y j , zk ,t n f ijkn
Solution
fi n 1 fi n f n 2 fi n f i n1 fi n 1 fi n f n 1 2 f i n 1 f i n11
i 1 i 1
Dt Dx 2 Dt Dx 2
fijn 1 fijn f ijn f i n1 j f ijn fijn1
u v
Dt Dx Dy
fijn 1 fijn f ijn f i n1 j f ijn1 fijn
u v
Dt Dx Dy
fi n 1 2 fi n f i n 1 2 f i 1 2 f i f i 1
n n n
u
Dt 2 Dx 2
1 fi 1 jk fi 1 jk fij 1k fij 1k fijk 1 fijk 1
fijk 2 2 2
2(Dx Dy Dz ) Dx 2 Dy 2 Dz 2
Sijk
55
Elliptic equations
Relevant phenomena: potential and steady diffusion problems,
e.g. potential flow, electric potential, steady state T & C profiles
Basic form:
d2 f
S
dx 2
2 f 2 f 2 f
S 2 f S
x 2 y 2 z 2
div grad f S
df
Neumann condition: 0 at x=L
dx
FDE
fi 1 2 f i f i 1 f1 1 f i max fi max 1
i
Dx 2
56
Variation of the number of divisions for 1D
Poisson equation (ODE)
f1 1
i=1 i=imax=2 f 2 f 2 1 f1
f1 1
f 2 1 2 f 2 f 2 1 f1 2 f 2 f3
i=1 Dx i=2 i=imax=3 2
Dx 2 Dx 2
f 3 f 31 f 2
f1 1
f1 2 f2 f3 2Dx2
f2 2 f3 f4 3Dx2
... ... ... ...
fi1 2 fi fi1 iDx2
... ... ... ...
fi max3 2 fi max2 fi max1 i max2Dx2
fi max2 2 fi max1 fimax i max1Dx2
fi max fi max1
57
Linear system for 1D Poisson equation
1 f1 1
1 2 1 f Dx2
2 2
1 2 1 f3 3Dx2
... ... ... ... ...
1 2 1 fi iDx2
... ... ... ... ...
1 2 1 f Dx2
i max2 i max2 2
1 2 1 fi max1 i max1Dx
1 fi max fimax1
This matrix is imax x imax
This can be huge
Sparse matrix
Example 27.1
Problem sketch and conditions d 2T
h '(Ta T ) 0
dx 2
L 10m
Ta 20C
TBC1 TBC2
Tx 0 40C
Tx L 200C
h ' 0.01m 2
T2 T3 T4 T5
1
Dx 2
Ti 1 2Ti Ti 1 h ' Ta Ti 0 Discretization
58
Solution Ex. 27.1
Linear equation system 2.04 1 0 0 T2 40.8
1 2.04 1 0 T 0.8
3
0 1 2.04 1 T4 0.8
0 0 1 2.04 T5 200.8
Temperature
distribution
Parabolic equations
Relevant phenomena: diffusion, heat conduction, viscous flow
Basic form:
f 2 f
2
t x
f 2 f 2 f 2 f f
2 2 2 2 f
t t
x y z
f
div grad f
t
Conditions for obtaining solution f (x,t):
Initial conditions: f (x,0)
Boundary conditions
Steady state solution of parabolic eq.= solution of elliptic eq.
59
Example of a parabolic equation: heat equation
(diffusion)
Problem f t , x 2 f
2 (0 x L)
t x
60
Solution-Ex. 27.1
Temperature distribution in steady state
Dt n 1 2Dt Dt
T
2 i 1
1 2 h ' Dt Ti n 1 2 Ti n11 Ti n h ' DtTa
Dx Dx Dx
61
Example of a parabolic equation: 2D heat
diffusion in a plate
Problem
T 2T 2T
2 2
t x y
T : thermal diffusivity
0
y : termal conductivity
h: convection heat
T T transfer coefficient
0 h T T
x x
T T0
62
Example of a parabolic equation: non-
dimensional equation
T 2T 2T x * t * T T
2 2 x* , t ,T T0 T : cooling
t x y L0 t0 T0 T
2
*
T *
T0 T T
2 *2 T T0 T T ...
*
t0 t L0 x
T * 2T * 2T * t0
* *2 *2 *
t * x y L20
T
h T T
x
T * T0 T T h T * T0 T T T
*
L0 x
T * hL0 *
* T BiT *
x
T * Example of boundary
0
y * condition
T *
T* BiT *
0 T * x*
x* BiT *
x* T* T*
i max * i max 1 BiT *i max
hL Dx
Bi 0 5
T *i max 1
T *i max
1 Dx* Bi
T* 1
63
Example of a parabolic equation: discretization
of 2D heat diffusion in a plate-Explicit Euler
method
Problem
T 2T 2T
2 2
t x y
Tijn 1 Tijn Ti n1 j 2Tijn Ti n1 j Tijn1 2Tijn Tijn1
Dt D x 2
D y 2
j
64
System of equations for 2D heat diffusion in a
plate-Implicit Euler method
For example: imax=5, jmax=5
Excluding the borders, there are 3 interior points in x and y
13F F 0 F 0 0 0 0 0 T2,2n1 FT0 T2,2n
F 14F F
0 F 0 0 0 0 T3,2n1 FT0 T3,2n
0 F 14FC 0 0 F 0 0 0 T4,2n1 FT0 T4,2n
F 0 0 13F F 0 F 0 0 T2,3n1 T2,3n
0 F 0 F 14F F 0 F 0 T3,3n1 T3,3n
n1 n
0 0 F 0 F 14 F C 0 0 F T4,3 T4,3
0 0 0 F 0 0 12F F 0 T2,4n1 T2,4n
0 0 0 0 F 0 F 13F F T3,4n1 T3,4n
0
0 0 0 0 F 0 F 13FCT4,4n1 T4,4n
Dt F
F C
Dx 2 1 Dx Bi
65
Hyperbolic equations
Relevant phenomena: all kind of waves (sound, light, water
wave)
Basic form: 2 f 2 f
c 0
2
t 2 x 2
2 f 2 f
2
2 f 2 f 2 f
c 2 2 0 c 2 2 f 0
t 2 x y 2
z t 2
f
2
c 2 div grad f 0
t 2
66
Example of a hyperbolic equation: advection
equation in 2D
General form and discretization
f
u f 0
t
f f f
u v 0
t x y
f f f
u v
t x y
n
f ijn 1 f ijn f f
u v
Dt x y
67
Example of a hyperbolic equation: advection-
diffusion equation in 1D
f f 2 f x t
u 2 x* , t *
t x x L0 L0 / u
f f 1 f2
Lu
* , Pe 0
t x
*
Pe x *2
f f2
x=0 x=L
Pe * *2 0 Steady state
x x f=0 f=1
Solve with first-order upwind and central difference schemes
U 0U 0 u * U 0U 0u* u * U 0 2u *
L0 t * L0 x* L0 L0 x*2
u * * u
*
2u * L0U 0
u , Pe
t * x U 0 L0 x*2
u *
u*
1 u 2 *
Non-dimensional form
u*
t *
x Pe x*2
68
Parallel computation with OpenMP (shared
memory)
• Compilation with OpenMP
gfortran code.f90 -fopenmp
• Enable OpenMP in the main program
!$ use omp_lib
!$ call omp_set_num_threads(l)
• Put tags in the code
!$ omp parallel private (m)
!$ omp do
do m = 1, mmax
f ( m) f old (m) Dt A(m) C (m) F ( m)old !$ omp end do
69
Equations of motion for incompressible fluids &
discretization
u 1
u 0 u u P 2 u g
t
u u u u 1 2u 2u 2u
u v w P 2 2 2 g
t x y z x y z
Discretization
n 1
uijn1 uin11j vijn1 vijn11
D 0
ij
Dx Dy u n u n u n
ij 0
u ijn i 1nj
n
u 0
x
For velocity u ui 1 j uij n
ij
Poiseuille flow in 2D
Inflow Outflow
uin =1
Fluid
Pout=0
vin =0
70
Discretization of space by the finite volume
method
Fictitious cells
jmax+1
jmax
Dy
Staggered mesh 1
Pij+1 Pi+1j+1
j +1 ui-1j+1 0
vij uij+1 v
i+1j 0 1 i Dx imax imax+1
Pij Pi+1j
j ui-1j
uij
vij-1 vi+1j-1
i i +1
71
OpenFOAM
1. Introduction
https://openfoam.org/
2. Installation
https://openfoam.org/download/windows/
https://www.youtube.com/watch?v=2nX483FKMj0
https://www.youtube.com/watch?v=e90FyyfVbe0
3. Structure
72
Solver
• https://github.com/OpenFOAM/OpenFOAM-
dev/blob/master/applications/solvers/incompressible/icoFoam/i
coFoam.C
73
Units
[0 1 -1 0 0 0 0]
0 Mass[kg]
1 Length[m]
1 Time[s]
0 Temperature[K]
0 Quantity[mol]
0 Current[A]
0 Luminous intensity[cd]
BlockMeshDict
3 2
7 6
• Vertices
• Faces
0 1
4 5
74
Numerical results
• Grid: blockMesh
• Solver: icoFoam
• Visualize: touch example.foam
T *
0
y * laplacianFoam:
Heat conduction
T * T *
0 * BiT *
x* x
hL
Bi 0 5
T* 1
75
Non-dimensional 2D heat diffusion in a plate
with OpenFOAM
Problem
T * 2T * 2T *
* *2 *2
t * x y
T *
0
y *
T *
T * BiT *
0 x*
x* h Bi, T 0, TB T *
* 1
1
T* 1
Dx / 2
T *
0
y *
T *
BiT *
x*
1 1
f
T*
0 1 1
x* h BiDx / 2
refValue=T∞=0, refGradient=0
T* 1
If Dx=1 and Bi=5, then, valueFraction=f=0.714286
76
167
77