UDF

A user-defined function, or UDF, is a C or C++ function that can be dynamically loaded with the ANSYS Fluent solver to enhance its standard features. For example, you can use a UDF to:

  • Customize boundary conditions, material property definitions, surface and volume reaction rates, source terms in ANSYS Fluent transport equations, source terms in user-defined scalar (UDS) transport equations, diffusivity functions, and so on.
  • Adjust computed values on a once-per-iteration basis.
  • Initialize of a solution.
  • Perform asynchronous (on demand) execution of a UDF.
  • Execute at the end of an iteration, upon exit from ANSYS Fluent, or upon loading of a compiled UDF library.
  • Enhance postprocessing.
  • Enhance existing ANSYS Fluent models (such as discrete phase model, multiphase mixture model, discrete ordinates radiation model).

UDFs are identified by a .c or .cpp extension (for example, myudf.c). One source file can contain a single UDF or multiple UDFs, and you can define multiple source files. 

In this post, we are going to share with you some practical and useful User-defined functions (UDFs). Follow us to be updated for future UDFs. We invited you to share your experiences and knowledge of UDF codes in this post. We will publish your UDFs with your own name and homepage.


  • Simulating the Non-Newtonian Fluid Using Casson Fluid Model

A user defined function (UDF) can be used to implement the Casson fluid model. The Casson fluid model is given below:

Casson model

A UDF incorporating the equations above is included in the file that you can see here:

/*********************************************************************
UDF for applying the Casson Non-newtonian fluid model
By Amir Sabernaeemi, fluidcodes.ir
02-11-2022
**********************************************************************/
#include “udf.h”
#define Hct 0.4 //Hematocrit Hct = 40% (female normal), 33% (post-angioplasty)or 45% (male normal)
#define mu_p 0.00145
DEFINE_PROPERTY(casson_viscosity,c,t)
{
real mu_casson;
real gamma = C_STRAIN_RATE_MAG(c,t);
real N_inf = sqrt(mu_p*pow((1-Hct),-0.25));
real mu_inf = sqrt(pow((0.625*Hct),3));
if(gamma!=0){
mu_casson = pow(mu_inf,2)/gamma+2*mu_inf*N_inf/sqrt(gamma)+pow(N_inf,2);}
else{
mu_casson = 0.0035;
}
return mu_casson;
}


  • A realistic time-varying boundary condition in order to mimic the pulsatile nature of blood flow

As we know, mammalian blood flow is pulsatile and cyclic in nature. Thus the velocity at the inlet is not set to be a constant, but instead, in this case, it is a time-varying periodic profile. The pulsatile profile within each period is considered to be a combination of two phases. During the systolic phase, the velocity at the inlet varies in a sinusoidal pattern.

The sine wave during the systolic phase has a peak velocity of 0.5 m/s and a minimum velocity of 0.1 m/s. Assuming a heartbeat rate of 120 per minute, the duration of each period is 0.5 s. This model for pulsatile blood flow is proposed by Sinnott et, al. A figure of the profile within two periods is given below:

Velocity inlet profile blood

To describe the profile more clearly, a mathematical description is also given below:

A UDF incorporating the equations above is included in the file that you can see here:

/***********************************************************************/
/* vinlet_udf.c
*/
/* UDFs for specifying time dependant velocity profile boundary condition
*/
/***********************************************************************/
//Written by Chiyu Jiang
//Cornell University

//www.fluidcodes.ir

#include “udf.h” //file that contains definitions for define functions and fluent operations
#define PI 3.141592654

DEFINE_PROFILE(inlet_velocity,th,i)
{
face_t f;
begin_f_loop(f,th)
double t = (CURRENT_TIME*2-floor(CURRENT_TIME*2))/2; //t is the local time within each period

{
if(t <= 0.218)
F_PROFILE(f,th,i) = 0.5*sin(4*PI*(t+0.0160236));
else
F_PROFILE(f,th,i) = 0.1;
}
end_f_loop(f,th);
}


  • Oscillatory Shear Index Calculation

The oscillatory shear index (OSI) was introduced to characterize the degree of shear reversal in a pulsatile flow–ranging from 0.0 in a uni-directional flow to 0.5 in a reversing flow with no mean shear direction.

Just remember to use this UDF, you need to active at least 5 user-define memories (UDS) and active your function hooks.

/******************************************************
Oscillatory shear index (OSI)
******************************************************/

#include “udf.h”
#include “math.h”
#include “storage.h”
#include “sg_udms.h”

#define domain_ID 1
#define zone_ID 7

/* Define Names for the WSS UDMI and its components */
DEFINE_EXECUTE_ON_LOADING(nameinit, libudf)
{
Set_User_Memory_Name(0, “uWSS”);
Set_User_Memory_Name(1, “uWSSx”);
Set_User_Memory_Name(2, “uWSSy”);
Set_User_Memory_Name(3, “uWSSz”);
Set_User_Memory_Name(4, “OSI”);
}

/* Initialize the UDM value to zero in complete domain */
DEFINE_INIT(meminit,domain)
{
Thread *c_thread;
cell_t c;

thread_loop_c(c_thread,domain)
{
begin_c_loop(c, c_thread)
{
C_UDMI(c,c_thread,0)= 0;
C_UDMI(c,c_thread,1)= 0;
C_UDMI(c,c_thread,2)= 0;
C_UDMI(c,c_thread,3)= 0;
C_UDMI(c,c_thread,4)= 0;
}
end_c_loop(c, c_thread)
}
}

/* Calculate wall shear stress and store them in UDM */
DEFINE_EXECUTE_AT_END(OSI)
{
Domain *domain;
real area;
face_t f;
real A[ND_ND];
cell_t c, c0;
Thread *t,*t0, *c_thread;
real wallshear [ND_ND];

domain = Get_Domain(domain_ID);
t = Lookup_Thread(domain,zone_ID);

begin_f_loop(f, t)
{
F_AREA(A,f,t);
area = NV_MAG(A);
NV_V(wallshear,=,F_STORAGE_R_N3V(f,t, SV_WALL_SHEAR));
c0 = F_C0(f,t);
t0 = THREAD_T0(t);
C_UDMI(c0,t0,0) += NV_MAG(wallshear)/area;
C_UDMI(c0,t0,1) += -wallshear[0]/area;
C_UDMI(c0,t0,2) += -wallshear[1]/area;
C_UDMI(c0,t0,3) += -wallshear[2]/area;
C_UDMI(c0,t0,4) = (1.-sqrt(C_UDMI(c0,t0,1)*C_UDMI(c0,t0,1) + C_UDMI(c0,t0,2)*C_UDMI(c0,t0,2) + C_UDMI(c0,t0,3)*C_UDMI(c0,t0,3))/C_UDMI(c0,t0,0))/2;
}
end_f_loop(f,t)
}


Related topics:

  1. 3D Bifurcating Artery



0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *