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 read in data (problem executing)

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By pakk

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   September 9, 2016, 04:54
Default UDF to read in data (problem executing)
  #1
New Member
 
Erik Jan
Join Date: Aug 2016
Posts: 7
Rep Power: 9
ejpostema is on a distinguished road
Hi All,

I am creating a UDF to use an velocity profile from MRI as a boundary condition. I read the velocity profile from a CSV, which contain 30 13x16 matrices. So there a 30 velocity profiles depending on time. I interpolate between the points in a later stage.

I wrote a DEFINE_INIT function to read in the data, but I get an error in fluent when I execute this function on demand (to test it).

Code:
Node 9: Process 3868: Received signal SIGSEGV.

==============================================================================
MPI Application rank 0 exited before MPI_Finalize() with status 2
 The fl process could not be started.

I have copy the same code to test it. And I am able to compile and read the CSV file within codeblocks. I don't know what is going wrong here

THis is my DEFINE_INIT functions


Code:
DEFINE_INIT(read_csv,d)//, fname)
{
	char buffer[500];
	int i, j, k;
	int num;
	float x, y, z, pixelspace;
	FILE *file = fopen("C:\\Data\\Astrid\\CFD\\Flow_matlab_files\\test1.csv", "r");
	{
		fscanf(file, "%f %f %f %f", &x, &y, &z, &pixelspace);
		for (k = 0; k < TIMESTEP; k++)
		{
			for (i = 0; i < HEIGHT; i++)
			{
				for (j = 0; j < WIDTH; j++)
				{
					fscanf(file, "%d", &num);
					numArray[k][i][j] = num;
				}
			}
		}
	}
	//construct x and y coordinates
	for (i = 0; i <= WIDTH; i++)
	{
		xCor[i] = x + pixelspace*i;
	}
	for (i = 0; i <= HEIGHT; i++)
	{
		yCor[i] = y + pixelspace*i;
	}
	
}
ejpostema is offline   Reply With Quote

Old   September 9, 2016, 07:38
Default
  #2
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
You are doing parallel, it would help to first try it in serial mode.

Quote:
And I am able to compile and read the CSV file within codeblocks
What is codeblocks?

Nowhere in your code you define what numArray, xCor or yCor are. I can infer that they are arrays, but are they big enough?

Code:
DEFINE_INIT(read_csv,d)//, fname)
What is this? Why the two slashes and the fname?

