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

How to calculate the number of if conditions executed in a fluent udf?

Register Blogs Community New Posts Updated Threads Search

Like Tree7Likes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   December 16, 2019, 19:20
Default How to calculate the number of if conditions executed in a fluent udf?
  #1
Member
 
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7
Shubhamp is on a distinguished road
DEFINE_EXECUTE_AT_END(sensort)
{
real sensor_coordinate[ND_ND];
real sensor_temperature;
real xmin;
real xmax;
real ymin;
real ymax;
real zmin;
real zmax;
real x,y,z,nt;

cell_t c;
Domain *d;
Thread *t;
d = Get_Domain(1);
xmin=-0.1;
xmax=0.1;
ymin=-0.1;
ymax=0.1;
zmin=1.4;
zmax=1.6;
thermosensor_temperature=0.0;
nt=0.0;
thread_loop_c(t,d)
{
begin_c_loop(c,t)
{
C_CENTROID(sensor_coordinate,c,t);

x=sensor_coordinate[0];
y=sensor_coordinate[1];
z=sensor_coordinate[2];


if ((x >= xmin) && (x <= xmax))
{
if ((y >= ymin) && (y <= ymax))
{
if ((z >= zmin) && (z <= zmax))
{
sensor_temperature = sensor_temperature + C_T(c,t);
nt=nt+1.0;
}
}
}
}
end_c_loop(c,t)
}
sensor_temperature = sensor_temperature/nt;
sensor_temperature1 = sensor_temperature;
}
DEFINE_PROFILE (velocity,t,i)
{
real velocity;
face_t f;
if (sensor_temperature1 >= 300)
{
velocity=1.5;
}
else
{
velocity=0.0;
}
begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = velocity;
}
end_f_loop(f,t)
}

Here I have two doubts:
1) Does the min and max value of each direction depend upon the actual mesh length? How these values are decided?
2) how can I get the total number of "if" executions when v=1.5 during a transient simulation?
3) If I want to obtain the temperature value at that coordinates in each 3600 s, does using DEFINE_ON_DEMAND macro make more sense?
Suggestions are highly required.
Thanks in advance.
Shubhamp is offline   Reply With Quote

Old   December 17, 2019, 07:33
Default
  #2
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
1. min and max values of each direction is decided by user according to his needs.

2. what does it mean: number of "if" executions
this code is made such way, that "if" could be executed only once, when
sensor_temperature1 >= 300 -> velocity=1.5;

3. to execute macro DEFINE_ON_DEMAND you should click it manually
DEFINE_EXECUTE_AT_END executes after each time step (iteration in steady state)
DEFINE_ADJUST executes before each time step

more information in Ansys Fluent Customization manual
Shubhamp likes this.
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Old   December 17, 2019, 18:53
Default
  #3
Member
 
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7
Shubhamp is on a distinguished road
Hello Alexander,
Apologies for not explaining it properly. Actually I am using transient ambient weather temperature as an inlet condition. So, when sensor_temperature1 > F_T(f,t), then only the v should be 1.5 m/s or else v = 0.0.
In this case, let say if I carry out the transient simulation for 24 hr (86400 s), then I wanted to know how many times v was 1.5 m/s.
Hope this explanation help to figure out the problem.

Thanks
Shubhamp is offline   Reply With Quote

Old   December 17, 2019, 23:57
Default
  #4
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
change DEFINE_PROFILE function and add other lines to your code
Code:
#include "udf.h"

int if_number =0;

DEFINE_ON_DEMAND(get_if_number)
{
	Message0(" ********  int if_number = %d **************\n",if_number);
}

DEFINE_PROFILE (velocity,t,i)
{
real velocity;
face_t f;
if (sensor_temperature1 >= 300)
{
velocity=1.5;
if_number++;
}
else
{
velocity=0.0;
}
begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = velocity;
}
end_f_loop(f,t)
}
in GUI you can execute "get_if_number" on demand to get this value
Shubhamp likes this.
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Old   December 18, 2019, 00:15
Default
  #5
