Beruflich Dokumente
Kultur Dokumente
Handout 11
Implementing the SIMPLE Algorithm
the solution is segregated (sequential), i.e. 𝑥-momentum equation is solved for 𝑢 by assuming 𝑣 and 𝑝 to
be known, and 𝑦-momentum equation is solved for 𝑣 by assuming 𝑢 and 𝑝 to be known.
nonlinear terms are linearized by calculating mass flow rates using old velocities.
neighbor contributions are neglected in relating pressure corrections to velocity corrections
To compensate all these and to end up with a converging solution, we used pressure and velocity under-relaxation
as follows
𝑝 = 𝑝𝑜𝑙𝑑 + 𝛼𝑝 𝑝′
𝑢𝑛𝑒𝑤 = 𝛼𝑉 𝑢 + (1 − 𝛼𝑉 )𝑢𝑜𝑙𝑑
𝑣 𝑛𝑒𝑤 = 𝛼𝑉 𝑣 + (1 − 𝛼𝑉 )𝑣 𝑜𝑙𝑑
However in practice velocity under-relaxation is not used like this. Instead it is directly applied to the solution of 𝑢∗
and 𝑣 ∗ velocities by modifying, for example, Eqn. (12a) as follows (a bit of mathematics is required to see how this
works)
𝑢 𝑢
𝑎𝑖,𝑗 ∗ 𝑢 ∗ 𝑜𝑙𝑑 𝑜𝑙𝑑 𝑢
𝑎𝑖,𝑗 𝑜𝑙𝑑
𝑢𝑖,𝑗 = ∑ 𝑎𝑛𝑏 𝑢𝑛𝑏 − (𝑝𝑖+1,𝑗 − 𝑝𝑖,𝑗 )Δ𝑦 + 𝑏𝑖,𝑗 + (1 − 𝛼𝑉 ) 𝑢𝑖,𝑗
𝛼
⏟𝑉 𝛼
⏟𝑉
𝑛𝑒𝑤
𝑛𝑏 𝑛𝑒𝑤
𝑢 𝑢
𝑎𝑖,𝑗 𝑎𝑖,𝑗
In the rest of this handout the “new” superscript will be dropped from the central coefficient for clarity, but we
need to remember how it makes use of the velocity under-relaxation.
Eqn. (12b) is modified in a similar way. After this modification velocities are updated without any extra relaxation
as follows
𝑢 = 𝑢∗ + 𝑢′ , 𝑣 = 𝑣∗ + 𝑣′
Mathematically we are not doing anything different than what we were doing before. But numerically this approach
is preferred because in this way 𝑢∗ and 𝑣 ∗ equations become more “diagonally dominant” due to the central
coefficients being divided by 𝛼𝑉 , which is a number less than 1. And this is a desired property for the convergence
of iterative algebraic system solvers such as Gauss-Seidel.
1
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
Simplified equations used in the code based on central differencing and ℎ = Δ𝑥 = Δ𝑦 with no body force
𝑢 𝑚̇𝑒𝑢 𝑢
𝑢
𝑚̇𝑤 𝑢 𝑚̇𝑛𝑢 𝑢 𝑚̇𝑠𝑢
𝑎𝑖+1,𝑗 =− +𝜇 , 𝑎𝑖−1,𝑗 =− +𝜇 , 𝑎𝑖,𝑗+1 =− +𝜇 , 𝑎𝑖,𝑗−1 =− +𝜇
2 2 2 2
y-momentum equation discretized for 𝑣 ∗ is very similar to the above one (1𝑏)
𝑃𝐶 ′ 𝑃𝐶 ′ 𝑃𝐶
𝑎𝑖,𝑗 𝑝𝑖,𝑗 = ∑ 𝑎𝑛𝑏 𝑝𝑛𝑏 + 𝑏𝑖,𝑗 (2)
𝑛𝑏
𝑃𝐶 𝑢 𝑢 𝑣 𝑣
𝑎𝑖,𝑗 = 𝑑𝑖,𝑗 + 𝑑𝑖−1,𝑗 + 𝑑𝑖,𝑗 + 𝑑𝑖,𝑗−1
𝑃𝐶 𝑢 𝑃𝐶 𝑢 𝑃𝐶 𝑣 𝑃𝐶 𝑣
𝑎𝑖+1,𝑗 = 𝑑𝑖,𝑗 , 𝑎𝑖−1,𝑗 = 𝑑𝑖−1,𝑗 , 𝑎𝑖,𝑗+1 = 𝑑𝑖,𝑗 , 𝑎𝑖,𝑗−1 = 𝑑𝑖,𝑗−1
Step 2
New definitions
𝑢 ℎ 𝑢 ℎ 𝑣 ℎ 𝑣 ℎ
where 𝑑𝑖,𝑗 = 𝑢 , 𝑑𝑖−1,𝑗 = 𝑢 , 𝑑𝑖,𝑗 = 𝑣 , 𝑑𝑖,𝑗−1 = 𝑣
used in the
𝑎𝑖,𝑗 𝑎𝑖−1,𝑗 𝑎𝑖,𝑗 𝑎𝑖,𝑗−1 code.
𝑃𝐶 ∗ ∗ ∗ ∗
𝑏𝑖,𝑗 = −𝑢𝑖,𝑗 + 𝑢𝑖−1,𝑗 − 𝑣𝑖,𝑗 + 𝑣𝑖,𝑗−1
′ ′ ′ 𝑢
𝑢𝑖,𝑗 = −(𝑝𝑖+1,𝑗 − 𝑝𝑖,𝑗 )𝑑𝑖,𝑗 (3𝑎)
Step 3
′ ′ ′ 𝑣
𝑣𝑖,𝑗 = −(𝑝𝑖,𝑗+1 − 𝑝𝑖,𝑗 )𝑑𝑖,𝑗 (3𝑏)
𝑝 = 𝑝𝑜𝑙𝑑 + 𝛼𝑝 𝑝′ (4𝑎)
𝑢 = 𝑢∗ + 𝑢′
Step 4
(4𝑏)
𝑣 = 𝑣∗ + 𝑣′ (4𝑐)
2
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
Boundary Conditions with Ghost Cells for the Lid-Driven Cavity Problem
Consider the same mesh of 9 pressure cells given in Handout 8. With the ghost cells totally there are 25 pressure
nodes, 20 u-velocity nodes and 20 v-velocity nodes.
BCs for the u and v velocities are implemented in the same way as described in Handout 8. In summary we solve
Eqns (1a) and (1b) only for the 6 unknown inner velocities. These calculations may need known velocities at the
boundaries or velocities of the ghost cells. We update the ghost cell velocities at the beginning of each SIMPLE
iteration by using a linear interpolation across the boundaries.
Let’s see how the pressure correction equation (PCE) need to be modified for the boundary cells. Here are the p-
cells and the known velocities that affect the PCE of the boundary p-cells.
As an example consider cell 𝑝2,2 . Continuity equation for this cell is (ℎ = Δ𝑥 = Δ𝑦)
3
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
ℎ ( 𝑢2,2 − 𝑢
⏟1,2 + 𝑣2,2 − 𝑣
⏟2,1 ) = 0 (5)
𝑢𝐿 𝑣𝐵
where 𝑢𝐿 and 𝑣𝐵 are the known velocities at the left and bottom boundaries. Cancel the constant ℎ and write the
unknown velocities as the summation of “star” and “prime” velocities.
∗ ′ ∗ ′
(𝑢2,2 + 𝑢2,2 ) − 𝑢𝐿 + (𝑣2,2 + 𝑣2,2 ) − 𝑣𝐵 = 0 (6)
Replace the velocity corrections with pressure corrections using Eqn. (3)
∗ ′ ′ 𝑢 ∗ ′ ′ 𝑣
[𝑢2,2 − (𝑝3,2 − 𝑝2,2 )𝑑2,2 ] − 𝑢𝐿 + [𝑣2,2 − (𝑝2,3 − 𝑝2,2 )𝑑2,2 ] − 𝑣𝐵 = 0 (7)
To summarize, there is nothing special we need to do for the boundary pressure nodes.
As we discussed in class, for this lid driven cavity problem no pressure information is provided at the boundaries.
Pressure values will have no reference value to settle around. For such cases we usually set the pressure at a node
to a reference value. In the code we equated the pressure at the lower left corner (𝑝2,2 ) to zero for simplicity. To
do this we set the pressure correction at that p-cell to zero at each iteration, therefore the initial zero pressure will
not be modified.
Note that in incompressible flows the pressure values are not important, but the gradient of pressure is what
matters. Therefore setting the pressure to zero at the corner cell or setting it to a different value will not make any
difference on the velocity field. Only all the pressure values will go up or down by the same constant, which is not
important.
For flows with fully developed outlet boundaries we usually set pressure to zero at the outlet.
4
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
Ny = Nx;
h = 1/Nx; % h is the cell size (=dx=dy). Cavity is a square of size 1x1.
% Allocate memory. Note that staggered grid with ghost cells is being used.
u = zeros(Nx+1,Ny+2); % x velocity values
uStar = zeros(Nx+1,Ny+2); % Temporary x velocity values
uPrime = zeros(Nx+1,Ny+2); % x velocity correction
Note that the
v = zeros(Nx+2,Ny+1); % y velocity values mesh is staggered.
vStar = zeros(Nx+2,Ny+1); % Temporary y velocity values
vPrime = zeros(Nx+2,Ny+1); % y velocity correction
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialize the solution
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Print the header for the data that'll be written on the screen
fprintf(' iter u at (0.5,0.5) v at (0.5,0.5)\n');
fprintf('=========================================\n');
5
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Start the SIMPLE iterations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for n = 1:maxIter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Update ghost cell velocities using linear interpolation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1:Nx+1
uOld(i,1) = 2*u_BOTTOM - uOld(i,2); % Bottom boundary
uOld(i,Ny+2) = 2*u_TOP - uOld(i,Ny+1); % Top boundary
end Update ghost cell
velocities using
for j = 1:Ny+1 linear interpolation.
vOld(1,j) = 2*v_LEFT - vOld(2,j); % Left boundary
vOld(Nx+2,j) = 2*v_RIGHT - vOld(Nx+1,j); % Right boundary
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 1a. Solve the x momentum equation (Eqn. 1a of Handout 11) using
% Gauss-Seidel. The result is stored in uStar.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% For the initial guess of the G-S solution use the previous iteration values
uStar = uOld;
% Gauss-Seidel iterations
for iter = 1:maxGSiter Gauss-Seidel solution of
for i = 2:Nx % These i and j are the indices of the u-cell the x-momentum eqn.
for j = 2:Ny+1
uStar(i,j) = (au_E(i,j) * uStar(i+1,j) + au_W(i,j) * uStar(i-1,j) ...
+ au_N(i,j) * uStar(i,j+1) + au_S(i,j) * uStar(i,j-1) ...
- h * (pOld(i+1,j) - pOld(i,j)) ...
+ (1-alphaV) * au_C(i,j) * uOld(i,j) ) / au_C(i,j); Velocity under-relaxation
end
end also affects the right
% WARNING: Here we typically check the convergence of Gauss-Seidel. hand side
end
6
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 1b. Solve the y momentum equation (Eqn. 1b of Handout 11) using
% Gauss-Seidel. The result is stored in vStar.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% For the initial guess of the G-S solution use the previous iteration
% values
vStar = vOld;
% Gauss-Seidel iterations
for iter = 1:maxGSiter Gauss-Seidel solution of
for i = 2:Nx+1 % These i and j are the indices of the v-cell the y-momentum eqn.
for j = 2:Ny
vStar(i,j) = (av_E(i,j) * vStar(i+1,j) + av_W(i,j) * vStar(i-1,j) ...
+ av_N(i,j) * vStar(i,j+1) + av_S(i,j) * vStar(i,j-1) ...
- h * (pOld(i,j+1) - pOld(i,j)) ...
+ (1-alphaV) * av_C(i,j) * vOld(i,j) ) / av_C(i,j);
end Velocity under-relaxation
end also affects the right
% WARNING: Here we typically check the convergence of Gauss-Seidel.
end hand side
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 2. Solve the pressure correction equation (PCE) (Eqn. 2 of
% Handout 11) using Gauss-Seidel
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Calculate the
% First setup the coefficients
for i = 2:Nx+1 % These i and j are the indices of the p-cell coefficients of the
for j = 2:Ny+1 pressure correction eqn.
7
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
% IMPORTANT: In the cavity flow problem velocities at all boundaries are given. No pressure
% information is provided at the boundaries. It's better to fix pressure at a point.
% Let's fix it to zero at the bottom-left cell, i.e. set the pressure correction
% at this cell to zero.
i = 2;
j = 2;
aPC_C(i,j) = 1.0; % Center cell coefficient
aPC_E(i,j) = 0.0; % East neighbor coefficient Set the pressure correction of
aPC_W(i,j) = 0.0; % West neighbor coefficient the bottom-left cell to zero.
aPC_N(i,j) = 0.0; % North neighbor coefficient
aPC_S(i,j) = 0.0; % South neighbor coefficient
bPC(i,j) = 0.0; % RHS value
% Gauss-Seidel loop
Gauss-Seidel solution of
pPrime(:,:) = 0; % Initialize pPrime's to zero.
for iter = 1:maxGSiter the pressure correction
for i = 2:Nx+1 eqn.
for j = 2:Ny+1
pPrime(i,j) = (aPC_E(i,j) * pPrime(i+1,j) + aPC_W(i,j) * pPrime(i-1,j) ...
+ aPC_N(i,j) * pPrime(i,j+1) + aPC_S(i,j) * pPrime(i,j-1) ...
+ bPC(i,j) ) / aPC_C(i,j);
end
end
% WARNING: Here we typically check the convergence of Gauss-Seidel.
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 3. Calculate velocity corrections using Eqn. 3 of Handout 11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculate u corrections
for i = 2:Nx
for j = 2:Ny+1
uPrime(i,j) = -(pPrime(i+1,j) - pPrime(i,j)) * dU(i,j);
end Calculate velocity corrections.
end
No need to use velocity
% Calculate v corrections under-relaxation here. we
for i = 2:Nx+1 already used it above.
for j = 2:Ny
vPrime(i,j) = -(pPrime(i,j+1) - pPrime(i,j)) * dV(i,j);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 4. Correct p, u and v using Eqn. 4 of Handout 11
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
% Correct u velocities. Note that under-relaxation was already applied in uStar calculation.
for i = 2:Nx
for j = 2:Ny+1
u(i,j) = uStar(i,j) + uPrime(i,j); Correct u velocities
end
end
% Correct v velocities. Note that under-relaxation was already applied in vStar calculation.
for i = 2:Nx+1
for j = 2:Ny
v(i,j) = vStar(i,j) + vPrime(i,j); Correct v velocities
end
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 = v(Nx/2+1, Ny/2+1);
fprintf('%5d %8.3e %8.3e\n', n, uCenter, vCenter);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot the u velocity variation along the vertical centerline of the
% cavity
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if mod(n,10) == 0
yCenter = h/2:h:1-h/2; Post processing
uCenter = u(Nx/2+1, 2:Ny+1);
uOld = u;
vOld = v;
pOld = p;
9
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
Sample Solutions
10
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
Solution 4: To test the mesh size – velocity under-relaxation relation observed in the previous solution node number
is further increased to Nx = Ny = 100. This time we were able to get a converged solution with 𝛼𝑉 = 0.3,
𝛼𝑃 = 0.7. So the observation holds. Unfortunately in MATLAB getting a converged solution with this somewhat fine
grid takes too much time.
In the discretization of momentum equations it is possible to use upwinding for the convection term. In the x-
momentum equation the convection term is integrated as follows
Calculation of mass flow rates will not change when we use upwinding, i.e. still central differencing of previous
known velocities will be used. But the unknown 𝑢 and 𝑣’s at the faces of the u-cell will be expressed in terms of
nodal values in a different way. With 1st order upwinding, convection integral for the u-cell (𝑖, 𝑗) will be evaluated
as
max(𝑚̇𝑒𝑢 , 0)𝑢𝑖,𝑗 − max(−𝑚̇𝑒𝑢 , 0)𝑢𝑖+1,𝑗 + max(𝑚̇𝑤 𝑢
, 0)𝑢𝑖,𝑗 − max(−𝑚̇𝑤 𝑢
, 0)𝑢𝑖−1,𝑗 +
max(𝑚̇𝑛𝑢 , 0)𝑢𝑖,𝑗 − max(−𝑚̇𝑛𝑢 , 0)𝑢𝑖,𝑗+1 + max(𝑚̇𝑠𝑢 , 0)𝑢𝑖,𝑗 − max(−𝑚̇𝑠𝑢 , 0)𝑢𝑖,𝑗−1
𝑢 1
𝑎𝑖,𝑗 = [max(𝑚̇𝑒𝑢 , 0) + max(𝑚̇𝑤
𝑢
, 0) + max(𝑚̇𝑛𝑢 , 0) + max(𝑚̇𝑠𝑢 , 0) + 4𝜇]
𝛼𝑉
𝑢 𝑢 𝑢 𝑢
𝑎𝑖+1,𝑗 = max(𝑚̇𝑒𝑢 , 0) + 𝜇 , 𝑎𝑖−1,𝑗 𝑢
= max(𝑚̇𝑤 , 0) + 𝜇 , 𝑎𝑖,𝑗+1 = max(𝑚̇𝑛𝑢 , 0) + 𝜇 , 𝑎𝑖,𝑗−1 = max(𝑚̇𝑠𝑢 , 0) + 𝜇
11
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
More Solutions
12
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
With central differencing even with 𝛼𝑉 = 𝛼𝑃 = 0.01 the solution blows up. You need to use too very small under-
relaxation factors, which will result in a very slowly converging run. I do not have any result to present for central
differencing.
Question: What about high order upwind schemes such as 2nd order TVD schemes? Are they really the cure?
13