Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Fortran 2003 wrapper for Python

112 views
Skip to first unread message

Hossein

unread,
Jan 16, 2013, 3:55:15 AM1/16/13
to
Hello All,

Is there any doable way to write wrappers for a Fortran 2003 code so everything can be accessed from Python? Swig offers this for C/C++. According to FortWrap (http://fortwrap.sourceforge.net/ ) this is not currently possible.

Anyone has ideas?

Cheers!
H.

Wolfgang Kilian

unread,
Jan 16, 2013, 4:05:41 AM1/16/13
to
I'm not an expert with Python, but I think that Python can access C
functions, and probably more of C stuff.

So, any function or subroutine written in Fortran that is interoperable
with C (has the bind(C) attribute), can seamlessly be accessed by Python
or any other language that can access C functions.

-- Wolfgang

--
E-mail: firstnameini...@domain.de
Domain: yahoo

Hossein

unread,
Jan 16, 2013, 5:34:18 AM1/16/13
to
The problem is not simple Fortran functions. In 2003 and 2008 standards, it is possible to port derived data types to C. But, the derived data types have to have certain characteristics (bind(C)).

I am more talking about the type bound procedures which have type extensions. Or the polymorphic variables (Class(*)) and so on.

Wolfgang Kilian

unread,
Jan 16, 2013, 5:59:02 AM1/16/13
to
There are entities which are not interoperable. Polymorphic objects
(extensible types) are explicitly excluded from interoperability.
Similarly, Fortran pointers are not interoperable with any entity in C,
etc. (There is a document for extending the scope of interoperability,
but this is beyond F2008.)

For those, the Fortran compiler may do what it likes, if
standard-conforming. Without bind(C), entities may be optimized away,
for instance. There may be means to access them, but the corresponding
program will not be portable, not even w.r.t. changing compiler
switches. Therefore, I don't think that generic wrapper packages beyond
straight bind(C) are possible.

-- Wolfgang

Hossein

unread,
Jan 16, 2013, 9:16:58 AM1/16/13
to

I did some tests. It seems that there is not way to access the values of the derived data types (general ones) from C. But, we can pass the Class(ty1) variable to a subroutine which gets type(ty1) and then get the C_LOC of the class.

Then, we can pass this C_LOC to the C function without knowing what it is. Then, to call it back from C we have to write another Bind(C) function in Fortran which accepts the C_LOC variable and transfers it back to Fortran type and Call the type bound procedure.

I tested with Intel and Gfortran and they both work. But, I don't know how efficient this is. Also, one has to write another Swig project for Fortran 2003.

Cheers
H.

James Van Buskirk

unread,
Jan 16, 2013, 9:33:05 AM1/16/13
to
"Wolfgang Kilian" <kil...@invalid.com> wrote in message
news:kd5qh5$ajv$1...@dont-email.me...

> So, any function or subroutine written in Fortran that is interoperable
> with C (has the bind(C) attribute), can seamlessly be accessed by Python
> or any other language that can access C functions.

It is unfortunate that the converse is not the case.

--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end


William Clodius

unread,
Jan 17, 2013, 1:05:02 AM1/17/13
to

Paul Anton Letnes

unread,
Jan 17, 2013, 2:42:17 PM1/17/13
to
I'll heartily recommend f2py, but it has its limitations. I'm not sure
it covers the original poster's needs, but it's decent at wrapping
f90/f95 code. Although, I always wind up having to write a wrapper module.

Paul

Hossein

unread,
Jan 24, 2013, 8:33:54 AM1/24/13
to
Dear Paul,

As in my original post, I need Python for F2003. From a discussion with Tobias, I am relatively sure that this is not possible with a standard way. There are some workarounds, but I think I have to write my own interface generator or modify what already exists.

Cheers
H.
0 new messages