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 variable temperature with time

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 21, 2013, 03:25
Default UDF for variable temperature with time
  #1
New Member
 
Çağlar Coşkun
Join Date: Feb 2012
Posts: 20
Rep Power: 14
CaglarCoskun is on a distinguished road
Hello

I have a UDF, which varies the temperature according to the temperature at a certain point in the model. I write the coordinates of the point to the UDF where I want to check the temperature and the UDF varies the temperature of a wall with time according to that.

I have a dummy model (a cube) to test the UDF. I write the coordinates of center of the cube and it seems to work fine. However, when I change my dummy model with a different one, all the things go wrong.

What might be the problem?
CaglarCoskun is offline   Reply With Quote

Old   November 21, 2013, 17:46
Default
  #2
New Member
 
majid
Join Date: Mar 2011
Posts: 8
Rep Power: 15
majidmng is on a distinguished road
Hi
Is it possible to help me to write UDF for Inlet gas temperature varying with time?I have equation of tempratue vs time.
majidmng is offline   Reply With Quote

Old   November 22, 2013, 14:35
Default
  #3
Senior Member
 
François Grégoire
Join Date: Jan 2010
Location: Canada
Posts: 392
Rep Power: 17
macfly is on a distinguished road
majidmng, here is an example:
Code:
#include "udf.h"

DEFINE_PROFILE(Tinlet, thread, position) 
{
  face_t f;
  real t = CURRENT_TIME;
  begin_f_loop(f, thread)
	  {
		  F_PROFILE(f, thread, position) = 100 + t;
	  }
  end_f_loop(f, thread)
}
macfly is offline   Reply With Quote

Old   November 24, 2013, 06:04
Default
  #4
New Member
 
Çağlar Coşkun
Join Date: Feb 2012
Posts: 20
Rep Power: 14
CaglarCoskun is on a distinguished road
Any help, idea, thought, opinion??
CaglarCoskun is offline   Reply With Quote

Old   November 24, 2013, 16:38
Default
  #5
Senior Member
 
ghost82's Avatar
 
Rick
Join Date: Oct 2010
Posts: 1,016
Rep Power: 27
ghost82 will become famous soon enough
Quote:
Originally Posted by CaglarCoskun View Post
Any help, idea, thought, opinion??
If you want help you have to write here your code...how can we help you without the code??
ghost82 is offline   Reply With Quote

Old   November 26, 2013, 04:29
Default
  #6
New Member
 
Çağlar Coşkun
Join Date: Feb 2012
Posts: 20
Rep Power: 14
CaglarCoskun is on a distinguished road
Quote:
Originally Posted by ghost82 View Post
If you want help you have to write here your code...how can we help you without the code??
Hello again.

What I am trying to do is to generate a wall temperature profile. I fitted a piecewise linear function to my actual temperature plot, and try to implement this function as a boundary condition to Fluent.

My function is as follows:



As you can see from the image above, there are a number of parts and every part is a linear function. The transitions in parts 1-3 are dependent in time, such as 1. part for first 3.5 minutes, 2. part for next 2 minutes etc. The duration of 4. part is controlled by a temperature at a certain point in my model. (there is actually a thermostat at this point). When the temperature at the thermostat reaches a certain value, the function jumps to the 5. part.

The same story is also valid for 7. part. When the temperature at thermostat decreases to a value, the function jumps to the 1. part and whole of the function is redrawn in a periodic manner.

This is my problem. The UDF that I wrote is as follows:

Code:
#include "udf.h"
#define PI 3.141592654
#define T1 268.00
#define T2 273.00
#define Xs 0.05  // Xs, Ys, Zs are the coordinates of point where the thermostat is located
#define Ys 0.175
#define Zs -0.2
#define dx 0.005

  real Dt0=20.0;
  real Dt1=3.5*60.0;
  real Dt2=2*60.0;
  real Dt3=5.5*60.0;
  real slope_1=27*60;
  real Dt4=3000*60.0;
  real Dt5=2*60.0;
  real Dt6=2.5*60.0;
  real Dt7=7.5*60.0;
  real slope_2=13.5*60;
  real Dt8=3000*60;
  real period=362210;

  real Tevapfz1=-27.9+273.15; 
  real Tevapfz2=-20.8+273.15;
  real Tevapfz3=-20.3+273.15;
  real Tevapfz4=-20.6+273.15;
  real Tevapfz5=-18.9+273.15;
  real Tevapfz6=-22.5+273.15;
  real Tevapfz7=-23.7+273.15;
  real Tevapfz8=-26.1+273.15;
  real Tevapfz9=-27.9+273.15;

  real A=0.0;           
  real B=-27.9+273.15;
  real Evap_time=0.0;    
  real c_time=0.0;
  real Evap_time_stop=0.0;
  real Evap_time_work=0.0;
  real aralik=0.0;
  
