Você está na página 1de 28

A Finite Volume method to solve Lid Driven Cavity Flow in

a Staggered grid using Pressure Correction Technique


Saikat Mukherjee
Engineering Science and Mechanics
Virgina Tech
May 15, 2017

Abstract
A CFD code has been written to solve the problem of Lid Driven Flow in a Cavity of viscous
fluid, using Finite Volume discretization. A fractional step algorithm with pressure correction
and Adam Bamsforth time stepping scheme has been implemented to advance forward in time.
A uniform cartesian staggered grid has been used.

Introduction
Lid driven cavity is a very ubiquitous problem and is considered as a bench mark platform to
validate new CFD codes and results. The problem consists of a 2D square lid driven cavity with
sides of L units length. The upper lid is moving with a velocity of Ulid units. The density and
viscosity are assumed to be constant.

Figure 1: The lid driven cavity

Governing Equations
The incompressible 2D Navier Stokes (NS) equation has been used with the primitive variables being
non-dimensionalized. The velocities have been scaled with the lid velocity, Ulid , the length scale of

1
L has been used and thus we also scale the time with a time scale of ULlid . The pressure has been
2
scaled with ρUlid , where ρ is the density of the fluid. The dimensional form of 2D incompressible
NS equation is -
∂u0 ∂v 0
+ =0 (1)
∂x0 ∂y 0
ρ∂u0 ρ∂u0 u0 ρ∂u0 v 0 ∂p0 µ∂ 2 u0 µ∂ 2 u0
+ + = − + ( + ) (2)
∂t0 ∂x0 ∂y 0 ∂x0 ∂x02 ∂y 02
ρ∂v 0 ρ∂v 0 v 0 ρ∂u0 v 0 ∂p0 µ∂ 2 v 0 µ∂ 2 v 0
+ + =− 0 +( + ) (3)
∂t0 ∂y 0 ∂x0 ∂y ∂x02 ∂y 02
After non-dimensionalising, the mass and momentum equations can be written as -
∂u ∂v
+ =0 (4)
∂x ∂y
∂u ∂uu ∂uv ∂p 1 ∂ 2u ∂ 2u
+ + =− + ( + ) (5)
∂t ∂x ∂y ∂x Re ∂x2 ∂y 2
∂v ∂vv ∂uv ∂p 1 ∂ 2v ∂ 2v
+ + =− + ( + ) (6)
∂t ∂y ∂x ∂y Re ∂x2 ∂y 2
u0 v0 x0 y0 p0 −pref
Where u = Ulid
, v = Ulid
, x= L
, y = L
, t = t0 ULlid , p = 2
ρUlid
with Re being the Reynolds
number.
ρUlid L
Re =
µ

Boundary Condition
No-slip boundary condition has been applied for u and v velocities at bottom and side walls. So -

u = 0; v = 0 (7)

In the top wall


u = Ulid ; v = 0 (8)

Numerical Discretization
Finite Volume method has been used to numerically discretize the governing equations on a staggered
grid. Staggered grid was used to avoid pressure-velocity decoupling in the inner nodes.

2
Figure 2: Representative figure of a staggered grid

As the figure shows the u and v velocities are computed on the locations marked by the horizontal
and vertical arrows. The u locations just to the left of the scalar nodes which are marked by round
points, are given the same grid location as the scalar nodes. The same holds for v locations just
below the scalar nodes. From the figure, dxs and dys are the distance between scalar nodes while
dxu and dyv are the distances between the u nodes and v nodes respectively. Thus the control
volume surrounding the u node at (i,j) has a volume of dxs dyv .
Applying the Second Ordered Central (SOC) finite volume operator on the the x-momentum
equation we have -

∂u
dxs dyv + (uu|e − uu|w )dyv + (uv|n − uv|s )dxs =
∂t
1 ∂u ∂u 1 ∂u ∂u
− (p(i, j) − p(i − 1, j))dyv + ( |e − |w )dyv + ( |n − |s )dxs (9)
Re ∂x ∂x Re ∂y ∂y
Where e, w, n, s denote the East, West, North and South cell faces of the control volume respectively.
The cell face fluxes can be calculated as -

