3.5. Time-Dependent Macros

You can access time-dependent variables in your UDF in two different ways: direct access using a solver macro, or indirect access using an RP variable macro. Table 3.43: Solver Macros for Time-Dependent Variables contains a list of solver macros that you can use to access time-dependent variables in Ansys Fluent. An example of a UDF that uses a solver macro to access a time-dependent variable is provided below. See DEFINE_DELTAT for another example that utilizes a time-dependent macro.

Table 3.43: Solver Macros for Time-Dependent Variables

Macro Name

Returns

CURRENT_TIME

real current flow time (in seconds)

CURRENT_TIMESTEP

real current physical time step size (in seconds)

PREVIOUS_TIME

real previous flow time (in seconds)

PREVIOUS_2_TIME

real flow time two steps back in time (in seconds)

PREVIOUS_TIMESTEP

real previous physical time step size (in seconds)

N_TIME

integer number of time steps

N_ITER

integer number of iterations



Important:  You must include the unsteady.h header file in your UDF source code when using the PREVIOUS_TIME or PREVIOUS_2_TIME macros since it is not included in udf.h.



Important:   N_ITER can only be utilized in compiled UDFs.


Some time-dependent variables such as current physical flow time can be accessed directly using a solver macro (CURRENT_TIME) , or indirectly by means of the RP variable macro.

Solver Macro Usage

 real current_time;
 current_time = CURRENT_TIME; 

"Equivalent" RP Macro Usage

 real current_time;
 current_time = RP_Get_Real("flow-time"); 

Table 3.44: Solver and RP Macros that Access the Same Time-Dependent Variable shows the correspondence between solver and RP macros that access the same time-dependent variables.

Table 3.44: Solver and RP Macros that Access the Same Time-Dependent Variable

Solver Macro

"Equivalent" RP Variable Macro

CURRENT_TIME

RP_Get_Real("flow-time")

CURRENT_TIMESTEP

RP_Get_Real("physical-time-step")

N_TIME

RP_Get_Integer("time-step")



Important:  You should not access a Scheme variable using any of the RP_GET_... functions from inside a cell or face looping macro (c_loop or f_loop). This type of communication between the solver and cortex is very time consuming and therefore should be done outside of loops.


Example

The integer time step count (accessed using N_TIME) is useful in DEFINE_ADJUST functions for detecting whether the current iteration is the first in the time step.

 /**********************************************************************
    Example UDF that uses N_TIME
 ***********************************************************************/
 static int last_ts = -1;  /* Global variable. Time step is never <0 */
 
 DEFINE_ADJUST(first_iter_only, domain)
 {
    int curr_ts;
    curr_ts = N_TIME;
    if (last_ts != curr_ts)
    {
    last_ts = curr_ts;
    /* things to be done only on first iteration of each time step
    can be put here */
    }
 } 

Important:  There is a new variable named first_iteration that can be used in the above if statement. first_iteration is true only at the first iteration of a timestep. Since the adjust UDF is also called before timestepping begins, the two methods vary slightly as to when they are true. You must decide which behavior is more appropriate for your case.