7.3.3. Limitations of Parallel UDFs

The macro PRINCIPAL_FACE_P can be used only in compiled UDFs (this is true for serial UDFs as well).

PRF_GRSUM1 and similar global reduction macros (Global Reduction Macros) cannot be used within macros such as DEFINE_SOURCE and DEFINE_PROPERTY UDFs, which are generally called for each cell (or face) and thus are called a different number of times on each compute node. As a workaround, you can use macros that are called only once on each node, such as DEFINE_ADJUST, DEFINE_ON_DEMAND, and DEFINE_EXECUTE_AT_END UDFs. For example, you could write a DEFINE_ADJUST UDF that calculates a global sum value in the adjust function, and then save the variable in user-defined memory. You can subsequently retrieve the stored variable from user-defined memory and use it inside a DEFINE_SOURCE UDF. This is demonstrated below.

In the following example, the spark volume is calculated in the DEFINE_ADJUST function and the value is stored in user-defined memory using C_UDMI. The volume is then retrieved from user-defined memory and used in the DEFINE_SOURCE UDF.

#include "udf.h"
 
 static real spark_center[ND_ND]={20e-3, 1e-3};
 static int fluid_chamber_ID = 2;
 
 DEFINE_ADJUST(adjust, domain)
 {
 #if !RP_HOST
 real vol, xc[ND_ND], dis[ND_ND], radius;
 cell_t c;
 Thread * tc;
 tc = Lookup_Thread(domain, fluid_chamber_ID);
 radius = RP_Get_Real("spark/radius");
 vol = 0;
 begin_c_loop_int (c, tc)
    {
      C_CENTROID(xc, c, tc);
      NV_VV(dis, =, xc, -, spark_center);

      if (NV_MAG(dis) < radius)
       {
          vol += C_VOLUME(c, tc);
        }
    }
 end_c_loop_int (c, tc)
 vol = PRF_GRSUM1(vol);

 begin_c_loop_int (c, tc)
    {
      C_UDMI(c, tc, 1) = vol;
    }
 end_c_loop_int (c, tc) 
 #endif
 }
 
 DEFINE_SOURCE(energy_source, c, t, dS, eqn)
 {
    #if !RP_HOST /* Not required because DEFINE_SOURCE is executed on nodes as indicated 
    by the arguments "c" and "t" */
      real xc[ND_ND], dis[ND_ND];
      real source, radius, vol, CA, rpm, start_CA;
      rpm  = RP_Get_Real("dynamesh/in-cyn/crank-rpm");
      start_CA  = RP_Get_Real("spark/start-ca");
      CA = rpm*CURRENT_TIME*6+RP_Get_Real("dynamesh/in-cyn/crank-start-angle");
      if(CA>=start_CA&&CA<(start_CA+RP_Get_Real("spark/duration")*rpm*6))
       {
         radius = RP_Get_Real("spark/radius");
         vol = C_UDMI(c, t, 1);
         C_CENTROID(xc, c, t);
         NV_VV(dis, =, xc, -, spark_center);
         if (NV_MAG(dis) < radius)
           {
              source =
              RP_Get_Real("spark/energy")/RP_Get_Real("spark/duration")/vol;
             return source;
            }
         else
            {
              return 0;
            }
         }
      else
         {
          return 0;
         }
    #endif /* !RP_HOST */
 } 

Important:  Interpreted UDFs cannot be used while running in parallel with an Infiniband interconnect. The compiled UDF approach should be used in this case.



Release 18.2 - © ANSYS, Inc. All rights reserved.