CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Fluent UDF and Scheme Programming (https://www.cfd-online.com/Forums/fluent-udf/)
-   -   What's wrong with my UDF? (https://www.cfd-online.com/Forums/fluent-udf/220468-whats-wrong-my-udf.html)

Jonathon_L September 8, 2019 23:39

What's wrong with my UDF?
 
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)
}
}

pakk September 9, 2019 16:41

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!

pakk September 9, 2019 16:47

And some suggestions to improve your code:

Quote:

Originally Posted by Jonathon_L (Post 744188)
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 (Post 744188)
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 (Post 744188)
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);


Jonathon_L September 9, 2019 22:09

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!
Thank you for your reply, and I'll try.


All times are GMT -4. The time now is 12:30.