Member
 
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7
Shubhamp is on a distinguished road
Thanks Alexander for clearing the doubt on obtaining the number of if statements.
But if we look on a transient basis the inlet temperature is not a constant value as the velocity operation (1.5 m/s) will be occurring when "sensor_temperature1" is greater than the inlet temperature (F_T(f,t)).
I implemented this in the DEFINE_PROFILE (velocity,t,i) macro but after compilation it is showing error as "uninitialized variable". Could you please suggest me how to initialize it?

Thanks
Shubhamp is offline   Reply With Quote

Old   December 18, 2019, 00:59
Default
  #6
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
show your code, frankly speaking, I don't understand what are you talking about:
code you are using, compilation log and errors
Shubhamp likes this.
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Old   December 18, 2019, 01:17
Default
  #7
Member
 
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7
Shubhamp is on a distinguished road
Please see the attached code:
#include "udf.h"
real sensor_temperature1;
real sensor_temperature1 = 400.15;
int if_number = 0;
/* Obtain the mean temperature at the location of sensor */
/* sensor located at coordinates (x,y,z) = (0.0,0.0,1.5) */
DEFINE_EXECUTE_AT_END(tsensor)
{
real x[ND_ND];
real sensor_temperature;
real nt;

cell_t c;
Domain *d;
Thread *t;
d = Get_Domain(1);
sensor_temperature=0.0;
nt=0.0;
/* Begin loop to determine the temperature at the centroid of cells near the sensor */
thread_loop_c(t,d)
{
begin_c_loop(c,t)
{
C_CENTROID(x,c,t);

if ((-0.175 < x[0] < 0.175),(-0.175 < x[1] < 0.175),(1.325 < x[2] < 1.675))
{
sensor_temperature = sensor_temperature + C_T(c,t);
nt=nt+1.0;
}
}
end_c_loop(c,t)
}
sensor_temperature = sensor_temperature/nt;
sensor_temperature1 = sensor_temperature;
}
DEFINE_PROFILE(velocity,t,i)
{
real velocity;
face_t f;
begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = F_T(f,t);
F_PROFILE(f,t,i) = F_UDSI(f,t,0);
}
end_f_loop(f,t)
if (F_T(f,t) <= sensor_temperature1 && F_UDSI(f,t,0) < 0.03)
{
velocity = 1.5;
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);
}

The actual process is to obtain the temperature at each time step from the designated coordinates and then use that value to change the inlet velocity value based on the if condition. Here i have to first initialize the inlet temperature and inlet UDS values. Please suggest if their is any mistake?
Shubhamp is offline   Reply With Quote

Old   December 18, 2019, 02:01
Default
  #8
Member
 
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7
Shubhamp is on a distinguished road
Hello Alexander,
Should I include another DEFINE_EXECUTE_AT_END macro to get the inlet temperature and UDS values first (specify them as a global variable) and then incorporate that global variable into the "if condition" of the DEFINE_PROFILE macro to change the inlet velocity? And is the representation of the coordinates correct in the code?

Thanks
Shubhamp is offline   Reply With Quote

Old   December 18, 2019, 04:12
Default
  #9
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
you have a lot of problems here.
First of all I recommend you always compile your code, so you can check compilation log and see which problems you have if any.

In DEFINE_PROFILE(velocity,t,i):
Code:
begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = F_T(f,t);
F_PROFILE(f,t,i) = F_UDSI(f,t,0);
}
end_f_loop(f,t)
what is it?
F_PROFILE(f,t,i) is macro to RETURN value, the value which you will write here will be return to fluent through DEFINE_PROFILE(velocity,t,i)

So if you want to calculate velocity you use: F_PROFILE(f,t,i) = velocity;
which makes sense

Big question is F_UDSI(f,t,0); ????? Why do use UDS here?
It is not defined anywhere. I have no idea about it
so your condition doesn't work
Code:
if (F_T(f,t) <= sensor_temperature1 && F_UDSI(f,t,0) < 0.03)
Other principal question, are you going to modify (change from 0 to 1.5) velocity in the whole boundary, or only in face element, where temperature is less than sensor_temperature1 ????

