CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Running, Solving & CFD

Call Fortran program from OpenFoam

Register Blogs Community New Posts Updated Threads Search

Like Tree6Likes
  • 1 Post By tsjb00
  • 1 Post By rolando
  • 1 Post By gschaider
  • 3 Post By anfho

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   September 28, 2005, 14:47
Default Hi. I am trying to call a Fort
  #1
New Member
 
Bei
Join Date: Mar 2009
Posts: 21
Rep Power: 17
tsjb00 is on a distinguished road
Hi. I am trying to call a Fortran program from OpenFoam code, where some standard Fortran libraries are needed. I compile the Fortran program individually and add the .o file in the list of objectFile. Is that correct? What else should I do? And I have no idea how to include the information of the Fortran libraries. Please give me some advice. Thanks!

Bei
Zhiheng Wang likes this.
tsjb00 is offline   Reply With Quote

Old   September 29, 2005, 08:23
Default Hi Bei, I donīt know, if this
  #2
Member
 
Rolando Maier
Join Date: Mar 2009
Posts: 89
Rep Power: 17
rolando is on a distinguished road
Hi Bei,
I donīt know, if this is the most comfortable way to include Fortran into C/C++, but I got it going under a linux system some time ago:

You may have a function or routine in Fortran in a file called test.f:
SUBROUTINE TEST(par1, par2, par3)
.
.
.

To call this routine in C/C++ create a header file, maybe definitions.h:
#define test test__ (double underscore)

Include this header file, where you want to call the fortran routine, maybe:
#include "definitions.h"
main(...)
{
.
.
test(par1, par2, par3);
.
.
}

Thatīs it. Have fun.
By the way:
-Distinuish small and capital letters above.
-Put the definitions of all routines/functions, you want to use, in definitions.h

Rolando
Zhiheng Wang likes this.
rolando is offline   Reply With Quote

Old   September 29, 2005, 10:53
Default I think there are no OpenFOAM
  #3
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
I think there are no OpenFOAM specific problems. The major problem is that Fortran passes variables "by Referenence" while C (as a general rule) passes "by value".

Googling for "fortran c mixing" gives a number of useful pages amongst them:

http://owen.sj.ca.us/rkowen/howto/FandC/FandC.call.html

I know this is not the place to start a language dispute, but as a wise man once said: "The true programmer can program FORTRAN in any language" If your routines are not to large I would port them to C because then you won't have to fuss with portability issues if you switch systems/compilers.

If you must stay with the Fortran-code, just one tip: if you are unsure how many additional _ you Fortran-compiler produces or what the routine-names look like use "objdumd -t" on the .o-files. The names that are listed there (small/large, underscore etc) can be used in the C-programs.
Zhiheng Wang likes this.
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   September 29, 2005, 13:09
Default Thanks for the answers! I will
  #4
New Member
 
Bei
Join Date: Mar 2009
Posts: 21
Rep Power: 17
tsjb00 is on a distinguished road
Thanks for the answers! I will follow your tips and see how it works.

Best regards,

Bei
tsjb00 is offline   Reply With Quote

Old   March 9, 2011, 17:27
Default
  #5
Member
 
Join Date: Sep 2010
Posts: 36
Rep Power: 16
anfho is on a distinguished road
I would say, this is a really good example:

testC.cpp :
Code:
#include <iostream>
using namespace std;
 
extern"C"
{
  void fortfunc_(int *ii, float *ff);
}
 
main()
{
   int ii=5;
   float ff=5.5;
   fortfunc_(&ii, &ff);
   return 0;
}
testF.f :
Code:
      subroutine fortfunc(ii,ff)
      integer ii
      real*4  ff
      write(6,100) ii, ff
 100  format('ii=',i2,' ff=',f6.3)
      return
      end
Compile :
* f77 -c testF.f
* g++ -c testC.cpp
* g++ -o test testF.o testC.o

Run :
./test

Output :
ii= 5 ff= 5.500

That's it! Regards, Andreas


P.S.1: Found here:
http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html
It's all out there.

P.S.2: @Bei:
Please post your solutions, once you find them. You also profit from others' suggestions.

P.S.3: @Roland:
Double underscore doesn't work and a Fortran (dosen't matter if F77/90/95/2003) function in C++ has to be defined within
Code:
extern "C"
{
   'type-def' 'function-name'_(arg1 *, arg2 *,...);
}
well, see example above.
tunkers, chengyu and zli like this.

Last edited by anfho; March 9, 2011 at 17:49.
anfho is offline   Reply With Quote

Old   April 17, 2013, 19:18
Default
  #6
Senior Member
 
Join Date: Nov 2012
Posts: 171
Rep Power: 14
hz283 is on a distinguished road
Hi Bernhard,

If I add the line to call a fortran code in, e.g. rhoPimpleFoam.C, and I pass the velocity field to the fortran code. is this velocity field for the whole computational domain or only for one core? From the fortran code side, another quantity will be passed to the openfoam, should this quantity be for the whole domain or only for the particular rank?

I not know how the parallelization is implemented in openfoam. How can we extract the rank No. and logical variable ROOT from openfoam. Thank you very much!

Quote:
Originally Posted by gschaider View Post
I think there are no OpenFOAM specific problems. The major problem is that Fortran passes variables "by Referenence" while C (as a general rule) passes "by value".

Googling for "fortran c mixing" gives a number of useful pages amongst them:

http://owen.sj.ca.us/rkowen/howto/FandC/FandC.call.html

I know this is not the place to start a language dispute, but as a wise man once said: "The true programmer can program FORTRAN in any language" If your routines are not to large I would port them to C because then you won't have to fuss with portability issues if you switch systems/compilers.

If you must stay with the Fortran-code, just one tip: if you are unsure how many additional _ you Fortran-compiler produces or what the routine-names look like use "objdumd -t" on the .o-files. The names that are listed there (small/large, underscore etc) can be used in the C-programs.
hz283 is offline   Reply With Quote

Old   April 22, 2013, 16:26
Default
  #7
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by hz283 View Post
Hi Bernhard,

If I add the line to call a fortran code in, e.g. rhoPimpleFoam.C, and I pass the velocity field to the fortran code. is this velocity field for the whole computational domain or only for one core? From the fortran code side, another quantity will be passed to the openfoam, should this quantity be for the whole domain or only for the particular rank?
Each processor knows only its own data. Why should passing the data "between programming languages" somehow mysteriously collect the data onto one processor?

Quote:
Originally Posted by hz283 View Post
I not know how the parallelization is implemented in openfoam. How can we extract the rank No. and logical variable ROOT from openfoam. Thank you very much!
Have a look at the doxygen-documentation of the Pstream/UPstream-class http://foam.sourceforge.net/docs/cpp/a02426.html
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider 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
How to call FLUENT in my Fortran source? Sandia FLUENT 2 December 1, 2012 16:33
How to call a subrutine writen with Fortran Lana CFX 4 August 20, 2008 06:41
Could not run a Fortran program Henry Main CFD Forum 9 July 24, 2006 09:44
call fortran routines from UDF lily FLUENT 0 March 29, 2005 19:26
how to call C++ function from fortran with MPI kenn Main CFD Forum 1 February 17, 2002 21:39


All times are GMT -4. The time now is 21:45.