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

writing ADJUST macro for Parallel FLUENT

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   April 21, 2016, 23:29
Default writing ADJUST macro for Parallel FLUENT
  #1
Member
 
Peter Aestas
Join Date: Dec 2013
Posts: 64
Rep Power: 13
aestas is on a distinguished road
hi friends,

i am trying to writing an ADJUST macro for Parallel FLUENT for a transient case. Part of the function of My ADJUST is to get the temperature in a certain point at every time step, and the UDF is based on the example in UDF manual.

But when i ran the program, it works quite slow. My guess is that, in every timestep, the ADJUST marco will loop at every single iterations.

I am trying to figure out a way, let this part of ADJUST only loop one time in a sing timestep, but i have not found yet. Does anyone have a good idea?

Code:
DEFINE_ADJUST(my_adjust, d)
{

#if !RP_HOST
	Domain *domain=Get_Domain(1);
	Thread *thread,*thread_1;
	face_t f;
	Thread *t,*t0,*t1;
	cell_t c,c0,c1;
	real x[ND_ND];
	real thermal_con;
#else
	int i;
	int yushu;
	real CENTROID_T;
	real melt_depth;
	real solid_x,liquid_x;
#endif

#if !RP_NODE

	FILE *fp = NULL,*fp1 = NULL;
	char filename[]="test.txt";
	char filename1[]="test1.txt";
#endif
#if PARALLEL
	int size; /* data passing variables */
	real *array;
	real *x_array;
	real *y_array;
	real *fraction_array;
	real xx[ND_ND];
	int pe;


#endif
	/* Only Serial and Compute Nodes have data on threads */
#if !RP_HOST
	thread=Lookup_Thread(domain,WALLID);
#endif
#if !RP_NODE /* SERIAL or HOST */
	if ((fp = fopen(filename, "a"))==NULL)
		Message("\n Warning: Unable to open %s for writing\n",filename);
	else
		Message("\nWriting Pressure to %s...",filename);
#endif
	/* UDF Now does 3 different things depending on SERIAL, NODE or HOST */
	// #if !PARALLEL /* SERIAL */
	// 	begin_f_loop(f,thread)
	// 		fprintf(fp, "%g\n", F_T(f,thread));/* Simply write out pressure data */
	// 	end_f_loop(f,thread)
	// #endif /* !PARALLEL */
#if RP_NODE
	/* Each Node loads up its data passing array */
	size=THREAD_N_ELEMENTS_INT(thread);
	array = (real *)malloc(size * sizeof(real));
	x_array = (real *)malloc(size * sizeof(real));
	y_array = (real *)malloc(size * sizeof(real));


	if(temp_time!=CURRENT_TIME)
  {
		begin_f_loop_int(f,thread)
		{
			array[f]= F_T(f,thread);
			F_CENTROID(xx,f,thread);
			x_array[f]=xx[0];
			y_array[f]=xx[1];

		}
	}
	end_f_loop_int(f,thread)

		/* Set pe to destination node */
		/* If on node_0 send data to host */
		/* Else send to node_0 because */
		/* compute nodes connect to node_0 & node_0 to host */
		pe = (I_AM_NODE_ZERO_P) ? node_host : node_zero;
	PRF_CSEND_INT(pe, &size, 1, myid);
	PRF_CSEND_REAL(pe, array, size, myid);
	PRF_CSEND_REAL(pe, x_array, size, myid);
	PRF_CSEND_REAL(pe, y_array, size, myid);

	free(array);
	free(x_array);
	free(y_array);/* free array on nodes once data sent */
	/* node_0 now collect data sent by other compute nodes */
	/* and sends it straight on to the host */
	if (I_AM_NODE_ZERO_P)
		compute_node_loop_not_zero (pe)
	{
		PRF_CRECV_INT(pe, &size, 1, pe);
		array = (real *)malloc(size * sizeof(real));
		x_array=(real *)malloc(size * sizeof(real));
		y_array=(real *)malloc(size * sizeof(real));


		PRF_CRECV_REAL(pe, array, size, pe);
		PRF_CRECV_REAL(pe, x_array, size, pe);
		PRF_CRECV_REAL(pe, y_array, size, pe);

		PRF_CSEND_INT(node_host, &size, 1, myid);
		PRF_CSEND_REAL(node_host, array, size, myid);
		PRF_CSEND_REAL(node_host, x_array, size, myid);
		PRF_CSEND_REAL(node_host, y_array, size, myid);
		free((char *)array);
		free((char *)x_array);
		free((char *)y_array);
	}
#endif /* RP_NODE */

#if RP_HOST
	CENTROID_T=0.0;


	compute_node_loop (pe) /* only acts as a counter in this loop */
	{
		/* Receive data sent by each node and write it out to the file */
		PRF_CRECV_INT(node_zero, &size, 1, node_zero);
		array = (real *)malloc(size * sizeof(real));
		x_array=(real *)malloc(size * sizeof(real));
		y_array=(real *)malloc(size * sizeof(real));

		PRF_CRECV_REAL(node_zero, array, size, node_zero);
		PRF_CRECV_REAL(node_zero, x_array, size, node_zero);
		PRF_CRECV_REAL(node_zero, y_array, size, node_zero);
// 		for (i=0; i<size; i++)
// 
// 		fprintf(fp, "current time=%lf, x=%lf,y=%lf, temperature=%lf\n",CURRENT_TIME*1e3, x_array[i],y_array[i],array[i]);
		if(temp_time!=CURRENT_TIME)
		{
			for (i=0; i<size; i++)
			{
				if(x_array[i]<0.6*0.005e-3&&y_array[i]>hr/2*h&&y_array[i]<(0.6+hr/2)*h)
				{
					CENTROID_T=array[i];
					break;

				}

			}
		}

		free(array);
		free(x_array);
		free(y_array);
	}
	if(temp_time!=CURRENT_TIME)
	{
		temp_time=CURRENT_TIME;
		fprintf(fp, "current time=%lf, temperature=%lf\n",CURRENT_TIME*1.0e3, CENTROID_T);
	}

#endif /* RP_HOST */
#if !RP_NODE /* SERIAL or HOST */
	fclose(fp); /* Close the file that was only opened if on SERIAL or HOST */
	Message("Done\n");
#endif
}
aestas is offline   Reply With Quote

