Showing posts with label Simulation. Show all posts
Showing posts with label Simulation. Show all posts

Thursday, 20 February 2025

Flow Simulation around Shapes (Includes Free Code)

     This post is about the simulation of flow 🍃 around and through various objects 🪈 and obstacles ⭕. The simulated cases include flow through a partially blocked pipe, shown in Fig. 1. Flow around flat plates arranged in the shape of the letter "T", shown in Fig. 2. Flow around a wedge and flow around a triangle 🔺, which are shown in Fig. 3 and 4 respectively.

     To create obstacles in Python 🐍, the Path statement is used. This is similar to the inpolygon statement in MATLAB 🧮. For example, the following code 🖳 is used to create an ellipse ⬭.

X, Y = np.meshgrid(np.linspace(0, L, Nx), np.linspace(0, D, Ny)) # spatial grid
theta = np.linspace(0, np.pi, 100) # create equally spaced angles from 0 to pi
a = 0.05 # semi-major axis (along x)
b = 0.025 # semi-minor axis (along y)
shape_center_x = L / 2 # shape center x
shape_center_y = 0 # ellipse center x
x1 = shape_center_x + (a * np.cos(theta)) # x-coordinates
y1 = shape_center_y + (b * np.sin(theta)) # y-coordinates
shape_path = Path(np.column_stack((x1, y1))) # shape region
points = np.vstack((X.ravel(), Y.ravel())).T # mark inside region
custom_curve = shape_path.contains_points(points).reshape(X.shape) # create boolean mask
custom_curve_boundary = np.zeros_like(custom_curve, dtype=bool) # find boundary of shape
for i in range(1, Nx-1):
    for j in range(1, Ny-1):
        if custom_curve.T[i, j]: # inside shape
            if (not custom_curve.T[i+1, j] or not custom_curve.T[i-1, j] or 
                not custom_curve.T[i, j+1] or not custom_curve.T[i, j-1]):
                custom_curve_boundary.T[i, j] = True # mark as boundary
boundary_indices = np.where(custom_curve_boundary.T) # mark boundary
right_neighbors = (boundary_indices[0] + 1, boundary_indices[1]) # right index
left_neighbors = (boundary_indices[0] - 1, boundary_indices[1]) # left index
top_neighbors = (boundary_indices[0], boundary_indices[1] + 1) # top index
bottom_neighbors = (boundary_indices[0], boundary_indices[1] - 1) # bottom index
valid_right = ~custom_curve.T[right_neighbors] # check if points are on shape boundary
valid_left = ~custom_curve.T[left_neighbors]
valid_top = ~custom_curve.T[top_neighbors]
valid_bottom = ~custom_curve.T[bottom_neighbors]

     The arrays x1 and y1 are created using the equation for ellipse with the required parameters. The ellipse region is marked using the Path statement. Indices inside the ellipse are marked using a Boolean mask. Nested loops are used to mark the ellipse boundary using a curve. The right, left, top and bottom neighbor points are identified to be used for the application of Neumann boundary conditions for pressure (no-slip). Within the time loop, "where" statement is used to identify and apply the Neumann boundary conditions on the ellipse wall. The same method applied on any shape, for example aero-foils, circles, nozzles etc. The complete code to reproduce Fig. 1 is made available 😇. 

     It should be noted that, that the mesh is still stairstep. It doesn't matter how small the mesh resolution 😲. This code is developed for educational and research purposes only as there is not much application to stairstep mesh in real world❗

Code

