# How to create mass weighted average function by UDF on a surface? (not on a zone or thread)

 The following UDF allows you to compute mass weighted average of temperature based on any surface. You just have to provide surface ID. #include "udf.h" #include "surf.h" #include "cxsurf.h" #include "cxiface.h" #define SID 9 /*Surface ID to be modified*/ void facet_area_3D( Surface *s, int i, double area[] ) { double v[MAX_FACE_NODES][3]; int k, m; area[0]=area[1]=area[2]=0.0; for (k=0; k < s->facets[i]; k++) { v[k][0]=Get_Surface_Point_Coord(&( s->points[s->facets[i+k+1]]),X_DIM); v[k][1]=Get_Surface_Point_Coord(&( s->points[s->facets[i+k+1]]),Y_DIM); v[k][2]=Get_Surface_Point_Coord(&(s->points[s->facets[i+k+1]]),Z_DIM); } for (k=0; k < s->facets[i]; k++) { m = (k+1)%(s->facets[i]); area[0]+= 0.5*((v[m][1] + v[k][1]) * (v[m][2] - v[k][2])); area[1]+= 0.5*((v[m][2] + v[k][2]) * (v[m][0] - v[k][0])); area[2]+= 0.5*((v[m][0] + v[k][0]) * (v[m][1] - v[k][1])); } } DEFINE_ON_DEMAND(integrals) { Surface *s; cell_t c ; Thread *t; real mass_integral=0; real total_mass=0; double area[ND_ND],vel[ND_ND]; real avg_surf_temp; int i,k; /* s is the surface having data of surface id SID s->np no of points. s->nf no of faces. s->points points to array of np points. s->ip pointer to interpolation vector of np points. s->facets facet list of the surface. length of array is nfl. s->cells pointer to cells of each facet. s->nfl facet list length. */ s = SurfaceList+SID; for (i=0, k=0; knf; i+=s->facets[i]+1, k++) if (s->facets[i] >= 3) { facet_area_3D(s, i, area); /* Cell in which the facet lies */ c = RP_CELL(s->cells+k); t = RP_THREAD(s->cells+k); NV_D(vel,=,C_U(c,t),C_V(c,t),C_W(c,t)) ; mass_integral += C_R(c,t)*C_T(c,t)*fabs(NV_DOT(area,vel)); total_mass += C_R(c,t)*fabs( NV_DOT(area,vel)); } avg_surf_temp=mass_integral/total_mass; Message("Mass weighted average of Temperature at Surface ID=%d is %f",SID,avg_surf_temp); } #include "udf.h" #include "surf.h" #include "cxsurf.h" #include "cxiface.h" #define SID 9 /*Surface ID to be modified*/ void facet_area_3D( Surface *s, int i, double area[] ) { double v[MAX_FACE_NODES][3]; int k, m; area[0]=area[1]=area[2]=0.0; for (k=0; k < s->facets[i]; k++) { v[k][0]=Get_Surface_Point_Coord(&( s->points[s->facets[i+k+1]]),X_DIM); v[k][1]=Get_Surface_Point_Coord(&( s->points[s->facets[i+k+1]]),Y_DIM); v[k][2]=Get_Surface_Point_Coord(&(s->points[s->facets[i+k+1]]),Z_DIM); } for (k=0; k < s->facets[i]; k++) { m = (k+1)%(s->facets[i]); area[0]+= 0.5*((v[m][1] + v[k][1]) * (v[m][2] - v[k][2])); area[1]+= 0.5*((v[m][2] + v[k][2]) * (v[m][0] - v[k][0])); area[2]+= 0.5*((v[m][0] + v[k][0]) * (v[m][1] - v[k][1])); } } DEFINE_ON_DEMAND(integrals) { Surface *s; cell_t c ; Thread *t; real mass_integral=0; real total_mass=0; double area[ND_ND],vel[ND_ND]; real avg_surf_temp; int i,k; /* s is the surface having data of surface id SID s->np no of points. s->nf no of faces. s->points points to array of np points. s->ip pointer to interpolation vector of np points. s->facets facet list of the surface. length of array is nfl. s->cells pointer to cells of each facet. s->nfl facet list length. */ s = SurfaceList+SID; for (i=0, k=0; knf; i+=s->facets[i]+1, k++) if (s->facets[i] >= 3) { facet_area_3D(s, i, area); /* Cell in which the facet lies */ c = RP_CELL(s->cells+k); t = RP_THREAD(s->cells+k); NV_D(vel,=,C_U(c,t),C_V(c,t),C_W(c,t)) ; mass_integral += C_R(c,t)*C_T(c,t)*fabs(NV_DOT(area,vel)); total_mass += C_R(c,t)*fabs( NV_DOT(area,vel)); } avg_surf_temp=mass_integral/total_mass; Message("Mass weighted average of Temperature at Surface ID=%d is %f",SID,avg_surf_temp); }