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 copy temperature every t seconds

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

Like Tree1Likes
  • 1 Post By sergiocavaleirocosta

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 15, 2019, 13:46
Default UDF to copy temperature every t seconds
  #1
New Member
 
Sérgio Cavaleiro Costa
Join Date: Nov 2017
Location: Lisbon, Portugal
Posts: 12
Rep Power: 9
sergiocavaleirocosta is on a distinguished road
Hello,
I am trying to develop a code to, after some time, copy the temperature of a solid to another solid with the same geometry and similar mesh. I intend to simulate a transient solution of similar moving objects without the computational effort of calculating and remeshing every time step. This way the object would "jump" to the next position every few specified time.
In order to do this I have developed the UDF:

Code:
DEFINE_ADJUST(temp_adjust, domain)
{
	real current_time;
	current_time = CURRENT_TIME;
	
	div_t divide;
	real dt;
	dt = 0.5;
	divide = div(current_time * 100., dt * 100.);
	
	cell_t c;
	
	int i;
	real Temp1[408];
	real Temp2[408];
	
	Thread *t1 = Lookup_Thread(domain, 18);
	Thread *t2 = Lookup_Thread(domain, 19);
	
	if (divide.rem == 0){
		/*----------------body 1 -----------------*/
		i = 0;
		begin_c_loop (c, t2)
		{
			Temp1[i] = C_T(c, t1);
			C_T(c, t1) = 300.;
			i ++;
		}
		end_c_loop (c, t1)
		
		/*----------------body 2 -----------------*/
		i = 0;
		begin_c_loop (c, t1)
		{
			Temp2[i] = C_T(c, t2);
			C_T(c, t2) = Temp1[i];
			i ++;
		}
		end_c_loop (c, t2)
		
	}
}
I am trying to store the temperatures of the first solid in Temp1 to replace the second solid's temperature and the same thing to the second solid to propagate for further solids (not being used for now for simplicity).
The code is compiled without issues, yet during the run, when it is evaluated for the first time the Fluent crashes.

Can anyone help me with this? I can't figure out what may be the issue, even because I am new to UDFs.

Thanks in advance.
sergiocavaleirocosta is offline   Reply With Quote

Old   March 17, 2019, 21:33
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
your temperature array has 408 elements
Code:
Temp2[408]
are you sure, that your finite element volume has less that 409 elements?

best regards
AlexanderZ is offline   Reply With Quote

Old   March 18, 2019, 03:45
Default
  #3
New Member
 
Sérgio Cavaleiro Costa
Join Date: Nov 2017
Location: Lisbon, Portugal
Posts: 12
Rep Power: 9
sergiocavaleirocosta is on a distinguished road
Quote:
Originally Posted by AlexanderZ View Post
your temperature array has 408 elements
Code:
Temp2[408]
are you sure, that your finite element volume has less that 409 elements?

best regards
Yes, I even believe that there are less than that. I am using a very coarse mesh for testing.
Even because this mesh refers to the solid elements where there is only conduction.
sergiocavaleirocosta is offline   Reply With Quote

Old   March 18, 2019, 21:30
Default
  #4
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
was
Code:
		begin_c_loop (c, t2)
		{
			Temp1[i] = C_T(c, t1);
			C_T(c, t1) = 300.;
			i ++;
		}
		end_c_loop (c, t1)
		
		/*----------------body 2 -----------------*/
		i = 0;
		begin_c_loop (c, t1)
		{
			Temp2[i] = C_T(c, t2);
			C_T(c, t2) = Temp1[i];
			i ++;
		}
		end_c_loop (c, t2)
tobe

Code:
		begin_c_loop (c, t1)
		{
			Temp1[i] = C_T(c, t1);
			C_T(c, t1) = 300.;
			i ++;
		}
		end_c_loop (c, t1)
		
		/*----------------body 2 -----------------*/
		i = 0;
		begin_c_loop (c, t2)
		{
			Temp2[i] = C_T(c, t2);
			C_T(c, t2) = Temp1[i];
			i ++;
		}
		end_c_loop (c, t2)
that could be the reason

best regards
AlexanderZ is offline   Reply With Quote

Old   March 19, 2019, 04:12
Default
  #5
New Member
 
Sérgio Cavaleiro Costa
Join Date: Nov 2017
Location: Lisbon, Portugal
Posts: 12
Rep Power: 9
sergiocavaleirocosta is on a distinguished road
Quote:
Originally Posted by AlexanderZ View Post
was
Code:
		begin_c_loop (c, t2)
		{
			Temp1[i] = C_T(c, t1);
			C_T(c, t1) = 300.;
			i ++;
		}
		end_c_loop (c, t1)
		
		/*----------------body 2 -----------------*/
		i = 0;
		begin_c_loop (c, t1)
		{
			Temp2[i] = C_T(c, t2);
			C_T(c, t2) = Temp1[i];
			i ++;
		}
		end_c_loop (c, t2)
