Performing A 2D Gear Sloshing Simulation Inside A Closed Container By Using An User Defined Function (UDF) For Varying Lubricants Using Ansys Fluent.
Abstract:- The work is focused on performing a 2D Gear Sloshing simulation using a pressure-based transient solver for two different gearboxes having a clearance of 20% & 30% both filled with two phases of fluid. We will be running 4 different simulations each having a different percentage of immersions & materials (phases). Through this project, we will be able to understand & implement Dynamic Meshing for moving bodies such as gears. Further, we will be making use of a User Defined Function (UDF) which would allow us to define the motion of the gears in a synchronized fashion. The 4 different simulation cases that we are interested in are:-
Case 1: Phase 1 will be occupied with 80% air (lighter fluid) & Phase 2 will comprise 20% of Engine-oil (lubricate).
Case 2: Phase 1 will be occupied with 70% air (lighter fluid) & Phase 2 will comprise 30% of Engine-oil (lubricate).
Case 3: Phase 1 will be occupied with 80% air (lighter fluid) & Phase 2 will comprise 20% of Fluid n-heptane (c7h16).
Case 4: Phase 1 will be occupied with 70% air (lighter fluid) & Phase 2 will comprise 30% of Fluid n-heptane (c7h16).
Theory_________________________________________________________________
Fluid sloshing is the motion of a free-surface liquid in a partially filled vessel, experienced in the marine, automotive, rail, and aerospace industries. During sloshing, the liquid exerts a dynamic force on the surrounding vessel, which may cause leakage or damage to the vessel or its supporting structure. The sloshing motion is a highly nonlinear and random process depending on vessel motion, liquid depth, liquid properties, and vessel geometry. Multiphase CFD (computational fluid dynamics) methods (such as the volume of fluid or phase field) are used to predict liquid sloshing and its effect on the deformation and stresses in the vessel. Dynamic analysis of a tank containing liquid is a complex problem involving fluid-structure interaction. The tank-liquid system is simplified by an equivalent model in which the total liquid mass is divided into two zones – impulsive and convective. The behavior of the free liquid surface (the convective portion of liquid) subjected to the dynamic loading which may result in liquid spilling or tank wall damage. Therefore, sufficient freeboard must be required to design.
- Fuel in a tank (this could be in a car, rockets, or any oil tanker).
- Hull of a ship transporting oil from a rig.
- Coffee in a mug & water bottles.
In most CFD projects, we design geometry, create a static mesh for this geometry, and simulate this by fix mesh. But sometimes we want to move some boundary or we have deforming shape during times, such as in our project where we will have 2 gears moving at a constant velocity which would lead to mesh deformation i.e. the mesh needs to copy the movement pattern of the gears. Here we must use the dynamic mesh method. For example, in most aerodynamic problems we use fix mesh for our simulation but you can imagine there are two airplanes that are getting closer and you want to know the effect of fluid flow behavior around airplanes and determine the interaction between them during the time. So the relative position them are changing during time and should be modeled using dynamic mesh method. By using this method, mesh size and shape will be changed. If we use the re-meshing or layering method, the number of elements also will be changed. Dynamic mesh method is entirely different from moving and sliding mesh also moving reference frame. In moving mesh whole zone rotates or translates in some direction but in the dynamic mesh method, the boundary starts to translate or transform. We can apply a predefined velocity by UDF or profile for boundaries or velocity of an object can be predicted based on fluid flow and gravity force balance. If we want to know the velocity of an object based on this balance we should use the SIX DOF dynamic mesh method. Mesh quality during change should be conserved. ANSYS fluent has three different methods for changing mesh, smoothing, re-meshing, and layering. The dynamic mesh operation can be used in various CFD project:-
- Solid fuel combustion and modeling solid height level during burning fuel
- Fluid solid interaction of blood flow and vessel
- FSI simulation blood flow pumping ina human heart
- Fluid solid interaction of airflow around building and finding final deflection and fluctuation (two way and one-way FSI)
- Two and four-stroke internal combustion engine engine
Generally, the dynamic mesh technique is used when simulating models that require a moving mesh area. In fact, this model enables the spatial variation of the defined areas for fluid flow and allows the mesh of the region at the same time as the spatial location of the points and boundaries of the region changes compatibility with the place at the present moment. The dynamic mesh technique composed of three steps, including determining dynamic mesh methods, specifying specific modes with dynamic mesh options, and defining the dynamic mesh zone.

