Anda di halaman 1dari 144

Numerical analysis

ROBERTO CARLOS ROJAS MOLINA

Escuela Politécnica Nacional


Facultad de Ingeniería Mecánica

11
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Rules & evaluation
• Ask, work, comment, criticize, etc during the class and USB
• Be punctual*
• Respect: listen to, silence (mute)
• No cell phones unless it is an emergency call, ???
• NOT CHEAT
• No postponed examinations (valid reason)
Evaluation 100 100
Class participation (attendance), homework 10 10
Project 1.5+1.5 1.5+1.5
Test1 30 30
Exam 30 30
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
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
• Some classes at Kobe University: Akira Kageyama
• Any Fortran language & compiler user reference guide
• Any Python guide: numpy, sympy, scipy, pandas, matplotlib
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Numerical Analysis
1. Introduction
2. Basic programming concepts
3. Some equations in Mechanical Engineering
4. Linear algebraic equations
5. Roots of equations
6. Optimization
7. Curve fitting
8. Numerical differentiation and integration
9. Ordinary differential equations
10.Partial differential equations
Property of Roberto Rojas Property of Roberto Rojas
There
Do not are three
distribute main
or use scientific
without approaches
permission of thetoauthor
study a problem

Experimental Simulation More complicated


approach * MH4 Laboratory, Kobe University
situation
Property of Roberto Rojas Property of Roberto Rojas
Multiscale
Do not distributemodeling: there are
or use without three scales
permission of thetoauthor
study a physical phenomenon
Microscopic Scale Mesoscopic Scale Macroscopic Scale

* 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

Molecular Dynamics Lattice Boltzmann Discretization of Navier-


(Newton) Method Stokes equations
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Introduction
• 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
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Software&tools
• Programming: Fortran (Gfortran)
• Text editor: Visual Studio Code, Notepad++

• Visualization tools: gnuplot


Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Compiler
• Translate programming language (Fortran) to binary code so
that the computer can understand what we want to do
(commands)
• We are going to live in the “command prompt” or terminal

Windows

Linux
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or useWINDOWS
without permission of the author
Gfortran installation by MinGW
• Free Fortran compiler
https://sourceforge.net/projects/mingw-
w64/files/Toolchains%20targetting%20Win32/Personal%20Build
s/mingw-builds/installer/
WINDOWS
Property of Roberto Rojas Property of Roberto Rojas
Change
Do not system
distribute pathwithout
or use to enable commands
permission at the
of the author
terminal: Gfortran
1. My computer: properties
2. Advanced system settings
3. Advanced
4. Environment variables
5. Path and then edit
6. New
Property of Roberto Rojas Property of Roberto Rojas
LINUX permission of the author
Do not distribute or use without
Gfortran and Python installation
1. This tutorial is available at the shared folder
2. If the installation goes well, you will have this:
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Text editor
• Create a new text file and rename it to ****.f90
• Write the code: programming language Fortran
• Different font colors for: functions, comments, commands, etc.

0visualizatecolorstexteditor.f90
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Text editor: Visual Studio Code
• Shift+Control+p
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or useWINDOWS
without permission of the author
How to show file extensions

1. Explorer
2. View and then Options
3. Change folders and search
options
4. Uncheck hide extensions
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Compilation: Option 1
• Open Command Prompt from Windows Start button
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Compilation: Option 1 (continued)
• Go to the folder containing the code : clase.f90

• Compile the code, gfortran

• Run de compiled program

Windows

Linux
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or useWindows
without permission of the author
Compilation: Option 1-Problems
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or useWindows
without permission of the author
Compilation: Option 2
• Open Run Terminal directly from the compiler MinGW
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Visualization application: ParaView
• Read files: *.vtk (special format, numbers)
• Color distribution of parameters: low (blue) and high (red)
• http://www.paraview.org/
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Visualization application: gnuplot
• Plot functions and data files
• Read files: *.txt, *.dat, *.d, *.csv
• http://www.gnuplot.info/

Gnuplot folder:

scripts

screenshoots

Screen shots
Property of Roberto Rojas Property of Roberto Rojas
Visualization
Do not distribute application:
or use without permission of the author
Graph digitizing system-GSYS
• Digitalize images for comparison
• Text files with selected format
• http://www.jcprg.org/gsys/
• Java
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Numerical Analysis
1. Introduction
2. Basic programming concepts
3. Some equations in Mechanical Engineering
4. Linear algebraic equations
5. Roots of equations
6. Optimization
7. Curve fitting
8. Numerical differentiation and integration
9. Ordinary differential equations
10.Partial differential equations
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
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 and return a value. CALL name ……..
functions: part of the code to perform a name2(x) def name2(variable):
determined task and return a value with given calculation=f(variable)
arguments. name2(x) return calculation
modules: usually used to separate parts of the import module_name
code and improve readability, e.g declare
common variables. use module_name
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Basic programming concepts (continued)
• Syntax errors: depends on the programming language
• Conditional and repeated instructions

• Good program (code)


• readability
• self checking code (/ 0, logarithms of negative numbers)
• represent physics
• We are going to live in the terminal so lets practice!
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Why Fortran?
• 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 focus on
“numerical computing”, for this better Python
• Since it is a compiled language like c, cpp, visual basic, and
so on (opposite to interpreted language like matlab,
mathematica, python, etc), its is very fast

