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/)
-   -   Use DEFINE_ADJUST change boundary condition (https://www.cfd-online.com/Forums/fluent-udf/76237-use-define_adjust-change-boundary-condition.html)

liurengtong123 May 19, 2010 01:17

Use DEFINE_ADJUST change boundary condition
 
dear all friends:
now,i have some trouble in solving question about how to change boundary condition.details as follow:
boudary condition:heat flux q2=a1*qe^(-1/3)[Twall-268-4qe*(a2+a3+a4*qe^(1/3)))
qe is heat flux that Fluent compute itself.That is to say,i write a udf about boundary heat flux,and use macro DEFINE_ADJUST to modify boundary condition(heat flux) based on whether fabs((q2-qe)/q2)<=0.05.
it is my code:
#include "udf.h"
DEFINE_PROFILE(my_flux,thread,i)
{
face_t f;
Thread*t0=THREAD_T0(thread);
cell_t c0;
real a1,a2,a3,a4,T,qe,q2,xw[ND_ND],xc[ND_ND],dx[ND_ND],dy;
real flow_time,current_timestep;
a1=3.27e4;
a2=2.63e-4;
a3=2.08e-6;
a4=3.05987e-5;
flow_time=RP_Get_Real("flow-time");
current_timestep=CURRENT_TIMESTEP;
if(flow_time<=current_timestep)
{ begin_f_loop(f,thread)
{
F_PROFILE(f,thread,i)=-1000;
}end_f_loop(f,thread)
}
else
{ begin_f_loop(f,thread)
{
c0=F_C0(f,thread);
F_CENTROID(xw,f,thread);
C_CENTROID(xc,c0,t0);
NV_VV(dx,=,xc,-,xw);
dy=ND_MAG(dx[0],dx[1],dx[2]);
qe=(0.6*C_LIQF(c0,t0)+2.24*(1-C_LIQF(c0,t0)))*(F_T(f,thread)-C_T(c0,t0))/dy;/*heat flux computed through fluent itself*/
q2=a1*pow(qe,-1/3)*(F_T(f,thread)-268-4*qe*(a2+a3+a4*pow(qe,1/3)));/*gived boundary condition heat flux*/
if((fabs(q2-qe)/q2)<=0.2)/*judge q2 and qe */
F_PROFILE(f,thread,i)=0.6*qe+0.4*q2;
else if((fabs(q2-qe)/q2)<=0.1)
F_PROFILE(f,thread,i)=0.8*qe+0.2*q2;
else
F_PROFILE(f,thread,i)=q2;
}end_f_loop(f,thread)
}
}
temperature limited to 1.000000e+000 in 10000 cells on zone 2 in domain 1
iter continuity x-velocity y-velocity energy time/iter
62 2.3745e+09 4.9106e-02 8.2004e-02 5.1416e+19 0:00:06 18
temperature limited to 5.000000e+003 in 10000 cells on zone 2 in domain 1
63 3.2869e+08 6.5883e-02 1.1955e-01 4.4575e+25 0:00:08 17
temperature limited to 1.000000e+000 in 10000 cells on zone 2 in domain 1
64 2.5771e+09 5.0354e-02 8.0179e-02 3.3946e+22 0:00:06 16
temperature limited to 5.000000e+003 in 10000 cells on zone 2 in domain 1
65 2.6958e+08 8.3365e-02 1.4457e-01 3.2549e+28 0:00:05 15
Error: Floating point error: invalid number
Error Object: ()
how to do that? I need your help,

liurengtong123 May 23, 2010 21:57

about my question ,i will tell more details as follows:
I want to simulate water solidification in a tank. I will give a boundary condition(heat flux) and this boundary heat flux qe=f(qe).


liurengtong123 May 25, 2010 21:02

This is my code about DEFINE_ADJUST,i want to write a program that can link DEFINE_ADJUST and DEFINE_PROFILE in order to realize change boundary condition.please help me!!!Big thanks!!!
DEFINE_ADJUST(my_adjust,d)
{
int ID=6;
Thread*thread=Lookup_Thread(d,ID);
cell_t c;
cell_t c0;
face_t f;
Thread*t0=THREAD_T0(thread);
real a1,a2,a3,a4,T,qe,q2,xw[ND_ND],xc[ND_ND],dx[ND_ND],dy;
real flow_time,current_timestep;
a1=3.27e4;
a2=2.63e-4;
a3=2.08e-6;
a4=3.05987e-5;

if(!Data_Valid_P())
return;
thread_loop_f(thread,d)
{
begin_f_loop(f,thread)
{

c0=F_C0(f,thread);
F_CENTROID(xw,f,thread);
C_CENTROID(xc,c0,t0);
NV_VV(dx,=,xc,-,xw);
dy=ND_MAG(dx[0],dx[1],dx[2]);
qe=(0.6*C_LIQF(c0,t0)+2.24*(1-C_LIQF(c0,t0)))*(F_T(f,thread)-C_T(c0,t0))/dy;
q2=a1*pow(qe,-1/3)*(F_T(f,thread)-268-4*qe*(a2+a3+a4*pow(qe,1/3)));
if(fabs((q2-qe)/q2)<=0.05)
F_UDMI(f,thread,0)=q2;
else
F_UDMI(f,thread,1)=0.8*q2+0.2*qe;
}end_f_loop(f,thread)
}
}

ljp June 22, 2010 11:20

Hi,

I'm wondering if you have solved the problem you posted a while ago, or got any ideas on how to solve it. I have a much similar problem in trying to change the boundary condition by using either DEFINE_ADJUST or DEFINE_PROFILE. Thank you very much.

Regards,
ljp

om1234 June 22, 2010 16:26

hi
i'm not experienced with heat transfer and either i cant understand some parts of ur udf.but as i know:
1. c0 ,c1 r used when u wanna store/access to values of an interior faces and in boundary condition u dont need to do that and the values of the cell face is enoughg. [Thread*t0=THREAD_T0(thread);
cell_t c0;]
2.i can tell u if the int ID=6 is ur desired boundary u dont need to use define_adjust and u can write both of them in DEFINE_PROFILE.nevertheles to link the adjust to profile, u need to define the number of memory that u use in your problem e.g if your boundary is subdevided into 20 parts and u need to define qe in all of them so u should change the no. of memory(define>user define>memory) in fluent to 20 and use this order :for(k=0;k<n_udm;k++),F_UDMI(f,t,k) and if u store the q2 in memory in define_adjust and u wanna use it in define_profile u need to access to memory or vise versa:e.g
begin_f_loop(f,thread)
{
for(k=0;k<n_udm;k++)
c0=F_C0(f,thread);
F_CENTROID(xw,f,thread);
C_CENTROID(xc,c0,t0);
NV_VV(dx,=,xc,-,xw);
dy=ND_MAG(dx[0],dx[1],dx[2]);
qe=(0.6*C_LIQF(c0,t0)+2.24*(1-C_LIQF(c0,t0)))*(F_T(f,thread)-C_T(c0,t0))/dy;/*heat flux computed through fluent itself*/
q2=F_UDMI(f,t,k);
and the other lines.

om1234 June 22, 2010 16:32

although u can use printf("name:%f\n",value) to show the values in graph window.it's very helpfull

liurengtong123 June 23, 2010 03:56

I have no idea about my posted question ,i am confused.I feel it is very difficult.My boundary condition is a coupled condition.qe=f(qe).

TDi July 12, 2010 11:53

Can you elaborate?
 
Quote:

Originally Posted by liurengtong123 (Post 264145)
I have no idea about my posted question ,i am confused.I feel it is very difficult.My boundary condition is a coupled condition.qe=f(qe).

Can you help us understand what the trouble is? It is not clear where you are having difficulty.

Are you having difficulty setting up the boundary conditions? Do you want to know how to link your UDF to the problem? Is there a compile error? Do you need help with the math?

davesmith_01 July 12, 2010 12:18

Hi

Eventually I want to write a UDF for a pitching airfoil, which pitches up and down continuously. However I first want to learn about dynamic meshing in fluent, so I want to try the tutorial on fluent about cyl3d.msh, but I do not have this fiel, once I understand this I was going to write a code and try using the udf and dynamic meshing together, does anyone have cyl3d.msh?

Thanks

Or have you got a code for pitching an airfoil whilst oncoming flow is heading towards the airfoil? If I can have a look I will understand the process of writing this better

liurengtong123 July 12, 2010 22:57

Quote:

Originally Posted by TDi (Post 266929)
Can you help us understand what the trouble is? It is not clear where you are having difficulty.

Are you having difficulty setting up the boundary conditions? Do you want to know how to link your UDF to the problem? Is there a compile error? Do you need help with the math?

my problem is as follow:
First,i suppose boundary heat fllux q=constant,through macro DEFINE_PROFILE(f,thread) pass q to Fluent, every interation,i will judge if fabs(q2-qe)/qe<=0.05. if yes, continue to interate,if no, i want to change boundary heat flux as like q=02*qe+0.8*q2, then pass q to Fluent ,start a new interation,then judge again as above.
Now, my problem is that i do not know how to write a UDF reallize this process.How to change boundary heat flux through DEFINE_ADJUST macro.

liurengtong123 July 17, 2010 22:26

Quote:

Originally Posted by liurengtong123 (Post 266988)
my problem is as follow:
First,i suppose boundary heat fllux q=constant,through macro DEFINE_PROFILE(f,thread) pass q to Fluent, every interation,i will judge if fabs(q2-qe)/qe<=0.05. if yes, continue to interate,if no, i want to change boundary heat flux as like q=02*qe+0.8*q2, then pass q to Fluent ,start a new interation,then judge again as above.
Now, my problem is that i do not know how to write a UDF reallize this process.How to change boundary heat flux through DEFINE_ADJUST macro.

anybody help me!!I need your help !! This problem confused me for many months.

liurengtong123 July 29, 2010 02:54

Quote:

Originally Posted by liurengtong123 (Post 267855)
anybody help me!!I need your help !! This problem confused me for many months.

anybody can help me,i need your help, I hope somebody can help me!! Big thanks!!

liurengtong123 September 13, 2010 04:12

anybody can help me,i need your help, I hope somebody can help me!! Big thanks!!

Hathaway October 19, 2010 19:29

I believe I have a similar problem as you. I am trying to adjust a boundary condition each iteration based on the temperature of the faces along the boundary as calculated by the previous iteration. (e.g. the boundary condition is q=f(T), where yours is similar with q=f(q)). I was watching your thread for a solution, but it appears none have come.

I have found a means of fixing my problem that I will describe for you. My UDF contains both a DEFINE_PROFILE and a DEFINE_ADJUST macro.

I use the DEFINE_PROFILE macro as the set boundary condition when setting up the problem. The macro not only sets the boundary flux for the first iteration, but also reads and outputs the zone_id number and the property_id number associated with that boundary condition.

The DEFINE_ADJUST macro then reads the property_id and zone_id written in the DEFINE_PROFILE macro and looks up the appropriate thread pointer for the boundary of interest. Next, it loops through the boundary and reads the information needed to determine the new boundary flux (in my case it reads temperature, in your case, read whatever you need). Lastly it uses the F_PROFILE(f,t,i) macro to assign the new flux value, where 'i' is the property_id saved from the DEFINE_PROFILE macro earlier.

I've simplified down the code I'm using to post here. Now it acts as if i'm starting with an initial flux of 190 [W/m^2-K] then applying a heat flux to each face corresponding to a convection condition for a bulk fluid temperature of 375 [K] with a convection coefficient of 6 [W/m^2-K].
Code:

q = 6 * ( 375 - T )
After hooking both the DEFINE_PROFILE macro to the boundary condition, and the DEFINE_ADJUST macro to the entire simulation, everything seems to run as desired. The initial value is loaded for the first iteration, then each iteration thereafter obtains a newly calculated flux boundary condition.

Code:

#include "udf.h"

FILE *fid;

DEFINE_PROFILE(flux_setget,t,i)
{
  face_t f;
  int zoneid = THREAD_ID(t);

  /* Write the property and thread id values for the flux condition to file */
  fid = fopen("flux-ids", "w");
  fprintf(fid, "%d, %d\n", zoneid, i);
  fclose(fid);
 
  /* Set initial profile values */
  begin_f_loop(f,t)
  {
    F_PROFILE(f,t,i) = 190;
  }
  end_f_loop(f,t)
}

DEFINE_ADJUST(flux_adjuster, domain)
{
  int iprop;
  int izone;
  real temp;
  real newflux;
  Thread *t;
  face_t f;

  /* Read in the property and thread id values stored by the BC UDF */
  fid = fopen("flux-ids", "r");
  fscanf(fid,"%d, %d", &izone, &iprop);
  fclose(fid);

  /* Get the thread pointer based on the obtained zone id */
  t = Lookup_Thread(domain, izone);
 
  /* Read the needed data, then calculate and set the new flux values */
  begin_f_loop(f,t)
  {
    temp = F_T(f,t);
    newflux = 6 * (375 - temp);
    F_PROFILE(f,t,iprop) = newflux;
  }
  end_f_loop(f,t)
}

Hopefully something similar will work for you.

liurengtong123 November 16, 2010 07:56

my boundary heat flux is q=f(q).So,first of all ,i must assume boundary heat flux q=A(arbitrary constant).Fluent calculate itself after one timestep,judge q and f(q),if fabs([q-f(q)]/q)<=5%,so Fluent enter next timestep calculation;If fabs([q-f(q)]/q)>5%,so give boundary heat flux q1=0.7*q+0.3*f(q) to Fluent,Fluent calculate in the same timestep.
how to write UDF ?thanks

liurengtong123 November 23, 2010 01:36

Quote:

Originally Posted by Hathaway (Post 279878)
I believe I have a similar problem as you. I am trying to adjust a boundary condition each iteration based on the temperature of the faces along the boundary as calculated by the previous iteration. (e.g. the boundary condition is q=f(T), where yours is similar with q=f(q)). I was watching your thread for a solution, but it appears none have come.

I have found a means of fixing my problem that I will describe for you. My UDF contains both a DEFINE_PROFILE and a DEFINE_ADJUST macro.

I use the DEFINE_PROFILE macro as the set boundary condition when setting up the problem. The macro not only sets the boundary flux for the first iteration, but also reads and outputs the zone_id number and the property_id number associated with that boundary condition.

The DEFINE_ADJUST macro then reads the property_id and zone_id written in the DEFINE_PROFILE macro and looks up the appropriate thread pointer for the boundary of interest. Next, it loops through the boundary and reads the information needed to determine the new boundary flux (in my case it reads temperature, in your case, read whatever you need). Lastly it uses the F_PROFILE(f,t,i) macro to assign the new flux value, where 'i' is the property_id saved from the DEFINE_PROFILE macro earlier.

I've simplified down the code I'm using to post here. Now it acts as if i'm starting with an initial flux of 190 [W/m^2-K] then applying a heat flux to each face corresponding to a convection condition for a bulk fluid temperature of 375 [K] with a convection coefficient of 6 [W/m^2-K].
Code:

q = 6 * ( 375 - T )
After hooking both the DEFINE_PROFILE macro to the boundary condition, and the DEFINE_ADJUST macro to the entire simulation, everything seems to run as desired. The initial value is loaded for the first iteration, then each iteration thereafter obtains a newly calculated flux boundary condition.

Code:

#include "udf.h"
 
FILE *fid;
 
DEFINE_PROFILE(flux_setget,t,i)
{
  face_t f;
  int zoneid = THREAD_ID(t);
 
  /* Write the property and thread id values for the flux condition to file */
  fid = fopen("flux-ids", "w");
  fprintf(fid, "%d, %d\n", zoneid, i);
  fclose(fid);
 
  /* Set initial profile values */
  begin_f_loop(f,t)
  {
    F_PROFILE(f,t,i) = 190;
  }
  end_f_loop(f,t)
}
 
DEFINE_ADJUST(flux_adjuster, domain)
{
  int iprop;
  int izone;
  real temp;
  real newflux;
  Thread *t;
  face_t f;
 
  /* Read in the property and thread id values stored by the BC UDF */
  fid = fopen("flux-ids", "r");
  fscanf(fid,"%d, %d", &izone, &iprop);
  fclose(fid);
 
  /* Get the thread pointer based on the obtained zone id */
  t = Lookup_Thread(domain, izone);
 
  /* Read the needed data, then calculate and set the new flux values */
  begin_f_loop(f,t)
  {
    temp = F_T(f,t);
    newflux = 6 * (375 - temp);
    F_PROFILE(f,t,iprop) = newflux;
  }
  end_f_loop(f,t)
}

Hopefully something similar will work for you.

first of all,thanks for giving me help,i write my code as follows:
[CODE]#include "udf.h"
FILE*fid;DEFINE_PROFILE(heat_flux,t,i)
{
face_t f;
int zoneid=THREAD_ID(t);
fid=fopen("flux-ids","w");
fprintf(fid,"%d,%d\n",zoneid,i);
fclose(fid);
begin_f_loop(f,t)
{
F_PROFILE(f,t,i)=-1000;
}end_f_loop(f,t)
}[DEFINE_ADJUST(flux_adjust,domain)
{
int iprop;
int izone;
Thread*thread=Lookup_Thread(domain,izone);
Thread*t0=THREAD_T0(thread);
cell_t c;
cell_t c0;
face_t f;
real a1,a2,a3,a4,T,qe,q2,xw[ND_ND],xc[ND_ND],dx
[ND_ND],dy;
a1=3.27e4;
a2=2.63e-4;
a3=2.08e-6;
a4=3.06e-5;
fid=fopen("flux-ids","r");
fscanf(fid,"%d,%d",&izone,&iprop);
fclose(fid);
if(!Data_Valid_P())
return;thread_loop_f(thread,domain)
{
begin_f_loop(f,thread)
{
c0=F_C0(f,thread);
F_CENTROID(xw,f,thread);
C_CENTROID(xc,c0,t0);
NV_VV(dx,=,xc,-,xw);
dy=ND_MAG(dx[0],dx[1],dx[2]);
qe=(0.6*C_LIQF(c0,t0)+2.24*(1-C_LIQF(c0,t0)))
*(F_T(f,thread)-C_T(c0,t0))/dy;/*water or ice heat
flux*/
q2=a1*(pow(q2,-1/3))*(268+4*fabs(q2)*
(a2+a3+a4*fabs(pow(q2,1/3)))-F_T(f,thread));/*heat pipe
heat flux*/
if(fabs((q2-qe)/q2)>=0.005)
F_PROFILE(f,thread,iprop)=0.3*qe+0.7*q2;
else
F_PROFILE(f,thread,iprop)=0.5*qe+0.5*q2;
}end_f_loop(f,thread)
}
}
interpreted can succeed,but Fluent appear wrong information when it iterate.
Updating solution at time levels N and N-1.
done.
iter continuity x-velocity y-velocity energy time/iter
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: ()
Help me please

liurengtong123 November 28, 2010 22:08

Hi,first of all,thanks for your suggestion.
Now,i want to give you some deatils about my difficulties,so please give your email to me.
I hopely want to abtain your help,thanks very much!!!
My email is :liurengtong@163.com

liurengtong123 December 9, 2010 22:01

1 Attachment(s)
Hi
every one ,now i need your help .
I give detail about my problem through a picture .

TDi December 10, 2010 10:23

I am able to do something similar with only DEFINE_PROFILE. At every time step, I check the value of a virtual thermocouple and adjust the heat flux to a radiant heater using a control law.

Here's my code for your reference. Note that there is another function called at the beginning that returns the location of the cells I'm using as my virtual thermocouples. Also, note that this same DEFINE_PROFILE is used to control 8 separate heater surfaces (4 actual surfaces and 4 shadow faces).

Code:

DEFINE_PROFILE(heater_control,boundary_thread,i){
  /*i is the boundary property index, known to Fluent*/
  face_t face_thread;
  int zone_ID = THREAD_ID(boundary_thread);
  int j=0;
  real flow_time = RP_Get_Real("flow-time");
  real cell_temp;
  real error;
  real this_power;

  /*
    Set the temperature control measurement point and the index
    variable based on the value of zone_ID.
  */
  if(power[0]<=0){init();}

  switch(zone_ID){
  case FB_LEFT_ZONE:
    j=FB_LEFT;
    break;
  case FB_LEFT_SHADOW_ZONE:
    j=FB_LEFT_SHADOW;
    break;
  case BB_CENTER_LEFT_ZONE:
    j=BB_CENTER_LEFT;
    break;
  case BB_CENTER_LEFT_SHADOW_ZONE:
    j=BB_CENTER_LEFT_SHADOW;
    break;
  case BB_CENTER_RIGHT_ZONE:
    j=BB_CENTER_RIGHT;
    break;
  case BB_CENTER_RIGHT_SHADOW_ZONE:
    j=BB_CENTER_RIGHT_SHADOW;
    break;
  case FB_RIGHT_ZONE:
    j=FB_RIGHT;
    break;
  case FB_RIGHT_SHADOW_ZONE:
    j=FB_RIGHT_SHADOW;
    break;
  default:
    printf("\nDid not identify zone_ID.");
    break;
  }
  /*printf("\nDEFINE_PROFILE: flow time = %1.3e\t previous_time[%d] = %1.3e.",flow_time,j,previous_time[j]);*/
  if (flow_time > previous_time[j] + 0.5){/*This prevents multiple control efforts per time step*/
   
    /*Record the temperature of the virtual thermocouple*/
    cell_temp = C_T(c[j],t[j]);

    /*Heater control algorithm is proportional with control variable epsilon*/
    error = (set_temp[j]-cell_temp);
    printf("\n\tepsilon = %1.2e",error);
    epsilon[j][0] = epsilon [j][1];
    epsilon[j][1] = epsilon [j][2];
    epsilon[j][2] = error;

    u[j][0] = u[j][1];
    u[j][1] = u[j][2];
    u[j][2] = alpha * error;
    if(u[j][2] > 1){u[j][2]=1.0;}
    if(error>0){
        heater_state[j] = power[j]*u[j][2];
    }
    else{heater_state[j]=0;}/* Don't let the heater be a cold sink! */

    printf("\nHeater power in zone %d set to %1.3e.",zone_ID,heater_state[j]);
    printf("\n\tcontrol variable is %1.2e.",u[j][2]);
 
    previous_time[j]=flow_time;

    this_power = heater_state[j];
    begin_f_loop(face_thread,boundary_thread){
      F_PROFILE(face_thread,boundary_thread,i)=this_power;
    }
    end_f_loop(f,thread)
  }
}

I hope this can help you.

liurengtong123 December 10, 2010 23:34

Hi
thank you for giving me some suggestion.
I want to talk about my problem in detail ,so please give me your Email in order to discuss about it.
My Email :liurengtong@163.com
Thanks very much!!!


All times are GMT -4. The time now is 03:52.