
Below is an example of a compiled UDF that computes the gradient of temperature to the fourth power, and stores its magnitude in a userdefined scalar. The computed temperature gradient can, for example, be subsequently used to plot contours. Although the practical application of this UDF is questionable, its purpose here is to show the methodology of computing gradients of arbitrary quantities that can be used for postprocessing.
/***********************************************************************/ /* UDF for computing the magnitude of the gradient of T^4 */ /***********************************************************************/ #include "udf.h" /* Define which userdefined scalars to use. */ enum { T4, MAG_GRAD_T4, N_REQUIRED_UDS }; DEFINE_ADJUST(adjust_fcn, domain) { Thread *t; cell_t c; face_t f; /* Make sure there are enough userdefined scalars. */ if (n_uds < N_REQUIRED_UDS) Internal_Error("not enough userdefined scalars allocated");
/* Fill first UDS with temperature raised to fourth power. */ thread_loop_c (t,domain) { if (NULL != THREAD_STORAGE(t,SV_UDS_I(T4))) { begin_c_loop (c,t) { real T = C_T(c,t); C_UDSI(c,t,T4) = pow(T,4.); } end_c_loop (c,t) } } thread_loop_f (t,domain) { if (NULL != THREAD_STORAGE(t,SV_UDS_I(T4))) { begin_f_loop (f,t) { real T = 0.; if (NULL != THREAD_STORAGE(t,SV_T)) T = F_T(f,t); else if (NULL != THREAD_STORAGE(t>t0,SV_T)) T = C_T(F_C0(f,t),t>t0); F_UDSI(f,t,T4) = pow(T,4.); } end_f_loop (f,t) } } /* Fill second UDS with magnitude of gradient. */ thread_loop_c (t,domain) { if (NULL != THREAD_STORAGE(t,SV_UDS_I(T4)) && NULL != T_STORAGE_R_NV(t,SV_UDSI_G(T4))) { begin_c_loop (c,t) { C_UDSI(c,t,MAG_GRAD_T4) = NV_MAG(C_UDSI_G(c,t,T4)); } end_c_loop (c,t) } } thread_loop_f (t,domain) { if (NULL != THREAD_STORAGE(t,SV_UDS_I(T4)) && NULL != T_STORAGE_R_NV(t>t0,SV_UDSI_G(T4))) { begin_f_loop (f,t) { F_UDSI(f,t,MAG_GRAD_T4)=C_UDSI(F_C0(f,t),t>t0,MAG_GRAD_T4); } end_f_loop (f,t) } } }
The conditional statement if (NULL != THREAD_STORAGE(t,SV_UDS_I(T4))) is used to check if the storage for the userdefined scalar with index T4 has been allocated, while NULL != T_STORAGE_R_NV(t,SV_UDSI_G(T4)) checks whether the storage of the gradient of the userdefined scalar with index T4 has been allocated.
In addition to compiling this UDF, as described in Chapter 7, you will need to enable the solution of a userdefined scalar transport equation in FLUENT.
Define UserDefined Scalars...
See Section 10.3 for more details.