Calculate radial, tangential and axial velocity components in UDFs
A user wants to apply an energy source term that is function of radial velocity. Since only velocities in cartesian coordinates are available through UDF, this routine was created to calculate velocities in cylindrical coordinates. This UDF calculates velocities in cylindrical coordinates based in the origin and axis defined in the Fluid region. Then Ur is used to calculate the energy source term. The part that calculates the velocities can be used in any other macro. The axis and the origin of the axis are arbitrary. The axis is not aligned with any cartesian direction and is in the form of (ex,ey,ez). The main issue is to get er, et and ea vectors to calculate the velocities in cylindrical coordinates in a generic fashion. All the units in the UDF have to be in SI. The UDF is documented line by line to understand all steps.  #include "udf.h" #define SCOLD 940600.0 DEFINE_SOURCE(cell_cold, cell, thread, dS, eqn) { real NV_VEC(origin), NV_VEC(axis); real NV_VEC(V), NV_VEC(r), NV_VEC(R), NV_VEC(B); real NV_VEC(er), NV_VEC(et); real xc[ND_ND]; real Bmag, rmag, source; real ua, ur, ut; /* Get origin vector of fluid region */ NV_V (origin, =, THREAD_VAR(thread).cell.origin); /* Get axis of fluid region */ NV_V (axis, =, THREAD_VAR(thread).cell.axis); /* Store the 3 cartesian velocity components in vector V */ N3V_D(V, =, C_U(cell,thread),C_V(cell,thread),C_W(cell,thread)); /* Get current cell coordinate */ C_CENTROID(xc,cell,thread); /* Calculate (R) = (Xc)(Origin) */ NV_VV(R, =, xc, , origin); /* Calculate B = (R) dot (axis)*/ Bmag = NV_DOT(R,axis); /* Calculate (B) = B*axis */ NV_VS(B,=,axis,*,Bmag); /* Calculate (r) = (R)(B) This is the local radial vector*/ NV_VV(r, =, R, , B); /* Calculate r*/ rmag = NV_MAG(r); if (rmag != 0.) { NV_VS (er,=,r,/,rmag); NV_CROSS(et, axis, er); ur = NV_DOT(V,er); ut = NV_DOT(V,et); ua = NV_DOT(V,axis); } else { ur = 0.0; ut = 0.0; ua = NV_DOT(V,axis); } /*source term */ source = SCOLD*ur; /* derivative of source term w.r.t. enthalpy */ dS[eqn] = 0.0; return source; } 

