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.

Wednesday 3 April 2024

Airflow Simulation in Empty / Occupied Rooms and Environments

     One fine morning, I decided to code the Navier–Stokes equations [read if you are bored πŸ€£] . This post has the results of  flow simulation inside close environments of various aspect ratios. As is customary with all my CFD work using commercial and 🏑made CFD codes, this too is inspired by the free lectures of Dr. Lorena Barba.

The first case has an aspect ratio of 1:1 while for the second case, the aspect ratio is at 3:1. The airflow is at 0.4555 m/s for both cases. Both cases are isothermal at 293 K. There is no turbulence 🌬model [free code ] πŸ€‘.

The smallest resolved scale (~4x smallest mesh size) for 1:1 case is ~ 0.0045 m and for the 3:1 case is at 0.0112 m. Time scales (~4x time-step size) are at 0.0004 s and 0.0004 s, respectively. Fig. 1 shows results for 1:1 aspect ratio while the results for 3:1 case are shown in Fig. 2. For both cases, flow enters from top-left and exists at bottom-right of the rooms. The boundary conditions are taken from [1]. I compared the results with Fluent simulations I ran at same boundary conditions and stopped my simulations when eye-balling didn't revealed any difference πŸ˜†. What you expect? This is a blog not a journal 😝.


Fig. 1, 1:1 aspect ratio


Fig. 2, 3:1 aspect ratio

 
Fig. 3, 3:1 aspect ratio with partition


Fig. 4, Flow inside ducts


     If you want to hire me as your PhD student in the research projects related to turbo-machinery, aerodynamics, renewable energy, please reach out. Thank you very much for reading.

References

     [1] Horikiri, Kana & Yao, Yufeng. Validation Study of Convective Airflow in an Empty Room, "Recent Researches in Energy, Environment, Devices, Systems, Communications and Computers", ISBN: 978-960-474-284-4

Sunday 31 March 2024

13th Step of the 12 steps to Navier-Stokes πŸ˜‘

     Indeed, the 13th step now exists! This case is called the case of the backward facing step (BFS)! ⬜ 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.

The Code