A UDF is basically a C program or a C function that can be dynamically loaded with ANSYS FLUENT to enhance its standard features. The features of a typical UDF are as follows :
- Must be defined using DEFINE macros supplied by FLUENT
- Must have an include statement for the udf.h file
- Use predefined macros and functions to access FLUENT solver data and to perform other tasks
- Are executed as interpreted or compiled functions
- Are hooked to ANSYS FLUENT solver using a graphical user interface dialog boxes
UDF can be used for a number of applications like :
- Customize boundary conditions
- Material property definitions
- Adjust computed values on a once-per-iteration basis
- Initialize with a previous solution
- Execute at the end of an iteration
- Upon exit from ANSYS FLUENT
- Enhance post-processing
- Enhance existing ANSYS FLUENT models (such as the discrete phase model, multiphase mixture model, discrete ordinates radiation model)
UDFs are implemented in FLUENT code as macros. The general format for defining macros is the macro name that is defined followed by an underscore with the udf name in parenthesis followed by a coma and then mentioning the variables that are to be passed, as shown in examples below.

The definitions for DEFINE macros are contained in the udf.h header file, hence it is not possible to have something else defined in the UDF that is outside the udf.h header file.

The udf.h header file contains the following :
- Definitions for DEFINE macros
- #include compiler directives for C library function header files
- Header files (for example, stdio.h) for other ANSYS FLUENT-supplied macros and functions
A UDF should begin with #include followed with "udf.h" followed by whichever macro to be used can be defined, shown below.

Compiling & Interpreting UDFs :
Now let us understand how a UDF is compiled or interpreted i.e. how a UDF is exactly included in the FLUENT software. The file containing the UDF can be either interpreted or compiled in FLUENT. The way in which the source code is compiled and the code that results from the compilation process is different for both cases. In compiled UDFs, the UDFs are built in the same way as that the ANSYS FLUENT software itself is built, whereas in the interpreted UDFs the source code is compiled into a machine code that executes on an internal emulator whenever the UDF is invoked.
Let us now understand how UDFs are compiled. Basically, they are compiled either by graphical user interphase (GUI) or the text user interphase (TUI). In the case of GUI, it is a two-step process i.e. a window called compiled UDF flashes on the screen whenever we try to compile. The input it requires are :
- Build a shared-library object file from a source file
- Load the shared library into FLUENT.
So to summarize we create a library file from our UDF C program, later we load the library file into FLUENT as if it is a part of FLUENT.
Now let us see how interpreted UDF differs from compiled ones. This is a single-step process, occurring at runtime only, and involves the use of an interpreted UDFs dialog box.
Basically what happens when we interpret a UDF, is that the source code is compiled into an intermediate architecture machine code using a preprocessor. Now this machine code is executed on an internal emulator, or interpreter when the UDF is invoked. As an additional function is to be called during execution, it, therefore, incurs a reduction in performance.
Types of UDF :
As per the requirement, the basic/ general practice goes as :
- For small, straightforward functions we use interpreted UDFs
- For complex functions that have high significant CPU requirements and require access to a shared library, we use complied UDFs. Because compiled UDF is like a part of ANSYS FLUENT software itself.
Hooking UDFs into a problem setup :
We can either interpret or compile the UDF source file. Now the function contained in the interpreted code or shared library will appear in the dropdown list in dialogue boxes. We may choose this function to activate or "hook" our UDF in the CFD model.
Geometry Manipulation____________________________________________________
We start by converting the unit system to "m" & then import the pre-modeled design of the Gear Box geometry to Spaceclaim, where we will be editing our geometry according to our simulation requirements. Our geometry is made up of two volumes, the solid volume & the fluid volume. We start by extracting the fluid region viz the space that's occupied by the gear & the fluid medium. The next step requires us to extract the 2D geometry model from the 3d geometry. We do this to reduce the need for excessive computational power. The process to extract the 2D geometry requires us to create a plane along the axis such that it cuts the geometry into two halves & use the 'split-body' tool to cut the geometry. We copy the surface information from the split body & paste it into a new file. This creates our desired 2D geometry model. Pictorial representation of the steps involved in the workflow has been shown below.

Fig 1.1: Original geometry of the Gear Box with the Gears housed inside the solid geometry box

Fig 1.2: Locating the "Center of Volume" for both the gears