tobe

Code:
		begin_c_loop (c, t1)
		{
			Temp1[i] = C_T(c, t1);
			C_T(c, t1) = 300.;
			i ++;
		}
		end_c_loop (c, t1)
		
		/*----------------body 2 -----------------*/
		i = 0;
		begin_c_loop (c, t2)
		{
			Temp2[i] = C_T(c, t2);
			C_T(c, t2) = Temp1[i];
			i ++;
		}
		end_c_loop (c, t2)
that could be the reason

best regards

Thank you so much AlexanderZ. That was definitely an issue, yet I changed the code according to your suggestion and I am still with problems. I have placed:

Code:
Message("Ti = %g \n", Temp1[i]);
inside the second loop and the output in the console is 0 in the temperature diverging my solution. Then I did the same in the first loop to check and temperatures were correct! How can this be possible?

Thank you once again.
sergiocavaleirocosta is offline   Reply With Quote

Old   March 19, 2019, 07:17
Default
  #6
Senior Member
 
Join Date: Nov 2011
Posts: 109
Rep Power: 15
robboflea is on a distinguished road
Are you executing the UDF in parallel or in serial solver?
robboflea is offline   Reply With Quote

Old   March 19, 2019, 07:19
Default
  #7
New Member
 
Sérgio Cavaleiro Costa
Join Date: Nov 2017
Location: Lisbon, Portugal
Posts: 12
Rep Power: 9
sergiocavaleirocosta is on a distinguished road
Quote:
Originally Posted by robboflea View Post
Are you executing the UDF in parallel or in serial solver?
Hello robboflea, I am using parallel.
sergiocavaleirocosta is offline   Reply With Quote

Old   March 19, 2019, 07:34
Default
  #8
Senior Member
 
Join Date: Nov 2011
Posts: 109
Rep Power: 15
robboflea is on a distinguished road
In this case you have to consider that not all calculation nodes will have access to the whole mesh and that the two arrays Temp1 and Temp2 will exist as a separate copy in each calculation node!


When executing the first loop Temp1 is filled only in the core (or cores) that have a share of cells belonging to that boundary. If the second loop is not executed by the same core (quite likely and in any case something you have no control over) because the faces belonging to such boundary have been assigned to another core by mesh partitioning, then you will be assigning garbage values stored in Temp1 (not initialized at this point) to C_T(c,t2).


I advice you to first execute the UDF in serial mode and check that it works. Then work on the parallel version but for this you need basic understanding of MPI and, in general, how distributed memory calculations work.


Hope it helps,


Roberto
robboflea is offline   Reply With Quote

Old   March 19, 2019, 07:49
Default
  #9
New Member
 
Sérgio Cavaleiro Costa
Join Date: Nov 2017
Location: Lisbon, Portugal
Posts: 12
Rep Power: 9
sergiocavaleirocosta is on a distinguished road
Quote:
Originally Posted by robboflea View Post
In this case you have to consider that not all calculation nodes will have access to the whole mesh and that the two arrays Temp1 and Temp2 will exist as a separate copy in each calculation node!


When executing the first loop Temp1 is filled only in the core (or cores) that have a share of cells belonging to that boundary. If the second loop is not executed by the same core (quite likely and in any case something you have no control over) because the faces belonging to such boundary have been assigned to another core by mesh partitioning, then you will be assigning garbage values stored in Temp1 (not initialized at this point) to C_T(c,t2).


I advice you to first execute the UDF in serial mode and check that it works. Then work on the parallel version but for this you need basic understanding of MPI and, in general, how distributed memory calculations work.


Hope it helps,


Roberto

Thank you robboflea that makes a lot of sense and explains the different issues I have been experiencing during my tests.

I will do has you suggested and try in serial. But if that is the case how can I control the place where data is stored?

Something I have tried, and have abandoned later, was to write temperatures for a txt file and then read from it in the second loop, although I didn't manage to put it to work as well.

Greetings.
sergiocavaleirocosta is offline   Reply With Quote

Old   March 19, 2019, 08:15
Default
  #10
Senior Member
 
Join Date: Nov 2011
Posts: 109
Rep Power: 15
robboflea is on a distinguished road
You cannot control it (not that I am aware of).
However if you look in the Fluent manual, in the section named "Parallel UDF considerations" (or something similar) it will explain you in great detail how to deal with parallel UDFs and transfer data to/from nodes.


