2.2.11.  DEFINE_RW_FILE

2.2.11.1. Description

DEFINE_RW_FILE is a general-purpose macro that you can use to specify customized information that is to be written to a case or data file, or read from a case or data file. You can save and restore custom variables of any data type (for example, integer, real, CXBoolean, structure) using DEFINE_RW_FILE. It is often useful to save dynamic information (for example, number of occurrences in conditional sampling) while your solution is being calculated, which is another use of this function. Note that the read order and the write order must be the same when you use this function.

When using the DEFINE_RW_FILE, you should be attentive to which parts of the function should be done by the host, and which by the compute nodes. For details, see Compiler Directives.


Important:  Starting in ANSYS Fluent version 18.2, the DEFINE_RW_FILE macro is not supported for serial UDFs in the following circumstances: in a compiled UDF on Windows 2012 or earlier; in an interpreted UDF on any platform. As a workaround for such unsupported combinations, you can parallelize the UDF (as described in Parallelizing Your Serial UDF).


2.2.11.2. Usage

DEFINE_RW_FILE (name, fp)

Argument Type

Description

symbol name

UDF name.

FILE *fp

Pointer to the file you are reading or writing.

Function returns

void

There are two arguments to DEFINE_RW_FILE: name and fp. You supply name, the name of the UDF. fp is passed from the solver to the UDF.

2.2.11.3. Example

The following C source code listing contains examples of functions that write information to a data file and read it back. These functions are concatenated into a single source file that can be interpreted or compiled in ANSYS Fluent.

/***********************************************************************
   UDFs that increment a variable, write it to a data file
   and read it back in
 ************************************************************************/
 #include "udf.h"
 
 int kount = 0; /* define global variable kount */
 
 DEFINE_ADJUST(demo_calc,d)
 {
    kount++;
    printf("kount = %d\n",kount);
 }
 DEFINE_RW_FILE(writer,fp)
 {
    printf("Writing UDF data to data file...\n");
 #if !RP_NODE
    fprintf(fp,"%d",kount); /* write out kount to data file */
 #endif
 }
 DEFINE_RW_FILE(reader,fp)
 {
    printf("Reading UDF data from data file...\n");
 #if !RP_NODE
    fscanf(fp,"%d",&kount); /* read kount from data file */
 #endif
 } 

At the top of the listing, the integer kount is defined and initialized to zero. The first function (demo_calc) is an ADJUST function that increments the value of kount at each iteration, since the ADJUST function is called once per iteration. (See DEFINE_ADJUST for more information about ADJUST functions.) The second function (writer) instructs ANSYS Fluent to write the current value of kount to the data file, when the data file is saved. The third function (reader) instructs ANSYS Fluent to read the value of kount from the data file, when the data file is read.

The functions work together as follows. If you run your calculation for, say, 10 iterations (kount has been incremented to a value of 10) and save the data file, then the current value of kount (10) will be written to your data file. If you read the data back into ANSYS Fluent and continue the calculation, kount will start at a value of 10 and will be incremented at each iteration. Note that you can save as many static variables as you want, but you must be sure to read them in the same order in which they are written.

2.2.11.4. Hooking a Read/Write Case or Data File UDF to ANSYS Fluent

After the UDF that you have defined using DEFINE_RW_FILE is interpreted (Interpreting UDFs) or compiled (Compiling UDFs), the name of the argument that you supplied as the first DEFINE macro argument (for example, writer) will become visible and selectable via the User-Defined Function Hooks dialog box in ANSYS Fluent. Note that you can hook multiple read/write functions to your model. See Hooking DEFINE_RW_FILE and DEFINE_RW_HDF_FILE UDFs for details.


Release 18.2 - © ANSYS, Inc. All rights reserved.