%% clear and close
close all
clear
clc
%% define spatial and temporal grids
l_square = 1; % length of square
h = l_square/50; % grid spacing
dt = 0.1; % time step
L = 21; % cavity length
D = 2; % 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.0060444; % free stream velocity / inlet velocity / lid velocity
cfl = dt*Uinf/h; % % cfl number
travel = 4; % 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 = zeros(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(1:1*Nx/L), round(1:1*Ny/D)) = 0; % step geometry
p(round(1*Nx/L), round(1:1*Ny/D)) = p(round(1*Nx/L)+1, round(1:1*Ny/D)); % dp/dx = 0 at x = 1 and y = 0 to 1
p(1:round(1*Nx/L), round(1*Ny/D)) = p(1:round(1*Nx/L), round(1*Ny/D)+1); % dp/dy = 0 at x = 0 to 1 and y = 1
p(1:round(1*Nx/L), 1) = p(1:round(1*Nx/L), 2); % dp/dy = 0 at x = 0 to 1 and y = 1
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(round(1:1*Nx/L), round(1:1*Ny/D)) = 0; % step geometry
u(1, round(1:1*Ny/D)) = 0; % u = 0 at x = 0 and y = 0 to 1
u(1, round(1*Ny/D:2*Ny/D)) = Uinf; % u = Uinf at x = 0 and y = 1 to 2
u(round(1*Nx/L), round(1:1*Ny/D)) = 0; % u = 0 at x = 1 and y = 0 to 1
u(1:round(1*Nx/L), round(1*Ny/D)) = 0; % u = 0 at x = 0 to 1 and y = 1
u(1:round(1*Nx/L), 1) = 0; % u = 0 at x = 0 to 1 and y = 1
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
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(round(1:1*Nx/L), round(1:1*Ny/D)) = 0; % step geometry
v(1, round(1:1*Ny/D)) = 0; % v = 0 at x = 0 and y = 0 to 1
v(1, round(1*Ny/D:2*Ny/D)) = 0; % v = Uinf at x = 0 and y = 1 to 2
v(round(1*Nx/L), round(1:1*Ny/D)) = 0; % v = 0 at x = 1 and y = 0 to 1
v(1:round(1*Nx/L), round(1*Ny/D)) = 0; % v = 0 at x = 0 to 1 and y = 1
v(1:round(1*Nx/L), 1) = 0; % v = 0 at x = 0 to 1 and y = 1
v(Nx, :) = v(Nx-1, :); % dv/dx = 0 at x = L
v(:, 1) = 0; % u = 0 at y = 0
v(:, Ny) = 0; % u = 0 at y = D
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(1:round(1*Nx/L) , 1:round(1*Ny/D)) = NaN; % step geometry
v1(1:round(1*Nx/L) , 1:round(1*Ny/D)) = NaN; % step geometry
p1(1:round(1*Nx/L) , 1:round(1*Ny/D)) = NaN; % step geometry
velocity_magnitude1 = sqrt(u1.^2 + v1.^2); % velocity magnitude with box
%% Visualize velocity vectors and pressure contours
hold on
contourf(X, Y, u1', 64, 'LineColor', 'none'); % contour plot
set(gca, 'FontSize', 20)
% skip = 20;
% quiver(X(1:skip:end, 1:skip:end), Y(1:skip:end, 1:skip:end),...
% u1(1:skip:end, 1:skip:end)', v1(1:skip:end, 1:skip:end)', 1, 'k','LineWidth', 0.1); % Velocity vectors
% hh = streamslice(X, Y, u1', v1',2); % Streamlines
% set(hh, 'Color', 'k','LineWidth', 01);
colorbar; % Add color bar
colormap parula % Set color map
axis equal % Set true scale
xlim([0 L]); % Set axis limits
ylim([0 D]);
xticks([0 9 L]) % Set ticks
yticks([0 D]) % Set ticks
xt = [0 21]; % draw top wall
yt = [2 2];
xb = [1 21]; % draw bottom wall
yb = [0 0];
xbox = [0 1 1 0 0]; % draw box
ybox = [0 0 1 1 0];
plot(xbox, ybox, 'k', 'LineWidth', 2)
plot(xt, yt, 'k', 'LineWidth', 2)
plot(xb, yb, 'k', 'LineWidth', 2)
% clim([0 max(velocity_magnitude(:))]) % legend limits
% title('Velocity [m/s]');
xlabel('x [m]');
ylabel('y [m]');

     The results from this code at Re 400 are presented in Fig. 1. The re-attachment length is ~8 m from the trailing-edge of the box, which is same as previously published results, from example in [1].

Fig. 1, post-processes results

     Thank you for reading! If you want to hire me as your next PhD student, please do reach out!

References

 [1] Irisarri, D., Hauke, G. Stabilized virtual element methods for the unsteady incompressible Navier–Stokes equations. Calcolo 56, 38 (2019). https://doi.org/10.1007/s10092-019-0332-5

Thursday 12 October 2023

Saithe Fish UDF (ANSYS Fluent)

     This post is about Fish Simulation in ANSYS Fluent using a User Defined Function (UDF). The UDF is mentioned below. The flow conditions are taken from [1]. This goes with the videos shown in Fig. 1-2. The CAD files for t=0 are available here (for UDF 01).

Fig. 1, Animation of motion achieved through the UDF 01.

 
 Fig. 2, Animation of motion achieved through the UDF 02 (Validated).

     The results of present simulations are compared with [1]. The results are in excellent agreement as the Cl, max from [1] is at 1.57 while the maximum Cl, max from present simulation is at 1.6. The drag coefficient [1], Cd, max in [1] is at 0.164; while from the present simulations I got, 0.151. The Cd, avg comes out to be 0.072 [1] form the present simulations. I got a value of 0.064 from he present simulation. These would gradually become more accurate with mesh refinement, which I will certainly do if I send some ideas I have for peer review.

UDF 01:

#include "udf.h"
#include "unsteady.h"
#include "dynamesh_tools.h"
#include "math.h"


DEFINE_GRID_MOTION(dynamic,domain,dt,time,dtime)
{
 Thread *tf = DT_THREAD(dt);
 face_t f;
 Node *v;
 int n;
 double x, y, y_ref_previous, y_ref;
 SET_DEFORMING_THREAD_FLAG(THREAD_T0(tf));  
 begin_f_loop(f,tf) {
  f_node_loop(f,tf,n) {
   v = F_NODE(f,tf,n);
   if (NODE_POS_NEED_UPDATE(v)) {
    NODE_POS_UPDATED(v);
    x = NODE_X(v);
    real amplitude = 0.02 + 0.01*x + 0.1*x*x;
    y_ref_previous = amplitude * cos(2*M_PI*x + 2*M_PI*0.8*(PREVIOUS_TIME));
    y_ref = amplitude * cos(2*M_PI*x + 2*M_PI*0.8*(CURRENT_TIME));
     
if (NODE_Y(v) > y_ref_previous){
     NODE_Y(v) = y_ref+fabs(NODE_Y(v)-y_ref_previous);
    }
    else 
     if (NODE_Y(v) < y_ref_previous){
      NODE_Y(v) = y_ref-fabs(NODE_Y(v)-y_ref_previous);
     }
     else {
      NODE_Y(v) = y_ref;
     }
    }
   }
  }
 }
 end_f_loop(f,tf);

UDF 02 (Validated):

#include "udf.h"

DEFINE_GRID_MOTION(dynamic,domain,dt,time,dtime)
{
 Thread *tf = DT_THREAD(dt);
 face_t f;
 Node *v;
 int n;
 double x, y_ref_previous, y_ref, amplitude, fr;
 SET_DEFORMING_THREAD_FLAG(THREAD_T0(tf));  
 begin_f_loop(f,tf) {
  f_node_loop(f,tf,n) {
   v = F_NODE(f,tf,n);
   if (NODE_POS_NEED_UPDATE(v)) {
    NODE_POS_UPDATED(v);
    x = fabs(NODE_X(v));
    amplitude = 0.02 - 0.0825 * x + 0.1625 * x * x;
    fr = 2;
    y_ref_previous = amplitude * cos(2 * M_PI * x - 2 * M_PI * fr * (PREVIOUS_TIME));
    y_ref = amplitude * cos(2 * M_PI * x - 2 * M_PI * fr * (CURRENT_TIME));
     
if (NODE_Y(v) > y_ref_previous){
     NODE_Y(v) = y_ref + fabs(NODE_Y(v) - y_ref_previous);
    }
    else 
     if (NODE_Y(v) < y_ref_previous){
      NODE_Y(v) = y_ref - fabs(NODE_Y(v) - y_ref_previous);
     }
     else {
      NODE_Y(v) = y_ref;
     }
    }
   }
  }
 }
 end_f_loop(f,tf);

If you want to hire me as you next shining PhD/Master student or collaborate in research, please reach out! Thank you for reading!

References

[1] Gen-Jin Dong, Xi-Yun Lu; Characteristics of flow over traveling wavy foils in a side-by-side arrangement. Physics of Fluids 1 May 2007; 19 (5): 057107. https://doi.org/10.1063/1.2736083