[SciPy-user] Is it possible to pass Fortran derived data types to Python via C and SWIG?

David Huard david.huard@gmail....
Mon Dec 1 11:58:59 CST 2008


John,

this is something I've wanted to look at. Here is what I had planned to do,
so there is no guarantee that it will actually work...

The ISO_C_BINDING module is part of the 2003 standard and allows
interoperability between C and Fortran (it is included in the latest
gfortran compiler). It allows interoperability of Fortran derived types with
C structures (with certain restrictions). For example,

use iso_c_binding
type, bind(c) :: mytype
  real(c_float) :: data
  integer(c_int) :: n
end type

is interoperable with

typedef struct {
  float data;
  int n;
} mytype


Now, I am just guessing, but if such a module was built into a shared
library, maybe it could be accessed from python using ctypes STRUCTURES.

Regards,

David

On Sun, Nov 30, 2008 at 4:38 PM, Berthold Höllmann <
berthold@xn--hllmanns-n4a.de> wrote:

> Matthieu Brucher <matthieu.brucher <at> gmail.com> writes:
>
> >
> > 2008/11/30 David Cournapeau <david <at> ar.media.kyoto-u.ac.jp>:
> > > John Salvatier wrote:
> > >> I have a Fortran 90 algorithm which uses a derived data type to return
> > >> data, and I would like to make a python wrapper for this algorithm. I
> > >> understand that f2py cannot wrap derived data types; is it possible to
> > >> do so with a C interface for the Fortran algorithm and SWIG? I would
> > >> have to pass the derived data type into a C struct and then to Python.
> > >
> > > It is possible as long as you can pass the structure from fortran to C.
> > > I don't know anything about Fortran derived data types, but if it is a
> > > non trivial object (more than a set of fundamental types), I am afraid
> > > it will be difficult. Does F90 supports POD data ? Otherwise, you will
> > > need a scheme for marshalling your data from Fortran to C (to match
> > > exactly how the structure would look like in C at the binary level).
> > >
> > > David
> >
> > I've read an article (I don't remember where though, possibly CiSE)
> > that stated that it's really not an easy task, as each Fortran
> > compiler can do as it pleases it. So depending on the compiler and the
> > Fortran standard, it can be possible, or not. So as there are no
> > guaranties, you should write a function that transforms the Fortran
> > structure in several pieces that are then passed to the C function.
> >
> > Matthieu
>
> A feasible way to achieve this would be to write a Fortran wrapper
> around your routine(x) that decomposes your derived data type to
> standard types and exposes these in the interface. Than you can compose
> the derived data type again in the wrapper and pass it to the original
> routine. ::
>
>   module geom
>     type Point
>        real :: x, y
>     end type Point
>     type Circle
>        type (Point) :: Center
>        real :: Radius
>     end type Circle
>   end module geom
>   subroutine test(c)
>     use geom
>     type (Circle) :: c
>     print*, c%Radius
>     print*, c%Center%X
>     print*, c%Center%Y
>   end subroutine test
>   subroutine w_test(x, y, r)
>     use geom
>     real :: x, y, z
>     type (Circle) :: C
>     c%Radius = r
>     c%Center%X = x
>     c%Center%Y = y
>     call test(c)
>   end subroutine w_test
>
> Wrapping w_test should be trivial using f2py
>
> Regards
> Berthold
>
>
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20081201/b7e8d8f5/attachment.html 


More information about the SciPy-user mailing list