Sie sind auf Seite 1von 10

METU, Dept. of Mechanical Eng.

ME 705, Fall 2016, Dr. C. Sert

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.

Simplified equations used in the code for ℎ = Δ𝑥 = Δ𝑦 and no body force


𝑛
∗ 𝑛 1 𝜇
𝑢𝑖,𝑗 = 𝑢𝑖,𝑗 + Δ𝑡 [− (𝑢𝑒𝑢 𝑢𝑒𝑢 − 𝑢𝑤
𝑢 𝑢
𝑢𝑤 + 𝑢𝑛𝑢 𝑣𝑛u − 𝑢𝑠𝑢 𝑣𝑠u ) + 2 (𝑢𝑖+1,𝑗 + 𝑢𝑖−1,𝑗 + 𝑢𝑖,𝑗+1 + 𝑢𝑖,𝑗−1 − 4𝑢𝑖,𝑗 )] (20𝑎)
ℎ 𝜌ℎ
𝑛
∗ 𝑛 1 𝜇
𝑣𝑖,𝑗 = 𝑣𝑖,𝑗 + Δ𝑡 [− (𝑢𝑒𝑣 𝑣𝑒𝑣 − 𝑢𝑤
𝑣 𝑣
𝑣𝑤 + 𝑣𝑛𝑣 𝑣𝑛𝑣 − 𝑣𝑠𝑣 𝑣𝑠𝑣 ) + 2 (𝑣𝑖+1,𝑗 + 𝑣𝑖−1,𝑗 + 𝑣𝑖,𝑗+1 + 𝑣𝑖,𝑗−1 − 4𝑣𝑖,𝑗 )] (20𝑏)
ℎ 𝜌ℎ
𝜌ℎ ∗ ∗ ∗ ∗
𝑝𝑖+1,𝑗 + 𝑝𝑖−1,𝑗 + 𝑝𝑖,𝑗+1 + 𝑝𝑖,𝑗−1 − 4𝑝𝑖,𝑗 = (𝑢 − 𝑢𝑖−1,𝑗 + 𝑣𝑖,𝑗 − 𝑣𝑖,𝑗−1 ) (18)
Δ𝑡 𝑖,𝑗

𝑛+1 ∗ Δ𝑡 (𝑝𝑖+1,𝑗 − 𝑝𝑖,𝑗 )


𝑢𝑖,𝑗 = 𝑢𝑖,𝑗 − (21𝑎)
𝜌 ℎ

𝑛+1 ∗ Δ𝑡 (𝑝𝑖,𝑗+1 − 𝑝𝑖,𝑗 )


𝑣𝑖,𝑗 = 𝑣𝑖,𝑗 − (21𝑏)
𝜌 ℎ

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.

There are (Nx+2) x (Ny+2) = 25 pressure nodes.

There are (Nx+1) x (Ny+2) = 20 u-velocity nodes.

There are (Nx+2) x (Ny+1) = 20 v-velocity nodes.

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

BCs for the PPE:

We need to calculate 9 𝑝 values, i.e. p(2:Nx+1, 2:Ny+1)

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∗ )
𝑢𝐿 𝑣𝐵

Eqn (15) for this p-cell becomes

𝑛+1 ∗ Δ𝑡 (𝑝3,2 − 𝑝2,2 ) 𝜌ℎ ∗ 𝑛+1


𝑢2,2 = 𝑢𝑖,𝑗 − → 𝑝3,2 − 𝑝2,2 = (𝑢 − 𝑢2,2 ) (17𝑎∗ )
𝜌 ℎ Δ𝑡 2,2
This is not necesary (17𝑏∗ )

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𝑑∗ )

Add Eqn (17a*) and Eqn (17c*)

𝜌ℎ ∗ ∗ 𝑛+1 𝑛+1
𝑝3,2 + 𝑝2,3 − 2𝑝2,2 = 𝑢2,2 + 𝑣2,2 −𝑢2,2
⏟ − 𝑣2,2 (18∗ )
Δ𝑡
= −𝑢𝐿 −𝑣𝐵
( according to Eqn (16∗ ))