DEFINE_EXECUTE_AT_END(my_execute_at_end)

{ 

  double Tave;
  double Tave0; 
                                       
  Domain *d;
  Thread *t;
  cell_t c;
  FILE *pf;
  real x[2];
  
  double cellt;   
  d = Get_Domain(1);   
  Tave=0.0;
  Tave0=Tave;
  cellt=0.0;
  
  c_time=CURRENT_TIME;
  if(c_time>Dt0)
    Evap_time=(c_time-Dt0)-floor((c_time-Dt0)/period)*period;  
  if(c_time<=Dt0)
    Evap_time=0.0;

  thread_loop_c (t,d)
  {			
  begin_c_loop (c,t)
  {
  C_CENTROID(x,c,t);
			
  if ((x[0]>(Xs-dx)) && (x[0]<(Xs+dx)))  
  {
  if ((x[1]>(Ys-dx)) && (x[1]<(Ys+dx))) 
  {
  if ((x[2]>(Zs-dx)) && (x[2]<(Zs+dx)))
  {
  cellt+=1.0;
  Tave+=C_T(c,t);
  }
  }
  }
  }
  end_c_loop (c,t)
  }

  Tave/=cellt; //Tave is the average temperature at the location of thermostat
                           
  if (Evap_time<=Dt0)
  {
  A=0.0;
  B=Tevapfz1; 
  aralik=0.0;
  }

  if ((Evap_time<Dt1)&&(Evap_time>=Dt0))
  {
  A=(Tevapfz1-Tevapfz2)/(-Dt1); //A and B are the coefficients of linear functions at every part.
  B=Tevapfz1;
  aralik=1.0;
  }

  if ((Evap_time<Dt1+Dt2)&&(Evap_time>=Dt1))
  {
  A=(Tevapfz2-Tevapfz3)/(-Dt2);
  B=(Tevapfz3*Dt1-Tevapfz2*(Dt1+Dt2))/(-Dt2);
  aralik=2.0;
  }

  if ((Evap_time<Dt1+Dt2+Dt3)&&(Evap_time>=Dt1+Dt2))
  {
  A=(Tevapfz3-Tevapfz4)/(-Dt3);
  B=(Tevapfz4*(Dt1+Dt2)-Tevapfz3*(Dt1+Dt2+Dt3))/(-Dt3);
  Evap_time_stop=Evap_time;
  aralik=3.0;
  }

  if ((Evap_time<Dt1+Dt2+Dt3+Dt4)&&(Evap_time>=Dt1+Dt2+Dt3)) //The duration of this part is controlled by Tave
  {
  if (Tave<T2)
  {
  A=(Tevapfz4-Tevapfz5)/(-slope_1);
  B=(Tevapfz5*(Dt1+Dt2+Dt3)-Tevapfz4*(Dt1+Dt2+Dt3+slope_1))/(-slope_1);
  aralik=4.0;
  }
  
  if (Tave>=T2)
  {
  Dt4=Evap_time-Evap_time_stop;
  }    
  } 
  
  if ((Evap_time<Dt1+Dt2+Dt3+Dt4+Dt5)&&(Evap_time>=Dt1+Dt2+Dt3+Dt4))
  {
  A=(Tevapfz5-Tevapfz6)/(-Dt5);
  B=(Tevapfz6*(Dt1+Dt2+Dt3+Dt4)-Tevapfz5*(Dt1+Dt2+Dt3+Dt4+Dt5))/(-Dt5);
  aralik=5.0;
 
  }
    
  if ((Evap_time<Dt1+Dt2+Dt3+Dt4+Dt5+Dt6)&&(Evap_time>=Dt1+Dt2+Dt3+Dt4+Dt5))
  {
  A=(Tevapfz6-Tevapfz7)/(-Dt6);
  B=(Tevapfz7*(Dt1+Dt2+Dt3+Dt4+Dt5)-Tevapfz6*(Dt1+Dt2+Dt3+Dt4+Dt5+Dt6))/(-Dt6);
  aralik=6.0;
  }
                                                                      
  if ((Evap_time<Dt1+Dt2+Dt3+Dt4+Dt5+Dt6+Dt7)&&(Evap_time>=Dt1+Dt2+Dt3+Dt4+Dt5+Dt6))
  {
  A=(Tevapfz7-Tevapfz8)/(-Dt7);
  B=(Tevapfz8*(Dt1+Dt2+Dt3+Dt4+Dt5+Dt6)-Tevapfz7*(Dt1+Dt2+Dt3+Dt4+Dt5+Dt6+Dt7))/(-Dt7);
  Evap_time_work=Evap_time;
  aralik=7.0;
  }
	
  if ((Evap_time<Dt1+Dt2+Dt3+Dt4+Dt5+Dt6+Dt7+Dt8)&&(Evap_time>=Dt1+Dt2+Dt3+Dt4+Dt5+Dt6+Dt7))
  { 
  if (Tave>T1)
  {
  A=(Tevapfz8-Tevapfz9)/(-slope_2);
  B=(Tevapfz9*(Dt1+Dt2+Dt3+Dt4+Dt5+Dt6+Dt7)-Tevapfz8*(Dt1+Dt2+Dt3+Dt4+Dt5+Dt6+Dt7+slope_2))/(-slope_2);
  aralik=8.0;
  }
  
  if (Tave<=T1)
  {
  Dt8=Evap_time-Evap_time_work;
  period=Dt1+Dt2+Dt3+Dt4+Dt5+Dt6+Dt7+Dt8;
  }
  }
  
  
  printf("CURRENT_TIME=%f\n", CURRENT_TIME);
  printf("c_time=%f\n", c_time);
  printf("Evap_time=%f\n", Evap_time);
  printf("period=%f\n", period);
  printf("Dt4=%f\n", Dt4);
  printf("Dt8=%f\n", Dt8);
  printf("Evap_time_stop=%f\n", Evap_time_stop);
  printf("Evap_time_work=%f\n", Evap_time_work);
  printf("Tave=%f\n\n", Tave);
  printf("EVAP-FZ-TEMPERATURE=%f\n", (A*Evap_time+B));
  printf("ARALIK=%f\n\n", aralik);

}

