[Numpy-discussion] f2py with allocatable arrays

George Nurser gnurser@gmail....
Tue Jul 3 04:54:36 CDT 2012

Can you interface your fortran program twice?

First time return the number of particles, dimensions etc to python

python then creates work array of right size

Second interface pass work array as in/out array, dimension in fortran
argument list, to fortran
fortran copies allocatable arrays to argument arrays

clumsy, I know.

George Nurser

On 3 July 2012 02:17, Casey W. Stark <caseywstark@gmail.com> wrote:
> Hi numpy.
> Does anyone know if f2py supports allocatable arrays, allocated inside
> fortran subroutines? The old f2py docs seem to indicate that the allocatable
> array must be created with numpy, and dropped in the module. Here's more
> background to explain...
> I have a fortran subroutine that returns allocatable positions and
> velocities arrays. I wish I could get rid of the allocatable part, but you
> don't know how many particles it will create until the subroutine does some
> work (it checks if each particle it perturbs ends up in the domain).
> module zp
>   implicit none
>   contains
>   subroutine ics(..., num_particles, particle_mass, positions, velocities)
>     use data_types, only : dp
>     implicit none
>     ... inputs ...
>     integer, intent(out) :: num_particles
>     real (kind=dp), intent(out) :: particle_mass
>     real (kind=dp), intent(out), dimension(:, :), allocatable :: positions,
> velocities
>     ...
>   end subroutine
> end module
> I tested this with a fortran driver program and it looks good, but when I
> try with f2py, it cannot compile. It throws the error "Error: Actual
> argument for 'positions' must be ALLOCATABLE at (1)". I figure this has
> something to do with the auto-generated "*-f2pywrappers2.f90" file, but the
> build deletes the file.
> If anyone knows an f2py friendly way to rework this, I would be happy to
> try. I'm also fine with using ctypes if it can handle this case.
> Best,
> Casey
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

More information about the NumPy-Discussion mailing list