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

How to call FORTRAN files as UDF?

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By bawfuls

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   June 30, 2011, 23:15
Default How to call FORTRAN files as UDF?
  #1
New Member
 
Ehsan
Join Date: Jun 2011
Posts: 9
Rep Power: 15
Ehsan-F is on a distinguished road
I have some FORTRAN files and I like to use them as UDF files in FLUENT, but UDF files programming language is C with macros.
These FORTRAN files basically calculate source terms for some equations in FLUENT and the calculations need to be updated in every iteration.
I like to know, how hard is that to use FORTRAN files with a UDF interface? Does it smart enough to understand the FORTRAN tricks? If you have any special documents please share with me.
Thank you
Ehsan
Ehsan-F is offline   Reply With Quote

Old   July 1, 2011, 05:38
Default
  #2
Senior Member
 
Amir's Avatar
 
Amir
Join Date: May 2009
Location: Montreal, QC
Posts: 735
Blog Entries: 1
Rep Power: 23
Amir is on a distinguished road
Quote:
Originally Posted by Ehsan-F View Post
I have some FORTRAN files and I like to use them as UDF files in FLUENT, but UDF files programming language is C with macros.
These FORTRAN files basically calculate source terms for some equations in FLUENT and the calculations need to be updated in every iteration.
I like to know, how hard is that to use FORTRAN files with a UDF interface? Does it smart enough to understand the FORTRAN tricks? If you have any special documents please share with me.
Thank you
Ehsan
Hi Ehsen,
refer to:
UDF user manual: (6.3.26)
5.4 Link Precompiled Object Files From Non-FLUENT Sources
I didn't try it before; share your experiences if you have any progress !

Amir
Amir is offline   Reply With Quote

Old   July 1, 2011, 16:10
Default
  #3
New Member
 
Ehsan
Join Date: Jun 2011
Posts: 9
Rep Power: 15
Ehsan-F is on a distinguished road
Hi Amir,
Thanks for your reply,
I am actually fighting with that using instruction in 5.4 of the manual. I like to know if anyone has done that before and had success on that. Also if it is easier to translate whole FORTRAN file (in my case it is fairly complicated) to C or it is easier to write an interface?
Ehsan
Ehsan-F is offline   Reply With Quote

Old   June 4, 2012, 06:32
Default
  #4
New Member
 
Dinesh
Join Date: Nov 2009
Location: India
Posts: 25
Rep Power: 17
Dinesh_Dhande is on a distinguished road
Quote:
Originally Posted by Ehsan-F View Post
Hi Amir,
Thanks for your reply,
I am actually fighting with that using instruction in 5.4 of the manual. I like to know if anyone has done that before and had success on that. Also if it is easier to translate whole FORTRAN file (in my case it is fairly complicated) to C or it is easier to write an interface?
Ehsan
Hi Ehsan,
Did u find success in conversion of FORTRAN program in UDF? If yes kindly help me also. I am also fighting with conversion of the same kind of stuff. Thanks in advance.
Dinesh_Dhande is offline   Reply With Quote

Old   July 9, 2012, 22:38
Default
  #5
New Member
 
Join Date: Jul 2010
Posts: 8
Rep Power: 16
bawfuls is on a distinguished road
It can be done and I've done it with varying success, but it is tedious.

Here's a quick overview of the procedure:

-In your Fluent case that you want to link with Fortran, go to Define->User-Defined->Functions->Compiled...

-Under "Library Name" type in the name you want to use for your library of udf's and Fortran files (for example i'll use: libudf) then click the Build button. This will build the directory structure within your Fluent working directory.

-Your Fluent working directory will now have a folder in it with the library name you chose (in this case: libudf) and within that folder another for the computer architecture (in my case, win64). Within that folder is yet another, for the solver you are using (in my case, "2ddp" for 2-Dimensional double-precision).

