Calling FORTRAN subroutines in FLUENT V6.1 User Defined Functions (UDFs) for linux platforms.

This solution describes, by example, the procedures that need to be followed to call a fortran subroutine from a udf written in C for FLUENT V6.1. This procedure has been tested on lnx86 and some modifications may be necessary to work on other platforms. The procedure assumes the use of compiled udfs.
To perform a fortran subroutine call from a udf in FLUENT V6.1, the udf source code must call the subroutine correctly, and the fortran object file must be accounted for during the udf make operation. The following example shows how a DEFINE_INIT udf stored in a file called callfortran.c can call a fortran subroutine. The callfortran.c source code is listed below:

#include "udf.h"

DEFINE_INIT(my_init, domain)

int nt = 1;
int ns = 2;
int result;

add_2_numbers__(&nt,&ns,&result); /*two underscores after add_2_numbers for linux system*/
Message("nThe result is %dn",result);


Fortran call arguments are passed by reference and so each variable in the above function call is preceded by &. The format of the subroutine call made depends upon the platform that the udf will be run on. In some instances(in the present case), the function name must have the underscore symbol appended to the name, i.e., add_2_numbers__(&nt,&ns,&result). This is not the case for hpux platforms but is most likely the case for sgi ,linux or sun systems. The fortran subroutine that defines the function add_2_numbers is listed below.


subroutine add_2_numbers(x,y,z)
integer x,y,z
z = x+y

Assume this function was stored in a separate file called fortranadd.f. The fortran source code can be compiled with the following command: f77 -c fortranadd.f. The -c flag forces the compiler to generate an object file (extension .o file consisting of machine language) without invoking the linker (which would account for additional fortran libraries). The resulting fortranadd.o object file must be stored in the appropriate directory, /architecture/2d or 3d. For this example, lnx86/2d/fortranadd.o. The udf file, callfortran.c, must be stored in the /src directory. The structure of UDF directory before compiling would be:

libudf/src/callfortran.c, makefile

To compile "callfortran.c" and link its object file with fortran subroutine object file, makefile needs to be altered at one place. Add the name of the fortran object file to USER_OBJECTS variable. Please note that if there are more than one fortran object file, then all the names of the files need to be added.Then compile the udf in libudf directory by make "FLUENT_ARCH=lnx86".

Part of modified make file would be:

# makefile for user defined functions.

# User modifiable section.
SOURCES= callfortran.c
FLUENT_INC= /usr/local/Fluent.Inc/release

# Precompiled User Object files (for example .o files from .f sources)
USER_OBJECTS= fortranadd.o

# Build targets (do not modify below this line).
we need two underscores after the Fortran function name only if the Fortran function name is already having underscores in its name. In the example given in 901, Fortran function name is "add_2_numbers". So, to work properly, we need to give two underscores (add_2_numbers__) in FLUENT UDF. If we change the Fortran function name to "add2numbers", then we should give "add2numbers_" (one underscore only) in the UDF.
Ex: If Fortran subroutine name is "calcdense", one underscore is sufficient in the UDF. But, if we change the function name to "calc_dense", we need two underscores in the UDF to make it work.

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