# What's wrong with my UDF?

 Register Blogs Members List Search Today's Posts Mark Forums Read

 September 8, 2019, 22:39 What's wrong with my UDF? #1 New Member   刘宵 Join Date: Aug 2019 Posts: 4 Rep Power: 3 Hi all, This is my code and I'm trying to calculate the value of w by UDF. The value of w is related to the angle at which the fluid impacts the wall. So I loop through the cells first, then loop the faces inside the cells. Finally, I hope that I can save the value of w to the data so that I can see the distribution of w from the contour. But my UDF doesn't work, I don't know what' wrong with it. Thanks very much if you can give me some suggestions. #include "udf.h" #include "math.h" #include "metric.h" #include "mem.h" DEFINE_ON_DEMAND(on_demand_w) { real w; int n; real densic=3200; real c; real alpha; real A[ND_ND]; real vel[3]; real velm; real Am; real cos; real velx; real vely; real velz; real qm; Domain *d; Thread *t; cell_t c0; face_t f; d=Get_Domain(1); c=sqrt(3*480*1000000/densic); thread_loop_c(t,d) { c0=F_C0(f,t); begin_c_loop(c0,t) { real denmix=C_R(c0,t); c_face_loop(c0,t,n) { velx=F_U(f,t); vely=F_V(f,t); velz=F_W(f,t); vel[1]=velx; vel[2]=vely; vel[3]=velz; qm=velx*denmix*3.1416*0.0045*0.0045; F_AREA(A,f,t); velm=NV_MAG(vel); Am=NV_MAG(A); cos=NV_DOT(vel,A)/velm/Am; alpha=asin(cos)*180/3.1416; w=7/3.1416*qm/denmix*pow((velm/c),2.5)*sin(2*alpha)*sin(alpha); C_UDMI(c0,t,0)=w; fprintf("out_w:%g\n",w); } } end_c_loop(c0,t) } }

 September 9, 2019, 15:41 #2 Senior Member   Join Date: Nov 2013 Posts: 1,781 Rep Power: 22 Code: ```#include "udf.h" #include "math.h" #include "metric.h" #include "mem.h" DEFINE_ON_DEMAND(on_demand_w) { real w; int n; real densic=3200; real c; real alpha; real A[ND_ND]; real vel[3]; real velm; real Am; real cos; real velx; real vely; real velz; real qm; Domain *d; Thread *t; cell_t c0; face_t f; d=Get_Domain(1); c=sqrt(3*480*1000000/densic); thread_loop_c(t,d) { c0=F_C0(f,t); .. (rest of code removed because the first problem is here)``` You ask Fluent to use f, but you never tell Fluent what f is. Yes, you declare it as a face, but Fluent has no idea which face you mean. I cannot really figure out what you are trying to do, but probably you forgot to loop over all faces or something like that. Your compiler should have complained that something is wrong in this line. Always check compiler errors, especially if you have a problem with compiling!

September 9, 2019, 15:47
#3
Senior Member

Join Date: Nov 2013
Posts: 1,781
Rep Power: 22
And some suggestions to improve your code:

Quote:
 Originally Posted by Jonathon_L real denmix=C_R(c0,t);
You should have declared this variable higher up, at least in the default dialect of c that Fluent uses.

Quote:
 Originally Posted by Jonathon_L velx=F_U(f,t); vely=F_V(f,t); velz=F_W(f,t); vel[1]=velx; vel[2]=vely; vel[3]=velz;
This is not very useful, just simplify it to:
Code:
```	        vel[1]=F_U(f,t);
vel[2]=F_V(f,t);
vel[3]=F_W(f,t);```
Quote:
 Originally Posted by Jonathon_L alpha=asin(cos)*180/3.1416; w=7/3.1416*qm/denmix*pow((velm/c),2.5)*sin(2*alpha)*sin(alpha);
Your alpha is in degrees, but sin expects an angle in radians. Don't convert, just keep it as it is:
Code:
```                alpha=asin(cos);
w=7/3.1416*qm/denmix*pow((velm/c),2.5)*sin(2*alpha)*sin(alpha);```

September 9, 2019, 21:09
#4
New Member

Join Date: Aug 2019
Posts: 4
Rep Power: 3
Quote:
 You ask Fluent to use f, but you never tell Fluent what f is. Yes, you declare it as a face, but Fluent has no idea which face you mean. I cannot really figure out what you are trying to do, but probably you forgot to loop over all faces or something like that. Your compiler should have complained that something is wrong in this line. Always check compiler errors, especially if you have a problem with compiling!

 Tags udf code