CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Fluent UDF and Scheme Programming (https://www.cfd-online.com/Forums/fluent-udf/)
-   -   Give comment to a heater inside the model (https://www.cfd-online.com/Forums/fluent-udf/143089-give-comment-heater-inside-model.html)

ahvz October 16, 2014 10:12

Give comment to a heater inside the model
 
The target of my code is to give command to a heat source. in fact, I gave the coordinate of a node from the model which provides limits of heater on and off modes according to the specific command (when the temperature at node is less than that). but I received error when I use this code:

Code:

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


#include "udf.h"
#include "cxndsearch.h"
#include "dpm.h"
#include "math.h"


DEFINE_EXECUTE_AT_END(tsensor)
{
 cell_t c;
 Thread *t;
 CX_Cell_Id *cx_cell;
 real NV_VEC(pt);
 real pos[3];
 real posd[3];
 real xmin=-0.195;
 real ymin=0.0966492;
 real zmin=0.345;
 real q=5000000;
 NV_D(pt,=,xmin,ymin,zmin);
 pos[0]=xmin;
 pos[1]=ymin;
 pos[2]=zmin;
SV_locate_point(pos,cx_cell);
 c=cx_cell->ct.c;
t=cx_cell->ct.t;
if (C_T(c,t)<=294.15)
 {
 q=5000000.0;
 }
 else
 {
 q=0.0;
 }
}
DEFINE_SOURCE(heater_generation,c,t,dS,eqn)
{
real source;
source=q;
dS[eqn]=0.0;
return source;
}



error

Error:
received fatal signal (ACCESS_VIOLATION)
1. Note exact events leading to error.
2. Save case/data under new name.
3. Exit program and restart to continue.
4. Report error to your distributor.
Error Object: #f

any helps please?

Sun October 17, 2014 02:01

My guess is "q" should be defined as a global argument and with the current format DEFINE_SOURCE macro does not have access to the value of q.

ahvz October 18, 2014 07:56

Thanks for your reply,

I just found the below code interesting and seems to be adequate for my purpose (not sure yet). it works! without any error.

however, I do not understand why the command of OFF mode does not work!

in fact, the heater is ON from the beginning of analysis (since the temperature at sensor is lower than that defined for command of the heater ). so it is OK. but, it is still ON when the trumpeter goes upper than cut OFF temperature! do not know why. can you assist me on this trouble please?

Code:


#include "udf.h"
/* Obtain the mean temperature at the location of thermocouple */
/* Thermocouple located at coordinates (x,y,z) = (-0.195,0.0966492,0.345) */
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.195;
 xmax=-0.197;
 ymin=0.0966492;
 ymax=0.0968492;
 zmin=0.345;
 zmax=0.347;
 /* 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=294.15;
 
 heater_off = 0.0;
 heater_on = 5000000.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(f,t)
 }
}


ghost82 October 18, 2014 08:49

I would add a "Message" line in the DEFINE_PROFILE macro to print on the console the value of thermosensor_temperature, if its value is correct.
I would move real thermosensor_temperature; right after #include "udf.h" and delete the 2 other declarations inside DEFINE_PROFILE and DEFINE_EXECUTE_AT_END.
That variable needs to be global to be accessed both by DEFINE_PROFILE and DEFINE_EXECUTE_AT_END.
I think you also need to initialize somewhere the thermosensor_temperature, otherwise it could continue increasing.

Try this, I introduced a new global variable that can be accessed by both macros, so to be able to initialize thermosensor_temperature (I don't have tested it)

Code:

#include "udf.h"
real thermosensor_temperature1;
/* Obtain the mean temperature at the location of thermocouple */
/* Thermocouple located at coordinates (x,y,z) = (-0.195,0.0966492,0.345) */
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.195;
 xmax=-0.197;
 ymin=0.0966492;
 ymax=0.0968492;
 zmin=0.345;
 zmax=0.347;
 thermosensor_temperature=0.0;
 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(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;
 thermosensor_temperature1=thermosensor_temperature;
}
DEFINE_PROFILE(heater_bc,t,i)
{
 real cutoff_temperature;
 real heater_on;
 real heater_off;
 
 face_t f;
 cutoff_temperature=294.15;
 
 heater_off = 0.0;
 heater_on = 5000000.0;
 if (thermosensor_temperature1<=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)
 }
}

Daniele

ahvz February 6, 2015 04:53

Hi,

I tried to use this code but I think the "loop" does not work properly.

at the first time step the heater is ON mode regardless initial temperature. and in the remain steps the heater is OFF. help please.




Code:

#include "udf.h"
/* Obtain the mean temperature at the location of thermocouple */
/* Thermocouple located at coordinates (x,y,z) = (-0.195,0.0966492,0.345) */
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.195;
 xmax=-0.2;
 ymin=0.090;
 ymax=0.091;
 zmin=0.33;
 zmax=0.39;
 /* 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=294.15;
 
 heater_off = 0.0;
 heater_on = 1000.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(f,t)
 }
}


ghost82 February 6, 2015 04:56

Quote:

Originally Posted by ahvz (Post 530679)
Hi,

I tried to use this code but I think the "loop" does not work properly.

at the first time step the heater is ON mode regardless initial temperature. and in the remain steps the heater is OFF. help please.

Hi,
I saw that you quoted an udf different from the last udf I posted, in which I introduced a global variable.
Can you test udf in message #4?
http://www.cfd-online.com/Forums/flu...tml#post514940

PS: This code must be used in serial

ahvz February 6, 2015 05:07

Thanks for your attentions,

Now I am running the message#4...

ahvz February 6, 2015 05:19

Still have same problem with UDF message#4 (but I changed the coordinates of sensor placement only) as below:

Code:

#include "udf.h"
real thermosensor_temperature1;
/* Obtain the mean temperature at the location of thermocouple */
/* Thermocouple located at coordinates (x,y,z) = (-0.195,0.0966492,0.345) */
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.195;
 xmax=-0.2;
 ymin=0.090;
 ymax=0.091;
 zmin=0.33;
 zmax=0.39;
 thermosensor_temperature=0.0;
 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(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;
 thermosensor_temperature1=thermosensor_temperature;
}
DEFINE_PROFILE(heater_bc,t,i)
{
 real cutoff_temperature;
 real heater_on;
 real heater_off;
 
 face_t f;
 cutoff_temperature=294.15;
 
 heater_off = 0.0;
 heater_on = 1000.0;
 if (thermosensor_temperature1<=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)
 }
}