representation of the coordinates in the code is INCORRECT, you have correct version in very first your message
Shubhamp likes this.
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Old   December 18, 2019, 20:32
Default
  #10
Member
 
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7
Shubhamp is on a distinguished road
Hello Alexander,
This is my revised code (according to your suggestions) containing the defined inlet temperature and UDS at the whole inlet boundary. I had compiled it but received some errors that are mentioned as a comment alongside the respective line. Please suggest if the code is correct or not?

#include "udf.h"

real tavg_abr = 286.5;
real UDSavg_abr = 0.007;
real sensor_temperature1 = 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 sensor_temperature;
real nt;

cell_t c;
Domain *d;
Thread *t;
d = Get_Domain(1);
sensor_temperature=0.0;
nt=0.0;
/* Begin loop to determine the temperature at the centroid of cells near the sensor */
thread_loop_c(t,d)
{
begin_c_loop(c,t)
{
C_CENTROID(x,c,t);

if (-0.175 < x[0] < 0.175)
{
if (-0.175 < x[1] < 0.175)
{
if (1.325 < x[2] < 1.675)
{
sensor_temperature = sensor_temperature + C_T(c,t);
nt=nt+1.0;
}}}
}
end_c_loop(c,t)
}
sensor_temperature = sensor_temperature/nt;
sensor_temperature1 = sensor_temperature;
}

DEFINE_EXECUTE_AT_END(avg_inlet_tempandUDS)
{
Domain *d;
face_t f;
real A[ND_ND]; /*error C2143: syntax error: missing ';' before 'constant'and error C2059: syntax error: 'empty declaration' and error C2109: subscript requires array or pointer type*/
real area;
real area_tot = 0.0;
int ID = 8; /*this is the ID of the inlet boundary that I want to get the temperature and UDS from*/
Thread *t;
d = Get_Domain(1);
t = Lookup_Thread(d,ID);
begin_f_loop(f,t)
{
F_AREA(A,f,t); /*error C2109: subscript requires array or pointer type */
area = NV_MAG(A); /*error C2109: subscript requires array or pointer type and error C2198: 'sqrt': too few arguments for call */
area_tot += area;
tavg_abr += F_T(f,t)*area;
UDSavg_abr += F_UDSI(f,t,0)*area;
}
end_f_loop(f,t)
tavg_abr /= area_tot;
UDSavg_abr /= area_tot;
Message("tavg_abr = %g UDSavg_abr = %g area_tot = %g\n",tavg_abr, UDSavg_abr, area_tot);
}

