FLUENT 6 - Printing to the GUI or to an output file from UDFs


Output from a UDF using Message() only prints to the GUI. If the output is desired to go to a file, the fprintf function is commonly used. How can one easily switch between the two without having a lot of duplicate lines in the UDF code?
A single function is given to combine the two. This function, generically is called gf_print(FILE*, char* format, ...).

The first argument, if NULL, will direct the output to the GUI. If not NULL, the output is printed to the file as specified using the fopen statement. I've included the source code and a sample UDF that demos this capability. Parallel versions: gf_print0, and gf_print_cn are provided. The "0" function prints only from serial or the host node (in parallel). The "_cn" version only prints from compute node "cn" (the argument list is slightly different. If "cn < 0", the print is called for each compute node.

gf_print is a variable argument function, so it's calling list is of arbitrary length.

#include "udf.h"

void gf_print (FILE* file, const char* format, ...);
void gf_print0 (FILE* file, const char* format, ...);
void gf_print_cn(FILE* file, int cn, const char* format, ...);

DEFINE_ON_DEMAND(print_stuff)
{

int i = 10;
real x = 2.3;
char* outfile_name = "output.txt";
FILE* outfile = fopen(outfile_name, "a");

gf_print0 (NULL, "%10d %10.3e %sn", i, x, "gf_print0 to NULL");
gf_print_cn(NULL, 2, "%10d %10.3e %sn", i, x, "gf_print_cn to NULL");

gf_print0 (outfile, "%10d %10.3e %sn", i, x, "gf_print0 to NULL");
gf_print_cn(outfile, 2, "%10d %10.3e %sn", i, x, "gf_print_cn to NULL");

if (NNULLP(outfile)) fclose(outfile);

}



/************************************************************************
* In the following functions, if file = NULL, then print will be to GUI,
* otherwise, file is sent to stream "file"
************************************************************************/

void gf_print(FILE* file, const char* format, ...)
{
/* Gui or file print from serial or all host and compute nodes */

va_list ap;

if (file == NULL)
{
/* output to gui */
va_start(ap,format);
vfprintf(stdout, format, ap);
fflush(stdout);
va_end(ap);
}
else
{
/* output to file */
va_start(ap, format);
vfprintf(file, format, ap);
va_end(ap);
}
}



void gf_print0(FILE* file, const char* format, ...)
{

/* Gui or file print... host node or serial only */

#if !RP_NODE
va_list ap;
if (file == NULL)
{
/* output to gui */
va_start(ap,format);
vfprintf(stdout, format, ap);
fflush(stdout);
va_end(ap);
}
else
{
/* output to file */
va_start(ap, format);
vfprintf(file, format, ap);
va_end(ap);
}
#endif
}




void gf_print_cn(FILE* file, int cn, const char* format, ...)
{

/* Gui or file print... From compute node "cn" or if cn < 1,
all compute nodes */

#if RP_NODE
va_list ap;
if (cn < 0 || (myid == cn))
{
if (file == NULL)
{
/* output to gui */
va_start(ap,format);
vfprintf(stdout, format, ap);
fflush(stdout);
va_end(ap);
}
else
{
/* output to file */
va_start(ap, format);
vfprintf(file, format, ap);
va_end(ap);
}
}
#endif

}





Show Form
No comments yet. Be the first to add a comment!