CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Running, Solving & CFD (https://www.cfd-online.com/Forums/openfoam-solving/)
-   -   Call Fortran program from OpenFoam (https://www.cfd-online.com/Forums/openfoam-solving/60471-call-fortran-program-openfoam.html)

tsjb00 September 28, 2005 13:47

Hi. I am trying to call a Fort
 
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

rolando September 29, 2005 07:23

Hi Bei, I donīt know, if this
 
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

gschaider September 29, 2005 09:53

I think there are no OpenFOAM
 
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.

tsjb00 September 29, 2005 12:09

Thanks for the answers! I will
 
Thanks for the answers! I will follow your tips and see how it works. http://www.cfd-online.com/OpenFOAM_D...part/happy.gif

Best regards,

Bei

anfho March 9, 2011 16:27

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.

hz283 April 17, 2013 18:18

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 (Post 198675)
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.


gschaider April 22, 2013 15:26

Quote:

Originally Posted by hz283 (Post 421348)
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 (Post 421348)
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


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