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

Unable to use if statement in UDF

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

Like Tree1Likes
  • 1 Post By blackmask

Reply
 
LinkBack Thread Tools Display Modes
Old   May 23, 2013, 23:58
Default Unable to use if statement in UDF
  #1
AGP
Member
 
Anonymous
Join Date: Apr 2013
Posts: 34
Rep Power: 4
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, 10:07
Default
  #2
AGP
Member
 
Anonymous
Join Date: Apr 2013
Posts: 34
Rep Power: 4
AGP is on a distinguished road
Can someone please help.
AGP is offline   Reply With Quote

Old   May 27, 2013, 02:04
Default
  #3
Senior Member
 
sbaffini's Avatar
 
Paolo Lampitella
Join Date: Mar 2009
Location: Italy
Posts: 518
Blog Entries: 14
Rep Power: 17
sbaffini will become famous soon enough
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, 09:57
Default
  #4
AGP
Member
 
Anonymous
Join Date: Apr 2013
Posts: 34
Rep Power: 4
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, 09:25
Default if statement not executing properly
  #5
Member
 
Join Date: Sep 2011
Posts: 38
Rep Power: 5
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, 21:02
Default
  #6
Senior Member
 
Join Date: Aug 2011
Posts: 315
Rep Power: 11
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, 01:58
Default Thanks
  #7
Member
 
Join Date: Sep 2011
Posts: 38
Rep Power: 5
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, 07:28
Default Accessing information on the same thread
  #8
Member
 
Join Date: Sep 2011
Posts: 38
Rep Power: 5
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, 20:18
Default
  #9
Senior Member
 
Join Date: Aug 2011
Posts: 315
Rep Power: 11
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, 03:24
Default
  #10
Member
 
Join Date: Sep 2011
Posts: 38
Rep Power: 5
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, 21:20
Default
  #11
Senior Member
 
Join Date: Aug 2011
Posts: 315
Rep Power: 11
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, 11:30
Default
  #12
New Member
 
mostafa
Join Date: Jun 2013
Posts: 20
Rep Power: 4
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, 06:36
Default
  #13
Senior Member
 
Join Date: Nov 2013
Posts: 670
Rep Power: 9
pakk is on a distinguished road
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, 14:55
Default
  #14
New Member
 
mostafa
Join Date: Jun 2013
Posts: 20
Rep Power: 4
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

Reply

Thread Tools
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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
USING IF statement in a UDF for defining Velocity profile at Inlet amir2920 Fluent UDF and Scheme Programming 3 May 23, 2013 23:46
UDF parallel error: chip-exec: function not found????? shankara.2 Fluent UDF and Scheme Programming 1 January 16, 2012 23:14
Dynamic Mesh UDF Qureshi FLUENT 1 December 2, 2009 01:27
Need help with the error cfdproject OpenFOAM Other Meshers: ICEM, Star, Ansys, Pointwise, GridPro, Ansa, ... 0 April 14, 2009 15:45
UDF "AND" Statement Curtis FLUENT 1 December 11, 2002 22:34


All times are GMT -4. The time now is 03:22.