gfortran name_of_program python3 name_of_program


Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Data type and variable declaration in Fortran
• Data: variables, constants or parameters, operators and
reserved words
• Reserved words:if,then,else, do, end, sin, dsin, asin, dasin,etc
• Variable declaration: local (functions, subroutine) and global
• Integer numbers: INTEGER name
• Real numbers (floating point numbers): REAL(4) name,
REAL (8) name
• Text strings: CHARACTER name
• Arrays: REAL(8), DIMENSIONS (dim1,dim2) :: name,
REAL(8) :: name(dim)
• Parameters or constants: REAL(8), PARAMETER :: name
• Logical statements: LOGICAL :: name
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
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)

Sign (number) 1 bit 1 bit

Exponent width (2n) 11 bit (consider 1 bit for sign) 8 bit (consider 1 bit for sign)

Significant precision 52 bit 23 bit


(Mantissa)

Approximately 253~ 15 -17 digits 224~ 6 -9 digits


significant digits
Max. and min. 211-1~ 1024 21024~ 10308 28-1~ 128 2128~ 1038
number 10-308 10-38

* 1 byte = 8 bites, example: one character is one bite 1characterbit.txt 2numbers.f90


Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Fortran: operators, if, loop, and others
• Operators: +,-,*,/,=,**,mod(A,B),//
• Logical expressions: true or false
• .lt. (<); .le. (<=); .gt. (>); .ge. (>=); .eq. (==); .ne. (/=)
• Logical operators: .and.; .or.
• Conditional statements: if (logical expression) then
else if
else 3writedisplay.f90
end if
• Loops: do var =exp1, exp2, exp3 4readdisplayif.f90
do while (logical expressions)
5sum.f90
• Jump to specific location: goto, continue
• Exit ends the loop and go to the line after the loop, cycle skip
the rest of the loop and go to the start of the loop stop
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Fortran: I/O
• Read for input and write for output, e.g. read (unit, format)
• 3I6 : 3 integer numbers of width 6 digits
• F12.3: 1 real number of width 12 with 3 decimal digits (no
exponent)
• E10.3: 1 real number in exponent notation of width 10 and
3 decimal digits in the mantissa (exponent E)
• 2D9.5: two real numbers of width 9 with 5 decimal digits
(exponent D) 6readmatrix.f90

• A10: string with 10 characters 7readfromfile.f90

• 5X: 5 spaces 8example read from input

• /: new line 9grid

• File I/O: open (9-99, file= ‘name.dat’; ‘name.txt’; ‘name.d’;


any file, status= ‘old’; ‘new’; ’unknown’)
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Python: operators, if, loop, and others
• Operators: +,-,*,/,//,=,**,%,+
• Logical expressions: true or false
• <; .<=; >; >=; ==; !=
• Logical operators: and; or
• Conditional statements: if (logical expression):
expression
elif:
else:
• Loops: for var in range(exp1, exp2, exp3):
while (logical expressions):
• Break ends the loop and go to the line after the loop,
continue skip the rest of the loop and go back to the start
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
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

• File I/O name = open (‘name.dat’; ‘name.txt’; ‘name.d’; any


file, ‘r’; ‘w’)
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Tips for writting 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
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Some additional tips
• Flags: indicate compiler to perform additional tasks
• gfortran –o name code.f90:
this change the name of the executable file: “name.exe”
• gfortran –c module.f90:
compiles without a executable file: module.o (compiled
code), module.mod (to include a module in a program)
• gfortran –o name code.f90 module.o
compiles and include module.o previously compiled
• Bat files in Windows or run files in Linux: save time when
executing several commands
13 Modules, subroutines Factorial&Sum
10local.bat 11run.bat 11run 12PoiseuilleflowU0.f90
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
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, he could perform:
1mFLOPS * 100 years  3* 106 FLO
• In a computer, this is easy
• Computer speed: GFLOPS (109)
• Supercomputer: TFLOPS(1012)-PFLOPS(1015) www.top500.org
• Walk ~ 1m/s, light velocity ~ 3*108: 108
• Supercomputer vs. Brain : 1018 Taken from Akira Kageyama
Property of Roberto Rojas Property of Roberto Rojas
Tokyo
Do not distribute or Institute of Technology
use without permission of the author
Tsubame supercomputer 2.5

1442 nodes with 4264 GPUs: 2.5PFLOPS


Walk ~ 1m/s vs. light velocity ~ 3*108: 108
Brain ~ 1*10-3FLOPS vs. supercomputer : 1018
Property of Roberto Rojas Property of Roberto Rojas
Streamlines
Do not foruse
distribute or a given velocity
without field: vtk
permission filesauthor
of the
example taken from Fox&McDonald’s Introduction to Fluid Mechanics

Paraview: plot over line


Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Numerical Analysis
1. Introduction
2. Basic programming concepts
3. Some equations in Mechanical Engineering
4. Linear algebraic equations
5. Roots of equations
6. Optimization
7. Curve fitting
8. Numerical differentiation and integration
9. Ordinary differential equations
10.Partial differential equations
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
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    uu  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 

Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Elasticity & plasticity equations

 ji  ui 2
 Fi   2
x j t

1  ui u j 
 ij  
2  x j xi 

ij  Cijkl  kl
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Error analysis
• Perfect matching is IMPOSSIBLE
• Error: discrepancy between analytical, numerical and
experimental 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
2 3 4 5 6
x x x x x Code this equation
e x  1  x       ...... and test diferent
2! 3! 4! 5! 6! approximations

• Accuracy: closed to true value


• Precision: not closed to true value but in same position
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Relative error of single data
• Relative error is better for comparison or assessment of a
numerical technique

predicted  analytical Determine the


%error  100% relative error of
the previous
analitycal example

• Approximated error: for iterative methods


current  previous
%error  100%
current
• calculated until a condition is fulfilled, error in a given
range (es)
• Always look for the worst prediction
• Errors propagate and worst scenario UNSTABLE
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Total error
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Examples of error analysis: Poiseuille Flow
Uniform inflow at U0 2
Analytical Solution
Present method
No-slip
y
2R u/U0 1
x