Fig 1.3: Workflow to extract the required 2D geometry from the original geometry
Mesh Generation________________________________________________________
The process of discretizing our domain of interest i.e. the 2D geometry into smaller pieces is called meshing. Ansys Fluent uses a technique called the "bounding box" method to generate a body-fitted mesh. This is done by creating the smallest size hypothetical box possible that can be used to fit the entire geometry into it. We will be running a total of 4 case setups & each will have similar mesh properties. In the meshing module interface, we start by assigning names to our geometry. For our case, we have the left gear & the right gear. Next, we assign the user-defined parameters like element order, element size, method, proximity min size & no. of cells across the gap.
Fig 2.1: Mesh details & name selection
Setting Up the Flow Physics_______________________________________________
`->` Solver Type = Pressure Based, Time State = Transient
`->`Gravity = -9.81`(m/s^2)` along y-axis
`->`Viscous/Turbulence Model = Viscous K-epsilon (Realizable) with enhanced Wall function
`->`Model = Multiphase model with Volume of Fluid (VoF)
`->`Formulation Method = Implicit
`->`Phases = Air & Engine oil or Air & n-heptane
`->`Dynamic Mesh = Left_gear - left motion (0,0) & Right_gear - right motion (0.115,0)
`->`Immersion Region =
`->`Time Step Size = 1e-5, No.of Time Step = 2500
#User-Defined Function (UDF) For Gear Motion
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include "udf.h"
DEFINE_CG_MOTION(right_motion, dt, vel, omega, time, dtime)
{
vel[0] = 0.0; vel[1] = 0.0; vel[2] = 0.0;
omega[0] = 0.0; omega[1] = 0.0; omega[2] = 2.0e2; /* [rad/s]*/
}
DEFINE_CG_MOTION(left_motion, dt, vel, omega, time, dtime)
{
vel[0] = 0.0; vel[1] = 0.0; vel[2] = 0.0;
omega[0] = 0.0; omega[1] = 0.0; omega[2] = -2.0e2; /* [rad/s]*/
}
The User Defined Function (UDF) can be added to ANSYS fluent as a function to assign motion, define terms, customize boundary conditions or material properties. In general, it is used IN ANSYS to improve the already existing standard features. The above UDF function defines the angular velocity (omega,`omega`) along the z-axis for the 2 gears as 200rad/sec.

Fig 3.1: Fluid elements (materials) present in each phase for Case 1 & Case 2

Fig 3.2: Fluid elements (materials) present in each phase for Case 3 & Case 4

Fig 3.3: Domain with 20`%` Lubricant for Case 1 & 3 Fig 3.4: Domain with 30`%` Lubricant for Case 2 & 4
Contours & Plots___________________________________________________

Fig 3.5: Initial Domain with just one phase (air) Fig 3.6: Initial Domain with both phases (air & lubricant)
Case 1: Phase 1 will be occupied with 80% air (lighter fluid) & Phase 2 will comprise 20% of Engine-oil (lubricate).




Case 2: Phase 1 will be occupied with 70% air (lighter fluid) & Phase 2 will comprise 30% of Engine-oil (lubricate).



Case 3: Phase 1 will be occupied with 80% air (lighter fluid) & Phase 2 will comprise 20% of Fluid n-heptane (lubricate).



Case 4: Phase 1 will be occupied with 70% air (lighter fluid) & Phase 2 will comprise 30% of Fluid n-heptane (lubricate).



Improvements_________________________________________________________
The simulation results obtained are highly inaccurate & cannot be used in any engineering project or industry. This is due to the fact that the gear was able to spin only 1 revolution due to limited computing power. In industry scaled projects the minimum number of revolutions that gear should obtain is roughly around 5-7, to get to reasonable solution results.
Error's Encountered_____________________________________________________
'Negative cell volume detected' error = This message means that the element has a big distortion. Big enough to do some vertex, or edges, to penetrate an opposite face resulting in a negative volume, once it is calculated based on the metrics and the local coordinate system of the element that represents the volume of control. Quality, and determinants 2x2 and 3x3 will also be given a negative value due to this distortion. A negative volume will generate the unphysical behavior of equations. The time step probably is large when compared with the displacement of your mesh. To resolve this problem we need to check the mesh quality & maintain the skewness (should be under 8) & aspect ratio under the appropriate range. A bad mesh can lead to floating-point errors, need to use conformal mesh.
