Eliminating the need for multiple DEFINE_SOURCE UDFs in Fluent
Often, a problem has multiple source terms (continuity, energy, species, etc.) When UDFs are used for each to compute sources, it is cumbersome because separate DEFINE_SOURCE macros are often used for each source, making the hooking process a bit cumbersome. This may be avoided using the following technique.
The UDF used to hook source terms takes the form DEFINE_SOURCE(my_source, c, t, dS, eqn) { real s; return s; } Here, "eqn" is an integer which is used to identify the equation to which the UDF is hooked in the GUI panel. In FLUENT header "mem.h", the equation index is labeled by an enumeration, and we can use this to know which equation we've hooked to in the panel. The following values (see the header for the complete list) are probably the most commonly used: EQ_CONTINUITY, EQ_X_MOM, EQ_Y_MOM, EQ_Z_MOM, EQ_ENERGY, EQ_SPECIES, EQ_UDS When hooked, "eqn" takes the preceding enumeration values when hooked to the continuity, xmom, ymom, zmom, energy, species0, uds0 equations, respectively. The following UDF illustrates how a *single* DEFINE_SOURCE UDF may be written, making the hooking process much less prone to error. Note that in this function there are utility functions for computing the source terms of continuity, energy, and all UDS equations, but there is only one UDF to hook for all equations. Please note carefully how the jacobian is set in the utility functions using the "*" pointerdereferencing operator. The source terms are computed as follows in this madeup example: Continuity: S = 1, dS = 2 Energy: S = 3, dS = 4 Species n: S = 5+n, dS = 6+n #include "udf.h" /* Source functions */ real source_continuity (real* jac); real source_energy (real* jac); real source_uds (real* jac, int n); DEFINE_SOURCE(source,c,t,dS,eqn) { real source=0.; int n; /* The enumerators are defined in Fluent.Inc/fluent6.xxx/src/mem.h */ if (eqn == EQ_CONTINUITY) { source = source_continuity(dS+eqn); Message("Mass eqn: s = %10.3e ds = %10.3en",source, dS[eqn]); return source; } if (eqn == EQ_ENERGY) { source = source_energy(dS+eqn); Message("Energy eqn: s = %10.3e ds = %10.3en",source, dS[eqn]); return source; } for(n=0; n<n_uds; n++) { if (eqn == EQ_UDS + n) { source = source_uds(dS+eqn, n); Message("UDS eqn: s = %10.3e ds = %10.3en",source, dS[eqn]); return source; } } return source; } real source_continuity(real* jac) { real source; source = 1.; *jac = 2.0; return source; } real source_energy(real* jac) { real source; source = 3.; *jac = 4.0; return source; } real source_uds(real* jac, int n) { real source; source = 5.+n; *jac = 6.0+n; return source; } 

