CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > ANSYS > FLUENT

Integration of a Custom C++ Model into FLUENT

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   February 19, 2006, 18:35
Default Integration of a Custom C++ Model into FLUENT
  #1
Syed Haider
Guest
 
Posts: n/a
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

}

#endif

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

  Reply With Quote

Old   February 20, 2006, 05:49
Default Re: Integration of a Custom C++ Model into FLUENT
  #2
RoM
Guest
 
Posts: n/a
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)
{
*a=12.34;
*b=1;
};

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*);

DEFINE_ON_DEMAND(call_inerface)
{
real a;
int b;

Interfacefunction(&a,&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) \
$(OBJECTS) $(FLUENT_LIB) $(DLL_LIB)

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,
RoM
  Reply With Quote

Old   March 7, 2018, 00:37
Default
  #3
New Member
 
Join Date: Jun 2015
Posts: 10
Rep Power: 11
syble is on a distinguished road
Quote:
Originally Posted by RoM
;129128
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)
{
*a=12.34;
*b=1;
};

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*);

DEFINE_ON_DEMAND(call_inerface)
{
real a;
int b;

Interfacefunction(&a,&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) \
$(OBJECTS) $(FLUENT_LIB) $(DLL_LIB)

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,
RoM
Hi RoM,

Thanks for the detailed explanation.

Just wanted to clarify a few things as I'm not experienced with cpp:

Is the Udf file (the one containing the DEFINE_ON_DEMAND macro) still a .c file? And is this still the file to be read in with the Complied UDF's dialogue window in the Fluent.cas file?
You mention loading the dll into fluent - will this happen automatically when the Udf file is read in?
Finally in terms of changing the makefile - is this the file in the directory .\libudf\win64\3ddp? And if so do we add the
link $(LIBS) /dll /out:$(TARGET) \
$(OBJECTS) $(FLUENT_LIB) $(DLL_LIB)


below the link that is already in this makefile or do we replace the current link?

Thanks for your help!
syble is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seeking Macroscopic Particle Model in Fluent bzhang7 FLUENT 3 June 25, 2022 18:54
Superlinear speedup in OpenFOAM 13 msrinath80 OpenFOAM Running, Solving & CFD 18 March 3, 2015 06:36
About Aspen and Fluent Integration! xfish FLUENT 0 March 24, 2005 22:51
UDF of Zimont model in fluent Z Main CFD Forum 0 February 17, 2005 04:07
Covert Star-CD model to FLUENT Lam Siemens 6 June 24, 2003 21:21


All times are GMT -4. The time now is 02:01.