No-slip Reynolds number


U 0 (2 R)
Re  0
Continuous outflow  -1 0 1
Velocity field y/R
1

u/Uo

-1

Pressure field Developed velocity profile


Property of Roberto Rojas Property of Roberto Rojas
Do notExamples
distribute of
or error analysis:
use without Streamlines
permission of author
of the
driven cavity flows
Re = 100 Re = 100

U0

No-slip boundary
No-slip boundary

y Re = 10000 Re = 10000

x No-slip boundary

H=201, Re=100 and 1000


H=601, Re=5000 and 10000

Evolution of the secondary vortices


Property of Roberto Rojas Property of Roberto Rojas
DoExamples of error
not distribute analysis:
or use withoutVelocity profiles
permission of author
of the
driven cavity flows
Re = 100 x/L0 Re = 1000
0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1
1 LBM (Hou et al.)
1 1 1
FDM (Ertuk at al.)
FDLBM FDLBM
0.8 0.8
U0

v/U0
No-slip boundary

v/U0
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
Property of Roberto Rojas Property of Roberto Rojas
Examples
Do not distributeofor
error
useanalysis: Sedimentation
without permission of theofauthor
single particles falling through stagnant liquids
Dimensions (10, 10, 16) cm   Time step Re
Case 
[kg/m3] [Pa s] [s]
Initial position (5, 5, 12.75) cm 1 970 373x10-3 0.86 1.56x10-4 1.3
Diameter 1.5 cm (762) 2 965 212x10-3 0.86 2.73x10-4 3.7
3 962 113x10-3 0.74 3.405x10-4 10.5
Density 1120 kg/m3
4 960 58x10-3 0.64 4.14x10-4 30.1
Falling velocity [m/s] 0

-0.05

IB-FDLBM Measured
-0.1 (Present) (ten Cate et al., 2002)
Case Case
1 1
2 2
3 3
4 4

-0.15
0 1 2 3 4
Reasonable predictions of
t [s] the falling velocity
Property of Roberto Rojas Property of Roberto Rojas
Examples
Do not distributeoforerror
use analysis: Drafting-kissing-
without permission of the author
tumbling motion of two particles
2
2 cm 1.8
P2(0.999,7.2) 1.6
1.4
Drafting

XP [cm]
1.2
P1(1,6.8) 1
Niu et al. (2006)
0.8
D= 0.2 cm 20 s-1 Particle 1
Particle 2
0.6 IB-LBM based on Dupuis (2008)
20Dx for D 0.4 Particle 1
Nm = 360 Kissing Particle 2
Present
0.2 Particle 1
1010 kg/m3 0 Particle 2
0 1 2 3 4 5

Vorticity
t [s]
g =981 cm/s2
8 cm 8
Niu et al. (2006)
7 Particle 1
Particle 2
IB-LBM based on Dupuis (2008)
6
Tumbling
Particle 1
= 1.0x10-3 5
Particle 2
Present

YP [cm]
Particle 1
Pa·s Particle 2
-20 s-1 4
 = 1.0x103 3
kg/m3
2
 = 0.65
1
A = 0.5 0
Reasonable predictions
0 1 of the
2 particle
t [s]
3 paths
4 5
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Errors in arrays: norm

Maximum

u predicted  u exact
l  max
IJ  fluid u exact

L2-norm

 u  x   u  x  
predicted exact 2
x fluid 
Error 
  u  x  
exact 2
x fluid
Property of Roberto Rojas Property of Roberto Rojas
Examples
Do not distributeoforerror
use analysis: Circular Couette
without permission of the author
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 R R
 out 0.5
U Rin R
 out
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)
Property of Roberto Rojas Property of Roberto Rojas
Examples
Do not distributeoforerror
use analysis: Circular Couette
without permission of the author
flow-errors at different grid resolutions
0.5 Error
0.45 +=10; A=0.5
upredicted  uexact
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
10-3 -1
0 -4 -3 -2 -1 0 1 2 3 10 100 101 102
10 10 10 10 10 10 10 10  for SRT, + for TRT
 Read the given files and calculate the norm
(maximun error) in the velocity profiles
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Numerical Analysis
1. Introduction
2. Basic programming concepts
3. Some equations in Mechanical Engineering
4. Linear algebraic equations
5. Roots of equations
6. Optimization
7. Curve fitting
8. Numerical differentiation and integration
9. Ordinary differential equations
10.Partial differential equations
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Linear algebraic equations
1. Gauss elimination
2. LU decomposition and matrix inversion
3. Special matrices and Gauss-Seidel
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Numerical Analysis
1. Introduction
2. Basic programming concepts
3. Some equations in Mechanical Engineering
4. Linear algebraic equations
5. Roots of equations
6. Optimization
7. Curve fitting
8. Numerical differentiation and integration
9. Ordinary differential equations
10.Partial differential equations
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Roots of equations
1. Bracketing methods
2. Opened method
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Graphical method
• Easy and allows to identify the points
where a given a function is cero
• Low accuracy
• This result can be used as input for more
accurate methods
explicit
Try now to isolate c (implicit)

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=70,v=40m/s, t=10s

f(y) = sin 2x + cos 10x [0:pi]


Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Bisection method
• Function changes sign
• Guess that contains the root
• Reduce width of the bracket
• Halve the increment and
check where the prediction is
• Run until convergence

Code the method


Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
False-point method
• Bisection: inefficient since f
magnitude is not considered
• Intersection with x is an
improved prediction

• Replace xu or xl with xr Code the method

