5.3. Compile a UDF Using the TUI

The first step in compiling a UDF source file using the text user interface (TUI) involves setting up the folder structure where the shared (compiled) library will reside, for each of the versions of Ansys Fluent you want to run (that is, 2d_node and 2d_host). You will then modify the file named Makefile to set up source file parameters. Subsequently, you will execute the Makefile, which compiles the source file and builds the shared library from the resulting object files. Finally, you will load the UDF library into Ansys Fluent. Using the TUI option allows you the added advantage of building a shared library for precompiled object file(s) that are derived from non-Ansys Fluent sources (for example, .o objects from .f sources). See Link Precompiled Object Files From Non-Ansys Fluent Sources for details.


Important:  Note that when running serial or parallel Ansys Fluent on a Windows system, it is recommended that you install a supported version of Microsoft Visual Studio or Clang on your machine, preferably on the C: drive. If the compiler you installed is an older version that is not supported, then you should enable the use of a built-in compiler (Clang) that is provided as part of the Fluent installation by entering the following text command prior to compiling: define/user-defined/use-built-in-compiler? yes. If Fluent determines that you have not installed a compiler on your computer, the built-in compiler is used automatically, whether or not you have enabled this text command.


5.3.1. Set Up the Directory Structure

The folder/directory structures for Windows systems and Linux systems are different, so the procedure for setting up the folder/directory structure is described separately for each.

5.3.1.1. Windows Systems

For compiled UDFs on Windows systems, two Ansys Fluent files are required to build your shared UDF library: makefile_nt.udf and user_nt.udf. The file user_nt.udf has a user-modifiable section that allows you to specify source file parameters.

The procedure below outlines steps that you must follow to set up the folder structure required for the shared library.

  1. In your working folder, make a folder that will store your UDF library (for example, libudf).

  2. Make a folder below this called src.

  3. Put all your UDF source files into this folder (for example, libudf\src).

  4. Make an architecture folder below the library folder called win64 for Intel systems running Windows (for example, libudf\win64).

  5. In the architecture folder (for example, libudf\win64), create folders for the Ansys Fluent versions you want to build for your architecture. (for example, win64\2d_node and win64\2d_host). Possible versions are:

    2d_node and 2d_host

    single-precision 2D

    3d_node and 3d_host

    single-precision 3D

    2ddp_node and 2ddp_host

    double-precision 2D

    3ddp_node and 3ddp_host

    double-precision 3D


    Important:  Note that you must create two build folders for each version of the solver (two for the 3D version, two for the 2D double-precision version, and so on), regardless of the number of compute nodes.


  6. Copy user_nt.udf from

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

    to all the version sub-folders you have made (for example, libudf\win64\3d_node and libudf\win64\3d_host).

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

  7. Copy makefile_nt.udf from

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

    to all the version sub-folders you have made (for example, libudf\win64\3d_node and libudf\win64\3d_host) and rename it Makefile.

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


    Note:  If you are compiling a UDF outside of the Fluent environment, you need to add the FLUENT_INC=<your fluent path> and FLUENT_ARCH=<machine architecture> (for example, win64) environment variables to the <user>_nt.udf file.



Important:  Be sure the path to your Ansys Fluent home directory is in your command search path environment variable by executing the setenv.exe program located in the Ansys Fluent directory (for example, C:\Program Files\ANSYS Inc\v242\fluent\ntbin\win64).


5.3.1.2. Linux Systems

For compiled UDFs on Linux systems, three Ansys Fluent files are required to build your shared UDF library: makefile.udf, makefile.udf2, and user.udf.

The procedure below outlines steps that you must follow to set up the directory structure required for the shared library.

  1. In your working directory, make a directory that will store your UDF library (for example, libudf).

  2. Copy makefile.udf2 from

    path/ansys_inc/v242/fluent/fluent24.2.0/src/udf

    to the library directory (for example, libudf), and name it makefile.

    Note that path is the directory in which you have installed Ansys Fluent.

  3. In the library directory you just created in Step 1, make a directory that will store your source file and name it src.

  4. Copy your source file (for example, myudf.c) to the source directory (src).

  5. Copy makefile.udf from

    path/ansys_inc/v242/fluent/fluent24.2.0/src/udf

    to the /src directory, and name it makefile.

  6. Identify the architecture name of the machine on which you are running (for example, lnamd64). This can be done by either typing the command (fluent-arch) in the Ansys Fluent TUI window, or running the Ansys Fluent utility program fluent_arch at the command line of a Linux shell.

  7. In the library directory (for example, libudf), create an architecture directory that is named after the architecture identifier determined in the previous step (for example, lnamd64).

  8. In the architecture directory, create directories named after the Ansys Fluent versions for which you want to build shared libraries (for example, lnamd64/2d_node and lnamd64/2d_host). Possible versions are:

    2d_node and 2d_host

    single-precision 2D

    3d_node and 3d_host

    single-precision 3D

    2ddp_node and 2ddp_host

    double-precision 2D

    3ddp_node and 3ddp_host

    double-precision 3D


    Important:  Note that you must create two build directories for each version of the solver (two for the 3D version, two for the 2D double-precision version, and so on), regardless of the number of compute nodes.


  9. Copy user.udf from path/ansys_inc/v242/fluent/fluent24.2.0/src/udf/user.udf to all the version sub-folders that you have made (for example, libudf/lnamd64/3d_node and libudf/lnamd64/3d_host).

