[ANSYS, Inc. Logo] return to home search
next up previous contents index

2.2.10 DEFINE_RW_FILE



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 (e.g., integer, real, CXBoolean, structure) using DEFINE_RW_FILE. It is often useful to save dynamic information (e.g., 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.



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.

figure   

DEFINE_RW_FILE cannot be used in UDFs that are executed on Windows systems.



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");
  fprintf(fp,"%d",kount); /* write out kount to data file */
}
DEFINE_RW_FILE(reader,fp)
{
  printf("Reading UDF data from data file...\n");
  fscanf(fp,"%d",&kount); /* read kount from data file */
}

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 Section  2.2.1 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.



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 (Chapter  4) or compiled (Chapter  5), the name of the argument that you supplied as the first DEFINE macro argument (e.g., 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 Section  6.1.7 for details.


next up previous contents index Previous: 2.2.9 DEFINE_ON_DEMAND
Up: 2.2 General Purpose DEFINE
Next: 2.3 Model-Specific DEFINE Macros
Release 12.0 © ANSYS, Inc. 2009-01-14