3
u(i + 1, j) + u(i, j)
u|e =
2
u(i, j) + u(i − 1, j)
u|w =
2
v(i, j + 1) + v(i − 1, j + 1)
v|n =
2
v(i, j − 1) + v(i − 1, j − 1)
v|s =
2
Using Central differencing for the values of the unknown u at the cell faces -
u(i + 1, j) + u(i, j)
u|e =
2
u(i, j) + u(i − 1, j)
u|w =
2
u(i, j) + u(i, j + 1)
u|n =
2
u(i, j) + u(i, j − 1)
u|s =
2
The derivatives in the cell faces are -
∂u u(i + 1, j) − u(i, j)
|e =
∂x dxu
∂u u(i, j) − u(i − 1, j)
|w =
∂x dxu
∂u u(i, j + 1) − u(i, j)
|n =
∂y dys
∂u u(i, j) − u(i, j − 1)
|s =
∂y dys
Using these results in (9) we have -

ut+dt − ut p(i, j) − p(i − 1, j)


=− + Hx ; (10)
dt dxs
where
Hx = −(AuE uE + AuW uW + AuN uN + AuS uS + AuP uP )/dxu dyv
and

u|e 1
AuE = ( − )dyv
2 Redxu

4
u|w 1
AuW = ( − )dyv
2 Redxu
v|n 1
AuN = ( − )dxs
2 Redys
v|s 1
AuS = ( − )dxs
2 Redys
u|e 1 u|w 1 v|n 1 v|s 1
AuP = ( + )dyv + (− + )dyv + ( + )dxs + (− + )dxs
2 Redxu 2 Redxu 2 Redyu 2 Redys
Similarly for the y-momentum equation -

vt+dt − vt p(i, j) − p(i, j − 1)


dxu dys = − + Hy ; (11)
dt dys
where
Hy = −(AvE vE + AvW vW + AvN vN + AvS vS + AvP vP )/dxu dys
and

u|e 1
AvE = ( − )dys
2 Redxs
u|w 1
AvW = ( − )dys
2 Redxs
v|n 1
AvN = ( − )dxu
2 Redyv
v|s 1
AvS = ( − )dxu
2 Redyv
u|e 1 u|w 1 v|n 1 v|s 1
AvP = ( + )dys + (− + )dys + ( + )dxu + (− + )dxu
2 Redxs 2 Redxs 2 Redyv 2 Redyv

Here P stands for the point in consideration (i,j). Here E, W, N, S stand for East (i+1,j),
West(i-1,j), North(i,j+1) and South(i,j-1) respectively.

Since we are using a uniform grid dxu = dyv = dxs = dys

5
Algorithm and Solution procedure
In this algorithm an intermediate velocity filed (ũ and ṽ) are first computed using Hx and Hy from
the present field and after that the velocity field on the next time step is updated including the
pressure gradient. Adams Bamsforth time stepping has been used to march forward in time.
1
ũ = un + ∆t(3H n − H n−1 ) (12)
2

un+1 = ũ − ∆t∇p (13)


12 and 13 are called the predictor and corrector step respectively. A thing to note here is that the
pressure is unknown at (n+1)th level. So to find that the solution of 13 is used in the continuity
equation at (n+1)th level to form a Pressure Poisson’s equation.
The discretized continuity equation is -

(u(i + 1, j) − u(i, j))dyv + (v(i, j + 1) − v(i, j))dxu = 0 (14)

The pressure equations are

p(i + 1, j) − p(i, j)
u(i + 1, j)n+1 = ũ(i + 1, j) − ∆t( )
dxs
p(i, j) − p(i − 1, j)
u(i, j)n+1 = ũ(i, j) − ∆t( )
dxs
p(i, j + 1) − p(i, j)
v(i, j + 1)n+1 = ṽ(i, j + 1) − ∆t( )
dys
p(i, j) − p(i, j − 1)
v(i, j)n+1 = ṽ(i, j) − ∆t( )
dys
Substituting in the continuity equation we have a poisson like equation -
X p
ApP pP + Al pl = Si,j (15)
l

where ApE
= ApW= dyv /dxs ; ApN = ApS = dxu /dys and ApP = (−2 dx
dyv
s
− 2 dxu
dys
)
The source term is
1
Si,j = [((ũ)(i + 1, j) − ũ(i, j))dyv + (ṽ(i, j + 1) − ṽ(i, j))dxu ]
∆t

The algorithm thus follows the following steps -

6
1. Solve for the intermediate velocity field without considering the pressure term like in Equation
(12)

2. Solve the pressure Poisson equation to correct the pressure at the next time level using the
intermediate velocity field as in Equation(15). In this case a Gauss-Seidel solver was used
with the residual at each iteration converging to a value of 1e-5.

3. Correct the intermediate field with the pressure obtained and go to next time step as in
Equation(13)

