CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   Fluent UDF and Scheme Programming (http://www.cfd-online.com/Forums/fluent-udf/)
-   -   temperature at a point (http://www.cfd-online.com/Forums/fluent-udf/83785-temperature-point.html)

ehooi January 11, 2011 09:10

temperature at a point
 
Hi,
Does anyone know how to write the UDF to calculate the temperature at a point? I know surface monitors allow me to check the temperature at a point. However, the temperature at the particular point is needed to change the boundary condition.

Thank you.


Sincerely,
EH

Sangeeta January 11, 2011 11:31

Quote:

Originally Posted by ehooi (Post 290002)
Hi,
Does anyone know how to write the UDF to calculate the temperature at a point? I know surface monitors allow me to check the temperature at a point. However, the temperature at the particular point is needed to change the boundary condition.

Thank you.


Sincerely,
EH

DO u want to keep cahnging the BC depending on temperature?

ehooi January 11, 2011 11:36

Quote:

Originally Posted by Sangeeta (Post 290019)
DO u want to keep cahnging the BC depending on temperature?

Yes and it is time-dependent. I am hoping to specify the BC to be something like (syntax no accounted):

if (Tpoint < 333)
F_PROFILE(f,t,i)=10000
else
F_PROFILE(f,t,i)=0
end

Kind of like a heater to ensure that the temperature of the domain is always greater than a specified level. I know the coordinates where Tpoint is to be determined but how do I calculate it in UDF.

ComputerGuy January 11, 2011 22:14

ehooi,

Try something like the following.
Code:

DEFINE_PROFILE(heat_flux_profile,t,i)
{
        real thermostat_coordinate[ND_ND];
        real set_point_temperature;
        real heat_flux;
        real lower_bound_x,upper_bound_x;
        real lower_bound_y,upper_bound_y;
        real lower_bound_z,upper_bound_z;
        cell_t c;
        real x,y,z;
        real cutoff_temperature;
       
        heat_flux=10000;
        cutoff_temperature=333.;
       
        lower_bound_x=1.;upper_bound_x=2.;
        lower_bound_y=1.;upper_bound_y=2.;
        lower_bound_z=1.;upper_bound_z=2.;
       
        begin_c_loop(c,t)
        {
                C_CENTROID(thermostat_coordinate,c,t);
               
                x=thermostat_coordinate[0];
                y=thermostat_coordinate[1];
                z=thermostat_coordinate[2];
               
                if(((x>=lower_bound_x)&&(x<=upper_bound_x))&&((y>=lower_bound_y)&&(y<=upper_bound_y))&&((z>=lower_bound_z)&&(z<=upper_bound_z))
                {
                        if(C_T(c,t)<=cutoff_temperature)
                        {
                                F_PROFILE(c,t,i)=heat_flux;
                        }
                }
               
        }
}

I haven't checked this code for syntax, but the logic is there.

See the following thread for time dependent flow:
Time-dependent flows

You can modify the code I've posted to account for transient heat flux, temperature cutoffs, etc. You'll also need to modify the lower and upper bounds on the x,y, and z coordinates.

ComputerGuy

ehooi January 12, 2011 04:26

Quote:

Originally Posted by ComputerGuy (Post 290059)
ehooi,

Try something like the following.
Code:

DEFINE_PROFILE(heat_flux_profile,t,i)
{
    real thermostat_coordinate[ND_ND];
    real set_point_temperature;
    real heat_flux;
    real lower_bound_x,upper_bound_x;
    real lower_bound_y,upper_bound_y;
    real lower_bound_z,upper_bound_z;
    cell_t c;
    real x,y,z;
    real cutoff_temperature;
 
    heat_flux=10000;
    cutoff_temperature=333.;
 
    lower_bound_x=1.;upper_bound_x=2.;
    lower_bound_y=1.;upper_bound_y=2.;
    lower_bound_z=1.;upper_bound_z=2.;
 
    begin_c_loop(c,t)
    {
        C_CENTROID(thermostat_coordinate,c,t);
 
        x=thermostat_coordinate[0];
        y=thermostat_coordinate[1];
        z=thermostat_coordinate[2];
 
        if(((x>=lower_bound_x)&&(x<=upper_bound_x))&&((y>=lower_bound_y)&&(y<=upper_bound_y))&&((z>=lower_bound_z)&&(z<=upper_bound_z))
        {
            if(C_T(c,t)<=cutoff_temperature)
            {
                F_PROFILE(c,t,i)=heat_flux;
            }
        }
 
    }
}

I haven't checked this code for syntax, but the logic is there.

See the following thread for time dependent flow:
Time-dependent flows

You can modify the code I've posted to account for transient heat flux, temperature cutoffs, etc. You'll also need to modify the lower and upper bounds on the x,y, and z coordinates.

ComputerGuy

Hello ComputerGuy,
Thank you very much. This is very helpful. Let me see if I interpret this correctly. The 'begin_c_loop' basically calculates the centroid of every cell in my domain.

The upper and lower bounds of x, y and z basically defines the limits or the region where the specific point which I want to calculate the temperature is located.

So when the x, y and z is within the bounds, and when the temperature is less than the cutoff temperature, F_PROFILE specifies the required heat flux.

In this case, I guess set_point_temperature is redundant?

Am I right?

Thank you again for your guidance.

ehooi January 12, 2011 07:05

Hi ComputerGuy,
I have tried ammending the codes that you have shown earlier and I came up with this:

Code:


#include "udf.h"
/* Obtain the mean temperature at the location of thermocouple */
/* Thermocouple located at coordinates (x,y,z) = (0.275,0.27,-.900) */
DEFINE_EXECUTE_AT_END(tsensor)
{
real thermosensor_coordinate[ND_ND];
real thermosensor_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.273;
xmax=0.277;
ymin=0.268;
ymax=0.272;
zmin=-0.898;
zmax=-0.90;
/* Begin loop to determine the temperature at the centroid of cells near the thermocouple */
 
thread_loop_c(t,d)
{
nt=0.0;
begin_c_loop(c,t)
{
C_CENTROID(thermosensor_coordinate,c,t);
 
x=thermosensor_coordinate[0];
y=thermosensor_coordinate[1];
z=thermosensor_coordinate[2];
 
 
if ((x >= xmin) and (x <= xmax)) /* line 49 */
{
if ((y >= ymin) and (y <= ymax))
{
if ((z >= zmin) and (z <= zmax))
{
thermosensor_temperature=thermosensor_temperature + C_T(c,t) /* get thermocouple temperature */
nt=nt+1.0 /* count number */
}
}
}
}
end_c_loop(c,t)
}
thermosensor_temperature=thermosensor_temperature/nt
}
DEFINE_PROFILE(heater_bc,t,i)
{
real cutoff_temperature;
real heater_on; /* line 71 */
real heater_off; /* line 74 */
 
face_t f; /* line 74 */
cutoff_temperature=333.0;
heater_on=15923.5;
heater_off=0.0;
if (thermosensor_temperature<=cutoff_temperature)
{
begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = heater_on
}
end_f_loop(f,t)
}
else
{
begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = heater_off
}
end_f_loop
}
}

Since I am intending to check the value at the end of each time step, I have used the DEFINE_EXECUTE_AT_END. Is that correct?

After interpreting this code, I obtain the following error:
cpp -I"C:\PROGRA~1\ANSYSI~1\v121\fluent\fluent12.1.4/src" -I"C:\PROGRA~1\ANSYSI~1\v121\fluent\fluent12.1.4/cortex/src" -I"C:\PROGRA~1\ANSYSI~1\v121\fluent\fluent12.1.4/client/src" -I"C:\PROGRA~1\ANSYSI~1\v121\fluent\fluent12.1.4/multiport/src" -I. -DUDF
ONFIG_H="<C:/Users/EHOOI/AppData/Local/Temp/udfconfig-node1.h>" "C:/Users/EHOOI/AppData/Local/Temp/udfex1.c.1.c"
Error: C:/Users/EHOOI/AppData/Local/Temp/udfex1.c.1.c: line 49: parse error.
Error: C:/Users/EHOOI/AppData/Local/Temp/udfex1.c.1.c: line 71: parse error.
Error: C:/Users/EHOOI/AppData/Local/Temp/udfex1.c.1.c: line 72: parse error.
Error: C:/Users/EHOOI/AppData/Local/Temp/udfex1.c.1.c: line 74: parse error.
Error: C:/Users/EHOOI/AppData/Local/Temp/udfex1.c.1.c: line 76: cutoff_temperature: undeclared variable

The lines where the error are located are pointed out as comments in the code. I cant detect the error that is mentioned.

Could you please help me with this?

Thank you.

ComputerGuy January 12, 2011 08:28

First, use && instead of "and" in your logic. Not sure if "and" actually works (I get my languages jumbled).

Next, in the following lines:
thermosensor_temperature=thermosensor_temperature + C_T(c,t) /* get thermocouple temperature */
nt=nt+1.0 /* count number */

You need semicolons after each line, before the comment. Additionally, why are you adding a thermosensor temperature to the cell temperature? Perhaps I'm confused about the logic, but wouldn't it just be
thermosensor_temp=C_T(c,t);?

Try and fix those things and see how it goes.

ComputerGuy

ehooi January 12, 2011 08:39

I add them up and divide it by the number of added values to obtain the average. My logic behind it is that there may be more than one cell within the lower and upper bounds specified (I may not be right in this assumption). Hence, I calculate the average of it.

Thanks for your pointers. I will try them out now. Can't believe I overlooked the semi-colons there :o

ehooi January 12, 2011 09:05

Hi ComputerGuy,
I have successfully interpreted the code. Couldn;t run it at the moment because my model still needs some tweaking but the successful interpretation of the code is a good sign.

In case anyone else is interested, the code is listed here:
Code:


#include "udf.h"
/* Obtain the mean temperature at the location of thermocouple */
/* Thermocouple located at coordinates (x,y,z) = (0.275,0.27,-.900) */
DEFINE_EXECUTE_AT_END(tsensor)
{
 real thermosensor_coordinate[ND_ND];
 real thermosensor_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.273;
 xmax=0.277;
 ymin=0.268;
 ymax=0.272;
 zmin=-0.898;
 zmax=-0.90;
 /* Begin loop to determine the temperature at the centroid of cells near the thermocouple */
 
 thread_loop_c(t,d)
 {
 nt=0.0;
 begin_c_loop(c,t)
 {
  C_CENTROID(thermosensor_coordinate,c,t);
 
  x=thermosensor_coordinate[0];
  y=thermosensor_coordinate[1];
  z=thermosensor_coordinate[2];
 
 
  if ((x >= xmin) && (x <= xmax))
  {
  if ((y >= ymin) && (y <= ymax))
  {
    if ((z >= zmin) && (z <= zmax))
    {
    thermosensor_temperature=thermosensor_temperature + C_T(c,t); /* get thermocouple temperature */
    nt=nt+1.0;      /* count number */
    }
  }
  }
 }
 end_c_loop(c,t)
 }
 thermosensor_temperature=thermosensor_temperature/nt;
}
DEFINE_PROFILE(heater_bc,t,i)
{
 real cutoff_temperature;
 real thermosensor_temperature;
 real heater_on;
 real heater_off;
 
 face_t f;
 cutoff_temperature=333.0;
 
 heater_off = 0.0;
 heater_on = 15923.5;
 if (thermosensor_temperature<=cutoff_temperature)
 {
  begin_f_loop(f,t)
  {
  F_PROFILE(f,t,i) = heater_on;
  }
  end_f_loop(f,t)
 }
 else
 {
  begin_f_loop(f,t)
  {
  F_PROFILE(f,t,i) = heater_off;
  }
  end_f_loop(f,t)
 }
}

Thank you

ComputerGuy January 12, 2011 22:02

Good job -- glad I could help. I understand the volume averaging scheme on temperature; I just didn't see the piece of code where you divided by the cell count.

Thanks for sharing your final code with us.

ComputerGuy

ehooi January 13, 2011 11:57

Hi ComputerGuy,
In the code that I have written, there are two define macros, DEFINE_ADJUST and DEFINE_PROFILE. In DEFINE_ADJUST, I calculated the value for the variable thermosensor_temperature and in DEFINE_PROFILE, I use the value of thermosensor_temperature.

Can I know if the value of thermosensor_temperature calculated in DEFINE_ADJUST will be automatically stored such that the macro DEFINE_PROFILE can access it or do I have to write some code to store it?

Thanks.


EH

ComputerGuy January 14, 2011 19:11

It won't be automatically stored as written. You can simply write the value to a user-defined memory location (look up: C_UDMI).

ComputerGuy

ahvz October 28, 2013 09:49

I tried to used the below code but I have faced with "warning"

warning C4700: uninitialized local variable 'thermosensor_temperature' used
warning C4700: uninitialized local variable 'thermosensor_temperature' used

how to solve this warning? any helps ?

what is the reason of this kind of warnings ?




Code:

/***********************************************************************
udfexample.c
UDF for specifying the heater
 ************************************************************************/


#include "udf.h"
/* Obtain the mean temperature at the location of thermocouple */
/* Thermocouple located at coordinates (x,y,z) = (-0.24,0.185,0.26) */
DEFINE_EXECUTE_AT_END(tsensor)  /* DEFINE_EXECUTE_AT_END is a general-purpose macro that is executed at the end of an iteration in a steady state run, or at the end of a time step in a transient run. You can use DEFINE_EXECUTE_AT_END when you want to calculate flow quantities at these particular times. Note that you do not have to specify whether your execute-at-end UDF gets executed at the end of a time step or the end of an iteration. This is done automatically when you select the steady or unsteady time method in your ANSYS FLUENT model*/
{
 real thermosensor_coordinate[ND_ND];
 real thermosensor_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.22;
 xmax=-0.26;
 ymin=0.183;
 ymax=0.188;
 zmin=0.25;
 zmax=0.27;
 /* Begin loop to determine the temperature at the centroid of cells near the thermocouple */
 thread_loop_c(t,d)
 {
 nt=0.0;
 begin_c_loop(c,t)
 {
  C_CENTROID(thermosensor_coordinate,c,t);
 
  x=thermosensor_coordinate[0];
  y=thermosensor_coordinate[1];
  z=thermosensor_coordinate[2];
 
 
  if ((x >= xmin) && (x <= xmax))
  {
  if ((y >= ymin) && (y <= ymax))
  {
    if ((z >= zmin) && (z <= zmax))
    {
    thermosensor_temperature=thermosensor_temperature + C_T(c,t); /* get thermocouple temperature */
    nt=nt+1.0;      /* count number */
    }
  }
  }
 }
 end_c_loop(c,t)
 }
 thermosensor_temperature=thermosensor_temperature/nt;
}
DEFINE_PROFILE(heater_bc,t,i)
{
 real cutoff_temperature;
 real thermosensor_temperature;
 real heater_on;
 real heater_off;
 
 face_t f;
 cutoff_temperature=295.15;
 
 heater_off = 0.0;
 heater_on = 22500;
 if (thermosensor_temperature<=cutoff_temperature)
 {
  begin_f_loop(f,t)
  {
  F_PROFILE(f,t,i) = heater_on;
  }
  end_f_loop(f,t)
 }
 else
 {
  begin_f_loop(f,t)
  {
  F_PROFILE(f,t,i) = heater_off;
  }
  end_f_loop(f,t)
 }
}


pakk January 22, 2015 04:24

Quote:

Originally Posted by ahvz (Post 459425)
I tried to used the below code but I have faced with "warning"

warning C4700: uninitialized local variable 'thermosensor_temperature' used
warning C4700: uninitialized local variable 'thermosensor_temperature' used

how to solve this warning? any helps ?

what is the reason of this kind of warnings ?

The variable is not initialized when you use it. It does not have a value yet, but at a certain line in your code you say it should increase its value. The solution: initialize the variable:
Code:

real thermosensor_temperature = 0.0;

hariswch2 September 15, 2015 05:15

how to assign temp from first part of program to second part
 
Quote:

Originally Posted by ehooi (Post 290141)
Hi ComputerGuy,
I have successfully interpreted the code. Couldn;t run it at the moment because my model still needs some tweaking but the successful interpretation of the code is a good sign.

In case anyone else is interested, the code is listed here:
Code:


#include "udf.h"
/* Obtain the mean temperature at the location of thermocouple */
/* Thermocouple located at coordinates (x,y,z) = (0.275,0.27,-.900) */
DEFINE_EXECUTE_AT_END(tsensor)
{
 real thermosensor_coordinate[ND_ND];
 real thermosensor_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.273;
 xmax=0.277;
 ymin=0.268;
 ymax=0.272;
 zmin=-0.898;
 zmax=-0.90;
 /* Begin loop to determine the temperature at the centroid of cells near the thermocouple */
 
 thread_loop_c(t,d)
 {
 nt=0.0;
 begin_c_loop(c,t)
 {
  C_CENTROID(thermosensor_coordinate,c,t);
 
  x=thermosensor_coordinate[0];
  y=thermosensor_coordinate[1];
  z=thermosensor_coordinate[2];
 
 
  if ((x >= xmin) && (x <= xmax))
  {
  if ((y >= ymin) && (y <= ymax))
  {
    if ((z >= zmin) && (z <= zmax))
    {
    thermosensor_temperature=thermosensor_temperature + C_T(c,t); /* get thermocouple temperature */
    nt=nt+1.0;      /* count number */
    }
  }
  }
 }
 end_c_loop(c,t)
 }
 thermosensor_temperature=thermosensor_temperature/nt;
}
DEFINE_PROFILE(heater_bc,t,i)
{
 real cutoff_temperature;
 real thermosensor_temperature;
 real heater_on;
 real heater_off;
 
 face_t f;
 cutoff_temperature=333.0;
 
 heater_off = 0.0;
 heater_on = 15923.5;
 if (thermosensor_temperature<=cutoff_temperature)
 {
  begin_f_loop(f,t)
  {
  F_PROFILE(f,t,i) = heater_on;
  }
  end_f_loop(f,t)
 }
 else
 {
  begin_f_loop(f,t)
  {
  F_PROFILE(f,t,i) = heater_off;
  }
  end_f_loop(f,t)
 }
}

Thank you

.................................................. .................................................. .
Hi ehooi,

Thank you very much for sharing the program with us.

I interpreted your program with some modifications but it was showing udf tsensor and udf heater, both are running fine individually.

while combining both as single program, the thermocouple sensor temperature obtained in the first part of program i.e.,udf tsensor is not assigning as input to second part of program i.e., udf heater.

I would like to know how to assign thermocouple temperate form first part of program (output) to second part of program as a input

Thanks in advance

ghost82 September 15, 2015 08:50

Have a look at this post:
http://www.cfd-online.com/Forums/flu...ide-model.html

If I remember well it is an update of this post.

The problem you have is that the variable that is passed to the second block must be defined as global variable.

hariswch2 September 15, 2015 09:24

Quote:

Originally Posted by ghost82 (Post 564080)
Have a look at this post:
http://www.cfd-online.com/Forums/flu...ide-model.html

If I remember well it is an update of this post.

The problem you have is that the variable that is passed to the second block must be defined as global variable.

.................................................. .................................................. .
Thank u very much for reply ghost82

I defined as you told but still heater is in on position only after crossing higher limit also.

below is my code....

#include "udf.h"
real p;
/* Obtain the mean temperature at the location of thermocouple */
/* Thermocouple located at coordinates (x,y,z) = (0.115,0.135,0.25) */
DEFINE_EXECUTE_AT_END(tsensor) /* exectued at the end of time step in trasient and iteration in steadddy state*/
{
real thermosensor_coordinate[ND_ND];
real thermosensor_temperature;
real xmin;
real xmax;
real ymin;
real ymax;
real zmin;
real zmax;
real x,y,z;
int nt;

cell_t c;
Domain *d;
Thread *t;
d = Get_Domain(1);


xmin=0.115;
xmax=0.135;
ymin=0.115;
ymax=0.135;
zmin=0.243;
zmax=0.250;

/* Begin loop to determine the temperature at the centroid of cells near the thermocouple */
nt=0;
thread_loop_c(t,d)
{
begin_c_loop(c,t)
{
C_CENTROID(thermosensor_coordinate,c,t);

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

if ((x >= xmin) && (x <= xmax))
{
if ((y >= ymin) && (y <= ymax))
{
if ((z >= zmin) && (z <= zmax))
{
printf("X-cord = %f\n",x);
printf("y-cord = %f\n",y);
printf("Z-cord = %f\n",z);
thermosensor_temperature=thermosensor_temperature + C_T(c,t); /* get thermocouple temperature */
printf("thermosensor_temperature = %f\n",thermosensor_temperature);
nt=nt+1.0; /* count number */
printf("value_nt = %d\n",nt);
}
}
}
}
end_c_loop(c,t)
}
thermosensor_temperature=thermosensor_temperature/nt;
printf("final_temp = %f\n",thermosensor_temperature);
p = thermosensor_temperature;
}


DEFINE_SOURCE(heater,c,t,ds,eqn)
{
real cutoff_temperature;
real source;

cutoff_temperature = 301.0;

if (p <= cutoff_temperature)
{
source = 327680;
}

else
{
source = 0;
}
return source;
}



I dont know what might be the problem

can u plz help me

Thanks in advance

ghost82 September 15, 2015 09:30

Look at udf in message #4 of the other post.
I think you have to reinizialize also "p" in the first block (just after of before initializing nt, for example), what I'm calling thermosensor_temperature
Code:

thermosensor_temperature=0.0;
If it doesn't work, adapt your udf to that of message #4, because another difference is that thermosensor_temperature is initialized in first block, but I was passing its value to thermosensor_temperature1 before going to the second block.

hariswch2 September 15, 2015 10:02

Quote:

Originally Posted by ghost82 (Post 564087)
Look at udf in message #4 of the other post.
I think you have to reinizialize also "p" in the first block (just after of before initializing nt, for example), what I'm calling thermosensor_temperature
Code:

thermosensor_temperature=0.0;
If it doesn't work, adapt your udf to that of message #4, because another difference is that thermosensor_temperature is initialized in first block, but I was passing its value to thermosensor_temperature1 before going to the second block.

.................................................. ...............................................

As you told, "p" i defined as global variable and reinitialized as p=0 before the command nt=0 but still the temperature is increasing mode only( heater on only)

i tried to print "p" values in second block but it was showing zero and same "p" value i printed in first block, it was showing correct value.

the problem what i thought was '"p" value is not assigning from first block to second block, even though it is global variable.

Can u plz identify the problem....

ghost82 September 15, 2015 10:08

Try to declare nt as real, not integer, maybe the division returns 0 if nt is defined as int.
can you also change name p to something else?maybe 'p' is reserved?


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