• Check the sign of f(xl)*f(xr)


• Run until convergence
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Roots of equations
1. Bracketing methods
2. Opened method
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Introduction
• Bracketing methods are convergent, need interval
• Opened methods require only a single starting value or two
values that do not necessarily including the root. Sometimes,
these diverge. However, when these converge, it is very fast
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Newton-Raphson method
• Initial guess and a tangent to the x-axis
• The intersection with x-axis is the estimated root

Code the method


Example

Briefly explanation Maxima


Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Poor convergence
• Inflection points and oscillation
• Multiple roots
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Secant method
• Derivative is an issue
• Derivative can be approximated by a backward FD
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Secant method-continued

• May diverge
• Fast convergence
Modification
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Numerical Analysis
1. Introduction
2. Basic programming concepts
3. Some equations in Mechanical Engineering
4. Linear algebraic equations
5. Roots of equations
6. Optimization
7. Curve fitting
8. Numerical differentiation and integration
9. Ordinary differential equations
10.Partial differential equations
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Optimization
1. One-dimensional unconstrained optimization
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Numerical Analysis
1. Introduction
2. Basic programming concepts
3. Some equations in Mechanical Engineering
4. Linear algebraic equations
5. Roots of equations
6. Optimization
7. Curve fitting
8. Numerical differentiation and integration
9. Ordinary differential equations
10.Partial differential equations
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Curve fitting
1. Least-squares regression
2. Interpolation
3. Fourier approximation
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Numerical Analysis
1. Introduction
2. Basic programming concepts
3. Some equations in Mechanical Engineering
4. Linear algebraic equations
5. Roots of equations
6. Optimization
7. Curve fitting
8. Numerical differentiation and integration
9. Ordinary differential equations
10.Partial differential equations
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Introduction-derivation
 Derivative

 First derivative: slope


 Second derivative: curvature
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Introduction-integration
 Integral  What functions?
Continuous function
Discrete function
 Summation Zero order
f (x) f (x)

a b x a x
b
D n=6 D n=5
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Integration
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Integration-trapezoidal rule
 First order polynomial (line)  Slope straight line equation
y1  mx  B

 Exact for linear functions


 Multiple applications
improve the solution
Property of Roberto Rojas Property of Roberto Rojas
Integration-trapezoidal
Do not distribute rule-multiple
or use without permission of the author
applications
 Interval: equal width
Property of Roberto Rojas Property of Roberto Rojas
Integration-Simpson‘s
Do not distribute or use without1/3 rule-multiple
permission of the author
applications
 Higher-order polynomial
 3 points: limits and midway
 Second order polynomial
a b

y2  Ax 2  Bx  C

n=2

n: even
Property of Roberto Rojas Property of Roberto Rojas
Do notExample:
distributeFlow past
or use a circular
without cylinderof(bluff
permission the author
body), drag and lift coefficient
Fluid in motion: pressure and shear stress
Differential force (vector)
Area vector or
scalar? x-axis
y-axis

Area?
In 2D circle
dA=dS*span
dS=rd
dA=Dd/2*span

Given values:
projected : fluid density
V: freestream velocity
A projected: depends on
projected the body shape
Briefly explanation Paraview and file.vtk
Property of Roberto Rojas Property of Roberto Rojas
Flows
Do not past a circular
distribute cylinder
or use without at a wide range
permission of the of
author
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)
1 IB-FDLBM (Present)
10

CD St 0.2
100
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 
Property of Roberto Rojas Property of Roberto Rojas
Streamlines
Do not of flows
distribute or past a permission
use without stationary circular
of the author
cylinder at different Re
Re = 100 Re = 100000

Re = 20

Re = 40
A Pair of stationary Vortex shedding Irregular vortex
vortices shedding

Flow patterns are in good agreement with


experimental and numerical patterns
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Shear stress in flow past a circular cylinder
n
v


v cos()
u

u sin()
t

ut= u sin()-v cos()


Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Derivation
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Discretization
 Space and time are continuous
 Infinite  finite (discretize)
Continuous
Discrete

Time 1 Time 2
1D
space space

Time 1 Time 2

2D

space space
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Taylor expansion

f  xi   2 f  xi  Dx 2  3 f  xi  Dx3
f  xi  Dx   f  xi   Dx    ....
x x 2
2 x 3
6

Constant f  xi  Dx   f  xi  Zero order

Line f  xi  First order


f  xi  Dx   f  xi   Dx
x
Capture f  xi   2 f  xi  Dx 2 Second
curvature f  xi  Dx   f  xi   Dx  order
x x 2 2
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Basic finite difference schemes
 Discretization of space and time
xi  iDx ,t n  nDt f ( xi ,t n )  f in f ( xi ,t n 1 )  fin 1 f ( xi 1 ,t n )  f in1
 Difference approximation of derivative using Taylor expansion
1 
f xi  Dx ,t n
   n
 f xi ,t 
f n
x i
 2 f n Dx 2  3 f n Dx3
Dx  2
x i 2
 3
x i 6
 O Dx 4  
2 
f xi  Dx ,t n    n
 f xi ,t 
f n
x i
 2 f n Dx 2  3 f n Dx 3
Dx  2
x i 2
 3
x i 6
 O Dx 4   f(x)

f n f in1  fin f n fin  fin1


  ODx    ODx  i-1 i i+1
x i Dx x i Dx
1 Forward (downwind) 2 Backward (Upwind)
f n f in1  f in1
  O Dx 2   2 f n
x 2

f in1  2 f in  fin1
Dx 2
 
 O Dx 2