Old   April 22, 2016, 00:52
Default
  #2
`e`
Senior Member
 
Join Date: Mar 2015
Posts: 892
Rep Power: 18
`e` is on a distinguished road
Use the first_iteration variable (inbuilt within Fluent; don't initialise or declare this variable):

Code:
if (first_iteration)
{
    // code only executed at the first iteration of a time step
}
This variable has been available since at least v15, otherwise use the long-winded method listed in the UDF manual under Time-Dependent Macros.
`e` is offline   Reply With Quote

Old   April 22, 2016, 03:12
Default
  #3
Member
 
Peter Aestas
Join Date: Dec 2013
Posts: 64
Rep Power: 13
aestas is on a distinguished road
Thanks so much! i find the first_iteration variable in UDF manual, and i have solved this problem by using this!


Quote:
Originally Posted by `e` View Post
Use the first_iteration variable (inbuilt within Fluent; don't initialise or declare this variable):

Code:
if (first_iteration)
{
    // code only executed at the first iteration of a time step
}
This variable has been available since at least v15, otherwise use the long-winded method listed in the UDF manual under Time-Dependent Macros.
aestas is offline   Reply With Quote

Old   April 22, 2016, 03:24
Default
  #4
Senior Member
 
Join Date: Feb 2010
Posts: 164
Rep Power: 17
gearboy is on a distinguished road
USE DEFINE_EXECUTE_AT_END instead. It will be executed once at each time step end.

Quote:
Originally Posted by aestas View Post
hi friends,

i am trying to writing an ADJUST macro for Parallel FLUENT for a transient case. Part of the function of My ADJUST is to get the temperature in a certain point at every time step, and the UDF is based on the example in UDF manual.

But when i ran the program, it works quite slow. My guess is that, in every timestep, the ADJUST marco will loop at every single iterations.

I am trying to figure out a way, let this part of ADJUST only loop one time in a sing timestep, but i have not found yet. Does anyone have a good idea?

Code:
DEFINE_ADJUST(my_adjust, d)
{

#if !RP_HOST
    Domain *domain=Get_Domain(1);
    Thread *thread,*thread_1;
    face_t f;
    Thread *t,*t0,*t1;
    cell_t c,c0,c1;
    real x[ND_ND];
    real thermal_con;
#else
    int i;
    int yushu;
    real CENTROID_T;
    real melt_depth;
    real solid_x,liquid_x;
#endif

#if !RP_NODE

    FILE *fp = NULL,*fp1 = NULL;
    char filename[]="test.txt";
    char filename1[]="test1.txt";
#endif
#if PARALLEL
    int size; /* data passing variables */
    real *array;
    real *x_array;
    real *y_array;
    real *fraction_array;
    real xx[ND_ND];
    int pe;


#endif
    /* Only Serial and Compute Nodes have data on threads */
#if !RP_HOST
    thread=Lookup_Thread(domain,WALLID);
#endif
#if !RP_NODE /* SERIAL or HOST */
    if ((fp = fopen(filename, "a"))==NULL)
        Message("\n Warning: Unable to open %s for writing\n",filename);
    else
        Message("\nWriting Pressure to %s...",filename);
#endif
    /* UDF Now does 3 different things depending on SERIAL, NODE or HOST */
    // #if !PARALLEL /* SERIAL */
    //     begin_f_loop(f,thread)
    //         fprintf(fp, "%g\n", F_T(f,thread));/* Simply write out pressure data */
    //     end_f_loop(f,thread)
    // #endif /* !PARALLEL */
#if RP_NODE
    /* Each Node loads up its data passing array */
    size=THREAD_N_ELEMENTS_INT(thread);
    array = (real *)malloc(size * sizeof(real));
    x_array = (real *)malloc(size * sizeof(real));
    y_array = (real *)malloc(size * sizeof(real));


    if(temp_time!=CURRENT_TIME)
  {
        begin_f_loop_int(f,thread)
        {
            array[f]= F_T(f,thread);
            F_CENTROID(xx,f,thread);
            x_array[f]=xx[0];
            y_array[f]=xx[1];

        }
    }
    end_f_loop_int(f,thread)

        /* Set pe to destination node */
        /* If on node_0 send data to host */
        /* Else send to node_0 because */
        /* compute nodes connect to node_0 & node_0 to host */
        pe = (I_AM_NODE_ZERO_P) ? node_host : node_zero;
    PRF_CSEND_INT(pe, &size, 1, myid);
    PRF_CSEND_REAL(pe, array, size, myid);
    PRF_CSEND_REAL(pe, x_array, size, myid);
    PRF_CSEND_REAL(pe, y_array, size, myid);

    free(array);
    free(x_array);
    free(y_array);/* free array on nodes once data sent */
    /* node_0 now collect data sent by other compute nodes */
    /* and sends it straight on to the host */
    if (I_AM_NODE_ZERO_P)
        compute_node_loop_not_zero (pe)
    {
        PRF_CRECV_INT(pe, &size, 1, pe);
        array = (real *)malloc(size * sizeof(real));
        x_array=(real *)malloc(size * sizeof(real));
        y_array=(real *)malloc(size * sizeof(real));


        PRF_CRECV_REAL(pe, array, size, pe);
        PRF_CRECV_REAL(pe, x_array, size, pe);
        PRF_CRECV_REAL(pe, y_array, size, pe);

        PRF_CSEND_INT(node_host, &size, 1, myid);
        PRF_CSEND_REAL(node_host, array, size, myid);
        PRF_CSEND_REAL(node_host, x_array, size, myid);
        PRF_CSEND_REAL(node_host, y_array, size, myid);
        free((char *)array);
        free((char *)x_array);
        free((char *)y_array);
    }
#endif /* RP_NODE */

#if RP_HOST
    CENTROID_T=0.0;


    compute_node_loop (pe) /* only acts as a counter in this loop */
    {
        /* Receive data sent by each node and write it out to the file */
        PRF_CRECV_INT(node_zero, &size, 1, node_zero);
        array = (real *)malloc(size * sizeof(real));
        x_array=(real *)malloc(size * sizeof(real));
        y_array=(real *)malloc(size * sizeof(real));

        PRF_CRECV_REAL(node_zero, array, size, node_zero);
        PRF_CRECV_REAL(node_zero, x_array, size, node_zero);
        PRF_CRECV_REAL(node_zero, y_array, size, node_zero);
//         for (i=0; i<size; i++)
// 
//         fprintf(fp, "current time=%lf, x=%lf,y=%lf, temperature=%lf\n",CURRENT_TIME*1e3, x_array[i],y_array[i],array[i]);
        if(temp_time!=CURRENT_TIME)
        {
            for (i=0; i<size; i++)
            {
                if(x_array[i]<0.6*0.005e-3&&y_array[i]>hr/2*h&&y_array[i]<(0.6+hr/2)*h)
                {
                    CENTROID_T=array[i];
                    break;

                }

            }
        }

        free(array);
        free(x_array);
        free(y_array);
    }
    if(temp_time!=CURRENT_TIME)
    {
        temp_time=CURRENT_TIME;
        fprintf(fp, "current time=%lf, temperature=%lf\n",CURRENT_TIME*1.0e3, CENTROID_T);
    }

#endif /* RP_HOST */
#if !RP_NODE /* SERIAL or HOST */
    fclose(fp); /* Close the file that was only opened if on SERIAL or HOST */
    Message("Done\n");
#endif
}
gearboy is offline   Reply With Quote

Old   April 22, 2016, 03:44
Default
  #5
Member
 
Peter Aestas
Join Date: Dec 2013
Posts: 64
Rep Power: 13
aestas is on a distinguished road
Thank you my friend, i tried your suggestion, and find it also perfectly solved my problem!
Quote:
Originally Posted by gearboy View Post
USE DEFINE_EXECUTE_AT_END instead. It will be executed once at each time step end.
aestas 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
CFX vs. FLUENT turbo CFX 4 April 13, 2021 09:08
How to Mix AIR and WATER Elvis1991 FLUENT 12 December 1, 2016 13:28
writing the values of particles in fluent krishna.n Fluent Multiphase 0 April 18, 2016 02:57
what is the syntax for writing inverse hyperbolic functions in UDFs ANSYS FLUENT murthya3 Fluent UDF and Scheme Programming 9 March 2, 2016 16:35
HELP! Fluent macro definition hamish888 FLUENT 1 November 1, 2010 07:12


All times are GMT -4. The time now is 18:41.