DEFINE_PROFILE(evapfz_temp, thread, position) 

{

  face_t f;
  cell_t c;
  real x[2];

	begin_f_loop(f, thread)
   		{
                    F_PROFILE(f, thread, position)= A*Evap_time+B;
        }
  		end_f_loop(f, thread)
  		
}
The trouble is, I draw a simple model to test the UDF above (a cube), I wrote the center coordinates of the cube and it works very well. When I change my model to a different one, everything goes wrong. As you can see in the picture below, "model1 evap" is true, "model2 evap" is the wrong temperature profiles. "Model1 T1" and "Model2 T2" are the temperature plots at the thermostat for both cases.



Thanks for your helps
Attached Images
File Type: jpg forum_soru_2.JPG (68.4 KB, 711 views)
File Type: jpg forum_soru.jpg (82.0 KB, 704 views)

Last edited by CaglarCoskun; November 26, 2013 at 08:52.
CaglarCoskun is offline   Reply With Quote

Old   January 14, 2014, 09:57
Default
  #7
New Member
 
Çağlar Coşkun
Join Date: Feb 2012
Posts: 20
Rep Power: 14
CaglarCoskun is on a distinguished road
Hello again

We have realized some mistakes about the algorithm and solved them, however the UDF still doesn't work.

The main problem seems about parallelizing the UDF. The average temperature (designated as "Tave" in the code) are calculated different by every compute node. Some compute nodes even can't calculate a "Tave" value and their output becomes as "indefinite". Because of this confusion, some nodes go to the next part of the temperature profile and some of them don't so the whole process becomes messy.

To solve the problem, I am trying to take the highest of all Tave's among all compute nodes.

There is a UDF for a problem similar to mine in the following link, but I couldn't get any help from this code: http://aerojet.engr.ucdavis.edu/flue...df/node218.htm

The final version of the code is given below. Can anyone help me about paralellizing this code?

Code:
#include "udf.h"

