CFD Lecture 2
-
Upload
abdul-rahim -
Category
Documents
-
view
247 -
download
0
description
Transcript of CFD Lecture 2
-
ENGR90024 COMPUTATIONAL FLUID DYNAMICS
Lecture O02
Eulers method, programming and the Unix environment!
!
-
d
dt= f(t,)
tmin t tmax(tmin) = min
Initial Value Problem
Mathemat
ical Method
sExact solution
Approximate solution
Numerical/Computer Methods
Compare
= 1 et
0 1 2 3 4 5 6 7 80
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
t
(t)
Lecture O01
Started in Lecture O01 !will finish it in Lecture O02
-
d
dt= f(t,)
tmin t tmax(tmin) = min
Initial Value Problem Approximate solution
Numerical/Computer Methods
Started in Lecture O01 !will finish it in Lecture O02
-
Explicit Euler Method (section 1.2.1 of the printed lecture notes)To derive Eulers method, start with Eq. (O01.7)
(tl+1) = (tl) +t
1!
d
dt(tl)
d
dt= f(t,)
tmin t tmax(tmin) = min
Initial Value ProblemApproximate solution
Numerical/Computer Methods
If we truncate Taylors series, Eq. (O01.7), and ignore terms of the order of t2, we will get the following approximation for the value of (tl+1) (remember that tl+1=tl+),
(tl+1) = (tl) +td
dt(tl)
(O02.1)(tl +t) = (tl) +td
dt(tl)
-
Substituting Eq. (O02.2) into Eq. (O02.1)
where l+1 is the approximate value of (tl+1) and l is the approximate value of (tl).The formula above is called the explicit Eulers method and it can be used to approximate the solution at time level tl+1 when you know the (approximate) solution at time level tl.
l+1 = l +tf(l, tl) (O02.3)
Note that the above expression has error O(t2). Recall that ODEs are typically expressed as
d
dt(t) = f(, t) (O02.2)
to obtainf(, t)(tl +t) = (tl) +t
-
d
dt= f(l, tl)
(t)
t
tl tl+1
ErrorPredicted value of l+1
True value of l+1
Smaller t will lead to smaller error
l+1 = l +tf(l, tl)
(t)(tl+1)
(tl)Eulers formula
-
(t)
t
tl tl+1
ErrorPredicted value of l+1
True value of l+1
Smaller t will lead to smaller error
l+1 = l +tf(l, tl)
d
dt= f(l, tl)
(t)(tl+1)
(tl)Eulers formula
-
(tl+1)
(t)
t
tl tl+1
l
ErrorPredicted value of l+1
True value of l+1
Smaller t will lead to smaller error
l+1 = l +tf(l, tl)
d
dt= f(l, tl)
(t)
Eulers formula
-
(t1,1)
(t2, 2)
(t3, 3)
(t4, 4)
(t5, 5)
(t)
d(t)
dtt
d(t)
dtt d(t)
dtt
d(t)
dtt
min
t1=tmin t2 t3 t4 t5=tmax
t t t t
Predicted (t)
-
Example O02.1 (similar to Example 1.1 of the printed lecture notes): !Using Eulers method, solve !!!!!For 0 < t < 8 with (t=0)=0 and! a) t=2! b) t=1! c) t=0.5! d) t=0.1
d
dt= 1
-
(t1,1)
(t2, 2)
(t3, 3)
(t4, 4)
(t5, 5)
(t)
d(t)
dtt
d(t)
dtt d(t)
dtt
d(t)
dtt
min
t1=tmin t t t t5=tmax
t t t t
function MPO02p1() clear all; close all; Delta_t=2.0; t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end plot(t,phi,'ko-') hold on ezplot(@(t)1-exp(-t),[0,8,0,2]) xlabel('t'); ylabel('\phi'); legend('Euler','True');
-
function MPO02p1() clear all; close all; Delta_t=2.0; t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end plot(t,phi,'ko-') hold on ezplot(@(t)1-exp(-t),[0,8,0,2]) xlabel('t'); ylabel('\phi'); legend('Euler','True'); !!!!
0 1 2 3 4 5 6 7 80
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
t
1exp(t)
EulerTrue
Output
-
0 1 2 3 4 5 6 7 80
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
t
EulerTrue
0 1 2 3 4 5 6 7 80
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
t
1exp(t)
EulerTrue
0 1 2 3 4 5 6 7 80
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
t
EulerTrue
0 1 2 3 4 5 6 7 80
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
t
EulerTrue
t=2.0 t=1.0
t=0.5 t=0.1
-
End of Example O02.1
-
d
dt= f(t,)
tmin t tmax(tmin) = min
Initial Value Problem
Mathemat
ical Method
sExact solution
Approximate solution
Numerical/Computer Methods
Compare
= 1 et
0 1 2 3 4 5 6 7 80
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
t
(t)
0 1 2 3 4 5 6 7 80
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
t
EulerTrue
-
However, we have to take O(t-1) steps to find the solution attime tNt.A conservative position would to assume that theerror accumulate at every time stepENtglobal =
(tNt) Nt =
(t0) 0 +tf(0, t0) +tf(1, t1) + . . .++tf(Nt1, tNt1)
Euler method, Eq. (O02.3) has local truncation error O(t2).This means that if you halve t, you can expect the error toreduce by a quarter.
Local & Global Truncation Error (page 6 printed notes)
Hence, the global truncation error is O(t). In general, if amethod has local truncation error of O(tN), then the globaltruncation error is O(tN-1).
(tl+1) = (tl) +t
1!
d
dt(tl)
-
function MPO02p1() clear all; close all; Delta_t=2.0; t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end plot(t,phi,'ko-') hold on ezplot(@(t)1-exp(-t),[0,8,0,2]) xlabel('t'); ylabel('\phi'); legend('Euler','True'); !!!!
This is a very inefficient MATLAB program!!
for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
-
How to make the our Matlab program run faster : Memory Allocation (see Example 1.1 page 7 of printed lecture notes)In Matlab program MPO02p1.m, a new block of memory is allocated to the variable phi() at every single iteration of the for loop
for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
This is very inefficient because Matlab has to find a new block ofmemory for phi() every time it goes into the for loop. It then has tocopy the data to this new block. This is very inefficient.
phi=zeros(1,100);
This command allocates memory to 100 elements in the phi()vector (1 x 100) and all elements have a zero value.
It is much more efficient to preallocate memory to phi() before you use itin the for loop. You can preallocate memory and zero every element inthe vector by issuing the command
-
!t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
!t=0:Delta_t:8.0 phi=zeros(1,length(t)); for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
Without preallocation of memory
With preallocation of memory
-
!t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
phi= 0
!t=0:Delta_t:8.0 phi=zeros(1,length(t)); for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
Without preallocation of memory
With preallocation of memory
phi= 0 (2)
-
!t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
phi= 0 (2)
!t=0:Delta_t:8.0 phi=zeros(1,length(t)); for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
Without preallocation of memory
With preallocation of memory
-
!t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
phi= 0 (2)
!t=0:Delta_t:8.0 phi=zeros(1,length(t)); for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
Without preallocation of memory
With preallocation of memory
phi= 0 (2)(3)
-
!t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
!t=0:Delta_t:8.0 phi=zeros(1,length(t)); for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
Without preallocation of memory
With preallocation of memory
phi= (3)0 (2)
-
!t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
!t=0:Delta_t:8.0 phi=zeros(1,length(t)); for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
Without preallocation of memory
With preallocation of memory
phi= (3)0 (2)
-
!t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
!t=0:Delta_t:8.0 phi=zeros(1,length(t)); for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
Without preallocation of memory
With preallocation of memory
phi=
(3)
0 (2)
phi= (4)0 (2)
(3)
-
!t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
!t=0:Delta_t:8.0 phi=zeros(1,length(t)); for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
Without preallocation of memory
With preallocation of memory
phi= (3)0 (2) (4)
-
!t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
!t=0:Delta_t:8.0 phi=zeros(1,length(t)); for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
Without preallocation of memory
With preallocation of memory
phi= (3)0 (2) (4)
-
!t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
!t=0:Delta_t:8.0 phi=zeros(1,length(t)); for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
Without preallocation of memory
With preallocation of memory
phi= 0 (2)
phi= (5)0 (2)(3) (4)
(3) (4)
-
!t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
phi= 0 (2)(3)(4)(5)
!t=0:Delta_t:8.0 phi=zeros(1,length(t)); for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
Without preallocation of memory
With preallocation of memory
-
!t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
phi= 0 (2)(3)(4)(5)
!t=0:Delta_t:8.0 phi=zeros(1,length(t)); for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
phi= 0 0 0 0 0
Without preallocation of memory
With preallocation of memory
-
!t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
phi= 0 (2)(3)(4)(5)
!t=0:Delta_t:8.0 phi=zeros(1,length(t)); for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
phi= 0 0 0 0(2)
Without preallocation of memory
With preallocation of memory
-
!t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
phi= 0 (2)(3)(4)(5)
!t=0:Delta_t:8.0 phi=zeros(1,length(t)); for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
phi= 0 0 0(2)(3)
Without preallocation of memory
With preallocation of memory
-
!t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
phi= 0 (2)(3)(4)(5)
!t=0:Delta_t:8.0 phi=zeros(1,length(t)); for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
phi= 0 0(2)(3)(4)
Without preallocation of memory
With preallocation of memory
-
!t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
phi= 0 (2)(3)(4)(5)
!t=0:Delta_t:8.0 phi=zeros(1,length(t)); for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
phi= 0 (2)(3)(4)(5)
Without preallocation of memory
With preallocation of memory
-
!t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
phi= 0 (2)(3)(4)(5)
!t=0:Delta_t:8.0 phi=zeros(1,length(t)); for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end
phi= 0 (2)(3)(4)(5)
Without preallocation of memory
With preallocation of memory
-
CASE STUDY O02.2 (similar to Example 1.1 of the printed lecture notes): !Execute MPO02p1.m for !(I) t=1.0e-4 !(II)t=1.0e-5 !(III)t=1.0e-6!(IV)t=1.0e-7 !!For each value of t, and how much time it takes to run. !!Preallocate memory to phi() and run the same program again. See how long it now takes to execute. !!Use the tic() and toc() function to record the time it takes to execute a certain command in Matlab.
-
function MPO02p2() clear all; close all; Delta_t=1.0e-4; %Delta_t=1.0e-5; %Delta_t=1.0e-6; %Delta_t=1.0e-7; t=0:Delta_t:10.0; % preallocating memory & set elements to zero %phi=zeros(1,numel(t)); phi(1)=0.0; tic % start timer for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)); end temp=toc; %end timer fprintf(1,'It takes %e seconds to execute the Matlab program\n',temp);
-
WITHOUT preallocation of
memory
WITH preallocation of
memory
t=1.0e-4 3.51e-2s 2.23e-03s
t=1.0e-5 3.58e-1s 2.24E-02s
t=1.0e-6 3.81s 2.48e-1s
t=1.0e-7 141.1s 25.9s
Time it takes to execute MPO02p2.m with and without preallocation of the array phi() for different t
-
End of Case Study O02.2
-
Unix Programming Environment
-
ENGR90024 COMPUTATIONAL FLUID DYNAMICS
Lecture O04
Ordinary Differential Equations (ODEs):!
C Programming & the Unix Environment!
!
Example O02.3: !Write a C++ program that uses Eulers method to solve !!!!!For 0 < t < 8 with (t=0)=0 and a) t=2 b) t=1 c) t=0.5 d) t=0.1
d
dt= 1
-
In the Unix environment, most tasks are conducted from the command line. !
Use the mkdir command to create a directory, O02p3, for your files!
mkdir O02p3
Check that the directory is there by using the ls command.!ls !O02p3
Use the cd command to change directory to O02p3!cd O02p3
-
Edit a file called CPO02p3.cpp with the gedit program by typing !
gedit CPO02p3.cpp Type in the following C++ program (looks very similar to the
Matlab program MPO02p1.m)
- #include #include #include #include #include !using namespace std; !int main(int argc, char **argv) { /* Simulation parameters */ float t_min = 0.0; float t_max = 10.0; float Delta_t = 0.5; float phi_min = 0.0; int N_t = (t_max-t_min)/Delta_t; int l = 0; fstream OutputFile; /* Allocate arrays */ float t[N_t+1]; float phi[N_t+1]; float phi_analytical[N_t+1]; /* NOTE: Here we are using 'static allocation' to store the arrays. */ /* Set initial conditions */ t[0] = t_min; phi[0] = phi_min; phi_analytical[0] = phi_min; /* Main time marching loop */ for(l=0; l
-
function MPO02p1() clear all; close all; Delta_t=0.1; t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end plot(t,phi,'ko-') hold on ezplot(@(t)1-exp(-t),[0,8,0,2]) xlabel('t'); ylabel('\phi'); legend('Euler','True');
#include #include #include #include #include using namespace std; int main(int argc, char **argv) { /* Simulation parameters */ float t_min = 0.0; float t_max = 10.0; float Delta_t = 0.5; float phi_min = 0.0; int N_t = (t_max-t_min)/Delta_t; int l = 0; fstream OutputFile; /* Allocate arrays */ float t[N_t+1]; float phi[N_t+1]; float phi_analytical[N_t+1]; /* NOTE: Here we are using 'static allocation' to store the arrays. */ /* Set initial conditions */ t[0] = t_min; phi[0] = phi_min; phi_analytical[0] = phi_min; /* Main time marching loop */ for(l=0; l
-
function MPO02p1() clear all; close all; Delta_t=0.1; t=0:Delta_t:8.0 phi(1)=0.0; for n=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(n)) end plot(t,phi,'ko-') hold on ezplot(@(t)1-exp(-t),[0,8,0,2]) xlabel('t'); ylabel('\phi'); legend('Euler','True');
#include #include #include #include #include using namespace std; int main(int argc, char **argv) { /* Simulation parameters */ float t_min = 0.0; float t_max = 10.0; float Delta_t = 0.5; float phi_min = 0.0; int N_t = (t_max-t_min)/Delta_t; int l = 0; fstream OutputFile; /* Allocate arrays */ float t[N_t+1]; float phi[N_t+1]; float phi_analytical[N_t+1]; /* NOTE: Here we are using 'static allocation' to store the arrays. */ /* Set initial conditions */ t[0] = t_min; phi[0] = phi_min; phi_analytical[0] = phi_min; /* Main time marching loop */ for(l=0; l
-
compile the C++ program by typing the command!
g++ -o CPO02p3 CPO02p3.cpp
Run/execute the program by typing!./CPO02p3
After you execute the program, you should find that you have the file !
CPO02p3.out in that directory
-
Check this by typing the ls command!
ls !CPO02p3 CPO02p3.cpp CPO02p3.out !
Execute the Matlab program CPO02p3_Postprocessing.m to plot the the results (see next slide).
-
%%%%%%%%%%%%%%%%%%%%%%%%%%% % CPO02p3_Postprocessing.m %%%%%%%%%%%%%%%%%%%%%%%%%%% close all, clear all, clc; load 'CPO02p3.out' [N_t N_e] = size(CPO02p3); % Plot the solution plot(CPO02p3(:,1), CPO02p3(:,2), '.-', CPO02p3(:,1), CPO02p3(:,3), '-', 'LineWidth', 2, 'MarkerSize', 20); axis([0 10 0 2]); grid on; xlabel('t'); ylabel('\phi'); !
-
0 1 2 3 4 5 6 7 8 9 100
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
t
-
CPO02p3.cpp
g++
CPO02p3
Flow diagram
CPO02p3.out
0 1 2 3 4 5 6 7 8 9 100
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
t
CPO02p3_Postprocessing.m
-
End of Example O02.3