DEFINE_PROFILE(velocity,t,i)
{
real velocity;
face_t f;
if (tavg_abr <= sensor_temperature1 && 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)
{
Message("******** int if_number = %d **************\n",if_number);
}

Thanks
Shubhamp is offline   Reply With Quote

Old   December 19, 2019, 01:52
Default
  #11
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
change this part
Code:
begin_f_loop(f,t)
{
F_AREA(A,f,t);
area = NV_MAG(A); 
area_tot += area;
tavg_abr += F_T(f,t)*area;
UDSavg_abr += F_UDSI(f,t,0)*area;
}
end_f_loop(f,t)
# if RP_NODE /* Perform node synchronized actions here; Does nothing in Serial */
area_tot = PRF_GRSUM1(area_tot);
tavg_abr = PRF_GRSUM1(tavg_abr);
UDSavg_abr = PRF_GRSUM1(UDSavg_abr);
# endif /* RP_NODE */
You didn't answer my questions! Answer each of them
What is F_UDSI(f,t,0) ??? Most likely you don't need it
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Old   December 19, 2019, 02:25
Default
  #12
Member
 
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7
Shubhamp is on a distinguished road
Hello Alexander,
As I need to define the inlet UDS (absolute humidity) value, hence I used F_UDSI(f,t,0) in the code (inlet boundary has both temperature and absolute humidity). Is there any other process to execute it?
I compiled the code after revising it according to your suggestions but it is showing the same error as it was showing the first time (as mentioned above). Could you please have a re-look at that?

Thanks
Shubhamp is offline   Reply With Quote

Old   December 19, 2019, 04:52
Default
  #13
Member
 
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7
Shubhamp is on a distinguished road
Hello,
I managed to compile the code without any errors. However, the tavg_abr and UDSavg_abr values are not according to what I am expecting, thus getting unexpected velocity. I have attached my transient inlet temperature and UDS values for your referral.

Message:
tavg_abr = -nan(ind) UDSavg_abr = -nan(ind) area_tot = 0
tavg_abr = 439.543 UDSavg_abr = 0.007 area_tot = 18.72
sensor_temperature1 = -nan(ind)
sensor_temperature1 = 315.15

inlet values:
(time
0
3600
7200
10800
14400
18000
21600
25200
28800
32400
36000
39600
43200
46800
50400
54000
57600
61200
64800
68400
72000
75600
79200
82800
86400
)
(temperature
286.5
285.25
285.15
286.05
286.3
287.2
287.45
288.7
290
291.2
292.2
293.5
294.75
295.6
296.4
296.55
296.55
296.5
295.9
294.85
292.45
290.05
288.35
286.55
286.15
)
(absolute_humidity
0.007
0.007
0.007
0.007
0.008
0.008
0.008
0.008
0.008
0.008
0.008
0.008
0.009
0.009
0.008
0.009
0.009
0.009
0.009
0.009
0.009
0.008
0.008
0.008
0.008
)
)
Shubhamp is offline   Reply With Quote

Old   December 19, 2019, 04:54
Default
  #14
Member
 
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7
Shubhamp is on a distinguished road
Hello ArashA,
Are you getting any unexpected values during the simulation? Please share if you did it correctly.

Thanks
Shubhamp is offline   Reply With Quote

Old   December 19, 2019, 05:06
Default
  #15
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
I've compiled this code with no errors
Code:
#include "udf.h"

real tavg_abr = 286.5;
real UDSavg_abr = 0.007;
real sensor_temperature1 = 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 sensor_temperature;
real nt;

cell_t c;
Domain *d;
Thread *t;
d = Get_Domain(1);
sensor_temperature=0.0;
nt=0.0;
/* Begin loop to determine the temperature at the centroid of cells near the sensor */
thread_loop_c(t,d)
{
begin_c_loop(c,t)
{
C_CENTROID(x,c,t);

if (-0.175 < x[0] < 0.175)
{
if (-0.175 < x[1] < 0.175)
{
if (1.325 < x[2] < 1.675)
{
sensor_temperature = sensor_temperature + C_T(c,t);
nt=nt+1.0;
}}}
}
end_c_loop(c,t)
}
sensor_temperature = sensor_temperature/nt;
sensor_temperature1 = sensor_temperature;
}

DEFINE_EXECUTE_AT_END(avg_inlet_tempandUDS)
{
Domain *d;
face_t f;
real A[ND_ND];
real area;
real area_tot = 0.0;
int ID = 8; /*this is the ID of the inlet boundary that I want to get the temperature and UDS from*/
Thread *t;
d = Get_Domain(1);
t = Lookup_Thread(d,ID);
begin_f_loop(f,t)
{
F_AREA(A,f,t);
area = NV_MAG(A); 
area_tot += area;
tavg_abr += F_T(f,t)*area;
UDSavg_abr += F_UDMI(f,t,0)*area;
}
end_f_loop(f,t)
# if RP_NODE /* Perform node synchronized actions here; Does nothing in Serial */
area_tot = PRF_GRSUM1(area_tot);
tavg_abr = PRF_GRSUM1(tavg_abr);
UDSavg_abr = PRF_GRSUM1(UDSavg_abr);
# endif /* RP_NODE */
tavg_abr /= area_tot;
UDSavg_abr /= area_tot;
Message0("tavg_abr = %g UDSavg_abr = %g area_tot = %g\n",tavg_abr, UDSavg_abr, area_tot);
}