What I expected from this code is to control the heater when the temperature at sensor placement is lower than cut off temeprature the heater treats as ON and when the temperature at sensor placement is above cut off temperature the heater treats as OFF.

I used a graph to monitor the temperature variations of the heater and it shown that the heater is ON at first time step and then OFF till end of analysis...
what is your opinion?

Regards,

ghost82 February 6, 2015 05:21

Can you share the cas file?
It will be simpler to me to analyze where are the errors.
If you want you can upload it somewhere and send me a pm to download it.

ghost82 February 6, 2015 07:49

Hi,
I opened your cas file and I received a warning, and I'm not sure if it's ok or not.
The warning is:
Warning: zone of type interior found between different solids!
Material of cell zone 11 is refm, while material of cell zone 13 is xps.
This will adversely affect the solution.
It is recommended that you fix this issue via the TUI command
/mesh/modify-zones/slit-interior-between-diff-solids

So, I decided to test the udf of message #4 with a simple test case: a cube with an inlet, an outlet and 4 walls (one of them is the heater), and I put the T sensor in the center of the cube.

By initializing the domain with different temperatures I can see if the heater is switching on or off.

Some insights:
DEFINE_EXECUTE_AT_END executes at the end of each time step (not iteration): if you have the cut off temperature (in udf) > initialization temperature (fluent gui) the heater will be however on at first time step (whilst it should be off); this is because thermosensor_temperature1 is not initialized and it has a not determined value for the first time step (it should be a value of zero). To avoid this you can add a line after real thermosensor_temperature1, such as:

if you want the heater off for the first time step:
Code:

#include "udf.h"
real thermosensor_temperature1;
thermosensor_temperature1=296.0;

the 296.0 value is an arbitrary value > cut off temperature, to initialize for the first time step the heater to be off.
In the next time steps this value will be overwritten by looping the cells.
If you want the heater on for the first time step the macro is ok as it is or (better) you can initialize thermosensor_temperature1 with a value < cut off temperature, such as:
Code:

#include "udf.h"
real thermosensor_temperature1;
thermosensor_temperature1=290.0;

Apart this small issue, the udf works correctly.

I think your problem could be the min/max coordinates, expecially the y values: you set:
ymin=0.090
ymax=0.091

EDIT: it seems you have 4 values inside the range you specified, so this is not a problem.
Problem is in the next message: just switch the xmin/xmax values.

-------------------------------
Are you sure that in this range there are cell centered values? if your mesh is coarser in that region than 1 mm you have not cell centered values, you will never enter the loop, and the thermosensor_temperature1 will remain at a not determined value: 0.0/0.0.

Printing on the console the temperature of the sensor for your cas will give you:
-1.#IND00
-------------------------------

ghost82 February 6, 2015 08:11

...And here the issue:

Code:

xmin=-0.195;
 xmax=-0.2;
 ymin=0.090;
 ymax=0.091;
 zmin=0.33;
 zmax=0.39;

your xmax is < xmin!!!!!
Just switch xmin/xmax

ahvz February 6, 2015 10:17

Many many thanks for your excellent assistant on my problem. you right! my problem was the coordinate's values. I double checked them all and now they are correct and also the heater is getting command from the UDF.

Without your help I could not solve this problem. and you understood it very well.

Kind regards,

kams September 22, 2015 06:04

heatflux on and off
 
Quote:

Originally Posted by ghost82 (Post 530719)
Hi,
I opened your cas file and I received a warning, and I'm not sure if it's ok or not.

thermosensor_temperature1, such as:

if you want the heater off for the first time step:
Code:

#include "udf.h"
real thermosensor_temperature1;
thermosensor_temperature1=296.0;

the 296.0 value is an arbitrary value > cut off temperature, to initialize for the first time step the heater to be off.
In the next time steps this value will be overwritten by looping the cells.
If you want the heater on for the first time step the macro is ok as it is or (better) you can initialize thermosensor_temperature1 with a value < cut off temperature, such as:
Code:

#include "udf.h"
real thermosensor_temperature1;
thermosensor_temperature1=290.0;

Apart this small issue, the udf works correctly.



-------------------------------

Hi Ghost82,

you have explained clearly regarding UDF on heater.

even i tried the same as above example. As i compile the above program it is showing error.
..\..\src\print_27.c(3) : error C2371: 'thermosensor_temperature1' : redefinition; different basic types
..\..\src\print_27.c(2) : see declaration of 'thermosensor_temperature1'

i think this error is due to defining thermosensor_temperature1=296;
if we don't define the above line it wont get initialize by the above temperature and the program gets compiled without any error .

but the program runs as heater on condition without cutting off the temperature and heat flux.

please let me know what could be the issue on same

Best regards
Kams

pakk September 22, 2015 10:02

The problem could be that you did not copy the text literally.

Did you by chance type this:

Code:

#include "udf.h"
real thermosensor_temperature1;
real thermosensor_temperature1=296.0;


kams September 23, 2015 02:00

Quote:

Originally Posted by pakk (Post 565156)
The problem could be that you did not copy the text literally.

Did you by chance type this:

Code:

#include "udf.h"
real thermosensor_temperature1;
real thermosensor_temperature1=296.0;


Thanks for replying Mr.pakk
dint consider real in third line.

Please let me know what could be the issues.

Best regards
Kams


All times are GMT -4. The time now is 18:25.