x i 2Dx i
1 - 2 Centered 1 + 2 Centered
Property of Roberto Rojas Property of Roberto Rojas
High-accuracy
Do not distribute or use differentiation formulas
without permission of the author
first derivative-forward scheme
Property of Roberto Rojas Property of Roberto Rojas
High-accuracy
Do not distribute or use differentiation formulas
without permission of the author
forward schemes
Property of Roberto Rojas Property of Roberto Rojas
High-accuracy
Do not distribute or use differentiation formulas
without permission of the author
backward schemes
Property of Roberto Rojas Property of Roberto Rojas
High-accuracy
Do not distribute or use differentiation formulas
without permission of the author
centered schemes
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Example

5
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Example
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Example
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
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  2 f 2 f 2 f 2
  2  ui 1  ui   2  ui 1  ui  vi 1  vi   2  vi 1  vi    ....
2

2 !  u uv v 
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Partial derivatives
 For a two-dimensional function f(x,y)

 Mixed partial derivative


Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Numerical Analysis
1. Introduction
2. Basic programming concepts
3. Some equations in Mechanical Engineering
4. Linear algebraic equations
5. Roots of equations
6. Optimization
7. Curve fitting
8. Numerical differentiation and integration
9. Ordinary differential equations (Section taken from Prof. Tomiyama)
10.Partial differential equations
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Initial value problem of ODE
 Standard form of initial value problem of ODE
df t 
 F  f t , t  I.C. (Initial Condition) f 0   f 0
dt
2
df f d f
Ex.1:   , f  0   f 0
Ex. 2:   2
f , f  0   f 0
, f '  0   f ' 0

dt  dt 2
t df
 f1  f , f 2  f ' 
f  f e
0 
dt
df1
 f2 d  f1   0 1   f1 
:time constant dt  2
dt  f   0  f 
relaxation time df 2  2   2
  f12
f

f0=5,=1 dt
df
 Af , f 0   f 0
dt

time
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Discretization in 1D

f n+1i-1 f n+1i f n+1i+1


tn+1

f ni-1 f ni f ni+1
tn
x1 xi-1 xi xi+1 ximax
i=1 i = imax
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
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, backward and centered differences for a derivative
f t  Dt   f t  
n n n n 1
n n df d 2
f Dt 2
df f  f n

dt
Dt  2
2
 ...  Forward
dt dt Dt
  
n n
df d2 f Dt 2
n n
f t  Dt  f t  Dt  2  ... df n
f n  f n1
dt dt 2  Backward
dn Dt
df n
f n 1  f n 1
 Centered
dt 2Dt
f(t)
n n 1
df df
f n 1 n
 f  Dt f n 1  f n  Dt
dt dt
n-1 n n+1
Explicit Euler Implicit Euler
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Euler method (pseudo code)
 Ex.: df f fn
  , f (0)  f 0 Explicit Euler: f n1  f n  Dt
dt  
INPUT PARAMETERS tau, f0, dt, tend
f=f0
t=0.d0 Numerical stability
do n = 1, tend  Dt  n
n 1
t=t+dt f  1   f  rf n
f=f-(f/tau)*dt   
write(*,*) t,f r 1
end do  f n  rn f 0
Unstable
stop  Dt  2 FAIL
IMPORTANT: Know-How
Dt in explicit method must be less than the time constant!
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Round-off Error
 Limitation of the digit numbers  computer

n 1 n fn If   1, Dt  0.000001
f  f  Dt

f n 1  0.999999 f n
n 1
If CPU can handle only 5 digits, 0.9999991, then f  f n

round-off
Mathematically Dt0 makes FDE solution converge to exact
solution
In reality, too small Dt results in increasing errors

Error

Dt
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Other schemes for time derivation 1
 Euler type  1 Euler
f n 1  f n   F n  1    F n 1  Dt   0 Backward Euler  impl.
  1 / 2 Trapezoidal

 4th order Runge-Kutta n  n k1Dt n Dt 


k1  F k2  F  f  ,t  
 2 2 
n 1 k1  2k2  2k3  k4  k2 Dt n Dt 
f  f  n
Dt k3  F  n
f  ,t  
6  2 2 

 2nd order Runge-Kutta 


k4  F f n  k3Dt ,t n 1 
 
1
Dt
Ff n 
n
f  f 
2 n

2
  n  
1
f n 1  f n  Dt  F  f 2 
  
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Other schemes for time derivation 2

 Adams-Bashforth (coefficients calculated using Runge-Kutta)

f n 1 n
 n
 f  55F  59 F n 1
 37 F n2
 9F n 3
 Dt
24
 Predictor-corrector method

f *  f n  F n Dt : explicit solution (predictor)


n 1 F n  F  f * , t n1 
f  f  n
Dt : implicit solution (corrector)
2
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Other examples
 25_4 y (0)  4
d2y dy y '(0)  0
2
 0.6  8 y  0
dx dx x[0 : 5]
Solution Dx  0.5
dy
z
dx y new
y old
 Dx  z old

dz
 0.6 z  8 y  0 z new  z old  Dx  8 y old  0.6 z old 
dx
y0  4
z0  0
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Other examples
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Other examples
Solution
dx x new
x old
 Dt  v old
v
dt Dt
dv v new
v old
  cv  kx 
old old

m  cv  kx  0 m
dt

x 0  1[m]
v 0  0[m / s ]
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Other examples
 2D damped oscillator
x 0  1[m]
dx dy
u v y 0  1[m]
dt dt
du dv u 0  1[m / s]
m    cu  kx  m    cv  ky 
dt dt v 0  0[m / s ]
m  1[kg ]
c  0.25[ Ns / m]
k  1[ N / m]
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Other examples Solution

E  Ekinetic  E potential
1 1 2
E  mV (t )  kx (t )
2