#Copyright <2025> <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
from matplotlib.path import Path
#%% define parameters
l_square = 1 # length of square
h = 0.025 / 25 # grid spacing
dt = 0.00001 # time step
L = 0.3 # domain length
D = 0.05 # domain depth
Nx = round(L / h) + 1 # grid points in x-axis
Ny = round(D / h) + 1 # grid points in y-axis
nu = 1 / 100 # kinematic viscosity
Uinf = 1 # free stream velocity / inlet velocity / lid velocity
cfl = dt * Uinf / h # cfl number
travel = 5 # 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
#%% initialize variables
u = np.zeros((Nx, Ny)) # x-velocity
v = np.zeros((Nx, Ny)) # y-velocity
p = np.zeros((Nx, Ny)) # pressure
#%% create a shape
X, Y = np.meshgrid(np.linspace(0, L, Nx), np.linspace(0, D, Ny)) # spatial grid
theta = np.linspace(0, np.pi, 100) # create equally spaced angles from 0 to pi
a = 0.05 # semi-major axis (along x)
b = 0.025 # semi-minor axis (along y)
shape_center_x = L / 2 # shape center x
shape_center_y = 0 # ellipse center x
x1 = shape_center_x + (a * np.cos(theta)) # x-coordinates
y1 = shape_center_y + (b * np.sin(theta)) # y-coordinates
shape_path = Path(np.column_stack((x1, y1))) # shape region
points = np.vstack((X.ravel(), Y.ravel())).T # mark inside region
custom_curve = shape_path.contains_points(points).reshape(X.shape) # create boolean mask
custom_curve_boundary = np.zeros_like(custom_curve, dtype=bool) # find boundary of shape
for i in range(1, Nx-1):
    for j in range(1, Ny-1):
        if custom_curve.T[i, j]: # inside shape
            if (not custom_curve.T[i+1, j] or not custom_curve.T[i-1, j] or 
                not custom_curve.T[i, j+1] or not custom_curve.T[i, j-1]):
                custom_curve_boundary.T[i, j] = True # mark as boundary
boundary_indices = np.where(custom_curve_boundary.T) # mark boundary
right_neighbors = (boundary_indices[0] + 1, boundary_indices[1]) # right index
left_neighbors = (boundary_indices[0] - 1, boundary_indices[1]) # left index
top_neighbors = (boundary_indices[0], boundary_indices[1] + 1) # top index
bottom_neighbors = (boundary_indices[0], boundary_indices[1] - 1) # bottom index
valid_right = ~custom_curve.T[right_neighbors] # check if points are on shape boundary
valid_left = ~custom_curve.T[left_neighbors]
valid_top = ~custom_curve.T[top_neighbors]
valid_bottom = ~custom_curve.T[bottom_neighbors]
#%% 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 / (8 * dt) * (u[2:, 1:-1] - u[:-2, 1:-1] + v[1:-1, 2:] - v[1:-1, :-2]) # pressure
    # apply pressure boundary conditions
    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 / 2
    p[custom_curve.T] = 0 # p = 0 inside shape
    # dp/dn = 0 at shape boundary (no slip)
    p[boundary_indices] = np.where(valid_right, p[right_neighbors], p[boundary_indices]) # right neighbor is fluid
    p[boundary_indices] = np.where(valid_left, p[left_neighbors], p[boundary_indices]) # left neighbor is fluid
    p[boundary_indices] = np.where(valid_top, p[top_neighbors], p[boundary_indices]) # top neighbor is fluid
    p[boundary_indices] = np.where(valid_bottom, p[bottom_neighbors], p[boundary_indices]) # bottom neighbor is fluid
    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 * h) * (p[2:, 1:-1] - p[:-2, 1:-1]) + (1 / Re) * 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
    # u boundary conditions
    u[0, :] = Uinf # u = Uinf at x = 0
    u[-1, :] = u[-2, :] # du/dx = 0 at x = L
    u[:, 0] = 0 # u = 0 at y = 0
    u[:, -1] = u[:, -2] # du/dy = 0 at y = D / 2
    u[custom_curve.T] = 0 # u = 0 inside shape
    u[custom_curve_boundary.T] = 0 # no slip
    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 * h) * (p[1:-1, 2:] - p[1:-1, :-2]) + (1 / Re) * 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
    # v boundary conditions
    v[0, :] = 0 # v = 0 at x = 0
    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 / 2
    v[custom_curve.T] = 0 # u = 0 inside shape
    v[custom_curve_boundary.T] = 0 # no slip
