Showing posts with label Flow. Show all posts
Showing posts with label Flow. Show all posts

Saturday, 21 December 2024

2D Heat Equation Code (Finite Difference Method)

     After 🎉 successfully teaching 👨‍🏫 the neural network about the 🔥 heat / diffusion 💉 equation, yours truly thought it is the time ⏱️ to write a simple code 🖥️ for discretized domain as well. A code yours truly created in abundant spare time is mentioned in this blog 📖.

     A complex (sinusoidal) 🌊 boundary condition is implemented. Dirichlet and Neumann boundary conditions are also implemented. The code is vectorized ↗ so there is only one loop 😁.  CFL condition is implemented in the time-step calculation so time-step ⏳ is adjusted based on the mesh size automatically 😇.

     For simple geometries, traditional numerical methods are is still better than PINNs. 🧠

Code

#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 numpy as np
import matplotlib.pyplot as plt
# mesh parameters
L = 1 # length of plate
D = np.pi # width of plate
h = 1 / 50 # grid size
Nx = int((L / h) + 1) # grid points in x-axis
Ny = int((D / h) + 1) # grid points in y-axis
alpha = 1 # thermal diffusivity
dt = h**2 / (4 * alpha) # time step (based on CFL condition)
total_time = 1 # total time
nt = int(total_time / dt) # total time steps
# initialization
T = np.zeros((Nx, Ny)) # initial condition
T_new = np.zeros_like(T)
x = np.linspace(0, L, Nx)
y = np.linspace(0, D, Ny)
# solve 2D-transient heat equation
for n in range(nt):
    T_new[1:-1, 1:-1] = T[1:-1, 1:-1] + ((alpha * dt) / h**2) * (T[2:, 1:-1] + T[:-2, 1:-1] + T[1:-1, 2:] + T[1:-1, :-2] - 4 * T[1:-1, 1:-1])
    T[:, :] = T_new
    # apply boundary conditions
    T[0, :] = np.sin(2 * np.pi * y / D) # T = sin(y) at x = 0
    T[-1, :] = T[-2, :] # dT/dx = 0 at x = L
    T[:, 0] = 0 # T = 0 at y = 0
    T[:, -1] = 0 # T = 0 at y = D
# plotting
X, Y = np.meshgrid(x, y, indexing="ij")
plt.figure(dpi = 500)
plt.contourf(X, Y, T, levels = 64, cmap = "jet")
plt.gca().set_aspect('equal', adjustable = 'box')
plt.colorbar(label = "Temperature")
plt.title("Temperature Distribution")
plt.xlabel("x")
plt.ylabel("y")
plt.show()

     The code creates the output as shown in Fig. 1.



Fig. 1, Temperature distribution

     Thank you for reading! If you want to hire me as a post-doc researcher in the fields of thermo-fluids and / or fracture mechanics, do reach out!

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 they even bothered? 😂) 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.

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.

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

Sunday, 22 March 2020

Hypersonic Flow over a Two Dimensional Heated Cylinder

     This post is about the simulation of hypersonic flow over a heated circular cylinder, in two dimensions.

     Equation 1 is used as a relationship between Mach and the Reynold number.

M= Re*μ*√(R*T) ÷ d*P*√γ     (1)

     w.r.t. equation 1, the parameters represent the following quantities.

     M     Freestream Mach number at 17.6
     Re    Reynolds number at 376,000
     μ     Dynamic viscosity at 1.329045e-5 Ns.m-2
     R     Specific gas constant at 286.9 J.(kg.K)-1
     T     Freestream temperature 200 K
     d     Cylinder diameter at 5.6730225e-4 m
     P     Freestream pressure at 101325 Pa
     γ     Specific heat ratio at 1.4
     Tw  Wall temperature of cylinder at 500 K
     Pr    Prandtl number at 0.736

     The boundary conditions were taken from [1]. A comparison with [1] is shown in Fig. 1. Inside Fig. 1, the red dotted line with circles represents the data from [1]. The black solid line represents the data from the present simulation. Within Fig. 1, 0° represents the stagnation point. The velocity, pressure, Mach number and temperature contours are shown in Fig. 2.


Fig. 1 A comparison with previous research [1].


Fig. 2, Top Row, L-R: Velocity and pressure contours. Bottom Row, L-R: Mach number and temperature contours.

The computational mesh and the computational domain with boundary conditions visible are shown in Fig. 3-4, respectively. The computational domain had a size of 20D x 20D. The mesh had 836,580 total cells and 944 cells were located at the solid fluid boundary. Several local mesh controls were employed to capture the shockwave properly.


Fig. 3, The computational mesh.