2 2
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Numerical Analysis
1. Introduction
2. Basic programming concepts
3. Some equations in Mechanical Engineering
4. Linear algebraic equations
5. Roots of equations
6. Optimization
7. Curve fitting
8. Numerical differentiation and integration
9. Ordinary differential equations (Section taken from Prof. Tomiyama)
10.Partial differential equations
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Partial differential equations
 Order

f 2 f
5f 5f
t t 2

 Linear or non-linear

2 f 2 f 2 f 2 f
 2 S   f 2

x 2
y x 2 y 2
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Types of partial differential equations
 General form of 2nd order linear PDE
2 f 2 f 2 f f f
A 2 B  C 2  a  b  cf  g  0
x xy y x y

B 2  AC  0 Elliptic

B 2  AC  0 Parabolic

B 2  AC  0 Hyperbolic
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Typical equations
Diffusion equation
f 2 f : diffusion coefficient [m2/s] Parabolic
 2
t x
Wave equation
2 f  2
f c: phase velocity [m/s] Hyperbolic
c 2
0
t 2
x 2

Poisson equation
Laplace equation
2 f 2 f Elliptic
 2 S S: source & sink
x 2
y
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Examples of 2 nd order PDF
 Advection-diffusion equation
f f 2 f f
u   2  S  u   f   2 f   S
t x x t
Advection f  u f  0 f f f f
u v w  0 Hyperbolic
eq. t x t x y z
f  2
f f  2 f 2 f 2 f 
Diffusion  2  2  2  2  Parabolic
eq . t x t  x y z 
Poisson d2 f 2 f 2 f 2 f Elliptic
S  2  2 S
eq. dx 2
x 2
y z

Navier- u u  2
u 1 P
u  2  Parabolic
Stokes eq. t x x  x Hyperbolic
Elliptic
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Advection-diffusion equation
Property of Roberto Rojas Property of Roberto Rojas
Discretization
Do not distribute orofuse
partial differential
without equations
permission of the by
author
finite difference method
 Using the following functions
f  iDx   f  xi  , f ( xi ,t n )  f i n , f ( xi , y j ,t n )  f ijn , f ( xi , y j , zk ,t n )  f ijkn
 Write down FDE:
f 2 f Time: centered, space: centered
 2 Time: backward, space: centered
t x
f f f Time: forward, space: upwind (u,v >0)
u v 0 Time: forward, space: upwind (u>0, v <0)
t x y
2 f 2 2 f
2
u 2
0 Time: centered, space: centered
t x
2 f 2 f 2 f
 2  2  S ( x, y, z ) Space: centered and solve for f
x 2
y z ijk
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Solution
f i n 1  fi n 1  fi n1  2 fi n  fi n1  f i n 1  f i n  fi n11  2 fi n 1  f i n11 
     
2Dt  Dx 2
 Dt  D x 2

f ijn 1  fijn f ijn  f i n1 j f ijn  fijn1
 u v
Dt Dx Dy
f ijn 1  fijn f ijn  f i n1 j f ijn1  f ijn
 u v
Dt Dx Dy

f i n 1  2 f i n  fi n 1 f n
 2 f n
 f n
 u 2 i 1 i i 1

Dt 2 Dx 2
1  fi 1 jk  fi 1 jk f1 j 1k  fij 1k fijk 1  fijk 1 
fijk  2 2 2  2
 2
 2 
2( x  Dy  Dz )  Dx Dy Dz 
 Sijk
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Elliptic equations
 Relevant phenomena: potential and steady diffusion problems,
e.g. potential flow, electric potential, steady state T&C profiles
 Basic form:
d2 f
2
S
dx
2 f 2 f 2 f
 2  2 S 2 f  S
x 2
y z

div grad f  S

S=0  Laplace equation S≠0  Poisson equation


Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Boundary value problem (boundary conditions)
 PDE in space
 Types of boundary conditions:
 Dirichlet condition (B.C. of first kind)  specify the values of f
 Neumann condition (B.C. of second kind)  specify f / n
 Robin condition (Mixed B.C.)  specify af  bf / n
 Types of boundary value problem

Dirichlet Pr. Neumann Pr. Mixed Pr.


Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Example-elliptic equation: 1D Poisson equation
 Problem d2 f
2
 (0  x  L)
dx

 Dirichlet condition: f =1 at x=0 x=0 x=L


i=1 i=imax
 Neumann condition: df  0 at x=L
dx
f i 1  2 f i  f i 1 fi max  fi max 1
 FDE  i f1  1
Dx 2

This is a system of linear equations


with imax equations
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
1D Poisson equation-solution

f1  1
f1 2 f2 f3  2Dx2
f2 2 f3 f4  3Dx2
... ... ...  ...
fi1 2 fi fi1  i Dx2
... ... ...  ...
fi max3 2 fi max2 fi max1  i max2Dx2
fi max2 2 fi max1 fi max  i max1Dx2
fi max  fi max1

imax equations
imax unknows ( fi )
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
1D Poisson equation-solution-linear system
1   f1   1 
1 2 1   f    Dx2 
  2   2 
 1 2 1   f3   3Dx  2

    
 ... ... ...   ...   ... 
 1 2 1   fi    iDx2 
    
 ... ... ...   ...   ... 
 1 2 1   fi max2  i max2Dx2 
    
 1 2 1  fi max1   i max1Dx 
2

 1 f   f 
  i max   i max1 
This matrix is imax x imax
This can be large
Sparse matrix
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Example 27.1
 Problem sketch and conditions  2T
 h(Ta  T )  0
x 2

