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

Computing Average Temperature using DEFINE_ADJUST macro

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree2Likes
  • 1 Post By T81
  • 1 Post By kirmaks

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 17, 2009, 07:54
Default Computing Average Temperature using DEFINE_ADJUST macro
  #1
T81
New Member
 
Join Date: Mar 2009
Posts: 15
Rep Power: 17
T81 is on a distinguished road
Hi!

I have the following 2D channel geometry, like my previous post:

..............____________________________
heat --> |............................................ |
flux ---> |____________________________|

fluid ---> ----------------axis--------------------
..............____________________________
heat --> |............................................ |
flux ---> |____________________________|

I want to compute the average temperature of the walls (solid), in a transient analysis.

First, I define a User-Defined Memory (Define-->User-Defined-->Memory...)
Then I patch the value 1 for the User Memory O to the Solid Zone.

Then I wrote the following code using the DEFINE_ADJUST macro, hooked it, but when I start the iterations I get:

"Error:
FLUENT 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: ()
"

What's wrong in my code?

Thanks in advance.

Explaining the code: In every iteration, if a region has a value > 0.5 for UDMI 0 (solid has the value 1, fluid 0), the code computes the sum of temperatures for this region, and then the average value.

Code:
/***************************************************/
/* User-Defined Function for computing Solid Average Temperature  */
/***************************************************/

#include "udf.h"

DEFINE_ADJUST(my_adjust,d)
{
  real n = 0.;
  real Tsum = 0.;
  real T_AVG = 0.;
  
  Thread *t;
  cell_t c;
  real flag = C_UDMI(c,t,0);

 thread_loop_c(t,d)
 {
   begin_c_loop(c,t)
   if (flag > 0.5)
  {
    Tsum = Tsum + C_T(c,t);
     n = n + 1;
  }
   end_c_loop(c,t)
 }
  
  T_AVG = Tsum/n;
}
aestas likes this.
T81 is offline   Reply With Quote

Old   March 23, 2009, 07:33
Default
  #2
Member
 
Henrik Ström
Join Date: Mar 2009
Posts: 33
Rep Power: 17
HenrikS is on a distinguished road
Hi,

I think the problem might be that you are trying to assign a value for "flag" when c and t are not yet defined. Change

real flag = C_UDMI(c,t,0);

to

real flag;

and write

flag = C_UDMI(c,t,0);

on a new line after

begin_c_loop(c,t)
{

(where c and t are known).
Hope this helps!

/Henrik
HenrikS is offline   Reply With Quote

Old   March 23, 2009, 07:41
Default
  #3
T81
New Member
 
Join Date: Mar 2009
Posts: 15
Rep Power: 17
T81 is on a distinguished road
Thanks HenrikS for your answer,
I resolved this problem, just needed to have the real temp_avg = 0.; outside the DEFINE_ADJUST loop.

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

Now I want to do the following case:

If temp_avg > 700 then for the next 100 secs heat flux is 0.

Just need help to write this block of code, but I'm confused with this "for the next 100 secs" thing...

Thanks in advance.
T81 is offline   Reply With Quote

Old   March 23, 2009, 10:32
Default
  #4
Member
 
Henrik Ström
Join Date: Mar 2009
Posts: 33
Rep Power: 17
HenrikS is on a distinguished road
I suggest you define yet another global variable (outside the DEFINE_ADJUST-macro) that stores the amount of time for which to turn off the heat flux. For example:

real time_heat_flux_zero = 0.0;

Then, when temp_avg > 700, set

time_heat_flux_zero = CURRENT_TIME + 100.0;

and then add an if-statement:

if (time_heat_flux_zero > 0.0)
{
/* do what you wish to do here... */
time_heat_flux_zero -= CURRENT_TIMESTEP;
}
else
{
/* check if temp_avg > 700 etc */
}

I'm not sure if this will work satisfactory with adaptive time stepping, but I hope you get what I'm trying to say :-). Good luck!

/Henrik
HenrikS is offline   Reply With Quote

Old   March 23, 2009, 12:13
Default
  #5
T81
New Member
 
Join Date: Mar 2009
Posts: 15
Rep Power: 17
T81 is on a distinguished road
HenrikS thanks again for the reply,
i defined the time_heat_flux_zero global variable and I'm posting the current block code:

