1
Implementing the SIMPLE Algorithm to the Lid Driven Cavity and
Square Elbow Flow Problems
Rob Morien, Ali Bakhshinejad
UWM Department of Mechanical Engineering
ME723 – CFD
December 18, 2015
Final Project
Abstract
The lid driven cavity flow problem is commonly used as an assessment tool for verification of
solution accuracy during testing of two dimensional flow problems within the various CFD
codes. This report analyzes the lid driven cavity flow problem using the SIMPLE algorithm for
multigrid development and Matlab code to generate the results. Once the Matlab code for the
multigrid has been constructed, it is then used to solve a square elbow flow problem. The
Matlab results for the square elbow flow problem are then confirmed with Fluent CFD analysis.
Introduction
The geometry of the domain for the lid driven flow problem is a square box in the x-y plane with
all sides having equal dimensions as shown in figure 1. The two sides and bottom are rigidly
attached to ground with no-slip conditions and the top lid moves tangentially with unit velocity.
Figure 1, Lid Driven Cavity Flow Problem Geometry
2
The geometry of the domain for the square elbow flow problem is a square box in the x-y plane
with all sides having equal dimensions as shown in figure 1. The two corner pieces are rigidly
attached to ground with no-slip conditions and the inlet and outlet openings have length L/2.
Figure 2, Square Elbow Flow Problem
Assuming an incompressible fluid and an initial guess of zero pressure at the bottom left corner
of the cavity, we seek to determine the steady state velocity and pressure distribution inside of
the cavities.
Methodology
Using the assumptions and boundary conditions described in the introduction, we derive the
multigrid domain from the two-dimensional steady incompressible Navier-Stokes equations.
The u and v momentum equations for the x and y directions are respectively:
    uu u p +S      V i
    vv v p +S      V j (1)
Considering the u momentum equation first, we integrate with respect to the control volume to
get a finite volume formulation:
    u
V
u u p +S dV        V i
Next, we apply the divergence theorem:
      u
A A A
u d u d pd +S dV        V A A A i
Let
u u   J V
3
Substituting, we get:
  u
A A
d pd +S dV    J A A i
(2)
In order to prevent checker boarding of the velocity field during simulation, we will use a
staggered grid approach during the discretization of eq. (2) as shown in figure 1.
Figure 3, Staggered Grid for the u Momentum Equation
Considering the LHS of eq. (2) first,
ˆ ˆ ˆ ˆf f E E n n P P s s
A
d           J A J A J A J A J A J A
Where the area terms in the staggered grid are:
E y A i
ˆn x A j
P y  A i
ˆs x  A j
Looking at the EJ term:
 E E E E
u u   J V
4
And so,
1
0
x
E E
yE E
uu
y u y
uv
 
     
                
J i
Which expands to:
  2
E E x E
y u u y     J i
Since the velocities are only on the staggered grid i.e. eu , eeu , etc., we interpolate to nodes using
central differencing:
2
ee e
E
u u
u


  ee e
x E
u u
u
x



Therefore:
2
2
ee e ee e
E
u u u u
y y
x
 
     
        
     
J i
(3)
Next, considering the ˆnJ term:
ˆ
ˆ ˆ
0
1
x
n n
yn n
uu
x u x
uv
 
     
                
J j
  ˆ ˆ ˆ ˆn n n y n
x v u u x     J j
Again, using central differencing for the velocities:
ˆ
2
nne e
n
u u
u


ˆ
2
n ne
n
v v
v


 ˆ
nne e
y n
u u
u
y



Therefore:
ˆ
2 2
nne e n ne nne e
n
u u v v u u
x x
y
 
      
              
J j
(4)
Next, considering the PJ term:
1
0
x
P P
yP P
uu
y u y
uv
 
     
                
J i
Which expands to:
5
  2
P P x P
y u u y      J i
Using the central differencing for the velocities:
2
e w
P
u u
u


  e w
x P
u u
u
x



Therefore:
2
2
e w e w
P
u u u u
y y
x
 
     
         
     
J i
(5)
Finally, considering the ˆsJ term:
ˆ ˆ
ˆ ˆ
0
1
x
s s
ys s
uu
x u x
uv
 
     
                
J j
Which expands to:
  ˆ ˆ ˆ ˆs s s y s
x v u u x      J j
Using the central differencing for the velocities:
ˆ
2
sse e
s
u u
u


ˆ
2
s se
s
v v
v


 ˆ
e sse
y s
u u
u
y



Therefore:
ˆ
2 2
sse e s se e sse
s
u u v v u u
x x
y
 
      
               
J j
(6)
Next, we consider the pressure term:
   ˆ ˆ ˆ ˆf f E E n n P P s s
A
pd p p p + p + p      A i A i A A A A i
   E P
A
pd p p y    A i
(7)
Finally, we write the discretized u momentum equation as:
0f f f f u e e nb nb u
nb
p S V a u a u b            J A A i
6
2
2
2
2 2
2
2 2
ee e ee e
nne e n ne nne e
e w e w
sse e s se e sse
u u u u
y
x
u u v v u u
x
y
u u u u
y
x
u u v v u u
x
y
 
 
 
 
     
     
     

                    
 
     
       
     
      
             
  0
uf f
f
E P u
S Vp
p p y S x y








      
 
 
 
 
 
  

A i
J A
2e
x y
a
y x

  
    
ee w
y
a a
x


  

nne sse
x
a a
y


  

    
    
 
2
2
4
ee e nne e n ne
u P E u
e w sse e s se
u u y u u v v x
b p p y S x y
u u y u u v v x
       
        
        
Next, considering the v momentum equation, we integrate with respect to the control volume to
get a finite volume formulation:
    v
V
v v P +S dV        V j
Next, we apply the divergence theorem:
      v
A A A
v d v d Pd +S dV        V A A A j
Let
v v   J V
Substituting, we get:
  v
A A
d Pd +S dV    J A A j
(8)
Figure 2 shows the staggered grid used for the v momentum equation.
7
Figure 4, Staggered Grid for the v Momentum Equation
Considering the LHS of eq. (8) first,
ˆ ˆ ˆ ˆf f N N w w P P e e
A
d           J A J A J A J A J A J A
Where the area terms in the staggered grid are:
N x A j
ˆw y  A i
P x  A j
ˆe y A i
Looking at the NJ term:
 N N N N
v v   J V
And so,
0
1
x
N N
yN N
vu
x v x
vv
 
     
                
J j
Which expands to:
  2
N N y N
x v v x     J j
Using central differencing to determine the velocities:
8
2
nn n
N
v v
v


  nn n
y N
v v
v
y



Therefore
2
2
nn n nn n
N
v v v v
x x
y
 
    
             
J j
(9)
Next, considering the ˆwJ term:
ˆ ˆ
ˆ ˆ
1
0
x
w w
yw w
vu
y v y
vv
 
     
                
J i
  ˆ ˆ ˆ ˆw w w x w
y u v v y      J i
Again, using central differencing for the velocities:
ˆ
2
nnw w
w
u u
u


ˆ
2
n nw
w
v v
v


 ˆ
n nw
x w
v v
v
x



Therefore:
ˆ
2 2
nnw w n nw n nw
w
u u v v v v
y y
x
 
       
               
J i
(10)
Next, considering the PJ term:
0
1
x
P P
yP P
vu
x v x
vv
 
     
                
J j
Which expands to:
  2
P P y P
x v v x      J j
Using the central differencing for the velocities:
2
n s
P
v v
v


  n s
y P
v v
v
y



Therefore:
9
2
2
n s n s
P
v v v v
x x
y
 
    
              
J j
(11)
Finally, considering the ˆeJ term:
ˆ ˆ
ˆ ˆ
1
0
x
e e
ye e
vu
y v y
vv
 
     
                
J i
Which expands to:
  ˆ ˆ ˆ ˆe e e x e
y u v v y     J i
Using the central differencing for the velocities:
ˆ
2
nne e
e
u u
u


ˆ
2
ne n
e
v v
v


 ˆ
ne n
x e
v v
v
x



Therefore:
ˆ
2 2
nne e ne n ne n
e
u u v v v v
y y
x
 
       
              
J i
(12)
Next, we consider the pressure term:
   ˆ ˆ ˆ ˆf f N N w w P P e e
A
pd p p p + p + p      A j A j A A A A j
   N P
A
pd p p x    A j
(13)
Finally, we write the discretized v momentum equation as:
0f f f f v n n nb nb v
nb
p S V a u a u b            J A A j
2
2
2
2 2
2
2 2
nn n nn n
nnw w n nw n nw
n s n s
nne e ne n ne n
v v v v
x
y
u u v v v v
y
x
v v v v
x
y
u u v v v v
y
x
 
 
 
 
    
          

                      
 
     
            
       
            
  0
vf f
f
N P v
S Vp
p p x S x y








      

 
 
 
 
 

A j
J A
10
2n
x y
a
y x

  
    
ne nw
y
a a
x


  

nn s
x
a a
y


  

    
    
 
2
2
4
nn n nnw w n nw
v P N v
n s nne e ne n
v v x u u v v y
b p p x S x y
v v x u u v v y
       
        
        
We now consider the continuity equation which is given by:
=0 V
Integrating with respect to volume:
V
dV = 0 V
And applying the divergence theorem we get:
  f f
A
   V A = V A
This form of the continuity equation may get discretized using the main cell (colored in green in
figure 1). Doing so gives:
       f f e w n se w n s
             V A V A V A V A V A
Which results in:
        0e w n su u x v v y               (14)
11
The SIMPLE Algorithm
The SIMPLE algorithm, short for semi-implicit method for pressure linked equations, is an
algorithm that links the u and v momentum equations and the continuity equation together.
The u momentum equation with corresponding velocity term that is desired to be solved for is
given by:
 e e nb nb e P Ea u a u b A p p    (15)
   
e
nb nb e nb nb
e P E e P E
e e e
d
a u b A a u b
u p p d p p
a a a
 
      
This formulation will provide the velocity at e provided that we know the pressure at the faces
of the cell. However, since the pressure is not commonly known for the general case, we must
make a guess for the pressure which we will call * *
*, ,P Ep p p , etc. Based on the choice for *p , we
then solve for *
*,u v , etc. from the momentum equations. Therefore with a guessed pressure, eq.
(15) becomes:
 * * * *
