1.1. What is a User-Defined Function?

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. See Appendix A: C Programming Basics for some basic information on C programming.

UDFs are defined using DEFINE macros provided by Ansys Fluent (see DEFINE Macros). They are coded using additional macros and functions (also supplied by Ansys Fluent) that access Ansys Fluent solver data and perform other tasks. See Additional Macros for Writing UDFs for details.

Every UDF must contain the udf.h file inclusion directive (#include "udf.h") at the beginning of the source code file, which enables both the definition of DEFINE macros and other Ansys Fluent-provided macros and functions, and their inclusion in the compilation process. See Including the udf.h Header File in Your Source File for details.

Source files containing UDFs can be either interpreted or compiled in Ansys Fluent.

  • For interpreted UDFs, source files are interpreted and loaded directly at run time in a single-step process.

  • For compiled UDFs, the process involves two separate steps. A shared object code library is first built, then loaded, into Ansys Fluent. See Interpreting UDFs and Compiling UDFs.

After being interpreted or compiled, UDFs will become visible and selectable in Ansys Fluent dialog boxes, and can be hooked to a solver by choosing the function name in the appropriate dialog box. This process is described in Hooking UDFs to Ansys Fluent.

In summary, UDFs:

  • Are written in the C (version C99 or earlier) or C++ programming languages.

  • Must be defined using DEFINE macros supplied by Ansys Fluent.

  • Must have an include statement for the udf.h file.

  • Use predefined macros and functions to access Ansys Fluent solver data and to perform other tasks.

  • Are executed as interpreted or compiled functions.

  • Are hooked to an Ansys Fluent solver using a graphical user interface dialog boxes.


Important:
  • Fluent sets compiler flags automatically based on the file extension. *.c files must be written in C99 or earlier, *.cpp is reserved for C++ code, and header files always use *.h regardless of the language used.

  • Ensure the C++ standard you are following is supported by your compiler. The Clang compiler (version 10.0.0) included in the Fluent installation supports C++17.

  • You are encouraged to parallelize all UDFs that you create or modify, as described in Parallelizing Your Serial UDF.