Code:
DEFINE_PROFILE(q_profile, thread, position)
{
  real r[3];  /* this will hold the position vector */
  real x;
  face_t f;
  
  begin_f_loop(f, thread)
  {  
    if (temp_avg > 700)
    {
     time_heat_flux_zero = CURRENT_TIME + 500.0;
     if (time_heat_flux_zero > 0.0)
{
/* do what you wish to do here... */
     F_PROFILE(f, thread, position) = 0;
     time_heat_flux_zero -= CURRENT_TIMESTEP;
}
    }
   else
   {
     F_CENTROID(r,f,thread);
     x = r[0];
     F_PROFILE(f, thread, position) = QMAX;
   }
  }  
  end_f_loop(f, thread)
}
It doesn't work.
Am I doing something wrong?
T81 is offline   Reply With Quote

Old   March 24, 2009, 04:06
Default
  #6
Member
 
Henrik Ström
Join Date: Mar 2009
Posts: 33
Rep Power: 17
HenrikS is on a distinguished road
I was thinking that in the DEFINE_ADJUST-macro, you check whether the average temperature is > 700 and, if that is the case, set the time_heat_flux_zero-variable to CURRENT_TIME + 500:

if (temp_avg > 700)
{
time_heat_flux_zero = CURRENT_TIME + 500.0;
}

Then, your DEFINE_PROFILE-macro will become:

DEFINE_PROFILE(q_profile, thread, position)
{
real r[3]; /* this will hold the position vector */
real x;
face_t f;

begin_f_loop(f, thread)
{
if (time_heat_flux_zero > 0.0)
{
F_PROFILE(f, thread, position) = 0;
time_heat_flux_zero -= CURRENT_TIMESTEP;
}
else
{
F_CENTROID(r,f,thread);
x = r[0];
F_PROFILE(f, thread, position) = QMAX;
}
}
end_f_loop(f, thread)
}

Note that this will reset the counter to 500 seconds every time temp_avg exceeds 700. I'm not exactly sure if this is what you want, but it sounds reasonable I think.

As the macro was written in your last post, I think the problem was that no matter what the time_heat_flux_zero-variable was, there came a F_PROFILE call for QMAX nonetheless (overwriting the 0 in F_PROFILE from earlier).

Hope this solves your problems!

/Henrik
HenrikS is offline   Reply With Quote

Old   March 24, 2009, 06:08
Default
  #7
T81
New Member
 
Join Date: Mar 2009
Posts: 15
Rep Power: 17
T81 is on a distinguished road
HenrikS thanks again for the quick reply,
this is exactly what I want to do.

I thing it's very close to solve the problem.

I tried the code you posted before but the heat flux doesn't set to 0. Temperature is raising all the time.

The code does two things:

1. Computes the average temperature of the solid in the first iteration of each timestep and stores the value in a User Defined Memory.

2. If this temperature exceeds a value (i.e 700K), heat flux is set to O for the next 500 seconds, else heat flux = max.

Thanks in advance.


P.S you wrote time_heat_flux_zero -= CURRENT_TIMESTEP; above, I think the right is time_heat_flux_zero -= CURRENT_TIME; so I change it.

I'm posting the whole code here:

Code:
/********************/
/* User-Defined Function */
/*******************/
#include "udf.h"
#define QMAX 536000.
int last_ts = -1;   /*  Global variable.  Time step is never <0 */
real temp_avg = 0.;
real time_heat_flux_zero = 0.;

DEFINE_ADJUST(my_adjust,d)
{
  real n = 0.;
  real temp_sum = 0.;
  
  Thread *t;
  cell_t c;
      
  int curr_ts;
  curr_ts = N_TIME;
  if (last_ts != curr_ts)
  {
    last_ts = curr_ts;

    /* things to be done only on first iteration of each time step 
       can be from here.......  */
  
 thread_loop_c(t,d)
 {
   real flag = C_UDMI(c,t,0);
   begin_c_loop(c,t)
   if (flag > 0.85)
  {
    temp_sum += C_T(c,t);
    n = n+1;
  }
   end_c_loop(c,t)
 }
  temp_avg = temp_sum/n;
  if (temp_avg > 700)
{
  time_heat_flux_zero = CURRENT_TIME + 500.0;
}
  printf("\n  -----------------------------------");
  printf("\n |Average Solid Temperature = %g|\n",temp_avg);
  printf("  -----------------------------------\n");
    printf("\n  -----------------------------------");
    printf("\n |Time = %g|\n",time_heat_flux_zero);
    printf("  -----------------------------------\n");
    /* .......to here  */
}
}

