Beruflich Dokumente
Kultur Dokumente
Handout 8
MATLAB Code for the Projection Method and BC Details for the Lid-Driven Cavity
Problem
Let’s simplify the equations of the last page of Handout 7 for the case ℎ = Δ𝑥 = Δ𝑦 because the code given in the
next page is written for this case. Also drop the body force term for simplicity.
Boundary Conditions with Ghost Cells for the Lid-Driven Carvity Problem
The code given at the end of this handout is written specifically for the lid-driven cavity problem. Let’s study the
BCs of this problem first.
𝑦 𝑢 𝑇 = 𝑈𝑙𝑖𝑑
𝑣𝑇 = 0
𝑢𝐿 = 0 𝑢𝑅 = 0
𝑣𝐿 = 0 𝑣𝑅 = 0
𝐿
𝑥
𝑢𝐵 = 0
𝑣𝐵 = 0
1
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
Consider the following mesh of (Nx = 3) x (Ny = 3) p cells. Thick lines are the domain boundaries. All around the
domain we placed ghost cells. For structured grids ghost cells provide an easy way to implement the BCs.
2
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
BCs for 𝑢:
We need to calculate only 6 𝑢 values, the ones for the inner u-cells, i.e. u(2:Nx, 2:Ny+1).
𝑢 on the left and right boundaries are given as 𝑢𝐿 = 0 and 𝑢𝑅 = 0. Do not solve for them.
To calculate ghost node values we use linear interpolation. For example for 𝑢1,1
𝑢1,1 + 𝑢1,2
= 𝑢𝐵 → 𝑢1,1 = 2𝑢𝐵 − 𝑢1,2 , where 𝑢𝐵 is the 𝑢 at the bottom boundary
2
Or for 𝑢3,5
𝑢3,5 + 𝑢3,4
= 𝑢𝑇 → 𝑢3,5 = 2𝑢 𝑇 − 𝑢3,4 , where 𝑢 𝑇 is the 𝑢 at the top boundary
2
After the end (or beginning) of each time step ghost node values are updated using equations similar to these.
3
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
BCs for 𝑣:
We need to calculate only 6 𝑣 values, the ones for the inner v-cells, i.e. v(2:Nx+1, 1:Ny).
𝑣 on the bottom and top boundaries are given as 𝑣𝐵 = 0 and 𝑣𝑇 = 0. Do not solve for them.
To calculate ghost node values we use linear interpolation. For example for 𝑣1,2
𝑣1,2 + 𝑣2,2
= 𝑣𝐿 → 𝑣1,2 = 2𝑣𝐿 − 𝑣2,2 , where 𝑣𝐿 is the 𝑣 at the left boundary
2
Or for 𝑣5,3
𝑣5,3 + 𝑣4,3
= 𝑣𝑅 → 𝑣5,3 = 2𝑣𝑅 − 𝑣4,3 , where 𝑣𝑅 is the 𝑣 at the right boundary
2
After the end (or beginning) of each time step ghost node values are updated using equations similar to these.
4
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
For the p-cells at the boundaries we need to derive a special Eqn. (18). Consider cell 𝑝2,2 . Eqn. (16) for it is
𝑛+1 𝑛+1 𝑛+1 𝑛+1
ℎ ( 𝑢2,2 − 𝑢⏟
1,2 + 𝑣2,2 − 𝑣
⏟2,1 ) = 0 (16∗ )
𝑢𝐿 𝑣𝐵
5
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
𝑛+1 ∗
Δ𝑡 (𝑝2,3 − 𝑝2,2 ) 𝜌ℎ ∗ 𝑛+1
𝑣2,2 = 𝑣2,2 − → 𝑝2,3 − 𝑝2,2 = (𝑣 − 𝑣2,2 ) (17𝑐 ∗ )
𝜌 h Δ𝑡 2,2
This is not necesary (17𝑑∗ )
𝜌ℎ ∗ ∗ 𝑛+1 𝑛+1
𝑝3,2 + 𝑝2,3 − 2𝑝2,2 = 𝑢2,2 + 𝑣2,2 −𝑢2,2
⏟ − 𝑣2,2 (18∗ )
Δ𝑡
= −𝑢𝐿 −𝑣𝐵
( according to Eqn (16∗ ))
Number of unknowns decreased from 5 to 3 on the left hand side. If we solve the system using Gauss-
Seidel, this can be taken care automatically by setting ghost cell pressure values to zero and always keep
them like that. That way the only difference of the left hand compared to the original Eqn (18) becomes
the change of the coefficient of the center unknown, i.e. 𝑝2,2 from -4 to 22.
On the right hand side there seem to be a change, but actually not. Because in the code 𝑢𝐿 and 𝑣𝐵 will be
∗ ∗
seen as 𝑢1,2 and 𝑢2,1 , making the right hand side of Eqn (18*) the same as the original Eqn (18), so no
change is required.
If we repeat this for a non-corner boundary cell, such as 𝑝4,3 , we’ll see that the only difference compared to Eqn
(18) will be the change of the coefficient of the center unknown from -4 to -3.
6
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
% Allocate memory. Note that staggered grid with ghost cells is being used.
u = zeros(Nx+1,Ny+2); % x velocity values
v = zeros(Nx+2,Ny+1); % y velocity values The mesh is
p = zeros(Nx+2,Ny+2); % Pressure values
uStar = zeros(Nx+1,Ny+2); % Temporary x velocities staggered.
vStar = zeros(Nx+2,Ny+1); % Temporary y velocities
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialize the solution
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
u(:,:) = 0.0;
v(:,:) = 0.0;
p(:,:) = 0.0;
uOld = u; % uOld and vOld are used to check steady state convergence
vOld = v;
7
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set the coefficient of the center unknown of the PPE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
coefPPE = zeros(Nx+1,Ny+1);
for i = 2:Nx+1
Set the coefficient of the
for j = 2:Ny+1
if (i == 2 && j == 2) || (i == Nx+1 && j == 2) || ... center unknown of the PPE
(i == 2 && j == Ny+1) || (i == Nx+1 && j == Ny+1)
coefPPE(i,j) = -2; = -4 for non-boundary cells
elseif (i == 2) || (i == Nx+1) || (j == 2) || (j == Ny+1) = -2 for corner cells
coefPPE(i,j) = -3;
else = -3 for non-corner
coefPPE(i,j) = -4; boundary cells
end
end
end
% Print the header for the data that'll be written on the screen
fprintf(' n u at (0.5,0.5) v at (0.5,0.5)\n');
fprintf('=========================================\n');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculate ghost cell velocities using linear interpolation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1:Nx+1
u(i,1) = 2*u_BOTTOM - u(i,2); % Bottom boundary Calculate ghost cell
u(i,Ny+2) = 2*u_TOP - u(i,Ny+1); % Top boundary velocities.
end
for j = 1:Ny+1
v(1,j) = 2*v_LEFT - v(2,j); % Left boundary
v(Nx+2,j) = 2*v_RIGHT - v(Nx+1,j); % Right boundary
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 1a. Calculate the temporary u velocities. Eqn (20a) of Handout 8
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 2:Nx
for j = 2:Ny+1
ue = 0.5 * (u(i,j) + u(i+1,j));
uw = 0.5 * (u(i,j) + u(i-1,j));
un = 0.5 * (u(i,j) + u(i,j+1)); Use central differencing
us = 0.5 * (u(i,j) + u(i,j-1));
to get face velocities
vn = 0.5 * (v(i,j) + v(i+1,j));
vs = 0.5 * (v(i,j-1) + v(i+1,j-1));
8
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 1b. Calculate the temporary v velocities. Eqn (20b) of Handout 8
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 2:Nx+1
for j = 2:Ny
ve = 0.5 * (v(i,j) + v(i+1,j));
vw = 0.5 * (v(i,j) + v(i-1,j));
vn = 0.5 * (v(i,j) + v(i,j+1)); Use central differencing
vs = 0.5 * (v(i,j) + v(i,j-1));
to get face velocities
ue = 0.5 * (u(i,j) + u(i,j+1));
uw = 0.5 * (u(i-1,j) + u(i-1,j+1));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 2. Solve the pressure Poisson equation (PPE) using Gauss-Seidel
% Eqn (18) of Handout 8.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Here we typically check the convergence of Gauss-Seidel. But for this steady
% problem it is not very critical to solve the PPE accurately so we skipped
% this check.
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 3. Correct the temporary velocities. Eqn (21a-b) of Handout 8
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Correct u velocities
for i = 2:Nx
for j = 2:Ny+1
u(i,j) = uStar(i,j) - dt / (rho * h) * (p(i+1,j) - p(i,j));
Eqn (21a) of
end Handout 8
end
9
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
% Correct v velocities
for i = 2:Nx+1
for j = 2:Ny
v(i,j) = vStar(i,j) - dt / (rho * h) * (p(i,j+1) - p(i,j)); Eqn (21b) of
end Handout 8
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot the u velocity variation along the vertical centerline of the Post-
% cavity processing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if mod(n,10) == 0
yCenter = h/2:h:1-h/2;
uCenter = u(Nx/2+1:Nx/2+1, 2:Ny+1);
10