CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > ANSYS > FLUENT > Fluent UDF and Scheme Programming

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

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

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   April 11, 2019, 06:33
Default Viscosity UDF works when interpreted, Doesn't when compiled?
  #1
Member
 
Sebi
Join Date: Mar 2019
Posts: 49
Rep Power: 7
bloodflow is on a distinguished road
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)
}
bloodflow is offline   Reply With Quote

Old   April 11, 2019, 08:05
Default
  #2
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
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 <math.h>

Please let me know if it works, because I'm not 100% sure if this is the reason.
pakk is offline   Reply With Quote

Old   April 11, 2019, 08:17
Default
  #3
Member
 
Sebi
Join Date: Mar 2019
Posts: 49
Rep Power: 7
bloodflow is on a distinguished road
Quote:
Originally Posted by pakk View Post
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 <math.h>

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.
bloodflow is offline   Reply With Quote

Old   April 11, 2019, 09:01
Default
  #4
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
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.)
pakk is offline   Reply With Quote

Old   April 11, 2019, 09:06
Default
  #5
Member
 
Sebi
Join Date: Mar 2019
Posts: 49
Rep Power: 7
bloodflow is on a distinguished road
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........
bloodflow is offline   Reply With Quote

Reply

Tags
compile, udf

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Viscosity UDF Gives different Results to fluent Model? bloodflow Fluent UDF and Scheme Programming 1 March 28, 2019 20:11
Problem with divergence TDK FLUENT 13 December 14, 2018 06:00
WILLING TO PAY/ FREELANCER REQUIRED / small UDF coding force loads over body / 6DOF acasas CFD Freelancers 1 January 23, 2015 07:26
I want to use pressure of 1 face in udf that compiled on other face iman_1844 Fluent UDF and Scheme Programming 3 June 10, 2010 12:55
error of Interpreted udf Zhang Junmei FLUENT 6 December 12, 2001 08:03


All times are GMT -4. The time now is 20:44.