DEFINE_PROFILE(q_profile, thread, position)
{
 real r[3];  /* this will hold the position vector */
 real x;
 face_t f;

 begin_f_loop(f, thread)
 {
    if (time_heat_flux_zero > 0.0)
    {
       F_PROFILE(f, thread, position) = 0;
       time_heat_flux_zero -= CURRENT_TIME;
    }
    else
    {
       F_CENTROID(r,f,thread);
       x = r[0];
       F_PROFILE(f, thread, position) = QMAX;
    }
 }
 end_f_loop(f, thread)
}
T81 is offline   Reply With Quote

Old   March 24, 2009, 06:24
Default
  #8
Member
 
Henrik Ström
Join Date: Mar 2009
Posts: 33
Rep Power: 17
HenrikS is on a distinguished road
I think I messed it up a little bit. The problem is that DEFINE_PROFILE might be called several times in one time step, whereas I believe DEFINE_ADJUST is only called one time. Therefore, change:

if (temp_avg > 700)
{
time_heat_flux_zero = CURRENT_TIME + 500.0;
}

in DEFINE_ADJUST to:

if (temp_avg > 700)
{
time_heat_flux_zero = CURRENT_TIMESTEP + 500.0;
}
if (time_heat_flux_zero > 0.0)
{
time_heat_flux_zero -= CURRENT_TIMESTEP;
}

And then, in DEFINE_PROFILE, simply remove

time_heat_flux_zero -= CURRENT_TIME;


What code now does is:

In DEFINE_ADJUST:
1) calculate average temperature
2) if average temperature > 700 set time_heat_flux_zero to 500 seconds + size of current time step
3) if time_heat_flux_zero > 0.0 reduce time_heat_flux_zero with size of current time step

In DEFINE_PROFILE:
1) if time_heat_flux_zero > 0.0 set heat flux to zero, otherwise to QMAX

The thing is that time_heat_flux_zero should be reduced down to 0 with the current time step size once every time step, and during the time when time_heat_flux_zero is nonzero, the heat flux should be zero. I now hope this works.

/Henrik
HenrikS is offline   Reply With Quote

Old   March 24, 2009, 06:30
Default
  #9
Member
 
Henrik Ström
Join Date: Mar 2009
Posts: 33
Rep Power: 17
HenrikS is on a distinguished road
If it is more logical, then it is of course possible to switch around

if (temp_avg > 700)
{
time_heat_flux_zero = CURRENT_TIMESTEP + 500.0;
}
if (time_heat_flux_zero > 0.0)
{
time_heat_flux_zero -= CURRENT_TIMESTEP;
}

in DEFINE_ADJUST to:

if (time_heat_flux_zero > 0.0)
{
time_heat_flux_zero -= CURRENT_TIMESTEP;
}
if (temp_avg > 700)
{
time_heat_flux_zero = 500.0;
}

This will reduce time_heat_flux_zero with the size of the current time step down to 0, but reset it to 500 if the average temperature exceeds 700. (i.e. it is exactly the same as in my previous post, only a bit more transparent) :-)
HenrikS is offline   Reply With Quote

Old   March 24, 2009, 06:41
Default
  #10
T81
New Member
 
Join Date: Mar 2009
Posts: 15
Rep Power: 17
T81 is on a distinguished road
HenrikS you rule!!!
It works perfect!!!!

Thanks again for the support.

It's really very nice to educate yourself through this process.
Thanks again.
T81 is offline   Reply With Quote

Old   October 3, 2017, 19:31
Default
  #11
New Member
 
sina sadighi
Join Date: Jul 2017
Posts: 5
Rep Power: 8
sinasadighi is on a distinguished road
Quote:
Originally Posted by HenrikS View Post
If it is more logical, then it is of course possible to switch around

if (temp_avg > 700)
{
time_heat_flux_zero = CURRENT_TIMESTEP + 500.0;
}
if (time_heat_flux_zero > 0.0)
{
time_heat_flux_zero -= CURRENT_TIMESTEP;
}

in DEFINE_ADJUST to:

if (time_heat_flux_zero > 0.0)
{
time_heat_flux_zero -= CURRENT_TIMESTEP;
}
if (temp_avg > 700)
{
time_heat_flux_zero = 500.0;
}