#%% post process
u1 = u.copy() # u-velocity for plotting with shape
v1 = v.copy() # v-velocity for plotting with shape
p1 = p.copy() # pressure for plotting with shape
# shape geometry for plotting
u1[custom_curve.T] = np.nan
v1[custom_curve.T] = np.nan
p1[custom_curve.T] = np.nan
velocity_magnitude1 = np.sqrt(u1**2 + v1**2) # velocity magnitude with shape
# visualize velocity vectors and pressure contours
plt.figure(dpi = 500)
plt.contourf(X, Y, u1.T, 128, cmap = 'jet')
plt.plot(x1, y1, color='black', alpha = 1, linewidth = 2)
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.axis('off')
plt.show()
plt.figure(dpi = 500)
plt.contourf(X, Y, v1.T, 128, cmap = 'jet')
plt.plot(x1, y1, color='black', alpha = 1, linewidth = 2)
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.axis('off')
plt.show()
plt.figure(dpi = 500)
plt.contourf(X, Y, p1.T, 128, cmap = 'jet')
plt.plot(x1, y1, color='black', alpha = 1, linewidth = 2)
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.axis('off')
plt.show()
plt.figure(dpi = 500)
plt.streamplot(X, Y, u1.T, v1.T, color = 'black', cmap = 'jet', density = 2, linewidth = 0.5, arrowstyle='->', arrowsize = 1)  # plot streamlines
plt.plot(x1, y1, color='black', alpha = 1, linewidth = 2)
plt.gca().set_aspect('equal', adjustable = 'box')
plt.axis('off')
plt.show()


Fig. 1, Flow in a clogged pipe


Fig. 2, Flow around a blunt obstacle


Fig. 3, Flow around asymmetric wedge



Fig. 4, Flow around a triangle

     Within Figs. 1 - 4, top row shows u and v components of velocity, bottom row shows pressure and streamlines 💫

     Thank you for reading! If you want to hire me as your next shinning post-doc, do let reach out!

Wednesday, 8 January 2025

CFD Wizardry: A 50-Line Python Marvel

     In abundant spare time ⏳, yours truly has implemented the non-conservative and non-dimensional form of the discretized Navier-Stokes 🍃 equations. The code 🖳 in it's simplest form is less than 50 lines including importing libraries and plotting! 😲 For validation, refer here. More examples and free code is available here, here and here. Happy codding!

Code

# Copyright <2025> <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
l_square = 1 # length of square
h = l_square / 500 # grid spacing
dt = 0.00002 # time step
L = 1 # domain length
D = 1 # domain depth
Nx = round(L / h) + 1 # grid points in x-axis
Ny = round(D / h) + 1 # grid points in y-axis
nu = 1 / 100 # kinematic viscosity
Uinf = 1 # free stream velocity / inlet velocity / lid velocity
cfl = dt * Uinf / h # cfl number
travel = 200 # 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
u = np.zeros((Nx, Ny)) # x-velocity
v = np.zeros((Nx, Ny)) # y-velocity
p = np.zeros((Nx, Ny)) # pressure
for nt in range(ns): # solve 2D Navier-Stokes equations
    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 / (8 * dt) * (u[2:, 1:-1] - u[:-2, 1:-1] + v[1:-1, 2:] - v[1:-1, :-2]) # pressure
    p[0, :] = p[1, :] # dp/dx = 0 at x = 0
    p[-1, :] = p[-2, :] # dp/dx = 0 at x = L
    p[:, 0] = p[:, 1] # dp/dy = 0 at y = 0
    p[:, -1] = 0 # p = 0 at y = D
    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 * h) * (p[2:, 1:-1] - p[:-2, 1:-1]) + (1 / Re) * 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
    u[0, :] = 0 # u = 0 at x = 0
    u[-1, :] = 0 # u = 0 at x = L
    u[:, 0] = 0 # u = 0 at y = 0
    u[:, -1] = Uinf # u = Uinf at y = D
    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 * h) * (p[1:-1, 2:] - p[1:-1, :-2]) + (1 / Re) * 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
    v[0, :] = 0 # v = 0 at x = 0
    v[-1, :] = 0 # v = 0 at x = L
    v[:, 0] = 0 # v = 0 at y = 0
    v[:, -1] = 0 # v = 0 at y = D
X, Y = np.meshgrid(np.linspace(0, L, Nx), np.linspace(0, D, Ny)) # spatial grid
plt.figure(dpi = 200)
plt.contourf(X, Y, v.T, 128, cmap = 'jet') # plot contours
plt.colorbar()
plt.streamplot(X, Y, u.T, v.T, color = 'black', cmap = 'jet', density = 2, linewidth = 0.5, arrowstyle='->', arrowsize = 1) # plot streamlines
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()

