www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to interact with fortran code

reply "seany" <seany uni-bonn.de> writes:
I apologize many times for this question, may be this had already 
been answered somewhere, but considering today the last of my 
nerve is broken, I can not really find the soution.

So I have a D code, which acts as a central manager of all my 
codes, reads user input, reads files, etc, and based on the file 
readouts, I would like to pass some variables from the D code to 
a fortran code, in binary format, perhaps, if such a thing 
exists, instead of encoding to text/ ASCII first.

I would also like to read some (not all) variables back from the 
fortran code.

The Fortran code resides in a subdirectory to the path/to/d/code

How to do this? is there a preffered way / easier than system 
call way to interface D and Fortran code? This must be Fortan 
code - these are the standard atmospheric chemistry codes.

I apologize again if the question is stupid, trust me, today all 
my nerves are broken.
Jul 09 2014
parent reply "Chris" <wendlec tcd.ie> writes:
On Wednesday, 9 July 2014 at 15:00:25 UTC, seany wrote:
 I apologize many times for this question, may be this had 
 already been answered somewhere, but considering today the last 
 of my nerve is broken, I can not really find the soution.

 So I have a D code, which acts as a central manager of all my 
 codes, reads user input, reads files, etc, and based on the 
 file readouts, I would like to pass some variables from the D 
 code to a fortran code, in binary format, perhaps, if such a 
 thing exists, instead of encoding to text/ ASCII first.

 I would also like to read some (not all) variables back from 
 the fortran code.

 The Fortran code resides in a subdirectory to the path/to/d/code

 How to do this? is there a preffered way / easier than system 
 call way to interface D and Fortran code? This must be Fortan 
 code - these are the standard atmospheric chemistry codes.

 I apologize again if the question is stupid, trust me, today 
 all my nerves are broken.
Off the top of my head I'd say you could try to interface Fortran and C. Then you could interface D and C, i.e. D > C > Fortran. http://fortranwiki.org/fortran/show/Generating+C+Interfaces https://gcc.gnu.org/onlinedocs/gcc-3.4.4/g77/C-Interfacing-Tools.html
Jul 09 2014
parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Wednesday, 9 July 2014 at 15:09:08 UTC, Chris wrote:
 On Wednesday, 9 July 2014 at 15:00:25 UTC, seany wrote:
 I apologize many times for this question, may be this had 
 already been answered somewhere, but considering today the 
 last of my nerve is broken, I can not really find the soution.

 So I have a D code, which acts as a central manager of all my 
 codes, reads user input, reads files, etc, and based on the 
 file readouts, I would like to pass some variables from the D 
 code to a fortran code, in binary format, perhaps, if such a 
 thing exists, instead of encoding to text/ ASCII first.

 I would also like to read some (not all) variables back from 
 the fortran code.

 The Fortran code resides in a subdirectory to the 
 path/to/d/code

 How to do this? is there a preffered way / easier than system 
 call way to interface D and Fortran code? This must be Fortan 
 code - these are the standard atmospheric chemistry codes.

 I apologize again if the question is stupid, trust me, today 
 all my nerves are broken.
Off the top of my head I'd say you could try to interface Fortran and C. Then you could interface D and C, i.e. D > C > Fortran. http://fortranwiki.org/fortran/show/Generating+C+Interfaces https://gcc.gnu.org/onlinedocs/gcc-3.4.4/g77/C-Interfacing-Tools.html
To expand on that: You don't actually need to write a C glue layer between D and Fortran. All you need to do is make your Fortran functions accessible for C code. As I'm not familiar with Fortran, I don't know how exactly that works, but it could involve telling the compiler to use the right calling convention, and use the right name mangling. On the D side, you can then declare and use the Fortran functions as follows: extern(C) float my_fortran_func(int a, int b); void bar() { writeln("Fortran returned: ", my_fortran_func(10, 20)); } Here is an article that describes how to interface with C code from D: http://wiki.dlang.org/Bind_D_to_C
Jul 10 2014
next sibling parent "Chris" <wendlec tcd.ie> writes:
On Thursday, 10 July 2014 at 12:12:20 UTC, Marc Schütz wrote:
 On Wednesday, 9 July 2014 at 15:09:08 UTC, Chris wrote:
 On Wednesday, 9 July 2014 at 15:00:25 UTC, seany wrote:
 I apologize many times for this question, may be this had 
 already been answered somewhere, but considering today the 
 last of my nerve is broken, I can not really find the soution.

 So I have a D code, which acts as a central manager of all my 
 codes, reads user input, reads files, etc, and based on the 
 file readouts, I would like to pass some variables from the D 
 code to a fortran code, in binary format, perhaps, if such a 
 thing exists, instead of encoding to text/ ASCII first.

 I would also like to read some (not all) variables back from 
 the fortran code.

 The Fortran code resides in a subdirectory to the 
 path/to/d/code

 How to do this? is there a preffered way / easier than system 
 call way to interface D and Fortran code? This must be Fortan 
 code - these are the standard atmospheric chemistry codes.

 I apologize again if the question is stupid, trust me, today 
 all my nerves are broken.
