# Viscosity UDF works when interpreted, Doesn't when compiled?

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

 April 11, 2019, 06:33 Viscosity UDF works when interpreted, Doesn't when compiled? #1 Member   Sebi Join Date: Mar 2019 Posts: 49 Rep Power: 7 Hi Again, I am learning how to compile UDF's instead of just intrerpreting them. I have successfully compiled a UDF and am using it to define a Transient velocity inlet, and a Viscosity model. The compiled UDF works correctly for the velocity inlet, but the viscosity UDF returns a constant viscosity of mu=0.00476. The same UDF works properly when compiled, is there a difference in how they should be written? The Full UDF is below. /* Carreau-Yasuda Viscosity Model */ #include "udf.h" double muinf=0.00476; double mu0=0.0519; double lambda=0.438; double p=0.409; double n=0.191; DEFINE_PROPERTY(cell_viscosity,c,t) { real diff; real shear; real prod; real power; real cyvisco; diff=(mu0+muinf); prod=((lambda)*(C_STRAIN_RATE_MAG(c,t))); shear=(1+(pow(prod,p))); power=((n-1)/(p)); cyvisco=((muinf)+(diff)*(pow(shear,power))); return cyvisco; } /* 2D Parabolic Transient Velocity Profile For Laminar Flow */ #include "udf.h" float a0 = 0.1564; float a1 = -0.03045; float b1 = 0.007764; float a2 = 0.0242; float b2 = 0.03948; float a3 = -0.01151; float b3 = -0.008163; float a4 = 0.009875; float b4 = 0.003343; float a5 = -0.01003; float b5 = 0.002862; float a6 = 0.006133; float b6 = -0.0007519; float a7 = 0.001413; float b7 = 0.001232; float a8 = -0.003654; float b8 = -0.000137; float w = 7.858; DEFINE_PROFILE(LaminarCoronaryVelocity, thread, position) { real x[ND_ND]; real y; real z; real t = CURRENT_TIME; face_t f; begin_f_loop(f, thread) { F_CENTROID(x,f,thread); y = x[1]; z = x[2]; F_PROFILE(f, thread, position) = (2*(a0 + a1*cos(t*w) + b1*sin(t*w) + a2*cos(2*t*w) + b2*sin(2*t*w) + a3*cos(3*t*w) + b3*sin(3*t*w) + a4*cos(4*t*w) + b4*sin(4*t*w) + a5*cos(5*t*w) + b5*sin(5*t*w) + a6*cos(6*t*w) + b6*sin(6*t*w) + a7*cos(7*t*w) + b7*sin(7*t*w) + a8*cos(8*t*w) + b8*sin(8*t*w))*(1-(y*y+z*z)/(0.00000196))); } end_f_loop(f, thread) }

 April 11, 2019, 08:05 #2 Senior Member   Join Date: Nov 2013 Posts: 1,965 Rep Power: 26 You use pow, which is not a standard c function, but is included in the math library. To include this in a compiled function, use: Code: `#include ` Please let me know if it works, because I'm not 100% sure if this is the reason.

April 11, 2019, 08:17
#3
Member

Sebi
Join Date: Mar 2019
Posts: 49
Rep Power: 7
Quote:
 Originally Posted by pakk You use pow, which is not a standard c function, but is included in the math library. To include this in a compiled function, use: Code: `#include ` Please let me know if it works, because I'm not 100% sure if this is the reason.
I have added that and it seems to make no difference.

If I make the function much simpler and just have visco=0.05*shear, the viscosity does vary, but when I include the other things it seems to mess up.

I am compiling this and running on linux if that makes any difference.

 April 11, 2019, 09:01 #4 Senior Member   Join Date: Nov 2013 Posts: 1,965 Rep Power: 26 OK, that was a tough one... Your problem is that you were using global variables. Don't use them, except when necessary. Here, they are not necessary at all. Your global variable p is used in one of the templates that Fluent uses. Don't ask me which. But I could see that the effect was that in your function cell_viscosity, the value of p was zero. (In unix, in Windows the value was 0.409.) So, don't define your variables globally if you only use them locally: Code: ```/* Carreau-Yasuda Viscosity Model */ #include "udf.h" DEFINE_PROPERTY(cell_viscosity,c,t) { double muinf=0.00476; double mu0=0.0519; double lambda=0.438; double p=0.409; double n=0.191; real diff; real shear; real prod; real power; real cyvisco; diff=(mu0+muinf); prod=((lambda)*(C_STRAIN_RATE_MAG(c,t))); shear=(1+(pow(prod,p))); power=((n-1)/(p)); cyvisco=((muinf)+(diff)*(pow(shear,power))); return cyvisco; } /* 2D Parabolic Transient Velocity Profile For Laminar Flow */ DEFINE_PROFILE(LaminarCoronaryVelocity, thread, position) { float a0 = 0.1564; float a1 = -0.03045; float b1 = 0.007764; float a2 = 0.0242; float b2 = 0.03948; float a3 = -0.01151; float b3 = -0.008163; float a4 = 0.009875; float b4 = 0.003343; float a5 = -0.01003; float b5 = 0.002862; float a6 = 0.006133; float b6 = -0.0007519; float a7 = 0.001413; float b7 = 0.001232; float a8 = -0.003654; float b8 = -0.000137; float w = 7.858; real x[ND_ND]; real y; real z; real t = CURRENT_TIME; face_t f; begin_f_loop(f, thread) { F_CENTROID(x,f,thread); y = x[1]; z = x[2]; F_PROFILE(f, thread, position) = (2*(a0 + a1*cos(t*w) + b1*sin(t*w) + a2*cos(2*t*w) + b2*sin(2*t*w) + a3*cos(3*t*w) + b3*sin(3*t*w) + a4*cos(4*t*w) + b4*sin(4*t*w) + a5*cos(5*t*w) + b5*sin(5*t*w) + a6*cos(6*t*w) + b6*sin(6*t*w) + a7*cos(7*t*w) + b7*sin(7*t*w) + a8*cos(8*t*w) + b8*sin(8*t*w))*(1-(y*y+z*z)/(0.00000196))); } end_f_loop(f, thread) }``` I tried it in one of my cases, and it gave a viscosity that depended on the strain rate. (By the way: you should only include udf.h once.)

 April 11, 2019, 09:06 #5 Member   Sebi Join Date: Mar 2019 Posts: 49 Rep Power: 7 YES You sir are a God amongst men! Thank you so much for going to the trouble of finding that out. Now back to the original problem I was trying to solve in the first place about DPM position macros........

 Tags compile, udf