Thread overview
How to interact with fortran code
Jul 09, 2014
seany
Jul 09, 2014
Chris
Jul 10, 2014
Marc Schütz
Jul 10, 2014
Chris
Jul 11, 2014
bachmeier
July 09, 2014
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.
July 09, 2014
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
July 10, 2014
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
July 10, 2014
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?
July 11, 2014
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