Top Banner
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
33

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

Feb 11, 2017

Download

Documents

Rob Morien
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: RJM-AB-CFD-Final-Project-Dec-18-2015

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

Page 2: RJM-AB-CFD-Final-Project-Dec-18-2015

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:

uV

u u p +S dV V i

Next, we apply the divergence theorem:

uA A A

u d u d pd +S dV V A A A i

Let

u u J V

Page 3: RJM-AB-CFD-Final-Project-Dec-18-2015

3

Substituting, we get:

uA 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 sA

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 Eu u J V

Page 4: RJM-AB-CFD-Final-Project-Dec-18-2015

4

And so,

1

0

x

E E

yE E

uuy u y

uv

J i

Which expands to:

2

E E x Ey 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 eE

u uu

ee ex E

u uu

x

Therefore:

2

2

ee e ee eE

u u u uy y

x

J i

(3)

Next, considering the nJ term:

ˆ

ˆ ˆ

0

1

x

n n

yn n

uux u x

uv

J j

ˆ ˆ ˆ ˆn n n y nx v u u x J j

Again, using central differencing for the velocities:

ˆ2

nne en

u uu

ˆ2

n nen

v vv

ˆ

nne ey n

u uu

y

Therefore:

ˆ2 2

nne e n ne nne en

u u v v u ux x

y

J j

(4)

Next, considering the PJ term:

1

0

x

P P

yP P

uuy u y

uv

J i

Which expands to:

Page 5: RJM-AB-CFD-Final-Project-Dec-18-2015

5

2

P P x Py u u y J i

Using the central differencing for the velocities:

2

e wP

u uu

e wx P

u uu

x

Therefore:

2

2

e w e wP

u u u uy y

x

J i

(5)

Finally, considering the sJ term:

ˆ ˆ

ˆ ˆ

0

1

x

s s

ys s

uux u x

uv

J j

Which expands to:

ˆ ˆ ˆ ˆs s s y sx v u u x J j

Using the central differencing for the velocities:

ˆ2

sse es

u uu

ˆ2

s ses

v vv

ˆ

e ssey s

u uu

y

Therefore:

ˆ2 2

sse e s se e sses

u u v v u ux x

y

J j

(6)

Next, we consider the pressure term:

ˆ ˆ ˆ ˆf f E E n n P P s sA

pd p p p + p + p A i A i A A A A i

E PA

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

Page 6: RJM-AB-CFD-Final-Project-Dec-18-2015

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 uy

x

u u v v u ux

y

u u u uy

x

u u v v u ux

y

0

uf f

f

E P u

S Vp

p p y S x y

A i

J A

2e

x ya

y x

ee w

ya a

x

nne sse

xa a

y

2

24

ee e nne e n ne

u P E u

e w sse e s se

u u y u u v v xb 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:

vV

v v P +S dV V j

Next, we apply the divergence theorem:

vA A A

v d v d Pd +S dV V A A A j

Let

v v J V

Substituting, we get:

vA A

d Pd +S dV J A A j

(8)

Figure 2 shows the staggered grid used for the v momentum equation.

Page 7: RJM-AB-CFD-Final-Project-Dec-18-2015

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 eA

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 Nv v J V

And so,

0

1

x

N N

yN N

vux v x

vv

J j

Which expands to:

2

N N y Nx v v x J j

Using central differencing to determine the velocities:

Page 8: RJM-AB-CFD-Final-Project-Dec-18-2015

8

2

nn nN

v vv

nn ny N

v vv

y

Therefore

2

2

nn n nn nN

v v v vx x

y

J j

(9)

Next, considering the wJ term:

ˆ ˆ

ˆ ˆ

1

0

x

w w

yw w

vuy v y

vv

J i

ˆ ˆ ˆ ˆw w w x wy u v v y J i

Again, using central differencing for the velocities:

ˆ2

nnw ww

u uu

ˆ2

n nww

v vv

ˆ

n nwx w

v vv

x

Therefore:

ˆ2 2

nnw w n nw n nww