e e nb nb e P Ea u a u b A p p    (16)
Subtracting eq. (16) from eq. (15), we get:
       
' ' ' '
* * * *
e nb P E
e e e nb nb nb e P P e E E
u u p p
a u u a u u A p p A p p      
(17)
Where '
u and '
p are called the velocity and pressure correction respectively. In other words, the
dashed terms are the values necessary to bring the guessed term (starred terms) to the correct
value, or ' *
e eu u u  .
Upon inspection of eq. (17) we see that the velocity correction at point e is a combined effect of
the correction of velocity and pressure at the neighboring points. In the Simple algorithm, the
summation term in eq. (17) is generally omitted from the calculation since not doing so would
result in an implicit framework, hence the semi-implicit in the title of the algorithm. Therefore,
omitting the summation term, eq. (17) becomes:
12
 ' ' '
e e e P Ea u A p p 
 ' ' '
e e P Eu d p p 
Therefore, the updated velocity terms become:
* '
,e new e eu u u 
 * ' '
,e new e e P Eu u d p p  
(18)
In a similar manner,
 * ' '
,n new n n P Nv v d p p  
(19)
Where, equations 18 and 19 are called the velocity correction equations. Although we don’t yet
know what the pressure correction is, it should converge in the momentum equations in such a
way that the velocity field satisfies the continuity equation. Therefore, it is the continuity
equation which acts as the governing equation for the correction of pressure.
We then substitute the velocity correction equations into the discretized continuity equation
derived in eq. (14).
   
   
* ' ' * ' '
* ' ' * ' '
0
e e P E w w w P
n n P N s s s P
u d p p u d p p y
v d p p v d p p x


       
          (20)
Therefore, the pressure correction equation is:
' ' ' ' '
P P E E W W N N S Sa p a p a p a p a p b    
(21)
Where,
E ea d y 
W Wa d y 
N Na d x 
S Sa d x 
   * * * *