-Your libudf directory may also (I can't recall) have another folder in it named "src". If it doesn't have this folder, create it. This is the folder where you will place the UDF c-file that must be compiled and which will interface with Fluent and the Fortran code. For example, we will name this file: udfsource.c

-Open that bottom folder (2ddp) and open the text file named user_nt.udf and edit it to look as follows:
Code:
SOURCES =  $(SRC)udfsource.c 
VERSION = 2ddp
PARALLEL_NODE = none
USER_OBJECTS =C:\*FLUENT CASE WORKING DIRECTORY*\FORTRAN_CODE.lib
The SOURCES term must match the name of your c-file in the src directory. The $(SRC) is a prefix that points to the src directory, as far as I understand it. The VERSION term must match the fluent solver version you are using (in this case 2ddp). The USER_OBJECTS term needs to point to the Fortran library file that you will be using, which should ideally be located in your Fluent working directory. (You need to put all of your relevant Fortran code into one big file and compile it as a static library. Also make sure you select Static Storage as an option in your Fortran compiler.)

-There are also a whole laundry list of Fortran libraries you may need to include in the USER_OBJECTS term, depending on your Fortran compiler. I am using Absoft to compile my Fortran code, so here is the full list of what mine looks like:
Code:
C:\Absoft11.0\lib64\afiodll.lib C:\Absoft11.0\lib64\GdiPlus.lib C:\Absoft11.0\lib64\Uuid.lib C:\Absoft11.0\lib64\Uxtheme.lib
 C:\Absoft11.0\lib64\absoftmain.lib C:\Absoft11.0\lib64\af77math.lib C:\Absoft11.0\lib64\af77mathdll.lib 
C:\Absoft11.0\lib64\af90math.lib C:\Absoft11.0\lib64\af90mathdll.lib C:\Absoft11.0\lib64\af90mblas.lib 
C:\Absoft11.0\lib64\afio.lib C:\Absoft11.0\lib64\afiodll.lib C:\Absoft11.0\lib64\amisc.lib C:\Absoft11.0\lib64\blacs.lib 
C:\Absoft11.0\lib64\blacsC.lib C:\Absoft11.0\lib64\blacsF77.lib C:\Absoft11.0\lib64\bufferoverflowu.lib 
C:\Absoft11.0\lib64\comsupp.lib C:\Absoft11.0\lib64\comsuppd.lib C:\Absoft11.0\lib64\comsuppw.lib 
C:\Absoft11.0\lib64\comsuppwd.lib C:\Absoft11.0\lib64\cryptui.lib C:\Absoft11.0\lib64\dciman32.lib 
C:\Absoft11.0\lib64\delayimp.lib C:\Absoft11.0\lib64\f90gl.lib C:\Absoft11.0\lib64\f90glu.lib C:\Absoft11.0\lib64\f90glut.lib 
C:\Absoft11.0\lib64\glut32.lib C:\Absoft11.0\lib64\gtrts32w.lib C:\Absoft11.0\lib64\gtrtst32.lib C:\Absoft11.0\lib64\httpapi.lib 
C:\Absoft11.0\lib64\inetcomm.lib C:\Absoft11.0\lib64\ksuser.lib C:\Absoft11.0\lib64\libaomp.lib C:\Absoft11.0\lib64\libcmt.lib 
C:\Absoft11.0\lib64\libcmtd.lib C:\Absoft11.0\lib64\libcpmt.lib C:\Absoft11.0\lib64\libcpmtd.lib C:\Absoft11.0\lib64\libgomp.lib 
C:\Absoft11.0\lib64\libinstr.lib C:\Absoft11.0\lib64\libmv.lib C:\Absoft11.0\lib64\libpthread.lib C:\Absoft11.0\lib64\mrwe.lib 
C:\Absoft11.0\lib64\msdasc.lib C:\Absoft11.0\lib64\msvcmrt.lib C:\Absoft11.0\lib64\msvcmrtd.lib C:\Absoft11.0\lib64\msvcprt.lib 
C:\Absoft11.0\lib64\msvcprtd.lib C:\Absoft11.0\lib64\msvcrt.lib C:\Absoft11.0\lib64\msvcrtd.lib C:\Absoft11.0\lib64\msvcurt.lib 
C:\Absoft11.0\lib64\msvcurtd.lib C:\Absoft11.0\lib64\mtxdm.lib C:\Absoft11.0\lib64\nmapi.lib C:\Absoft11.0\lib64\normaliz.lib 
C:\Absoft11.0\lib64\npptools.lib C:\Absoft11.0\lib64\odbc32.lib C:\Absoft11.0\lib64\odbcbcp.lib 
C:\Absoft11.0\lib64\odbccp32.lib C:\Absoft11.0\lib64\oldnames.lib C:\Absoft11.0\lib64\oledb.lib C:\Absoft11.0\lib64\osptk.lib 
C:\Absoft11.0\lib64\parser.lib C:\Absoft11.0\lib64\plplot.lib C:\Absoft11.0\lib64\plplot_d.lib C:\Absoft11.0\lib64\powrprof.lib 
C:\Absoft11.0\lib64\refblas.lib C:\Absoft11.0\lib64\reflapack.lib C:\Absoft11.0\lib64\runtmchk.lib 
C:\Absoft11.0\lib64\scalapack.lib C:\Absoft11.0\lib64\sisbkup.lib C:\Absoft11.0\lib64\strsafe.lib C:\Absoft11.0\lib64\unix.lib 
C:\Absoft11.0\lib64\vms.lib C:\Absoft11.0\lib64\wmiutils.lib
consecutive entries in the USER_OBJECTS term should be separated by a space.

-So, once your user_nt.udf file is properly set up, you can now compile the whole thing together. (Note that the fortran code should already be compiled into a single library file as I mentioned previously.) To do this, open your c-compiler (in my case, Microsoft Visual Studio 2008 command prompt) and navigate to the libudf\win64\2ddp directory. In windows, the compile command is nmake, so I just type that once I'm in that directory. In other operating systems, this process would be different and I don't know how to do it. NOTE: When you do this on Windows (it's different in Linux) the udf library can NOT already be loaded in Fluent. So you must either close the fluent case (save it first) or go to Define->User-Defined->Functions->Manage... and select your fluent library (in this case: libudf) and click Unload.

-Now there should be several new files in that libudf\win64\2ddp directory, including libudf.lib, which is the library Fluent will ultimately use. Go back to your Fluent case, go to Define->User-Defined->Functions->Compiled... and under Source Files click Add..., then navigate to the libudf\src directory and select your c-file (in this case: udfsourc.c) and click OK. The file should now show up on this list under Source Files. Click the Load button on the Compiled UDFs window to load your library. If you have a subroutine in your UDF called DEFINE_EXECUTE_ON_LOADING, this subroutine will execute when you click the load button.

-Now you can link cell source terms to the udf. If you've read the Fluent UDF Manual (and you should) your c-file will have a subroutine called DEFINE_SOURCE, whose first argument is a name of your choosing. You can go to Cell Zone Conditions->(whatever fluid zone)->Edit and check the Source Terms box. Then click the Source Terms tab in that window, and you can see all the particular equations you want to add sourc terms to (mass/momentum/energy/etc.) Click Edit next to the equation you want, increment the "number of sources" to 1, and your source udf will show up on the drop-down list here.

That is how to get everything all pointed in the right direction, but there are still some things to be aware of when writing your c and fortran codes:
-At the top of your c-file, just after the #include lines, you need to declare all the fortran functions you want to call from within the c. They need to be declared using this format:
Code:
void fsource_(double *, double *);
where in my case, the Fortran function is called "fsource" and has two arguments which are both double-precision real numbers. Note that there must be an underscore immediately after the function name, and a space and asterisk after each input variable. I don't know exactly why this is the case.
-Similarly, when you call a fortran function from the c-code, it must be done using this format:
Code:
fsource_(&all_source[0][0], &wall_source[0]);
Again, there must be an underscore after the function name, but there also must be an ampersand (&) before each variable name. In this case, I am passing a 2-d and a 1-d array to the fortran code. I must specify the first element in the arrays when I do this, and array indices in C start at 0. There are some more minor examples explaining this stuff in the Fluent UDF Manual.

-Additionally, every time you wish to make a change to your c-file, you must unload the udf library from fluent, recompile the c-code, and re-load the library into Fluent. When you wish to make changes to the Fortran code, you must unload the udf library from Fluent, recompile the Fortran code, then recompile the c-code as well (since the c-compile will include the Fortran library) then re-load the library into Fluent.


There may be additional complications with getting your c-compiler to work. I had trouble with it and someone in the IT department here sorted it all out for me. That part of the process will depend on what kind of system you are using.

Good luck, and consider rewriting or translating the Fortran code into C instead, to avoid this whole headache. I really think it's probably easier, unless you are an experienced programmer or have one around with the time to help you get going.
Rigel likes this.
bawfuls is offline   Reply With Quote

Old   July 11, 2012, 09:47
Smile
  #6
New Member
 
Dinesh
Join Date: Nov 2009
Location: India
Posts: 25
Rep Power: 17
Dinesh_Dhande is on a distinguished road
Dear friend bawfuls,
Thaks for ur reply.
It looks very tedious process. Hats of to u if u hav done yourself.
I hav a fortran program with me and i want to convert it to C.
If you come across any simple solution of conversion of fortran files to C kindly let me know.I will be very thankful to you. Thanks in advance.
Dinesh_Dhande is offline   Reply With Quote

Old   September 11, 2012, 12:03
Default
  #7
vnm
New Member
 
Join Date: Jun 2009
Posts: 15
Rep Power: 17
vnm is on a distinguished road
How to call a precompiled .obj file in a udf (c language)
vnm 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
CFX11 + Fortran compiler ? Mohan CFX 20 March 30, 2011 19:56
how to call diffusivity from Fluent to UDF Kim FLUENT 0 June 18, 2008 13:52
call fortran routines from UDF lily FLUENT 0 March 29, 2005 19:26
UDF CALL FOR PRESSURE D Harvey FLUENT 2 June 3, 2004 22:46
Open files in a UDF Riccardo FLUENT 2 June 26, 2002 11:45


All times are GMT -4. The time now is 18:36.