Académique Documents
Professionnel Documents
Culture Documents
PROJECT#5
UIUC
TABLE OF CONTENTS
PROBLEM DESCRIPTION ......................................................................................................................... 3
NUMERICAL SCHEME.............................................................................................................................. 6
SUMMARY ................................................................................................................................................ 26
2
PROBLEM DESCRIPTION
The aim of this project is to develop a computer program to solve the two-dimensional Navier-Stokes
equations for an elliptic flow in a square cavity with top wall sliding at a given velocity. The flow is assumed
to have constant density and viscosity and the NS unsteady equations are shown below:
u v
0
x y
u u u P 2u 2u
u v 2 2 (1.1)
t x y x x y
v v v P 2v 2v
u v 2 2
t x y y x y
u, v are the Cartesian velocities, x, y the Cartesian coordinates; P the pressure and , the fluid density
and dynamic viscosity. respectively.
An explicit time discretization scheme is used for convection and diffusion terms, and the Pressure-Poison
equation for determining the pressure field at the new time step. We also used the collocated scheme for
the velocities but the pressure equation was formulated to avoid the checkerboard splitting.
The square domain has side length of 1 meter and will be discretized in a grid of 40 x 40 intervals, or 42 x
42 finite difference points. The lid velocity is 1 m/s, the density of 1 kg / m3 and the viscosity is
adjusted to match the Re number of: 50, 100 and 400. Our objective is also analyze how the velocity field
and the pressure distribution, as well the circulation zone changes as function of the Reynolds number, and
to check whether our code is working we will use the results present at literature and the output of the 1st
fluent project. The following figure shows the domain, with the ghost nodes and the wall region highlighted:
3
In the cavity fluid flow the Re number is defined as follows:
V L
Re (1.2)
Where L is the cavity side length and V the top wall velocity.
As consequence of our implicit scheme, we also have to ensure that the time step is lower than a limit value
in order to make the simulation stable:
1
t (1.3)
u v 2 2
2 2
x t x y
DISCRETIZED EQUATIONS
Following we present the discretized equations using the finite difference method, with first order accuracy
in time and second order in space. The superscript n is used to represent the time instant, and the subscript
i, j 1/ 2 refers to the value on the face of the element (assuming constant mesh size - x and y are
the same in all domain).
Discretized equations:
v ni , j 1/2
n
v
n
v i , j 1/2
(1.4)
y i , j y
u
n
u n
i , j 1/2 u n i , j 1/2
(1.5)
x i , j x
n 1
v
n
v v ni, j
i, j
(1.6)
t i , j t
n 1
u
n
u u ni, j
i, j
(1.7)
t i , j t
4
Convective terms:
u
n
uin, j n
u
x i , j 2x
ui 1, j uin1, j (1.8)
n
u vn
v i , j uin, j 1 uin, j 1 (1.9)
y i , j 2x
v
n
uin, j n
u
x i , j 2 x
vi 1, j vin1, j (1.10)
n
v vin, j n
v vi , j 1 vin, j 1 (1.11)
y i , j 2x
Viscous terms:
u n 2uin, j uin1, j
n
2u
2 i 1, j (1.12)
x i , j x 2
u n 2uin, j uin, j 1
n
2u
2 i , j 1 (1.13)
y i , j y 2
Pressure contribution:
P
n
Pi n1, j Pi n1, j
(1.16)
x i , j 2x
Pi ,nj 1 Pi ,nj 1
n
P
(1.17)
y i , j 2y
5
NUMERICAL SCHEME
The implemented code initially receives the following parameters:
Domain dimensions
Number of grid elements
Fluid properties
Lid velocity
With these parameters, perform the following steps before iterating in time:
uˆi , j uin, j
Cxn Dxn
t
uin, j vin, j
C n
x
2x
u u 2x uin, j 1 uin, j 1
n
i 1, j
n
i 1, j (1.18)
vˆi , j vin, j
C yn Dyn
t
uin, j vin, j
C yn
2x
vin1, j vin1, j 2x
v n
i , j 1 vin, j 1 (1.19)
6
II. Evaluate û and v̂ at i 1/ 2, j 1/ 2 positions by averaging:
III. Evaluate discrete continuity errors in û and v̂ at (i,j) and solve the Pressure-Poisson eq. using SOR
solver:
Pi 1, j 2 Pi , j Pi 1, j Pi , j 1 2 Pi , j Pi , j 1
S m i , j (1.21)
x 2 y 2
Where
n 1 n 1
IV. Correct grid nodes velocities at u i , j , v i , j using uˆi , j , vˆi , j and Pi 1, j , Pi 1, j , Pi , j 1 , Pi , j 1 :
t
uin, j 1 uˆi , j
2 x
Pi n1,1j Pi n1,1j
(1.22)
t
vin, j 1 vˆi , j
2 y
Pi,nj11 Pi,nj11
V. Update the velocities at the ghost nodes, so they can be used in the next iteration.
VI. March to next time step
RESULTS OF CALCULATIONS
The following plots shows the results for Re = 50, 100 and 400:
7
Re = 50
A. Contours of u and v velocity for the three Reynolds numbers at 4 time intervals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.6 0.8 1
x[m]
11
B. Contours of u and v velocity at steady state for the three Reynolds numbers.
u[m/s]
1 0.9
0.9 0.8
0.8 0.7
0.6
0.7
0.5
0.6
0.4
y[m]
0.5
0.3
0.4
0.2
0.3
0.1
0.2
0
0.1
-0.1
0
0 0.2 0.4 0.6 0.8 1
x[m]
v[m/s]
1 0.8
0.9 0.6
0.8 0.4
0.7 0.2
0.6 0
-0.2
y[m]
0.5
-0.4
0.4
-0.6
0.3
-0.8
0.2
-1
0.1
-1.2
0
0 0.2 0.4 0.6 0.8 1
x[m]
12
C. Streamlines or velocity vectors for the three Re at steady state.
1
0.9
0.8
0.7
0.6
y[m]
0.5
0.4
0.3
0.2
0.1
0
0 0.2 0.4 0.6 0.8 1
x[m]
D. u-velocity profile on vertical centerline and v-velocity profile on horizontal centerline for
the three Re at steady state.
13
Re = 100
A. Contours of u and v velocity for the three Reynolds numbers at 4 time intervals separated
reasonably well apart.
I. T = 1% of steady state time
u[m/s]
1 0.9
0.9 0.8
0.8 0.7
0.6
0.7
0.5
0.6
0.4
y[m]
0.5
0.3
0.4
0.2
0.3
0.1
0.2
0
0.1
-0.1
0
0 0.2 0.4 0.6 0.8 1
x[m]
v[m/s]
1 0.8
0.9 0.6
0.8 0.4
0.7 0.2
0.6 0
-0.2
y[m]
0.5
-0.4
0.4
-0.6
0.3
-0.8
0.2
-1
0.1
-1.2
0
0 0.2 0.4 0.6 0.8 1
x[m]
14
II. T = 50% of steady state time
u[m/s]
1 0.8
0.9 0.7
0.8 0.6
0.7 0.5
0.6 0.4
y[m]
0.5 0.3
0.4 0.2
0.3 0.1
0.2 0
0.1 -0.1
0 -0.2
0 0.2 0.4 0.6 0.8 1
x[m]
v[m/s]
1
0.4
0.9
0.2
0.8
0
0.7
-0.2
0.6
-0.4
y[m]
0.5 -0.6
0.4 -0.8
0.3 -1
0.2 -1.2
0.1 -1.4
0 -1.6
0 0.2 0.4 0.6 0.8 1
x[m]
15
III. T = 70% of steady state time
u[m/s]
1 0.8
0.9 0.7
0.8 0.6
0.7 0.5
0.6 0.4
y[m]
0.5 0.3
0.4 0.2
0.3 0.1
0.2 0
0.1 -0.1
0 -0.2
0 0.2 0.4 0.6 0.8 1
x[m]
v[m/s]
1
0.4
0.9
0.2
0.8
0
0.7 -0.2
0.6 -0.4
y[m]
0.5 -0.6
0.4 -0.8
-1
0.3
-1.2
0.2
-1.4
0.1
-1.6
0
0 0.2 0.4 0.6 0.8 1
x[m]
16
IV. T = 95% of steady state time
u[m/s]
1 0.8
0.9 0.7
0.8 0.6
0.7 0.5
0.6 0.4
y[m]
0.5 0.3
0.4 0.2
0.1
0.3
0
0.2
-0.1
0.1
-0.2
0
0 0.2 0.4 0.6 0.8 1
x[m]
v[m/s]
1 0.5
0.9
0.8 0
0.7
0.6
-0.5
y[m]
0.5
0.4
-1
0.3
0.2
-1.5
0.1
0
0 0.2 0.4 0.6 0.8 1
x[m]
17
B. Contours of u and v velocity at steady state for the three Reynolds numbers.
u[m/s]
1 0.8
0.9 0.7
0.8 0.6
0.7 0.5
0.6 0.4
y[m]
0.5 0.3
0.4 0.2
0.1
0.3
0
0.2
-0.1
0.1
-0.2
0
0 0.2 0.4 0.6 0.8 1
x[m]
v[m/s]
1 0.5
0.9
0.8 0
0.7
0.6
-0.5
y[m]
0.5
0.4
-1
0.3
0.2
-1.5
0.1
0
0 0.2 0.4 0.6 0.8 1
x[m]
18
C. Streamlines or velocity vectors for the three Re at steady state.
0.9
0.8
0.7
0.6
y[m]
0.5
0.4
0.3
0.2
0.1
0
0 0.2 0.4 0.6 0.8 1
x[m]
D. u-velocity profile on vertical centerline and v-velocity profile on horizontal centerline for
the three Re at steady state.
19
Re = 400
A. Contours of u and v velocity for the three Reynolds numbers at 4 time intervals separated
reasonably well apart.
I. T = 1% of steady state time
u[m/s]
1 0.8
0.9 0.7
0.8 0.6
0.7 0.5
0.4
0.6
0.3
y[m]
0.5
0.2
0.4
0.1
0.3
0
0.2
-0.1
0.1
-0.2
0
0 0.2 0.4 0.6 0.8 1
x[m]
v[m/s]
1 0.5
0.9 0
0.8 -0.5
0.7 -1
-1.5
0.6
-2
y[m]
0.5
-2.5
0.4
-3
0.3
-3.5
0.2
-4
0.1
-4.5
0
0 0.2 0.4 0.6 0.8 1
x[m]
20
II. T = 50% of steady state time
u[m/s]
1 0.8
0.9
0.6
0.8
0.7
0.4
0.6
y[m]
0.5
0.2
0.4
0.3 0
0.2
-0.2
0.1
0
0 0.2 0.4 0.6 0.8 1
x[m]
v[m/s]
1 0.5
0.9
0
0.8
-0.5
0.7
0.6 -1
y[m]
0.5
-1.5
0.4
-2
0.3
0.2 -2.5
0.1
-3
0
0 0.2 0.4 0.6 0.8 1
x[m]
21
III. T = 70% of steady state time
u[m/s]
1 0.8
0.9
0.6
0.8
0.7
0.4
0.6
y[m]
0.5
0.2
0.4
0.3 0
0.2
-0.2
0.1
0
0 0.2 0.4 0.6 0.8 1
x[m]
v[m/s]
1 0.5
0.9
0
0.8
0.7 -0.5
0.6
-1
y[m]
0.5
0.4 -1.5
0.3
-2
0.2
0.1 -2.5
0
0 0.2 0.4 0.6 0.8 1
x[m]
22
IV. T = 95% of steady state time
u[m/s]
1 0.8
0.9
0.6
0.8
0.7
0.4
0.6
y[m]
0.5
0.2
0.4
0.3 0
0.2
-0.2
0.1
0
0 0.2 0.4 0.6 0.8 1
x[m]
v[m/s]
1 0.5
0.9
0
0.8
0.7
-0.5
0.6
-1
y[m]
0.5
0.4
-1.5
0.3
0.2 -2
0.1
-2.5
0
0 0.2 0.4 0.6 0.8 1
x[m]
23
B. Contours of u and v velocity at steady state for the three Reynolds numbers.
u[m/s]
1 0.8
0.9
0.6
0.8
0.7
0.4
0.6
y[m]
0.5
0.2
0.4
0.3 0
0.2
-0.2
0.1
0
0 0.2 0.4 0.6 0.8 1
x[m]
v[m/s]
1 0.5
0.9
0
0.8
0.7
-0.5
0.6
-1
y[m]
0.5
0.4
-1.5
0.3
0.2 -2
0.1
-2.5
0
0 0.2 0.4 0.6 0.8 1
x[m]
24
C. Streamlines or velocity vectors for the three Re at steady state.
0.9
0.8
0.7
0.6
y[m]
0.5
0.4
0.3
0.2
0.1
0
0 0.2 0.4 0.6 0.8 1
x[m]
D. u-velocity profile on vertical centerline and v-velocity profile on horizontal centerline for
the three Re at steady state.
25
SUMMARY
MATLAB CODE
26
function [model] = main
% ========================================================
% fluid properties
% ========================================================
model.U_top = 1;
model.width = 1;
model.Re = 100;
model.rho = 1;
model.nu = model.U_top*model.width/model.Re;
model.mu = model.nu*model.rho;
% ========================================================
% domain properties
% ========================================================
model.height = 1;
model.nx = 42;
model.ny = 42;
model.dx = model.width/(model.nx-2);
model.dy = model.height/(model.ny-2);
% ========================================================
% sigeom.mulation parameters
% ========================================================
model.T = 25;
model.dt = .8*(1/(model.U_top/model.dx+2*model.nu/(model.dx^2)));
model.nt = floor(model.T/model.dt);
model
pause
% ========================================================
% convergence parameters
% ========================================================
model.pressure_res = 1e-4;
model.u_res = 1e-5;
model.v_res = 1e-5;
model.n_iter_sor = 2;
model.tol_sor = .001;
model.sor_alpha = 1.6;
% =======================================================
% initializing variables and the ghost node values
% =======================================================
u = zeros(model.ny,model.nx,model.nt);
v = zeros(model.ny,model.nx,model.nt);
p = zeros(model.ny,model.nx,model.nt);
p_old = p(:,:,1);
u(1,2:end-1,1) = 2*model.U_top-u(2,2:end-1,1);
% =======================================================
% marching on time
% =======================================================
for k = 1:model.nt-1
% =======================================================
% calculating the hat terms
% =======================================================
27
% =======================================================
% calculating the hat terms
% =======================================================
u_hat = zeros(model.ny,model.nx);
v_hat = zeros(model.ny,model.nx);
u_hat_mid = zeros(model.nx-1,model.ny);
v_hat_mid = zeros(model.nx,model.ny-1);
res_continuity = zeros(model.ny,model.nx);
for i=2:model.nx-1
for j=2:model.ny-1
conv_x = model.rho*u(i,j,k)/(2*model.dx)*(u(i,j+1,k)-u(i,j-
1,k))+model.rho*v(i,j,k)/(2*model.dy)*(u(i+1,j,k)-u(i-1,j,k));
conv_y = model.rho*u(i,j,k)/(2*model.dx)*(v(i,j+1,k)-v(i,j-
1,k))+model.rho*v(i,j,k)/(2*model.dy)*(v(i+1,j,k)-v(i-1,j,k));
diff_x = model.mu/(model.dx^2)*(u(i,j+1,k)-2*u(i,j,k)+u(i,j-
1,k))+model.mu/(model.dy^2)*(u(i+1,j,k)-2*u(i,j,k)+u(i-1,j,k));
diff_y = model.mu/(model.dx^2)*(v(i,j+1,k)-2*v(i,j,k)+v(i,j-
1,k))+model.mu/(model.dy^2)*(v(i+1,j,k)-2*v(i,j,k)+v(i-1,j,k));
u_hat(i,j) = u(i,j,k)+model.dt/model.rho*(diff_x-conv_x);
v_hat(i,j) = v(i,j,k)+model.dt/model.rho*(diff_y-conv_y);
% conv_x = model.rho*u(i,j,k)/(2*model.dx)*(u(i+1,j,k)-u(i-
1,j,k))+model.rho*v(i,j,k)/(2*model.dy)*(u(i,j+1,k)-u(i,j-1,k));
% conv_y = model.rho*u(i,j,k)/(2*model.dx)*(v(i+1,j,k)-v(i-
1,j,k))+model.rho*v(i,j,k)/(2*model.dy)*(v(i,j+1,k)-v(i,j-1,k));
% diff_x = model.mu/(model.dx^2)*(u(i+1,j,k)-2*u(i,j,k)+u(i-
1,j,k))+model.mu/(model.dy^2)*(u(i,j+1,k)-2*u(i,j,k)+u(i,j-1,k));
% diff_y = model.mu/(model.dx^2)*(v(i+1,j,k)-2*v(i,j,k)+v(i-
1,j,k))+model.mu/(model.dy^2)*(v(i,j+1,k)-2*v(i,j,k)+v(i,j-1,k));
% u_hat(i,j) = u(i,j,k)+model.dt/model.rho*(diff_x-conv_x);
% v_hat(i,j) = v(i,j,k)+model.dt/model.rho*(diff_y-conv_y);
end
end
% =======================================================
% computing the hat terms on mid points
% =======================================================
u_hat_mid = .5*(u_hat(1:end-1,:)+u_hat(2:end,:));
v_hat_mid = .5*(v_hat(:,1:end-1)+v_hat(:,2:end));
for j=2:model.ny-1
for i=2:model.nx-1
res_continuity(i,j) = model.rho/model.dt*((u_hat_mid(i,j)-u_hat_mid(i,j-
1))/model.dx+(v_hat_mid(i,j)-v_hat_mid(i-1,j))/model.dy);
end
end
% =======================================================================
% solving pressure poison eq. using SOR solver : sweep from left to right
% =======================================================================
for q = 1:model.n_iter_sor
p(1,:,k+1) = p(2,:,k+1);
p(end,:,k+1) = p(end-1,:,k+1);
p(:,1,k+1) = p(:,2,k+1);
p(:,end,k+1) = p(:,end-1,k+1);
28
for i = 2:model.ny-1
RHS_p = zeros(model.nx-2,1);
LHS_p = diag(-2*ones(model.nx-2,1))+diag(1*ones(model.nx-
3,1),1)+diag(1*ones(model.nx-3,1),-1);
for j = 2:model.nx-1
Sm = model.rho/model.dt*((u_hat_mid(j,i)-u_hat_mid(j-
1,i))/model.dx+(v_hat_mid(j,i)-v_hat_mid(j,i-1))/model.dy);
pressure_term = (p(j,i+1,k+1)-2*p(j,i,k+1)+p(j,i-1,k+1))/(model.dy^2);
RHS_p(j-1) = Sm-pressure_term;
end
RHS_p = RHS_p*(model.dx^2);
RHS_p(1) = RHS_p(1) - p(1,i,k+1);
RHS_p(end) = RHS_p(end) - p(end,i,k+1);
% =======================================================================
% updating the velocities at n+1 instant (not including the ghost nodes)
% =======================================================================
for j = 2:model.ny-1
for i = 2:model.nx-1
u(i,j,k+1) = u_hat(i,j) - model.dt*(p(i+1,j,k+1)-p(i-
1,j,k+1))/(2*model.rho*model.dx);
v(i,j,k+1) = v_hat(i,j) - model.dt*(p(i,j+1,k+1)-p(i,j-
1,k+1))/(2*model.rho*model.dy);
end
end
% =======================================================================
% updating the velocities at the ghost nodes
% =======================================================================
% west side
u(:,1,k+1) = - u(:,2,k+1);
% east side
u(:,end,k+1) = -u(:,end-1,k+1);
% south side
u(end,:,k+1) = -u(end-1,:,k+1);
% north side
u(1,2:end-1,k+1) = 2*model.U_top-u(2,2:end-1,k+1);
% fprintf('\n marching');
% model.u_plot = u(:,:,k+1);
% model.v_plot = v(:,:,k+1);
%
% plot_output(model)
% pause
disp(k)
close all;
end
model.u_plot = u(:,:,end);
model.v_plot = v(:,:,end);
plot_output(model)
end 29
function plot_output(model)
set(0,'defaultaxesfontname','times');
set(0,'defaultaxesfontsize',12);