5.3.2. Build the UDF Library

After you have set up the folder structure and put the files in the proper places, you can compile and build the shared library using the TUI.

5.3.2.1. Windows Systems

  1. Edit every user_nt.udf file in each version folder to set the following parameters: CSOURCES, HSOURCES, VERSION, and PARALLEL_NODE.

    CSOURCES =

    the user-defined source file(s) to be compiled.

    Use the prefix $(SRC) before each filename. For example, $(SRC)udfexample.c for one file, and $(SRC)udfexample1.c $(SRC)udfexample2.c for two files.

    HSOURCES =

    the user-defined header file(s) to be compiled.

    Use the prefix $(SRC) before each filename. For example, $(SRC)udfexample.h for one file, and $(SRC)udfexample1.h $(SRC)udfexample2.h for two files.

    VERSION =

    the version of the solver you are running which will be the name of the build folder where user_nt.udf is located. (2d_host, 2d_node, 3d_host, 3d_node, 2ddp_host, 2ddp_node, 3ddp_host, or 3ddp_node).

    PARALLEL_NODE =

    the parallel communications library.

    Specify none for the host version, or one of the following for the node version:

    intel: parallel using Intel MPI

    msmpi: parallel using Microsoft MPI


    Important:  Be sure to edit both copies of user_nt.udf (the one in the host folder and the one in the node folder), and specify the appropriate CSOURCES, HSOURCES, VERSION, and PARALLEL_NODE in each file. Set PARALLEL_NODE = none for the host version and one of the other options (that is, intel or msmpi) for the node version, depending on which message passing method you are going to use.


    An excerpt from a sample user_nt.udf file is shown below:

    # Replace text in " " (and remove quotes)
    # | indicates a choice
    # note: $(SRC) is defined in the Makefile
    
      CSOURCES = $(SRC)udfexample.c
      HSOURCES = $(SRC)udfexample.h
      VERSION = 2d_host
      PARALLEL_NODE = none
      
    
  2. If you have installed a supported compiler, go to each version folder (for example, libudf\win64\2d_node and libudf\win64\2d_host) in the appropriate command prompt window and compile the UDF using the nmake command (for Microsoft Visual Studio) or the scons command (for Clang). The following is an example using the Visual Studio command prompt window:

     C:\users\user_name\work_dir\libudf\win64\2d_node>nmake 
    

    The following messages will be displayed:

     Microsoft (R) Program Maintenance Utility Version 7.10.3077 
    Copyright (C) Microsoft Corporation. All rights reserved.
     cl /c /Za /DUDF_EXPORTING
       -Ic:\Program Files\ANSYS Inc\v242\fluent\fluent24.2.0\win64\2d_node
       -Ic:\Program Files\ANSYS Inc\v242\fluent\fluent24.2.0\src
       -Ic:\Program Files\ANSYS Inc\v242\fluent\fluent24.2.0\cortex\src
       -Ic:\Program Files\ANSYS Inc\v242\fluent\fluent24.2.0\client\src
       -Ic:\Program Files\ANSYS Inc\v242\fluent\fluent24.2.0\tgrid\src
       -Ic:\Program Files\ANSYS Inc\v242\fluent\fluent24.2.0\multiport\src
          ..\..\src\udfexample.c 
    Microsoft (R) 32-bit C/C++ Standard Compiler Version 13.10.3077 for 80x86 
    Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.
     
     udfexample.c
     # Generating udf_names.c because of Makefile udfexample.obj
             cl /c /Za /DUDF_EXPORTING
       -Ic:\Program Files\ANSYS Inc\v242\fluent\fluent24.2.0\win64\2d_node
       -Ic:\Program Files\ANSYS Inc\v242\fluent\fluent24.2.0\src
       -Ic:\Program Files\ANSYS Inc\v242\fluent\fluent24.2.0\cortex\src
       -Ic:\Program Files\ANSYS Inc\v242\fluent\fluent24.2.0\client\src
       -Ic:\Program Files\ANSYS Inc\v242\fluent\fluent24.2.0\tgrid\src
       -Ic:\Program Files\ANSYS Inc\v242\fluent\fluent24.2.0\multiport\src
            udf_names.c 
    Microsoft (R) 32-bit C/C++ Standard Compiler Version 13.10.3077 for 80x86 
    Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.
     
     udf_names.c
     # Linking libudf.dll because of Makefile user_nt.udf 
    udf_names.obj udfexample.obj 
    link /Libpath:c:\Program Files\ANSYS Inc\v242\fluent\fluent24.2.0\win64\2d_node
           /dll
       /out:libudf.dl 
    l udf_names.obj udfexample.obj  fl2429s.lib 
    Microsoft (R) Incremental Linker Version 7.10.3077 
    Copyright (C) Microsoft Corporation. All rights reserved.
    
         Creating library libudf.lib and object libudf.exp
     
     C:\Program Files\ANSYS Inc\v242\fluent\ntbin\win64\libudf\win64\2d_node>
    

    Important:  Note that if there are problems with the build, you can do a complete rebuild by typing nmake clean and then nmake again.


  3. If you installed an older version of Microsoft Visual Studio or Clang on your machine that is not supported, enter the following text command: define/user-defined/use-built-in-compiler? yes. This ensures the use of a built-in compiler (Clang) that is provided with the Fluent installation. If Fluent determines that you have not installed a compiler on your computer, the built-in compiler is used automatically, whether or not you have enabled this text command.

    Then compile the UDF using the following text command: define/user-defined/compiled-functions.

