|
[Sponsors] |
May 24, 2013, 00:58 |
Unable to use if statement in UDF
|
#1 |
Member
Anonymous
Join Date: Apr 2013
Posts: 34
Rep Power: 13 |
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) } } |
|
May 24, 2013, 11:07 |
|
#2 |
Member
Anonymous
Join Date: Apr 2013
Posts: 34
Rep Power: 13 |
Can someone please help.
|
|
May 27, 2013, 03:04 |
|
#3 |
Senior Member
|
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).
|
|
May 27, 2013, 10:57 |
|
#4 |
Member
Anonymous
Join Date: Apr 2013
Posts: 34
Rep Power: 13 |
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...
|
|
July 22, 2013, 10:25 |
if statement not executing properly
|
#5 |
Member
Join Date: Sep 2011
Posts: 39
Rep Power: 15 |
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. |
|
July 22, 2013, 22:02 |
|
#6 |
Senior Member
|
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) |
|
July 23, 2013, 02:58 |
Thanks
|
#7 | |
Member
Join Date: Sep 2011
Posts: 39
Rep Power: 15 |
Quote:
Regards |
||
July 23, 2013, 08:28 |
Accessing information on the same thread
|
#8 | |
Member
Join Date: Sep 2011
Posts: 39
Rep Power: 15 |
Quote:
/* 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) } |
||
July 23, 2013, 21:18 |
|
#9 |
Senior Member
|
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) |
|
July 25, 2013, 04:24 |
|
#10 | |
Member
Join Date: Sep 2011
Posts: 39
Rep Power: 15 |
Quote:
|
||
July 26, 2013, 22:20 |
|
#11 |
Senior Member
|
The line
Code:
if (NULL != THREAD_STORAGE(t,SV_T) 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. |
|
April 27, 2015, 12:30 |
|
#12 | |
New Member
mostafa
Join Date: Jun 2013
Posts: 22
Rep Power: 13 |
Quote:
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. |
||
April 28, 2015, 07:36 |
|
#13 |
Senior Member
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27 |
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); } } |
|
April 28, 2015, 15:55 |
|
#14 | |
New Member
mostafa
Join Date: Jun 2013
Posts: 22
Rep Power: 13 |
Quote:
#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!? |
||
June 5, 2016, 22:02 |
nested if statement in udf
|
#15 |
New Member
mm
Join Date: May 2016
Posts: 24
Rep Power: 9 |
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. |
|
June 6, 2016, 00:05 |
|
#16 |
New Member
mostafa
Join Date: Jun 2013
Posts: 22
Rep Power: 13 |
Hi.
your if clause is wrong. you should write that if clause like this: else if (100.0 < t && t <= 180.0 ) good luke |
|
June 6, 2016, 05:28 |
|
#17 | |
New Member
mm
Join Date: May 2016
Posts: 24
Rep Power: 9 |
Quote:
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 |
||
June 6, 2016, 06:38 |
|
#18 |
Senior Member
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27 |
No you did not do right, because you still don't follow Mostafa's correct advise...
|
|
June 6, 2016, 06:59 |
|
#19 | |
New Member
mm
Join Date: May 2016
Posts: 24
Rep Power: 9 |
Quote:
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) } |
||
June 6, 2016, 07:17 |
|
#20 |
New Member
mm
Join Date: May 2016
Posts: 24
Rep Power: 9 |
please guide me
thanks |
|
|
|
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 |