Rob
robboflea is offline   Reply With Quote

Old   March 25, 2019, 06:28
Default
  #11
New Member
 
Sérgio Cavaleiro Costa
Join Date: Nov 2017
Location: Lisbon, Portugal
Posts: 12
Rep Power: 9
sergiocavaleirocosta is on a distinguished road
Thank you for the explanations.

I followed your suggestion and I have been reading the fluent udf parallel information and I have been trying to understand how this code should be adapted to the parallel approach, but I am completely lost!

How can I access to variables? I have been playing with the #if !RP_HOST, #if RP_NODE, node_to_host_real(Temp1), but so far nothing have worked.

Can anyone give me a guide?

Thank you.
sergiocavaleirocosta is offline   Reply With Quote

Old   March 25, 2019, 06:44
Default
  #12
Senior Member
 
Join Date: Nov 2011
Posts: 109
Rep Power: 15
robboflea is on a distinguished road
The macros that you mentioned are only used to identify particular nodes. In order to access the solution variables (temperature, velocity...) you need to use the same access macros that you would use for serial implementation. No difference here.


There is a section on "Cell MAcros" on the Fluent manual. For example, to access density in a cell you'd use C_R(c,t).


All the info you need are in the fluent manual. If you have any specific issue you can post it here and me (or other people) may get a chance in guessing what's wrong but without being specific I cannot do anything but refer you to the fluent manual


Good luck!
robboflea is offline   Reply With Quote

Old   March 25, 2019, 07:05
Default
  #13
New Member
 
Sérgio Cavaleiro Costa
Join Date: Nov 2017
Location: Lisbon, Portugal
Posts: 12
Rep Power: 9
sergiocavaleirocosta is on a distinguished road
Once again, I am very pleased with your help.

I am trying to solve the issue you mentioned.

Quote:
Originally Posted by robboflea View Post

In this case you have to consider that not all calculation nodes will have access to the whole mesh and that the two arrays Temp1 and Temp2 will exist as a separate copy in each calculation node!

When executing the first loop Temp1 is filled only in the core (or cores) that have a share of cells belonging to that boundary. If the second loop is not executed by the same core (quite likely and in any case something you have no control over) because the faces belonging to such boundary have been assigned to another core by mesh partitioning, then you will be assigning garbage values stored in Temp1 (not initialized at this point) to C_T(c,t2).

