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

UDF for energy source term

Register Blogs Community New Posts Updated Threads Search

Like Tree6Likes
  • 3 Post By ComputerGuy
  • 3 Post By ComputerGuy

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   February 5, 2012, 15:46
Default UDF for energy source term
  #1
New Member
 
Join Date: Feb 2012
Posts: 3
Rep Power: 14
engloly is on a distinguished road
in my heat transfer model i want to write a udf for the energy source term
S y = - density * dH\dt
where H= the total enthalpy , t= time

my udf is ;

#include "udf.h"
#define dens 800.0
DEFINE_SOURCE(heat source,cell,thread,dS,eqn)
{
real source ;
source= - C_H-G(cell,thread)*dens ;
dS [eqn] =0 ;
return source ;
}


can any one help me to , i dont know if the cell enthaly gredient is equal to the chane of enthalpy with time?
engloly is offline   Reply With Quote

Old   February 25, 2012, 09:26
Default
  #2
New Member
 
Join Date: Feb 2012
Posts: 3
Rep Power: 14
engloly is on a distinguished road
im waiting for ur help???
engloly is offline   Reply With Quote

Old   February 28, 2012, 08:36
Default
  #3
New Member
 
Philipp Schapotschnikow
Join Date: Oct 2010
Posts: 11
Rep Power: 15
Philipp_Sch is on a distinguished road
No, the gradient is a spacial derivative. Besides, it is a vector. You need a time derivative, which YOU have to provide.
Philipp_Sch is offline   Reply With Quote

Old   March 1, 2012, 21:57
Default
  #4
New Member
 
Join Date: Feb 2012
Posts: 3
Rep Power: 14
engloly is on a distinguished road
yes u are right
but how can i write the dh/dt?
engloly is offline   Reply With Quote

Old   March 6, 2012, 16:49
Default
  #5
Senior Member
 
Micael
Join Date: Mar 2009
Location: Canada
Posts: 156
Rep Power: 18
Micael is on a distinguished road
I did this before. I don't have access to it right now, but here is the idea (I AM NOT SURE OF THE EXACT SYNTAX, I just write it roughly by memory):

1- compute/store H in a UDM with a "Adjust" UDF
2- then derive it in your Source UDF, something like that (first order in that example):
time_step = "macro for time step goes here";
dHdt = (UDM(0) - UDM_M1(0)) / time_step; // suppose you put H in UDM(0)

For what I remember, it was not that easy to implement, but the idea is there. Among all, make sure to understand exactly when "Adjust" is executed.

This line:
dS [eqn] =0 ;
can be a problem. Make sure to understand what it means, look at UDF guide. You may need to derive your source term to ever converge (depend on your problem). I always derive my source term, it is either safer or downright necessary (numerically).

Keep motivation high, this problem will be challenging if you are beginner, but it is feasible.

Last edited by Micael; March 7, 2012 at 17:50.
Micael is offline   Reply With Quote

Old   March 7, 2012, 16:40
Default
  #6
Senior Member
 
Real Name :)
Join Date: Jan 2010
Location: United States
Posts: 192
Rep Power: 16
ComputerGuy is on a distinguished road
Quote:
Originally Posted by engloly View Post
yes u are right
but how can i write the dh/dt?
Perhaps something like the following:

Make sure you enable a user-defined memory location... Also, I'm assuming you want the density at the current time step, not the average density between the two timesteps (current and previous). Also, enthalpy has a spatial gradient (x/y/z), but you want the gradient w.r.t. time.

This is a crude approximation of a derivative. Ideally, you should do some higher order approximations.

Finally: there is an "if" statement in there to ensure that, at the beginning of the simulation where you have no derivative information, no source term is calculated.

--ComputerGuy

Code:
#include "udf.h"
DEFINE_SOURCE(heat_source,cell,thread,dS,eqn)
{
	real source;
	source=0.0;
	real dHdt;
	dHdt=(C_H(cell,thread)-C_UDMI(cell,thread,0))/CURRENT_TIMESTEP;
	if(CURRENT_TIME>0)
	{
		source= -dHdt*C_R(cell,thread);
	}
	C_UDMI(cell,thread,0)=C_H(cell,thread);
	dS[eqn] =0.0;
	return source;
}
piyupant, Baden and dehai like this.
ComputerGuy is offline   Reply With Quote

Old   March 7, 2012, 18:00
Default
  #7
Senior Member
 
Micael
Join Date: Mar 2009
Location: Canada
Posts: 156
Rep Power: 18
Micael is on a distinguished road
Quote:
Originally Posted by ComputerGuy View Post
Perhaps something like the following:

Make sure you enable a user-defined memory location... Also, I'm assuming you want the density at the current time step, not the average density between the two timesteps (current and previous). Also, enthalpy has a spatial gradient (x/y/z), but you want the gradient w.r.t. time.

This is a crude approximation of a derivative. Ideally, you should do some higher order approximations.

Finally: there is an "if" statement in there to ensure that, at the beginning of the simulation where you have no derivative information, no source term is calculated.

--ComputerGuy

Code:
#include "udf.h"
DEFINE_SOURCE(heat_source,cell,thread,dS,eqn)
{
    real source;
    source=0.0;
    real dHdt;
    dHdt=(C_H(cell,thread)-C_UDMI(cell,thread,0))/CURRENT_TIMESTEP;
    if(CURRENT_TIME>0)
    {
        source= -dHdt*C_R(cell,thread);
    }
    C_UDMI(cell,thread,0)=C_H(cell,thread);
    dS[eqn] =0.0;
    return source;
}
I don't think that can work. The reason is that DEFINE_SOURCE should be called more than once per time step (many iterations to solve a given time step). Hence, this line should be remove:
Code:
 