4. Repeat the process until steady state is reached. The system can be said to be steady when
the changes in the solution are less than desired tolerance, which in the case studied is 1e-8

Boundary treatment
Because of the staggered grid used one has to be especially careful at the boundaries for the different
equations.
For the x-momentum equation, Hx has a different value at the top and bottom wall boundaries
since the coefficients change their values due to the grid spacing becoming half of the original. At
the bottom wall
v|s 2
AuS = (− − )
2 Redys
u|e 1 u|w 1 v|n 1 v|s 1 dyv
AuP = ( + )dyv + (− + )dyv + ( + )dxs + (− + )dxs +
2 Redxu 2 Redxu 2 Redyu 2 Redys Redxu
Similarly at the top wall

v|n 2
AuS = ( − )
2 Redys
dyv
AuP = AuP +
Redxu
Similarly for the y-momentum equation these changes have to be accounted for in the east and
west walls.
At the east wall -

u|e 2
AvE = ( − )dys
2 Redxs
dxu
AvP = AvP +
Redyv
At the west wall -

u|w 2
AvW = (− − )dys
2 Redxs

7
dxu
AvP = AvP +
Redyv
These adjustment wouldn’t be necessary if one uses ’Ghost Cells’. The code written has the
ability to turn ghost cells on or off with a switch.
The boundary conditions for the pressure equation applied at the West wall are -

ApN = ApS = dxu /dys

ApE = dyv /dxs


ApW = 0

dyv dxu
ApP = ( −2 )
dxs dys
Thus ApW = 0 at the west wall. The other boundaries are treated in a similar manner.

Results
The code was tested on 3 grid levels, having 32×32, 64×64 and 128×128 cells. Reynold’s number of
100 was selected. The L2 norm of the change in u-velocity was plotted against the non-dimensional
solution time.

8
-1
10 Error history for 32*32 cells

-2
10
L2 norm of change in u velocity

dt=6.25e-3
dt=3.125e-3
dt=0.0125
10-3

-4
10

10-5

-6
10

-7
10

10-8
0 5 10 15 20
Solution time

Figure 3: Error history for 32 × 32 cells, Re=100

9
10-1

-2
L2 norm of change in u velocity

10 dt=3e-3
dt=1e-3
dt=5e-4
10-3

-4
10

-5
10

10-6

-7
10

-8
10
-5 0 5 10 15 20 25
Nondimensional solution time

Figure 4: Error history for 64 × 64 cells, Re=100

10
Figure 5: Error history for 128 × 128 cells, Re=100

Solution time for convergence to steady state appears to be reducing with grid spacing(∆x) as
well as time step(∆t). It is to be noted however that although the solution time might seem to
reduce with lesser grids and lesser time steps, the actual simulation time in the computer is however
more. The number of iterations is more for more number of grid points, as well as for lesser time
steps. This can be seen in the next plot of CPU time. The CPU timw has been plotted in a semi-log
scale against the time-step (dt)

11
32*32
64*64
128*128

4
10
CPU time

103

0 0.005 0.01 0.015


dt

Figure 6: Plot of CPU time vs time step for different grid sizes, Re=100

As can be seen the CPU times have an opposite trend to the non-dimensional solution time.
The u-velocity distribution at x=0.5 and the v-velocity distribution at y=0.5 with the data given
in Tables I and II of the seminal paper by Ghia-Ghia-Shin[1]. All the three grid sizes were used
with a representative time step of 5e-4.

12
1

128*128
0.8
ghia data
64*64
32*32

0.6
y

0.4

0.2

0
-0.2 0 0.2 0.4 0.6 0.8
u-velocity in mid plane

Figure 7: u-velocity in the mid-plane, Re=100

13
0.15

0.1 128*128
ghia data
64*64
0.05
v-velocity in midplane

32*32

-0.05

-0.1

-0.15

-0.2

0 0.2 0.4 0.6 0.8 1


x

Figure 8: v-velocity in the mid plane, Re=100

As can be seen from the figures, the 128 × 128 grid size most accurately matches with the data
from the paper. Also the variation in the v-velocity seems more prominent for lower grid sizes.
A representative flowfield at 128 × 128 grid size with time step of 5e-4 is shown below -

14
1 1

u(m/s) v(m/s)

0.8 0.9 0.8 0.3


0.8 0.2
0.7 0.1
0.6 0
0.5 -0.1
0.4 -0.2
0.6 0.3
0.6 -0.3
0.2 -0.4
y(m)

y(m)
0.1 -0.5
0
-0.1
0.4 -0.2 0.4

0.2 0.2