Roberto
From your comment, I understand that the Temp1 array should be passed or to the host node or to all nodes in order to get the data available in the second loop (since I don't know in which node will be the loop). That is why I have been working around with node_to_host_real(Temp1). Nevertheless, when I print the Temp1 array in the second loop I get the non-sense data you pointed out, so I think it means that this loop cannot access to the Temp1 yet. I believe that the issue is, how can I make Temp1 available from the second loop?

Thank you so much.


Here it is my current code:
Code:
DEFINE_ADJUST(temp_adjust, domain)
{
	real current_time;
	current_time = CURRENT_TIME;

	div_t divide;
	real dt;
	dt = RP_Get_Real("dt");
	divide = div(current_time * 100., dt * 100.);
	
	cell_t c;
	
	int i;
	real Temp1[408];
	real Temp2[408];
	#if !RP_HOST
		if (divide.rem == 0){
			Thread *t1 = Lookup_Thread(domain, 18);
			Thread *t2 = Lookup_Thread(domain, 19);
			/*----------------body 1 -----------------*/
			i = 0;
			begin_c_loop (c, t1)
			{
				Temp1[i] = C_T(c, t1);
				C_T(c, t1) = 300.;
				i ++;
			}
			end_c_loop (c, t1)

			node_to_host_real_1(Temp1);
			
			/*----------------body 2 -----------------*/
			i = 0;
			begin_c_loop (c, t2)
			{
				Message("Temperatura = %g \n", Temp1[i]);
				Temp2[i] = C_T(c, t2);
				C_T(c, t2) = Temp1[i];
				i ++;
			}
			end_c_loop (c, t2)
		}
	#endif
}
sergiocavaleirocosta is offline   Reply With Quote

Old   March 25, 2019, 07:29
Default
  #14
Senior Member
 
Join Date: Nov 2011
Posts: 109
Rep Power: 15
robboflea is on a distinguished road
What are you trying to achieve is not easy and you are not using the correct strategy. Is your mesh conformal at the interface? Do you have an exact mapping between the nodes at the interface of the two domains?

If not you have to interpolate your data when passing the information and that is not easy. Also another issue is that you're passing data by node ID. Your first loop fills Temp1[i] with the cell temperature but, even if you were to pass correctly this array to the second wall, you cannot be sure that C_T(c,t2) corresponds to the same node where Temp1[i] has been recorded. You have to check that the coordinates are the same (within tolerance). You cannot even be sure that the second loop is shorter than the first one. If not your code will segf when trying to access Temp1[i] on a out of bound index.

Also, when passing arrays between nodes you have to first communicate the dimension of the array. I suggest you study a bit of MPI to understand how data communication works.

Have you tried communicating boundary conditions by just exporting and reading boundary profiles from Fluent? Fluent can interpolate those data on non conformal grids for you (not sure about the quality of the interpolation, I always work with conformal grids) but I think it's worth a shot

Last edited by robboflea; March 25, 2019 at 07:30. Reason: added more details
robboflea is offline   Reply With Quote

Old   March 25, 2019, 07:54
Default
  #15
New Member
 
Sérgio Cavaleiro Costa
Join Date: Nov 2017
Location: Lisbon, Portugal
Posts: 12
Rep Power: 9
sergiocavaleirocosta is on a distinguished road
Quote:
Originally Posted by robboflea View Post
What are you trying to achieve is not easy and you are not using the correct strategy. Is your mesh conformal at the interface? Do you have an exact mapping between the nodes at the interface of the two domains?

If not you have to interpolate your data when passing the information and that is not easy. Also another issue is that you're passing data by node ID. Your first loop fills Temp1[i] with the cell temperature but, even if you were to pass correctly this array to the second wall, you cannot be sure that C_T(c,t2) corresponds to the same node where Temp1[i] has been recorded. You have to check that the coordinates are the same (within tolerance). You cannot even be sure that the second loop is shorter than the first one. If not your code will segf when trying to access Temp1[i] on a out of bound index.

Also, when passing arrays between nodes you have to first communicate the dimension of the array. I suggest you study a bit of MPI to understand how data communication works.

Have you tried communicating boundary conditions by just exporting and reading boundary profiles from Fluent? Fluent can interpolate those data on non conformal grids for you (not sure about the quality of the interpolation, I always work with conformal grids) but I think it's worth a shot
You have just referred to some of my fears!
In reality, both solid have similarly structured mesh. Nevertheless, I am not sure if both have the same cell numbering order (most probably not). Because both meshes are "equal", with the same number of volumes, I believe I can avoid the interpolation you mention. It is just a translation of the temperature with step size.
My first approach was writing coordinates and temperatures to a file, but I got the same kind of issues so I gave up. Do you believe it would be a better approach? Is there a way to refer to a cell by its coordinates?

Thanks.
sergiocavaleirocosta is offline   Reply With Quote

Old   March 25, 2019, 08:21
Default
  #16
Senior Member
 
Join Date: Nov 2011
Posts: 109
Rep Power: 15
robboflea is on a distinguished road
There is no way to refer to the cell by coordinates but it is possible to access to the cell centroid and check its coordinates. With this you may be able to check that you're passing the information between the same node. However it is a very time-consuming and tedious process (and you don't want to do this every time you do the passing of information).


If I were you I'd just avoid using UDFs in your situation and use .prof files.
robboflea is offline   Reply With Quote

Old   March 25, 2019, 08:25
Default
  #17
New Member
 
Sérgio Cavaleiro Costa
Join Date: Nov 2017
Location: Lisbon, Portugal
Posts: 12
Rep Power: 9
sergiocavaleirocosta is on a distinguished road
Quote:
Originally Posted by robboflea View Post
There is no way to refer to the cell by coordinates but it is possible to access to the cell centroid and check its coordinates. With this you may be able to check that you're passing the information between the same node. However it is a very time-consuming and tedious process (and you don't want to do this every time you do the passing of information).


If I were you I'd just avoid using UDFs in your situation and use .prof files.
Thank you, robboflea, you are the best!
I will see profile files then.
robboflea likes this.
sergiocavaleirocosta is offline   Reply With Quote

Reply

Tags
temperature, transient 3d, udf crash

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Inlet temperature as a function of outlet temperature UDF wimblo Fluent UDF and Scheme Programming 5 November 25, 2021 07:06
whats the cause of error? immortality OpenFOAM Running, Solving & CFD 13 March 24, 2021 08:15
problem in temperature and liquid fraction value in the udf durg FLUENT 0 October 23, 2018 02:52
Outlet temperature to temperature boundary condition of velocity inlet. (UDF) dillon Fluent UDF and Scheme Programming 3 March 25, 2018 12:06
UDF slip and temperature jump from IFRT abir Fluent UDF and Scheme Programming 1 July 30, 2012 06:44


All times are GMT -4. The time now is 02:20.