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

Have trouble that the updated UDF variables can't transfer to TUI scheme files

Register Blogs Community New Posts Updated Threads Search

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

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   July 21, 2022, 21:31
Post Have trouble that the updated UDF variables can't transfer to TUI scheme files
  #1
New Member
 
Heisenberg Geehrte
Join Date: Jul 2022
Posts: 4
Rep Power: 3
Geehrte is on a distinguished road
The post starter wanted to use TUI command to switch boundary condition type when the outlet pressure reach the critical point(10000Pa). The UDF and TUI commands are being used in this case.
Here is my idea in handling this. I defined a variable "outlet_pressure" in scheme file and then it's called in UDF by "RP_GET_Real" command. In UDF I computed the average pressure on outlet surface and then update this variable. Finally, by using "rpgetvar", I can access the value of outlet pressure and decide whether to change the outlet boundary condition or not.
However, the post starter found that after a few calculation steps, the TUI can't access the updated UDF "outlet_pressure" value. In Fluent console panel I tapped (rpgetvar 'outlet_pressure) command and it returned the initially set value 1.
I used EXECUTE_AT_END macro in UDF, here is UDF code:
Code:
#include "udf.h"

DEFINE_EXECUTE_AT_END(pres_check)
	/* Average pressure on outlet surface */
{
	int zone_ID_outlet = 10;
	real A[ND_ND];
        Thread *thread;
	face_t f;
	real o_pressure, outlet_pressure;
	real P_ave = 1.0, P_sum = 0.0, A_sum = 0.0;
	Domain *d;
	d = Get_Domain(1);
	thread = Lookup_Thread(d, zone_ID_outlet);
	o_pressure = RP_Get_Real("outlet_pressure");
	begin_f_loop(f, thread)
	{
		F_AREA(A,f,thread);
		A_sum += NV_MAG(A);
		P_sum += NV_MAG(A)*F_P(f,thread);
	}
	end_f_loop(f, thread)
	P_ave = P_sum/A_sum;
	if (P_ave > o_pressure)
		o_pressure = P_ave;
	RP_Set_Real("outlet_pressure",o_pressure);
	printf("Outlet Pressure = %f \n",o_pressure);
}
The scheme file is executed every time step. Here is scheme code:
Code:
(rp-var-define 'outlet_pressure 1.0 'real #f)
(cond
((<= (%rpgetvar 'outlet_pressure) 10000) 
(ti-menu-load-string "define/boundary-conditions/zone-type 10 wall"))
(else (ti-menu-load-string "define/boundary-conditions/zone-type 10 pressure-outlet"))
)
So why I can't get a calculated value? The "o_pressure" value obviously updated every time step! Can you help me figure it out? Thanks for your kindness help!
Geehrte is offline   Reply With Quote

Old   July 23, 2022, 11:08
Default
  #2
Senior Member
 
Join Date: Sep 2017
Posts: 246
Rep Power: 11
obscureed is on a distinguished road
One possibility is that the rp-var-define command is constantly overwriting the value. It is fairly common to make sure that rp-var-define is called only once with something like
Code:
(if (not (rp-var-object 'rpvarname))(rp-var-define 'rpvarname 0.0 'real #f))
or you could just do it once per case and take it out of the repeated Scheme.

Really I think that you need to debug the sequence yourself:
-- Set outlet_pressure to a large negative value; run pres_check; confirm that outlet_pressure has been updated to a sensible value.
-- Check that outlet_pressure still has a sensible value when the Scheme starts and ends.
-- Check that the ti-menu-load-string commands do what you intend.
-- Check that the Scheme cond command does what you intend.
-- etc
If every step works, the whole thing will work. If not, and if you can't fix the broken step, you'll have a more specific question.
Geehrte likes this.
obscureed is offline   Reply With Quote

Old   July 24, 2022, 03:06
Default
  #3
New Member
 
Heisenberg Geehrte
Join Date: Jul 2022
Posts: 4
Rep Power: 3
Geehrte is on a distinguished road
Hi, Obscureed! I noticed that you are a veteran in this forum and I have learnt a lot from your former posts. Thanks for your advice.
Now I have replaced my rp-var-define command into this to make sure that the rp-var-define macro execute only once.
Code:
(define (make-new-rpvar 'outlet_pressure -999.0 'real)
(if (not (rp-var-object 'outlet_pressure))
(rp-var-define 'outlet_pressure -999.0 'real #f)))
(make-new-rpvar 'outlet_pressure -999.0 'real)
After I hooked the EXECUTE_AT_END macro and made sure my scheme file works every time step, you can see the running results as following(from 65-75 iterations)
Code:
Updating solution at time level N...
 done.
  iter  continuity  x-velocity  y-velocity      energy           k     epsilon     time/iter
    65  9.1066e-03  8.5559e-04  2.0053e-03  9.3002e-08  2.6643e-04  2.2503e-03  0:00:00    5
    66  9.5180e-03  1.8930e-03  1.4070e-03  1.3492e-07  2.7044e-04  2.2077e-03  0:00:00    4
    67  9.3741e-03  1.3079e-03  3.3517e-03  7.6416e-08  2.7824e-04  2.2561e-03  0:00:00    3
    68  9.3179e-03  1.2106e-03  1.6374e-03  7.9531e-08  2.8102e-04  2.1450e-03  0:00:00    2
    69  9.2098e-03  1.5034e-03  4.6282e-03  8.4639e-08  2.8510e-04  2.0802e-03  0:00:00    1
    70  9.1362e-03  1.0286e-03  1.6592e-03  7.8024e-08  2.9011e-04  2.0498e-03  0:00:00    0
Outlet Pressure = 10261.125977 
/file/read-macro "scheme3.scm"Loading ".\.\scheme3.scm"
define/boundary-conditions/zone-type 10 wall
 Zone not slit.Done.

Updating solution at time level N...
 done.
  iter  continuity  x-velocity  y-velocity      energy           k     epsilon     time/iter
    70  9.1362e-03  1.0286e-03  1.6592e-03  7.8024e-08  2.9011e-04  2.0498e-03  0:00:00    5
    71  9.5955e-03  1.0906e-03  3.6865e-03  1.6078e-07  2.9754e-04  2.1202e-03  0:00:00    4
    72  9.5147e-03  3.4935e-03  6.3283e-03  7.0936e-08  3.0514e-04  2.1359e-03  0:00:00    3
    73  9.4124e-03  1.8094e-03  5.3499e-03  8.1904e-08  3.0944e-04  2.0748e-03  0:00:00    2
    74  9.3880e-03  1.5320e-03  4.6221e-03  7.7788e-08  3.1366e-04  1.9670e-03  0:00:00    1
    75  9.2679e-03  2.0402e-03  1.1904e-03  7.5247e-08  3.1794e-04  1.9048e-03  0:00:00    0
Outlet Pressure = 10719.134766 
/file/read-macro "scheme3.scm"Loading ".\.\scheme3.scm"
define/boundary-conditions/zone-type 10 wall
 Zone not slit.Done.
So I made sure that the pressure calculation works well in UDF, the value of "o_pressure" updates every time step. But, when I stopped running calculation, and check the value of "outlet_pressure". It was still the original defined value.
Code:
(rpgetvar 'outlet_pressure)
-999
So I'm confused that whether it's RP_Set_Real command in UDF don't work or simply the %rpgetvar command in scheme don't work.
Geehrte is offline   Reply With Quote

Old   August 16, 2022, 10:43
Default
  #4
New Member
 
Heisenberg Geehrte
Join Date: Jul 2022
Posts: 4
Rep Power: 3
Geehrte is on a distinguished road
I have tried a lot of times these days, but I can't get it through. Is there anyone could help me? Thanks a lot!
Geehrte is offline   Reply With Quote

Old   August 18, 2022, 01:01
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
if you are using fluent version v19 and earlier, code should be parallelized

so for your case command RP_Set_Real("outlet_pressure",o_pressure);
should be executed on host
compile following code

Code:
#include "udf.h"

DEFINE_EXECUTE_AT_END(pres_check)
	/* Average pressure on outlet surface */
{
	int zone_ID_outlet = 10;
	real A[ND_ND];
        Thread *thread;
	face_t f;
	real o_pressure, outlet_pressure;
	real P_ave = 1.0, P_sum = 0.0, A_sum = 0.0;
	Domain *d;
	d = Get_Domain(1);
	thread = Lookup_Thread(d, zone_ID_outlet);
	#if !RP_NODE
		o_pressure = RP_Get_Real("outlet_pressure");
	#endif
	host_to_node_real_1(o_pressure);
	Message0("o_pressure on node from scheme = %f \n",o_pressure);	
	begin_f_loop(f, thread)
	{
		F_AREA(A,f,thread);
		A_sum += NV_MAG(A);
		P_sum += NV_MAG(A)*F_P(f,thread);
	}
	end_f_loop(f, thread)
	P_ave = P_sum/A_sum;
	if (P_ave > o_pressure)
		o_pressure = P_ave;
	Message0("Outlet Pressure on node = %f \n",o_pressure);	
	node_to_host_real_1(o_pressure);
	#if !RP_NODE
		RP_Set_Real("outlet_pressure",o_pressure);
		Message("Outlet Pressure on host = %f \n",o_pressure);
	#endif	
}

scheme
Code:
(define (make-new-rpvar name default type)(if (not (rp-var-object name))(rp-var-define name default type #f)))
(make-new-rpvar 'outlet_pressure 1.0  'real)
(cond
((<= (%rpgetvar 'outlet_pressure) 10000) 
(ti-menu-load-string "define/boundary-conditions/zone-type 10 wall"))
(else (ti-menu-load-string "define/boundary-conditions/zone-type 10 pressure-outlet"))
)
scheme should be executed on each timestep
Geehrte likes this.
__________________
best regards


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

Old   August 19, 2022, 04:41
Default
  #6
New Member
 
Heisenberg Geehrte
Join Date: Jul 2022
Posts: 4
Rep Power: 3
Geehrte is on a distinguished road
Thank you so much, Master AlexanderZ. Since I tried your modified codes in parallel, an connection between UDF and TUI was established!
Geehrte 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
Foam::error::PrintStack almir OpenFOAM Running, Solving & CFD 91 December 21, 2022 04:50
Call FORTRAN files as UDF in a Linux system azores Fluent UDF and Scheme Programming 6 October 21, 2022 15:53
Mass transfer UDF expecting void return Quinos Fluent UDF and Scheme Programming 0 January 8, 2014 18:36
OpenFOAM15 paraFoam bug koen OpenFOAM Bugs 19 June 30, 2009 10:46
[making animations] fclose fails to close files? Mika FLUENT 0 March 30, 2001 08:19


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