L  10m
Ta  20C
TBC1 TBC2
T1  40C
T2  200C
h '  0.01m2
T1 T2 T3 T4

1
2  i 1
T  2Ti  Ti 1   h ' Ta  Ti 0 Discretization
Dx
Valid for all interior nodes.
Ti 1   2  h ' Dx  Ti  Ti 1  h ' Dx Ta  0
2 2
First and last terms are BC
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Solution Ex. 27.1
Linear system

Temperature
distribution
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
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 (r,t):
 Initial conditions: f (r,0)
 Boundary conditions
 Steady state solution of parabolic eq.= solution of elliptic eq.
Property of Roberto Rojas Property of Roberto Rojas
Do notExample-parabolic equation:
distribute or use without heat equation
permission of the author
(diffusion)
 Problem f  t , x  2 f
  2 (0  x  L)
t x

 Dirichlet condition: f =1 at x=0 x=0 x=L


i=1 i=imax
 Neumann condition: f  0 at x=L f
t 1
 Initial condition: f 0i=2:imax =0
 FDE x
f i n 1  fi n f i n1  2 f i n  f i n1

Dt Dx 2
Dt
fi n 1
 fi  d  f
n n
i 1  2 fi  f
n n
i 1  d 2
Diffusion number
Dx
d<½
B.C.: f 1
1
n
f n
i max  f n
i max 1 stable
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Example 27.1
 Problem sketch, conditions, assumptions and discretization
 2T
 h(Ta  T )  0
x 2

L  10m
Ta  20C
T1  40C
T2  200C
h  0.01m 2
T  2T
 2  h(Ta  T )  0 Steady state solution
t x
Dt
Ti n 1
 Ti  2 Ti n1  2Ti n  Ti n1   Dt * h * Ta  Ti n 
n

Dx
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Solution Ex. 27.1
 Temperature distribution in steady state
Property of Roberto Rojas Property of Roberto Rojas
Example-parabolic
Do not distribute or use equation: 2D heat diffusion
without permission of the author
in a plate
 Problem
T   2T  2T 
  2  2 
t  x y 

T Example of boundary
0 condition
y
T
T T   BiT
0   BiT x
x x Ti max  Ti max 1
  BiTi max 1
Dx
Bi  5 Ti max  Ti max 1  DxBiTi max 1

T 1
Property of Roberto Rojas Property of Roberto Rojas
2D heat diffusion
Do not distribute in a plate-temperature
or use without permission of the author
distribution
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Hyperbolic equations
 Relevant phenomena: all kind of waves sound, light, water
wave
 Basic form:  2 f  2
f
 c 2
0
t 2
x 2

2 f 2 f
2
2 f 2 f  2 f
c  2  2  2   0  c  f 0
2 2

t 2
 x y z  t 2

2 f
 c 2
div grad f  0
t 2

 Conditions for obtaining solution f (r,t):


 Two initial conditions: f (r,0) and f
t r ,0
 Boundary conditions
Property of Roberto Rojas Property of Roberto Rojas
Example-hyperbolic
Do not distribute equation:
or use without advection
permission of the author
equation
 General form and discretization

 u    0
t
  
u v 0
t x y
    
 u  v 
t  x y 
n
ijn 1  ijn    
 u  v 
Dt  x y 
Property of Roberto Rojas Property of Roberto Rojas
Example-hyperbolic
Do not distribute equation:
or use without advection
permission of the author
equation in one dimension
 2  2
      
      0
2
u  u  u
t 2
x 2
 t x  t x 
 
u 0
t x
in 1  in in  in1
 u First order upwind scheme
Dt Dx
n 1 Dt n Dt n
i  i  u
n
i  u i 1
Dx Dx
Dt n
n 1
i  i  un

Dx
  i   i 1 
n
CFL (Courant-Friedrich-Levy) number
in 1  in  c  in  in1  u Dt c<1
c
Dx stable
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Finite difference schemes for advection
 First order upwind scheme f f  xI , y J   f  xI  m , y J 
  ODx 
 Second order u.s. x Dx
f 3 f  xI , y J   4 f  xI  m , y J   f  xI  2 m , y J 
  ODx 2 
x 2Dx
 Third order u.s.
f f  xI  2 m , y J   6 f  xI  m , y J   3 f  xI , y J   2 f  xI  m , y J 
  ODx 3 
x 6Dx
m  1,u  0; m  1,u  0
 Fourth order central difference scheme
f  f  xI  2 , y J   8 f  xI 1 , y J   8 f  xI 1 , y J   f  xI  2 , y J 
  O Dx 4 
