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

How do I read/write files in parallel using UDF?

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   December 6, 2022, 05:57
Default How do I read/write files in parallel using UDF?
  #1
New Member
 
Join Date: Nov 2022
Posts: 10
Rep Power: 3
rephical is on a distinguished road
Hey all, I'm trying to create a udf that reads a file containing x,y,z coordinates and then returning the value of the cell nearest to it. I managed to create the code, but I have trouble parallelizing it. At first, I had trouble because ansys repeats my udf 4 times (which is equal to the number of solver processes I input), so I decided to just use my udf on compute node zero by using if(I_AM_NODE_ZERO_P). However, I began to realize that this will fail because the other compute nodes will not be processed, and thus the I will not get the cell values at those compute nodes. My question now is, how do I parallelize reading/writing csv files using UDF? I consulted the ansys documentation on parallelizing udf, but I do not know how to start. Here is my code:

Code:
#include <udf.h>
#include <cxndsearch.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static ND_Search *domain_table = NULL;

DEFINE_ON_DEMAND(find_cells)
{
    #if !RP_HOST
    if(I_AM_NODE_ZERO_P){
    cell_t c;
    Thread *t;
    CX_Cell_Id *cx_cell;
    real cCentroid;
    real cVolume;


    real P[3];
    real P_Cell[3];

    FILE *fp;
    char line[1024];
    int rayID, side, prevID, nextID, surfID;
    double x,y,z;

    fp = fopen("rays_csv_new.csv","r");

    if(fp == NULL){
        printf("Trouble reading file\nAborting..");
        fclose(fp);
        abort();
    }

    domain_table = CX_Start_ND_Point_Search(domain_table,TRUE,-1);
    Message("Starting Cell Search...");

    while(fgets(line,1024,fp)){
        sscanf(line,"%d,%lg,%lg,%lg,%d,%d,%d,%d",&rayID,&x,&y,&z,&side,&prevID,&nextID,&surfID);
        P[0] = x;
        P[1] = y;
        P[2] = z;

        cx_cell = CX_Find_Cell_With_Point(domain_table,P,0.0);

        if(cx_cell){
            c = RP_CELL(cx_cell);
            t = RP_THREAD(cx_cell);
            cCentroid = C_CENTROID(P_Cell,c,t);
            cVolume = C_VOLUME(c,t);

            Message("%g \t %g \t %g \t %g\n",P[0],P[1],P[2],cVolume);

        }else{
            Message("Could not find cell at [%g,%g,%g]\n",P[0],P[1],P[2]);
        }
    }
    domain_table = CX_End_ND_Point_Search(domain_table);
    fclose(fp);
    }
    #endif
}
I tested my code the following dataset:

Code:
x y z
0.227898,1.0,-0.448053
4,-0.106937,1.0,0.379007
7,-0.350153,1.0,0.031367
9,0.293532,1.0,0.357582
11,-0.403077,1.0,-0.409584
13,0.476352,1.0,-0.0787738
15,-0.400827,1.0,-0.0563139
18,0.189004,1.0,0.217153
Since I was only processing compute node 0, the cells that are within compute node 0 will be found, but those that are outside it won't. As a result, the output looks like this:

Code:
Could not find cell at [0.227898,1,-0.448053]
Could not find cell at [-0.106937,1,0.379007]
Could not find cell at [-0.350153,1,0.0313674]
0.293532 	 1 	 0.357582 	 1.82506e-08
Could not find cell at [-0.403077,1,-0.409584]
Could not find cell at [0.476352,1,-0.0787738]
Could not find cell at [-0.400827,1,-0.0563139]
0.189004 	 1 	 0.217153 	 1.95857e-08
rephical is offline   Reply With Quote

Old   December 7, 2022, 02:26
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
try this code,
modify FL variable which represents the number of lines into your table file
Code:
#include <udf.h>
#include <cxndsearch.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FL	100 /*file length*/

static ND_Search *domain_table = NULL;
int rayID[FL], side[FL], prevID[FL], nextID[FL], surfID[FL];
real x[FL],y[FL],z[FL];
	
void my_read_from_table(char *file)
{
	FILE *fp;
	char line[1024];
	int i=0;
	fp = fopen(file,"r");

    if(fp == NULL){
        printf("Trouble reading file\nAborting..");
        fclose(fp);
        abort();
    }	
	while(fgets(line,1024,fp)){
        sscanf(line,"%d,%lf,%lf,%lf,%d,%d,%d,%d",&rayID[i],&x[i],&y[i],&z[i],&side[i],&prevID[i],&nextID[i],&surfID[i]);
		i++;
	}
	fclose(fp);
	Message("DEBUG: last line from table:\n %d,%f,%f,%f,%d,%d,%d,%d \n",rayID[FL],x[FL],y[FL],z[FL],side[FL],prevID[FL],nextID[FL],surfID[FL]);
}