0 0
0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1
x(m) x(m)

(a) u velocity contour (b) v velocity contour

Figure 9: A representative flow field at Re=100, 128 × 128 cells

High Reynolds Number calculation


A simulation was run for Re = 1000, on a 128×128 finite volume grid. A time step of 5e-3 was used.
It is to be noted that the time-step is 10 times the maximum used for Re=100. This is because from
Von-neumann stability condition, the time step is directly proportional to the Reynolds number.The
solution took significantly fewer time steps(20655) than the Re = 100 case to converge. This may
be due to the bigger time step which jumps over the flow features in the domain. The representative
flow field is shown -

15
1

u(m/s)

0.8 0.9 1
0.8
0.7
0.6 v(m/s)
0.5 0.8 0.3
0.4 0.2
0.6 0.3 0.1
0
0.2
y(m)

-0.1
0.1 -0.2
0 0.6 -0.3
-0.1 -0.4

y(m)
0.4 -0.2 -0.5
-0.6
-0.3
0.4

0.2
0.2

0
0 0.2 0.4 0.6 0.8 1 0
0 0.2 0.4 0.6 0.8 1
x(m) x(m)

(a) u velocity contour (b) v velocity contour

Figure 10: A representative flow field at Re=1000, 128 × 128 cells

As can be seen from the figures, at Re=100, the velocity of the lid has not diffused that much
inside the cavity compared to Re =1000 case. The center of the primary vortex is high up near the
top lid. This is because of the lower momentum which does not diffuse enough inside the domain,
hence creates very little advection, a weak circulation, with the vortex core being near the lid. This
can also be understood by the size of the secondary vortices, which are very small for these cases.
At Re=100, we see the advection has slightly increased. more momentum is diffused in the cavity,
and the primary vortex core is lower. The secondary vortices are also bigger. At Re=1000 cases,
because of the large momentum of the lid, shearing the flow, a lot of momentum is diffused inside the
cavity. This results in better circulation signified by the primary vortex core being lower. We can
also see the secondary vortices are larger in size, signifying higher backflow because of the no-slip
condition at the wall. The secondary vortices almost nullify the effect of the primary vortex. We
can also see a little circulation developing at the left hand top corner. The contours almost looks
the same if bottom lid was moving in opposite direction with Ulid velocity.
The u and v-velocities in the mid-planes were also matched with corresponding Re=1000 data
from [1]. The results show good correspondence -

16
1

128*128
0.8
ghia data

0.6
y

0.4

0.2

0
-0.2 0 0.2 0.4 0.6 0.8
u-velocity in mid-plane

Figure 11: u-velocity in the mid-plane, Re=1000

17
0.2 128*128
ghia data
v-velocity in mid-plane

-0.2

-0.4

0 0.2 0.4 0.6 0.8 1


x

Figure 12: v-velocity in the mid plane, Re=1000

Conclusion
As can be seen with the results, the methods used showed good correspondence with bench mark
solutions like [1]. The code written is somewhat slow because of the Gauss-Seidel routine used to
solve for pressure, but since this was a class project, due to limited amount of time, other methods
were not implemented. ADI and multigrid methods would definitely make the code a lot faster.

18
Parallelizing it can make it a lot faster as well. Lastly, since the simulations were run on a basic
laptop with only one Intel(R)Core(TM)i54210U@1:70GHz2:40GHz processor, the CPU times were
large. They can be definitely much smaller on a faster system. Overall though, the solutions should
remain the same and as can be seen from the results, give us pretty good approximations on the
Lid-Driven Cavity Flow problem.

Acknowledgment
This work has been submitted as a final project for ME/AOE 6434 COMPUTATIONAL FLUID
DYNAMICS and HEAT TRANSFER. I would like to thank Dr. Danesh Tafti, the teacher of this
course for his valuable suggestions.

References
[1] U. Ghia, K. Ghia, and C. Shin, “High-re solutions for incompressible flow using the navier-stokes
equations and a multigrid method,” Journal of Computational Physics, vol. 48, no. 3, pp. 387 –
411, 1982.

Appendix
The matlab routines used are attached below. Here is the main script -
1 %S a i k a t Mukherjee
2 %F i n a l P r o j e c t CFD
3

4 clear all ;
5 clc ;
6 %% I n i t i a l Parameters
7 Nc=32; %No o f c e l l s
8

9 Nx=Nc+2;
10 Ny=Nc+2;
11 n=Nc+2;
12

13 N=(Nc ) ∗( Nc ) ;
14

