Compiling and Loading User Defined Functions using FLUENT 14.5 or 15.0
The Fluent solver is a general-purpose code. In order to customize the Fluent solver, users can use their own C-codes called user-defined functions (UDFs) to accomplish:
- Special boundary conditions
- Customized or solution dependent material properties
- New physical models
- Reaction rates
- Source terms
- Customized post-processing
- Solving user-supplied partial differential equations
- More …
UDFs can either be compiled or interpreted. Fluent is bundled with a C++ interpreter that can be used to interpret UDFs. To compile UDFs, you can use Microsoft Visual Studio (Express or Professional Editions) under Windows. Most Linux systems provide a C compiler as a standard feature.
Here are a few important differences between an interpreted code vs. a compiled code:
- C++ Interpreter bundled with Fluent
- Interpreter executes code on a “line by line” basis instantaneously
- Advantage – Does not require a third-party compiler
- Disadvantage – Interpreter is slow, and cannot do some functions
- UDF code is translated once into machine language (object modules)
- Efficient way to run UDFs
- Creates shared libraries which are linked with the rest of the solver
- Does require a compilation step between creating/editing your UDF and using it
In some situation you need to compile your code. To do so, you will need to configure Visual Studio to become accessible from Fluent. The following steps will help you achieve this:
1. ANSYS did not provide support for the 2012 compiler until Release 15. In order to use the 2012 compiler and compile Fluent UDFs using the Fluent Launcher and if you are running Fluent 14.5 you will need to install the file: udf-batch-file-fixed-for-2012 (contact us). If you are running Fluent 15 you do not need to do this.
2. Browse to C:\Program Files\ANSYS Inc\v145\fluent\ntbin\win64 and rename udf.bat.
3. Rename udf-batch-file-fixed-for-2012 to udf.bat and place it in the C:\Program Files\ANSYS Inc\v145\fluent\ntbin\win64 directory.
4. Download and Install Microsoft Visual Studio 2012 Express for Windows Desktop
http://www.microsoft.com/en-us/download/details.aspx?id=34673 (If this link is not active do an Internet search for: “Download Microsoft Visual Studio 2012 Express for Windows Desktop”
NOTE: If you are running ANSYS Fluent serial and not parallel you can stop here and compile
without taking any further steps.
5. Share the “fluent” folder located at: C:\Program Files\Ansys Inc\v145\fluent or: C:\Program Files\Ansys Inc\v150\fluent
6. Create a working directory that contains the FLUENT case, data files and UDF source code files. DO NOT COMPILE OR LOAD UDFS FROM FOLDERS WITH SPACES IN THE NAME AND DO NOT INCLUDE SPACES IN THE NAME OF YOUR CASE AND DATA FILES.
7. Share the Working directory folder.
Follow these steps to share a folder:
Right-click on the folder and choose Properties, then select the Sharing tab.
Click Advanced Sharing
Check Share this folder, then click Permissions
Check Allow on the Full Control row
Make sure that you have Full Control
Click Apply, OK
8. Map a drive letter to the shared folder, for example, Z:\ (When compiling UDFs and running in parallel you must use a mapped drive letter to the shared folder. FLUENT will use this shared folder as its Working Directory in the FLUENT Launcher.)
Follow these steps to map a drive:
- There are several ways to map a drive letter. After the working directory is shared with Full Control, right-click on Computer (My Computer) and choose Map Network Drive.
- Enter in your computer name and the share name as shown in the image below.
NOTE: If you have problems with any of these steps please contact your local IT department.
Launch ANSYS Fluent
1. Launch ANSYS Fluent
2. Select Show More Options from the ANSYS Fluent Launcher.
3. Set your Working Directory to the shared, mapped drive letter
4. Change the Fluent Root Path to be UNC (your computer name and the share name)
5. Select OK to start FLUENT, then select Define > User Defined > Functions > Compiled.
6. Select the Add button to add your source file.
7. Select Build.
8. After the UDF has been successfully built it is necessary to type in the network path to the compiled UDF in the Library Name panel BEFORE loading the UDF.
9. In front of the folder, libudf, type in the name of the computer where the UDF resides using the UNC format and the name of the shared folder. In the example below, the shared folder name is "working”.
10. Click Load.
After you have compiled the UDF the working folder will include a new subfolder called libudf.
Compiling ANSYS Fluent UDFs from the Command Line
If you wish to compile ANSYS Fluent UDFs from the command line follow these steps
1. Open up the Microsoft Visual Studio x64 Cross Tools Command Prompt window
2. Change to your shared, mapped network drive letter.
3. Start Fluent from the command line. You may need to put Fluent on path or call Fluent directly call it from your installation folder:
4. Follow the procedures above for compiling UDFs.