x 12Dx

 WENO fifth order (homework)


Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Advection-diffusion equation
Property of Roberto Rojas Property of Roberto Rojas
Do notParallel computation
distribute withpermission
or use without OpenMp (shared
of the author
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

end do !$ omp end parallel


Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Equations of fluid motion&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 

D n 1

uijn 1  uin11j

vijn 1  vijn11
0
u  u
n
ij
n
i 1 j
 ij  0 
u n

u  
n

u  0
ij
Dx Dy x
u  u
n
i 1 j
n
ij
n
ij

uijn1  uijn u n
u n
y P n1
i 1 j  P n1
ij
 u n
i 1 j  2u n
ij  u n
i 1 j u n
ij 1  2u n
ij  u n
ij 1

 uij
n x
 vC
n
    
Dt Dx Dy Dx  Dx 2
Dy 2
 
Pij+1
( j +1 ) ui-1j+1 uij+1
vij vi+1j
Staggered mesh
Pij Pi+1j
(j) ui-1j
uij

vij-1 vi+1j-1
(i) ( i +1 )
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author

134
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Elliptic equation solvers
 Linear equation system
 Iterative methods
 Point Jacobi method
 Gauss-Seidel method
 SOR method
 ICCG (Incomplete conjugate gradient)
 ILUCR (Incomplete LU conjugate residual)
 BiCGStab (Bi-conjugate conjugate gradient stabilize)
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Point Jacobi method
 Poisson equation

d2 f f i 1  2 f i  fi 1
S   Si
dx 2
Dx 2

 Iterative process: the m+1-th value is a function of the m-th


value 1 m
fi   fi 1  fi m1  Dx 2 Si 
m 1

2
 The iteration process finishes (convergence) if the maximum
error is less than the admissible error
max fi m 1  f i m  
 This method is very slow
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Gauss-Seidel method
 Poisson equation

d2 f f i 1  2 f i  fi 1
S   Si
dx 2
Dx 2

 Iterative process: the m+1-th value is a function of the m-th


value 1 m
fi   fi 1  fi m11  Dx 2 Si 
m 1

2
 The iteration process finishes (convergence) if the maximum
error is less than the admissible error
max fi m 1  f i m  
 This method is slow
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Successive over-relaxation SOR method
 Poisson equation

d2 f f i 1  2 f i  fi 1
S   Si
dx 2
Dx 2

 Iterative process: the m+1-th value is a function of the m-th


value 1
 f   f i m1  f i m11  2 fi m  Dx 2 Si 
2
fi m 1  fi m   f  : relaxation factor around 1.7

 The iteration process finishes (convergence) if the maximum


error is less than the admissible error
max fi m 1  fi m  
 This method is faster
Property of Roberto Rojas Property of Roberto Rojas
Equations
Do not distribute orofuse
motion for permission
without incompressible
of the author
Newtonian fluid
Mass conservation   u  0 Continuity
Momentum u 1 1
 u  u   P    g
conservation t  
Viscous stress tensor for incompressible fluid
 u j ui 2   u j ui 
ij      ij   u   ij   u ij     
 xi x j 3   xi x j 
u 1  2
 u u   P   u  g Navier-Stokes
t   equations
u v w
  0 In Cartesian
x y z
coordinates
u u u u 1    2u 2u  2u 
u v w   P   2  2  2   g for u(u,v,w)
t x y z    x y z 
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Discretization of the fluid equations
jmax Pij+1 Pi+1j+1
j +1 ui-1j+1
vij uij+1 vi+1j
Dy
Pij Pi+1j
j j ui-1j
uij

vij-1 vi+1j-1
2 i i +1
1 Staggered mesh
1 2 i Dx imax

n 1

uijn 1  uin11j

vijn 1  vijn11
0

 u n
 u n
u n
ij  0
D u xn   ijn i 1nj
u  0
ij
Dx Dy ui 1 j  uij n
ij

uijn1  uijn n u
n
u n
y P n1
i 1 j  P n1
ij
 u n
 i1 j  2u n
ij  u n
i 1 j u n
ij 1  2u n
ij  u n
ij 1

 uij  vC
n
x
    
Dt Dx Dy Dx 
 Dx 2
Dy 2

Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Simplified Marker & Cell Method (SMAC)
uijn 1  uin11j vijn 1  vijn11
  0    un 1
Dx Dy
n1 n1
Dt Pi 1 j  P
uijn1   ij
 Fxijn Solution of parabolic
 Dx
equation
n1 n1
Dt P  P Dt n1 Dt n
n1
vij   ij 1 ij n1
 Fyij  u   P  F ,
n n

u   P  F n
 Dy  
Dt u  un1  u
u   P P  P n1
 P n

  un1    u    u  0    u    u
 Solution of
 2P    u Poissons’ equation
Dt
Pn1  Pn P Dt Update
n 1
 
u  u  u  u  P

Property of Roberto Rojas Property of Roberto Rojas
SimplifiedorMarker
Do not distribute & Cellpermission
use without Method (SMAC)
of the author
(continued)
Dt n
 Solution of parabolic equation u    P  F n


Dt Pi1 j  Pij 
n n n n
D t P P
uij    Fxijn , vij  
ij 1 ij
 Fyijn
 Dx  Dy
 uij  ui 1 j vij  vij 1
 Computation of divu Dij   0
Dx Dy

 Solution of Poisson’s equation  P    u 2

Dt
Pi 1 j  2Pij  Pi 1 j Pij 1  2Pij  Pij 1  
  Dij
Dx 2
Dy 2
Dt
Dt
 Update Pij  Pij Pij , u  u  P
n1 n n 1


Dt Pi1 j Pij Dt Pij 1 Pij
uij  uij 
n1
, vij  vij 
n1

 Dx  Dy
Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Boundary conditions
Velocity B.C. Pressure B.C.
No-slip wall Inflow Continuous outflow
v1j v2j v1j v2j normal
u1 j  0 u1 j  uin
j u1j
j
u1j
j
Pimax-1 j Pimax j
Pimax j  0
v1 j  v2 j v1 j  2vin  v2 j
1 2 1 2 imax-1 imax

Slip wall Continuous outflow Other B.C.


v1j v2j normal
u1 j  0 vimax-1j vimaxj
j
u1j
j
uimax-2j uimax-1j uimaxj
j P1 j  P2 j
v1 j  v2 j uimax j  uimax1 j
P1 j P2 j

1 2 imax-1 imax vimax j  vimax1 j 1 2


Property of Roberto Rojas Property of Roberto Rojas
Do not distribute or use without permission of the author
Example
No-slip B.C. u=v=0
jmax

Inflow Dy Outflow
u=uin Pout=0
=const
v=0
2

1
1 2 Dx imax

No-slip B.C. u=v=0

Anda mungkin juga menyukai