[f2py] Wraping Fortran code with allocatable arrays.
Benjamin Kutz <[email protected]>
| Newsgroups | gmane.comp.python.f2py.user |
|---|---|
| Message-ID | <[email protected]> |
Hi everybody,
I try to wrap a huge Fortran code. At the moment it is only necessary to
access some of those subroutines form python, all the other routines are
called from those wrapped ones. So far I have no problem (any more ;))
to that.
The different wrapped routines have to be called one after another. When
I call the second wrapped fortran subroutine from python, this routine
need some variables from the first called routine. As long as a Fortan
variable is global on the Fortran side that is no problem. I just define
the variable in a common block in Fortran and everything works fine. I
don't need to know these variables on the Python side.
When it comes to allocatable arrays I have a problem. As far as I know I
can`t share an allocatable array with a common block. So I have to share
it in the parameter list of the subroutine. That means, that in case of
a wrapped routine I have to pass this array to Python and back to the
next wrapped Fortran routine. Therefore I have chosen the following
implementation.
My foo.pyf file looks now as follows:
! -*- f90 -*-
python module foo ! in
interface ! in :foo
subroutine init(abc,deg) ! in :foo:init.f
real allocatable,dimension(:), intend(out) :: abc
real allocatable,dimension(:,:), intend(out) :: deg
end subroutine init
subroutine calc(abc,deg) ! in :foo:calc.f
real allocatable,dimension(:), intend(in,out) :: abc
real allocatable,dimension(:,:), intend(in,out) :: deg
end subroutine calc
end interface
end python module foo
$f2py -c foo.pyf *.o
My python script calls those routines in the following manner:
>>> from foo import *
>>> start = init(abc, deg)
>>> rechnung = calc(abc,deg)
Now I finally come to my questions.
1. How do I have to define the variables abc and deg in python, to
assure a correct data excange?
2. What do I have to do, that the array, which can become extremely
large, stay in the correct Fortran-contiguous form?
3. Is there any other way to define those arrays as global variables on
the Fortran side?
Thank you very much in advance.
Greetings
Ben