Newbie question: passing arrays from C to python and vice versa

62 views
Skip to first unread message

V. Khiem TRUONG

unread,
Dec 21, 2011, 3:22:01 PM12/21/11
to cython...@googlegroups.com
              Hello,

I'm working on coupling of CSD code with CFD code in the field of aerospace engineering. My concern is about
coupling between a commercial structural code, written in fortran, with a python file. The problem boils down
to passing numerical arrays from fortran and python and vice versa.

I have made some google review and it appears to me (I might be wrong, please correct me), that the solution
could be provided by making a C/API interface between fortran and python. This could be done in two steps:
first making C interface to fortran based on the iso_c_binding module, next interfacing C subroutines to python.
One good candidate for the second step would be Cython. I make a rapid reading of the code: I understand that
C arrays can be passed to python by the so-called extension procedure.

But I couldn't understand how C can receive arrays from python. From the google survey and review of the archive
of Cython users (particularly discussions between Tiago Pereira and Robert Bradshaw), it seems that could be done,
but I just don't understand, probably due my lack of knowledge of C language.I need further explanation about how
to get arrays from python to C.

        Regards,

          Khiem

Dag Sverre Seljebotn

unread,
Dec 23, 2011, 1:58:09 AM12/23/11
to cython...@googlegroups.com
Have a look at a) the fwrap project, b) the kind of cython code it generates.

There's also f2py.

DS
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

Khiem

unread,
Dec 23, 2011, 10:32:57 AM12/23/11
to cython-users
Hello Dag,

Many thanks for your reply,

As I said, I use a commercial code: although it provides
interfacing through user subroutine, I don't think that fwrap
or f2py would be appropriate. The best shot would be cython. As I have
not yet sufficient knowledge of cython, could you
indicate me examples showing transfer of arrays between C and python
using cython.

Merry Christmas and best regards,

Khiem

On 23 déc, 07:58, Dag Sverre Seljebotn <d.s.seljeb...@astro.uio.no>
wrote:
> Have a look at a) the fwrap project, b) the kind of cython code it generates.
>
> There's also f2py.
>
> DS
> --
> Sent from my Android phone with K-9 Mail. Please excuse my brevity.
>

Dag Sverre Seljebotn

unread,
Dec 23, 2011, 11:12:21 AM12/23/11
to cython...@googlegroups.com
Don' t have time for a full example, sorry, but look up PyArray_DATA on docs.scipy.org in the NumPy C API section.

For a full example you could just write a dummy fortran routine with array arguments and use fwrap and inspect the generated cython sources. (There's plenty of such examples in the SciPy port to .NET on jasonmccampbell's github account, although for wrapping F77 using Cython)

Dag

--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

Reply all
Reply to author
Forward
0 new messages