W E S Nb u u y v v x        
13
The following shows the steps taken in order to calculate the velocity and pressure:
Summary of the SIMPLE algorithm
1) Guess a *
p
2) Solve for momentum equations * *
,u v
3) Solve for the pressure correction equation ( 'p )
4) Using step 3, update velocity u, v
5) Correct *
'p p p 
Results
Figures 5 through 10 below show the velocity distributions for the lid driven cavity and square
elbow flow problems. Figure 11 shows the Fluent CFD results of the square elbow.
Figure 5, Velocity Vector Plot, U and V Velocities, Lid Driven Cavity
14
Figure 6, Velocity Surface Plot of U Velocity, Lid Driven Cavity
Figure 7, Velocity Surface Plot of V Velocity, Lid Driven Cavity
15
Figure 8, Velocity Vector Plot, U and V Velocities, Square Elbow
Figure 9, Velocity Surface Plot of U Velocity, Square Elbow
16
Figure 10, Velocity Surface Plot of V Velocity, Square Elbow
Figure 11, Fluent CFD Velocity Streamlines, Square Elbow
17
Conclusion
In this report we have developed a finite volume code for the lid driven cavity and square elbow
flow problems in Matlab. A simple ANSYS Fluent model was developed as well for comparison
purposes. In the results, we observed that because of the nature of the geometry, we will not
observe any flow circulation. At the end, our code still has some errors compared to the fluent
solver which probably can be resolved with using more refined mesh in our code.
Appendix
The following Matlab codes were implemented to generate the results in this report. The first
code runs the lid driven cavity flow problem and the second code runs the square elbow flow
problem.
Lid Driven Cavity Flow Problem:
clear all;
close all;
clc;
%Parameters
L=1; %side length of domain
U=5; %Upper plate velocity
N=21; %Gridsize x-direction
M=21; %Gridsize y-direction
dx=1/(N-1); %spatial step in x-direction
dy=1/(M-1); %spatial step in y-direction
mu = 0.25;
rho = 1;
%Indexing the domain (staggered grid) example 1:N
i_u=1:(N-1); %Index describing the u-nodes in the x-
direction (i)
j_u=1:M; %Index describing the u-nodes in the y-
18
direction (j)
i_v=1:N; %Index describing the v-nodes in the x-
direction (i)
j_v=1:(M-1); %Index describing the v-nodes in the y-
direction (j)
i_p=1:N; %Index describing the p-nodes in the x-
direction (i)
j_p=1:M; %Index describing the p-nodes in the y-
direction (j)
i_uip=2:(N-2); %Index of inner points for u-nodes
j_uip=2:(M-1);
i_vip=2:(N-1); %Index of inner points for v-nodes
j_vip=2:(M-2);
i_pip=2:(N-1); %Index of inner points for p-nodes
j_pip=2:(M-1);
%Initial values (inner points)
u(i_u,j_u)=0; %Velocity in the x-direction in the whole
domain
v(i_v,j_v)=0; %Velocity in the y-direction in the whole
domain
p(i_p,j_p)=0; %Pressure field in the whole domain
% Initual guess for u, v and p
u_star(1:(N-1),1:M)=0; u_star(:,:)=0; %Initial guess of u
v_star(1:N,1:(M-1))=0; v_star(:,:)=0; %Initial guess of v
p_star(1:N,1:M)=0; p_star(:,:)=0; %Initial guess of p
%Initial boundary conditions
u_star(i_u,M)=U; %Top Boundary velocity for u
u_star(i_u,M-1)=U; %Top Boundary velocity for u
u_star(i_u,1)=0; %Bottom Boundary velocity for u
%Left Boundary velocity for u
u_star(1,j_u)=0;
%Right Boundary velocity for u
u_star(N-1,j_u)=0;
%Top Boundary velocity for v
v_star(i_v,M-1)=0;
%Bottom Boundary velocity for v
v_star(i_v,1)=0;
v_star(1,j_v)=0; %Left Boundary velocity for v
v_star(N,j_v)=0; %Right Boundary velocity for v
%Bottom boundary pressure
19
p(i_p,1)=p(i_p,2); %p at wall is approx same as
p near wall
%Left boundary
p(1,j_p)=p(2,j_p); %p at wall is approx same as
p near wall
%Right boundary
p(N,j_p)=p(N-1,j_p); %p at wall is approx same as
p near wall
%Top boundary
p(i_p,M)=p(i_p,(M-1)); %p at wall is approx same
as p near wall
%----------------------------Loop starts-----------------
% Loop performing SIMPLE algorithm until convergence
% Use suitable loop to achieve converged results
u_corr(1:(N-1),1:M)=0; u_corr(:,:)=0;
v_corr(1:N,1:(M-1))=0; v_corr(:,:)=0;
p_corr(1:N,1:M)=0; p_corr(:,:)=0;
for i=2:N-1
for j=2:M-1
p_corr(i,j)=0.1;
end
end
q=0;
while (q<50) % Control convergence
q=q+1;
disp(['Iteration ', int2str(q)]);
% Solve momentum Equations with guessed pressures
u_new(1:(N-1),1:M)=0;
for i=2:(N-2)
for j=2:(M-2)
aue = mu * (max(0,(1- 0.1 * abs(rho * u_star(i+1,j)*
dx/mu))^5)) + max (-(rho * u_star(i+1,j)* dx),0);
auw = mu * (max(0,(1- 0.1 * abs(rho * u_star(i-1,j)*
dx/mu))^5)) + max ((rho * u_star(i-1,j)* dx),0);
20
aun = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j+1)*
dx/mu))^5)) + max (-(rho * u_star(i,j+1)* dx),0);
aus = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j-1)*
dx/mu))^5)) + max ((rho * u_star(i,j-1)* dx),0);
aup = aue + auw + aun + aus;
u_star(i,j)= (aue * u_star(i+1,j) + auw * u_star(i-
1,j) + aun * u_star(i,j+1) + aus * u_star(i,j-1) +
(dy*(p_star(i-1,j)-p_star(i,j))))/(aup);
end
end
u_star(1:(N-1),M)=U; %Top Boundary
velocity for u_new
u_star(1:(N-1),M-1)=U; %Top Boundary
velocity for u_new
u_star(1:(N-1),1)=0; %Bottom
Boundary velocity for u_new
for j=2:(M-1) %Left & Right
Boundary velocity for u_new
u_star(1,j)=(u(2,j))/3; %u velocity
near wall approx to one third of adjacent u node
u_star(N-1,j)=(u(N-2,j))/3;
end
v_star(1:N,1:(M-1))=0;
for i=2:(N-1)
for j=2:(M-2)
ave = mu * (max(0,(1- 0.1 * abs(rho * v_star(i+1,j)*
dx/mu))^5)) + max (-(rho * v_star(i+1,j)* dx),0);
avw = mu * (max(0,(1- 0.1 * abs(rho * v_star(i-1,j)*
dx/mu))^5)) + max ((rho * v_star(i-1,j)* dx),0);
avn = mu * (max(0,(1- 0.1 * abs(rho * v_star(i,j+1)*
dx/mu))^5)) + max (-(rho * v_star(i,j+1)* dx),0);
avs = mu * (max(0,(1- 0.1 * abs(rho * v_star(i,j-1)*
dx/mu))^5)) + max ((rho * v_star(i,j-1)* dx),0);
avp = ave + avw + avn + avs;
v_star(i,j)=(ave * v_star(i+1,j) + avw * v_star(i-
1,j) + avn * v_star(i,j+1) + avs * v_star(i,j-1) +
(dx*(p_star(i,j-1)- p_star(i,j))))/(avp);
end
end
v_star(1,1:(M-1))=0; %Left Boundary
21
velocity for v
v_star(N,1:(M-1))=0; %Right Boundary
velocity for v
for i=2:(N-1) %Top & Bottom
Boundary velocity for v
v_star(i,M-1)=0;%(v_star(i,M-2))/3; %v velocity
near top approx to one third of adjacent v node
v_star(i,1)=0;%(v_star(i,2))/3;
end
% Use new velocities to get the pressure correction term
% By solving the proper poisson equation (loop!!!)
p_cor3(1:N,1:M)=0;
for r=1:30
disp([' Inner Loop ', int2str(r),' of Loop
#',int2str(q) ]);
for i=2:(N-2)
for j=2:(M-2)
D(i,j)= (u_star(i,j)-u_star(i+1,j)+v_star(i,j)-
v_star(i,j+1))*rho*dx;
aue = mu * (max(0,(1- 0.1 * abs(rho *
u_star(i+1,j)* dx/mu))^5)) + max (-(rho * u_star(i+1,j)* dx),0);
auw = mu * (max(0,(1- 0.1 * abs(rho * u_star(i-
1,j)* dx/mu))^5)) + max ((rho * u_star(i-1,j)* dx),0);
aun = mu * (max(0,(1- 0.1 * abs(rho *
u_star(i,j+1)* dx/mu))^5)) + max (-(rho * u_star(i,j+1)* dx),0);
aus = mu * (max(0,(1- 0.1 * abs(rho *
u_star(i,j-1)* dx/mu))^5)) + max ((rho * u_star(i,j-1)* dx),0);
aup = aue + auw + aun + aus;
ave = mu * (max(0,(1- 0.1 * abs(rho *
v_star(i+1,j)* dx/mu))^5)) + max (-(rho * v_star(i+1,j)* dx),0);
avw = mu * (max(0,(1- 0.1 * abs(rho * v_star(i-
1,j)* dx/mu))^5)) + max ((rho * v_star(i-1,j)* dx),0);
avn = mu * (max(0,(1- 0.1 * abs(rho *
v_star(i,j+1)* dx/mu))^5)) + max (-(rho * v_star(i,j+1)* dx),0);
avs = mu * (max(0,(1- 0.1 * abs(rho *
v_star(i,j-1)* dx/mu))^5)) + max ((rho * v_star(i,j-1)* dx),0);
avp = ave + avw + avn + avs;
apw = rho * dx * dx / aup;
ape = rho * dx * dx / aue;
22
aps = rho * dx * dx / avp;
apn = rho * dx * dx / ave;
ap = ape + apw + apn + aps;
p_cor3(i,j)= (ape * p_corr(i+1,j) + apw *
p_corr(i-1,j) + apn * p_corr(i,j+1) + aps * p_corr(i,j-1) +
D(i,j)) / ap;
end
end
p_corr=p_cor3;
end
% The new guessed field of pressure and velocity
% Calculate Corrected pressure
for i=1:N
for j=1:M
p(i,j)=p_star(i,j)+p_corr(i,j);
end
end
%Check boundary conditions for p
p(i_p,1)=p(i_p,2); %Bottom Wall BC
p(1,j_p)=p(2,j_p); %Left Wall BC
p(N,j_p)=p(N-1,j_p); %Right Wall BC
p(i_p,M)=p(i_p,(M-1)); %Top BC
p_star=p;
% Calculate corrected velocities
for i=2:(N-2)
for j=2:(M-2)
aue = mu * (max(0,(1- 0.1 * abs(rho * u_star(i+1,j)*
dx/mu))^5)) + max (-(rho * u_star(i+1,j)* dx),0);
auw = mu * (max(0,(1- 0.1 * abs(rho * u_star(i-1,j)*
dx/mu))^5)) + max ((rho * u_star(i-1,j)* dx),0);
aun = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j+1)*
dx/mu))^5)) + max (-(rho * u_star(i,j+1)* dx),0);
aus = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j-1)*
dx/mu))^5)) + max ((rho * u_star(i,j-1)* dx),0);
aup = aue + auw + aun + aus;
due = dx / aup;
23
u_corr(i,j)= due * (p_corr(i-1,j)-p_corr(i,j));
u(i,j)=u_star(i,j)+u_corr(i,j);
% Check boundary conditions for u
u(1:(N-1),M)=U; %Top
Boundary velocity for u
u(1:(N-1),M-1)=U; %Top
Boundary velocity for u
u(1:(N-1),1)=0; %Bottom
Boundary velocity for u
for j_u=2:(M-1) %Left
Boundary velocity for u
u(1,j_u)=0;%(u(2,j_u))/3; %u
velocity near wall approx to one third of adjacent u node
end
for j_u=2:(M-1) %Right
Boundary velocity for u
u(N-1,j_u)=0;%(u(N-2,j_u))/3; %u
velocity near wall approx to one third of adjacent u node
end
end
end
u_star=u;
for i=2:(N-2)
for j=2:(M-2)
ave = mu * (max(0,(1- 0.1 * abs(rho *
v_star(i+1,j)* dx/mu))^5)) + max (-(rho * v_star(i+1,j)* dx),0);
avw = mu * (max(0,(1- 0.1 * abs(rho * v_star(i-
1,j)* dx/mu))^5)) + max ((rho * v_star(i-1,j)* dx),0);
avn = mu * (max(0,(1- 0.1 * abs(rho *
v_star(i,j+1)* dx/mu))^5)) + max (-(rho * v_star(i,j+1)* dx),0);
avs = mu * (max(0,(1- 0.1 * abs(rho *
v_star(i,j-1)* dx/mu))^5)) + max ((rho * v_star(i,j-1)* dx),0);
avp = ave + avw + avn + avs;
dvn = dx / avp;
v_corr(i,j)= dvn * (p_corr(i,j-1)-p_corr(i,j));
v(i,j)=v_star(i,j)+v_corr(i,j);
24
% Check boundary conditions for v
v(1,1:(M-1))=0; %Left
Boundary velocity for v
v(N,1:(M-1))=0; %Right
Boundary velocity for v
for i_v=2:(N-1) %Top
Boundary velocity for v
v(i_v,M-1)=0;%(v(i_v,M-2))/3; %v
velocity near top approx to one third of adjacent v node
end
for i_v=2:(N-1) %Bottom
Boundary velocity for v
v(i_v,1)=0;%(v(i_v,2))/3; %v
velocity near top approx to one third of adjacent v node
end
end
end
v_star=v;
end
%----------------------------Loop ends--------------------
% Calculate plotvariables on a non-staggered grid (pressure-
nodes)
for i=2:N-1
for j=2:M-1
u_plot(i,j)=0.5*(u_star(i-1,j)+u_star(i,j));
end
end
u_plot(2:(N-1),M)=U; %Top Boundary velocity for u
u_plot(2:(N-1),M-1)=U;
u_plot(1:(N-1),1)=0; %Bottom Boundary velocity
for u
u_plot(1,1:M)=0; %Left Boundary velocity for
u
u_plot(N,1:M)=0; %Right Boundary velocity for
u
u_plot=u_plot';
for i=2:N-1
25
for j=2:M-1
v_plot(i,j)=0.5*(v_star(i,j-1)+v_star(i,j));
end
end
v_plot(2:(N-1),M)=0; %Top Boundary velocity for u
v_plot(1:(N-1),1)=0; %Bottom Boundary velocity
for u
v_plot(1,1:M)=0; %Left Boundary velocity for
u
v_plot(N,1:M)=0; %Right Boundary velocity for
u
v_plot=v_plot';
for i=1:N
for j=1:M
p_plot(i,j)=p(i,j);
end
end
quiver(u_plot,v_plot)
%figure,contour(u,40)
%figure,contour(p,40)
figure,surf(u_plot)
figure,surf(v_plot)
Square Elbow Flow Problem:
clear all;
close all;
clc;
%Parameters
L=1; %side length of domain
U=5; %Upper plate velocity
N=21; %Gridsize x-direction
26
M=21; %Gridsize y-direction
dx=1/(N-1); %spatial step in x-direction
dy=1/(M-1); %spatial step in y-direction
mu = 0.25;
rho = 1;
%Indexing the domain (staggered grid) example 1:N
i_u=1:N; %Index describing the u-nodes in the x-direction
(i)
j_u=1:M; %Index describing the u-nodes in the y-
direction (j)
i_v=1:N; %Index describing the v-nodes in the x-
direction (i)
j_v=1:M; %Index describing the v-nodes in the y-direction
(j)
i_p=1:N; %Index describing the p-nodes in the x-
direction (i)
j_p=1:M; %Index describing the p-nodes in the y-
direction (j)
i_uip=2:(N-2); %Index of inner points for u-nodes
j_uip=2:(M-1);
i_vip=2:(N-1); %Index of inner points for v-nodes
j_vip=2:(M-2);
i_pip=2:(N-1); %Index of inner points for p-nodes
j_pip=2:(M-1);
%Initial values (inner points)
u(i_u,j_u)=0; %Velocity in the x-direction in the whole
domain
v(i_v,j_v)=0; %Velocity in the y-direction in the whole
domain
p(i_p,j_p)=0; %Pressurefield in the whole domain
% Initual guess for u, v and p
u_star(1:(N-1),1:M)=0; u_star(:,:)=0; %Initial guess of u
v_star(1:N,1:(M-1))=0; v_star(:,:)=0; %Initial guess of v
p_star(1:N,1:M)=0; p_star(:,:)=0; %Initial guess of p
%Initial boundary conditions
u_star(i_u,M)=0; %Top Boundary velocity for u
u_star(i_u,M-1)=0; %Top Boundary velocity for u
u_star(i_u,1)=0; %Bottom Boundary velocity for u
%Left Boundary velocity for u
u_star(1:10,j_u)=U;
27
%Right Boundary velocity for u
u_star(N-1,j_u)=0;
%Top Boundary velocity for v
v_star(10:N,M-1)=U;
v_star(10:N,M)=U;
v_star(1:9,M) = 0;
v_star(1:9,M-1)=0;
%Bottom Boundary velocity for v
v_star(i_v,1)=0;
v_star(1,j_v)=0; %Left Boundary velocity for v
v_star(N,j_v)=0; %Right Boundary velocity for v
%Bottom boundary pressure
p(i_p,1)=p(i_p,2); %p at wall is approx same as
p near wall
%Left boundary
p(1,j_p)=p(2,j_p); %p at wall is approx same as
p near wall
%Right boundary
p(N,j_p)=p(N-1,j_p); %p at wall is approx same as
p near wall
%Top boundary
p(i_p,M)=p(i_p,(M-1)); %p at wall is approx same
as p near wall
%----------------------------Loop starts-----------------
% Loop performing SIMPLE algorithm until convergence
% Use suitable loop to achieve converged results
u_corr(1:(N-1),1:M)=0; u_corr(:,:)=0;
v_corr(1:N,1:(M-1))=0; v_corr(:,:)=0;
p_corr(1:N,1:M)=0; p_corr(:,:)=0;
for i=2:N-1
for j=2:M-1
p_corr(i,j)=0.1;
end
end
q=0;
while (q<300) % Control convergence
q=q+1;
28
disp(['Iteration ', int2str(q)]);
% Solve momentum Equations with guessed pressures
u_new(1:(N-1),1:M)=0;
for i=2:(N-1)
for j=2:(M-1)
aue = mu * (max(0,(1- 0.1 * abs(rho * u_star(i+1,j)*
dx/mu))^5)) + max (-(rho * u_star(i+1,j)* dx),0);
auw = mu * (max(0,(1- 0.1 * abs(rho * u_star(i-1,j)*
dx/mu))^5)) + max ((rho * u_star(i-1,j)* dx),0);
aun = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j+1)*
dx/mu))^5)) + max (-(rho * u_star(i,j+1)* dx),0);
aus = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j-1)*
dx/mu))^5)) + max ((rho * u_star(i,j-1)* dx),0);
aup = aue + auw + aun + aus;
u_star(i,j)= (aue * u_star(i+1,j) + auw * u_star(i-
1,j) + aun * u_star(i,j+1) + aus * u_star(i,j-1) +
(dy*(p_star(i-1,j)-p_star(i,j))))/(aup);
end
end
u_star(10:(N-1),M)=0; %Top Boundary
velocity for u_new
u_star(10:(N-1),M-1)=0; %Top
Boundary velocity for u_new
u_star(1:(N-1),1)=0; %Bottom
Boundary velocity for u_new
for j=1:10 %Left & Right
Boundary velocity for u_new
% u_star(1,j)=(u(2,j))/3; %u velocity
near wall approx to one third of adjacent u node
% u_star(N-1,j)=(u(N-2,j))/3;
u_star(1,j)=U;
end
u_star(N-1,:) = 0;
v_star(1:N,1:(M-1))=0;
for i=2:(N-1)
for j=2:(M-1)
ave = mu * (max(0,(1- 0.1 * abs(rho * v_star(i+1,j)*
dx/mu))^5)) + max (-(rho * v_star(i+1,j)* dx),0);
29
avw = mu * (max(0,(1- 0.1 * abs(rho * v_star(i-1,j)*
dx/mu))^5)) + max ((rho * v_star(i-1,j)* dx),0);
avn = mu * (max(0,(1- 0.1 * abs(rho * v_star(i,j+1)*
dx/mu))^5)) + max (-(rho * v_star(i,j+1)* dx),0);
avs = mu * (max(0,(1- 0.1 * abs(rho * v_star(i,j-1)*
dx/mu))^5)) + max ((rho * v_star(i,j-1)* dx),0);
avp = ave + avw + avn + avs;
v_star(i,j)=(ave * v_star(i+1,j) + avw * v_star(i-
1,j) + avn * v_star(i,j+1) + avs * v_star(i,j-1) +
(dx*(p_star(i,j-1)- p_star(i,j))))/(avp);
end
end
v_star(1,1:10)=0; %Left Boundary
velocity for v
v_star(N,1:(M-1))=0; %Right Boundary
velocity for v
for i=10:21 %Top & Bottom
Boundary velocity for v
%v_star(i,M-1)=0;%(v_star(i,M-2))/3; %v
velocity near top approx to one third of adjacent v node
%v_star(i,1)=0;%(v_star(i,2))/3;
v_star(i,M-1)=U;
v_star(i,M)=U;
end
v_star(1:9,M-1)=0;
v_star(1:9,M)=0;
% Use new velocities to get the pressure correction term
% By solving the proper poisson equation (loop!!!)
p_cor3(1:N,1:M)=0;
for r=1:30
disp([' Inner Loop ', int2str(r),' of Loop
#',int2str(q) ]);
for i=2:(N-2)
for j=2:(M-1)
D(i,j)= (u_star(i,j)-u_star(i+1,j)+v_star(i,j)-
v_star(i,j+1))*rho*dx;
aue = mu * (max(0,(1- 0.1 * abs(rho *
u_star(i+1,j)* dx/mu))^5)) + max (-(rho * u_star(i+1,j)* dx),0);
30
auw = mu * (max(0,(1- 0.1 * abs(rho * u_star(i-
1,j)* dx/mu))^5)) + max ((rho * u_star(i-1,j)* dx),0);
aun = mu * (max(0,(1- 0.1 * abs(rho *
u_star(i,j+1)* dx/mu))^5)) + max (-(rho * u_star(i,j+1)* dx),0);
aus = mu * (max(0,(1- 0.1 * abs(rho *
u_star(i,j-1)* dx/mu))^5)) + max ((rho * u_star(i,j-1)* dx),0);
aup = aue + auw + aun + aus;
ave = mu * (max(0,(1- 0.1 * abs(rho *
v_star(i+1,j)* dx/mu))^5)) + max (-(rho * v_star(i+1,j)* dx),0);
avw = mu * (max(0,(1- 0.1 * abs(rho * v_star(i-
1,j)* dx/mu))^5)) + max ((rho * v_star(i-1,j)* dx),0);
avn = mu * (max(0,(1- 0.1 * abs(rho *
v_star(i,j+1)* dx/mu))^5)) + max (-(rho * v_star(i,j+1)* dx),0);
avs = mu * (max(0,(1- 0.1 * abs(rho *
v_star(i,j-1)* dx/mu))^5)) + max ((rho * v_star(i,j-1)* dx),0);
avp = ave + avw + avn + avs;
apw = rho * dx * dx / aup;
ape = rho * dx * dx / aue;
aps = rho * dx * dx / avp;
apn = rho * dx * dx / ave;
ap = ape + apw + apn + aps;
p_cor3(i,j)= (ape * p_corr(i+1,j) + apw *
p_corr(i-1,j) + apn * p_corr(i,j+1) + aps * p_corr(i,j-1) +
D(i,j)) / ap;
end
end
p_corr=p_cor3;
end
% The new guessed field of pressure and velocity
% Calculate Corrected pressure
for i=1:N
for j=1:M
p(i,j)=p_star(i,j)+p_corr(i,j);
end
end
%Check boundary conditions for p
p(i_p,1)=p(i_p,2); %Bottom Wall BC
p(1,j_p)=p(2,j_p); %Left Wall BC
p(N,j_p)=p(N-1,j_p); %Right Wall BC
31
p(i_p,M)=p(i_p,(M-1)); %Top BC
p_star=p;
% Calculate corrected velocities
for i=2:(N-2)
for j=2:(M-1)
aue = mu * (max(0,(1- 0.1 * abs(rho * u_star(i+1,j)*
dx/mu))^5)) + max (-(rho * u_star(i+1,j)* dx),0);
auw = mu * (max(0,(1- 0.1 * abs(rho * u_star(i-1,j)*
dx/mu))^5)) + max ((rho * u_star(i-1,j)* dx),0);
aun = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j+1)*
dx/mu))^5)) + max (-(rho * u_star(i,j+1)* dx),0);
aus = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j-1)*
dx/mu))^5)) + max ((rho * u_star(i,j-1)* dx),0);
aup = aue + auw + aun + aus;
due = dx / aup;
u_corr(i,j)= due * (p_corr(i-1,j)-p_corr(i,j));
u(i,j)=u_star(i,j)+u_corr(i,j);
% Check boundary conditions for u
u(10:(N-1),M)=0; %Top
Boundary velocity for u_new
u(10:(N-1),M-1)=0; %Top
Boundary velocity for u_new
u(1:(N-1),1)=0; %Bottom
Boundary velocity for u_new
for jj=1:10 %Left &
Right Boundary velocity for u_new
% u_star(1,j)=(u(2,j))/3; %u
velocity near wall approx to one third of adjacent u node
% u_star(N-1,j)=(u(N-2,j))/3;
u(1,jj)=U;
end
u(N-1,:) = 0;
end
end
u_star=u;
for i=2:(N-2)
for j=2:(M-1)
32
ave = mu * (max(0,(1- 0.1 * abs(rho *
v_star(i+1,j)* dx/mu))^5)) + max (-(rho * v_star(i+1,j)* dx),0);
avw = mu * (max(0,(1- 0.1 * abs(rho * v_star(i-
1,j)* dx/mu))^5)) + max ((rho * v_star(i-1,j)* dx),0);
avn = mu * (max(0,(1- 0.1 * abs(rho *
v_star(i,j+1)* dx/mu))^5)) + max (-(rho * v_star(i,j+1)* dx),0);
avs = mu * (max(0,(1- 0.1 * abs(rho *
v_star(i,j-1)* dx/mu))^5)) + max ((rho * v_star(i,j-1)* dx),0);
avp = ave + avw + avn + avs;
dvn = dx / avp;
v_corr(i,j)= dvn * (p_corr(i,j-1)-p_corr(i,j));
v(i,j)=v_star(i,j)+v_corr(i,j);
% Check boundary conditions for v
v(1,1:10)=0; %Left Boundary
velocity for v
v(N,1:(M-1))=0; %Right
Boundary velocity for v
for ii=10:21 %Top &
Bottom Boundary velocity for v
%v_star(i,M-1)=0;%(v_star(i,M-2))/3; %v
velocity near top approx to one third of adjacent v node
%v_star(i,1)=0;%(v_star(i,2))/3;
v(ii,M-1)=U;
v(ii,M)=U;
end
v(1:9,M-1)=0;
v(1:9,M)=0;
end
end
v_star=v;
end
%----------------------------Loop ends--------------------
% Calculate plotvariables on a non-staggered grid (pressure-
nodes)
%u_plot(1:N,1:M)=0; u_plot(:,:)=0;
for i=2:N-1
33
for j=2:M-1
u_plot(i,j)=0.5*(u_star(i-1,j)+u_star(i,j));
end
end
u_plot(10:(N-1),M)=0; %Top Boundary
velocity for u_new
u_plot(10:(N-1),M-1)=0; %Top Boundary
velocity for u_new
u_plot(1:(N-1),1)=0; %Bottom Boundary
velocity for u_new
u_plot(1,1:10)=U;
u_plot(N,:) = 0;
u_plot=u_plot';
for i=2:N-1
for j=2:M-1
v_plot(i,j)=0.5*(v_star(i,j-1)+v_star(i,j));
end
end
v_plot(1,1:10)=0; %Left Boundary
velocity for v
v_plot(N,1:(M-1))=0; %Right Boundary
velocity for v
v_plot(10:21,M-1)=U;
v_plot(10:21,M)=U;
v_plot(1:9,M-1)=0;
v_plot(1:9,M)=0;
v_plot=v_plot';
for i=1:N
for j=1:M
p_plot(i,j)=p(i,j);
end
end
quiver(u_plot,v_plot)
figure,surf(u_plot)
figure,surf(v_plot)

