A.13. C Library Functions

C compilers include a library of standard mathematical and I/O functions that you can use when you write your UDF code. Lists of standard C library functions are presented in the following sections. Definitions for standard C library functions can be found in various header files (for example, global.h). These header files are all included in the udf.h file.

A.13.1. Trigonometric Functions

The trigonometric functions shown below are computed (with one exception) for the variable x. Both the function and the argument are double-precision real variables. The function acos(x) is the arccosine of the argument x, . The function atan2(x,y) is the arctangent of x/y, . The function cosh(x) is the hyperbolic cosine function, and so on.

double acos (double x);

Returns the arccosine of x

double asin (double x);

Returns the arcsine of x

double atan (double x);

Returns the arctangent of x

double atan2 (double x, double y);

Returns the arctangent of x/y

double cos (double x);

Returns the cosine of x

double sin (double x);

Returns the sine of x

double tan (double x);

Returns the tangent of x

double cosh (double x);

Returns the hyperbolic cosine of x

double sinh (double x);

Returns the hyperbolic sine of x

double tanh (double x);

Returns the hyperbolic tangent of x

A.13.2. Miscellaneous Mathematical Functions

The C functions shown on the left below correspond to the mathematical functions shown on the right.

double sqrt (double x);

double pow(double x, double y);

double exp (double x);

double log (double x);

ln

double log10 (double x);

double fabs (double x);

double ceil (double x);

smallest integer not less than

double floor (double x);

largest integer not greater than

A.13.3. Standard I/O Functions

A number of standard input and output (I/O) functions are available in C and in Ansys Fluent. They are listed below. All of the functions work on a specified file except for printf, which displays information that is specified in the argument of the function. The format string argument is the same for printf, fprintf, and fscanf. Note that all of these standard C I/O functions are supported by the interpreter, so you can use them in either interpreted or compiled UDFs. For more information about standard I/O functions in C, you should consult a reference guide (for example, [6]).

Common C I/O Functions

fopen("filename", "mode");

opens a file

fclose(fp);

closes a file

printf("format", ...);

formatted print to the console

fprintf(fp, "format",...);

formatted print to a file

fscanf(fp, "format",...);

formatted read from a file


Important:  It is not possible to use the scanf C function in Ansys Fluent.



Important:  These standard C I/O functions cannot be used when using the DEFINE_DPM_OUTPUT macro due to certain file operations that must be performed by Ansys Fluent. In this case, Ansys Fluent will handle file opening and closing. The macros par_fprintf and par_fprintf_head are provided for writing output to the file. For additional details, refer to The par_fprintf_head and par_fprintf Functions and DEFINE_DPM_OUTPUT .


A.13.3.1. fopen

FILE *fopen(const char *filename, const char *mode);

The function fopen opens a file in the mode that you specify. It takes two arguments: filename and mode. filename is a pointer to the file you want to open. mode is the mode in which you want the file opened. The options for mode are read "r", write "w", and append "a". Both arguments must be enclosed in quotes. The function returns a pointer to the file that is to be opened.

Before using fopen, you will first need to define a local pointer of type FILE that is defined in stdio.h (for example, fp). Then, you can open the file using fopen, and assign it to the local pointer as shown below. Recall that stdio.h is included in the udf.h file, so you do not have to include it in your function.

FILE *fp; /* define a local pointer fp of type FILE */
     fp = fopen("data.txt","r");  /* open a file named data.txt in
                read-only mode and assign it to fp */ 

A.13.3.2. fclose

int fclose(FILE *fp);

The function fclose closes a file that is pointed to by the local pointer passed as an argument (for example, fp).

fclose(fp);  /* close the file pointed to by fp */ 

A.13.3.3. printf

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

The function printf is a general-purpose printing function that prints to the console in a format that you specify. The first argument 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 values of the replacement variables specified as arguments following the format string will be substituted sequentially for each instance of %type in the format string. The % character is used to designate the format type. Some common format characters are: %d for integers, %f for floating point numbers, and %e for floating point numbers in exponential format (with e before the exponent). The format string for printf is the same as for fprintf and fscanf.

In the example below, the text Content of variable a is: will be displayed in the console, and the value of the replacement variable, a, will be substituted in the message for %d.

Example:

int a = 5;
 printf("Content of variable a is: %d\n", a); /* \n denotes a new line */ 

Important:  It is recommended that you use the Ansys Fluent Message utility instead of printf for compiled UDFs. See Message for details on the Message macro.


A.13.3.4. fprintf

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

The function fprintf writes to a file that is pointed to by fp, in a format that you specify. The second argument is the format string. It specifies how the remaining arguments are to be written to the file. The format string for fprintf is the same as for printf and fscanf.

Example:

FILE *fp;
 fprintf(fp,"%12.4e %12.4e %5d\n",x_array[j][0], x_array[j][1], noface);
 
 int data1 = 64.25;
 int data2 = 97.33;
 fprintf(fp, "%4.2d %4.2d\n", data1, data2);

Important:  Note that the standard C function fprintf cannot be used when using a DEFINE_DPM_OUTPUT macro because certain additional file operations must be handled by Ansys Fluent. For file writing in a DEFINE_DPM_OUTPUT macro the par_fprintf and par_fprintf_head macros must be used. For additional details, refer to The par_fprintf_head and par_fprintf Functions and DEFINE_DPM_OUTPUT .


A.13.3.5. fscanf

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

The function fscanf reads from a file that is pointed to by fp, in a format that you specify. The second argument is the format string. It specifies how the data that is to be read is to be interpreted. The replacement variables that follow the format string are used to store values that are read. The replacement variables are preceded by the & character. Note that the format string for fscanf is the same as for fprintf and printf.

In the example below, two floating point numbers are read from the file pointed to by fp, and are stored in the variables f1 and f2.

Example:

FILE *fp;
 fscanf(fp, "%f %f", &f1, &f2);

Important:  You cannot use the scanf I/O function in Ansys Fluent. You must use fscanf instead.