Lid-Driven Cavity

     The case of lid-driven cavity in the turbulent flow regime can now be solved in reasonable amount of time. The results are shown in Fig. 1. I stopped the code while the flow is still developing as you are reading a blog and not a Q1 journal. 😆 Within Fig. 1, streamlines, v and u component of velocity and pressure are shown going from left to right and top to bottom. At the center of Fig. 1, the velocity magnitude is superimposed. As this is DNS, the smallest spatial scale resolved is ~8e-3 m [8 mm]. While, the smallest time-scale ⌛ resolved is ~8e-4 s [0.8 ms].

Fig. 1, The results at Reynolds number of 10,000

Free-Jet

          The case of free jets in the turbulent flow regime can now be solved in reasonable amount of time. The results are shown in Fig. 2. I stopped the code while the flow is still developing. Once again, I remind you that you are reading a blog and not a Q1 journal. 😆 Within Fig. 2, streamlines and species are shown. As this is DNS, the smallest spatial scale resolved is ~0.02 m [2 cm]. While, the smallest time-scale ⌛ resolved is ~4e-4 s [0.4 ms]. The code for implementing species, in this case temperature using the energy equation is available on the previous post.

Fig. 2, Free jet at Reynolds number 10000

Heated Room

     The benchmark case of mixed convection in an open room in the turbulent flow regime can now be solved in reasonable amount of time as well. The results are shown in Fig. 3. I stopped the code while the flow field stopped showing any changes. 😆 As this is DNS, the smallest spatial scale resolved is ~0.0144 m [1.44 cm]. While, the smallest time-scale ⌛ resolved is ~1e-4 s [1 ms]. The code for implementing species, in this case temperature using the energy equation is available on the previous post. In the previous post, the momentum equation has no changes as the gravity vector is at 0 m/s2. For this example, Boussinesq assumption is used.

Fig. 3, Flow inside a heated room at Reynolds number of 5000

Backward - Facing Step (BFS)

     Another benchmark case of flow around a backwards facing step can now be solved in reasonable amount of time as well. The flow is fully turbulent. The results are shown in Fig. 4. I stopped the code while the flow field is still developing. 😆 As this is DNS, the smallest spatial scale resolved is ~0.01 m [1 cm]. While, the smallest time-scale ⌛ resolved is ~1e-4 s [1 ms]. As can be seen from Fig. 4, there are no abnormalities in the flow field.

Fig. 4, Flow around a backwards facing step at Reynolds number of 10000

PS: I fully understand, there is no such thing as 2D turbulence 🍃. Just don't kill the vibe please 💫.

Artificial Compressibility

     The artificial compressibility method is now implemented in the code. The output is flow inside the lid-driven cavity at Reynolds number 10,000. The smallest scale resolved is at 0.001 m and smallest time scale resolved is at 0.0001 s. This version of code seems to be more stable as compared to the one that uses pressure Poisson equation. The results are shown in Fig. 5.

Fig. 5, Look at all those secondary vortices 😚



#Copyright <2025> <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 parameters
l_cr = 1 # characteristic length
h = l_cr / 1000 # grid spacing
dt = 0.0001 # time step
L = 1 # domain length
D = 1 # domain depth
Nx = round(L / h) + 1 # grid points in x-axis
Ny = round(D / h) + 1 # grid points in y-axis
nu = 1 / 10000 # kinematic viscosity
Uinf = 1 # free stream velocity / inlet velocity / lid velocity
cfl = dt * Uinf / h # cfl number
travel = 20 # 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_cr * Uinf / nu) # Reynolds number

#%% intialization
u = np.zeros((Nx, Ny)) # x-velocity
v = np.zeros((Nx, Ny)) # y-velocity
p = np.zeros((Nx, Ny)) # pressure

#%% pre calculate for speed
P1 = dt / h
P2 = (2 / Re) * dt / h**2
P3 = 1 - (4 * P2)

