Author Topic: How to create a UDF for mass weighted average function on a surface?  (Read 7743 times)

Offline pitney1

  • Jr. Member
  • **
  • Posts: 65
  • Reputation: +0/-0
  • Searching for solution
    • View Profile
Advertisement
I want to make a UDF which I can use for calculating mass-weighed average of the properties on a specified surface. Can any one give me something to start with?

Thanks.

Offline piso

  • Newbie
  • *
  • Posts: 13
  • Reputation: +3/-0
  • Searching for solution
    • View Profile
Re: How to create a UDF for mass weighted average function on a surface?
« Reply #1 on: February 06, 2012, 07:10:52 PM »
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; 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; k++)
{
m = (k+1)%(s->facets);
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; k<s->nf; i+=s->facets+1, k++)
if (s->facets >= 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; 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; k++)
{
m = (k+1)%(s->facets);
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; k<s->nf; i+=s->facets+1, k++)
if (s->facets >= 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);
}