[Numpy-discussion] f2py with allocatable arrays
Tue Jul 3 05:19:21 CDT 2012
> 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
> work (it checks if each particle it perturbs ends up in the domain).
> module zp
> implicit none
> 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 ::
> 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
> 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.
Can you split your code so that you have a subroutine which calculates the
number of particles only, and call this subroutine from your 'original'
routine? If yes, then you might be able to say somehting like
real (kind=dp), intent(out), dimension(get_particle_number(WHATEVER) ::
Not entirely sure, though ... Can someone with more f2py knowledge confirm
More information about the NumPy-Discussion