|
[Sponsors] |
September 28, 2005, 14:47 |
Hi. I am trying to call a Fort
|
#1 |
New Member
Bei
Join Date: Mar 2009
Posts: 21
Rep Power: 17 |
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 |
|
September 29, 2005, 08:23 |
Hi Bei,
I donīt know, if this
|
#2 |
Member
Rolando Maier
Join Date: Mar 2009
Posts: 89
Rep Power: 17 |
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 |
|
September 29, 2005, 10:53 |
I think there are no OpenFOAM
|
#3 |
Assistant Moderator
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51 |
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.
__________________
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 |
|
September 29, 2005, 13:09 |
Thanks for the answers! I will
|
#4 |
New Member
Bei
Join Date: Mar 2009
Posts: 21
Rep Power: 17 |
Thanks for the answers! I will follow your tips and see how it works.
Best regards, Bei |
|
March 9, 2011, 17:27 |
|
#5 |
Member
Join Date: Sep 2010
Posts: 36
Rep Power: 16 |
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; } Code:
subroutine fortfunc(ii,ff) integer ii real*4 ff write(6,100) ii, ff 100 format('ii=',i2,' ff=',f6.3) return end * 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 *,...); } Last edited by anfho; March 9, 2011 at 17:49. |
|
April 17, 2013, 19:18 |
|
#6 | |
Senior Member
Join Date: Nov 2012
Posts: 171
Rep Power: 14 |
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:
|
||
April 22, 2013, 16:26 |
|
#7 | ||
Assistant Moderator
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51 |
Quote:
Quote:
__________________
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 |
|||
|
|
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 |