#%% solve 2D Navier-Stokes equations
for nt in range(ns):
    pn = p.copy()
    p[1:-1, 1:-1] = pn[1:-1, 1:-1] - P1 * (u[2:, 1:-1] - u[:-2, 1:-1] + v[1:-1, 2:] - v[1:-1, :-2]) # pressure
    p[0, :] = p[1, :] # dp/dx = 0 at x = 0
    p[-1, :] = p[-2, :] # dp/dx = 0 at x = L
    p[:, 0] = p[:, 1] # dp/dy = 0 at y = 0
    p[:, -1] = p[:, -2] # dp/dy = 0 at y = D
    un = u.copy()
    vn = v.copy()
    u[1:-1, 1:-1] = un[1:-1, 1:-1] * P3 - P1 * (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]) + p[2:, 1:-1] - p[:-2, 1:-1]) + P2 * (un[2:, 1:-1] + un[:-2, 1:-1] + un[1:-1, 2:] + un[1:-1, :-2]) # x momentum
    u[0, :] = 0 # u = 0 at x = 0
    u[-1, :] = 0 # u = 0 at x = L
    u[:, 0] = 0 # u = 0 at y = 0
    u[:, -1] = Uinf # u = Uinf at y = D
    v[1:-1, 1:-1] = vn[1:-1, 1:-1] * P3 - P1 * (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]) + p[1:-1, 2:] - p[1:-1, :-2]) + P2 * (vn[2:, 1:-1] + vn[:-2, 1:-1] + vn[1:-1, 2:] + vn[1:-1, :-2]) # y momentum
    v[0, :] = 0 # v = 0 at x = 0
    v[-1, :] = 0 # v = 0 at x = L
    v[:, 0] = 0 # v = 0 at y = 0
    v[:, -1] = 0 # v = 0 at y = D

#%% post processing
X, Y = np.meshgrid(np.linspace(0, L, Nx), np.linspace(0, D, Ny)) # spatial grid
plt.figure(dpi = 200)
plt.contourf(X, Y, u.T, 128, cmap = 'jet') # plot contours
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 = 200)
plt.contourf(X, Y, v.T, 128, cmap = 'jet') # plot contours
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 = 200)
plt.contourf(X, Y, p.T, 128, cmap = 'jet') # plot contours
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 = 200)
plt.streamplot(X, Y, u.T, v.T, color = 'black', cmap = 'jet', density = 2, linewidth = 0.5, arrowstyle='->', arrowsize = 1) # plot streamlines
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()

velocity_magnitude = np.sqrt(u**2 + v**2)  # calculate velocity magnitude
plt.figure(dpi = 200)
plt.contourf(X, Y, velocity_magnitude.T, 128, cmap = 'plasma_r') # plot contours
plt.streamplot(X, Y, u.T, v.T, color = 'black', cmap = 'jet', density = 2, linewidth = 0.1, arrowstyle='->', arrowsize = 0.5) # plot streamlines
plt.gca().set_aspect('equal', adjustable='box')
plt.xticks([0, L])
plt.yticks([0, D])
plt.xlabel('x [m]')
plt.ylabel('y [m]')
plt.axis("off")
plt.show()

     Thank you for reading! If you want to hire me as your next shinning post-doc, do let reach out!

Wednesday, 18 August 2021

Computational Fluid Dynamics Simulation of a Swimming Fish (Includes UDF)

      This post is about the simulation of a swimming fish. The fish body is made of NACA 0020 and 0015 aero-foils (air-foils). The fluke is made of NACA 0025 aero-foil (air-foil), as shown in Fig. 1. the CAD files with computational domain modelled around the fish is available here.



Fig. 1, The generic fish CAD model

      The motion of the fish's body is achieved using a combination of two user-defined functions (UDF). The UDFs use DEFINE_GRID_MOTION script mentioned below, for the head/front portion. This is taken from the ANSYS Fluent software manual, available in its original form here. The original UDF is modified for present use as required. To move the mesh, dynamic mesh option within ANSYS Fluent is enabled; with smoothing and re-meshing options. The period of oscillation is kept at 2.0 s. The Reynolds number of flow is kept at 100,000; which is typical for a swimming fish.

/**********************************************************

 node motion based on simple beam deflection equation
 compiled UDF
 **********************************************************/
#include "udf.h"

