%% define spatial and temporal grids
l_square = 1; % length of square
h = l_square/10; % grid spacing
dt = 1; % time step
L = 40; % cavity length
D = 15; % cavity depth
Nx = round((L/h)+1); % grid points in x-axis
Ny = round((D/h)+1); % grid points in y-axis
nu = 0.000015111; % kinematic viscosity
Uinf = 0.0015111; % free stream velocity / inlet velocity / lid velocity
cfl = dt*Uinf/h; % % cfl number
travel = 10; % times the disturbance travels entire length of computational domain
TT = travel*L/Uinf; % total time
ns = TT/dt; % number of time steps
Re = l_square*Uinf/nu; % Reynolds number
rho = 1.2047; % fluid density
%% initialize flowfield
u = Uinf*ones(Nx,Ny); % x-velocity
v = zeros(Nx,Ny); % y-velocity
p = zeros(Nx,Ny); % pressure
i = 2:Nx-1; % spatial interior nodes in x-axis
j = 2:Ny-1; % spatial interior nodes in y-axis
[X, Y] = meshgrid(0:h:L, 0:h:D); % spatial grid
maxNumCompThreads('automatic'); % select CPU cores
%% solve 2D Navier-Stokes equations
for nt = 1:ns
pn = p;
p(i, j) = (pn(i+1, j)+pn(i-1, j)+pn(i, j+1)+pn(i, j-1))/4 ...
-h*rho/(8*dt)*(u(i+1, j)-u(i-1, j)+v(i, j+1)-v(i, j-1)); % pressure poisson
p(1, :) = p(2, :); % dp/dx = 0 at x = 0
p(Nx, :) = 0; % p = 0 at x = L
p(:, 1) = p(:, 2); % dp/dy = 0 at y = 0
p(:, Ny) = p(:, Ny-1); % dp/dy = 0 at y = D
p(round(5*Nx/L:6*Nx/L), round(7*Ny/D:8*Ny/D)) = 0; % box geometry
p(round(5*Nx/L), round(7*Ny/D:8*Ny/D)) = p(round(5*Nx/L)-1, round(7*Ny/D:8*Ny/D)); % left side
p(round(6*Nx/L), round(7*Ny/D:8*Ny/D)) = p(round(6*Nx/L)+1, round(7*Ny/D:8*Ny/D)); % right side
p(round(5*Nx/L:6*Nx/L), round(7*Ny/D)) = p(round(5*Nx/L:6*Nx/L), round(7*Ny/D)-1); % bottom side
p(round(5*Nx/L:6*Nx/L), round(8*Ny/D)) = p(round(5*Nx/L:6*Nx/L), round(8*Ny/D)+1); % top side
un = u;
vn = v;
u(i, j) = un(i, j)-dt/(2 * h)*(un(i, j).*(un(i+1, j)-un(i-1, j))+vn(i, j).*(un(i, j+1)-un(i, j-1))) ...
-dt/(2*rho*h)*(p(i+1, j)-p(i-1, j)) ...
+nu*dt/h^2*(un(i+1, j)+un(i-1, j)+un(i, j+1)+un(i, j-1)-4*un(i, j)); % x-momentum
u(1, :) = Uinf; % u = Uinf at x = L
u(Nx, :) = u(Nx-1, :); % du/dx = 0 at x = L
u(:, 1) = 0; % u = 0 at y = 0
u(:, Ny) = 0; % u = 0 at y = D
u(round(5*Nx/L:6*Nx/L), round(7*Ny/D:8*Ny/D)) = 0; % box geometry
u(round(5*Nx/L), round(7*Ny/D:8*Ny/D)) = 0; % left side
u(round(6*Nx/L), round(7*Ny/D:8*Ny/D)) = 0; % right side
u(round(5*Nx/L:6*Nx/L), round(7*Ny/D)) = 0; % bottom side
u(round(5*Nx/L:6*Nx/L), round(8*Ny/D)) = 0; % top side
v(i, j) = vn(i, j)-dt/(2*h)*(un(i, j).*(vn(i+1, j)-vn(i-1, j))+vn(i, j).*(vn(i, j+1)-vn(i, j-1))) ...
-dt/(2*rho*h)*(p(i, j+1)-p(i, j-1)) ...
+ nu*dt/h^2*(vn(i+1, j)+vn(i-1, j)+vn(i, j+1)+vn(i, j-1)-4*vn(i, j)); % y-momentum
v(1, :) = 0; % v = 0 at x = L
v(Nx, :) = v(Nx-1, :); % dv/dx = 0 at x = L
v(:, 1) = 0; % v = 0 at y = 0
v(:, Ny) = 0; % v = 0 at y = D
v(round(5*Nx/L:6*Nx/L), round(7*Ny/D:8*Ny/D)) = 0; % box geometry
v(round(5*Nx/L), round(7*Ny/D:8*Ny/D)) = 0; % left side
v(round(6*Nx/L), round(7*Ny/D:8*Ny/D)) = 0; % right side
v(round(5*Nx/L:6*Nx/L), round(7*Ny/D)) = 0; % bottom side
v(round(5*Nx/L:6*Nx/L), round(8*Ny/D)) = 0; % top side
end
%% post-processing
velocity_magnitude = sqrt(u.^2 + v.^2); % velocity magnitude
u1 = u; % u-velocity for plotting with box
v1 = v; % v-velocity for plotting with box
p1 = p; % p-velocity for plotting with box
u1(round(5*Nx/L:6*Nx/L), round(7*Ny/D:8*Ny/D)) = NaN; % step geometry
v1(round(5*Nx/L:6*Nx/L), round(7*Ny/D:8*Ny/D)) = NaN; % step geometry
p1(round(5*Nx/L:6*Nx/L), round(7*Ny/D:8*Ny/D)) = NaN; % step geometry
velocity_magnitude1 = sqrt(u1.^2 + v1.^2); % velocity magnitude with box
%% Visualize velocity vectors and pressure contours
hold on, axis off
contourf(X, Y, u1', 64, 'LineColor', 'none'); % contour plot
set(gca, 'FontSize', 20)
hh = streamslice(X, Y, u1', v1', 20); % streamlines
set(hh, 'Color', 'k','LineWidth', 01);
colorbar; % add color bar
colormap jet % set color map
axis equal % set true scale
xlim([0 L]); % set axis limits
ylim([2 13]);
xticks([0 L]) % set ticks
yticks([0 D]) % set ticks
xlabel('x [m]');
ylabel('y [m]');
Cite as:
Fahad Butt (2024). Flow Around Square Cylinder (https://fluiddynamicscomputer.blogspot.com/), Blogger. Retrieved Month Date, Year
Copyright <2024> <Fahad Butt>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Python version:
As the original series is in Python, here is the Python code for Step 14 of 12. Also, I removed mixed terms from pressure poisson equation, just because 😁.
# Copyright <2024> <FAHAD BUTT>
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#%% import libraries
import numpy as np
import matplotlib.pyplot as plt
#%% define spatial and temporal grids
l_square = 1 # length of square
h = l_square / 10 # grid spacing
dt = 10 # time step
L = 35 # domain length
D = 21 # domain depth
Nx = round(L / h) + 1 # grid points in x-axis
Ny = round(D / h) + 1 # grid points in y-axis
nu = 0.000015111 # kinematic viscosity
Uinf = 0.0015111 # free stream velocity / inlet velocity / lid velocity
cfl = dt * Uinf / h # cfl number
travel = 10 # times the disturbance travels entire length of computational domain
TT = travel * L / Uinf # total time
ns = int(TT / dt) # number of time steps
Re = round(l_square * Uinf / nu) # Reynolds number
rho = 1.2047 # fluid density
#%% initialize flowfield
u = Uinf * np.ones((Nx, Ny)) # x-velocity
v = np.zeros((Nx, Ny)) # y-velocity
p = np.zeros((Nx, Ny)) # pressure
X, Y = np.meshgrid(np.linspace(0, L, Nx), np.linspace(0, D, Ny)) # spatial grid
#%% Solve 2D Navier-Stokes equations
for nt in range(ns):
pn = p.copy()
p[1:-1, 1:-1] = (pn[2:, 1:-1] + pn[:-2, 1:-1] + pn[1:-1, 2:] + pn[1:-1, :-2]) / 4 - h * rho / (8 * dt) * (u[2:, 1:-1] - u[:-2, 1:-1] + v[1:-1, 2:] - v[1:-1, :-2]) # pressure
# boundary conditions for pressure
p[0, :] = p[1, :] # dp/dx = 0 at x = 0
p[-1, :] = 0 # p = 0 at x = L
p[:, 0] = p[:, 1] # dp/dy = 0 at y = 0
p[:, -1] = p[:, -2] # dp/dy = 0 at y = D
p[round(10 * Nx / L):round(11 * Nx / L), round(10 * Ny / D):round(11 * Ny / D)] = 0 # box geometry
p[round(10 * Nx / L), round(10 * Ny / D):round(11 * Ny / D)] = p[round(10 * Nx / L) - 1, round(10 * Ny / D):round(11 * Ny / D)] # box left
p[round(11 * Nx / L), round(10 * Ny / D):round(11 * Ny / D)] = p[round(11 * Nx / L) + 1, round(10 * Ny / D):round(11 * Ny / D)] # box right
p[round(10 * Nx / L):round(11 * Nx / L), round(10 * Ny / D)] = p[round(10 * Nx / L):round(11 * Nx / L), round(10 * Ny / D) - 1] # box bottom
p[round(10 * Nx / L):round(11 * Nx / L), round(11 * Ny / D)] = p[round(10 * Nx / L):round(11 * Nx / L), round(11 * Ny / D) + 1] # box top
un = u.copy()
vn = v.copy()
u[1:-1, 1:-1] = (un[1:-1, 1:-1] - dt / (2 * h) * (un[1:-1, 1:-1] * (un[2:, 1:-1] - un[:-2, 1:-1]) + vn[1:-1, 1:-1] * (un[1:-1, 2:] - un[1:-1, :-2])) - dt / (2 * rho * h) * (p[2:, 1:-1] - p[:-2, 1:-1]) + nu * dt / h**2 * (un[2:, 1:-1] + un[:-2, 1:-1] + un[1:-1, 2:] + un[1:-1, :-2] - 4 * un[1:-1, 1:-1])) # x momentum
# boundary conditions for x-velocity
u[0, :] = Uinf # u = Uinf at x = L
u[-1, :] = u[-2, :] # du/dx = 0 at x = L
u[:, 0] = Uinf # u = 0 at y = 0
u[:, -1] = Uinf # u = 0 at y = D
u[round(10 * Nx / L):round(11 * Nx / L), round(10 * Ny / D):round(11 * Ny / D)] = 0 # box geometry
u[round(10 * Nx / L), round(10 * Ny / D):round(11 * Ny / D)] = 0 # box left
u[round(11 * Nx / L), round(10 * Ny / D):round(11 * Ny / D)] = 0 # box right
u[round(10 * Nx / L):round(11 * Nx / L), round(10 * Ny / D)] = 0 # box bottom
u[round(11 * Nx / L):round(11 * Nx / L), round(11 * Ny / D)] = 0 # box top
v[1:-1, 1:-1] = (vn[1:-1, 1:-1] - dt / (2 * h) * (un[1:-1, 1:-1] * (vn[2:, 1:-1] - vn[:-2, 1:-1]) + vn[1:-1, 1:-1] * (vn[1:-1, 2:] - vn[1:-1, :-2])) - dt / (2 * rho * h) * (p[1:-1, 2:] - p[1:-1, :-2]) + nu * dt / h**2 * (vn[2:, 1:-1] + vn[:-2, 1:-1] + vn[1:-1, 2:] + vn[1:-1, :-2] - 4 * vn[1:-1, 1:-1])) # y momentum
# boundary conditions for y-velocity
v[0, :] = 0 # v = 0 at x = L
v[-1, :] = v[-2, :] # dv/dx = 0 at x = L
v[:, 0] = 0 # v = 0 at y = 0
v[:, -1] = 0 # v = 0 at y = D
v[round(10 * Nx / L):round(11 * Nx / L), round(10 * Ny / D):round(11 * Ny / D)] = 0 # box geometry
v[round(10 * Nx / L), round(10 * Ny / D):round(11 * Ny / D)] = 0 # box left
v[round(11 * Nx / L), round(10 * Ny / D):round(11 * Ny / D)] = 0 # box right
v[round(10 * Nx / L):round(11 * Nx / L), round(10 * Ny / D)] = 0 # box bottom
v[round(11 * Nx / L):round(11 * Nx / L), round(11 * Ny / D)] = 0 # box top
#%% post-processing
velocity_magnitude = np.sqrt(u**2 + v**2) # velocity magnitude
u1 = u.copy() # u-velocity for plotting with box
v1 = v.copy() # v-velocity for plotting with box
p1 = p.copy() # p-velocity for plotting with box
# box geometry for plotting
u1[round(10 * Nx / L):round(11 * Nx / L), round(10 * Ny / D):round(11 * Ny / D)] = np.nan
v1[round(10 * Nx / L):round(11 * Nx / L), round(10 * Ny / D):round(11 * Ny / D)] = np.nan
p1[round(10 * Nx / L):round(11 * Nx / L), round(10 * Ny / D):round(11 * Ny / D)] = np.nan
velocity_magnitude1 = np.sqrt(u1**2 + v1**2) # velocity magnitude with box
# visualize velocity vectors and pressure contours
plt.figure(dpi = 500)
plt.contourf(X, Y, u1.T, 128, cmap = 'hsv')
# plt.colorbar()
plt.gca().set_aspect('equal', adjustable='box')
plt.xticks([0, L])
plt.yticks([0, D])
plt.xlabel('x [m]')
plt.ylabel('y [m]')
plt.show()
plt.figure(dpi = 500)
plt.contourf(X, Y, v1.T, 128, cmap = 'turbo')
# plt.colorbar()
plt.gca().set_aspect('equal', adjustable='box')
plt.xticks([0, L])
plt.yticks([0, D])
plt.xlabel('x [m]')
plt.ylabel('y [m]')
plt.show()
plt.figure(dpi = 500)
plt.contourf(X, Y, p1.T, 128, cmap = 'jet')
# plt.colorbar()
plt.gca().set_aspect('equal', adjustable='box')
plt.xticks([0, L])
plt.yticks([0, D])
plt.xlabel('x [m]')
plt.ylabel('y [m]')
plt.show()
References
[1] Khademinejad, Taha & Talebizadeh Sardari, Pouyan & Rahimzadeh, Hassan. (2015). Numerical Study of Unsteady Flow around a Square Cylinder in Compare with Circular Cylinder.
[2] Sohankar, A., Norbergb, C., Davidson, L., Numerical simulation of unsteady low-Reynolds number flow around rectangular cylinders at incidence, Journal of Wind Engineering and Industrial Aerodynamics, 69–71 (1997) 189-201.
[3] Cheng, M., Whyte, D. S., Lou, J., Numerical simulation of flow around a square cylinder in uniform-shear flow, Journal of Fluids and Structures, 23 (2007) 207–226.
[4] Lam, K., Lin, Y. F., L. Zou, Y. Liu, Numerical study of flow patterns and force characteristics for square and rectangular cylinders with wavy surfaces, Journal of Fluids and Structures, 28 (2012) 359–377.