DEFINE_ON_DEMAND is a general-purpose
macro that you can use to specify a UDF that is executed “on
demand" in ANSYS Fluent, rather than having ANSYS Fluent call it automatically
during the calculation. Your UDF will be executed immediately, after
it is activated, but it is not accessible while the solver is iterating.
Note that the domain pointer d is not explicitly
passed as an argument to DEFINE_ON_DEMAND. Therefore, if you want to use the domain variable in your on-demand
function, you will need to first retrieve it using the Get_Domain utility provided by ANSYS Fluent (shown in the
example below). See Domain Pointer (Get_Domain) for
details on Get_Domain.
DEFINE_ON_DEMAND (name)
|
Argument Type |
Description |
|
|
UDF name. |
Function returns
void
There is only one argument to DEFINE_ON_DEMAND: name. You supply name, the name of the UDF.
The following UDF, named on_demand_calc, computes and prints the minimum, maximum, and average temperatures
for the current data field. It then computes a temperature function
(2–1) |
and stores it in user-defined memory location (which is allocated as described
in Cell Macros). After you hook the on-demand
UDF (as described in Hooking
DEFINE_ON_DEMAND UDFs),
the field values for will be available in drop-down
lists in postprocessing dialog boxes in ANSYS Fluent. You can select this
field by choosing User Memory 0 in the User-Defined Memory... category. If you write a data file
after executing the UDF, the user-defined memory field will be saved
to the data file. This source code can be interpreted or compiled
in ANSYS Fluent.
/**********************************************************************
UDF to calculate temperature field function and store in
user-defined memory. Also print min, max, avg temperatures.
***********************************************************************/
#include "udf.h"
DEFINE_ON_DEMAND(on_demand_calc)
{
Domain *d; /* declare domain pointer since it is not passed as an
argument to the DEFINE macro */
real tavg = 0.;
real tmax = 0.;
real tmin = 0.;
real temp,volume,vol_tot;
Thread *t;
cell_t c;
d = Get_Domain(1); /* Get the domain using ANSYS Fluent utility */
/* Loop over all cell threads in the domain */
thread_loop_c(t,d)
{
/* Compute max, min, volume-averaged temperature */
/* Loop over all cells */
begin_c_loop(c,t)
{
volume = C_VOLUME(c,t); /* get cell volume */
temp = C_T(c,t); /* get cell temperature */
if (temp < tmin || tmin == 0.) tmin = temp;
if (temp > tmax || tmax == 0.) tmax = temp;
vol_tot += volume;
tavg += temp*volume;
}
end_c_loop(c,t)
tavg /= vol_tot;
printf("\n Tmin = %g Tmax = %g Tavg = %g\n",tmin,tmax,tavg);
/* Compute temperature function and store in user-defined memory*/
/*(location index 0) */
begin_c_loop(c,t)
{
temp = C_T(c,t);
C_UDMI(c,t,0) = (temp-tmin)/(tmax-tmin);
}
end_c_loop(c,t)
}
}
Get_Domain is a macro that retrieves
the pointer to a domain. It is necessary to get the domain pointer
using this macro since it is not explicitly passed as an argument
to DEFINE_ON_DEMAND. The function, named
on_demand_calc, does not take any explicit
arguments. Within the function body, the variables that are to be
used by the function are defined and initialized first. Following
the variable declarations, a looping macro is used to loop over each
cell thread in the domain. Within that loop another loop is used to
loop over all the cells. Within the inner loop, the total volume and
the minimum, maximum, and volume-averaged temperature are computed.
These computed values are printed to the ANSYS Fluent console. Then a
second loop over each cell is used to compute the function and store
it in user-defined memory location
. Refer to Additional Macros for Writing UDFs for a description of predefined
macros such as
C_T and begin_c_loop.
After the UDF that you have defined using DEFINE_ON_DEMAND is interpreted (Interpreting UDFs)
or compiled (Compiling UDFs), the name
of the argument that you supplied as the first DEFINE macro argument (for example, on_demand_calc) will become visible and selectable in the Execute On
Demand dialog box in ANSYS Fluent. See Hooking DEFINE_ON_DEMAND UDFs for details.