#define PI 3.141592654
#define T1 280.34
#define T2 281.32
#define Xs -0.820   // Xs, Ys, Zs are the coordinates of point where the thermostat is located
#define Ys 0.347
#define Zs 0.279
#define dx 0.005
#define dy 0.005
#define dz 0.002

  real Dt1=0.5*60.0;
  real Dt2=1.5*60.0;
  real Dt3=9*60.0;
  real slope_1=27.5*60;
  real Dt4=3000*60.0;
  real Dt5=1.5*60.0;
  real Dt6=2*60.0;
  real slope_2=10*60;
  real Dt7=3000*60;

  real Tevapfz1=-21+273.15; 
  real Tevapfz2=-19.1+273.15;
  real Tevapfz3=-2.6+273.15;
  real Tevapfz4=4.8+273.15;
  real Tevapfz5=10+273.15;
  real Tevapfz6=-14+273.15;
  real Tevapfz7=-17+273.15;
  real Tevapfz8=-21+273.15;

  real A=0.0;           
  real B=-21+273.15;
  real Evap_time=0.0;    
  real c_time=0.0;
  real Evap_time_stop=0.0;
  real Evap_time_work=0.0;
  real aralik=0.0;
  real gz=0.0;
  
  FILE * fp;
  
DEFINE_EXECUTE_AT_END(my_execute_at_end)

{ 

  double cellt;                                     
  double Tave;           //The average temperature at the location of thermostat (calculated by one node)
  double Tave0;          //Sum of all Tave's  
  double Tave_total;     //Highest of Tave's  
  
#if !RP_HOST 
                                       
  Domain *d;
  Thread *t;
  cell_t c;
  real x[2]; 
  d = Get_Domain(1);  
  
#endif
  
  Tave=0.0;
  Tave0=Tave;
  cellt=0.0;
  Tave_total=0.0;
  
#if !RP_HOST

  thread_loop_c (t,d)
  {			
  begin_c_loop (c,t)
  {
  C_CENTROID(x,c,t);
			
  if ((x[0]>(Xs-dx)) && (x[0]<(Xs+dx)))  
  {
  if ((x[1]>(Ys-dy)) && (x[1]<(Ys+dy))) 
  {
  if ((x[2]>(Zs-dz)) && (x[2]<(Zs+dz)))
  {
  cellt+=1.0;
  Tave+=C_T(c,t);
  }
  }
  }
  }
  end_c_loop (c,t)
  }

  Tave/=cellt;
  
#if RP_NODE
  
  Tave_total=PRF_GRHIGH1(Tave);
  
#endif
  
#endif

  c_time=CURRENT_TIME;
  Evap_time=c_time-gz;

  if (Evap_time<Dt1)
  {
  A=(Tevapfz1-Tevapfz2)/(-Dt1);
  B=Tevapfz1;
  aralik=1.0;
  }

  if ((Evap_time<Dt1+Dt2)&&(Evap_time>=Dt1))
  {
  A=(Tevapfz2-Tevapfz3)/(-Dt2);
  B=(Tevapfz3*Dt1-Tevapfz2*(Dt1+Dt2))/(-Dt2);
  aralik=2.0;
  }

  if ((Evap_time<Dt1+Dt2+Dt3)&&(Evap_time>=Dt1+Dt2))
  {
  A=(Tevapfz3-Tevapfz4)/(-Dt3);
  B=(Tevapfz4*(Dt1+Dt2)-Tevapfz3*(Dt1+Dt2+Dt3))/(-Dt3);
  Evap_time_stop=Evap_time;
  aralik=3.0;
  }

  if ((Evap_time<Dt1+Dt2+Dt3+Dt4)&&(Evap_time>=Dt1+Dt2+Dt3)&&(Tave_total<T2))
  {
  A=(Tevapfz4-Tevapfz5)/(-slope_1);
  B=(Tevapfz5*(Dt1+Dt2+Dt3)-Tevapfz4*(Dt1+Dt2+Dt3+slope_1))/(-slope_1);
  aralik=4.0;
  }
  
  if ((Evap_time<Dt1+Dt2+Dt3+Dt4)&&(Evap_time>=Dt1+Dt2+Dt3)&&(Tave_total>=T2))
  {
  Dt4=Evap_time-Evap_time_stop;
  }
  
  if ((Evap_time<Dt1+Dt2+Dt3+Dt4+Dt5)&&(Evap_time>=Dt1+Dt2+Dt3+Dt4))
  {
  A=(Tevapfz5-Tevapfz6)/(-Dt5);
  B=(Tevapfz6*(Dt1+Dt2+Dt3+Dt4)-Tevapfz5*(Dt1+Dt2+Dt3+Dt4+Dt5))/(-Dt5);
  aralik=5.0;
  }
    
  if ((Evap_time<Dt1+Dt2+Dt3+Dt4+Dt5+Dt6)&&(Evap_time>=Dt1+Dt2+Dt3+Dt4+Dt5))
  {
  A=(Tevapfz6-Tevapfz7)/(-Dt6);
  B=(Tevapfz7*(Dt1+Dt2+Dt3+Dt4+Dt5)-Tevapfz6*(Dt1+Dt2+Dt3+Dt4+Dt5+Dt6))/(-Dt6);
  Evap_time_work=Evap_time;
  aralik=6.0;
  }
	
  if ((Evap_time<Dt1+Dt2+Dt3+Dt4+Dt5+Dt6+Dt7)&&(Evap_time>=Dt1+Dt2+Dt3+Dt4+Dt5+Dt6)&&(Tave_total>T1))
  {
  A=(Tevapfz7-Tevapfz8)/(-slope_2);
  B=(Tevapfz8*(Dt1+Dt2+Dt3+Dt4+Dt5+Dt6)-Tevapfz7*(Dt1+Dt2+Dt3+Dt4+Dt5+Dt6+slope_2))/(-slope_2);
  aralik=7.0;
  }
  
  if ((Evap_time<Dt1+Dt2+Dt3+Dt4+Dt5+Dt6+Dt7)&&(Evap_time>=Dt1+Dt2+Dt3+Dt4+Dt5+Dt6)&&(Tave_total<=T1))
  {
  Dt7=Evap_time-Evap_time_work;
  gz=Dt1+Dt2+Dt3+Dt4+Dt5+Dt6+Dt7;
  }
  
  fp=fopen("output.txt","a");
  
  fprintf(fp, "CURRENT_TIME=%f\n", CURRENT_TIME);
  fprintf(fp, "Evap_time=%f\n", Evap_time);
  fprintf(fp, "Dt4=%f\n", Dt4);
  fprintf(fp, "Dt7=%f\n", Dt7);
  fprintf(fp, "A=%f\n", A);
  fprintf(fp, "B=%f\n", B);
  fprintf(fp, "Evap_time_stop=%f\n", Evap_time_stop);
  fprintf(fp, "Evap_time_work=%f\n", Evap_time_work);
  fprintf(fp, "Tave=%f\n\n", Tave);
  fprintf(fp, "EVAP-FZ-TEMPERATURE=%f\n", (A*Evap_time+B));
  fprintf(fp, "ARALIK=%f\n\n", aralik);
  fprintf(fp, "Tave_total=%f\n\n", Tave_total);

  fclose(fp);

  PRF_GSYNC();

}

