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

Compiling an external Fortran library of thermodynamics and using inside reactingFoam

Register Blogs Community New Posts Updated Threads Search

Like Tree4Likes
  • 2 Post By jherb
  • 1 Post By jherb
  • 1 Post By Mehdi3031

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 17, 2016, 13:01
Question Compiling an external Fortran library of thermodynamics and using inside reactingFoam
  #1
Member
 
Mehdi Aminyavari
Join Date: Feb 2016
Location: Milan
Posts: 35
Rep Power: 10
Mehdi3031 is on a distinguished road
Dear all, Hello!
I am very new to OpenFoam and C++!!! and I am trying to use a static library of thermodynamic for mixture(NH3-H2O) that is written in Fortran90 in MyReactingFoam solver(that I developed). What I have done up to now step by step is this:

1)I have put all the ".f90" files of my Library in this directory:
mehdi@mehdi-Inspiron-7737:~/OpenFOAM/mehdi-3.0.1/platforms/linux64GccDPInt32Opt/lib/LibPropBmNEW$
there I have my library module called "LibPropBM0001.f90" where I manage all the pointers and functions and subroutines that each of this functions are in a separate file and calculate a specific property, like "rhoL.f90", Tbubling.f90", etc.

2) I compiled all these files in the order of their dependencies with gfortran to make the .o files of each,. Therefore, in that folder now I have "rhoL.o", "Tbubling.o", "LibPropBM0001.o"(I produced them by the order of their dependency)
Commands:
gfortran -c rhoL.f90 -o rhoL.o
gfortran -c Tbubling.f90 -o Tbubling.o
...
gfortran -c LibPropBM001.f90 -o LibPropBM001.o


3)Then I made the static library by inserting this command in the shell:
ar cr LibPropBM001.a *.o
and it produced a "LibPropBM001.a" file, so I think library is compiled well!!(is it?!)

4)in the Make/options of "MyReactingFoam", I gave path to this thermodynamics library as following:

EXE_LIBS = \
-lfiniteVolume \
-lfvOptions \
...(etc.)
-lgfortran\
$(HOME)/OpenFOAM/mehdi-3.0.1/platforms/linux64GccDPInt32Opt/lib/LibPropBmNEW/LibPropBM001.a


5)Now when I compile the solver in its directory with "wmake", everything works and it compiles, but when I try to use any of the function of the library somewhere in MyReactingFoam.c like bellow:

int main(int argc, char *argv[])
{

...
Info<< "rhoL = " << rhoL_(300.0,512.0,0.4) << endl;
...
}

I get this error:

MyReactingFoam.C: In function ‘int main(int, char**)’:
MyReactingFoam.C:129:39: error: ‘rhoL_’ was not declared in this scope
Info<< "rhoL =" << rhoL_(300.0,512.0,0.4) << endl;



I don't know how to declare an external function of a library here, I tried to add a header like:
#include "LibPropBM001.a"
or this:
float rhoL_(float ii, float jj, float kk);
etc.
But non worked!! I would appreciate if anybody can give me a hint of what I am missing here and save my head from banging to the keyboard...


Cheers
Mehdi
Mehdi3031 is offline   Reply With Quote

Old   March 18, 2016, 09:29
Default
  #2
Senior Member
 
Joachim Herb
Join Date: Sep 2010
Posts: 650
Rep Power: 22
jherb is on a distinguished road
Did you put
Code:
extern"C" {

float rhoL_(float* ii, float* jj, float* kk);

}
Also Fortran functions always take arguments by reference, so your Fortran function is really
Code:
float rhoL_(float* ii, float* jj, float* kk);
And you have to call it with pointers to variables so
Code:
float a = 300.0;
float b = 512.0;
float c = 0.4;

float result = rhoL_(&a, &b, &c);
Also make sure that you use the right naming convention for your Fortran functions.

See the documentation for
-fno-underscoring
-fsecond-underscore
-fcase-lower
of gfortran.

Perhaps this can help:
http://www.yolinux.com/TUTORIALS/Lin...rtranAndC.html
http://docs.cray.com/books/S-2179-52.../ppgzmrwh.html
Mehdi3031 and ssc0109 like this.
jherb is offline   Reply With Quote

Old   March 18, 2016, 11:45
Default
  #3
Member
 
Mehdi Aminyavari
Join Date: Feb 2016
Location: Milan
Posts: 35
Rep Power: 10
Mehdi3031 is on a distinguished road
Dear Joachim,
Thank you for your kind answer, It seems that this is the source of my error...!
I tried your suggestion first as following:
I added the Extern "C" part before " int main(int argc, char *argv[]) ", and then inside the main function tried to call rhoL as bellow:
************************************************** *****************************************
extern"C" {
float rhoL_(float* ii, float* jj, float* kk);
}
int main(int argc, char *argv[])
{

...
float a = 300.0; float b = 512.0; float c = 0.4; float result = rhoL_(&a, &b, &c);
Info<< "rhoL is" << result << endl;
...
}

************************************************** ****************************************
But I got this error:

************************************************** ****************************************
In function `main':
MyReactingFoam.C : (.text.startup+0x1544): undefined reference to `rhoL_'

************************************************** ****************************************
I looked at the links and the documentations which is quite complicated to understand, but from what I got, seems that you have to call the Fortran function in upper case letters, so I also tried this:

************************************************** ****************************************
extern"C"
{
float RHOL_(float* ii, float* jj, float* kk);
}
int main(int argc, char *argv[])
{

...
float a = 300.0;
float b = 512.0;
float c = 0.4;
float result = RHOL_(&a, &b, &c);
Info<< "rhoL is" << result << endl;
...
}

************************************************** ****************************************
But still the same (The exact error as before).
what I am not sure if I understood from your reply and the documentations is this part:

Also Fortran functions always take arguments by reference, so your Fortran function is really:
float rhoL_(float* ii, float* jj, float* kk);


Does it mean that in my Fortran Library also I have to change the code and declare everything with underscore "_" ??? Sounds quite strange to me since in Fortran compiler this library is working properly...
If it can help, I will attach the main modules and the rhoL function file as an example so that you can check it out how is it written in Fortran.

By the way, I always thought that extern "C" is for the case that you are using just a function file of Fortran, not a sub function of a library of Fortran that you are addressing to it and using all its functions and subroutines. isn't that true?
Attached Files
File Type: gz libPropBM001.tar.gz (4.0 KB, 11 views)
Mehdi3031 is offline   Reply With Quote

Old   March 18, 2016, 18:13
Default
  #4
Senior Member
 
Joachim Herb
Join Date: Sep 2010
Posts: 650
Rep Power: 22
jherb is on a distinguished road
I guess the correct name is everything lower case for calling it in OpenFOAM. The case in Fortran should not matter. Take a look in LibPropBM001.a how the function are named there:
Code:
strings LibPropBM001.a
or
Code:
strings LibPropBM001.a | grep -i rhoL
Mehdi3031 likes this.
jherb is offline   Reply With Quote

Old   March 18, 2016, 19:10
Default
  #5
Member
 
Mehdi Aminyavari
Join Date: Feb 2016
Location: Milan
Posts: 35
Rep Power: 10
Mehdi3031 is on a distinguished road
Dear Joachim,
Thank you so much...
I looked into the LibPropBM001.a fie as you suggested and realized that gfortran converts all the names of the functions to lower case, therefore I changed "rhoL_" to "rhol_" and now everything works and I can use all the library in OpenFoam .
Thank you once again.

Regards
Mehdi
ssc0109 likes this.
Mehdi3031 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



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