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

Unable to use if statement in UDF

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   May 24, 2013, 00:58
Default Unable to use if statement in UDF
  #1
AGP
Member
 
Anonymous
Join Date: Apr 2013
Posts: 34
Rep Power: 13
AGP is on a distinguished road
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 is offline   Reply With Quote

Old   May 24, 2013, 11:07
Default
  #2
AGP
Member
 
Anonymous
Join Date: Apr 2013
Posts: 34
Rep Power: 13
AGP is on a distinguished road
Can someone please help.
AGP is offline   Reply With Quote

Old   May 27, 2013, 03:04
Default
  #3
Senior Member
 
sbaffini's Avatar
 
Paolo Lampitella
Join Date: Mar 2009
Location: Italy
Posts: 2,192
Blog Entries: 29
Rep Power: 39
sbaffini will become famous soon enoughsbaffini will become famous soon enough
Send a message via Skype™ to sbaffini
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).
sbaffini is offline   Reply With Quote

Old   May 27, 2013, 10:57
Default
  #4
AGP
Member
 
Anonymous
Join Date: Apr 2013
Posts: 34
Rep Power: 13
AGP is on a distinguished road
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...
AGP is offline   Reply With Quote

Old   July 22, 2013, 10:25
Default if statement not executing properly
  #5
Member
 
Join Date: Sep 2011
Posts: 39
Rep Power: 15
Kamu is on a distinguished road
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.
Kamu is offline   Reply With Quote

Old   July 22, 2013, 22:02
Default
  #6
Senior Member
 
Join Date: Aug 2011
Posts: 421
Blog Entries: 1
Rep Power: 22
blackmask will become famous soon enough
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.
blackmask is offline   Reply With Quote

Old   July 23, 2013, 02:58
Default Thanks
  #7
Member
 
Join Date: Sep 2011
Posts: 39
Rep Power: 15
Kamu is on a distinguished road
Quote:
Originally Posted by blackmask View Post
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 is offline   Reply With Quote

Old   July 23, 2013, 08:28
Default Accessing information on the same thread
  #8
Member
 
Join Date: Sep 2011
Posts: 39
Rep Power: 15
Kamu is on a distinguished road
Quote:
Originally Posted by blackmask View Post
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)
}
Kamu is offline   Reply With Quote

Old   July 23, 2013, 21:18
Default
  #9
Senior Member
 
Join Date: Aug 2011
Posts: 421
Blog Entries: 1
Rep Power: 22
blackmask will become famous soon enough
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)
blackmask is offline   Reply With Quote

Old   July 25, 2013, 04:24
Default
  #10
Member
 
Join Date: Sep 2011
Posts: 39
Rep Power: 15
Kamu is on a distinguished road
Quote:
Originally Posted by blackmask View Post
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
Kamu is offline   Reply With Quote

Old   July 26, 2013, 22:20
Default
  #11
Senior Member
 
Join Date: Aug 2011
Posts: 421
Blog Entries: 1
Rep Power: 22
blackmask will become famous soon enough
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.
Kamu likes this.
blackmask is offline   Reply With Quote

Old   April 27, 2015, 12:30
Default
  #12
New Member
 
mostafa
Join Date: Jun 2013
Posts: 22
Rep Power: 13
mostafa_zeynalabedini is on a distinguished road
Quote:
Originally Posted by blackmask View Post
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.
mostafa_zeynalabedini is offline   Reply With Quote

Old   April 28, 2015, 07:36
Default
  #13
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
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 ).
pakk is offline   Reply With Quote

Old   April 28, 2015, 15:55
Default
  #14
New Member
 
mostafa
Join Date: Jun 2013
Posts: 22
Rep Power: 13
mostafa_zeynalabedini is on a distinguished road
Quote:
Originally Posted by pakk View Post
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!?
mostafa_zeynalabedini is offline   Reply With Quote

Old   June 5, 2016, 22:02
Default nested if statement in udf
  #15
New Member
 
mm
Join Date: May 2016
Posts: 24
Rep Power: 9
mmunige is an unknown quantity at this point
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.
mmunige is offline   Reply With Quote

Old   June 6, 2016, 00:05
Default
  #16
New Member
 
mostafa
Join Date: Jun 2013
Posts: 22
Rep Power: 13
mostafa_zeynalabedini is on a distinguished road
Hi.
your if clause is wrong. you should write that if clause like this:
else if (100.0 < t && t <= 180.0 )
good luke
mostafa_zeynalabedini is offline   Reply With Quote

Old   June 6, 2016, 05:28
Default
  #17
New Member
 
mm
Join Date: May 2016
Posts: 24
Rep Power: 9
mmunige is an unknown quantity at this point
Quote:
Originally Posted by mostafa_zeynalabedini View Post
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
mmunige is offline   Reply With Quote

Old   June 6, 2016, 06:38
Default
  #18
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
No you did not do right, because you still don't follow Mostafa's correct advise...
pakk is offline   Reply With Quote

Old   June 6, 2016, 06:59
Default
  #19
New Member
 
mm
Join Date: May 2016
Posts: 24
Rep Power: 9
mmunige is an unknown quantity at this point
Quote:
Originally Posted by pakk View Post
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 is offline   Reply With Quote

Old   June 6, 2016, 07:17
Default
  #20
New Member
 
mm
Join Date: May 2016
Posts: 24
Rep Power: 9
mmunige is an unknown quantity at this point
please guide me
thanks
mmunige is offline   Reply With Quote

Reply


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
Dynamic Mesh UDF Qureshi FLUENT 7 March 23, 2017 08:37
USING IF statement in a UDF for defining Velocity profile at Inlet amir2920 Fluent UDF and Scheme Programming 3 May 24, 2013 00:46
UDF parallel error: chip-exec: function not found????? shankara.2 Fluent UDF and Scheme Programming 1 January 16, 2012 23:14
[Commercial meshers] ST_Malloc: out of memory.malloc_storage: unable to malloc Velocity SA, cfdproject OpenFOAM Meshing & Mesh Conversion 0 April 14, 2009 16:45
UDF "AND" Statement Curtis FLUENT 1 December 11, 2002 22:34


All times are GMT -4. The time now is 14:10.