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. discretization. A fractional fractional step algorithm algorithm with pressure pressure correction 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 alidate new CFD codes and results. results. The problem consists consists of a 2D square square lid driven driven cavity cavity with sides sides of L units length. length. The upper upper lid is moving moving with a veloci velocity ty of U lid units. The densi density ty and lid units. 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, velocity, U lid lid , the length scale of 1
L has been used and thus we also scale the time with a time scale of U Llid . The press pressure ure has been been lid 2 scaled with ρU lid , where ρ is the density density of the fluid. The dimensional dimensional form of 2D incompress incompressible ible NS equation is ∂u ∂v + =0 (1) ∂x ∂y
ρ∂u ρ∂ u u ρ∂ u v ∂p µ∂ 2 u µ∂ 2 u + + = − +( + ) ∂t ∂x ∂y ∂x ∂x 2 ∂y 2
ρ∂v ρ∂ v v ρ∂ u v ∂p µ∂ 2 v µ∂ 2 v + + = − +( + ) ∂t ∂y ∂x ∂y ∂x 2 ∂y 2 After non-dimensionalising, the mass and momentum equations can be written as
Where u = number.
u , U lid lid
v =
(2)
(3)
∂u ∂v + =0 ∂x ∂y
(4)
1 ∂ 2 u ∂ 2 u ∂u ∂ uu ∂ uv ∂p + + = − + ( + ) ∂t ∂x ∂y ∂x Re ∂x 2 ∂y 2
(5)
1 ∂ 2 v ∂ 2 v ∂v ∂ vv ∂ uv ∂p + + = − + ( + ) ∂t ∂y ∂x ∂y Re ∂x 2 ∂y 2
(6)
v , U lid lid
x=
x , L
y =
y , L
t=t
= Re = Re
U lid lid , L
p=
p − pref 2 ρU lid
with Re being the Reynolds
ρU lid lid L µ
Boundary Condition No-slip boundary condition has been applied for u for u and and v v velocities at bottom and side walls. So u = 0; v = 0
(7)
= U lid u = U lid ; v = 0
(8)
In the top wall
Numerical Discretization Finite Volume Volume method has been used to numerically discretize 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, points, are given given the same grid location location as the scalar scalar nodes. The same holds holds for v locatio locations ns just below the scalar nodes. From the figure, dxs and dys are the distance between scalar nodes while distances es bet b etwe ween en the u nodes nodes and v nodes nodes respect respectiv ively ely.. Th Thus us the contro controll dxu and dyv are the distanc volume surrounding the u node at (i,j) has a volume of dx of dx s dyv . Applying the Second Ordered Central (SOC) finite volume operator on the the x-momentum equation we have ∂u ( uu||e − uu ( uv||n − uv dxs dyv + (uu − uu||w )dyv + (uv − uv||s )dxs = ∂t 1 ∂u 1 ∂u ∂ u ∂ u ( p((i, j ) − p ))dyv + ( |e − ( |n − − ( p − p((i − 1 − 1,, j ))dy |w )dyv + |s )dxs (9) Re ∂x ∂x Re ∂y ∂y Where e 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
1 , j ) + u + u((i, j ) u(i + 1, 2 + u((i − 1 u(i, j ) + u − 1,, j ) u|w = 2 + v((i − 1 v (i, j + 1) + v − 1,, j + 1) v |n = 2 + v((i − 1 v (i, j − 1) − 1) + v − 1,, j − 1) − 1) v |s = 2 Using Central differencing for the values of the unknown u at the cell faces u|e =
u|e =
1 , j ) + u + u((i, j ) u(i + 1, 2
+ u((i − 1 − 1,, j ) u(i, j ) + u 2 + u((i, j + 1) u(i, j ) + u u|n = 2 + u((i, j − 1) − 1) u(i, j ) + u u|s = 2 The derivatives in the cell faces are u|w =
1, j ) − u ∂u u(i + 1, − u((i, j ) |e = ∂x dxu ∂u u(i, j ) − u − u((i − 1 − 1,, j ) |w = ∂x dxu 1) − u ∂u u(i, j + 1) − u((i, j ) |n = ∂y dys ∂u u(i, j ) − u − u((i, j − 1) − 1) |s = ∂y dys Using these results in (9) we have ut+dt − u − ut p( p(i, j ) − p − p((i − 1 − 1,, j ) = − + H x; dt dxs where u u u u u = −((AE + AW + AP H x = − uE + A uW + AN uN + AS uS + A uP )/dxu dyv
and u =( AE
1 u|e )dyv − 2 Redxu 4
(10)
1 u|w )dyv − 2 Redxu 1 v |n u =( )dxs AN − 2 Redys
u =( AW
u =( AS
1 v |s )dxs − 2 Redys
1 1 1 1 u|e u|w v |n v |s + )dyv + (− (− + )dyv + ( + )dxs + (− (− + )dxs 2 2 2 2 Redxu Redxu Redyu Redys Similarly for the y-momentum equation u =( AP
vt+dt − v − vt p( p(i, j ) − p − p((i, j − 1) − 1) = − + H y ; dxu dys = − dt dys
(11)
where v v v + AW + AP H y = −( −(AvE vE + A vW + AN vN + AvS vS + A vP )/dxu dys
and AvE = (
1 u|e )dys − 2 Redxs
1 u|w )dys − 2 Redxs 1 v |n v =( )dxu AN − 2 Redyv
v =( AW
AvS = ( v =( AP
1 v |s )dxu − 2 Redyv
1 1 1 1 u|e u|w v |n v |s + )dys + (− (− + )dys + ( + )dxu + (− (− + )dxu 2 2 2 2 Redxs Redxs Redyv Redyv
Here Here P stands stands for the point in consid considera eratio tion n (i,j). (i,j). Here Here E, W, N, S stand stand for East East (i+1,j (i+1,j), ), West(i-1,j), North(i,j+1) and South(i,j-1) respectively. Since we are using a uniform grid dx u = dy = dy v = dx s = dy s
5
Algorithm and Solution procedure In this algorithm an intermediate velocity filed (˜u and v˜) are first computed using H using H x and and H H y 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 ˜ = u = u n + ∆t(3H (3H n − H n 1 ) u 2 −
un+1 = u˜ − ∆ − ∆tt∇ p
(12)
(13)
12 and 13 are called the predictor and corrector step respectively. A thing to note here is that the pressure pressure is unknown at (n+1)th level. level. So to find that the solution of 13 is used in the continui continuity ty equation at (n+1)th level to form a Pressure Poisson’s equation. The discretized continuity equation is (u(i + 1, 1, j ) − u ))dyv + (v (i, j + 1) − 1) − v ))dxu = 0 − u((i, j ))dy v((i, j ))dx
(14)
The pressure pressure equations are 1, j ) − p p( p(i + 1, − p((i, j ) 1, j )n+1 = u˜(i + 1, 1 , j ) − ∆ ) u(i + 1, − ∆tt( dxs p( p(i, j ) − p − p((i − 1 − 1,, j ) ) u(i, j )n+1 = u˜(i, j ) − ∆ − ∆tt( dxs 1) − p p( p(i, j + 1) − p((i, j ) 1) − ∆ ∆tt( ) v (i, j + 1) n+1 = v˜(i, j + 1) − dys − p((i, j − 1) − 1) p( p(i, j ) − p ) v (i, j )n+1 = v˜(i, j ) − ∆ − ∆tt( dys Substituting in the continuity equation we have a poisson like equation A pP pP +
A pl pl = S i,j i,j
l dyv u where A pE = A pW = dy v /dxs ; A pN = A pS = dx u /dys and A pP = (−2 dx ) − 2 dx dys s The source term is
S i,j i,j =
1 [((˜ )(i + 1, 1 , j ) − ˜ ))dyv + (˜ ( v˜(i, j + 1) − 1) − ˜ ˜v (i, j ))dx ))dxu ] u)(i − ˜ u(i, j ))dy ∆t
The algorithm thus follows the following steps -
6
(15)
1. Solve for the intermediate intermediate velocity field without 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 interme intermediate diate velocity velocity field as in Equation(15). Equation(15). In this case a Gauss-Seid Gauss-Seidel el 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 process until steady steady state is reached. reached. The system system can be b e 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, H equation, H x has a different value at the top and bottom wall boundaries since the coefficients coefficients change their values values due to the grid spacing becoming half of the original. original. At the bottom wall 2 v |s u − = (− ) AS 2 Redys 1 1 1 1 u|e u|w v |n v |s dyv + )dyv + (− + )dyv + ( + )dxs + (− + )dxs + 2 2 2 2 Redxu Redxu Redyu Redys Redxu Similarly at the top wall u =( AP
2 v |n ) − 2 Redys dyv u u = A P + AP 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 =( AS
2 u|e )dys − 2 Redxs dxu v v = A P + AP Redyv
AvE = (
At the west wall v = (− AW
2 u|w )dys − 2 Redxs 7
dxu Redyv These These adjust adjustmen mentt wouldn’ ouldn’tt be necess necessary ary if one uses ’Ghost ’Ghost Cells’. Cells’. The code writte written n 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 v v = A P + AP
A pN = A pS = dx u /dys A pE = dy v /dxs A pW = 0 A pP = (
dyv dxu ) − 2 dxs dys
Thus A Thus A pW = 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 × 64 and 128× 128 × 128 cells. Reynold’s number of 100 was selected. The L The L 2 norm of the change in u-velocity was plotted against the non-dimensional solution time.
8
10
-1
Error history for 32*32 cells
-2
y10 t i c o l e10-3 v u n i -4 e10 g n a h -5 c10 f o m r10-6 o n 2 L -7
dt=6.25e-3 dt=3.125e-3 dt=0.0125
10
10
-8
0
5
10
15
Solution time
Figure 3: Error history for 32 × 32 × 32 32 cells, Re=100
9
20
10
-1
y10-2 t i c o l e -3 v10 u n i -4 e10 g n a h -5 c10 f o m r10-6 o n 2 L -7
dt=3e-3 dt=1e-3 dt=5e-4
10
10
-8
-5
0
5
10
15
20
Nondimensional solution time
Figure 4: Error history for 64 × 64 × 64 64 cells, Re=100
10
25
Figure 5: Error history for 128 × 128 × 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 step(∆ t). It is to be noted noted howev however er that although although the solution solution time might might seem seem to reduce with lesser grids and lesser time steps, the actual simulation time in the computer is however more. The number number of iterations iterations is more for more number number of grid points, as well as for lesser time steps. steps. This can be seen in the next plot of CPU time. time. The CPU timw timw has been plotted in a semi-log scale against the time-step (dt)
11
32*32 64*64 128*128 10
4
e m i t U P C
10
3
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 ables I and II of the seminal seminal paper by Ghia-G Ghia-Ghia hia-Sh -Shin[ in[1]. 1]. All the three three grid sizes sizes were used with a representative time step of 5e-4.
12
1
128*128 ghia data 64*64 32*32
0.8
0.6
y
0.4
0.2
0 -0.2
0
0.2
0.4
0.6
u-velocity in mid plane
Figure 7: u-velocity in the mid-plane, Re=100
13
0.8
0.15
128*128 ghia data 64*64 32*32
0.1
e 0.05 n a l p d 0 i m n i y-0.05 t i c o l e -0.1 v v -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 × 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 × 128 128 grid size with time step of 5e-4 is shown below -
14
1
1
u(m/s)
v(m/s)
0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 -0.1 -0.2
0.8
0.6 ) m ( y 0.4
0.6 ) m ( y 0.4
0.2
0
0.3 0.2 0.1 0 -0.1 -0.2 -0.3 -0.4 -0.5
0.8
0.2
0
0.2
0.4
0.6
x(m)
0.8
0
1
(a) u velocity contour
0
0.2
0.4
0.6
x(m)
0.8
1
(b) v velocity contour
Figure 9: A representative flow field at Re=100, 128 × 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 time-step is 10 times the maximum 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 solution took significan significantly tly fewer fewer time steps(20655 steps(20655)) than the Re = 100 case to conver converge. ge. 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.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 -0.1 -0.2 -0.3
0.8
0.6
) m ( y 0.4
1
v(m/s) 0.3 0.2 0.1 0 -0.1 -0.2 -0.3 -0.4 -0.5 -0.6
0.8
0.6 ) m ( y 0.4
0.2 0.2
0
0
0.2
0.4
0.6
0.8
1
x(m)
(a) u velocity contour
0
0
0.2
0.4
0.6
x(m)
0.8
(b) v velocity contour
× 128 cells Figure 10: A representative flow field at Re=1000, 128 × 128 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 advection has slightly slightly increased. increased. more momentum momentum is diffused diffused in the cavity cavity, and the primary vortex vortex core is lower. lower. The secondary secondary vortices vortices are also bigger. At Re=1000 Re=1000 cases, because of the large momentum of the lid, shearing the flow, a lot of momentum is diffused inside the cavity cavity.. This results in bette b etterr circulation circulation signified by the primary primary vortex core being lower. lower. We can also see the secondary vortices are larger in size, signifying higher backflow because of the no-slip condition condition at the wall. The secondary secondary vortices vortices almost nullify nullify the effect effect of the primary vortex. vortex. We can also see a little circulation circulation developin developingg at the left hand top corner. The contours contours almost looks the same if bottom lid was moving in opposite direction with U lid lid 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
1
128*128 ghia data
0.8
0.6
y
0.4
0.2
0
-0.2
0
0.2
0.4
0.6
u-velocity in mid-plane
Figure 11: u-velocity in the mid-plane, Re=1000
17
0.8
128*128 ghia data
0.2
e n a l p d i 0 m n i y t i c o l -0.2 e v v -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 solutions like [1]. The code written is somewhat slow because because of the Gauss-Seidel Gauss-Seidel routine routine used to solve for pressure, but since this was a class project, due to limited amount of time, other methods were were not implemented. implemented. ADI and multigrid multigrid methods would definitely definitely make the code a lot faster. faster. 18
Parallel Parallelizing izing it can make make it a lot faster faster as well. well. Lastly Lastly, since the simulations simulations were were run on a basic laptop with only one Intel(R)Core(TM)i54210U@1:70GHz2:40GHz processor, the CPU times were large. large. They can be definitely definitely much much smaller on a faster faster system. Overall Overall though, though, the solutions 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 DYNAMIC DYNAMICS S and HEAT HEAT TRANSFER TRANSFER.. I wou would ld like like to thank Dr. Danesh 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 flow using the navier-stokes 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 2
%S %S a i k a t M u kh kh e rj rj ee ee %F %F i n a l P r o j e c t CFD
3 4 5 6 7
c le ar a l l ; c l c ; %% I n i t i a l P a ra ra me m e te t e rs rs Nc=32; %No o f c e l l s
8 9 10 11
Nx=Nc+2; Ny=Nc+2; n=Nc+2;
12 13
N=(Nc) ∗ (Nc) ;
14 15 16 17
x = 0 : 1 / ( N c− c −1 ) : 1 ; y = 0 : 1 / ( N c− c −1 ) : 1 ; [ X , Y ] = meshgrid ( x , y ) ;
18
19
19 20
dt=5e − 4;
%t i me me s t e p
21 22 23 24
dx=1/Nc; dy=1/Nc;
25 26 27 28 29 30
Re=100; %reynold ’ s no U lid=1; %t o p l i d v e l o c i t y L=1; G=0; %S wi w i tc tc h f o r g ho h o st st c e l l s
31 32 33
34 35 36
nmax=500000; t o l er e r m a i n = 1 e − 8; state t o l e r p = 1e 1 e − 5; isConverged=0; i t i n i t =1;
% t o l e r e n c e f o r u an a n d v c ha h a n ge ge t o r ea e a ch c h s te t e ad ad y % t o l e r e n c e f o r p r e ss s s u r e p o is i s so s o n ’ s e q u at a t io io n
37 38 39 40 41 42 43 44 45
%% i n i t i a l i z e u= z e r o s (Nx, s (Nx, Ny) ; u t i l d a= a = z e r o s (Nx, s (Nx, Ny) ; u o l d =z = z e r o s (Nx, s (Nx, Ny) ; v=z v=z e r o s (Nx, s (Nx, Ny) ; v t i l d a =z =z e r o s (Nx, s (Nx, Ny) ; v o l d =z = z e r o s (Nx, s (Nx, Ny) ;
46 47 48 49 50
p= z e r o s (Nx, s (Nx, Ny) ; p new=z new= z e r o s (Nx, s (Nx, Ny) ; u old=u; v old=v;
51 52 53 54 55
f p 1 = f o p e n ( n ( ’ . / h i s t o r y g h o s t 3 2 5 e − 4.dat ’ , ’ , ’ ’w w ’ ) ; f p r i n t f ( f p 1 , ’ TI TITLE = ” E r r o r H i s t o r y ” \ n ’ ) ’ ) ; f p r i n t f ( f p 1 , ’ 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 or o r u ” ” E r r or o r v ” \ n ’ ) ’ ) ;
56 57 58
tic 20
59
f o r i t e r = i t i n i t : nm nmax
60 61
%% %% B o un un da da ry ry c o n d i t i o n s
62 63 64
%Velocity [ u , v ]= ]= s e t b c s ( u , v , U l i d , Nx Nx , Ny Ny , G) G) ;
65 66
%% x mom mom & y mom mom
67 68 69
[ Hx prev , Hy prev]=H prev]=HxHy( u old , v ol d , Nx, Ny,G, Re , dx , dy) ; [ Hx, Hy]=H y]=Hx xHy( u ol d , v ol d ,Nx, Ny, G,Re , dx , dy) ;
70 71
%% I n te t e r m ed e d i a te te 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 76
u t i l d a ( 2 : Nx Nx − 1,1:Ny− 1,1:Ny−1 ) = u ( 2 : N x− x − 1,1:Ny− 1,1:Ny−1 ) + d t ∗Hx(2:Nx− Hx(2:Nx− 1,1:Ny− 1,1:Ny−1) ; v t i l d a ( 1 : Nx Nx − 1,2:Ny− 1,2:Ny−1 ) = v ( 1 : Nx Nx − 1,2:Ny− 1,2:Ny−1 ) + d t ∗Hy(1:Nx− Hy(1:Nx− 1,2:Ny− 1,2:Ny−1) ;
77 78
else
79 80 81
82
u t i l d a ( 2 : Nx Nx − 1,1:Ny− 1,1:Ny−1 ) = u ( 2 : N x− x − 1,1:Ny− 1,1:Ny−1 ) + d t / 2 ∗ ( 3 ∗ Hx(2:Nx− Hx(2:Nx− 1,1:Ny− 1,1:Ny−1) − Hx prev ( 2: Nx − 1,1:Ny− 1,1:Ny− 1)) ; v t i l d a ( 1 : Nx Nx − 1,2:Ny− 1,2:Ny−1 ) = v ( 1 : Nx Nx − 1,2:Ny− 1,2:Ny−1 ) + d t / 2 ∗ ( 3 ∗ Hy(1:Nx− Hy(1:Nx− 1,2:Ny− 1,2:Ny−1) − Hy prev ( 1: Nx − 1,2:Ny− 1,2:Ny− 1)) ;
83 84 85 86
en d
87 88 89
%% p r e s s u r e p o i s s o n e q u a ti ti o n
90 91 92 93 94 95 96 97
A S p = z e r o s (Nx, s (Nx, Ny) ; A S p ( : , 3 : Nx N x−1) = −dx/dy; AN p = z e r o s (Nx, s (Nx, Ny) ; AN p ( : , 2 : Ny Ny−2) = −dx/dy; AW p = z e r o s (Nx, s (Nx, Ny) ; AW p(3:Nx− p(3:Nx − 1 , : ) = −dy/dx; 21
98 99 100
AE p = z e r o s (Nx, s (Nx, Ny) ; A E p ( 2 : N x− x − 2 , : ) = −dy/dx; AP p = (AW (AW p + AE p + AS p + AN p) ;
101 102 103 104 105
106 107
f o r i =2:Nx− =2:Nx−1 f o r j =2:Ny =2:Ny−1 S( i , j )= (( u ti l d a ( i +1,j ) − u ti l d a ( i , j ) ) ∗ d y + ( v t i l d a ( i , j + 1 ) − v t i l d a ( i , j ) ) ∗ dx)/ dt ; %Source ter term m en d en d
108 109 110 111 112
%% G au a u ss ss S e i d e l w=1; re s =100;
113 114
w h i l e ( e ( re s > t o l e r p )
115 116 117
f o r i =2:Nx− =2:Nx−1 f o r j =2:Ny =2:Ny−1
118 119 120
p new( i , j ) = (1/AP p( i , j ) ) ∗ (AP p ( i , j ) ∗(1 −w) ∗ p( i , j ) + w ∗ ( − 1, j ) + AE p( i , AS p ( i , j ) ∗ p new( i , j − j −1) 1) + AW p( i , j ) ∗ p new( i −1, j ) ∗ p( i +1, j ) + AN p( i , j ) ∗ p( i , j +1) +1) + S( i , j ) ) ) ;
121
122
123
d i f f ( i , j )=(AE p ( i , j ) ∗ p ( i +1, j ) + AW p( i , j ) ∗ p ( i − i −1, 1, j ) + AN p( i , j ) ∗ p( i , j+1) + AS p ( i , j ) ∗ p ( i , j − j −1) 1) − AP p( i , j ) ∗ p( i , j ) + S( i , j ) ) .ˆ 2 ;
124
en d
125
en d
126 127
p=p new ne w ; r e s =((1/N) =((1/N) ∗ sum ( d i f f ( ( : ) ) ) ˆ 0 . 5 ;
128 129 130 131
en d
132 133
%e %e n d g a us us s s e i d e l 22
134 135 136 137 138 139 140 141 142
%% Time Time upd ate f o r i =2:Nx− =2:Nx−1 f o r j =2:Ny =2:Ny−1 u( i , j )=u t i l d a ( i , j ) − d t ∗ (p ( i , j )− )−p ( i − i −1, 1, j ) )/dx ; v( i , j )=v t i l d a ( i , j ) − d t ∗ (p ( i , j )− )−p ( i , j − j −11 ) ) / d y ; en d en d
143 144 145
e r r u = ( u− u−u o l d ) . ˆ 2 ; e r r o r u = (sum ( sum ( e r r u ( : ) ) /N /N ) ˆ 0 . 5 ;
146 147 148
e r r v = ( v− v−v o l d ) . ˆ 2 ; e r r o r v = (sum ( sum ( e r r v ( : ) ) /N /N ) ˆ 0 . 5 ;
149 150 151 152 153
u old=u; v old=v;
154 155 156 157 158 159
%% N o r m a l i ze ze p r e s s u r e p=p−p ( 5 , 5 ) ; %% t=dt ∗ ( i t e r − r −1) 1) ;
160 161 162 163
i f ( (mo (mod( it er , 1 )==0 )==0) | | ( i t e r == == i t i n i t ) ) f p r i n t f ( f p 1 , ’% ’%d d %e %e %e \ n ’ ’ , i t e r , t , e r r o r u , e r r o r v ) ; f p r i n t f ( ’%d %e %e %e \ n ’ , i t e r , t , e r ro ro r u , e r r o r v ) ;
164 165 166
en d cond = max max(( e r r o r u , e r r o r v ) ;
167 168 169 170 171 172
i f ( ( cond cond<
173 174
23
175 176 177 178
%End o f t im i m e i t e r a t i o n l o op op en d toc
179 180
%% p r i n t o n s c r e e n
181 182 183 184 185
186
f c l o s e ( e ( fp 1 ) ; toc i f i s C o n v e r g e d == 0 f p r i n t f ( ’ S o l u t i o n f a i l e d t o b e c o m e s t ea e a d y i n %d %d i t e r a t i o n s ! ! ! ’ , nmax nmax ) ; en d
187 188 189 190
i f i s C o n v e r g e d == 1 f p r i n t f ( ’ S o l u t i o n b e c a m e s t e ad a d y i n %d i t e r a t i o n s ! ! ! ’ ’ , i t e r ) ; en d
191 192 193 194
%% v e lo l o c i t y i n c e l l c e nt n t e rs rs u c = 0 . 5 ∗ ( u ( 2 : en end d − 1 , 2 : en end d −1 ) + u ( 3 : en end d , 2 : en d −1)) ; v c = 0 . 5 ∗ ( v ( 2 : en end d − 1 , 2 : en end d −1 ) + v ( 2 : en end d − 1 , 3 : en d ) ) ;
195 196 197 198 199 200
%% st re a ms li ce (X,Y, (X,Y, uc ’ , vc ’ ,2 ) a x i s ( s ( [ 0 , L , 0 , L ] ) x l a b e l ( l ( ’X ’X/L /L ’ ) y l a b e l ( l ( ’Y ’Y/L /L ’ )
201 202
%%
203 204 205 206 207 208 209 210 211 212 213 214
f p 2 = f o p e n ( n ( ’ . / c a v i t y g h o s t 3 2 5 e − e − 4.dat ’ , ’ , ’ w ’ ) ; f p r i n t f ( f p 2 , ’ TI TITLE = ” C a v i t y F i e l d D at at a ”\ ” \ n ’ ) ’ ) ; f p r i n t f ( f p 2 , ’ va ri a bl es =”x(m x(m)) ””y( ””y(m m) ””u( ””u(m m/s ) ””v( ””v(m m/s ) ” \ n ’ ) ’ ) ; f p r i n t f ( f p 2 , ’ z o ne T=” =”n n=%d” \ n ’ , ’ , Nc) ; f p r i n t f ( f p 2 , ’ I= %d %d J= %d %d \ n ’ ’ ,Nc, ,Nc, Nc) ; f p r i n t f ( f p 2 , ’DATAPACKING=POINT\ ’DATAPACKING=POINT\ n ’ ) ’ ) ; f o r i =1: l e n g t h ( h ( x ) f o r j = 1 : l e n g t h ( h ( y ) f p r i n t f ( f p 2 , ’% ’%e %e %e %e \ n ’ , ’ , x( i ) , y( j ) , uc( i , j ) , vc ( i , j ) ) ; en d en d 24
This is the function for setting up the boundary conditions 1 2 3
f u n c t i o n [ u , v ] = s e t b cs c s ( u , v , U l i d , Nx Nx , Ny Ny ,G ,G ) %U %UNTITLED4 Su Summary of th is fu nc ti on goes here % D et e t ai a i le l e d e xp x p la l a na n a ti t i on o n g o es e s h er er e
4 5 6 7 8 9 10 11
f o r i =1 :Nx u( i ,2 ) =0; i f f G==1 u ( i , 1 ) =−u( i , 2 ) ; en d
12 13 14 15 16 17
en d f o r i =1 :Nx %v ( i , 1 ) =0; v( i , 2 ) =0; en d
%%Bottom w a l l
18 19 20 21 22 23 24 25 26
%% %%Si de wa ll s f o r j =1:Ny =1:Ny %u ( 1 , j ) =0; u (2 , j ) =0; u( Nx, j ) =0; %u(Nx− %u(Nx− 1 , j ) = 0; 0; en d
27 28
f o r j =1:Ny =1:Ny
29 30 31 32 33 34 35 36 37 38
v (2 , j ) =0; %n o g ho h o st st c e l l %v ( 2 ,: ) =0 =0;; i f f G==1 v (1 , j )=−v (2 , j ) ; v (Nx, j )=−v(Nx− v(Nx− 1, j ) ; en d v(Nx− v(Nx− 1 , j ) = 0; 0; %v(Nx− %v(Nx − 1 ,:)=0; en d
39 40
%%To Top p wall 25
41 42
f o r i =1 :Nx u( i ,Ny) ,Ny)=U =U li d ;
43 44 45 46
i f f G==1 u ( i , Ny) =2∗ =2∗ U l i d − u( i , Ny−1) ; en d
47 48
en d
49 50 51 52 53 54
f o r i =1 :Nx v( i ,Ny) ,Ny) =0; %v( i ,Ny− ,Ny−1)=0; en d
55 56
en d This is the function for the discretized momentum equations -
1 2 3
f u n c t i o n [ Hx , Hy Hy ] = HxHy ( u , v , Nx Nx , Ny Ny , G , Re Re , d x , d y ) %U %UNTITLED2 Su Summary of th is fu nc ti on goes here % D et e t ai a i le l e d e xp x p la l a na n a ti t i on o n g o es e s h er er e
4 5 6
f o r i =2:Nx− =2:Nx−1 f o r j =2:Ny =2:Ny−1
7
rh o u e ( i , j )= (u ( i +1, j ) + u ( i , j ) ) /2 ;
8 9
r h o u w ( i , j ) = ( u ( i , j ) + u ( i −1, − 1, j ) ) /2 ;
10 11
r h o v n ( i , j ) = ( v ( i , j + 1 ) + v ( i −1 − 1 , j + 1 ) ) / 2 ;
12 13
r h o v s ( i , j ) = ( v ( i −1, − 1, j ) + v( i , j ) ) /2 ;
14 15
en d
16 17
en d
18 19 20 21 22
f o r i =2:Nx− =2:Nx−1 f o r j =2:Ny =2:Ny−1
23 24
AE u( i , j ) = ( rh o u e ( i , j ) /2 − 1/(Re∗ 1/(Re ∗ dx) ) ∗ dy ; 26
AW u( i , j ) = ( − r h o u w ( i , j ) / 2 − 1/(Re∗ 1/(Re ∗ dx ) ) ∗ dy ; AN u( i , j ) = ( rh o v n ( i , j ) /2 − 1/(Re∗ 1/(Re ∗ dy) ) ∗ dx ; AS u( i , j ) = ( − r h o v s ( i , j ) / 2 − 1/(Re∗ 1/(Re ∗ dy ) ) ∗ dx ; AP u( i , j ) = ( rh o u e ( i , j ) /2 + 1/(Re∗ 1/(Re ∗ dx) ) ∗ d y + (− (− r h o u w ( i , j ) / 2 + 1 / ( R e∗ e ∗ dx ) ) ∗ d y + ( r h o v n ( i , j ) / 2 + 1 / ( Re Re ∗ dy) ) ∗ d x + (− r ho ho v s ( i , j ) /2 + 1/(Re∗ 1/(Re∗ dy ) ) ∗ dx ;
25 26 27 28
29
en d
30 31 32 33 34 35
36
37 38 39
en d i f f G==0 f o r i =2:Nx− =2:Nx−1 AS u( i , 2 )=( )=(− r h o v s ( i , j ) / 2 − 2/(Re∗ 2/(Re ∗ dy) ) ∗ dx ; AN u( i , Ny−1)= 1)=(r ho v n ( i , j ) /2 − 2/(Re∗ 2/(Re ∗ dy ) ) ∗ dx ; %correction d u e t o g r i d s p ac a c in in g AP u ( i , 2 ) = ( r h o u e ( i , j ) / 2 + 1 / ( Re Re ∗ dx) ) ∗ d y + (− r h o u w ( i , j ) / 2 + 1 / ( Re∗ Re ∗ dx ) ) ∗ d y + ( r h o v n ( i , j ) / 2 + 1 / ( Re Re ∗ dy ) ) ∗ d x + (− r h o v s ( i , j ) / 2 + 1/(Re∗ 1/(Re ∗ dy ) ) ∗ dx+(1/Re) ∗ (dy/dx) ; AP u( i , Ny−1)= 1)=AP u ( i , Ny− Ny−1)+(1/Re) ∗ (dy/dx) ; en d en d
40 41 42
f o r i =3:Nx− =3:Nx−1 f o r j =2:Ny =2:Ny−1
43
Hx( i , j ) = −(AE (AE u ( i , j ) ∗ u ( i +1, j ) + AW u( i , j ) ∗ u ( i − i −1, 1, j ) + AN u( i , j ) ∗ u( i , j+1) + AS u ( i , j ) ∗ u ( i , j − j −1) 1) + AP u ( i , j ) ∗ u( i , j ) ) /( dx∗ dx ∗ dy) ;
44
45
en d
46 47
en d
48 49 50
%y mom
51 52 53
f o r i =2:Nx− =2:Nx−1 f o r j =2:Ny =2:Ny−1
54 55 56 57 58 59
AE v( i AW v( i AN v( i AS v( i AP v( i
,j) ,j) ,j) ,j) ,j)
= = = = =
( rh o u e ( i , j ) /2 − 1/(Re∗ 1/(Re ∗ dx) ) ∗ dy ; ( − r h o u w ( i , j ) / 2 − 1/(Re∗ 1/(Re ∗ dx ) ) ∗ dy ; ( rh o v n ( i , j ) /2 − 1/(Re∗ 1/(Re ∗ dy) ) ∗ dx ; ( − r h o v s ( i , j ) / 2 − 1/(Re∗ 1/(Re ∗ dy ) ) ∗ dx ; ( rh o u e ( i , j ) /2 + 1/(Re∗ 1/(Re ∗ dx) ) ∗ d y + (− (− r h o u w ( i , j ) / 2 27
+ 1 / ( R e∗ e ∗ dx ) ) ∗ d y + ( r h o v n ( i , j ) / 2 + 1 / ( Re Re ∗ dy) ) ∗ d x + (− r ho ho v s ( i , j ) /2 + 1/(Re∗ 1/(Re∗ dy ) ) ∗ dx ; en d
60 61
en d
62 63 64 65 66
67
68 69 70
i f f G==0 f o r j =2:Ny =2:Ny−1 AE v( 2 , j )=(rh o u e ( i , j ) /2 − 2/(Re∗ 2/(Re ∗ dx) ) ∗ dy ; AW v( Nx− Nx− 1, j )=( )=(− r h o u w ( i , j ) / 2 − 2/(Re∗ 2/(Re ∗ dx) ) ∗ dy ; %correction d u e t o g r i d s p ac a c in in g AP v( 2 , j )=(rho u e ( i , j ) /2 + 1/(Re∗ 1/(Re ∗ dx) ) ∗ d y + (− r h o u w ( i , j ) / 2 + 1 / ( Re∗ Re ∗ dx ) ) ∗ d y + ( r h o v n ( i , j ) / 2 + 1 / ( Re Re ∗ dy ) ) ∗ d x + (− r h o v s ( i , j ) / 2 + 1/(Re∗ 1/(Re ∗ dy ) ) ∗ dx+(1/Re) ∗ (dy/dx) ; AP v( Nx− 1, j )= )=AP v (Nx− (Nx− 1, j ) +(1/R +(1/Re) e) ∗ (dy/dx) ; en d en d
71 72 73
f o r i =2:Nx− =2:Nx−1 f o r j =3:Ny =3:Ny−1
74
Hy( i , j ) = −(AE v ( i , j ) ∗ v ( i +1, j ) + AW v( i , j ) ∗ v ( i − i −1, 1, j ) + AN v( i , j ) j −1) 1) + AP v( i , j ) ∗ v( i , j ) ) /( dx∗ dx ∗ dy) ; ∗ v( i , j+1) + AS v ( i , j ) ∗ v ( i , j −
75
76
en d
77 78
en d
79 80 81 82
en d
28