DEFINE_GRID_MOTION(undulating_head,domain,dt,time,dtime)
{
  Thread *tf = DT_THREAD(dt);
  face_t f;
  Node *v;
  real NV_VEC(omega), NV_VEC(axis), NV_VEC(dx);
  real NV_VEC(origin), NV_VEC(rvec);
  real sign;
  int n;
  
  /* set deforming flag on adjacent cell zone */
  SET_DEFORMING_THREAD_FLAG(THREAD_T0(tf));

  sign = 0.15707963267948966192313216916398 * cos (3.1415926535897932384626433832795 * time);
  
  Message ("time = %f, omega = %f\n", time, sign);
  
  NV_S(omega, =, 0.0);
  NV_D(axis, =, 0.0, 1.0, 0.0);
  NV_D(origin, =, 0.7, 0.0, 0.0);
  
  begin_f_loop(f,tf)
    {
      f_node_loop(f,tf,n)
        {
          v = F_NODE(f,tf,n);

          /* update node if x position is greater than 0.02
             and that the current node has not been previously
             visited when looping through previous faces */
          if (NODE_X(v) > 0.05 && NODE_X(v) < 0.7 && NODE_POS_NEED_UPDATE (v))
            {
              /* indicate that node position has been update
                 so that it's not updated more than once */
              NODE_POS_UPDATED(v);

              omega[1] = sign * pow (NODE_X(v), 0.5);
              NV_VV(rvec, =, NODE_COORD(v), -, origin);
              NV_CROSS(dx, omega, rvec);
              NV_S(dx, *=, dtime);
              NV_V(NODE_COORD(v), +=, dx);
            }
        }
    }

  end_f_loop(f,tf);
}

      The computational mesh, as shown in Fig. 2, uses cut-cell method with inflation layers. The mesh has 2,633,133 cells. The near wall y+ is kept at 5. The Spalart-Allmaras turbulence model is used to model the turbulence. The second order upwind scheme is used to discretize the momentum and modified turbulent viscosity equations. The time-step for this study is kept at 100th/period of oscillation.


Fig. 2, The mesh and zoom in view of the trailing edge.

      The animation showing fish motion is shown in Fig. 3. Within Fig. 3, the left side showcases the velocity iso-surfaces coloured by pressure and the vorticity iso-surfaces coloured by velocity magnitude is shown on the right.


Fig. 3, The animation.

      Another animation showing the fish motion is shown in Fig.4. Within Fig. 4, the left side shows surface pressure while the right side shows pressure iso-surfaces coloured by vorticity.


Fig. 4, The animation.

      If you want to collaborate on the research projects related to turbo-machinery, aerodynamics, renewable energy, please reach out. Thank you very much for reading.

Sunday, 7 October 2018

High Camber Wing CFD Simulation

     This post is about the numerical simulation of a high camber, large aspect ratio wing. The wing had an aspect ratio of 5:1. The Reynolds number of flow was 500,000. The wing was at an angle of attack of zero degree. The aero-foil employed had a cross section of NACA 9410.

     The software employed was Flow Simulation Premium. A Cartesian mesh was created using the immersed boundary method. The mesh had 581,005 cells. Among those 581,005 cells, 55,882 were at the solid-fluid boundary. A time step of ~0.00528167 s was employed*. The domain was large enough to accurately trace the flow around the wing without any numerical or reversed flow errors. The software employs κ-ε turbulence model with damping functions, SIMPLE-R (modified) as the numerical algorithm and second order upwind and central approximations as the spatial discretization schemes for the convective fluxes and diffusive terms. The time derivatives are approximated with an implicit first-order Euler scheme.

     The mesh is shown in Fig. 1. The four layers of different mesh density are also visible in Fig. 1, the mesh is refined near the wing surface using a mesh control. The velocity around the wing section is shown in Fig. 2, using a cut plot at  the center of the wing. In Fig. 2, the wing body is super imposed by pressure plot. The velocity vectors showing the direction of flow are superimposed on both the wing body and the velocity cut plot.


Fig. 1, The computational domain.


Fig. 2, The velocity and pressure plots.

     The results of the simulation was validated against the results from XFLR5 software. XFLR5 predicted slightly higher lift and slightly less drag on the wing for same boundary conditions because the XFLR5 simulations were inviscid.

     Thank you for reading. If you would like to contribute to the research, both financially and scientifically, please feel free to reach out.

     *Time step is averaged because of the fact that a smaller time step was employed at the start of the numerical simulation.

Saturday, 28 July 2018

Steady-State VS Transient Propeller Numerical Simulation Comparison

     This post is about the comparison between steady-state and transient computational fluid dynamics analysis of two different propellers. The propellers under investigation are 11x7 and 11x4.7 propellers. The first number in the propeller nomenclature is the propeller diameter and the second number represents the propeller pitch, both parameters are in inch. The transient analysis was carried out using the sliding mesh technique while the steady-state results were obtained by the local rotating region-averaging method. For details about 11x7 propeller click here, for the details about 11x4.7 propeller, click here.
 
     As expected, the propeller efficiencies of transient and steady-state analysis are within 0.9% of each other, as shown in Fig. 1-2. Therefore, it is advised to simulate propellers and horizontal axis wind turbines using the steady-state technique as long as no time-dependent boundary conditions are employed.
 