15 x = 0 : 1 / ( Nc−1) : 1 ;
16 y = 0 : 1 / ( Nc−1) : 1 ;
17 [ X,Y] = meshgrid ( x , y ) ;
18

19
19

20 dt=5e −4; %time s t e p


21

22

23 dx=1/Nc ;
24 dy=1/Nc ;
25

26

27 Re=100; %r e y n o l d ’ s no
28 U l i d =1; %top l i d v e l o c i t y
29 L=1;
30 G=0; %Switch f o r g h o s t c e l l s
31

32 nmax=500000;
33 t o l e r m a i n = 1e −8; %t o l e r e n c e f o r u and v change t o r e a c h s t e a d y
state
34 t o l e r p = 1e −5; %t o l e r e n c e f o r p r e s s u r e p o i s s o n ’ s e q u a t i o n
35 i s C o n v e r g e d =0;
36 i t i n i t =1;
37

38

39 %% i n i t i a l i z e
40 u=z e r o s (Nx , Ny) ;
41 u t i l d a=z e r o s (Nx , Ny) ;
42 u o l d=z e r o s (Nx , Ny) ;
43 v=z e r o s (Nx , Ny) ;
44 v t i l d a=z e r o s (Nx , Ny) ;
45 v o l d=z e r o s (Nx , Ny) ;
46

47 p=z e r o s (Nx , Ny) ;


48 p new=z e r o s (Nx , Ny) ;
49 u o l d=u ;
50 v o l d=v ;
51

52

53 f p 1 = f o p e n ( ’ . / h i s t o r y g h o s t 3 2 5 e −4. dat ’ , ’w ’ ) ;
54 f p r i n t f ( fp1 , ’TITLE = ” E r r o r H i s t o r y ”\n ’ ) ;
55 f p r i n t f ( fp1 , ’ v a r i a b l e s =” I t e r a t i o n ”” t i m e u n i t s ”” E r r o r u ”” E r r o r v ”\n ’ ) ;
56

57

58 tic

20
59 f o r i t e r= i t i n i t : nmax
60

61 %% Boundary c o n d i t i o n s
62

63 %V e l o c i t y
64 [ u , v]= s e t b c s ( u , v , U l i d , Nx , Ny ,G) ;
65

66 %% x mom & y mom


67

68 [ Hx prev , Hy prev ]=HxHy( u o l d , v o l d , Nx , Ny ,G, Re , dx , dy ) ;


69 [ Hx , Hy]=HxHy( u o l d , v o l d , Nx , Ny ,G, Re , dx , dy ) ;
70

71 %% I n t e r m e d i a t e v e l o c i t i e s
72

73 i f i t e r==i t i n i t
74

75 u t i l d a ( 2 : Nx−1 ,1:Ny−1) = u ( 2 : Nx−1 ,1:Ny−1) + dt ∗Hx ( 2 : Nx−1 ,1:Ny−1) ;


76 v t i l d a ( 1 : Nx−1 ,2:Ny−1) = v ( 1 : Nx−1 ,2:Ny−1) + dt ∗Hy ( 1 : Nx−1 ,2:Ny−1) ;
77

78 else
79

80

81 u t i l d a ( 2 : Nx−1 ,1:Ny−1) = u ( 2 : Nx−1 ,1:Ny−1) + dt /2∗(3∗Hx ( 2 : Nx−1 ,1:Ny−1) −


Hx prev ( 2 : Nx−1 ,1:Ny−1) ) ;
82 v t i l d a ( 1 : Nx−1 ,2:Ny−1) = v ( 1 : Nx−1 ,2:Ny−1) + dt /2∗(3∗Hy ( 1 : Nx−1 ,2:Ny−1) −
Hy prev ( 1 : Nx−1 ,2:Ny−1) ) ;
83

84

85

86 end
87

88

89 %% p r e s s u r e p o i s s o n e q u a t i o n
90

91

92 AS p = z e r o s (Nx , Ny) ;
93 AS p ( : , 3 : Nx−1) = −dx/dy ;
94 AN p = z e r o s (Nx , Ny) ;
95 AN p ( : , 2 : Ny−2) = −dx/dy ;
96 AW p = z e r o s (Nx , Ny) ;
97 AW p( 3 : Nx− 1 , : ) = −dy/dx ;

21
98 AE p = z e r o s (Nx , Ny) ;
99 AE p ( 2 : Nx− 2 , : ) = −dy/dx ;
100 AP p = (AW p + AE p + AS p + AN p) ;
101

102

