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

Proper way to use cell/thread objects in UDF cell macros?

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By AlexanderZ

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   December 14, 2020, 17:16
Default Proper way to use cell/thread objects in UDF cell macros?
  #1
jhz
New Member
 
Join Date: Dec 2020
Posts: 19
Rep Power: 5
jhz is on a distinguished road
Hello,

I am using pre-defined cell macros (Fluent UDFs) to access flow field variables - such as C_U(c, t) for the u-velocity. I've included a code snippet to demonstrate the utilization. The exact application is less relevant to my problem, but to briefly explain it is an actuator-line model for a 3-bladed vertical axis wind turbine. The intended function of the code is to obtain (x,y,z) coords for actuator points and find a cell for which the distance between that coord and the cell centroid is minimum. Once it is found, I wanted to store the cell, thread references into the vars 'min_c' and 'min_t', to hopefully call later in a cell access macro like C_U(min_c, min_t).

My question is: the following code when hooked to the simulation and run results in a 'Node ___ : Process ___ : Received Signal SIGSEGV' error, which I've found to be a segmentation fault. I was not able to find documentation on this error in general apart from specific posts, but through unit testing I've narrowed the problem to the macro calls in the 2nd last line -> 'C_U(min_c, min_t)' and 'C_W(min_c, min_t)'. Without these calls there is no error - meaning calling C_CENTROID() inside the cell loop according to the UDF manual is a correct implementation.

Does this mean I cannot store cell_t and Thread object references from within the cell/thread loop and then outside of the loop use them in cell macros? Also, I have not been able to find relevant documentation in the UDF manual for Fluent, so I would be grateful for any resource you can provide documenting the nature/application of cell_t and Thread objects (I understand their basic definitions but they seemingly do not work like traditional programming objects). Any help is appreciated!

Code:
#include "udf.h"
#include <math.h>