Fig. 1, Propeller efficiency plot.
  
 Fig. 2, Propeller efficiency plot.
 
     It can be seen from Fig. 3-4 that time taken by the steady-state simulation to converge is on average 42.37% less that the transient analysis.  The steady-state analysis takes considerably less time to give a solution then a transient analysis.
 
Fig. 3, Solution time.
 
Fig. 4, Solution time.
 
Thank you for reading. If you would like to collaborate on research projects, please reach out.

Friday, 23 October 2015

Pipe Flow Simulation

Just ran another simulation related to HMT, this problem became steady state after about 36 seconds.

Water at 318 K starts flowing (0.00035 m^3/s) through a steel pipe initially at 298 K. The steel pipe had convection to air at 298 K at 3,000 W/m^2.K. A simple simulation yielded inner and outer wall temperatures of the pipe to be 309.07 K and 311.26 K respectively. Then I ran a transient simulation, to find out the time taken by the pipe’s walls to reach these temperatures (f...rom 298 K) as water flows through it. It came out to be around 36 seconds.

Then I ran a FEA. To calculate stresses induced in the pipe due to water pressure, thermal effects, gravity etc. The pipe’s diameter increased by 0.005866 mm and von-mises stress induced was 117,016,056 N/m^2 with a factor of safety of 5.302.

Then I ran fatigue study to see if the pipe will survive under these loads for 20 years or not. It will I think. The fatigue S-N curves were not available so I used the ones for carbon steel (slightly different from the ones I used for CFD analysis and FEA); so will it last for 20 years I am not sure yet (searching for curves).



 Temperatures at inner wall surface
 Temperature at outer wall surface
displacement and stress animation

Sunday, 5 July 2015

Canal Turbine Concept


It's a concept I am currently working on, so far I gave made a CAD model (renderings attached) of it in SolidWorks and analyzed it using its built in CFD module.

There are many advantages of canal turbines over wind turbines, prominent one's being:

 

Unidirectional flow


Water flows in one direction in a canal so we don't need pitch and yaw control surfaces. That simplifies the design process and reduces weight.

Constant flow rate


We (humans) control water flow rate through canals and it's almost same all year, so we don't have to worry about blade aero foil design to suit variable/abruptly variable flow rate, that makes design process further straight forward.

Large Electricity potential


Canals are 100s of km long, imagine the electricity potential in the canals. You can put these turbines in irrigation canals and it'll power nearby villages and all the irrigation equipment etc.

Higher Power/Discharge Ratio


Water is ~816 times dense (powerful) than air, so for the same discharge (flow) rate we get potentially 816 times more power. Which means more we can make designs that are lighter, smaller and easier to manage and maintain.

Easy maintenance


Fitted less than ~1 m deep inside the canal and can be retracted for maintenance at ground level, making maintenance very easy or better yet, we can maintain them while canals are being cleaned.