103 f o r i =2:Nx−1
104 f o r j =2:Ny−1
105 S ( i , j )= ( ( u t i l d a ( i +1, j ) − u t i l d a ( i , j ) ) ∗dy + ( v t i l d a ( i , j +1) −
v t i l d a ( i , j ) ) ∗dx ) / dt ; %Source term
106 end
107 end
108

109

110 %% Gauss S e i d e l
111 w=1;
112 r e s =100;
113

114 w h i l e ( r e s >t o l e r p )
115

116

117

118 f o r i =2:Nx−1
119 f o r j =2:Ny−1
120

121 p new ( i , j ) = ( 1 / AP p ( i , j ) ) ∗( AP p ( i , j ) ∗(1−w) ∗p ( i , j ) + w∗(


AS p ( i , j ) ∗ p new ( i , j −1) + AW p( i , j ) ∗ p new ( i −1, j ) + AE p ( i ,
j ) ∗p ( i +1, j ) + AN p( i , j ) ∗p ( i , j +1) + S ( i , j ) ) ) ;
122

123 d i f f ( i , j ) =(AE p ( i , j ) ∗p ( i +1, j ) + AW p( i , j ) ∗p ( i −1, j ) + AN p( i


, j ) ∗p ( i , j +1) + AS p ( i , j ) ∗p ( i , j −1) − AP p ( i , j ) ∗p ( i , j ) + S (
i , j )) .ˆ2;
124

125 end
126 end
127

128 p=p new ;


129 r e s =((1/N) ∗sum ( d i f f ( : ) ) ) ˆ 0 . 5 ;
130

131 end
132

133 %end g a u s s s e i d e l

22
134

135

136 %% Time update


137 f o r i =2:Nx−1
138 f o r j =2:Ny−1
139 u ( i , j )=u t i l d a ( i , j ) − dt ∗( p ( i , j )−p ( i −1, j ) ) /dx ;
140 v ( i , j )=v t i l d a ( i , j ) − dt ∗( p ( i , j )−p ( i , j −1) ) /dy ;
141 end
142 end
143

144 e r r u =(u−u o l d ) . ˆ 2 ;
145 e r r o r u =(sum ( e r r u ( : ) ) /N) ˆ 0 . 5 ;
146

147 e r r v =(v−v o l d ) . ˆ 2 ;
148 e r r o r v =(sum ( e r r v ( : ) ) /N) ˆ 0 . 5 ;
149

150

151

152 u o l d=u ;
153 v o l d=v ;
154

155

156 %% Normalize p r e s s u r e
157 p=p−p ( 5 , 5 ) ;
158 %%
159 t=dt ∗( i t e r −1) ;
160

161 i f ( (mod( i t e r , 1 ) ==0) | | ( i t e r==i t i n i t ) )


162 f p r i n t f ( fp1 , ’%d %e %e %e \n ’ , i t e r , t , e r r o r u , e r r o r v ) ;
163 f p r i n t f ( ’%d %e %e %e \n ’ , i t e r , t , e r r o r u , e r r o r v ) ;
164

165 end
166 cond = max( e r r o r u , e r r o r v ) ;
167

168 i f ( cond<t o l e r m a i n )
169 f p r i n t f ( fp1 , ’%d %e %e %e \n ’ , i t e r , t , e r r o r u , e r r o r v ) ;
170 isConverged = 1 ;
171 break ;
172 end
173

174

23
175

176 %End o f time i t e r a t i o n l o o p


177 end
178 toc
179

180 %% p r i n t on s c r e e n
181

182 f c l o s e ( fp1 ) ;
183 toc
184 i f i s C o n v e r g e d == 0
185 f p r i n t f ( ’ S o l u t i o n f a i l e d t o become s t e a d y i n %d i t e r a t i o n s ! ! ! ’ ,
nmax) ;
186 end
187

188 i f i s C o n v e r g e d == 1
189 f p r i n t f ( ’ S o l u t i o n became s t e a d y i n %d i t e r a t i o n s ! ! ! ’ , i t e r ) ;
190 end
191

192 %% v e l o c i t y i n c e l l c e n t e r s
193 uc = 0 . 5 ∗ ( u ( 2 : end −1 ,2: end −1) + u ( 3 : end , 2 : end −1) ) ;
194 vc = 0 . 5 ∗ ( v ( 2 : end −1 ,2: end −1) + v ( 2 : end −1 ,3: end ) ) ;
195

196 %%
197 s t r e a m s l i c e (X, Y, uc ’ , vc ’ , 2 )
198 axis ([0 ,L,0 ,L] )
199 x l a b e l ( ’X/L ’ )
200 y l a b e l ( ’Y/L ’ )
201