DEFINE_PROFILE(velocity,t,i)
{
real velocity;
face_t f;
if (tavg_abr <= sensor_temperature1 && 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)
{
Message("******** int if_number = %d **************\n",if_number);
}
I've changed F_UDSI(f,t,0) to F_UDMI(f,t,0), because this is what you probably want from your code. (as you are not calculating humidity but get it from file -> use user-defined memory, not scalar)

how do you read "inlet values"?
make additional messages to prove, that everything had been read correctly
Shubhamp likes this.
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Old   December 19, 2019, 05:44
Default
  #16
Member
 
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7
Shubhamp is on a distinguished road
Hello Alexander,
I had already changed to F_UDMI(f,t,0) and compiled it without any errors. But I am receiving the following message after first iteration:
tavg_abr = -nan(ind) UDSavg_abr = -nan(ind) area_tot = 0
tavg_abr = 439.543 UDSavg_abr = 0.007 area_tot = 18.72
sensor_temperature1 = -nan(ind)
sensor_temperature1 = 315.15

Then every iteration is showing the artificial wall as I had selected the no reverse flow at the outlet section. And the volume flow rate (by activating the surface monitor) shows unexpected value (but should be 0.945 m3/s as v is 0.051 m/s and the area is 18.72 m2). Even after the first time step, the tavg_abr value is showing around 512K.

I simply uploaded the .prof file of the inlet values and allocated it to the inlet sections.

As the tavg_abr > sensor_temperature1 here velocity is 0. However, it should be the opposite as the inlet temperature is 286.5K.


Thanks
Shubhamp is offline   Reply With Quote

Old   December 20, 2019, 02:56
Default
  #17
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
if you used code, I've sent you, you would never get this output:
tavg_abr = -nan(ind) UDSavg_abr = -nan(ind) area_tot = 0
tavg_abr = 439.543 UDSavg_abr = 0.007 area_tot = 18.72
sensor_temperature1 = -nan(ind)
sensor_temperature1 = 315.15

use latest version, and always attach code you are using with errors you get, each time

How do you define F_UDMI(f,t,0) ???? You didn;t answer this question,
just reading profile is not defining, you must hook it, how did you do that?

Explain line by line following code, what are you going to have from this:
Code:
thread_loop_c(t,d)
{
begin_c_loop(c,t)
{
C_CENTROID(x,c,t);

if (-0.175 < x[0] < 0.175)
{
if (-0.175 < x[1] < 0.175)
{
if (1.325 < x[2] < 1.675)
{
sensor_temperature = sensor_temperature + C_T(c,t);
nt=nt+1.0;
}}}
}
end_c_loop(c,t)
}
sensor_temperature = sensor_temperature/nt;
sensor_temperature1 = sensor_temperature;
answer each question
Shubhamp likes this.
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Old   December 20, 2019, 10:02
Default
  #18
Member
 
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7
Shubhamp is on a distinguished road
Hello Alexander,
Let me explain from the start:
The whole process is carried out in parallel processing.
First I am using the DEFINE_INIT macro to initialize the domain temperature and absolute humidity (absolute humidity is defined using UDS and stored in C_UDSI). Then using DEFINE_ADJUST, the domain variables were updated at every timestep. Now, in the meantime, sensor_temperature1 (at every timestep) should be computed along with tavg_abr and UDSavg_abr (both at every timestep) using DEFINE_EXECUTE_AT_END and then pass the values to alter the velocity (DEFINE_PROFILE).

Now answers to your query:
1. I had used your revised code along with DEFINE_INIT and DEFINE_ADJUST but am receiving the same output.
2. I uploaded the .prof file containing time, temperature and UDS (absolute humidity) values (File -> Read -> Profile). Then hooked the temperature and UDS values in the inlet boundary dialog box, respectively.
3. Mentioned code explanation:
I am trying to access the temperature of coordinate (0,0,1.5) in the domain that gets updated at each timestep. Then as sensor_temperature1 is defined as a global variable, it should be accessed by the DEFINE_PROFILE to adjust the inlet face velocity.

Please let me know if this explanation is sufficient.

Thanks
Shubhamp is offline   Reply With Quote

Old   December 22, 2019, 23:54
Default
  #19
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
no its not sufficient, I've told you explain line by line

each time you are using different code, I can't guess, how it looks like.
the way how you read UDS (humidity) is wrong (most likely)

try to read from file and apply humidity as a boundary condition using DEFINE_PROFILE macro

take into account, that you use parallel consideration
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Old   January 7, 2020, 21:08
Default
  #20
Member
 
South Australia
Join Date: Jan 2019
Posts: 32
Rep Power: 7
Shubhamp is on a distinguished road
Hello Alexander,
Apologies for the late reply. I have attached the new code with some modifications after your suggestions. Unfortunately, I am receiving unexpected values before the first iteration and after the first solution convergence as mentioned below. I have also attached the inlet values that are supposed to be shown by tavg_abr and UDSavg_abr. The main aspect of this code is to obtain the sensor_temperature and tavg_abr and UDSavg_abr at the defined coordinate and at the inlet face boundary, respectively after each timestep and then use the values to change the inlet velocity.

Please suggest if any correction is required. Thanks

#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)
{
#if PARALLEL
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)
#endif
}
sensor_temperature /= nt; /* Avg temperature after each timestep */
#if !RP_NODE
Message ("xc = %g\n",xc);
Message ("yc = %g\n",yc);
Message ("zc = %g\n",zc);
Message ("nt = %g\n",nt);
Message("sensor_temperature = %g\n",sensor_temperature);
#endif
}
DEFINE_EXECUTE_AT_END(avg_inlet_tempandUDS)
{
#if PARALLEL
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)
#endif
#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;
#if !RP_NODE
Message("tavg_abr = %g UDSavg_abr = %g area_tot = %g\n",tavg_abr, UDSavg_abr, area_tot);
#endif
}
DEFINE_PROFILE(velocity,t,i)
{
#if PARALLEL
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)
#endif
}
DEFINE_ON_DEMAND(get_if_number)
{
#if !RP_NODE
Message("******** int if_number = %g **************\n",int if_number);
#endif
}



