CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   FLUENT (https://www.cfd-online.com/Forums/fluent/)
-   -   0 or 0.0 in udf (https://www.cfd-online.com/Forums/fluent/90003-0-0-0-udf.html)

wlt_1985 June 28, 2011 09:42

0 or 0.0 in udf
 
I would like to write an udf and i referred few of the samples. I confused with the value of "0 and 0.0". Can pls help in making them clear?What are the differences between them?Pls help.

Best regards

Micael June 29, 2011 11:34

It is not about FLUENT nor much about zero, but about the C language. Lets see an example:

Code:


real c;
c = 1/2;

In that case, value of c is zero. Why? Because 1 and 2 are integers. The arithmetic on two integers produce another integer (the integer part of 1/2 is zero). Of course, since c is real, the result will be promoted to real, but that does not recover the lost precision.

In any case, you should be aware of that rule. One remedy is to simply add ".0" after at least one number, so that arithmetic will be done on real instead of integer (any integer in the arithmetic will be promoted to real before the calculation):

Code:


real c;
c = 1/2.0;


wlt_1985 June 29, 2011 12:39

Quote:

Originally Posted by Micael (Post 314089)
It is not about FLUENT nor much about zero, but about the C language. Lets see an example:

Code:


real c;
c = 1/2;

In that case, value of c is zero. Why? Because 1 and 2 are integers. The arithmetic on two integers produce another integer (the integer part of 1/2 is zero). Of course, since c is real, the result will be promoted to real, but that does not recover the lost precision.

In any case, you should be aware of that rule. One remedy is to simply add ".0" after at least one number, so that arithmetic will be done on real instead of integer (any integer in the arithmetic will be promoted to real before the calculation):

Code:


real c;
c = 1/2.0;



Thanks Micael. You totally help me a lot. May I have one more question?
Is the result of c=1.0/2.0 or c=1.00/2.000 similar to c=1/2.0?
I wrote udf and not sure whether was it correct or not. Can pls help to a look on it? Thanks a lot.

DEFINE_PROPERTY(acetic_acid_density, c, t)
{
real C; /* self-defined variable for convenience */
real D; /* self-defined variable for convenience */
real rho; /* calculated density in unit kg/m3 */
real T = C_T(c,t); /* temperature in unit kelvin, K */
const real A = 0.35182; /* dimensionless regression coeficient */
const real B = 0.26954; /* dimensionless regression coeficient */
const real n = 0.26843; /* dimensionless regression coeficient */
const real Tc = 592.71; /* critical temperature in unit kelvin, K */
C = 1-T/Tc; /* self-defined variable for convenience */
D = pow(C, n); /* self-defined variable for convenience */
rho = A*(pow(B, -D))*1000; /* calculated density in unit kg/m3 */
return rho;
}

DEFINE_PROPERTY(acetic_acid_viscosity, c, t)
{
real E; /* self-defined variable for convenience */
real mu; /* calculated viscosity in unit kg/m.s */
real T = C_T(c,t); /* temperature in unit kelvin, K */
const real A = -3.8937; /* dimensionless regression coeficient */
const real B = 784.82; /* dimensionless regression coeficient */
const real C = 0.006665; /* dimensionless regression coeficient */
const real D = -0.0000075606; /* dimensionless regression coeficient */
E = A + B/T + C*T + D*T*T; /* self-defined variable for convenience */
mu = 0.001*pow(10, E); /* calculated viscosity in unit kg/m.s */
return mu; /* calculated viscosity in unit kg/m.s */
}

DEFINE_PROPERTY(acetic_acid_thermal_conductivity, c, t)
{
real C; /* self-defined variable for convenience */
real D; /* self-defined variable for convenience */
real E; /* self-defined variable for convenience */
real ktc; /* calculated thermal conductivity in unit W/m.K */
real T = C_T(c,t); /* temperature in unit kelvin, K */
const real A = -1.2836; /* dimensionless regression coeficient */
const real B = 0.5893; /* dimensionless regression coeficient */
const real Tc = 592.71; /* critical temperature in unit kelvin, K*/
const real n = 2/7; /* dimensionless regression coeficient */
C = 1-T/Tc; /* self-defined variable for convenience */
D = pow(C, n); /* self-defined variable for convenience */
E = A + B*D; /* self-defined variable for convenience */
ktc = pow(10, E); /* calculated thermal conductivity in unit W/m.K */
return ktc;
}

Thanks to have a look. Your opinion will be appreciated.

Best regards

Micael June 29, 2011 13:45

Quote:

Originally Posted by wlt_1985 (Post 314100)
Is the result of c=1.0/2.0 or c=1.00/2.000 similar to c=1/2.0?

Yes, all the same. It can be put more compact like this:
Code:


c=1./2;

Following this, there is an obvious problem here:
Code:


const real n = 2/7; /* dimensionless regression coeficient */

n is clearly zero.

Otherwise your code looks good.

wlt_1985 June 29, 2011 15:03

Quote:

Originally Posted by Micael (Post 314108)
Yes, all the same. It can be put more compact like this:
Code:


c=1./2;

Following this, there is an obvious problem here:
Code:


const real n = 2/7; /* dimensionless regression coeficient */

n is clearly zero.

Otherwise your code looks good.

Thanks Micael. Sorry to trouble you again.
What are the differences between real and const real?Can I just put
real n=2./7, instead of
const real n=2./7?

Best regards

Micael June 29, 2011 16:27

const means it is not modifiable after declaration. It does not really add something usefull in the present situation.


All times are GMT -4. The time now is 21:32.