RJM-AB-CFD-Final-Project-Dec-18-2015

  • 1.
    1 Implementing the SIMPLEAlgorithm to the Lid Driven Cavity and Square Elbow Flow Problems Rob Morien, Ali Bakhshinejad UWM Department of Mechanical Engineering ME723 – CFD December 18, 2015 Final Project Abstract The lid driven cavity flow problem is commonly used as an assessment tool for verification of solution accuracy during testing of two dimensional flow problems within the various CFD codes. This report analyzes the lid driven cavity flow problem using the SIMPLE algorithm for multigrid development and Matlab code to generate the results. Once the Matlab code for the multigrid has been constructed, it is then used to solve a square elbow flow problem. The Matlab results for the square elbow flow problem are then confirmed with Fluent CFD analysis. Introduction The geometry of the domain for the lid driven flow problem is a square box in the x-y plane with all sides having equal dimensions as shown in figure 1. The two sides and bottom are rigidly attached to ground with no-slip conditions and the top lid moves tangentially with unit velocity. Figure 1, Lid Driven Cavity Flow Problem Geometry
  • 2.
    2 The geometry ofthe domain for the square elbow flow problem is a square box in the x-y plane with all sides having equal dimensions as shown in figure 1. The two corner pieces are rigidly attached to ground with no-slip conditions and the inlet and outlet openings have length L/2. Figure 2, Square Elbow Flow Problem Assuming an incompressible fluid and an initial guess of zero pressure at the bottom left corner of the cavity, we seek to determine the steady state velocity and pressure distribution inside of the cavities. Methodology Using the assumptions and boundary conditions described in the introduction, we derive the multigrid domain from the two-dimensional steady incompressible Navier-Stokes equations. The u and v momentum equations for the x and y directions are respectively:     uu u p +S      V i     vv v p +S      V j (1) Considering the u momentum equation first, we integrate with respect to the control volume to get a finite volume formulation:     u V u u p +S dV        V i Next, we apply the divergence theorem:       u A A A u d u d pd +S dV        V A A A i Let u u   J V
  • 3.
    3 Substituting, we get:  u A A d pd +S dV    J A A i (2) In order to prevent checker boarding of the velocity field during simulation, we will use a staggered grid approach during the discretization of eq. (2) as shown in figure 1. Figure 3, Staggered Grid for the u Momentum Equation Considering the LHS of eq. (2) first, ˆ ˆ ˆ ˆf f E E n n P P s s A d           J A J A J A J A J A J A Where the area terms in the staggered grid are: E y A i ˆn x A j P y  A i ˆs x  A j Looking at the EJ term:  E E E E u u   J V
  • 4.
    4 And so, 1 0 x E E yEE uu y u y uv                          J i Which expands to:   2 E E x E y u u y     J i Since the velocities are only on the staggered grid i.e. eu , eeu , etc., we interpolate to nodes using central differencing: 2 ee e E u u u     ee e x E u u u x    Therefore: 2 2 ee e ee e E u u u u y y x                        J i (3) Next, considering the ˆnJ term: ˆ ˆ ˆ 0 1 x n n yn n uu x u x uv                          J j   ˆ ˆ ˆ ˆn n n y n x v u u x     J j Again, using central differencing for the velocities: ˆ 2 nne e n u u u   ˆ 2 n ne n v v v    ˆ nne e y n u u u y    Therefore: ˆ 2 2 nne e n ne nne e n u u v v u u x x y                         J j (4) Next, considering the PJ term: 1 0 x P P yP P uu y u y uv                          J i Which expands to:
  • 5.
    5   2 PP x P y u u y      J i Using the central differencing for the velocities: 2 e w P u u u     e w x P u u u x    Therefore: 2 2 e w e w P u u u u y y x                         J i (5) Finally, considering the ˆsJ term: ˆ ˆ ˆ ˆ 0 1 x s s ys s uu x u x uv                          J j Which expands to:   ˆ ˆ ˆ ˆs s s y s x v u u x      J j Using the central differencing for the velocities: ˆ 2 sse e s u u u   ˆ 2 s se s v v v    ˆ e sse y s u u u y    Therefore: ˆ 2 2 sse e s se e sse s u u v v u u x x y                          J j (6) Next, we consider the pressure term:    ˆ ˆ ˆ ˆf f E E n n P P s s A pd p p p + p + p      A i A i A A A A i    E P A pd p p y    A i (7) Finally, we write the discretized u momentum equation as: 0f f f f u e e nb nb u nb p S V a u a u b            J A A i
  • 6.
    6 2 2 2 2 2 2 2 2 eee ee e nne e n ne nne e e w e w sse e s se e sse u u u u y x u u v v u u x y u u u u y x u u v v u u x y                                                                                              0 uf f f E P u S Vp p p y S x y                              A i J A 2e x y a y x          ee w y a a x       nne sse x a a y                   2 2 4 ee e nne e n ne u P E u e w sse e s se u u y u u v v x b p p y S x y u u y u u v v x                           Next, considering the v momentum equation, we integrate with respect to the control volume to get a finite volume formulation:     v V v v P +S dV        V j Next, we apply the divergence theorem:       v A A A v d v d Pd +S dV        V A A A j Let v v   J V Substituting, we get:   v A A d Pd +S dV    J A A j (8) Figure 2 shows the staggered grid used for the v momentum equation.
  • 7.
    7 Figure 4, StaggeredGrid for the v Momentum Equation Considering the LHS of eq. (8) first, ˆ ˆ ˆ ˆf f N N w w P P e e A d           J A J A J A J A J A J A Where the area terms in the staggered grid are: N x A j ˆw y  A i P x  A j ˆe y A i Looking at the NJ term:  N N N N v v   J V And so, 0 1 x N N yN N vu x v x vv                          J j Which expands to:   2 N N y N x v v x     J j Using central differencing to determine the velocities:
  • 8.
    8 2 nn n N v v v    nn n y N v v v y    Therefore 2 2 nn n nn n N v v v v x x y                      J j (9) Next, considering the ˆwJ term: ˆ ˆ ˆ ˆ 1 0 x w w yw w vu y v y vv                          J i   ˆ ˆ ˆ ˆw w w x w y u v v y      J i Again, using central differencing for the velocities: ˆ 2 nnw w w u u u   ˆ 2 n nw w v v v    ˆ n nw x w v v v x    Therefore: ˆ 2 2 nnw w n nw n nw w u u v v v v y y x                           J i (10) Next, considering the PJ term: 0 1 x P P yP P vu x v x vv                          J j Which expands to:   2 P P y P x v v x      J j Using the central differencing for the velocities: 2 n s P v v v     n s y P v v v y    Therefore:
  • 9.
    9 2 2 n s ns P v v v v x x y                       J j (11) Finally, considering the ˆeJ term: ˆ ˆ ˆ ˆ 1 0 x e e ye e vu y v y vv                          J i Which expands to:   ˆ ˆ ˆ ˆe e e x e y u v v y     J i Using the central differencing for the velocities: ˆ 2 nne e e u u u   ˆ 2 ne n e v v v    ˆ ne n x e v v v x    Therefore: ˆ 2 2 nne e ne n ne n e u u v v v v y y x                          J i (12) Next, we consider the pressure term:    ˆ ˆ ˆ ˆf f N N w w P P e e A pd p p p + p + p      A j A j A A A A j    N P A pd p p x    A j (13) Finally, we write the discretized v momentum equation as: 0f f f f v n n nb nb v nb p S V a u a u b            J A A j 2 2 2 2 2 2 2 2 nn n nn n nnw w n nw n nw n s n s nne e ne n ne n v v v v x y u u v v v v y x v v v v x y u u v v v v y x                                                                                             0 vf f f N P v S Vp p p x S x y                            A j J A
  • 10.
    10 2n x y a y x         ne nw y a a x       nn s x a a y                   2 2 4 nn n nnw w n nw v P N v n s nne e ne n v v x u u v v y b p p x S x y v v x u u v v y                           We now consider the continuity equation which is given by: =0 V Integrating with respect to volume: V dV = 0 V And applying the divergence theorem we get:   f f A    V A = V A This form of the continuity equation may get discretized using the main cell (colored in green in figure 1). Doing so gives:        f f e w n se w n s              V A V A V A V A V A Which results in:         0e w n su u x v v y               (14)
  • 11.
    11 The SIMPLE Algorithm TheSIMPLE algorithm, short for semi-implicit method for pressure linked equations, is an algorithm that links the u and v momentum equations and the continuity equation together. The u momentum equation with corresponding velocity term that is desired to be solved for is given by:  e e nb nb e P Ea u a u b A p p    (15)     e nb nb e nb nb e P E e P E e e e d a u b A a u b u p p d p p a a a          This formulation will provide the velocity at e provided that we know the pressure at the faces of the cell. However, since the pressure is not commonly known for the general case, we must make a guess for the pressure which we will call * * *, ,P Ep p p , etc. Based on the choice for *p , we then solve for * *,u v , etc. from the momentum equations. Therefore with a guessed pressure, eq. (15) becomes:  * * * * e e nb nb e P Ea u a u b A p p    (16) Subtracting eq. (16) from eq. (15), we get:         ' ' ' ' * * * * e nb P E e e e nb nb nb e P P e E E u u p p a u u a u u A p p A p p       (17) Where ' u and ' p are called the velocity and pressure correction respectively. In other words, the dashed terms are the values necessary to bring the guessed term (starred terms) to the correct value, or ' * e eu u u  . Upon inspection of eq. (17) we see that the velocity correction at point e is a combined effect of the correction of velocity and pressure at the neighboring points. In the Simple algorithm, the summation term in eq. (17) is generally omitted from the calculation since not doing so would result in an implicit framework, hence the semi-implicit in the title of the algorithm. Therefore, omitting the summation term, eq. (17) becomes:
  • 12.
    12  ' '' e e e P Ea u A p p   ' ' ' e e P Eu d p p  Therefore, the updated velocity terms become: * ' ,e new e eu u u   * ' ' ,e new e e P Eu u d p p   (18) In a similar manner,  * ' ' ,n new n n P Nv v d p p   (19) Where, equations 18 and 19 are called the velocity correction equations. Although we don’t yet know what the pressure correction is, it should converge in the momentum equations in such a way that the velocity field satisfies the continuity equation. Therefore, it is the continuity equation which acts as the governing equation for the correction of pressure. We then substitute the velocity correction equations into the discretized continuity equation derived in eq. (14).         * ' ' * ' ' * ' ' * ' ' 0 e e P E w w w P n n P N s s s P u d p p u d p p y v d p p v d p p x                     (20) Therefore, the pressure correction equation is: ' ' ' ' ' P P E E W W N N S Sa p a p a p a p a p b     (21) Where, E ea d y  W Wa d y  N Na d x  S Sa d x     * * * * W E S Nb u u y v v x        
  • 13.
    13 The following showsthe steps taken in order to calculate the velocity and pressure: Summary of the SIMPLE algorithm 1) Guess a * p 2) Solve for momentum equations * * ,u v 3) Solve for the pressure correction equation ( 'p ) 4) Using step 3, update velocity u, v 5) Correct * 'p p p  Results Figures 5 through 10 below show the velocity distributions for the lid driven cavity and square elbow flow problems. Figure 11 shows the Fluent CFD results of the square elbow. Figure 5, Velocity Vector Plot, U and V Velocities, Lid Driven Cavity
  • 14.
    14 Figure 6, VelocitySurface Plot of U Velocity, Lid Driven Cavity Figure 7, Velocity Surface Plot of V Velocity, Lid Driven Cavity
  • 15.
    15 Figure 8, VelocityVector Plot, U and V Velocities, Square Elbow Figure 9, Velocity Surface Plot of U Velocity, Square Elbow
  • 16.
    16 Figure 10, VelocitySurface Plot of V Velocity, Square Elbow Figure 11, Fluent CFD Velocity Streamlines, Square Elbow
  • 17.
    17 Conclusion In this reportwe have developed a finite volume code for the lid driven cavity and square elbow flow problems in Matlab. A simple ANSYS Fluent model was developed as well for comparison purposes. In the results, we observed that because of the nature of the geometry, we will not observe any flow circulation. At the end, our code still has some errors compared to the fluent solver which probably can be resolved with using more refined mesh in our code. Appendix The following Matlab codes were implemented to generate the results in this report. The first code runs the lid driven cavity flow problem and the second code runs the square elbow flow problem. Lid Driven Cavity Flow Problem: clear all; close all; clc; %Parameters L=1; %side length of domain U=5; %Upper plate velocity N=21; %Gridsize x-direction M=21; %Gridsize y-direction dx=1/(N-1); %spatial step in x-direction dy=1/(M-1); %spatial step in y-direction mu = 0.25; rho = 1; %Indexing the domain (staggered grid) example 1:N i_u=1:(N-1); %Index describing the u-nodes in the x- direction (i) j_u=1:M; %Index describing the u-nodes in the y-
  • 18.
    18 direction (j) i_v=1:N; %Indexdescribing the v-nodes in the x- direction (i) j_v=1:(M-1); %Index describing the v-nodes in the y- direction (j) i_p=1:N; %Index describing the p-nodes in the x- direction (i) j_p=1:M; %Index describing the p-nodes in the y- direction (j) i_uip=2:(N-2); %Index of inner points for u-nodes j_uip=2:(M-1); i_vip=2:(N-1); %Index of inner points for v-nodes j_vip=2:(M-2); i_pip=2:(N-1); %Index of inner points for p-nodes j_pip=2:(M-1); %Initial values (inner points) u(i_u,j_u)=0; %Velocity in the x-direction in the whole domain v(i_v,j_v)=0; %Velocity in the y-direction in the whole domain p(i_p,j_p)=0; %Pressure field in the whole domain % Initual guess for u, v and p u_star(1:(N-1),1:M)=0; u_star(:,:)=0; %Initial guess of u v_star(1:N,1:(M-1))=0; v_star(:,:)=0; %Initial guess of v p_star(1:N,1:M)=0; p_star(:,:)=0; %Initial guess of p %Initial boundary conditions u_star(i_u,M)=U; %Top Boundary velocity for u u_star(i_u,M-1)=U; %Top Boundary velocity for u u_star(i_u,1)=0; %Bottom Boundary velocity for u %Left Boundary velocity for u u_star(1,j_u)=0; %Right Boundary velocity for u u_star(N-1,j_u)=0; %Top Boundary velocity for v v_star(i_v,M-1)=0; %Bottom Boundary velocity for v v_star(i_v,1)=0; v_star(1,j_v)=0; %Left Boundary velocity for v v_star(N,j_v)=0; %Right Boundary velocity for v %Bottom boundary pressure
  • 19.
    19 p(i_p,1)=p(i_p,2); %p atwall is approx same as p near wall %Left boundary p(1,j_p)=p(2,j_p); %p at wall is approx same as p near wall %Right boundary p(N,j_p)=p(N-1,j_p); %p at wall is approx same as p near wall %Top boundary p(i_p,M)=p(i_p,(M-1)); %p at wall is approx same as p near wall %----------------------------Loop starts----------------- % Loop performing SIMPLE algorithm until convergence % Use suitable loop to achieve converged results u_corr(1:(N-1),1:M)=0; u_corr(:,:)=0; v_corr(1:N,1:(M-1))=0; v_corr(:,:)=0; p_corr(1:N,1:M)=0; p_corr(:,:)=0; for i=2:N-1 for j=2:M-1 p_corr(i,j)=0.1; end end q=0; while (q<50) % Control convergence q=q+1; disp(['Iteration ', int2str(q)]); % Solve momentum Equations with guessed pressures u_new(1:(N-1),1:M)=0; for i=2:(N-2) for j=2:(M-2) aue = mu * (max(0,(1- 0.1 * abs(rho * u_star(i+1,j)* dx/mu))^5)) + max (-(rho * u_star(i+1,j)* dx),0); auw = mu * (max(0,(1- 0.1 * abs(rho * u_star(i-1,j)* dx/mu))^5)) + max ((rho * u_star(i-1,j)* dx),0);
  • 20.
    20 aun = mu* (max(0,(1- 0.1 * abs(rho * u_star(i,j+1)* dx/mu))^5)) + max (-(rho * u_star(i,j+1)* dx),0); aus = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j-1)* dx/mu))^5)) + max ((rho * u_star(i,j-1)* dx),0); aup = aue + auw + aun + aus; u_star(i,j)= (aue * u_star(i+1,j) + auw * u_star(i- 1,j) + aun * u_star(i,j+1) + aus * u_star(i,j-1) + (dy*(p_star(i-1,j)-p_star(i,j))))/(aup); end end u_star(1:(N-1),M)=U; %Top Boundary velocity for u_new u_star(1:(N-1),M-1)=U; %Top Boundary velocity for u_new u_star(1:(N-1),1)=0; %Bottom Boundary velocity for u_new for j=2:(M-1) %Left & Right Boundary velocity for u_new u_star(1,j)=(u(2,j))/3; %u velocity near wall approx to one third of adjacent u node u_star(N-1,j)=(u(N-2,j))/3; end v_star(1:N,1:(M-1))=0; for i=2:(N-1) for j=2:(M-2) ave = mu * (max(0,(1- 0.1 * abs(rho * v_star(i+1,j)* dx/mu))^5)) + max (-(rho * v_star(i+1,j)* dx),0); avw = mu * (max(0,(1- 0.1 * abs(rho * v_star(i-1,j)* dx/mu))^5)) + max ((rho * v_star(i-1,j)* dx),0); avn = mu * (max(0,(1- 0.1 * abs(rho * v_star(i,j+1)* dx/mu))^5)) + max (-(rho * v_star(i,j+1)* dx),0); avs = mu * (max(0,(1- 0.1 * abs(rho * v_star(i,j-1)* dx/mu))^5)) + max ((rho * v_star(i,j-1)* dx),0); avp = ave + avw + avn + avs; v_star(i,j)=(ave * v_star(i+1,j) + avw * v_star(i- 1,j) + avn * v_star(i,j+1) + avs * v_star(i,j-1) + (dx*(p_star(i,j-1)- p_star(i,j))))/(avp); end end v_star(1,1:(M-1))=0; %Left Boundary
  • 21.
    21 velocity for v v_star(N,1:(M-1))=0;%Right Boundary velocity for v for i=2:(N-1) %Top & Bottom Boundary velocity for v v_star(i,M-1)=0;%(v_star(i,M-2))/3; %v velocity near top approx to one third of adjacent v node v_star(i,1)=0;%(v_star(i,2))/3; end % Use new velocities to get the pressure correction term % By solving the proper poisson equation (loop!!!) p_cor3(1:N,1:M)=0; for r=1:30 disp([' Inner Loop ', int2str(r),' of Loop #',int2str(q) ]); for i=2:(N-2) for j=2:(M-2) D(i,j)= (u_star(i,j)-u_star(i+1,j)+v_star(i,j)- v_star(i,j+1))*rho*dx; aue = mu * (max(0,(1- 0.1 * abs(rho * u_star(i+1,j)* dx/mu))^5)) + max (-(rho * u_star(i+1,j)* dx),0); auw = mu * (max(0,(1- 0.1 * abs(rho * u_star(i- 1,j)* dx/mu))^5)) + max ((rho * u_star(i-1,j)* dx),0); aun = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j+1)* dx/mu))^5)) + max (-(rho * u_star(i,j+1)* dx),0); aus = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j-1)* dx/mu))^5)) + max ((rho * u_star(i,j-1)* dx),0); aup = aue + auw + aun + aus; ave = mu * (max(0,(1- 0.1 * abs(rho * v_star(i+1,j)* dx/mu))^5)) + max (-(rho * v_star(i+1,j)* dx),0); avw = mu * (max(0,(1- 0.1 * abs(rho * v_star(i- 1,j)* dx/mu))^5)) + max ((rho * v_star(i-1,j)* dx),0); avn = mu * (max(0,(1- 0.1 * abs(rho * v_star(i,j+1)* dx/mu))^5)) + max (-(rho * v_star(i,j+1)* dx),0); avs = mu * (max(0,(1- 0.1 * abs(rho * v_star(i,j-1)* dx/mu))^5)) + max ((rho * v_star(i,j-1)* dx),0); avp = ave + avw + avn + avs; apw = rho * dx * dx / aup; ape = rho * dx * dx / aue;
  • 22.
    22 aps = rho* dx * dx / avp; apn = rho * dx * dx / ave; ap = ape + apw + apn + aps; p_cor3(i,j)= (ape * p_corr(i+1,j) + apw * p_corr(i-1,j) + apn * p_corr(i,j+1) + aps * p_corr(i,j-1) + D(i,j)) / ap; end end p_corr=p_cor3; end % The new guessed field of pressure and velocity % Calculate Corrected pressure for i=1:N for j=1:M p(i,j)=p_star(i,j)+p_corr(i,j); end end %Check boundary conditions for p p(i_p,1)=p(i_p,2); %Bottom Wall BC p(1,j_p)=p(2,j_p); %Left Wall BC p(N,j_p)=p(N-1,j_p); %Right Wall BC p(i_p,M)=p(i_p,(M-1)); %Top BC p_star=p; % Calculate corrected velocities for i=2:(N-2) for j=2:(M-2) aue = mu * (max(0,(1- 0.1 * abs(rho * u_star(i+1,j)* dx/mu))^5)) + max (-(rho * u_star(i+1,j)* dx),0); auw = mu * (max(0,(1- 0.1 * abs(rho * u_star(i-1,j)* dx/mu))^5)) + max ((rho * u_star(i-1,j)* dx),0); aun = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j+1)* dx/mu))^5)) + max (-(rho * u_star(i,j+1)* dx),0); aus = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j-1)* dx/mu))^5)) + max ((rho * u_star(i,j-1)* dx),0); aup = aue + auw + aun + aus; due = dx / aup;
  • 23.
    23 u_corr(i,j)= due *(p_corr(i-1,j)-p_corr(i,j)); u(i,j)=u_star(i,j)+u_corr(i,j); % Check boundary conditions for u u(1:(N-1),M)=U; %Top Boundary velocity for u u(1:(N-1),M-1)=U; %Top Boundary velocity for u u(1:(N-1),1)=0; %Bottom Boundary velocity for u for j_u=2:(M-1) %Left Boundary velocity for u u(1,j_u)=0;%(u(2,j_u))/3; %u velocity near wall approx to one third of adjacent u node end for j_u=2:(M-1) %Right Boundary velocity for u u(N-1,j_u)=0;%(u(N-2,j_u))/3; %u velocity near wall approx to one third of adjacent u node end end end u_star=u; for i=2:(N-2) for j=2:(M-2) ave = mu * (max(0,(1- 0.1 * abs(rho * v_star(i+1,j)* dx/mu))^5)) + max (-(rho * v_star(i+1,j)* dx),0); avw = mu * (max(0,(1- 0.1 * abs(rho * v_star(i- 1,j)* dx/mu))^5)) + max ((rho * v_star(i-1,j)* dx),0); avn = mu * (max(0,(1- 0.1 * abs(rho * v_star(i,j+1)* dx/mu))^5)) + max (-(rho * v_star(i,j+1)* dx),0); avs = mu * (max(0,(1- 0.1 * abs(rho * v_star(i,j-1)* dx/mu))^5)) + max ((rho * v_star(i,j-1)* dx),0); avp = ave + avw + avn + avs; dvn = dx / avp; v_corr(i,j)= dvn * (p_corr(i,j-1)-p_corr(i,j)); v(i,j)=v_star(i,j)+v_corr(i,j);
  • 24.
    24 % Check boundaryconditions for v v(1,1:(M-1))=0; %Left Boundary velocity for v v(N,1:(M-1))=0; %Right Boundary velocity for v for i_v=2:(N-1) %Top Boundary velocity for v v(i_v,M-1)=0;%(v(i_v,M-2))/3; %v velocity near top approx to one third of adjacent v node end for i_v=2:(N-1) %Bottom Boundary velocity for v v(i_v,1)=0;%(v(i_v,2))/3; %v velocity near top approx to one third of adjacent v node end end end v_star=v; end %----------------------------Loop ends-------------------- % Calculate plotvariables on a non-staggered grid (pressure- nodes) for i=2:N-1 for j=2:M-1 u_plot(i,j)=0.5*(u_star(i-1,j)+u_star(i,j)); end end u_plot(2:(N-1),M)=U; %Top Boundary velocity for u u_plot(2:(N-1),M-1)=U; u_plot(1:(N-1),1)=0; %Bottom Boundary velocity for u u_plot(1,1:M)=0; %Left Boundary velocity for u u_plot(N,1:M)=0; %Right Boundary velocity for u u_plot=u_plot'; for i=2:N-1
  • 25.
    25 for j=2:M-1 v_plot(i,j)=0.5*(v_star(i,j-1)+v_star(i,j)); end end v_plot(2:(N-1),M)=0; %TopBoundary velocity for u v_plot(1:(N-1),1)=0; %Bottom Boundary velocity for u v_plot(1,1:M)=0; %Left Boundary velocity for u v_plot(N,1:M)=0; %Right Boundary velocity for u v_plot=v_plot'; for i=1:N for j=1:M p_plot(i,j)=p(i,j); end end quiver(u_plot,v_plot) %figure,contour(u,40) %figure,contour(p,40) figure,surf(u_plot) figure,surf(v_plot) Square Elbow Flow Problem: clear all; close all; clc; %Parameters L=1; %side length of domain U=5; %Upper plate velocity N=21; %Gridsize x-direction
  • 26.
    26 M=21; %Gridsize y-direction dx=1/(N-1);%spatial step in x-direction dy=1/(M-1); %spatial step in y-direction mu = 0.25; rho = 1; %Indexing the domain (staggered grid) example 1:N i_u=1:N; %Index describing the u-nodes in the x-direction (i) j_u=1:M; %Index describing the u-nodes in the y- direction (j) i_v=1:N; %Index describing the v-nodes in the x- direction (i) j_v=1:M; %Index describing the v-nodes in the y-direction (j) i_p=1:N; %Index describing the p-nodes in the x- direction (i) j_p=1:M; %Index describing the p-nodes in the y- direction (j) i_uip=2:(N-2); %Index of inner points for u-nodes j_uip=2:(M-1); i_vip=2:(N-1); %Index of inner points for v-nodes j_vip=2:(M-2); i_pip=2:(N-1); %Index of inner points for p-nodes j_pip=2:(M-1); %Initial values (inner points) u(i_u,j_u)=0; %Velocity in the x-direction in the whole domain v(i_v,j_v)=0; %Velocity in the y-direction in the whole domain p(i_p,j_p)=0; %Pressurefield in the whole domain % Initual guess for u, v and p u_star(1:(N-1),1:M)=0; u_star(:,:)=0; %Initial guess of u v_star(1:N,1:(M-1))=0; v_star(:,:)=0; %Initial guess of v p_star(1:N,1:M)=0; p_star(:,:)=0; %Initial guess of p %Initial boundary conditions u_star(i_u,M)=0; %Top Boundary velocity for u u_star(i_u,M-1)=0; %Top Boundary velocity for u u_star(i_u,1)=0; %Bottom Boundary velocity for u %Left Boundary velocity for u u_star(1:10,j_u)=U;
  • 27.
    27 %Right Boundary velocityfor u u_star(N-1,j_u)=0; %Top Boundary velocity for v v_star(10:N,M-1)=U; v_star(10:N,M)=U; v_star(1:9,M) = 0; v_star(1:9,M-1)=0; %Bottom Boundary velocity for v v_star(i_v,1)=0; v_star(1,j_v)=0; %Left Boundary velocity for v v_star(N,j_v)=0; %Right Boundary velocity for v %Bottom boundary pressure p(i_p,1)=p(i_p,2); %p at wall is approx same as p near wall %Left boundary p(1,j_p)=p(2,j_p); %p at wall is approx same as p near wall %Right boundary p(N,j_p)=p(N-1,j_p); %p at wall is approx same as p near wall %Top boundary p(i_p,M)=p(i_p,(M-1)); %p at wall is approx same as p near wall %----------------------------Loop starts----------------- % Loop performing SIMPLE algorithm until convergence % Use suitable loop to achieve converged results u_corr(1:(N-1),1:M)=0; u_corr(:,:)=0; v_corr(1:N,1:(M-1))=0; v_corr(:,:)=0; p_corr(1:N,1:M)=0; p_corr(:,:)=0; for i=2:N-1 for j=2:M-1 p_corr(i,j)=0.1; end end q=0; while (q<300) % Control convergence q=q+1;
  • 28.
    28 disp(['Iteration ', int2str(q)]); %Solve momentum Equations with guessed pressures u_new(1:(N-1),1:M)=0; for i=2:(N-1) for j=2:(M-1) aue = mu * (max(0,(1- 0.1 * abs(rho * u_star(i+1,j)* dx/mu))^5)) + max (-(rho * u_star(i+1,j)* dx),0); auw = mu * (max(0,(1- 0.1 * abs(rho * u_star(i-1,j)* dx/mu))^5)) + max ((rho * u_star(i-1,j)* dx),0); aun = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j+1)* dx/mu))^5)) + max (-(rho * u_star(i,j+1)* dx),0); aus = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j-1)* dx/mu))^5)) + max ((rho * u_star(i,j-1)* dx),0); aup = aue + auw + aun + aus; u_star(i,j)= (aue * u_star(i+1,j) + auw * u_star(i- 1,j) + aun * u_star(i,j+1) + aus * u_star(i,j-1) + (dy*(p_star(i-1,j)-p_star(i,j))))/(aup); end end u_star(10:(N-1),M)=0; %Top Boundary velocity for u_new u_star(10:(N-1),M-1)=0; %Top Boundary velocity for u_new u_star(1:(N-1),1)=0; %Bottom Boundary velocity for u_new for j=1:10 %Left & Right Boundary velocity for u_new % u_star(1,j)=(u(2,j))/3; %u velocity near wall approx to one third of adjacent u node % u_star(N-1,j)=(u(N-2,j))/3; u_star(1,j)=U; end u_star(N-1,:) = 0; v_star(1:N,1:(M-1))=0; for i=2:(N-1) for j=2:(M-1) ave = mu * (max(0,(1- 0.1 * abs(rho * v_star(i+1,j)* dx/mu))^5)) + max (-(rho * v_star(i+1,j)* dx),0);
  • 29.
    29 avw = mu* (max(0,(1- 0.1 * abs(rho * v_star(i-1,j)* dx/mu))^5)) + max ((rho * v_star(i-1,j)* dx),0); avn = mu * (max(0,(1- 0.1 * abs(rho * v_star(i,j+1)* dx/mu))^5)) + max (-(rho * v_star(i,j+1)* dx),0); avs = mu * (max(0,(1- 0.1 * abs(rho * v_star(i,j-1)* dx/mu))^5)) + max ((rho * v_star(i,j-1)* dx),0); avp = ave + avw + avn + avs; v_star(i,j)=(ave * v_star(i+1,j) + avw * v_star(i- 1,j) + avn * v_star(i,j+1) + avs * v_star(i,j-1) + (dx*(p_star(i,j-1)- p_star(i,j))))/(avp); end end v_star(1,1:10)=0; %Left Boundary velocity for v v_star(N,1:(M-1))=0; %Right Boundary velocity for v for i=10:21 %Top & Bottom Boundary velocity for v %v_star(i,M-1)=0;%(v_star(i,M-2))/3; %v velocity near top approx to one third of adjacent v node %v_star(i,1)=0;%(v_star(i,2))/3; v_star(i,M-1)=U; v_star(i,M)=U; end v_star(1:9,M-1)=0; v_star(1:9,M)=0; % Use new velocities to get the pressure correction term % By solving the proper poisson equation (loop!!!) p_cor3(1:N,1:M)=0; for r=1:30 disp([' Inner Loop ', int2str(r),' of Loop #',int2str(q) ]); for i=2:(N-2) for j=2:(M-1) D(i,j)= (u_star(i,j)-u_star(i+1,j)+v_star(i,j)- v_star(i,j+1))*rho*dx; aue = mu * (max(0,(1- 0.1 * abs(rho * u_star(i+1,j)* dx/mu))^5)) + max (-(rho * u_star(i+1,j)* dx),0);
  • 30.
    30 auw = mu* (max(0,(1- 0.1 * abs(rho * u_star(i- 1,j)* dx/mu))^5)) + max ((rho * u_star(i-1,j)* dx),0); aun = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j+1)* dx/mu))^5)) + max (-(rho * u_star(i,j+1)* dx),0); aus = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j-1)* dx/mu))^5)) + max ((rho * u_star(i,j-1)* dx),0); aup = aue + auw + aun + aus; ave = mu * (max(0,(1- 0.1 * abs(rho * v_star(i+1,j)* dx/mu))^5)) + max (-(rho * v_star(i+1,j)* dx),0); avw = mu * (max(0,(1- 0.1 * abs(rho * v_star(i- 1,j)* dx/mu))^5)) + max ((rho * v_star(i-1,j)* dx),0); avn = mu * (max(0,(1- 0.1 * abs(rho * v_star(i,j+1)* dx/mu))^5)) + max (-(rho * v_star(i,j+1)* dx),0); avs = mu * (max(0,(1- 0.1 * abs(rho * v_star(i,j-1)* dx/mu))^5)) + max ((rho * v_star(i,j-1)* dx),0); avp = ave + avw + avn + avs; apw = rho * dx * dx / aup; ape = rho * dx * dx / aue; aps = rho * dx * dx / avp; apn = rho * dx * dx / ave; ap = ape + apw + apn + aps; p_cor3(i,j)= (ape * p_corr(i+1,j) + apw * p_corr(i-1,j) + apn * p_corr(i,j+1) + aps * p_corr(i,j-1) + D(i,j)) / ap; end end p_corr=p_cor3; end % The new guessed field of pressure and velocity % Calculate Corrected pressure for i=1:N for j=1:M p(i,j)=p_star(i,j)+p_corr(i,j); end end %Check boundary conditions for p p(i_p,1)=p(i_p,2); %Bottom Wall BC p(1,j_p)=p(2,j_p); %Left Wall BC p(N,j_p)=p(N-1,j_p); %Right Wall BC
  • 31.
    31 p(i_p,M)=p(i_p,(M-1)); %Top BC p_star=p; %Calculate corrected velocities for i=2:(N-2) for j=2:(M-1) aue = mu * (max(0,(1- 0.1 * abs(rho * u_star(i+1,j)* dx/mu))^5)) + max (-(rho * u_star(i+1,j)* dx),0); auw = mu * (max(0,(1- 0.1 * abs(rho * u_star(i-1,j)* dx/mu))^5)) + max ((rho * u_star(i-1,j)* dx),0); aun = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j+1)* dx/mu))^5)) + max (-(rho * u_star(i,j+1)* dx),0); aus = mu * (max(0,(1- 0.1 * abs(rho * u_star(i,j-1)* dx/mu))^5)) + max ((rho * u_star(i,j-1)* dx),0); aup = aue + auw + aun + aus; due = dx / aup; u_corr(i,j)= due * (p_corr(i-1,j)-p_corr(i,j)); u(i,j)=u_star(i,j)+u_corr(i,j); % Check boundary conditions for u u(10:(N-1),M)=0; %Top Boundary velocity for u_new u(10:(N-1),M-1)=0; %Top Boundary velocity for u_new u(1:(N-1),1)=0; %Bottom Boundary velocity for u_new for jj=1:10 %Left & Right Boundary velocity for u_new % u_star(1,j)=(u(2,j))/3; %u velocity near wall approx to one third of adjacent u node % u_star(N-1,j)=(u(N-2,j))/3; u(1,jj)=U; end u(N-1,:) = 0; end end u_star=u; for i=2:(N-2) for j=2:(M-1)
  • 32.
    32 ave = mu* (max(0,(1- 0.1 * abs(rho * v_star(i+1,j)* dx/mu))^5)) + max (-(rho * v_star(i+1,j)* dx),0); avw = mu * (max(0,(1- 0.1 * abs(rho * v_star(i- 1,j)* dx/mu))^5)) + max ((rho * v_star(i-1,j)* dx),0); avn = mu * (max(0,(1- 0.1 * abs(rho * v_star(i,j+1)* dx/mu))^5)) + max (-(rho * v_star(i,j+1)* dx),0); avs = mu * (max(0,(1- 0.1 * abs(rho * v_star(i,j-1)* dx/mu))^5)) + max ((rho * v_star(i,j-1)* dx),0); avp = ave + avw + avn + avs; dvn = dx / avp; v_corr(i,j)= dvn * (p_corr(i,j-1)-p_corr(i,j)); v(i,j)=v_star(i,j)+v_corr(i,j); % Check boundary conditions for v v(1,1:10)=0; %Left Boundary velocity for v v(N,1:(M-1))=0; %Right Boundary velocity for v for ii=10:21 %Top & Bottom Boundary velocity for v %v_star(i,M-1)=0;%(v_star(i,M-2))/3; %v velocity near top approx to one third of adjacent v node %v_star(i,1)=0;%(v_star(i,2))/3; v(ii,M-1)=U; v(ii,M)=U; end v(1:9,M-1)=0; v(1:9,M)=0; end end v_star=v; end %----------------------------Loop ends-------------------- % Calculate plotvariables on a non-staggered grid (pressure- nodes) %u_plot(1:N,1:M)=0; u_plot(:,:)=0; for i=2:N-1
  • 33.
    33 for j=2:M-1 u_plot(i,j)=0.5*(u_star(i-1,j)+u_star(i,j)); end end u_plot(10:(N-1),M)=0; %TopBoundary velocity for u_new u_plot(10:(N-1),M-1)=0; %Top Boundary velocity for u_new u_plot(1:(N-1),1)=0; %Bottom Boundary velocity for u_new u_plot(1,1:10)=U; u_plot(N,:) = 0; u_plot=u_plot'; for i=2:N-1 for j=2:M-1 v_plot(i,j)=0.5*(v_star(i,j-1)+v_star(i,j)); end end v_plot(1,1:10)=0; %Left Boundary velocity for v v_plot(N,1:(M-1))=0; %Right Boundary velocity for v v_plot(10:21,M-1)=U; v_plot(10:21,M)=U; v_plot(1:9,M-1)=0; v_plot(1:9,M)=0; v_plot=v_plot'; for i=1:N for j=1:M p_plot(i,j)=p(i,j); end end quiver(u_plot,v_plot) figure,surf(u_plot) figure,surf(v_plot)