5.1. Introduction

Compiled UDFs are built in the same way that the Ansys Fluent executable itself is built. Internally, a script called Makefile is used to invoke the system C compiler to build an object code library that contains the native machine language translation of your higher-level C source code. The object library is specific to the computer architecture being used during the Ansys Fluent session, as well as to the particular version of the Ansys Fluent executable being run. Therefore, UDF object libraries must be rebuilt any time Ansys Fluent is upgraded, when the computer’s operating system level changes, or when the job is run on a different type of computer architecture. The generic process for compiling a UDF involves two steps: compile/build and load.

The compile/build step takes one or more source files (for example, myudf.c) containing at least one UDF and compiles them into object files (for example, myudf.o or myudf.obj) and then builds a "shared library" (for example, libudf) with the object files. If you compile your source file using the GUI, this compile/build process is executed when you click Build in the Compiled UDFs dialog box. The shared library that you name (for example, libudf) is automatically built for the architecture and version of Ansys Fluent you are running during that session (for example, lnamd64/2d_host and lnamd64/2d_node), and will store the UDF object file(s).

If you compile your source file using the TUI, you must first set up target folders for the shared libraries, modify a file named Makefile to specify source parameters, and then execute the Makefile which directs the compile/build process. Compiling a UDF using the TUI has the added advantage of allowing precompiled object files derived from non-Ansys Fluent sources to be linked to Ansys Fluent (Link Precompiled Object Files From Non-Ansys Fluent Sources). This option is not available using the GUI.

After the shared library is built (using the TUI or GUI), you load the UDF library into Ansys Fluent before you can use it. You can do this using the Load button in the Compiled UDFs dialog box. After being loaded, all of the compiled UDFs that are contained within the shared library will become visible and selectable in graphics dialog boxes in Ansys Fluent. Note that compiled UDFs are displayed in Ansys Fluent dialog boxes with the associated UDF library name separated by two colons (::). For example, a compiled UDF named rrate that is associated with a shared library named libudf would appear in Ansys Fluent dialog boxes as rrate::libudf. This distinguishes UDFs that are compiled from those that are interpreted.

If you write your case file when a UDF library is loaded, the library will be saved with the case and will be automatically loaded whenever that case file is subsequently read. This process of "dynamic loading" saves you having to reload the compiled library every time you want to run a simulation.

Before you compile your UDF source file(s) using one of the two methods provided in Compiling a UDF Using the GUI and Compile a UDF Using the TUI, you must make sure that the udf.h header file is accessible in your path, or is saved locally within your working folder (Location of the udf.h File).

For more information, see the following sections:

5.1.1. Location of the udf.h File

UDFs are defined using DEFINE macros (see DEFINE Macros) and the definitions for DEFINE macros are included in udf.h. Consequently, before you compile your source file, the udf.h header file must be accessible in your path, or saved locally within your working folder.

The location of the udf.h file is:

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

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


Important:
  • You should not, under any circumstances, alter the udf.h file.

  • In general, you should not copy udf.h from the installation area. The compiler is designed to look for this file locally (in your current folder) first. If it is not found in your current folder, the compiler will look in the \src\udf folder automatically. In the event that you upgrade your release area, but do not delete an old copy of udf.h from your working folder, you will not be accessing the most recent version of this file.


There may be instances when you will want to include additional header files in the compilation process. Make sure that all header files needed for UDFs are located in a folder under the \src folder.

5.1.2. Compilers

The graphical and text interface processes for a compiled UDF require the use of a C compiler that is native to the operating system and machine you are running on. Most Linux operating systems provide a C compiler as a standard feature. For Linux machines, Ansys Fluent supports any ANSI-compliant compiler. If you are operating on a Windows system, it is recommended that you install a supported version of Microsoft Visual Studio or Clang on your machine before you proceed (for a list of supported versions, see Compiler Requirements for Windows Systems); note that if Fluent determines that you have not installed a compiler on your computer, a built-in compiler (Clang) that is provided as part of the Fluent installation is automatically used. If you are unsure about compiler requirements for your system, contact Ansys Fluent installation support.


Important:  Obsolete versions of any native compiler may not work properly with compiled UDFs.


When launching Ansys Fluent on Windows using Fluent Launcher, the Environment tab (Figure 5.1: The Environment Tab of the Fluent Launcher Dialog Box) allows you to specify compiler settings for compiling UDFs.

Figure 5.1: The Environment Tab of the Fluent Launcher Dialog Box

The Environment Tab of the Fluent Launcher Dialog Box

The Set up Compilation Environment for UDF option is enabled by default, and allows you to specify a batch file that contains UDF compilation environment settings. Enter a batch file name and path in the field, or click   to browse for a batch file. By default, the Fluent Launcher dialog box is set to use the udf.bat file that is saved in your computer as part of the Ansys Fluent installation. It is recommended that you keep the default batch file, which is tested with versions of MS Visual Studio C++ and Clang compilers, as well as the built-in compiler.