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/)
-   -   Computing Average Temperature using DEFINE_ADJUST macro (http://www.cfd-online.com/Forums/fluent-udf/62686-computing-average-temperature-using-define_adjust-macro.html)

T81 March 17, 2009 07:54

Computing Average Temperature using DEFINE_ADJUST macro
 
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;
}


HenrikS March 23, 2009 07:33

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

T81 March 23, 2009 07:41

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.

HenrikS March 23, 2009 10:32

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

T81 March 23, 2009 12:13

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?

HenrikS March 24, 2009 04:06

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

T81 March 24, 2009 06:08

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)
}


HenrikS March 24, 2009 06:24

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 March 24, 2009 06:30

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) :-)

T81 March 24, 2009 06:41

HenrikS you rule!!!
It works perfect!!!!

Thanks again for the support. :)

It's really very nice to educate yourself through this process.
Thanks again.


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