Sunday, 24 April 2022

1D Laplace's equation using Finite Difference Method

This post is about FDM for Laplace Equation with various boundary conditions.

MATLAB Code (1D, Dirichlet Boundary Conditions)

%% initialize the workspace, clear the command window


clear; clc


%% finite difference 1D laplace dirichlet boundary conditions %% d2u/dx2 = 0 %% u(o) = 10, u(L) = 4 %% Ax=b%%


N = 4 ; %number of grid points

L = 1; %length of domain

dx = L/(N-1); %element size


%% initialize variables %%


l = linspace(0,L,N); %independent

u=zeros(1,N); %dependent


%% boundary conditions %%


u(1)=10;

u(end)=4;


%% b vector %%


b=zeros(N-2,1);

b(1) = b(1) - u(1);

b(end) = b(end) - u(end);


%% A matrix


A = -2*eye(N-2,N-2);

for i=1:N-2

    if i<N-2

        A(i,i+1)=1;

    end

    if i>1

        A(i,i-1)=1;

    end

end


%% solve for unknowns %%


x = A\b;


%% fill the u vector with unknowns %%


u(2:end-1) = x;


%% plot the results %%


hold on; grid on; box on, grid minor

set(gca,'FontSize',40)

set(gca, 'FontName', 'Times New Roman')

ylabel('u','FontSize',44)

xlabel('l','FontSize',44)


plot(l,u,'-o','color',[0 0 0],'LineWidth',2,'MarkerSize',20)


MATLAB Code (1D, Mixed Boundary Conditions)

%% initialize the workspace, clear the command window

clear; clc

%% finite difference 1D laplace mixed boundary conditions %% d2u/dx2 = 0 %% u(o) = 10, du/dx(L) = 4 %% Ax=b%%

N = 5; %number of grid points
L = 1; %length of domain
dx = L/(N-1); %element size
a = 4;
%% initialize variables %%

l = linspace(0,L,N); %independent
u=zeros(1,N); %dependent

%% dirichlet boundary condition %%

u(1) = 10;

%% b vector %%

b=zeros(N-1,1);
b(1) = b(1) - u(1);
b(end) = b(end) + dx*a; %neumann boundary condition added to b vector

%% A matrix

A = -2*eye(N-1,N-1);
for i=1:N-1
    if i<N-1
        A(i,i+1)=1;
    end
    if i>1
        A(i,i-1)=1;
    end
end
A(N-1,N-1) = -1; %neumann boundary condition added to A matrix
%% solve for unknowns %%

x = A\b;

% fill the u vector with unknowns %%

u(2:end) = x;

%% plot the results %%

hold on; grid on; box on, grid minor
set(gca,'FontSize',40)
set(gca, 'FontName', 'Times New Roman')
ylabel('u','FontSize',44)
xlabel('l','FontSize',44)

plot(l,u','-o','color',[0 0 0],'LineWidth',2,'MarkerSize',20)

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.

Monday, 14 December 2020

Flapping Aerofoil For Propulsion

     This post is about a 2D NACA 0012 aerofoil undergoing forced flapping motion for propulsion purposes. Heaving motion is achieved by applying a vertical velocity on the aerofoil based on the Eqn. 1. Similarly the pitching motion is achieved by applying a rotational velocity, governed by Eqn. 2.


vy = 2*π*fh*Ho*sin(2*π*fh*t)                                              Eqn. 1

ω = -2*π*fh*ϑ*sin[(2*π*fh*t) + 1.5708]                               Eqn. 2

     w.r.t. Eqn. 1-2 reduced frequency is defined as (2*π*fh*Ho/U∞)), fh is the frequency of oscillations, while ωt and ϑo represent rotational velocity, instantaneous time and maximum pitching angle. Ho is the heaving amplitude and U∞ is the free stream velocity.

     The flapping motion is achieved by a combination of the heaving and pitching. In this particular simulation, the aerofoil is in the propulsion mode, meaning the feathering parameter χ is less in magnitude than 1.0. Feathering parameter is defined by Eqn. 3.

χ = ϑ/arctan(h0*2*π*fh/U∞)                                  Eqn. 3

     The boundary conditions employed for the simulation are at Re 1,000, K = 1.41, Ho = aerofoil chord lengthχ = 0.5489 and fh = 0.003391 Hz. The animation of the pressure, vorticity and velocity contours is shown in Fig. 1.


Fig. 1, Flow animation, fluid flow direction is from left to right.

     The results of present simulation are compared with [1]. In terms of maximum lift, a maximum deviation of 5% is observed as compared to [1], as shown in Fig. 2. The maximum lift coefficient for available data is ~4.224 while the maximum lift coefficient from the present simulation is ~4.057. The average thrust produced is within 2% of [1]. Average thrust coefficient per cycle from [1] is 0.9957 while the result from present simulation reveals the thrust coefficient to be 1.0098.


Fig. 2, A comparison of coefficient of lift.

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.

References

[1] https://doi.org/10.1017/jfm.2017.508

Thursday, 20 August 2020

The Fan Car

     The idea to reduce Drag and/or improve Downforce on a vehicle using fans at the rear has been around for decades. Specially in the world of motorsports. Examples include Gordon Murray's BT46 and the T50. Here an explanation is made as to why placing a fan behind a car or a container-carrier truck can be used to improve fuel economy.

     The sample car model is of the renowned Ahmed Body. For validation of the numerical simulation, please refer to this post.

     Fig. 1 shows pressure isosurfaces around the car body both with and without fans installed at the rear. It is clear that the pressure difference between rear and front of the car is more when the fans are not available. More pressure difference results in more Drag and a relatively bad fuel economy.


Fig. 1, T-B; Fan disabled, fan enabled


     Fig. 2 shows cross section view of the car. It can be seen that the the boundary layer is re-energized and as a result the flow separation is significantly reduced by adding a fan at the rear. By adding a fan, the vortices are not only moved away from the rear-end of the car but also have smaller size and less intensity, as shown in Fig. 3.


Fig. 2, T-B; Fan disabled, fan enabled. Red arrows represent direction of airflow


Fig. 3, L-R; Fan disabled, fan enabled

Thank you for reading. Please share my work. If you would like to collaborate on a project please reach out.