202 %%
203

204 f p 2 = f o p e n ( ’ . / c a v i t y g h o s t 3 2 5 e −4. dat ’ , ’w ’ ) ;


205 f p r i n t f ( fp2 , ’TITLE = ” Cavity F i e l d Data ”\n ’ ) ;
206 f p r i n t f ( fp2 , ’ v a r i a b l e s =”x (m) ”” y (m) ””u (m/ s ) ””v (m/ s ) ”\n ’ ) ;
207 f p r i n t f ( fp2 , ’ zone T=”n=%d”\n ’ , Nc ) ;
208 f p r i n t f ( fp2 , ’ I= %d J= %d\n ’ , Nc , Nc ) ;
209 f p r i n t f ( fp2 , ’DATAPACKING=POINT\n ’ ) ;
210 f o r i =1: l e n g t h ( x )
211 f o r j =1: l e n g t h ( y )
212 f p r i n t f ( fp2 , ’%e %e %e %e \n ’ , x ( i ) , y ( j ) , uc ( i , j ) , vc ( i , j ) ) ;
213 end
214 end

24
This is the function for setting up the boundary conditions -
1 f u n c t i o n [ u , v ] = s e t b c s ( u , v , U l i d , Nx , Ny ,G )
2 %UNTITLED4 Summary o f t h i s f u n c t i o n g o e s h e r e
3 % Detailed explanation goes here
4

7 f o r i =1:Nx
8 u ( i , 2 ) =0;
9 i f G==1
10 u ( i , 1 )=−u ( i , 2 ) ;
11 end
12

13 end %%Bottom w a l l
14 f o r i =1:Nx
15 %v ( i , 1 ) =0;
16 v ( i , 2 ) =0;
17 end
18

19

20 %%S i d e w a l l s
21 f o r j =1:Ny
22 %u ( 1 , j ) =0;
23 u ( 2 , j ) =0;
24 u (Nx , j ) =0;
25 %u (Nx−1, j ) =0;
26 end
27

28 f o r j =1:Ny
29

30 v ( 2 , j ) =0; %no g h o s t c e l l
31 %v ( 2 , : ) =0;
32 i f G==1
33 v ( 1 , j )=−v ( 2 , j ) ;
34 v (Nx , j )=−v (Nx−1, j ) ;
35 end
36 v (Nx−1, j ) =0;
37 %v (Nx− 1 , : ) =0;
38 end
39

40 %%Top w a l l

25
41 f o r i =1:Nx
42 u ( i , Ny)=U l i d ;
43

44 i f G==1
45 u ( i , Ny) =2∗ U l i d − u ( i , Ny−1) ;
46 end
47

48 end
49

50

51 f o r i =1:Nx
52 v ( i , Ny) =0;
53 %v ( i , Ny−1)=0;
54 end
55

56 end
This is the function for the discretized momentum equations -
1 f u n c t i o n [ Hx , Hy ] = HxHy( u , v , Nx , Ny ,G, Re , dx , dy )
2 %UNTITLED2 Summary o f t h i s f u n c t i o n g o e s h e r e
3 % Detailed explanation goes here
4

5 f o r i =2:Nx−1
6 f o r j =2:Ny−1
7

8 r h o u e ( i , j )= ( u ( i +1, j ) + u ( i , j ) ) / 2 ;
9

10 rho u w ( i , j ) = ( u ( i , j ) + u ( i −1, j ) ) / 2 ;
11

12 r h o v n ( i , j ) = ( v ( i , j +1) + v ( i −1, j +1) ) / 2 ;


13

14 r h o v s ( i , j ) = ( v ( i −1, j ) + v ( i , j ) ) / 2 ;
15

16 end
17 end
18

19

20

21 f o r i =2:Nx−1
22 f o r j =2:Ny−1
23

24 AE u ( i , j ) = ( r h o u e ( i , j ) /2 − 1 /( Re∗dx ) ) ∗dy ;

26
25 AW u( i , j ) = (−rho u w ( i , j ) /2 − 1 /( Re∗dx ) ) ∗dy ;
26 AN u( i , j ) = ( r h o v n ( i , j ) /2 − 1 /( Re∗dy ) ) ∗dx ;
27 AS u ( i , j ) = (− r h o v s ( i , j ) /2 − 1 /( Re∗dy ) ) ∗dx ;
28 AP u ( i , j ) = ( r h o u e ( i , j ) /2 + 1/ ( Re∗dx ) ) ∗dy + (−rho u w ( i , j ) /2
+ 1 /( Re∗dx ) ) ∗dy + ( r h o v n ( i , j ) /2 + 1/ ( Re∗dy ) ) ∗dx + (− r h o v s
( i , j ) /2 + 1/ ( Re∗dy ) ) ∗dx ;
29

