# UDF to get the average temperature of a face and use it to define a property

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

 August 11, 2016, 09:38 UDF to get the average temperature of a face and use it to define a property #1 New Member   Gonzalo Join Date: Jan 2015 Posts: 3 Rep Power: 4 Dear all, I have been reading this forum for a while, but this is my first post, so first I would like to say hi. I have been using Fluent for my research during the last 3 years and this forum has provided me with invaluable resources and answers, so thanks to all the contributors. I would like to kindly ask for some help with a UDF that I am trying to write. I am modelling the melting of a material into an enclosure by means of the enthalpy porosity approach. It is a transient, 2D axisymmetric simulation. What I want to do by the UDF is to define an effective thermal conductivity of a material as a function of the Rayleigh number. This Rayleigh number is at the same time a function of the average temperature of one of the walls in the previous time step. I know how to use the DEFINE_PROPERTY macro, but my problem is that I don’t know how to retrieve the average temperature of the wall from the previous time step. Thus, I decided first to write a Define on Demand macro in order to learn how to retrieve that average temperature. What I do is to run the simulation for a couple of time steps and then execute the macro on demand. This way I can compare the temperature that I want to retrieve by the UDF with the actual temperature of the wall. If this would work, then I could implement this part of the code in the DEFINE_PROPERTY macro….but obviously, I have facing some problems. The UDF that I have written is shown next: #include "udf.h" DEFINE_ON_DEMAND(demo_calc) { Domain *d; face_t f; real tavg; real temper; real vol_tot; real volume; int ID=7; /*this is the ID of the boundary wall that I want to get the temperature from*/ Thread *t; cell_t c; d = Get_Domain(1); t = Lookup_Thread(d,ID); thread_loop_c(t,d) { begin_c_loop(c,t) { volume = C_VOLUME(c,t); /* get cell volume */ temper = C_T(c,t); /* get cell temperature */ vol_tot += volume; tavg += temper*volume; } end_c_loop(c,t) tavg /= vol_tot; printf("temp = %g\n",tavg); } } I can interpret and execute the udf, however, instead of one temperature I get 8 different temperatures, as can be seen below: (Note that in the model I have 8 bc in total, which I guess that is not just a coincidence) temp = -1.#IND temp = -1.#IND temp = 562.851 temp = 1.4915e+06 temp = 559.15 temp = 1.63967e+06 temp = 559.15 temp = 1.41307e+06 The temperatures of 559 and 562K are in the range of the working temperatures of the model. However they don’t match the temperature of the wall that I am looking for. I have tried different codes and I have checked the manual and the forum looking for answers…but I wasn’t able to reach any solution. So any help would be really appreciated. Thank you very much in advance, Gon

 August 12, 2016, 06:11 #3 Member   Davoud Malekian Join Date: Jan 2016 Posts: 53 Rep Power: 3 another thing if you insist on finding the cells temp and ... cause i think you need them for DEFINE_PROPERTY , you should do something like this: #include "udf.h" static real vol_tot = 0; static real tsum = 0; static int i = 0; DEFINE_EXECUTE_AT_END(average_temp) { Domain *d; face_t f; real temper; real volume; int ID=7; Thread *t; Thread *t0; cell_t c; cell_t c0; d = Get_Domain(1); t = Lookup_Thread(d,ID); begin_f_loop(f,t) /*you have identified t in previous steps*/ { i++; c0 = F_C0(f,t); t0 = THREAD_T0(t); temper = C_T(c0,t0); volume = C_VOLUME(c0,t0); vol_tot += volume; tsum += temper; } end_f_loop(f,t) C_UDMI(c0,t0,0) = tsum /( i * vol_tot); } i don't know if it is right or not but you can check it your self and try to change it if you think it is wrong.

 August 13, 2016, 04:13 #4 New Member   Gonzalo Join Date: Jan 2015 Posts: 3 Rep Power: 4 Hi D.M, Many thanks for the help! I have been doing some tests and I think that your code can definitely help me. However, it gives an error while running. After some trials, I saw that this is solved if I the F_UDMI statement is moved within the "begin_f_loop". I have read that UDM is associated with cells, and thus it needs a loop when it is used. I concluded that this might be the reason for the error. Anyway, I have realized that this probably doesn’t affect my UDF. Since my objective is just to compute one temperature value (the average temperature of the boundary wall with ID = 8), I guess that I don’t need to use the UDM. I just need to write a UDF where that average temperature is a global variable. The value of this global variable is calculated at the end of each time step by the DEFINE_EXECUTE_AT_END macro, and used in the DEFINE_PROPERTY macro as an input. I used your code and I did some modifications to get the average value of the boundary wall at the end of each time step (see it at the end of the message). The tavg value is the one that will be used in the DEFINE_PROPERTY macro. The code is running…but now I have another problem: I get 4 different tavg values instead of one!! You can see it in the results attached below (I get them by means of the TUI). The funny story is that if I take the value of the 3 results that have physical meaning and calculate their average, then I get the average temperature that I am looking for. However, when I use the variable tavg to calculate the thermal conductivity in the DEFINE_PROPERTY macro I get also different values of thermal conductivity. It looks as if the wall with ID 8 would be divided into different zones at the same time... Tavg = -1.#IND area_tot = 0 Tavg = 582.391 area_tot = 0.0723761 Tavg = 559.15 area_tot = 0.0652473 Tavg = 559.15 area_tot = 0.0720006 I have tried several alternatives but I don’t get the solution. I thought that it might be related with the fact that the model is axisymmetric…but I used it on a 2D planar model and I got the same 4 results. .. Any help is really appreciated Thank you!! Gon P.S.: I started working with UDFs a few days ago...so I apologize if any of the things that I'm saying they make no sense... Code: #include "udf.h" real tavg; DEFINE_EXECUTE_AT_END(average_temp) { Domain *d; face_t f; real temper = 0.0; real A[ND_ND]; real area = 0.0; real area_tot = 0.0; int ID = 8; /*this is the ID of the boundary wall that I want to get the temperature from*/ Thread *t; int zone_ID; d = Get_Domain(1); t = Lookup_Thread(d,ID); tavg = 0.0; begin_f_loop(f,t) { F_AREA(A,f,t); area = NV_MAG(A)*2.0*M_PI; /*Since the model is axisymmetric, we have to multiply by 2pi*/ area_tot += area; temper = F_T(f,t); tavg += temper*area; } end_f_loop(f,t) tavg /= area_tot; printf("Tavg = %g area_tot = %g\n",tavg,area_tot); } DEFINE_PROPERTY(thermal_conductivity,c,t) { real keff; real Ra; real Nu; real k = 0.55; real C = 0.03; real n = 0.25; real beta = 0.0005; real Tm = 565.00; real v = 0.000002; real alfa = 0.0000005; real g = 9.81; real Lc = 0.33333; Ra=(g*beta*(tavg-Tm)*pow( Lc, 3))/(v*alfa); Nu=C*pow( Ra, n); keff = k*Nu; return keff; }

 August 14, 2016, 23:33 #5 New Member   Gonzalo Join Date: Jan 2015 Posts: 3 Rep Power: 4 Hi all, I have found that the reason why I got several results instead of 1 was that I was running the simulation in a parallel mode, so the mesh was partitioned. When I changed to a serial mode, then I got only one result and the code is working properly. Hopefully this info can be useful to other people. Cheers, Gon Tleja, D.M, lablan and 1 others like this.

 August 18, 2016, 15:35 #6 Member   Davoud Malekian Join Date: Jan 2016 Posts: 53 Rep Power: 3 thank u so much for posting the reason, i was wondering what the reason could be, tnx. gdb and AnasMK like this.

 Tags define property, get wall temperature, udf

 Thread Tools Display Modes Linear Mode

 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 OffTrackbacks are On Pingbacks are On Refbacks are On Forum Rules

All times are GMT -4. The time now is 05:27.