Fig. 4, The computational domain.

     The solution method is Finite Volume method. SIMPLE-R is the solver employed. Implicit central difference scheme for diffusion terms, second-order Upwind scheme for convective terms and first-order implicit for temporal terms are used. The mesh created uses the Cartesian mesh with Immersed Boundary method.


     Reference:

     Thank you for reading. If you would like to collaborate on research projects, please reach out. I am looking for a PhD position, any guidance would be appreciated.

Monday, 17 March 2014

Comparison between Down-Force and Drag Produced by a Legacy Spoiler VS a Spoiler with Tubercles (Humpback Whale Fin's Inspired)

Following data was obtained from Simulations carried out in SolidWorks Flow Simulation Premium.

Without Bumps

Air Speed in Km/h

Down Force in N

Drag in N

120
98.682
33.234
110
82.88
27.957
100
68.266
23.02
90
55.299
18.668
80
43.529
14.697
70
33.284
11.255
60
24.438
8.272
50
16.982
5.769
40
10.83
3.688
30
6.08
2.081
20
2.681
0.929
10
0.648
0.235


With Bumps

Air Speed in Km/h

Down Force in N

Drag in N

120
108.238
30.47
110
90.599
25.549
100
74.818
21.047
90
60.423
17.014
80
47.695
13.443
70
36.441
10.27
60
26.682
7.532
50
18.504
5.228
40
11.82
3.352
30
6.613
1.886
20
2.909
0.841
10
0.685
0.211

Comparison between Down Force and Drag

Air Speed in Km/h
Percentage Less Drag
Percentage More Down Force
120
8.32
8.83
110
8.61
8.51
100
8.57
8.76
90
8.86
8.48
80
8.53
8.73
70
8.75
8.66
60
8.95
8.41
50
9.38
8.23
40
9.11
8.38
30
9.37
8.06
20
9.47
7.84
10
10.21
5.4





It is clear that the spoiler with humpback whale's fin's inspired profile not only produce more down force at a particular velocity but also less drag.

Data for Spoiler without Humpback Whale's Fin's Inspired Bumps:

Wing Span: 100 cm
Chord Length: 17.5 cm
Air Velocity: 0-120 Km/h head on
Vertical Pitch: 22.5 Degree Downwards
Gravity Considered
Fluid: Dry Air at STP
Mesh Settings: Coarse (3/10)


Data for Spoiler with Humpback Whale's Fin's Bumps:

Wing Span: 100 cm
Chord Length Large: 17.5 cm
Chord Length Small: 15.75 cm
Air Velocity: 0-120 Km/h head on
Vertical Pitch: 22.5 Degree Downwards
Gravity Considered
Fluid: Dry Air at STP
Mesh Settings: Coarse (3/10)



Let's now take a look at visual representation of data.


This Plot Shows Air Velocity VS Drag, Down-Force by the Spoiler without Bumps


This Plot Shows Air Velocity VS Drag, Down-Force by the Spoiler with Bumps

As you can see from above two plots; the spoiler with the whale's fin like profile generates more down force and less drag.



This Plot Shows Air Velocity VS Down-Force Generated by the Spoilers

The green line represents the Down-Force generated by the spoiler with whale's fin's inspired design. It is around eight percent more at each velocity.


This Plot Shows Air Velocity VS Drag Generated by the Spoilers

The green line represents the Drag generated by the spoiler with whale's fin inspired design. It is around nine percent less at each velocity.


This Plot Shows Air velocity VS Down-Force to Drag Ratio

It is clear from this plot that Down-Force to Drag ratio is around sixteen percent more for whale's fin's inspired spoiler than the legacy one at each velocity.



This Plot Shows Air Flow Around the Spoiler without Bumps at 120 Km/h from the Right Side.


This Plot Shows Air Flow Around the Spoiler without Bumps at 120 Km/h.


This Plot Shows Air Flow Around the Spoiler with bumps at 120 Km/h.


This plot Shows Air Flow Around the Spoiler with bumps at 120 Km/h.

A simple stress analysis was carried out on both spoilers at 120 Km/h. FOS was greater than 1 for both cases.

Advantages of Spoilers:

The main benefit of installing a spoiler on a car is to help it maintain traction at very high speeds. Particularly at speeds around 90 Km/h. A car with a spoiler installed will be easier to handle at highway speeds. Rear spoilers such as the one's analysed in this study; push the back of the car down so the tires can grip the road better and increase stability. It also increases the braking ability of the car.

To build the prototypes and complete the study further, I need donations. To donate your part send an email to fadoobaba@live.com , tweet @fadoobaba, PM at https://www.facebook.com/ThreeDimensionalDesign orhttps://grabcad.com/fahad.rafi.butt or comment with your contact details and I will contact you!. Thank you for reading!

Do comment and share!