This will reduce time_heat_flux_zero with the size of the current time step down to 0, but reset it to 500 if the average temperature exceeds 700. (i.e. it is exactly the same as in my previous post, only a bit more transparent) :-)
hi
i need Ur help ,
i want a code that put outlet temperatrue average in 5 iteration into inlet in a channel.
how could i write it .
sinasadighi is offline   Reply With Quote

Old   October 9, 2017, 10:07
Default
  #12
Member
 
KirMaks
Join Date: Aug 2016
Posts: 34
Rep Power: 9
kirmaks is on a distinguished road
Quote:
Originally Posted by sinasadighi View Post
hi
i need Ur help ,
i want a code that put outlet temperatrue average in 5 iteration into inlet in a channel.
how could i write it .
Hallo,

You may define several (five) scheme variables in FLUENT for the temperature and update them after every iteration with the average outlet temperature (check rpsetvar, rpgetvar). Then use DEFINE_PROFILE and read these variables there for Your BCs. I think it should work.

Regards, Maks
sinasadighi likes this.
kirmaks is offline   Reply With Quote

Old   October 14, 2017, 02:47
Default
  #13
New Member
 
sina sadighi
Join Date: Jul 2017
Posts: 5
Rep Power: 8
sinasadighi is on a distinguished road
Quote:
Originally Posted by kirmaks View Post
Hallo,

You may define several (five) scheme variables in FLUENT for the temperature and update them after every iteration with the average outlet temperature (check rpsetvar, rpgetvar). Then use DEFINE_PROFILE and read these variables there for Your BCs. I think it should work.

Regards, Maks
hi
I wrote it , but there is a problem , how could i connect this two macro ?
#include "udf.h"
real ave_T;
int s ;
DEFINE_ADJUST(temp_ave, domain)
{
int ID = 11;
FILE *output;
Thread *thread=Lookup_Thread(domain, ID);
real total_temp=0;
real ave_T , T;
int i=0;
face_t face;

begin_f_loop(face, thread)
{
T = F_T(face, thread);
total_temp = total_temp + T;
i = i+1;
s=i/5;
}
end_f_loop(face, thread)

ave_T = (total_temp / i) ;

printf("Outlet Average Temperature : %f\n" , ave_T);

output = fopen("Outlet Average Temerature.txt","a");
fprintf(output, "Outlet Average Temerature: %f\n", ave_T);
fclose(output);
}

DEFINE_PROFILE(my_temp, thread, i)
{
real z ;
real ave_T ;
face_t face;
real r[ND_ND];
begin_f_loop(face, thread)
{
if (s=0)
{
F_CENTROID(r, face, thread) = ave_T ;
z=r[2];
}
}
end_f_loop(face, thread)
}
sinasadighi is offline   Reply With Quote

Old   October 16, 2017, 07:58
Default
  #14
Member
 
KirMaks
Join Date: Aug 2016
Posts: 34
Rep Power: 9
kirmaks is on a distinguished road
Hallo,

I think I have meant something a little different. But You can develop Your own solution further.

I have noticed one small mistake:You have declared a variable s as an integer and then You initialise it with i/5, it will definitely cause errors. Other parts I haven't check carefully.

You may try to hook the first part of Your UDF to the model and see if You get the proper value of the average temperature in the file. Then You need to think how to update it with the five consequent values.

You also need to open this file in DEFINE_PROFILE, I'm not sure that these file i/o operations will be completely safe and fast.

I have offered to declare additional scheme variables in FLUENT and update them with DEFINE_AJUST. You don't need to make file writing/reading operations then.

Regards, Maksim

Quote:
Originally Posted by sinasadighi View Post
kirmaks is offline   Reply With Quote

Reply

Tags
define_adjust, temperature average value, temperature summary value, udmi

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
TEMPERATURE-AVERAGE mech FLUENT 1 April 25, 2007 17:06
Temperature Average Danny FLUENT 0 May 3, 2005 06:47
Define_Adjust Macro Mahesh FLUENT 0 November 10, 2004 06:54
average temperature mehravar FLUENT 2 January 1, 2004 01:43
Bug in the DEFINE_ADJUST macro? Senthil FLUENT 5 September 3, 2002 23:26


All times are GMT -4. The time now is 06:11.