DEFINE_PROFILE(evapfz_temp, thread, index) 
{
                            
#if !RP_HOST

  real x[ND_ND];		
  real y;
  face_t f;

	begin_f_loop(f, thread)
   		{
      			F_CENTROID(x,f,thread);
      			F_PROFILE(f, thread, index) = A*Evap_time+B;
   		}
  		end_f_loop(f, thread)
  		
#endif

}

Last edited by CaglarCoskun; January 15, 2014 at 04:08.
CaglarCoskun is offline   Reply With Quote

Old   January 15, 2014, 09:39
Default
  #8
New Member
 
Join Date: Jun 2012
Posts: 4
Rep Power: 14
NikoN is on a distinguished road
Hi,

Excuse me for the silly question but is your UDF ok when you run on only 1 core ?
In your loops, it might be better if you use "else if". I recently encountered problems when using multiple "if" loops without using "else if".
NikoN is offline   Reply With Quote

Old   January 15, 2014, 10:15
Default
  #9
New Member
 
Çağlar Coşkun
Join Date: Feb 2012
Posts: 20
Rep Power: 14
CaglarCoskun is on a distinguished road
Hi.

Yes it works very well with 1 core but things become complicated when parallel processing is acitivated.

I tried "else if" but nothing changed.
CaglarCoskun 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
Transient simulation not converging skabilan OpenFOAM Running, Solving & CFD 14 December 17, 2019 00:12
Extrusion with OpenFoam problem No. Iterations 0 Lord Kelvin OpenFOAM Running, Solving & CFD 8 March 28, 2016 12:08
Moving mesh Niklas Wikstrom (Wikstrom) OpenFOAM Running, Solving & CFD 122 June 15, 2014 07:20
UDF for a time varying temperature majidmng Fluent UDF and Scheme Programming 0 November 21, 2013 02:38
How to write k and epsilon before the abnormal end xiuying OpenFOAM Running, Solving & CFD 8 August 27, 2013 16:33


All times are GMT -4. The time now is 15:54.