CFD Online Discussion Forums

CFD Online Discussion Forums (
-   FLUENT (
-   -   Integration of a Custom C++ Model into FLUENT (

Syed Haider February 19, 2006 18:35

Integration of a Custom C++ Model into FLUENT
Hi fellow CFD practitioners:

We are trying to integrate a custom heat exchanger model into FLUENT 6.2.16 in a 64-bit Windows XP environment using the User Defined Function (UDF) approach. We have developed the steady-state heat exchanger model as a hierarchy of C++ classes and have compiled and tested it using Microsoft Visual C++ 6.0. Having the impression that the FLUENT compiler is strictly designed to handle UDFs only written in C, we tried the following C-interface between the FLUENT UDF DEFINE macro and the C++ heat exchanger model (being used as wrapper function).

#ifdef __cplusplus

extern "C"

{ #endif

// ***START:*Defning Interfacing C-Wrapper Function*********

void CFunction_Design_HX(double, int, double, double, double, double, int, double*, double*, double*); // Suitable to C return parameters

// ***END:***Defning Interfacing C-Wrapper Function*********

#ifdef __cplusplus



However, FLUENT did not compile the UDF, apparently because it did not recognize the .cpp files of the heat exchanger model, even wrapped underneath the extern "C" layer.

Then, we tried a second route of integrating the C++ heat exchanger model as a PRECOMPILED object (.obj file). Even though, we have followed the integration process documented in Sections 8.3 and 8.4 of the FLUENT UDF manual, we have encountered many problems mainly due to the way the manual has mixed the Makefile parameters and dynamics between UNIX and Windows. We have done a lot of trial and error and now feel that we need to approach the wider CFD community for any guidance. Integrating the heat exchanger model to FLUENT is very critical for our research as it will be called once per iteration to adjust the solver variables (p, v, T). We would also like recommendations on a suitable FLUENT DEFINE marco for passing multiple parameters back and forth.

Does anybody have any experience with the C++/FLUENT integration issues? What would be the best way of achieving the integration? We will greatly appreciate any feed back in this regard.

Many thanks in advance.

Best regards,

Syed I. Haider, PhD, Senior Research Engineer, Georgia Institute of Technology

RoM February 20, 2006 05:49

Re: Integration of a Custom C++ Model into FLUENT
To call C++ function from a udf i would compile the cpp poject as seperate dll, define an "C"interface funtion and call this funtion from the udf. Here is short explanation how to do it:

First the interface funtion of your cpp file:

extern "C" __declspec( dllexport ) void __cdecl Interfacefunction(double* a, int* b)

I saved this function as "project1.cpp" and compiled it from the comand line with
cl project1.cpp /LD

Copy the resulting dll into your windows\system32 directory.

And now the Udf:

#include "udf.h"

extern void Interfacefunction(double*,int*);

real a;
int b;

CX_Message("\na=%g b=%d",a,b);

To build the libudf.dll you will have to modify the makefile. You will have to include the project1.lib in the linking stage:

link $(LIBS) /dll /out:$(TARGET) \

and define DLL_LIB somewhere at the top of your makefile. In my case it is
DLL_LIB = "C:\Programme\Microsoft Visual C++ Toolkit 2003\project1.lib"

Loading the dll into fluent and executing "call_interface" you will get

a=12.34 b=1

Hope it helps,

All times are GMT -4. The time now is 03:39.