DEFINE_ON_DEMAND(find_cells)
{
    cell_t c;
    Thread *t;
    CX_Cell_Id *cx_cell;
    real cCentroid;
    real cVolume;
    real P[3];
    real P_Cell[3];
	int i;

	#if !RP_NODE
		my_read_from_table("rays_csv_new.csv");
	#endif
	host_to_node_int(rayID,FL);
	host_to_node_int(side,FL);
	host_to_node_int(prevID,FL);
	host_to_node_int(nextID,FL);
	host_to_node_int(surfID,FL);
	
	host_to_node_int(x,FL);
	host_to_node_int(y,FL);
	host_to_node_int(z,FL);
	#if !RP_HOST
    domain_table = CX_Start_ND_Point_Search(domain_table,TRUE,-1);
    Message0("Starting Cell Search...");

	for (i=0;i<FL;i++)
	{
		P[0] = x[i];
        P[1] = y[i];
        P[2] = z[i];	
		cx_cell = CX_Find_Cell_With_Point(domain_table,P,0.0);

        if(cx_cell){
            c = RP_CELL(cx_cell);
            t = RP_THREAD(cx_cell);
            cCentroid = C_CENTROID(P_Cell,c,t);
            cVolume = C_VOLUME(c,t);

            Message0("%f \t %f \t %f \t %f\n",P[0],P[1],P[2],cVolume);

        }else{
            Message0("Could not find cell at [%f,%f,%f]\n",P[0],P[1],P[2]);
        }
    }
    domain_table = CX_End_ND_Point_Search(domain_table);
    #endif
}
__________________
best regards


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

Old   December 7, 2022, 02:43
Default
  #3
New Member
 
Join Date: Nov 2022
Posts: 10
Rep Power: 3
rephical is on a distinguished road
Quote:
Originally Posted by AlexanderZ View Post
try this code,
modify FL variable which represents the number of lines into your table file
Code:
#include <udf.h>
#include <cxndsearch.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FL	100 /*file length*/

static ND_Search *domain_table = NULL;
int rayID[FL], side[FL], prevID[FL], nextID[FL], surfID[FL];
real x[FL],y[FL],z[FL];
	
void my_read_from_table(char *file)
{
	FILE *fp;
	char line[1024];
	int i=0;
	fp = fopen(file,"r");

    if(fp == NULL){
        printf("Trouble reading file\nAborting..");
        fclose(fp);
        abort();
    }	
	while(fgets(line,1024,fp)){
        sscanf(line,"%d,%lf,%lf,%lf,%d,%d,%d,%d",&rayID[i],&x[i],&y[i],&z[i],&side[i],&prevID[i],&nextID[i],&surfID[i]);
		i++;
	}
	fclose(fp);
	Message("DEBUG: last line from table:\n %d,%f,%f,%f,%d,%d,%d,%d \n",rayID[FL],x[FL],y[FL],z[FL],side[FL],prevID[FL],nextID[FL],surfID[FL]);
}

DEFINE_ON_DEMAND(find_cells)
{
    cell_t c;
    Thread *t;
    CX_Cell_Id *cx_cell;
    real cCentroid;
    real cVolume;
    real P[3];
    real P_Cell[3];
	int i;

	#if !RP_NODE
		my_read_from_table("rays_csv_new.csv");
	#endif
	host_to_node_int(rayID,FL);
	host_to_node_int(side,FL);
	host_to_node_int(prevID,FL);
	host_to_node_int(nextID,FL);
	host_to_node_int(surfID,FL);
	
	host_to_node_int(x,FL);
	host_to_node_int(y,FL);
	host_to_node_int(z,FL);
	#if !RP_HOST
    domain_table = CX_Start_ND_Point_Search(domain_table,TRUE,-1);
    Message0("Starting Cell Search...");

	for (i=0;i<FL;i++)
	{
		P[0] = x[i];
        P[1] = y[i];
        P[2] = z[i];	
		cx_cell = CX_Find_Cell_With_Point(domain_table,P,0.0);

        if(cx_cell){
            c = RP_CELL(cx_cell);
            t = RP_THREAD(cx_cell);
            cCentroid = C_CENTROID(P_Cell,c,t);
            cVolume = C_VOLUME(c,t);

            Message0("%f \t %f \t %f \t %f\n",P[0],P[1],P[2],cVolume);

        }else{
            Message0("Could not find cell at [%f,%f,%f]\n",P[0],P[1],P[2]);
        }
    }
    domain_table = CX_End_ND_Point_Search(domain_table);
    #endif
}
Hello, thanks for replying. The code seems to be buggy. most of the output I get is:
Could not find cell at [0.000000,0.000000,0.000000]
Could not find cell at [0.000000,0.000000,0.000000]
Could not find cell at [0.000000,0.000000,0.000000]

But I don't think this thread matters anymore since I've decided to just use a serial solver to do the udf. I was hoping you could help me with my other problem located here
rephical is offline   Reply With Quote

Reply

Tags
parallel, 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
Dynamic mesh in parallel udf rsarma Fluent UDF and Scheme Programming 3 August 2, 2018 07:02
UDF Compiling / Parallel mode MV78 Fluent UDF and Scheme Programming 6 May 29, 2018 05:02
Fluent UDF wrong number of cells in parallel - correct in serial dralexpe Fluent UDF and Scheme Programming 7 May 17, 2018 08:26
Strange problem: unresolved external symbol only for parallel UDF lisa_china Fluent UDF and Scheme Programming 1 May 29, 2017 22:50
Parallel Fluent +UDF Jack Martinez FLUENT 0 June 28, 2007 11:19


All times are GMT -4. The time now is 17:25.