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/)
-   -   Unable to use if statement in UDF (https://www.cfd-online.com/Forums/fluent-udf/118276-unable-use-if-statement-udf.html)

AGP May 23, 2013 23:58

Unable to use if statement in UDF
 
Hello everyone,

I am trying to simulate a pulsatile flow loop using UDF based on flowrate vs. time data. But there seems to be some problem because fluent doesn't understand the if statement. Can someone please tell how to fix this. The code is really simple and I fail to see what could be wrong with it. Thanks in advance.

DEFINE_PROFILE(Pulsatileinlet, thread, index)
{

face_t f;
real timet = RP_Get_Real("flow-time");
real flowt,flowtf;
float time[81];
float flow[81];
int i;
FILE *fptime;
FILE *fpflow;
fpflow = fopen("Flowrate.txt","r");

if (fpflow ==NULL)
{
printf("Error: Unable to open file");
}
fptime = fopen("Time.txt","r");

if (fptime ==NULL)
{
printf("Error: Unable to open file");
}

for (i=0;i<81;i++)
{
fscanf (fptime,"%f\n",&time[i]);
fscanf (fpflow,"%f\n",&flow[i]);

//printf ("time = %f,flow rate = %f \n",time[i],flow[i]);

}

for (i=0;i<81;i++)
{
if (timet == time[i])
{
flowt = flow[i];

begin_f_loop(f,thread)
{
F_PROFILE(f,thread,index) = flowt;
}
end_f_loop(f,thread)

}

}

AGP May 24, 2013 10:07

Can someone please help. :(

sbaffini May 27, 2013 02:04

The if clause used by you is correct. There must be some other problem. The usual route to debug is to cut away all the superflous part and try compiling by adding a small piece at the time. At some point you will find what is wrong. If you can past here the smallest piece of code which doesn't work it could be easier to help (e.g., remove the file reading part at first).

AGP May 27, 2013 09:57

thanks paolo!! The problem was different data types. One was float and one was real. When I made all the data types real, the if loop started working! After 5 hrs of staring at the code... :)

Kamu July 22, 2013 09:25

if statement not executing properly
 
I think there is a problem with the if statement in FLUENT.
/* This udf specifies a non-uniform heat flux on the circumference of the absorber tube*/
#include "udf.h"

DEFINE_PROFILE(heat_gen, thread, position)
{
real x[ND_ND]; /* this will hold the position vector */
float y;
face_t f;
begin_f_loop(f,thread)
{
F_CENTROID(x, f, thread);
y = x[1];
if (-0.033 < y < -0.01858)
{
F_PROFILE(f, thread, position) = -6.2602e10 - 1.5741e13*y - 1.6338e15*(y*y)-8.9775e16*(y*y*y) -2.7551e18*(y*y*y*y)-4.4779e19*(y*y*y*y*y)-3.0114e20*(y*y*y*y*y);
}
else if (-0.01858 < y < 0.00332)
{
F_PROFILE(f, thread, position) = 7.0952e6 - 3.1159e9*y + 3.9774e11*(y*y) + 1.4705e13*(y*y*y);
}
else if (0.00332 < y < 0.033)
{
F_PROFILE(f, thread, position) = 1.94e06;
}
}
end_f_loop(f, thread)
}

I tried running the above code, but am getting the profile for only the first part. someone help.

blackmask July 22, 2013 21:02

You can not be a mathematician and programmer at the same time.
Mathematically (-0.033 < y < -0.01858) is a valid expression, but in c language it will be interpreted as (-0.033 < y ) < -0.01858, the first part is a boolean expression with a result "true" or "false", and then converted to an integer "0" or "1", which is not smaller than -0.01858 either way.

The correct implementation is
Code:

(-0.033 < y && y < -0.01858)
Other "if"-clauses should be modified accordingly.

Kamu July 23, 2013 01:58

Thanks
 
Quote:

Originally Posted by blackmask (Post 441344)
You can not be a mathematician and programmer at the same time.
Mathematically (-0.033 < y < -0.01858) is a valid expression, but in c language it will be interpreted as (-0.033 < y ) < -0.01858, the first part is a boolean expression with a result "true" or "false", and then converted to an integer "0" or "1", which is not smaller than -0.01858 either way.

The correct implementation is
Code:

(-0.033 < y && y < -0.01858)
Other "if"-clauses should be modified accordingly.

Thank you sir, indeed am not very good at programming. It is now working perfectly well with your suggestion. Thanks for making my week.

Regards

Kamu July 23, 2013 07:28

Accessing information on the same thread
 
Quote:

Originally Posted by blackmask (Post 441344)
You can not be a mathematician and programmer at the same time.
Mathematically (-0.033 < y < -0.01858) is a valid expression, but in c language it will be interpreted as (-0.033 < y ) < -0.01858, the first part is a boolean expression with a result "true" or "false", and then converted to an integer "0" or "1", which is not smaller than -0.01858 either way.

The correct implementation is
Code:

(-0.033 < y && y < -0.01858)
Other "if"-clauses should be modified accordingly.

I would like to access information on the same boundary. That is specify a non-uniform heat flux and a temperature dependent emissivity. This is the udf i wrote. After interpreting it, on trying to solve, I get the error access violation. Does it mean I can not specify two udfs on the same boundary??

/* This udf specifies a non-uniform heat flux on the circumference of the absorber tube*/

#include "udf.h"

DEFINE_PROFILE(wh_gene, thread, position)
{
real x[ND_ND]; /* this will hold the position vector */
real y,h;
face_t f;
begin_f_loop(f,thread)
{
F_CENTROID(x, f, thread);
y = x[1];
if (-0.035 < y && y < 0.035)
{
h = 7.6125e6 - 2.4124e9*y + 3.3632e11*(y*y) - 1.052e13*(y*y*y) - 8.7708e14*(y*y*y*y) + 4.275e16*(y*y*y*y*y) + 9.3265e17*(y*y*y*y*y*y) - 5.2063e19*(y*y*y*y*y*y*y)-3.6018e20*(y*y*y*y*y*y*y*y)+2.0991e22*(y*y*y*y*y*y *y*y*y);
F_PROFILE(f, thread, position) = h;
}
else
{
F_PROFILE(f,thread,position) = 0;
}
}
end_f_loop(f, thread)
}
/*Profile for emissivity*/
DEFINE_PROFILE(emmisive,thread,position)
{
face_t f;
real x[ND_ND]; This will hold position vector
real z;

begin_f_loop(f,thread)
{
z = F_T(f,thread);
F_PROFILE(f,thread,position) = 0.00031*z-0.0216;
}
end_f_loop(f,thread)
}

blackmask July 23, 2013 20:18

Try wrapping the "F_T" macro with thread storage checking
Code:

begin_f_loop(f,thread)
    {
  if (NULL != THREAD_STORAGE(t,SV_T))
    z = F_T(f,thread); 
  else
    z = 300.; // whatever value you feel appropriate
  F_PROFILE(f,thread,position) = 0.00031*z-0.0216;
    }
  end_f_loop(f,thread)


Kamu July 25, 2013 03:24

Quote:

Originally Posted by blackmask (Post 441592)
Try wrapping the "F_T" macro with thread storage checking
Code:

begin_f_loop(f,thread)
    {
  if (NULL != THREAD_STORAGE(t,SV_T))
    z = F_T(f,thread); 
  else
    z = 300.; // whatever value you feel appropriate
  F_PROFILE(f,thread,position) = 0.00031*z-0.0216;
    }
  end_f_loop(f,thread)


Thanks sir, the code now works perfectly well. Can you kindly tell me what the added statement means ( if (NULL != THREAD_STORAGE(t,SV_T)). Thanks once again

blackmask July 26, 2013 21:20

The line
Code:

if (NULL != THREAD_STORAGE(t,SV_T)
checks whether the pointer pointing to the storage of T is NULL.

If the the storage has not been allocated yet, i.e., pointer is NULL, you will receive the segmentation fault if you try to dereference the pointer via F_T, C_T macro.

mostafa_zeynalabedini April 27, 2015 11:30

Quote:

Originally Posted by blackmask (Post 441344)
You can not be a mathematician and programmer at the same time.
Mathematically (-0.033 < y < -0.01858) is a valid expression, but in c language it will be interpreted as (-0.033 < y ) < -0.01858, the first part is a boolean expression with a result "true" or "false", and then converted to an integer "0" or "1", which is not smaller than -0.01858 either way.

The correct implementation is
Code:

(-0.033 < y && y < -0.01858)
Other "if"-clauses should be modified accordingly.

i sir,
I have a problem with if clause in my udf. would you help me please?
I want to assess the interface in multiphase flow, and so I used 4 for statements. I also checked the udf that is go inside all 4 if clauses or not by aa flag. But it never goes inside them. I used so many composition but none of the works.
my udf is:

#include "udf.h"
#include "sg_pb.h"
#include "sg_mphase.h"
DEFINE_PB_NUCLEATION_RATE(nuc, cell, thread)
{
real Epsilon_Entrainment=10, VOFWater=0, VOFAir=0;
real flag=0;
Thread *mixture_thread = THREAD_SUPER_THREAD(thread);
Thread *ContinuousPhaseThread = THREAD_SUB_THREAD(mixture_thread,0);
Thread *LargeBubblesThread = THREAD_SUB_THREAD(mixture_thread,1);
C_UDMI(cell,mixture_thread,0)=0;
VOFWater=C_VOF(cell,ContinuousPhaseThread);
VOFAir=C_VOF(cell,LargeBubblesThread);
if (0.05 < VOFWater)
if ( VOFWater < 0.95 )
if ( 0.05 < VOFAir)
if ( VOFAir< 0.95 )
{
flag=1;
C_UDMI(cell,mixture_thread,0)=flag;
return (Epsilon_Entrainment);
}
}

I also checked this:
if ( 0.05 < VOFWater && VOFWater < 0.95 && 0.05 < VOFAir && VOFAir< 0.95)
{
flag=1;
C_UDMI(cell,mixture_thread,0)=flag;
return (Epsilon_Entrainment);
}
but it also doesn’t works.thanks in advanced.

pakk April 28, 2015 06:36

Does it go inside if you explicitly set the values to 'good values'? Such as:
Code:

VOFWater=0.5;
VOFAir=0.5;
if (0.05 < VOFWater)
if ( VOFWater < 0.95 )
if ( 0.05 < VOFAir)
if ( VOFAir< 0.95 )
{
flag=1;
C_UDMI(cell,mixture_thread,0)=flag;
return (Epsilon_Entrainment);
}
}

If it does go inside now, your problem is not in the if-statements. Maybe the UDF works well, and you simply don't have cells where (0.05 < VOFWater < 0.95 ) and ( 0.05 < VOFAir < 0.95 ).

mostafa_zeynalabedini April 28, 2015 14:55

Quote:

Originally Posted by pakk (Post 544102)
Does it go inside if you explicitly set the values to 'good values'? Such as:
Code:

VOFWater=0.5;
VOFAir=0.5;
if (0.05 < VOFWater)
if ( VOFWater < 0.95 )
if ( 0.05 < VOFAir)
if ( VOFAir< 0.95 )
{
flag=1;
C_UDMI(cell,mixture_thread,0)=flag;
return (Epsilon_Entrainment);
}
}

If it does go inside now, your problem is not in the if-statements. Maybe the UDF works well, and you simply don't have cells where (0.05 < VOFWater < 0.95 ) and ( 0.05 < VOFAir < 0.95 ).

thank you sir, you are right. I should check the other parts of udf and the if clauses are correct. may I also ask you about another problem in my udf? consider that I am doing my run in parallel mode. I want to find the thread of each phase and do something on each cell of each phase. Is this UDF correct?
#include "udf.h"
#include "sg_pb.h"
#include "sg_mphase.h"
DEFINE_PB_NUCLEATION_RATE(nuc, cell, thread)
{
real Epsilon_Entrainment=0, VOFWater = 0, VOFAir = 0;
real flag=0;
int phase1_domain_index = 0;
int phase2_domain_index = 1;
Thread *mixture_thread = THREAD_SUPER_THREAD(thread);
Thread *ContinuousPhaseThread = THREAD_SUB_THREAD(mixture_thread,phase1_domain_ind ex);
Thread *LargeBubblesThread = THREAD_SUB_THREAD(mixture_thread,phase2_domain_ind ex);
VOFWater = C_VOF(cell,ContinuousPhaseThread);
VOFAir = C_VOF(cell,LargeBubblesThread);
C_UDMI(cell,mixture_thread,0) = 0;
if (0.05<VOFWater && VOFWater<0.95 && 0.05<VOFAir && VOFAir<0.95)
{
flag=5;
C_UDMI(cell,mixture_thread,0)=flag;
C_UDMI(cell,mixture_thread,1)=C_VOLUME(cell,Contin uousPhaseThread);
Epsilon_Entrainment = 5e+08
}
return (Epsilon_Entrainment);
}

the cell volume that I take from this udf is completely different from real cell volume!!!
I think the threads are not correct for parallel mode!?

mmunige June 5, 2016 21:02

nested if statement in udf
 
Respected members

I am using udf for defining transient temperature at inlet boundary of my model in fluent. I am writing nested if statement, but model keeps on using the same equation after 180 seconds, and does not move to the next if else statement. My udf is as follows:

#include"udf.h"

DEFINE_PROFILE(inlet_temperature,thread,position )
{

face_t f;
begin_f_loop(f,thread)

{

real t = RP_Get_Real("flow-time");

if ( t <= 100.0 )

F_PROFILE(f,thread,position) = 379.48 + 0.0004*t;

else if (100.0 < t <= 180.0 )

F_PROFILE(f,thread,position) = 1.0624*t + 352.0;

else if (180.0 < t <= 200.0 )

F_PROFILE(f,thread,position) = 0.2716*t + 494.4;

else if (200.0 < t <= 400.0 )
F_PROFILE(f,thread,position) = 328.14*pow(t,0.097);
else
F_PROFILE(f,thread,position) = 727.82;

}
end_f_loop(f,thread)
}

sorry for my poor knowledge of programming.
please help me on this error.
Thanks.

mostafa_zeynalabedini June 5, 2016 23:05

Hi.
your if clause is wrong. you should write that if clause like this:
else if (100.0 < t && t <= 180.0 )
good luke

mmunige June 6, 2016 04:28

Quote:

Originally Posted by mostafa_zeynalabedini (Post 603478)
Hi.
your if clause is wrong. you should write that if clause like this:
else if (100.0 < t && t <= 180.0 )
good luke




Thanks for ur reply. But i noticed another error that i am not enclosing each statement with {}, i m writing now like this :
if ( t <= 100.0 )
{
F_PROFILE(f,thread,position) = 379.48 + 0.0004*t;

}
else if (100.0 < t <= 180.0 )
{
F_PROFILE(f,thread,position) = 1.0624*t + 352.0;
}

i m not sure still i am doing right or not..please guide further.
regards

pakk June 6, 2016 05:38

No you did not do right, because you still don't follow Mostafa's correct advise...

mmunige June 6, 2016 05:59

Quote:

Originally Posted by pakk (Post 603517)
No you did not do right, because you still don't follow Mostafa's correct advise...

here is my final udf for inlet and outlet temperatures.
please guide me if any error.

#include"udf.h"

DEFINE_PROFILE(inlet_temperature,thread,position )

{

face_t f;

begin_f_loop(f,thread)

{

real t = RP_Get_Real("flow-time");

if ( t <= 100.0 )

{

F_PROFILE(f,thread,position) = 379.48 + 0.0004*t;

}

else if (100.0 < t && t <= 180.0 )

{

F_PROFILE(f,thread,position) = 1.0624*t + 352.0;

}

else if (180.0 < t && t <= 200.0 )

{

F_PROFILE(f,thread,position) = 0.2716*t + 494.4;

}

else if (200.0 < t && t <= 400.0 )

{

F_PROFILE(f,thread,position) = 328.14*pow(t,0.097);

}
else if (400.0 < t && t <= 600.0 )

{

F_PROFILE(f,thread,position) = 315.73*pow(t,0.1035);

}

else if (600.0 < t && t <= 800.0 )

{

F_PROFILE(f,thread,position) = 317.74*pow(t,0.1026);

}

else if (800.0 < t && t <= 1000.0 )

{

F_PROFILE(f,thread,position) = 311.12*pow(t,0.1058);

}

else if (1000.0 < t && t <= 1300.0 )

{

F_PROFILE(f,thread,position) = 309.21*pow(t,0.1067);

}

else if (1300.0 < t && t <= 1600.0 )

{

F_PROFILE(f,thread,position) = 0.0524*t + 596.97;

}

else if (1600.0 < t && t <= 1900.0 )

{

F_PROFILE(f,thread,position) = 301.12*pow(t,0.1106);

}

else if (1900.0 < t && t <= 2200.0 )

{

F_PROFILE(f,thread,position) = 0.0397*t + 618.6;

else if (2200.0 < t && t <= 2600.0 )

{

F_PROFILE(f,thread,position) = 341.25*pow(t,0.0945);

}

else if (2600.0 < t && t <= 3000.0 )

{

F_PROFILE(f,thread,position) = 0.0177*t + 671.54;

}

else if (3000.0 < t && t <= 3400.0 )

{

F_PROFILE(f,thread,position) = 580.75*pow(t,0.0277);

}

else

{

F_PROFILE(f,thread,position) = 727.82;

}

}



end_f_loop(f,thread)

}

DEFINE_PROFILE(outlet_temperature,thread,position )

{

face_t f;



begin_f_loop(f,thread)

{

real t = RP_Get_Real("flow-time");

F_PROFILE(f,thread,position) = 277.0 + 0.0114*t;

}

end_f_loop(f,thread)

}

mmunige June 6, 2016 06:17

please guide me
thanks


All times are GMT -4. The time now is 02:48.