1.4. Interpreting and Compiling UDFs

Source code files containing UDFs can be either interpreted or compiled in Ansys Fluent. In both cases the functions are compiled, but the way in which the source code is compiled and the code that results from the compilation process is different for the two methods. These differences are explained in the following sections:

1.4.1. Compiled UDFs

Compiled UDFs are built in the same way that the Ansys Fluent executable itself is built: a Makefile script is used to invoke the system C or C++ compiler to build an object code library. You initiate this action in the Compiled UDFs dialog box by clicking Build. The object code library contains the native machine language translation of your higher-level C or C++ source code. The shared library must then be loaded into Ansys Fluent at run time by a process called "dynamic loading". You initiate this action in the Compiled UDFs dialog box by clicking Load. The object libraries are specific to the computer architecture being used, as well as to the particular version of the Ansys Fluent executable being run. The libraries must, therefore, 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.

In summary, compiled UDFs are compiled from source files using the graphical user interface, in a two-step process. The process involves the Compiled UDFs dialog box, where you first build a shared library object file from a source file, and then load the shared library that was just built into Ansys Fluent.


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.


1.4.2. Interpreted UDFs

Interpreted UDFs are interpreted from source files using the graphical user interface, but in a single-step process. The process, which occurs at runtime, involves using the Interpreted UDFs dialog box, where you Interpret a source file.

Inside Ansys Fluent, the source code is compiled into an intermediate, architecture-independent machine code using a C preprocessor. This machine code then executes on an internal emulator, or interpreter, when the UDF is invoked. This extra layer of code incurs a performance penalty, but enables an interpreted UDF to be shared effortlessly between different architectures, operating systems, and Ansys Fluent versions. If execution speed does become an issue, an interpreted UDF can always be run in compiled mode without modification.

The interpreter that is used for interpreted UDFs does not have all of the capabilities of a standard C compiler (which is used for compiled UDFs) and does not support all parallel UDF macros. For a detailed summary of limitations, see Limitations.


Note:  In some situations you may encounter an error with interpreted UDFs related to the C preprocessor (CPP) distributed with Fluent. As a workaround, you can enter the full path to the system's preprocessor in the CPP Command Name field in the Interpreted UDFs dialog box; for example, /usr/bin/cpp or gcc -E (Linux only).


1.4.3. Differences Between Interpreted and Compiled UDFs

The major difference between interpreted and compiled UDFs is that interpreted UDFs cannot access Ansys Fluent solver data using direct structure references; they can only indirectly access data through the use of Ansys Fluent-supplied macros. This can be significant if, for example, you want to introduce new data structures in your UDF.

Here is a summary of the differences between interpreted and compiled UDFs:

  • Interpreted UDFs:

    • Are portable to other platforms

    • Can all be run as compiled UDFs

    • Do not require a C compiler

    • Are slower than compiled UDFs

    • Are restricted in the use of the supported C programming language.

    • Cannot be written in C++

    • Cannot be linked to compiled system or user libraries

    • Can access data stored in an Ansys Fluent structure only using a predefined macro (see Additional Macros for Writing UDFs).

    See Interpreting UDFs for details on interpreting UDFs in Ansys Fluent.

  • Compiled UDFs:

    • Execute faster than interpreted UDFs

    • Are not restricted in their use of the supported C or C++ programming languages

    • Can call functions written in other languages (specifics are system- and compiler-dependent)

    • Cannot necessarily be run as interpreted UDFs if they contain certain elements of the C language that the interpreter cannot handle

    See Compiling UDFs for details on compiling UDFs in Ansys Fluent.

Thus, when deciding which type of UDF to use for your Ansys Fluent model:

  • Use interpreted UDFs for small, straightforward functions

  • Use compiled UDFs for complex functions that:

    • Have a significant CPU requirement (for example, a property UDF that is called on a per-cell basis every iteration)

    • Require access to a shared library.