|
[Sponsors] |
How to calculate the number of if conditions executed in a fluent udf? |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
January 8, 2020, 00:32 |
|
#21 |
Senior Member
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34 |
first if all try fixed code
Code:
#include "udf.h" real tavg_abr = 286.5; real UDSavg_abr = 0.007; real sensor_temperature = 315.15; int if_number = 0; /* Obtain the mean temperature at the location of thermocouple */ /* Thermocouple located at coordinates (x,y,z) = (0.0,0.0,1.5) */ DEFINE_EXECUTE_AT_END(tsensor) { real x[ND_ND]; real xc,yc,zc,nt; cell_t c; Domain *d; Thread *t; d = Get_Domain(1); nt=0.0; thread_loop_c(t,d) { begin_c_loop(c,t) { C_CENTROID(x,c,t); xc = x[0]; yc = x[1]; zc = x[2]; if ((xc >= 0.0) && (xc <= 0.35)) { if ((yc >= 0.0) && (yc <= 0.35)) { if ((zc >= 1.5) && (xc <= 1.85)) { sensor_temperature = sensor_temperature + C_T(c,t); /* temperature is obatined at each iteration */ nt=nt+1.0; /* number of iterations */ } } } } end_c_loop(c,t) } sensor_temperature /= nt; /* Avg temperature after each timestep */ Message0 ("xc = %f\n",xc); Message0 ("yc = %f\n",yc); Message0 ("zc = %f\n",zc); Message0 ("nt = %f\n",nt); Message0("sensor_temperature = %f\n",sensor_temperature); } DEFINE_EXECUTE_AT_END(avg_inlet_tempandUDS) { int ID = 8; Domain *d; face_t f; real area[ND_ND]; real a; real area_tot = 18.72; Thread *tf; d = Get_Domain(1); tf = Lookup_Thread(d,ID); begin_f_loop(f,tf) { F_AREA(area,f,tf); a = NV_MAG(area); area_tot += a; tavg_abr += F_T(f,tf)*a; /* inlet face temperature from ID = 8 is taken to compute the avg inlet temp */ UDSavg_abr += F_UDSI(f,tf,0)*a; /* inlet face scalar quantity from ID = 8 is taken to compute the avg inlet scalar quantity */ } end_f_loop(f,tf) #if RP_NODE area_tot = PRF_GRSUM1(area_tot); tavg_abr = PRF_GRSUM1(tavg_abr); UDSavg_abr = PRF_GRSUM1(UDSavg_abr); #endif tavg_abr /= area_tot; UDSavg_abr /= area_tot; Message0("tavg_abr = %f UDSavg_abr = %f area_tot = %f\n",tavg_abr, UDSavg_abr, area_tot); } DEFINE_PROFILE(velocity,t,i) { real velocity; face_t f; if (tavg_abr <= sensor_temperature && UDSavg_abr < 0.03) { velocity = 0.051; if_number++; } else { velocity = 0.0; } begin_f_loop(f,t) { F_PROFILE(f,t,i) = velocity; } end_f_loop(f,t) } DEFINE_ON_DEMAND(get_if_number) { Message0("******** int if_number = %d **************\n",if_number); } you should add to your UDF reading from file or hardcopy these data in UDF explicitly
__________________
best regards ****************************** press LIKE if this message was helpful |
|
January 8, 2020, 04:44 |
|
#22 |
Member
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7 |
Hello Alexander,
Thanks for your suggestions. I compiled and ran the code incorporating your suggestions but the coordinate values are not what is expected (0,0,1.5). Could you please suggest any more corrections? Below is the attached code with the output. Thanks #include "udf.h" real sensor_temperature = 315.15 DEFINE_EXECUTE_AT_END(tsensor) { real x[ND_ND]; real xc,yc,zc,nt; cell_t c; Domain *d; Thread *t; d = Get_Domain(1); nt=0.0; /* Begin loop to determine the temperature at the centroid of cells near the thermocouple */ thread_loop_c(t,d) { begin_c_loop(c,t) { C_CENTROID(x,c,t); xc = x[0]; yc = x[1]; zc = x[2]; if ((xc >= 0.0) && (xc <= 0.35)) { if ((yc >= 0.0) && (yc <= 0.35)) { if ((zc >= 1.5) && (xc <= 1.85)) { sensor_temperature = sensor_temperature + C_T(c,t); nt=nt+1.0; } } } } end_c_loop(c,t) } sensor_temperature /= nt; Message0 ("xc = %f\n",xc); Message0 ("yc = %f\n",yc); Message0 ("zc = %f\n",zc); Message0 ("nt = %f\n",nt); Message0("sensor_temperature = %f\n",sensor_temperature); } Output: xc = -2.738164 yc = 1.462940 zc = 4.978388 nt = 0.000000 sensor_temperature = inf |
|
January 9, 2020, 00:47 |
|
#23 |
Senior Member
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34 |
was
Code:
if ((zc >= 1.5) && (xc <= 1.85)) Code:
if ((zc >= 1.5) && (zc <= 1.85))
__________________
best regards ****************************** press LIKE if this message was helpful |
|
January 9, 2020, 00:59 |
|
#24 |
Member
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7 |
Hello Alexander,
Thanks for pointing the typo error. I compiled the corrected code but am receiving the same output. Below are the attached code and the output.Thanks #include "udf.h" real sensor_temperature = 315.15 DEFINE_EXECUTE_AT_END(tsensor) { real x[ND_ND]; real xc,yc,zc,nt; cell_t c; Domain *d; Thread *t; d = Get_Domain(1); nt=0.0; /* Begin loop to determine the temperature at the centroid of cells near the thermocouple */ thread_loop_c(t,d) { begin_c_loop(c,t) { C_CENTROID(x,c,t); xc = x[0]; yc = x[1]; zc = x[2]; if ((xc >= 0.0) && (xc <= 0.35)) { if ((yc >= 0.0) && (yc <= 0.35)) { if ((zc >= 1.5) && (zc <= 1.85)) { sensor_temperature = sensor_temperature + C_T(c,t); nt=nt+1.0; } } } } end_c_loop(c,t) } sensor_temperature /= nt; Message0 ("xc = %f\n",xc); Message0 ("yc = %f\n",yc); Message0 ("zc = %f\n",zc); Message0 ("nt = %f\n",nt); Message0("sensor_temperature = %f\n",sensor_temperature); } Output: xc = -2.738164 yc = 1.462940 zc = 4.978388 nt = 0.000000 sensor_temperature = inf |
|
January 9, 2020, 04:13 |
|
#25 |
Senior Member
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34 |
it is possible to see
xc = -2.738164 yc = 1.462940 zc = 4.978388 if your domain is big enough, check it because Message0 ("xc = %f\n",xc); Message0 ("yc = %f\n",yc); Message0 ("zc = %f\n",zc); show last coordinates from loop, there is no reason for coordinates to be filtered by if statment nt = 0.000000 sensor_temperature = inf sensor_temperature = inf because you have sensor_temperature /= nt; where nt=0 nt=0 could be only if code never goes inside if statement, check why check your domain and dimensons
__________________
best regards ****************************** press LIKE if this message was helpful |
|
January 9, 2020, 22:18 |
|
#26 |
Member
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7 |
Hello Alexander,
I have changed the code now and after compilation am getting the desired output. But am not sure if this approach is correct or not. I think it is just pasting the value of the global variable and the coordinate values. Could you please have a look at it? Thanks #include "udf.h" real sensor_temperature = 315.15 DEFINE_EXECUTE_AT_END(tsensor) { real x[ND_ND]; real xc,yc,zc,nt; cell_t c; Domain *d; Thread *t; d = Get_Domain(1); thread_loop_c(t,d) { begin_c_loop(c,t) { C_CENTROID(x,c,t); xc = 0.00000; yc = 0.00000; zc = 1.50000; if (x[0] = xc) { if (x[1] = yc) { if ( x[2] = zc) { sensor_temperature = C_T(c,t); } } } } end_c_loop(c,t) } Message0 ("xc = %f\n",xc); Message0 ("yc = %f\n",yc); Message0 ("zc = %f\n",zc); Message0("sensor_temperature = %f\n",sensor_temperature); } Outcome before first iteration: xc = 0.000000 yc = 0.000000 zc = 1.500000 sensor_temperature = 315.150000 step flow-time report-def-0 report-def-0 0 0.0000e+00 0.0000e+00 3.5944e-01 Outcome after first solution convergence: xc = 0.000000 yc = 0.000000 zc = 1.500000 sensor_temperature = 315.150000 step flow-time report-def-0 report-def-0 flow-time 1 2.0000e+00 -9.5503e-01 9.5472e-01 2.0000e+00 Flow time = 2s, time step = 1 |
|
January 10, 2020, 00:09 |
|
#27 |
Senior Member
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34 |
this is wrong
Code:
if (x[0] = xc) { if (x[1] = yc) { if ( x[2] = zc) { sensor_temperature = C_T(c,t); } } } but with x[0] == xc you will never get true, because exact match has very low chance use previous code in FLuent GUI go to General -> check -> in console you will get information of your domain show it, especially Domain Extents
__________________
best regards ****************************** press LIKE if this message was helpful |
|
January 10, 2020, 00:38 |
|
#28 |
Member
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7 |
Hello alexander,
Below is the attached domain parameters for your reference: Domain Extents: x-coordinate: min (m) = -6.400000e+00, max (m) = 6.400000e+00 y-coordinate: min (m) = -6.399987e+00, max (m) = 6.399991e+00 z-coordinate: min (m) = 0.000000e+00, max (m) = 1.236673e+01 Volume statistics: minimum volume (m3): 2.110109e-09 maximum volume (m3): 1.220315e-02 total volume (m3): 1.380014e+03 Face area statistics: minimum face area (m2): 2.388706e-06 maximum face area (m2): 1.069415e-01 I was having a look at a udf macro: CX_Find_Cell_With_Point: problem It was mentioned that to first locate the allotted coordinate in the domain and then find out the associated cell centroid followed by acquiring its variable. Is it of any help to incorporate it into the above code? Thanks |
|
January 10, 2020, 01:13 |
|
#29 |
Senior Member
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34 |
in my case code from January 8 works well
seems like your mesh has huge elements so no element is inside if ((xc >= 0.0) && (xc <= 0.35)) { if ((yc >= 0.0) && (yc <= 0.35)) { if ((zc >= 1.5) && (xc <= 1.85)) but it's hard to imagine
__________________
best regards ****************************** press LIKE if this message was helpful |
|
January 10, 2020, 01:27 |
|
#30 |
Member
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7 |
In my case, the element size is 350 mm with skewness metrics of 0.7. For such a large domain I had already carried out the mesh independency test and came down to 350 mm. Any suggestions to fit the current code in the domain?
Thanks |
|
January 10, 2020, 01:33 |
|
#31 |
Member
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7 |
I have also tried changing the xc, yc and zc limits but it is giving the same output as mentioned below:
Output: xc = -2.738164 yc = 1.462940 zc = 4.978388 nt = 0.000000 sensor_temperature = inf |
|
January 10, 2020, 07:06 |
|
#32 |
Senior Member
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34 |
element size is 350mm?
what are you going to get with this mesh? I've never saw something like this.... but, lets assume, that it is correct, change code to following: Code:
if ((xc >= -0.4) && (xc <= 0.4)) { if ((yc >= -0.4) && (yc <= 0.4)) { if ((zc >= 1.2) && (xc <= 1.9))
__________________
best regards ****************************** press LIKE if this message was helpful |
|
January 10, 2020, 08:51 |
|
#33 |
Member
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7 |
Hello alexander,
I incorporated your suggestion but am receiving the same output whatsoever be the limits of the xc, yc and zc. Should i export the coordinates of node or cell center of the domain and figure out a close range and then check out the code? And i have arrived at this mesh size by performing mesh independency tests with temperature and courant number. So it is quite reasonable for my model case. Thanks |
|
January 12, 2020, 19:41 |
|
#34 |
Senior Member
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34 |
how you can check, how many cells are inside your if statement:
in fluent gui go to ADAPT-> Mark/Adapt cells -> Region put there your coordinates, press MARK push Manage button here, select the region and press Display. find coordinates that will select as many cells as you need
__________________
best regards ****************************** press LIKE if this message was helpful |
|
January 13, 2020, 03:39 |
|
#35 |
Member
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7 |
Hello,
I followed your directions and with the designated limits of x,y, and z coordinates below: if ((xc >= -0.4) && (xc <= 0.4)) { if ((yc >= -0.4) && (yc <= 0.4)) { if ((zc >= 1.2) && (zc <= 1.9)) The GUI showed 93 cells. Then I took the coordinates of two farthest ends and ran the simulation. But it is showing the same output. #include "udf.h" real sensor_temperature = 315.15 DEFINE_EXECUTE_AT_END(tsensor) { real x[ND_ND]; real xc,yc,zc,nt; cell_t c; Domain *d; Thread *t; d = Get_Domain(1); nt=0.0; /* Begin loop to determine the temperature at the centroid of cells near the thermocouple */ thread_loop_c(t,d) { begin_c_loop(c,t) { C_CENTROID(x,c,t); xc = x[0]; yc = x[1]; zc = x[2]; if ((xc >= -0.468547) && (xc <= 0.493969)) { if ((yc >= -0.51177) && (yc <= 0.428963)) { if ((zc >= 1.34611) && (zc <= 1.59948)) { sensor_temperature = sensor_temperature + C_T(c,t); nt=nt+1.0; } } } } end_c_loop(c,t) } sensor_temperature /= nt; Message0 ("xc = %f\n",xc); Message0 ("yc = %f\n",yc); Message0 ("zc = %f\n",zc); Message0 ("nt = %f\n",nt); Message0("sensor_temperature = %f\n",sensor_temperature); } output: xc = -2.738164 yc = 1.462940 zc = 4.978388 nt = 0.000000 sensor_temperature = inf step flow-time report-def-0 report-def-0 flow-time 1 2.0000e+00 -9.5503e-01 9.5472e-01 2.0000e+00 I have also tried with: begin_c_loop_int(c,t) .. end_c_loop_int(c,t) and its "ext" counterpart but recieving the following output: xc = -0.469403 yc = 0.800998 zc = 0.450407 nt = 0.000000 sensor_temperature = inf tavg_abr = 19.521501 UDSavg_abr = 0.000455 area_tot = 299.520000 step flow-time report-def-0 report-def-0 flow-time 1 2.0000e+00 -9.5503e-01 9.5472e-01 2.0000e+00 The if-condition is not getting into the code. Does it gets affected by parallel consideration? Your suggestions are highly required. Thanks |
|
January 13, 2020, 18:53 |
|
#36 |
Senior Member
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34 |
you may try to add this before sensor_temperature /= nt; line
Code:
# if RP_NODE /* Perform node synchronized actions here Does nothing in Serial */ nt = PRF_GRSUM1(nt); sensor_temperature = PRF_GRSUM1(sensor_temperature); # endif /* RP_NODE */
__________________
best regards ****************************** press LIKE if this message was helpful |
|
January 13, 2020, 21:33 |
|
#37 |
Member
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7 |
Hello,
Thanks for the suggestions, Alexander. The nt and sensor_temperature are all good now. However, the coordinate values are not as desired. Is there any other reason why it is so? Thanks #include "udf.h" real sensor_temperature = 315.15 DEFINE_EXECUTE_AT_END(tsensor) { real x[ND_ND]; real xc,yc,zc,nt; real sensor_temperature = 0.0; /* had to re-initialize again otherwise the sensor_temperature value was increasing after each timestep.*/ cell_t c; Domain *d; Thread *t; d = Get_Domain(1); nt=0.0; thread_loop_c(t,d) { begin_c_loop(c,t) { C_CENTROID(x,c,t); xc = x[0]; yc = x[1]; zc = x[2]; if ((xc >= -0.468547) && (xc <= 0.493969)) { if ((yc >= -0.51177) && (yc <= 0.428963)) { if ((zc >= 1.34611) && (zc <= 1.59948)) { sensor_temperature = sensor_temperature + C_T(c,t); nt=nt+1.0; } } } } end_c_loop(c,t) } # if RP_NODE nt = PRF_GRSUM1(nt); sensor_temperature = PRF_GRSUM1(sensor_temperature); # endif sensor_temperature /= nt; Message0 ("xc = %f\n",xc); Message0 ("yc = %f\n",yc); Message0 ("zc = %f\n",zc); Message0 ("nt = %f\n",nt); Message0("sensor_temperature = %f\n",sensor_temperature); } output: xc = -2.738164 yc = 1.462940 zc = 4.978388 nt = 61.000000 sensor_temperature = 315.150000 |
|
January 13, 2020, 22:47 |
|
#38 |
Senior Member
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34 |
what you want from these coordinates?
you have a loop of cells, each cell has its own coordinates, but you return coordinates once you may put xc = x[0]; yc = x[1]; zc = x[2]; inside if statement to get coordinates of last cell inside your box
__________________
best regards ****************************** press LIKE if this message was helpful |
|
January 13, 2020, 23:02 |
|
#39 |
Member
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7 |
Hello, alexander,
The coordinate range that I have used denotes the sensor location. So I want the temperature of the cells averaged in that range only to act as a control unit for the inlet parameters. In this case, how can I be sure that the sensor_temperature that I am getting is the temperature in the given coordinate range? In addition, shouldn't the code show the last coordinate that must be within the given range? Your suggestions are highly required. Thanks |
|
January 14, 2020, 00:00 |
|
#40 |
Senior Member
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34 |
you may put
xc = x[0]; yc = x[1]; zc = x[2]; inside if statement to get coordinates of last cell inside your box
__________________
best regards ****************************** press LIKE if this message was helpful |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Foam::error::PrintStack | almir | OpenFOAM Running, Solving & CFD | 92 | May 21, 2024 08:56 |
[General] Extracting ParaView Data into Python Arrays | Jeffzda | ParaView | 30 | November 6, 2023 22:00 |
Running UDF with Supercomputer | roi247 | FLUENT | 4 | October 15, 2015 14:41 |
How to use UDF to get the Global courant number at time in fluent 14.0? | fangdian | Fluent UDF and Scheme Programming | 1 | August 18, 2015 20:33 |
[Commercial meshers] Trimmed cell and embedded refinement mesh conversion issues | michele | OpenFOAM Meshing & Mesh Conversion | 2 | July 15, 2005 05:15 |