30 end
31 end
32 i f G==0
33 f o r i =2:Nx−1
34 AS u ( i , 2 ) =(− r h o v s ( i , j ) /2 − 2 /( Re∗dy ) ) ∗dx ;
35 AN u ( i , Ny−1)=( r h o v n ( i , j ) /2 − 2 /( Re∗dy ) ) ∗dx ; %c o r r e c t i o n
due t o g r i d s p a c i n g
36 AP u ( i , 2 ) =( r h o u e ( i , j ) /2 + 1/ ( Re∗dx ) ) ∗dy + (−rho u w ( i , j ) /2 + 1/ (
Re∗dx ) ) ∗dy + ( r h o v n ( i , j ) /2 + 1/ ( Re∗dy ) ) ∗dx + (− r h o v s ( i , j ) /2 +
1 / ( Re∗dy ) ) ∗dx+(1/Re ) ∗( dy/dx ) ;
37 AP u ( i , Ny−1)=AP u ( i , Ny−1)+(1/Re ) ∗( dy/dx ) ;
38 end
39 end
40

41 f o r i =3:Nx−1
42 f o r j =2:Ny−1
43

44 Hx( i , j ) = −(AE u ( i , j ) ∗u ( i +1, j ) + AW u( i , j ) ∗u ( i −1, j ) + AN u( i , j )


∗u ( i , j +1) + AS u ( i , j ) ∗u ( i , j −1) + AP u ( i , j ) ∗u ( i , j ) ) / ( dx∗dy ) ;
45

46 end
47 end
48

49

50 %y mom
51

52 f o r i =2:Nx−1
53 f o r j =2:Ny−1
54

55 AE v ( i , j) = ( rho u e ( i , j ) /2 − 1 /( Re∗dx ) ) ∗dy ;


56 AW v( i , j) = (−rho u w ( i , j ) /2 − 1 /( Re∗dx ) ) ∗dy ;
57 AN v ( i , j) = ( rho v n ( i , j ) /2 − 1 /( Re∗dy ) ) ∗dx ;
58 AS v ( i , j) = (− r h o v s ( i , j ) /2 − 1 /( Re∗dy ) ) ∗dx ;
59 AP v ( i , j) = ( rho u e ( i , j ) /2 + 1/ ( Re∗dx ) ) ∗dy + (−rho u w ( i , j ) /2

27
+ 1 /( Re∗dx ) ) ∗dy + ( r h o v n ( i , j ) /2 + 1/ ( Re∗dy ) ) ∗dx + (− r h o v s
( i , j ) /2 + 1/ ( Re∗dy ) ) ∗dx ;
60 end
61 end
62

63 i f G==0
64 f o r j =2:Ny−1
65 AE v ( 2 , j ) =( r h o u e ( i , j ) /2 − 2 /( Re∗dx ) ) ∗dy ;
66 AW v(Nx−1, j )=(−rho u w ( i , j ) /2 − 2 /( Re∗dx ) ) ∗dy ; %c o r r e c t i o n
due t o g r i d s p a c i n g
67 AP v ( 2 , j ) =( r h o u e ( i , j ) /2 + 1/ ( Re∗dx ) ) ∗dy + (−rho u w ( i , j ) /2 + 1/ (
Re∗dx ) ) ∗dy + ( r h o v n ( i , j ) /2 + 1/ ( Re∗dy ) ) ∗dx + (− r h o v s ( i , j ) /2 +
1 / ( Re∗dy ) ) ∗dx+(1/Re ) ∗( dy/dx ) ;
68 AP v (Nx−1, j )=AP v (Nx−1, j ) +(1/Re ) ∗( dy/dx ) ;
69 end
70 end
71

72 f o r i =2:Nx−1
73 f o r j =3:Ny−1
74

75 Hy( i , j ) = −(AE v ( i , j ) ∗v ( i +1, j ) + AW v( i , j ) ∗v ( i −1, j ) + AN v ( i , j )


∗v ( i , j +1) + AS v ( i , j ) ∗v ( i , j −1) + AP v ( i , j ) ∗v ( i , j ) ) / ( dx∗dy ) ;
76

77 end
78 end
79

80

81

82 end

28

Você também pode gostar