3.1. Introduction

Ansys Fluent provides numerous C types, functions, and preprocessor macros to facilitate the programming of UDFs and the use of CFD objects as defined inside Ansys Fluent. The previous chapter presented DEFINE macros with which you must define your UDF. This chapter presents predefined functions (implemented as macros in the code) that are supplied by Ansys Fluent that you will use to code your UDF. These macros allow you to access Ansys Fluent solver data such as cell variables (for example, cell temperature, centroid), face variables (for example, face temperature, area), or connectivity variables (for example, adjacent cell thread and index) that your UDF can use in a computation. Ansys Fluent provides:

  • Macros commonly used in UDFs that return such values as the thread ID pointer (an internal Ansys Fluent structure) when they are passed the Zone ID (the number assigned to a zone in a boundary conditions dialog box).

  • The F_PROFILE macro, which enables your UDF to set a boundary condition value in the solver.

  • Other macros that enable your function to loop over nodes, cells, and faces in a thread or domain in order to retrieve and/or set values.

  • Data access macros that are specific to a particular model (for example, DPM, NOx).

  • Macros that perform vector, time-dependent, Scheme, and I/O operations.

Function definitions for the macros provided in this chapter are contained in header files. Header files are identified by the .h suffix as in mem.h, metric.h, dpm_types.h, and dpm_laws.h and are stored in multiple directories under the following:

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

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

The header files, unless explicitly noted, are included in the udf.h file, so your UDF does not need to contain a special #include compiler directive. You must, however, remember to include the #include "udf.h" directive in any UDF that you write.

Access to data from an Ansys Fluent solver is accomplished by hooking your UDF C function (after it is compiled or interpreted) to the code through the graphical user interface (GUI). After the UDF is correctly hooked, the solver’s data is passed to the function and is available to use whenever it is called. These data are automatically passed by the solver to your UDF as function arguments. Note that all solver data, regardless of whether they are passed to your UDF by the solver or returned to the solver by the UDF, are specified in SI units. Macros in this chapter are listed with their arguments, argument types, returned values (if applicable), and header file.

Each function behind a macro either outputs a value to the solver as an argument, or returns a value that is then available for assignment in your UDF. Input arguments belong to the following Ansys Fluent data types:

Node *node

pointer to a node

cell_t c

cell identifier

face_t f

face identifier

Thread *t

pointer to a thread

Thread **pt

pointer to an array of phase threads

Below is an example of a UDF that utilizes two data access macros (C_T and C_CENTROID) and two looping macros (begin..end_c_loop and thread_loop_c) to assign initial temperature. Two looping macros are used to set the cell temperature of each cell in every thread in the computational domain. begin..end_c_loop is used to loop over all the cells in a cell thread to get the cell centroid and set the cell temperature, and thread_loop_c allows this loop to be repeated over all cell threads in the domain.

C_CENTROID has three arguments: xc, c, and t. Cell identifier c and cell thread pointer t are input arguments, and the argument array xc (the cell centroid) is output (as an argument) to the solver and used in the UDF in a conditional test.

C_T is used to set the cell temperature to the value of 400 or 300, depending on the outcome of the conditional test. It is passed the cell’s ID c and thread pointer t and returns the real value of the cell temperature to the Ansys Fluent solver.

Example

 /***********************************************************************
   UDF for initializing flow field variables
   Example of C_T and C_CENTROID usage.
 ************************************************************************/
 
 #include "udf.h"
 
 DEFINE_INIT(my_init_func,d)
 {
  cell_t c;
  Thread *t;
  real xc[ND_ND];
  /* loop over all cell threads in the domain */
  thread_loop_c(t,d)
  {
   /* loop over all cells */
   begin_c_loop(c,t)
   {
    C_CENTROID(xc,c,t);
    if (sqrt(ND_SUM(pow(xc[0] - 0.5,2.),
        pow(xc[1] - 0.5,2.),
        pow(xc[2] - 0.5,2.))) < 0.25)
    C_T(c,t) = 400.;
    else
    C_T(c,t) = 300.;
   }
   end_c_loop(c,t)
  }
 }