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

UDF to get the average temperator of a face and use it to in a boundary condition

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 1 Post By AlexanderZ
  • 1 Post By AlexanderZ

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   December 10, 2020, 17:47
Default UDF to get the average temperator of a face and use it to in a boundary condition
  #1
New Member
 
Mickael Perrin
Join Date: Dec 2020
Posts: 9
Rep Power: 6
mperrin is on a distinguished road
Hi everyone,


First of all, i would like to thank's every poeple who share their knowledge and suggests solutions. Thank you !




I am actually modeling a magnetocaloric machine. A magnetocaloric solid materiel heat a coolant which flows in a hot reservoir.

The ideal modelling is to use a static volume machine and displace fluid with a dynamic mesh. I successfully realise this modeling on my local machine but i can't make it work on the computation center of my lab. So i left this approch for the moment to get a simpler one.

The purpose of my second approach is to work with a velocity-inlet / pressure-outlet boundary condition. Since i have to give a temperature for each of these conditions, the heat exchange might be disturbed. Thus, i want, at the end of each timestep realise a perfect mixture between the volume of fluid i inject in the reservoir with the actual temperature of the reservoir. Then, i want to patch this mixture temperature of all the reservoir and assign it as the tempature of the boundary condition (velocity inlet or pressure outlet).

I didnt stuy C language but i know code with Python so i konw basics. I works with UDF since i have to defined some complex functions but until now, some tests and easy macros were enough to do the job. This time i have to work with loop / thread / cell / face / domain and i am pretty lost.

I made a simple 2D case of 2 solid square (1x1m) to write the UDF. With the help of this forum and Ansys fluent user guide, i actually realised a working UDF but it calculate the average temperature of the entire domain. After some tries, i didnt figure out how to target a single domain.

Reading Ansys User guide example, i wrote an udf using a thread_loop_c(t,d) and a begin_c_loop(c,t) but this print me the entire domain average tempature. Using this post (UDF to get the average temperature of a face and use it to define a property), i tried to define a single domain but every test is concluded by a SIGSEGV error, closing fluent.

In my case :

ID 3 and 4 are cell zones conditions (solid and solid)

ID 1 an 2 are defined interior (i would like the average tempature off one of them)
ID 7 to 14 are the eight wall of the two solid

I didnt activate mesh interfaces since i just want to calculate the average tempature.

Here there is the code i made calculating the entire domain average tempature.

#include"udf.h"
#include"mem.h"
#include"math.h"
/* working version, return all domain average temperature */
DEFINE_EXECUTE_AT_END(execute_at_end)
{
Domain * domain;
Thread * thread;
cell_t cell;

real sum_TxVOLUME = 0.;
real sum_VOLUME = 0.;
real avg_T = 0.;

domain = Get_Domain(1);

#if !RP_HOST
thread_loop_c(thread, domain)
{
begin_c_loop_int(cell, thread)
sum_TxVOLUME += C_T(cell, thread) * C_VOLUME(cell, thread);
sum_VOLUME += C_VOLUME(cell, thread);
end_c_loop_int(cell, thread)
}

#if RP_NODE
sum_TxVOLUME = PRF_GRSUM1(sum_TxVOLUME);
sum_VOLUME = PRF_GRSUM1(sum_VOLUME);
#endif

avg_T = sum_TxVOLUME / sum_VOLUME;

#endif

node_to_host_real_3(sum_TxVOLUME, sum_VOLUME, avg_T);

#if !RP_NODE
printf("\n");
printf("Temperature Sum host : %g\n", sum_TxVOLUME);
printf("Volume Sum host : %g\n", sum_VOLUME);
printf("Temperature average host : %g\n", avg_T);
printf("\n");
fflush(stdout);
#endif
}