Plots for Comparison between Lift and Drag Produced by a Legacy Wing VS a Wing with Tubercles (Humpback Whale Fin's Inspired)

Comparison between Lift and Drag Produced by a Legacy Wing VS a Wing with Tubercles (Humpback Whale Fin's Inspired)

* Link for Plots (now showing here for some reason) http://3dimensionaldesigningandmanufacturing.blogspot.com/2015/07/plots-for-comparison-between-lift-and.html

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

Project: Design of a Wing/Blade with Tubercles for Airplanes and/or Turbines


Without Tubercles

Air Speed in Km/h

Lift in N

Drag in N

150
46.307
14.775
140
39.942
12.917
130
33.432
11.057
                         120
28.807
9.498
110
24.234
7.928
100
20.593
6.625
90
15.836
5.352
80
12.482
4.205
70
9.411
3.243
60
7.272
2.406
50
4.873
1.680
40
3.130
1.082
30
1.763
0.612
20
0.810
0.279
10
0.231
0.072

 

 

With Tubercles

Air Speed in Km/h

Lift in N

Drag in N

150
50.616
11.360
140
48.131
10.008
130
37.190
8.505
120
30.988
7.309
110
24.784
6.079
100
20.892
5.094
90
17.225
4.146
80
13.412
3.287
70
9.955
2.507
60
7.444
1.849
50
4.955
1.286
40
2.991
0.828
30
1.652
0.468
20
0.725
0.212
10
0.214
0.057

 

Comparison between Lift and Drag


Air Speed in Km/h
Percentage Less Drag
Percentage More Lift
150
23.113
 
8.513
140
22.520
 
17.014
130
23.080
 
10.105
120
22.974
7.038
110
23.322
2.219
100
23.109
1.431
90
22.534
8.064
80
21.831
6.934
70
22.695
5.465
60
23.150
2.311
50
23.452
1.655
40
23.475
-7.523
30
23.529
-6.719
20
24.014
-11.72
10
20.833
-7.94
 
 
 
 

 

It is clear that the wing with tubercles not only produces more lift at a particular velocity but also less drag.

Data for the Wing without Tubercles:


Wing Span: 1.07 m

Chord Length: 0.229 m

Air Velocity: 0-150 Km/h head on

Vertical Pitch: 0 Degree

Gravity Considered

Fluid: Dry Air at STP

Mesh Settings: Coarse (3/8)


Data for the Wing with Tubercles:


Wing Span: 1.067 m

Chord Length Large: 0.229 m

Chord Length Small: 0.203 m

Air Velocity: 0-150 Km/h head on

Vertical Pitch: 0 Degree

Gravity Considered

Fluid: Dry Air at STP

Mesh Settings: Coarse (3/8)


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


This Plot Shows Air Velocity VS Drag, Lift by the Wing without Tubercles


This Plot Shows Air Velocity VS Drag, Lift by the Wing with Tubercles

As you can see from above two plots; the wing with tubercles generates more lift and less drag.


This Plot Shows Air Velocity VS Lift Generated by the Wings

The green line represents the Lift generated by the wing with tubercles. It is between two to six percent more at each velocity.


This Plot Shows Air Velocity VS Drag Generated by the Wings

The green line represents the Drag generated by the wing with tubercles. It is around twenty two percent less at each velocity.


This Plot Shows Air velocity VS Lift to Drag Ratio

It is clear from this plot that Lift to Drag ratio of the wing with tubercles is around thirty three percent more for the wing without tubercles at a velocity point.

 


This Plot Shows Air Flow around the Wings at 150 Km/h from the Right Side


This Plot Shows Air Flow around the Wings at 150 Km/h

The Need for Tubercles


In aviation there are four forces at play, Lift which over comes Weight and Thrust which overcomes Drag. For a cruise speed at a particular altitude, three of these forces are almost constant. Our goal is to minimize Thrust, Drag and Weight and maximize Lift, this is because Thrust costs in terms of fuel flow rate and Weight and Drag negatively impacts on the agility of the aircraft. Aerodynamically efficient Wings and/or Blades with "Tubercles" will not only increase Lift and but also decrease Drag. This all means that we will need less Thrust for a cruise speed than before, that results in savings in terms of fuel which will result in healthier environment.

 

Applications:


 


Canal Turbine Concept


It's a concept I am currently working on, so far I gave made a CAD model (renderings attached) of it in SolidWorks and analyzed it using its built in CFD module.

There are many advantages of canal turbines over wind turbines, prominent one's being:

 

Unidirectional flow


Water flows in one direction in a canal so we don't need pitch and yaw control surfaces. That simplifies the design process and reduces weight.

Constant flow rate


We (humans) control water flow rate through canals and it's almost same all year, so we don't have to worry about blade aero foil design to suit variable/abruptly variable flow rate, that makes design process further straight forward.

Large Electricity potential


Canals are 100s of km long, imagine the electricity potential in the canals. You can put these turbines in irrigation canals and it'll power nearby villages and all the irrigation equipment etc.

Higher Power/Discharge Ratio


Water is ~816 times dense (powerful) than air, so for the same discharge (flow) rate we get potentially 816 times more power. Which means more we can make designs that are lighter, smaller and easier to manage and maintain.

Easy maintenance


Fitted less than ~1 m deep inside the canal and can be retracted for maintenance at ground level, making maintenance very easy or better yet, we can maintain them while canals are being cleaned.