1.3. Defining Your UDF Using DEFINE Macros

UDFs are defined using Ansys Fluent-supplied function declarations. These function declarations are implemented in the code as macros, and are referred to in this document as DEFINE (all capitals) macros. Definitions for DEFINE macros are contained in the udf.h header file (see Appendix B: DEFINE Macro Definitions for a listing). For a complete description of each DEFINE macro and an example of its usage, refer to DEFINE Macros.

The general format of a DEFINE macro is:

DEFINE_MACRONAME(udf_name, passed-in variables) 

where the first argument in the parentheses is the name of the UDF that you supply. Name arguments are case-sensitive and must be specified in lowercase. After the function has been interpreted or compiled, the name that you choose for your UDF will become visible and selectable in drop-down lists in Ansys Fluent.


Important:  The name for your UDF must be unique to avoid conflicts with Fluent internal functions of the same name. Otherwise, the Linker may choose the wrong function, resulting in a segmentation violation. A convenient example to ensure a unique name would be to append my_ to each function name, such as my_adjust or my_is_point_in_cell.


The second set of input arguments to the DEFINE macro are variables that are passed into your function from the Ansys Fluent solver.

For example, the macro:

DEFINE_PROFILE(inlet_x_velocity, thread, index) 

defines a boundary profile function named inlet_x_velocity with two variables, thread and index, that are passed into the function from Ansys Fluent. These passed-in variables are the boundary condition zone ID (as a pointer to the thread) and the index identifying the variable that is to be stored. After the UDF has been interpreted or compiled, its name (inlet_x_velocity) will become visible and selectable in drop-down lists in the appropriate boundary condition dialog box (for example, Velocity Inlet) in Ansys Fluent.


Important:  When using UDFs:

  • All of the arguments to a DEFINE macro need to be placed on the same line in your source code. Splitting the DEFINE statement onto several lines will result in a compilation error.

  • There must be no spaces between the macro (for example, DEFINE_PROFILE) and the first parenthesis of the arguments, as this will cause an error in Windows.

  • Do not include a DEFINE macro statement (such as DEFINE_PROFILE) within a comment in your source code. This will cause a compilation error.


1.3.1. Including the udf.h Header File in Your Source File

The udf.h header file contains:

  • Definitions for DEFINE macros

  • #include compiler directives for C or C++ library function header files

  • Header files (for example, mem.h) for other Ansys Fluent-supplied macros and functions.

You must, therefore, include the udf.h file at the beginning of every UDF source code file using the #include compiler directive:

#include "udf.h" 

For example, when udf.h is included in the source file containing the DEFINE statement from the previous section,

#include "udf.h"
 
 DEFINE_PROFILE(inlet_x_velocity, thread, index) 

upon compilation, the macro will expand to

void inlet_x_velocity(Thread *thread, int index) 

You can use /**/ to comment out snippets of a UDF that you do not want read. Commenting out commands must be done in the appropriate location:

/*DEFINE_ON_DEMAND(unique-name)
{
    printf("testing\n");
}*/

Note:  Due to the macro c-preprocessor expansion you cannot comment out portions of function arguments. Thus, the following is not allowed:

DEFINE_PROFILE(inlet-x_velocity, /*name must be unique*/thread, /*face thread*/index)
/*which variable of the boundary condition*/


Important:  You do not need to put a copy of udf.h in your local folder when you compile your UDF. The Ansys Fluent solver automatically reads the udf.h file from the following folder after your UDF is compiled, for example:

path\ANSYS Inc\v242\fluent\fluent24.2.0\src\udf

where path is the folder in which you have installed Ansys Fluent (by default, the path is C:\Program Files).