Tuesday 7 May 2024

14th Step of the 12 steps to Navier-Stokes 😑

     After the tremendous success of 13th Step (thanks to the two people who read it, don't understand why? ðŸ˜‚) The 14th step now exists! This case is called the case of flow around an obstacle! Like a box. ⬜ This is an unofficial continuation to this. If I get it approved by Dr. Barba, then it will be official. The original series is in Python but I coded this in MATLAB without using many MATLAB specific functions so the code can be translated to other programing languages 🖧 quite easily 😊.

     In terms of validation, Strouhal number is at 0.145 [1-4] for flow around a square cylinder at Re 100. This code gives St of 0.141. 🤓 Fig. 1 shows results from the code.

Fig. 1, Post processing

Code

%% clear and close
close all
clear
clc
beep off % annoying beep off :)
%% 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.

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.