Off the top of my head I'd say you could try to interface Fortran and C. Then you could interface D and C, i.e. D > C > Fortran. http://fortranwiki.org/fortran/show/Generating+C+Interfaces https://gcc.gnu.org/onlinedocs/gcc-3.4.4/g77/C-Interfacing-Tools.html
To expand on that: You don't actually need to write a C glue layer between D and Fortran. All you need to do is make your Fortran functions accessible for C code. As I'm not familiar with Fortran, I don't know how exactly that works, but it could involve telling the compiler to use the right calling convention, and use the right name mangling. On the D side, you can then declare and use the Fortran functions as follows: extern(C) float my_fortran_func(int a, int b); void bar() { writeln("Fortran returned: ", my_fortran_func(10, 20)); } Here is an article that describes how to interface with C code from D: http://wiki.dlang.org/Bind_D_to_C
Cool. I wonder would it be possible to use inline assembly?
Jul 10 2014
prev sibling parent "bachmeier" <no spam.net> writes:
On Thursday, 10 July 2014 at 12:12:20 UTC, Marc Schütz wrote:
 On Wednesday, 9 July 2014 at 15:09:08 UTC, Chris wrote:
 On Wednesday, 9 July 2014 at 15:00:25 UTC, seany wrote:
 I apologize many times for this question, may be this had 
 already been answered somewhere, but considering today the 
 last of my nerve is broken, I can not really find the soution.

 So I have a D code, which acts as a central manager of all my 
 codes, reads user input, reads files, etc, and based on the 
 file readouts, I would like to pass some variables from the D 
 code to a fortran code, in binary format, perhaps, if such a 
 thing exists, instead of encoding to text/ ASCII first.

 I would also like to read some (not all) variables back from 
 the fortran code.

 The Fortran code resides in a subdirectory to the 
 path/to/d/code

 How to do this? is there a preffered way / easier than system 
 call way to interface D and Fortran code? This must be Fortan 
 code - these are the standard atmospheric chemistry codes.

 I apologize again if the question is stupid, trust me, today 
 all my nerves are broken.
Off the top of my head I'd say you could try to interface Fortran and C. Then you could interface D and C, i.e. D > C > Fortran. http://fortranwiki.org/fortran/show/Generating+C+Interfaces https://gcc.gnu.org/onlinedocs/gcc-3.4.4/g77/C-Interfacing-Tools.html
To expand on that: You don't actually need to write a C glue layer between D and Fortran. All you need to do is make your Fortran functions accessible for C code. As I'm not familiar with Fortran, I don't know how exactly that works, but it could involve telling the compiler to use the right calling convention, and use the right name mangling.
If you are able to use Fortran 2003 or later, Fortran's iso_c_binding makes it simple to expose Fortran functions to be called from C and vice versa. I don't know of any good online references, but a lot of good examples can be found in the source for the Fortran interface to the GSL: http://www.lrz.de/services/software/mathematik/gsl/fortran/index.html An example of compiling and linking: http://compgroups.net/comp.lang.fortran/usage-of-iso_c_binding/155309 If iso_c_binding is not available, then it's not as pretty: http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html
Jul 11 2014