u u v v v vy y

x

J i

(10)

Next, considering the PJ term:

0

1

x

P P

yP P

vux v x

vv

J j

Which expands to:

2

P P y Px v v x J j

Using the central differencing for the velocities:

2

n sP

v vv

n sy P

v vv

y

Therefore:

Page 9: RJM-AB-CFD-Final-Project-Dec-18-2015

9

2

2

n s n sP

v v v vx x

y

J j

(11)

Finally, considering the eJ term:

ˆ ˆ

ˆ ˆ

1

0

x

e e

ye e

vuy v y

vv

J i

Which expands to:

ˆ ˆ ˆ ˆe e e x ey u v v y J i

Using the central differencing for the velocities:

ˆ2

nne ee

u uu

ˆ2

ne ne

v vv

ˆ

ne nx e

v vv

x

Therefore:

ˆ2 2

nne e ne n ne ne

u u v v v vy y

x

J i

(12)

Next, we consider the pressure term:

ˆ ˆ ˆ ˆf f N N w w P P e eA

pd p p p + p + p A j A j A A A A j

N PA

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 vx

y

u u v v v vy

x

v v v vx

y

u u v v v vy

x

0

vf f

f

N P v

S Vp

p p x S x y

A j

J A

Page 10: RJM-AB-CFD-Final-Project-Dec-18-2015

10

2n

x ya

y x

ne nw

ya a

x

nn s

xa a

y

2

24

nn n nnw w n nw

v P N v

n s nne e ne n

v v x u u v v yb 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:

VdV = 0 V

And applying the divergence theorem we get:

f fA 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)

Page 11: RJM-AB-CFD-Final-Project-Dec-18-2015

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 nbe P E e P E

e e e

d

a u b A a u bu 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:

Page 12: RJM-AB-CFD-Final-Project-Dec-18-2015

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

Page 13: RJM-AB-CFD-Final-Project-Dec-18-2015

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

Page 14: RJM-AB-CFD-Final-Project-Dec-18-2015

14

Figure 6, Velocity Surface Plot of U Velocity, Lid Driven Cavity

Figure 7, Velocity Surface Plot of V Velocity, Lid Driven Cavity

Page 15: RJM-AB-CFD-Final-Project-Dec-18-2015

15

Figure 8, Velocity Vector Plot, U and V Velocities, Square Elbow

Figure 9, Velocity Surface Plot of U Velocity, Square Elbow

Page 16: RJM-AB-CFD-Final-Project-Dec-18-2015

16

Figure 10, Velocity Surface Plot of V Velocity, Square Elbow

Figure 11, Fluent CFD Velocity Streamlines, Square Elbow

Page 17: RJM-AB-CFD-Final-Project-Dec-18-2015

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-

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

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

Page 19: RJM-AB-CFD-Final-Project-Dec-18-2015

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);

Page 20: RJM-AB-CFD-Final-Project-Dec-18-2015

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

Page 21: RJM-AB-CFD-Final-Project-Dec-18-2015

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;

Page 22: RJM-AB-CFD-Final-Project-Dec-18-2015

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;

Page 23: RJM-AB-CFD-Final-Project-Dec-18-2015

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);

Page 24: RJM-AB-CFD-Final-Project-Dec-18-2015

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

Page 25: RJM-AB-CFD-Final-Project-Dec-18-2015

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

Page 26: RJM-AB-CFD-Final-Project-Dec-18-2015

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;

Page 27: RJM-AB-CFD-Final-Project-Dec-18-2015

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;

Page 28: RJM-AB-CFD-Final-Project-Dec-18-2015

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);

Page 29: RJM-AB-CFD-Final-Project-Dec-18-2015

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);

Page 30: RJM-AB-CFD-Final-Project-Dec-18-2015

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

Page 31: RJM-AB-CFD-Final-Project-Dec-18-2015

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)

Page 32: RJM-AB-CFD-Final-Project-Dec-18-2015

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

Page 33: RJM-AB-CFD-Final-Project-Dec-18-2015

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)