DEFINE_ADJUST(sample_code,d)
{
	double PI = 3.14159265;
	int n_blades = 3;
	int n_points = 10;
	double theta_i[3] = {90*(PI/180), 210*(PI/180), 330*(PI/180)};
	double theta;
	double rot_speed = 2*8/0.6;
	double x_p, y_p, z_p;
	double y_0 = 5.0;
	double span = 4.0;
	double radius = 0.6;
	real centroid_arr[ND_ND];

	// loop over all blades and actuator points
	for (int n = 0; n < n_blades; n++) {
		for (int p = 0; p < n_points; p++) {
			// determine position of actuator point in global coordinates
			double ds = span/p;
			y_p = y_0 + 0.5*ds + p*ds;
			theta = theta_i[n] + rot_speed*CURRENT_TIMESTEP*N_TIME;
			x_p = radius*cos(theta);
			z_p = radius*sin(theta);

			// find (cell,thread) closest to actuator point
			Thread *t;
			cell_t c;
			int counter = 0;
			Thread *min_t;
			cell_t min_c;
			double dist;
			double min_dist;

			thread_loop_c(t,d)
			{
				begin_c_loop(c,t)
					C_CENTROID(centroid_arr, c, t);
					dist = sqrt(pow(centroid_arr[0] - x_p, 2) + pow(centroid_arr[1] - y_p, 2) + pow(centroid_arr[2] - z_p, 2));
					if (counter == 0) {
						min_dist = dist;
						point_forces[n][p][0] = centroid_arr[0];
						point_forces[n][p][1] = centroid_arr[1];
						point_forces[n][p][2] = centroid_arr[2];
						min_c = c;
						min_t = t;
					}
					else if (counter > 0) {
						if (dist < min_dist) {
							min_dist = dist;
							point_forces[n][p][0] = centroid_arr[0];
							point_forces[n][p][1] = centroid_arr[1];
							point_forces[n][p][2] = centroid_arr[2];
							min_c = c;
							min_t = t;
						}
					}
					counter = counter + 1;
				end_c_loop(c,t)
			}

			// perform calculations
			double v_mag = sqrt(pow(C_U(min_c, min_t), 2) + pow(C_W(min_c, min_t), 2));
}
jhz is offline   Reply With Quote

Old   December 14, 2020, 23:05
Default
  #2
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
I think it could be because the thread is a pointer.

Code:
min_t=t;
should then be
Code:
*min_t=*t;
Not so sure if that's the problem, but you can try.
pakk is offline   Reply With Quote

Old   December 15, 2020, 00:06
Default
  #3
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
I think, you can do it, you can use this approach with c_min and t_min

problem my comes from the part, where you are defining cell/threads

move this part out of loop
Code:
			// find (cell,thread) closest to actuator point
			Thread *t;
			cell_t c;
			int counter = 0;
			Thread *min_t;
			cell_t min_c;
			double dist;
			double min_dist;
put it just above // loop over all blades and actuator points

Segmentation error means you are trying to access variables, which are not defined (memory is not allocated), so may be there could be a case when non for your conditions are true:
Code:
if (counter == 0) {
or
Code:
else if (counter > 0) {
						if (dist < min_dist) {
check it
pakk likes this.
__________________
best regards


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

Old   December 15, 2020, 13:06
Default
  #4
jhz
New Member
 
Join Date: Dec 2020
Posts: 19
Rep Power: 5
jhz is on a distinguished road
Hi pakk,


I tried that, but unfortunately it doesn't work.
jhz is offline   Reply With Quote

Old   December 15, 2020, 13:10
Default
  #5
jhz
New Member
 
Join Date: Dec 2020
Posts: 19
Rep Power: 5
jhz is on a distinguished road
Hi Alexander,



I tried doing what you suggested in the first part - moving the declarations outside of the loop, but the error still persists. I think it won't make a difference as long as the thread/cell definitions are outside of the tread/cell loops, correct?


I'm not sure what you mean by the second half, relating to the if statements. Can you please clarify?


Also, I think the way that Fluent iterates through the declared Thread *t object in the loops may be important. From the code itself, it seems as though thread_loop_c and begin_c_loop operates implicitly on the thread pointer and iterates through it somehow. Do you know anything more about the inner workings of this? (it's not described in the UDF manual)


Thanks!
jhz is offline   Reply With Quote

Old   December 15, 2020, 15:20
Default
  #6
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
Where and how did you declare point_forces?
pakk is offline   Reply With Quote

Old   December 15, 2020, 16:49
Default
  #7
jhz
New Member
 
Join Date: Dec 2020
Posts: 19
Rep Power: 5
jhz is on a distinguished road
I declared point_forces before DEFINE_ADJUST as a global double 3D array, but I think I missed that code declaration in the code snippet above. That's not the part causing problems, sorry for any confusion.
jhz is offline   Reply With Quote

Old   December 17, 2020, 02:43
Default
  #8
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
may be your conditions are never true
your code:
Code:
thread_loop_c(t,d)
{
	begin_c_loop(c,t)
	{
		C_CENTROID(centroid_arr, c, t);
		dist = sqrt(pow(centroid_arr[0] - x_p, 2) + pow(centroid_arr[1] - y_p, 2) + pow(centroid_arr[2] - z_p, 2));
		if (counter == 0) {
			min_dist = dist;
			point_forces[n][p][0] = centroid_arr[0];
			point_forces[n][p][1] = centroid_arr[1];
			point_forces[n][p][2] = centroid_arr[2];
			min_c = c;
			min_t = t;
		}
		else if (counter > 0) {
			if (dist < min_dist) {
				min_dist = dist;
				point_forces[n][p][0] = centroid_arr[0];
				point_forces[n][p][1] = centroid_arr[1];
				point_forces[n][p][2] = centroid_arr[2];
				min_c = c;
				min_t = t;
			}
		}
		counter = counter + 1;
	}
	end_c_loop(c,t)
}
you may try to make following
Code:
thread_loop_c(t,d)
{
	begin_c_loop(c,t)
	{
		min_c = c;
		min_t = t;
		C_CENTROID(centroid_arr, c, t);
		dist = sqrt(pow(centroid_arr[0] - x_p, 2) + pow(centroid_arr[1] - y_p, 2) + pow(centroid_arr[2] - z_p, 2));
		if (counter == 0) {
			min_dist = dist;
			point_forces[n][p][0] = centroid_arr[0];
			point_forces[n][p][1] = centroid_arr[1];
			point_forces[n][p][2] = centroid_arr[2];
			min_c = c;
			min_t = t;
		}
		else if (counter > 0) {
			if (dist < min_dist) {
				min_dist = dist;
				point_forces[n][p][0] = centroid_arr[0];
				point_forces[n][p][1] = centroid_arr[1];
				point_forces[n][p][2] = centroid_arr[2];
				min_c = c;
				min_t = t;
			}
		}
		counter = counter + 1;
	}
	end_c_loop(c,t)
}

btw I don;t see brackets after begin_c_loop(c,t)
__________________
best regards


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

Reply

Tags
cell macro, convention, udf


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
[Other] refineWallLayer Error Yuby OpenFOAM Meshing & Mesh Conversion 2 November 11, 2021 11:04
Problem with cell macros Abhinand Fluent UDF and Scheme Programming 0 October 11, 2019 15:24
Calculating velocity gradients manually without any macros NonStopEagle Fluent UDF and Scheme Programming 0 September 25, 2019 10:29
how to store UDF output for arbitrary/particular cell athalia FLUENT 1 September 24, 2019 05:02
cell zone creation for degassing udf implementation shahjehan Fluent UDF and Scheme Programming 0 August 3, 2016 11:30


All times are GMT -4. The time now is 16:07.