5.3.2.2. Linux Systems

  1. Edit every user.udf file in each version folder to set the following parameters: CSOURCES, HSOURCES, and Ansys Fluent path.

    CSOURCES =

    The name of your source file(s) (for example, udfexample.c). Multiple sources can be specified by using a space delimiter (for example, udfexample1.c udfexample2.c).

    HSOURCES =

    The name of your header file(s) (for example, udfexample.h). Multiple headers can be specified by using a space delimiter (for example, udfexample1.h udfexample2.h).

    FLUENT_INC =

    The path to your release directory.

  2. An excerpt from a sample user.udf file is shown below:

    CSOURCES = udfexample.c
    HSOURCES = udfexample.h
    FLUENT_INC=/path/ansys_inc/v242/fluent
    

    In the previous example, path represents the directory where you installed Ansys Fluent.

  3. In your library directory (for example, libudf), execute the Makefile by typing a command that begins with make and includes the architecture of the machine you will run Ansys Fluent on, which you identified in a previous step. For example, for the Linux (lnamd64) architecture type:

      make "FLUENT_ARCH=lnamd64"
    

    Ansys Fluent will build a shared library for each version you created a directory for (Set Up the Directory Structure) and will display messages about the compile/build process in the console. You can view the compilation history in the log file that is saved in your working directory.

    For example, when compiling/building a shared library for a source file named profile.c and a UDF library named libudf on a Linux architecture, the console messages may include the following:

     Working...
      for d in lnamd64[23]*; do \
        ( \
         cd $d; \
         for f in ../../src*.[ch] ../../src/Makefile; do \
           if [ ! -f ’basename $f’ ]; then \
            echo "# linking to " $f "in" $d; \
            ln -s $f .; \
           fi; \
         done; \
         echo ""; \
         echo "# building library in" $d; \
         make -k>makelog 2>&1; \
         cat makelog; \
      ) \
      done
      # linking to ...  myudf.c in lnamd64/2d_node
    
      # building library in lnamd64/2d_node
      make[1]: Entering directory ..../udf_names.c
      # Generating udf_names
      make[2]: Entering directory ..../profile.c
      make libudf.so ...
      # Compiling udf_names.o ...
      # Compiling profile.o ...
      # Linking libudf.so ...
      make[2]: Leaving directory ..../udf_names.c
      make[1]: Leaving directory ..../profile.c
    
      You can also see the ’log’-file in
      the working directory for compilation history
      Done.
    

5.3.3. Load the UDF Library

You can load the shared library you compiled and built using the GUI from the Compiled UDFs dialog box or the UDF Library Manager dialog box. Follow the procedure outlined in Step 11. of Compiling a UDF Using the GUI or in Load and Unload Libraries Using the UDF Library Manager Dialog Box, respectively.