My inlet temperature and UDS values: /* the .prof file containing these transient values was read (File -> Read -> Profile) and then the temperature and UDS (absolute humidity defined as a scalar quantity) values were hooked at the inlet boundary condition (ID = 8) */
(time
0
3600
7200
10800
14400
18000
21600
25200
28800
32400
36000
39600
43200
46800
50400
54000
57600
61200
64800
68400
72000
75600
79200
82800
86400
)
(temperature
286.5
285.25
285.15
286.05
286.3
287.2
287.45
288.7
290
291.2
292.2
293.5
294.75
295.6
296.4
296.55
296.55
296.5
295.9
294.85
292.45
290.05
288.35
286.55
286.15
)
(absolute_humidity
0.007
0.007
0.007
0.007
0.008
0.008
0.008
0.008
0.008
0.008
0.008
0.008
0.009
0.009
0.008
0.009
0.009
0.009
0.009
0.009
0.009
0.008
0.008
0.008
0.008
)
)

Outcome before first iteration:
xc = 0
yc = 0
zc = 0
nt = 0
sensor_temperature = inf /* should be 315.15 as it is initialized globally but it is showing inf */
tavg_abr = 15.3045 UDSavg_abr = 0.000373932 area_tot = 18.72 /* tavg_abr and UDSavg_abr values should be 286.5 and 0.007 */
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
yc = 0
zc = 0 /* why is these values 0 */
nt = 0
sensor_temperature = inf /* unexpected character */
tavg_abr = 0.817547 UDSavg_abr = 1.9975e-05 area_tot = 18.72 /* unexpected values */
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
Shubhamp 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
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


All times are GMT -4. The time now is 20:59.