I tried to use (t = Lookup_Thread(d,ID) but i didnt make it works in this code;

Can anybody guide me in the way to get a single domain average tempature ?


Thank you for your time and your help

Best regards

Mickael
mperrin is offline   Reply With Quote

Old   December 11, 2020, 04:06
Default
  #2
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
you've made all work
don't mixed up meanings of domains and zones. You may have several domains in case you are simulating multiphase flow. Different solids are zones, To access them you can use t = Lookup_Thread(d,ID)

once you know zone ID, you don't need loop over all threads anymore. Thread is defined by t = Lookup_Thread(d,ID) macro

try this code
Code:
#include"udf.h"

/* working version, return all domain average temperature */
DEFINE_EXECUTE_AT_END(execute_at_end)
{
Domain * domain;
Thread * thread;
cell_t cell;

real sum_TxVOLUME = 0.;
real sum_VOLUME = 0.;
real avg_T = 0.;
int ID = 1;

domain = Get_Domain(1);

#if !RP_HOST
thread = Lookup_Thread(d,ID)

begin_c_loop_int(cell, thread)
{
sum_TxVOLUME += C_T(cell, thread) * C_VOLUME(cell, thread);
sum_VOLUME += C_VOLUME(cell, thread);
}
end_c_loop_int(cell, thread)


#if RP_NODE
sum_TxVOLUME = PRF_GRSUM1(sum_TxVOLUME);
sum_VOLUME = PRF_GRSUM1(sum_VOLUME);
#endif

avg_T = sum_TxVOLUME / sum_VOLUME;

#endif

node_to_host_real_3(sum_TxVOLUME, sum_VOLUME, avg_T);

#if !RP_NODE
printf("\n");
printf("Temperature Sum host : %g\n", sum_TxVOLUME);
printf("Volume Sum host : %g\n", sum_VOLUME);
printf("Temperature average host : %g\n", avg_T);
printf("\n");
fflush(stdout);
#endif
}
mperrin likes this.
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Old   December 14, 2020, 06:17
Default
  #3
New Member
 
Mickael Perrin
Join Date: Dec 2020
Posts: 9
Rep Power: 6
mperrin is on a distinguished road
Hello AlexanderZ,


Than you for your fast answer.




I just tested your code and after some tries, i found that i have to return the ID's zone of the solid Cell Zone Conditions and not the interior Boundary Condition.


Thank you for your knowledge and your time !


Best regards


Mickael
mperrin is offline   Reply With Quote

Old   December 15, 2020, 13:54
Default
  #4
New Member
 
Mickael Perrin
Join Date: Dec 2020
Posts: 9
Rep Power: 6
mperrin is on a distinguished road
Hello again,


I come back to have some help concerning the second part of my UDF.

I want to assign the temperature average that i obtained on every cell of the fluid zone (to simulate a perfect mixture).


I made some test and succesffully changed the temperature of every cell of a domain constitued by :
- 1 solid zone
- 2 solid zone separated by a wall
- 1 fluid zone and 1 solid zone separated by a wall
For these 3 configuration i test an udf which change the temeprature on all thread and then on a specified thread (with Lookup_thread)


However when i work on 1 fluid zone and 1 solid connected by an interface, my udf doesnt work. I tried to assign new temperature on the fluid zone : every timestep, the temperature should rise of 1 K but the rising temperature is effective only on the first timestep, the other dont change the tempeature.

Can someone help me ? thank you

The code i used to changed the fluid zone temperature :

Code:
#include "udf.h"

DEFINE_EXECUTE_AT_END(Tmaj)
{
    Domain * d;
    Thread * t;
    cell_t c;

    d = Get_Domain(1);
    t = Lookup_Thread(d, 5);

    begin_c_loop_all(c, t)
    {            
        C_T(c, t) = C_T(c, t) + 1;
    }
    end_c_loop_all(c, t)

}
And this is the average fluid temperature exported by a report definition :
Code:
"report-def-0"
"Time Step" "report-def-0"
("Time Step" "report-def-0")
0 301
1 301.9782876871894
2 301.961191028263
3 301.9473635085971
4 301.9359053883307
5 301.9261960178856
6 301.9178012554168
7 301.9104136896635
8 301.9038128222773
9 301.8978382863829
10 301.8923717135861
Best regards,
Mickael
mperrin is offline   Reply With Quote

Old   December 18, 2020, 06:34
Default
  #5
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
Code:
#include "udf.h"
real temp = 0.0;
DEFINE_INIT(my_init, d)
{
	Thread * t;
    cell_t c;
	t = Lookup_Thread(d, 5);
	begin_c_loop(c, t)
    {            
		temp = C_T(c, t);
    }
	end_c_loop(c, t)
}

DEFINE_EXECUTE_AT_END(Tmaj)
{
    Domain * d;
    Thread * t;
    cell_t c;
    d = Get_Domain(1);
    t = Lookup_Thread(d, 5);
    begin_c_loop(c, t)
    {            
		C_T(c, t) = temp ;
    }
    end_c_loop(c, t)
	temp = temp +1 ;
}
mperrin likes this.
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ 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



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