3.8. Miscellaneous Macros

3.8.1. Data_Valid_P()

You can check that the cell values of the variables that appear in your UDF are accessible before you use them in a computation by using the Data_Valid_P macro.

 cxboolean Data_Valid_P() 

Data_Valid_P is defined in the id.h header file, and is included in udf.h. The function returns 1 (true) if the data that is passed as an argument is valid, and 0 (false) if it is not.

Example:

 if(!Data_Valid_P()) return; 

Suppose you read a case file and, in the process, load a UDF. If the UDF performs a calculation using variables that have not yet been initialized, such as the velocity at interior cells, then an error will occur. To avoid this kind of error, an if else condition can be added to your code. If (if) the data are available, the function can be computed in the normal way. If the data are not available (else), then no calculation, or a trivial calculation can be performed instead. After the flow field has been initialized, the function can be reinvoked so that the correct calculation can be performed.

3.8.2. FLUID_THREAD_P()

 cxboolean FLUID_THREAD_P(t); 

You can use FLUID_THREAD_P to check whether a cell thread is a fluid thread. The macro is passed a cell thread pointer t, and returns 1 (or TRUE) if the thread that is passed is a fluid thread, and 0 (or FALSE) if it is not.

Note that FLUID_THREAD_P(t) assumes that the thread is a cell thread.

For example,

 FLUID_THREAD_P(t0); 

returns TRUE if the thread pointer t0 passed as an argument represents a fluid thread.

3.8.3. Get_Report_Definition_Values

You can access the last calculated report definition value for any report definition you created in Fluent using the Get_Report_Definition_Values API.

Before calling this API you must either specify the output arguments as NULL or allocate appropriate memory to them. The API will fill the output arguments that are not specified as NULL. If memory is not appropriately allocated, the calculation run may crash or result in unexpected behavior.

int Get_Report_Definition_Values(const char* name, int
timeStep/iteration, int* nrOfvalues, real* values, int* ids, int*
index)

A return value of 0 means the call was successful, 1 means the specified report definition does not exist.


Important:  For Get_Report_Definition_Values to work properly with its referenced report definition:

  • The report definition must be included in at least one report file or report plot.

  • The Average Over value of the report definition must match the Get Data Every frequency of the including report file or report plot.


Argument Type

Description

const char* [input]

name of the report definition

int [input]

flag with a value of 0 or 1. 0 provides the value calculated at an iteration, 1 provides the value at a time-step. (1 is only valid for unsteady calculations.)

int* [output]

number of values available for the report definition

real* [output]

values for the report definition

** int* [output]

surface/zone IDs corresponding to the values

int* [output]

calculation index

** If the per-zone or per-surface option is disabled and the number of surfaces/zones is greater than 1, the surface/zone id will return -1

 

Steady-State Example

The following example shows how to use the API for a steady-state case:

int nrOfvalues=0;
real *values;
int *ids;
int index;
int counter;

/*First call to get the number of values. For number of values, 
  the int pointer is passed, which is 0 for iterations.*/

int rv = Get_Report_Definition_Values("report-def-0", 0, &nrOfvalues, NULL, NULL,NULL);

if (rv==0 && nrOfvalues)
{

    Message("Report definition evaluated at iteration has %d values\n", nrOfvalues);

    /*Memory is allocated for values and ids.*/

    values = (real*) malloc(sizeof(real)* nrOfvalues);
    ids = (int*) malloc(sizeof(int)* nrOfvalues);

    /* Second call to get data. The number of values is null, but the last 
     * three are not.*/
    rv = Get_Report_Definition_Values("report-def-0", 0, NULL, values, ids, &index); 

    Message("Values correspond to iteration index:%d\n", index);

    for ( counter = 0; counter < nrOfvalues; counter++ )
    {
        Message("report definition values: %d, %f\n", ids[counter], values[counter]); 
    } 

    /*Memory is freed.*/
    free(values); 
    free(ids); 
 }
 else
 {
    /*The command can be unsuccessful if the report definition does not exist 
      or if it has not been evaluated yet.*/
     if (rv == 1)
     {
         Message("report definition: %s does not exist\n", "report-def-0");
     }
     else if ( nrOfvalues == 0 )
     {    
         Message("report definition: %s not evaluated at iteration level\n", "report-def-0");
     }
}

Transient Example

The following example shows how to use the API for a transient case:

int nrOfvalues=0;
real *values;
int *ids;
int index;
int counter;

/*First call to get the number of values. For number of values, 
  the int pointer is passed, which is 1 for timesteps.*/

int rv = Get_Report_Definition_Values("report-def-0", 1, &nrOfvalues, NULL, NULL,NULL);

if (rv==0 && nrOfvalues)
{

    Message("Report definition evaluated at time-step has %d values\n", nrOfvalues);

    /*Memory is allocated for values and ids.*/

    values = (real*) malloc(sizeof(real)* nrOfvalues);
    ids = (int*) malloc(sizeof(int)* nrOfvalues);

    /* Second call to get data. The number of values is null, but the last 
     * three are not.*/
    rv = Get_Report_Definition_Values("report-def-0", 1, NULL, values, ids, &index); 

    Message("Values correspond to time-step index:%d\n", index);

    for ( counter = 0; counter < nrOfvalues; counter++ )
    {
        Message("report definition values: %d, %f\n", ids[counter], values[counter]); 
    } 

    /*Memory is freed.*/
    free(values); 
    free(ids); 
 }
 else
 {
    /*The command can be unsuccessful if the report definition does not exist 
      or if it has not been evaluated yet.*/
     if (rv == 1)
     {
         Message("report definition: %s does not exist\n", "report-def-0");
     }
     else if ( nrOfvalues == 0 )
     {    
         Message("report definition: %s not evaluated at time-step level\n", "report-def-0");
     }
}

3.8.4. M_PI

The macro M_PI returns the value of .

3.8.5. NULLP & NNULLP

You can use the NULLP and NNULLP functions to check whether storage has been allocated for user-defined scalars. NULLP returns TRUE if storage is not allocated, and NNULLP returns TRUE if storage is allocated. Below are some examples of usage.

 NULLP(T_STORAGE_R_NV(t0, SV_UDSI_G(p1)))
 
 /* NULLP returns TRUE if storage is not allocated for
  user-defined storage variable       */
 
 NNULLP(T_STORAGE_R_NV(t0, SV_UDSI_G(p1)))
 
 /* NNULLP returns TRUE if storage is allocated for
  user-defined storage variable       */
 

3.8.6. N_UDM

You can use N_UDM to access the number of user-defined memory (UDM) locations that have been used in Ansys Fluent. The macro takes no arguments, and returns the integer number of memory locations used. It is defined in models.h.

3.8.7. N_UDS

You can use N_UDS to access the number of user-defined scalar (UDS) transport equations that have been specified in Ansys Fluent. The macro takes no arguments and returns the integer number of equations. It is defined in models.h.

3.8.8. SQR(k)

SQR(k) returns the square of the given variable k, or k*k.

3.8.9. UNIVERSAL_GAS_CONSTANT

UNIVERSAL_GAS_CONSTANT returns the value of the universal gas constant ().


Important:  Note that this constant is not expressed in SI units.


See DEFINE_VR_RATE for an example UDF that utilizes UNIVERSAL_GAS_CONSTANT.