3.7. Input/Output Functions

Ansys Fluent provides some utilities in addition to the standard C I/O functions that you can use to perform input/output (I/O) tasks. These are listed below and are described in the following sections:

Message(format, ...)

prints a message to the console

Error(format, ...)

prints an error message to the console

par_fprintf_head(fp, format, ...)

prints a header line at the top of a sample file when using the DEFINE_DPM_OUTPUT macro

par_fprintf(fp, format, ...)

prints one line of particle information to a sample file when using the DEFINE_DPM_OUTPUT macro

3.7.1. Message

The Message function is a utility that displays data to the console in a format that you specify.

int Message(const char *format,...);

The first argument in the Message function is the format string. It specifies how the remaining arguments are to be displayed in the console. The format string is defined within quotes. The value of the replacement variables that follow the format string will be substituted in the display for all instances of %type. The % character is used to designate the character type. Some common format characters are: %d for integers, %f for floating point numbers, %g for double data type, and %e for floating point numbers in exponential format (with e before the exponent). Consult a C programming language manual for more details. The format string for Message is similar to printf, the standard C I/O function (see Standard I/O Functions for details).

In the example below, the text Volume integral of turbulent dissipation: will be displayed in the console, and the value of the replacement variable, sum_diss, will be substituted in the message for all instances of %g.

Example:

 Message("Volume integral of turbulent dissipation: %g\n", sum_diss);
  /* g represents floating point number in f or e format */
  /* \n denotes a new line */ 

Important:  It is recommended that you use Message instead of printf in compiled UDFs.


3.7.2. Error

You can use Error when you want to stop execution of a UDF and print an error message to the console.

Example:

 if (table_file == NULL)
 Error("error reading file"); 

Important:   Error is not supported by the interpreter and can be used only in compiled UDFs.


3.7.3. The par_fprintf_head and par_fprintf Functions

When using DEFINE_DPM_OUTPUT to write a DPM sample file, the special text output functions par_fprintf and par_fprintf_head must be used in place of the C I/O function fprintf. Typically, one or more header lines is written to the top of the file with column headings or other non-repeating information using par_fprintf_head. Following the header, a line is written for each particle sample using par_fprintf. Further details and examples are provided in the following sections:

3.7.3.1. par_fprintf_head

The par_fprintf_head function generates a header at the top of the DPM sample file.

int par_fprintf_head(FILE *fp, const char *format, ...);

The first argument is the file pointer provided by the calling routine defined with DEFINE_DPM_OUTPUT. The second argument is the format string, used as described for the Message function in Message . An example of the usage of par_fprintf_head is given below:

Example:

par_fprintf_head(fp, "x-coordinate y-coordinate z-coordinate\n");

This prints the column names x-coordinate, y-coordinate, and z-coordinate followed by a line feed at the top of the file indicated by FILE pointer fp. Multiple calls can be made to par_fprintf_head as needed to write all desired information to the top of the sample file.

For an illustration of the use of par_fprintf_head within a DEFINE_DPM_OUTPUT UDF refer to the Example provided in DEFINE_DPM_OUTPUT .

3.7.3.2. par_fprintf

The par_fprintf function writes a single particle sample line into a DPM sample file.

int par_fprintf(FILE *fp, const char *format, ...);

The first argument is the file pointer provided by the calling routine defined with DEFINE_DPM_OUTPUT. The second argument is the format string, used as described for the Message function in Message . When used with par_fprintf, the first two replacement variables in the format string must be the particle injection ID and particle ID, respectively. The rest of the format string can be chosen to specify the output that will be written and its formatting.

For an illustration of the use of par_fprintf within a DEFINE_DPM_OUTPUT UDF refer to Example 1 - Sampling and Removing Particles in DEFINE_DPM_OUTPUT .