[Numpy-discussion] f2py with allocatable arrays

Sturla Molden sturla@molden...
Tue Jul 3 09:20:04 CDT 2012

Den 03.07.2012 11:54, skrev George Nurser:
>> 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

This looks like a Fortran error.  deallocate is called automatically on 
allocatable arrays when a subroutine exit (guarranteed from Fortran 95, 
optional in Fortran 90). Allocatable arrays might even be put on the 
stack depending on the requested size. I am not even sure intent(out) on 
an allocatable array is legal or an error the compiler should trap, but 
it is obviously very bad Fortran. So "positions" and "velocities" should 
here be declared pointer, not allocatable.

As for f2py: Allocatable arrays are local variables for internal use, 
and they are not a part of the subroutine's calling interface. f2py only 
needs to know about the interface, not the local variables.


More information about the NumPy-Discussion mailing list