C_UDMI(cell,thread,0)=C_H(cell,thread);
C_UDMI(cell,thread,0) should be compute only once per time step and an DEFINE_EXECUTE_AT_END (or was it an DEFINE_ADJUST? - I don't remember) can do this. I do remember that it was tricky, I could find those files this weekend.

Keep in mind that dS[eqn]=0 could lead to convergence problem, but it is ok to try it first like that.

Otherwise, ComputerGuy is right, DEFINE_SOURCE should looks similar to that.

Last edited by Micael; March 7, 2012 at 19:52.
Micael is offline   Reply With Quote

Old   March 7, 2012, 18:17
Default
  #8
Senior Member
 
Real Name :)
Join Date: Jan 2010
Location: United States
Posts: 192
Rep Power: 16
ComputerGuy is on a distinguished road
Quote:
Originally Posted by Micael View Post
I don't think that can work. The reason is that DEFINE_SOURCE should be called more than once per time step (many iterations to solve a given time step). Hence, this line should be remove:
Code:
 
C_UDMI(cell,thread,0)=C_H(cell,thread);
C_UDMI(cell,thread,0) should be compute only once per time step and an ADJUST can do this. I do remember that it was tricky, I could find those files this weekend.
Hmmm, you're right. Ok... Something like this ought to record the enthalpy once per timestep.

ComputerGuy

Code:
#include "udf.h"

DEFINE_EXECUTE_AT_END(ThisRunsAtEndOfTimestep)
{

  Domain *d;
  Thread *t;
  cell_t c;
  d = Get_Domain(1);   /* mixture domain if multiphase */

 thread_loop_c(t,d)
    {
     if (FLUID_THREAD_P(t))
       {
        begin_c_loop(c,t)
          {
           C_UDMI(c,t,0)=C_H(c,t);
         }
        end_c_loop(c,t)
       }
    }
}


DEFINE_SOURCE(heat_source,cell,thread,dS,eqn)
{
    real source;
    source=0.0;
    real dHdt;
    dHdt=(C_H(cell,thread)-C_UDMI(cell,thread,0))/CURRENT_TIMESTEP;
    if(CURRENT_TIME>0)
    {
        source= -dHdt*C_R(cell,thread);
    }
    dS[eqn] =0.0;
    return source;
}
DungPham, Tleja and Baden like this.
ComputerGuy is offline   Reply With Quote

Old   March 7, 2012, 19:51
Default
  #9
Senior Member
 
Micael
Join Date: Mar 2009
Location: Canada
Posts: 156
Rep Power: 18
Micael is on a distinguished road
Yes, that's it ComputerGuy!

Only left is the special case of the very first time step and the whole thing is adressed.
Micael is offline   Reply With Quote

Old   March 7, 2012, 20:13
Default
  #10
Senior Member
 
Real Name :)
Join Date: Jan 2010
Location: United States
Posts: 192
Rep Power: 16
ComputerGuy is on a distinguished road
Quote:
Originally Posted by Micael View Post
Yes, that's it ComputerGuy!

Only left is the special case of the very first time step and the whole thing is adressed.
I think, because the code runs at the end of the time step, it should be fine. I've also addressed this in the define_source macro, where I check to see that the time is > 0. If it isn't, it wouldn't be possible to obtain a derivative, and thus I set the source to zero.

engloly: Let us know if these work

ComputerGuy
ComputerGuy is offline   Reply With Quote

Old   March 8, 2012, 15:33
Default
  #11
Senior Member
 
Micael
Join Date: Mar 2009
Location: Canada
Posts: 156
Rep Power: 18
Micael is on a distinguished road
Still one detail, the UDM should be initialized so that during the first time step there is a value for it.
Micael is offline   Reply With Quote

Old   June 25, 2017, 06:05
Default Energy source term does not work!
  #12
New Member
 
mehran
Join Date: Mar 2017
Posts: 1
Rep Power: 0
mehrandadsetan is on a distinguished road
Dear all,

I wrote a udf for energy equation, but I think it depends on the magnitude of sigma_r.
For sigma_r equal to 3e-1, udf works well , but if you change it to 3e-3, udf doesn't work.
can anybody help me?

UDF code:

#include "udf.h"
#define PI 3.14159265
#define ei 40e-3
#define sigma_r 3e-3
#define sigma_t 0.5e-6
#define t0 0
DEFINE_SOURCE(Energy_source, c, ct, dS, eqn)
{
real source;
real pos[ND_ND];
real centre[ND_ND];
real flow_time;
real r;
NV_D(centre,=,0.8,0,0); /* Set a vector */
flow_time = CURRENT_TIME; /* Special Fluent macro */
C_CENTROID(pos,c,ct);
NV_VV(pos,=,pos,-,centre); /* Vector arithmetic */
r = NV_MAG(pos);
source = (ei/(4*pow(PI,2)*pow(sigma_r,3)*sigma_t))*exp(-0.5*pow((flow_time-t0)/sigma_t,2))*exp(-0.5*pow(r/sigma_r,2));
dS[eqn] = 0;
return source;
}
mehrandadsetan 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
Injection of the source term UDF stage81 FLUENT 0 September 27, 2010 07:20
UDF Source Term Christian FLUENT 4 August 1, 2009 05:53
The source term of UDF summer FLUENT 0 August 24, 2006 17:44
UDFs for Scalar Eqn - Fluid/Solid HT Greg Perkins FLUENT 0 October 13, 2000 23:03
UDFs for Scalar Eqn - Fluid/Solid HT Greg Perkins FLUENT 0 October 11, 2000 03:43


All times are GMT -4. The time now is 21:49.