Compared to the original Eqn (18) the changes are

 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.

To summarize, boundaries will affect Eqn (18) as follows

 Set the center coefficient of the corner cells to -3.


 Set the center coefficient of the non-corner cells to -2.
 In the meantime keep the ghost cell pressures at zero.

6
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert

% 2D flow solver using the projection method.


% It is design ed to solve the lid driven cavity problem.
% Uses forward time and central space differencing.
% Uses staggered mesh
% Uses ghost cells for boundary conditions

clc; % Clear the command window


clear all; % Clear previously defined variables
close all; % Close previously opened figure windows

refSoln = load ('Ghia_Re1000_Uvel'); % u velocity variation along the vertical centerline


% from Ghia's reference solution. The data is available
% at 4 different Reynolds numbers (100, 1000, 3200 and
% 5000). refSoln has 2 columns, y and u.

L = 1; % Cavity size. Preferred value is 1.


Nx = 40;% Number of pressure cells in x and y directions
Ny = 40;% (not including ghost cells).
% Provide an even value to avoid a possible error in
% the post-processing part.
rho = 1.0; % Density [kg/m^3]
visc = 1/1000; % Dynamic viscosity [Pa s]
dt = 0.02; % Time step
nStep = 10000; % Maximum number of time levels
maxGSiter = 1; % Maximum iteration value for the Gauss-Seidel solver

u_TOP = 1.0; % Tangential velocities at the boundaries [m/s] Be careful !!!


u_BOTTOM = 0.0;
v_LEFT = 0.0;
v_RIGHT = 0.0;

h = L/Nx; % h is the cell size (=dx=dy).

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

for n = 1:nStep % Time loop

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

uStar(i,j) = u(i,j) - dt / h * (ue*ue + un*vn - uw*uw - us*vs) ... Eqn (20a) of


+ dt * visc / rho / (h^2) * (u(i+1,j) + u(i,j+1) ...
+ u(i-1,j) + u(i,j-1) - 4 * u(i,j)); Handout 8
end
end

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

vStar(i,j) = v(i,j) - dt / h * (ve*ue + vn*vn - vw*uw - vs*vs) ...


+ dt * visc / rho / (h^2) * (v(i+1,j) + v(i,j+1) ...
Eqn (20b) of
+ v(i-1,j) + v(i,j-1) - 4 * v(i,j)); Handout 8
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 2. Solve the pressure Poisson equation (PPE) using Gauss-Seidel
% Eqn (18) of Handout 8.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Calculate the RHS of the PPE system.


for i = 2:Nx+1
for j = 2:Ny+1
rhsPPE(i,j) = (rho*h/dt) * (uStar(i,j) - uStar(i-1,j) + vStar(i,j) - vStar(i,j-1));
end
end

% Gauss-Seidel loop Solve the PPE, i.e.


for iter = 1:maxGSiter Eqn (18) of Handout
pOld = p;
for i = 2:Nx+1 8 using Gauss-Seidel.
for j = 2:Ny+1
p(i,j) = (rhsPPE(i,j) - p(i+1,j) - p(i-1,j) - p(i,j+1) - p(i,j-1)) / coefPPE(i,j);
end
end

% 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

% Write some results on the screen


% Calculate u and v at (0.5,0.5)
uCenter = u(Nx/2+1, Ny/2+1);
vCenter = u(Nx/2+1, Ny/2+1);
fprintf('%5d %8.3e %8.3e\n', n, uCenter, vCenter);
uOld = u;
vOld = v;

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

plot(refSoln(:,2), refSoln(:,1), uCenter, yCenter, 'ko', 'MarkerFaceColor', 'k')


axis([-1 1 0 1]);
grid on
xlabel('u [m/s]');
ylabel('y [m]');
title('u velocity component along the vertical centerline');
legend('Current', 'Ghia', 'Location', 'NorthWest')
drawnow;
end

end % End of time loop

10

Das könnte Ihnen auch gefallen