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) } } |
Can someone please help. :(
|
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).
|
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... :)
|
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. |
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) |
Thanks
Quote:
Regards |
Accessing information on the same thread
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) } |
Try wrapping the "F_T" macro with thread storage checking
Code:
begin_f_loop(f,thread) |
Quote:
|
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. |
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. |
Does it go inside if you explicitly set the values to 'good values'? Such as:
Code:
VOFWater=0.5; |
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!? |
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. |
Hi.
your if clause is wrong. you should write that if clause like this: else if (100.0 < t && t <= 180.0 ) good luke |
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 |
No you did not do right, because you still don't follow Mostafa's correct advise...
|
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) } |
please guide me
thanks |
All times are GMT -4. The time now is 02:48. |