Your 8th line is a {, but that does not make any sense... There is no for-loop or if-clause or anything similar to start a block...
pakk is offline   Reply With Quote

Old   September 9, 2016, 10:57
Default
  #3
New Member
 
Erik Jan
Join Date: Aug 2016
Posts: 7
Rep Power: 9
ejpostema is on a distinguished road
Quote:
Originally Posted by pakk View Post
You are doing parallel, it would help to first try it in serial mode.


What is codeblocks?

Nowhere in your code you define what numArray, xCor or yCor are. I can infer that they are arrays, but are they big enough?
Sorry, yeah I define those arrays earlier, posted only a piece of the code. Only posted the init function. They are big enough and are defined correctly (as an int)

Codeblocks is an IDE (like visual studio). So I started a project there where I coded the same function as above (without define init). I am able to compile it and execute the function. So I used it to test this part of the code

Quote:
Originally Posted by pakk View Post
Code:
DEFINE_INIT(read_csv,d)//, fname)
What is this? Why the two slashes and the fname?
oh some leftover. I commented something out, you can ignore it. Sorry for the inconvience

Quote:
Originally Posted by pakk View Post
Your 8th line is a {, but that does not make any sense... There is no for-loop or if-clause or anything similar to start a block...
I think I saw that in an example. Will see if it works to remove those (within codeblocks it does not matter)
ejpostema is offline   Reply With Quote

Old   September 9, 2016, 11:03
Default
  #4
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
Quote:
Originally Posted by ejpostema View Post
oh some leftover. I commented something out, you can ignore it. Sorry for the inconvience
I can ignore it, but the compiler will not ignore it.
This is not the correct way to use comments: you should use /* this style */. The style with two //-es is not proper c-style, although many compilers accept it.
pakk is offline   Reply With Quote

Old   September 12, 2016, 08:13
Default
  #5
New Member
 
Erik Jan
Join Date: Aug 2016
Posts: 7
Rep Power: 9
ejpostema is on a distinguished road
Quote:
Originally Posted by pakk View Post
I can ignore it, but the compiler will not ignore it.
This is not the correct way to use comments: you should use /* this style */. The style with two //-es is not proper c-style, although many compilers accept it.
Thnx for the feedback. I have removed those lines and curly braceless you mentioned as well.

I get a segmentation fault now. Will check for some more bugs
ejpostema is offline   Reply With Quote

Old   September 13, 2016, 04:50
Default
  #6
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
Segmentation faults really suggest that your arrays are not big enough. They should be at least the following size:
Code:
float xCor[WIDTH];
float yCor[HEIGHT];
int numArray[TIMESTEP][HEIGHT][WIDTH];
Debug your code by adding messages, to see how far your code got. I did this below, along with slightly reordering your code.

Code:
DEFINE_INIT(read_csv,d)
{
	char buffer[500];
	int i, j, k;
	int num;
	float x, y, z, pixelspace;
	FILE *file = fopen("C:\\Data\\Astrid\\CFD\\Flow_matlab_files\\test1.csv", "r");
	if (!file) {Message("Error opening file.\n"); exit(1);}

	fscanf(file, "%f %f %f %f", &x, &y, &z, &pixelspace);
	Message("First line read.\n");

	/*construct x and y coordinates*/
	for (j = 0; j <= WIDTH; j++)
	{
		xCor[j] = x + pixelspace*j;
	}
	for (i = 0; i <= HEIGHT; i++)
	{
		yCor[i] = y + pixelspace*i;
	}
	Message("Coordinates constructed.\n");

	for (k = 0; k < TIMESTEP; k++)
	{
		for (i = 0; i < HEIGHT; i++)
		{
			for (j = 0; j < WIDTH; j++)
			{
				fscanf(file, "%d", &num);
				numArray[k][i][j] = num;
			}
		}
		Message("Timestep %d read correctly.\n",k);
	}
	Message("Finished reading timesteps.\n");
}
pakk is offline   Reply With Quote

Old   October 26, 2016, 08:25
Default
  #7
New Member
 
Erik Jan
Join Date: Aug 2016
Posts: 7
Rep Power: 9
ejpostema is on a distinguished road
I left this problem for a while, since I had some other prioritues (some abstract deadlines).

I talked with somebody who has some better understanding on this stuff than me. He adviced me to break the code up into smaller pieces. First get on thing to work within fluent and then add the other stuff. Great advice so that is what I did.
Anyway I a now just focussing on the interpolation and have hardcode one matrix into my code. I'll add the other stuff later.

I am now getting this error message: 'MPI Application rank 3 exited before MPI_Finalize() with status -1073741819'

Does anyone understand what this means or what is wrong? thanks a lot in advance, I am the only one in my department with a bit of C knowledge

Code:
#include "udf.h" /*file that contains definitions for define functions and fluent operations*/

DEFINE_PROFILE(inlet_velocity, th, index)
{
	Message("ENTERED INLET_VELOCITY FUNCTION.\n");
	/*Declaration of constants*/
	float numArray[12][15];
	int pixelSize = 0.0000769;
	float MRIx = -0.0005;
	float MRIy = -0.0005;
	float MRIz = -0.0066797900;
	float xCor[12];
	float yCor[15];
	/*float dist[207]; /* xCor times yCor*/
	float posVector[ND_ND];  /* an array for the coordinates */
	float x, y, z, r;
	float x2x1, y2y1, x2x, y2y, yy1, xx1;
	float q11, q12, q21, q22;
	int i, j;
	/*float a, b, c, d, aa, bb, cc, dd;*/
	int ax, ay, bx, by, cx, cy, dx, dy;
	float x1, x2, x3, x4, y1, y2, y3, y4;
	/*int px1, px2, px3, px4, py1, py2, py3, py4;*/
	float vel;
	face_t f; // f is a face thread index
	float t = CURRENT_TIME;
	float nodeDistance;
	float distance0;
	float distance1;
	float distance2;
	float distance3;

	Message("FINISHED DECLARING VARIABLES.\n");
	/* Create the matrix with MRI flow velocity
	hardcoded for testing purposes, needs to be changed in the future */
	/* row 0 */
	numArray[0][0] = 0;
	numArray[0][1] = 0;
	numArray[0][2] = 0;
	numArray[0][3] = 0;
	numArray[0][4] = 0;
	numArray[0][5] = 0;
	numArray[0][6] = 0;
	numArray[0][7] = 0;
	numArray[0][8] = 0;
	numArray[0][9] = 0;
	numArray[0][10] = 0;
	numArray[0][11] = 0;
	numArray[0][12] = 0;

	/* row 1 */
	numArray[1][0] = 0;
	numArray[1][1] = 0;
	numArray[1][2] = 0;
	numArray[1][3] = 101;
	numArray[1][4] = 101;
	numArray[1][5] = 80;
	numArray[1][6] = 87;
	numArray[1][7] = 96;
	numArray[1][8] = 87;
	numArray[1][9] = 78;
	numArray[1][10] = 0;
	numArray[1][11] = 0;
	numArray[1][12] = 0;

	/* row 2 */
	numArray[2][0] = 0;
	numArray[2][1] = 0;
	numArray[2][2] = 0;
	numArray[2][3] = 73;
	numArray[2][4] = 93;
	numArray[2][5] = 87;
	numArray[2][6] = 93;
	numArray[2][7] = 89;
	numArray[2][8] = 88;
	numArray[2][9] = 87;
	numArray[2][10] = 106;
	numArray[2][11] = 137;
	numArray[2][12] = 0;

	/* row 3 */
	numArray[3][0] = 0;
	numArray[3][1] = 0;
	numArray[3][2] = 0;
	numArray[3][3] = 77;
	numArray[3][4] = 90;
	numArray[3][5] = 103;
	numArray[3][6] = 106;
	numArray[3][7] = 98;
	numArray[3][8] = 92;
	numArray[3][9] = 88;
	numArray[3][10] = 88;
	numArray[3][11] = 79;
	numArray[3][12] = 0;

	/* row 4 */
	numArray[4][0] = 0;
	numArray[4][1] = 0;
	numArray[4][2] = 91;
	numArray[4][3] = 94;
	numArray[4][4] = 100;
	numArray[4][5] = 113;
	numArray[4][6] = 119;
	numArray[4][7] = 110;
	numArray[4][8] = 92;
	numArray[4][9] = 81;
	numArray[4][10] = 81;
	numArray[4][11] = 67;
	numArray[4][12] = 0;

	/* row 5 */
	numArray[5][0] = 0;
	numArray[5][1] = 0;
	numArray[5][2] = 107;
	numArray[5][3] = 112;
	numArray[5][4] = 112;
	numArray[5][5] = 117;
	numArray[5][6] = 121;
	numArray[5][7] = 115;
	numArray[5][8] = 94;
	numArray[5][9] = 73;
	numArray[5][10] = 79;
	numArray[5][11] = 101;
	numArray[5][12] = 0;

	/* row 6 */
	numArray[6][0] = 0;
	numArray[6][1] = 90;
	numArray[6][2] = 109;
	numArray[6][3] = 121;
	numArray[6][4] = 118;
	numArray[6][5] = 115;
	numArray[6][6] = 115;
	numArray[6][7] = 111;
	numArray[6][8] = 98;
	numArray[6][9] = 85;
	numArray[6][10] = 80;
	numArray[6][11] = 92;
	numArray[6][12] = 0;

	/* row 7 */
	numArray[7][0] = 0;
	numArray[7][1] = 98;
	numArray[7][2] = 107;
	numArray[7][3] = 120;
	numArray[7][4] = 120;
	numArray[7][5] = 118;
	numArray[7][6] = 117;
	numArray[7][7] = 112;
	numArray[7][8] = 101;
	numArray[7][9] = 94;
	numArray[7][10] = 84;
	numArray[7][11] = 71;
	numArray[7][12] = 0;

	/* row 8 */
	numArray[8][0] = 0;
	numArray[8][1] = 105;
	numArray[8][2] = 110;
	numArray[8][3] = 117;
	numArray[8][4] = 122;
	numArray[8][5] = 123;
	numArray[8][6] = 121;
	numArray[8][7] = 115;
	numArray[8][8] = 102;
	numArray[8][9] = 98;
	numArray[8][10] = 93;
	numArray[8][11] = 70;
	numArray[8][12] = 0;

	/* row 9 */
	numArray[9][0] = 0;
	numArray[9][1] = 106;
	numArray[9][2] = 111;
	numArray[9][3] = 115;
	numArray[9][4] = 121;
	numArray[9][5] = 122;
	numArray[9][6] = 119;
	numArray[9][7] = 116;
	numArray[9][8] = 103;
	numArray[9][9] = 89;
	numArray[9][10] = 95;
	numArray[9][11] = 83;
	numArray[9][12] = 0;

	/* row 10 */
	numArray[10][0] = 0;
	numArray[10][1] = 0;
	numArray[10][2] = 105;
	numArray[10][3] = 110;
	numArray[10][4] = 115;
	numArray[10][5] = 116;
	numArray[10][6] = 118;
	numArray[10][7] = 117;
	numArray[10][8] = 100;
	numArray[10][9] = 75;
	numArray[10][10] = 75;
	numArray[10][11] = 81;
	numArray[10][12] = 0;

	/* row 11 */
	numArray[11][0] = 0;
	numArray[11][1] = 0;
	numArray[11][2] = 96;
	numArray[11][3] = 100;
	numArray[11][4] = 102;
	numArray[11][5] = 108;
	numArray[11][6] = 114;
	numArray[11][7] = 108;
	numArray[11][8] = 91;
	numArray[11][9] = 70;
	numArray[11][10] = 61;
	numArray[11][11] = 76;
	numArray[11][12] = 0;

	/* row 12 */
	numArray[12][0] = 0;
	numArray[12][1] = 0;
	numArray[12][2] = 0;
	numArray[12][3] = 85;
	numArray[12][4] = 87;
	numArray[12][5] = 94;
	numArray[12][6] = 98;
	numArray[12][7] = 94;
	numArray[12][8] = 85;
	numArray[12][9] = 75;
	numArray[12][10] = 71;
	numArray[12][11] = 0;
	numArray[12][12] = 0;

	/* row 13 */
	numArray[13][0] = 0;
	numArray[13][1] = 0;
	numArray[13][2] = 0;
	numArray[13][3] = 70;
	numArray[13][4] = 86;
	numArray[13][5] = 82;
	numArray[13][6] = 79;
	numArray[13][7] = 83;
	numArray[13][8] = 93;
	numArray[13][9] = 95;
	numArray[13][10] = 0;
	numArray[13][11] = 0;
	numArray[13][12] = 0;

	/* row 14 */
	numArray[14][0] = 0;
	numArray[14][1] = 0;
	numArray[14][2] = 0;
	numArray[14][3] = 0;
	numArray[14][4] = 0;
	numArray[14][5] = 89;
	numArray[14][6] = 78;
	numArray[14][7] = 84;
	numArray[14][8] = 106;
	numArray[14][9] = 108;
	numArray[14][10] = 0;
	numArray[14][11] = 0;
	numArray[14][12] = 0;

	/* row 15 */
	numArray[15][0] = 0;
	numArray[15][1] = 0;
	numArray[15][2] = 0;
	numArray[15][3] = 0;
	numArray[15][4] = 0;
	numArray[15][5] = 0;
	numArray[15][6] = 0;
	numArray[15][7] = 0;
	numArray[15][8] = 0;
	numArray[15][9] = 0;
	numArray[15][10] = 0;
	numArray[15][11] = 0;
	numArray[15][12] = 0;

	Message("FINISHED CREATION OF VENC MATRIX.\n");
	/* reconstruct the x and y coordinates of the venc matrix*/
	for (i = 0; i <= 12; i++)
	{
		xCor[i] = MRIx + pixelSize*i;
	}
	for (i = 0; i <= 15; i++)
	{
		yCor[i] = MRIy + pixelSize*i;
	}
	Message("Coordinates constructed.\n");

	begin_f_loop(f, th)
	{
		Message("BEGIN LOOP.\n");
		F_CENTROID(posVector, f, th);
		/* CHANGE THIS FOR THE REAL PROGRAM: x = PosVector[0]!!!*/
		x = posVector[1];	/* x coordinate */
		y = posVector[2];   /* y coordinate */
		z = posVector[0];	/* z coordinate */
		
		/* calculate the distances between the face and the points in the venc matrix */

		for (i = 0; i <= 12; i++)
		{
			
			for (j = 0; j <= 15; j++)
			{
				nodeDistance = sqrt((xCor[i] - x)*(xCor[i] - x) + (yCor[j] - y)*(yCor[j] - y));
				
				if (i == 0 && j == 0)
				{
					distance0 = nodeDistance;
					distance1 = nodeDistance;
					distance2 = nodeDistance;
					distance3 = nodeDistance;
				}
				else
				{
					if (nodeDistance < distance0)
					{
						distance0 = nodeDistance;
						ax = i;
						ay = j;
					}
					else if (nodeDistance < distance1)
					{
						distance1 = nodeDistance;
						bx = i;
						by = j;
					}
					else if (nodeDistance < distance2)
					{
						distance2 = nodeDistance;
						cx = i;
						cy = j;
					}
					else if (nodeDistance < distance3)
					{
						distance3 = nodeDistance;
						dx = i;
						dy = j; 
					}
				}
			}
		}

		q11 = numArray[ax][ay];
		q12 = numArray[bx][by];
		q21 = numArray[cx][cy];
		q22 = numArray[dx][dy];

		/*bilinear interpolation */
		x2x1 = xCor[bx] - xCor[ax];
		y2y1 = yCor[by] - yCor[ay];
		x2x = xCor[bx] - x;
		y2y = yCor[by] - y;
		xx1 = x - xCor[ax];
		yy1 = y - yCor[ay];

		vel = 1.0 / (x2x1 * y2y1) * (
			q11 * x2x * y2y +
			q21 * xx1 * y2y +
			q12 * x2x * yy1 +
			q22 * xx1 * yy1
			);

		vel = vel / 1000.0;

		/* give velocity to face*/
		F_PROFILE(f, th, index) = vel;
	}
	end_f_loop(f, th)
}
ejpostema is offline   Reply With Quote

Old   October 26, 2016, 09:19
Default
  #8
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
In general: debug in serial mode, not in parallel. In parallel, you will get MPI-warnings that are not so meaningful, in serial you have a bigger chance of seeing something useful.

More specific:

You define numArray:

Code:
float numArray[12][15];
This means the first index can run from 0 to 11, and the second index from 0-14.

But in your code, you assign it up to
Code:
numArray[15][12] = 0;
Note two things:
1. The order of 12 and 15 is reversed;
2. You try to assign more floats in your array than assigned.

One way to solve this: define your numArray differently:
Code:
float numArray[16][13];
This is the first problem I saw, I stopped looking after this.
ejpostema likes this.
pakk is offline   Reply With Quote

Old   October 31, 2016, 03:18
Default
  #9
New Member
 
Erik Jan
Join Date: Aug 2016
Posts: 7
Rep Power: 9
ejpostema is on a distinguished road
Thanks, I feel stupid. haha.

Checked the code and fixed some more errors like that. I am now getting a divergence error, which probably means I am getting closer

I'll look into it
ejpostema is offline   Reply With Quote

Old   November 9, 2016, 06:57
Default
  #10
New Member
 
Erik Jan
Join Date: Aug 2016
Posts: 7
Rep Power: 9
ejpostema is on a distinguished road
So taking a bit more care about the code itself I managed to complete de UDF.

there are some problems with the coordinates. I can solve a self made 3d tube, but not any other surfaces

I'll post it here when I have finished it.
Thanks
ejpostema 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
Fluent Radiation/porous media Schmitt pierre-Louis FLUENT 26 September 1, 2016 10:29
fluent udf problem: write specific data for every iteration in a file. nnvoro Fluent UDF and Scheme Programming 1 May 27, 2013 15:26
Help! Compiled UDF problem 4 Wave tank tutorial Shane FLUENT 1 September 3, 2010 02:32
Problem with my udf july Fluent UDF and Scheme Programming 3 June 20, 2010 06:56
problem with running UDF in batch mode James FLUENT 0 June 6, 2006 06:49


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