使用f2py编译fortran模块

时间:2011-12-19 17:02:23

标签: python fortran f2py

我有一个Fortran模块,我试图用f2py编译(下面列出)。当我删除模块声明并将子程序单独保留在文件中时,一切正常。但是,如果声明模块如下所示,我得到以下结果:

> f2py.py -c -m its --compiler=mingw itimes-s2.f
...
Reading fortran codes...
    Reading file 'itimes-s2.f' (format:fix,strict)
crackline: groupcounter=1 groupname={0: '', 1: 'module', 2: 'interface', 3: 'subroutine'}
crackline: Mismatch of blocks encountered. Trying to fix it by assuming "end" statement.
...
c:\users\astay13\appdata\local\temp\tmpgh5ag8\Release\users\astay13\appdata\local\temp\tmpgh5ag8\src.win32-3.2\itsmodule.o:itsmodule.c:(.data+0xec): undefined reference to `itimes_'
collect2: ld returned 1 exit status

在f2py中编译模块或子程序有什么不同?我是否在模块中留下了一些重要的东西导致f2py出现问题?请注意,当我单独使用gfortran时,模块编译得很好。

软件:Windows 7; gcc,gfortran 4.6.1(MinGW); python 3.2.2; f2py v2

itimes-s2.f:

  module its

  contains

  subroutine itimes(infile,outfile)

    implicit none

    ! Constants
    integer, parameter :: dp = selected_real_kind(15)

    ! Subroutine Inputs
    character(*), intent(in) :: infile
    character(*), intent(in) :: outfile

    ! Internal variables
    real(dp) :: num
    integer :: inu
    integer :: outu
    integer :: ios

    inu = 11
    outu = 22

    open(inu,file=infile,action='read')
    open(outu,file=outfile,action='write',access='append')

    do
      read(inu,*,IOSTAT=ios) num
      if (ios < 0) exit

      write(outu,*) num**2
    end do

  end subroutine itimes

  end module its

3 个答案:

答案 0 :(得分:8)

您正在尝试在Python模块中安装Fortran模块。如果您愿意,名称必须不同,例如

 f2py.py -c -m SOMEDIFFERENTNAME itimes-s2.f

结果将被称为pythonmodule.fortranmodule.yourfunction()

否则它可以在我的机器上运行。

答案 1 :(得分:2)

要使f2py正常工作,您需要有一个签名文件来指导界面创建或使用f2py注释修改源代码以帮助界面。有关详细信息,请参阅http://cens.ioc.ee/projects/f2py2e/usersguide/#signature-file

从该网站:

C FILE: FIB3.F
      SUBROUTINE FIB(A,N)
C
C     CALCULATE FIRST N FIBONACCI NUMBERS
C
      INTEGER N
      REAL*8 A(N)
Cf2py intent(in) n
Cf2py intent(out) a
Cf2py depend(n) a
      DO I=1,N
         IF (I.EQ.1) THEN
            A(I) = 0.0D0
         ELSEIF (I.EQ.2) THEN
            A(I) = 1.0D0
         ELSE 
            A(I) = A(I-1) + A(I-2)
         ENDIF
      ENDDO
      END
C END FILE FIB3.F

现在可以在一个命令中构建扩展模块:

f2py -c -m fib3 fib3.f

答案 2 :(得分:0)

要用f2py包装F90模块,您需要指定可以从模块外部访问的功能。为此,您有专用和公用的fortran90关键字。您还应该将扩展名从.f更改为.F90或.f90。我对您的代码进行了一些更改,以演示publicprivate关键字的使用。

首先创建用于模块对象的fortran接口的签名文件:

f2py -h interface.pyf -m module_name module_its.f90 --overwrite-signature

然后编译模块:

f2py interface.pyf -c module_its.f90 

您的代码已修改并命名为module_its.f90:

module its   
  ! use other modules
  implicit none
  private          ! everything in this module is private by standard 
                   ! (it can be used only inside this F90 module)
  public :: itimes ! Make you subroutine public
 
  contains

  subroutine itimes(infile,outfile)
    ! use other modules here to

    !implicit none holds for everthing in this module

    ! Constants
    integer, parameter :: dp = selected_real_kind(15)

    ! Subroutine Inputs
    character(*), intent(in) :: infile
    character(*), intent(in) :: outfile

    ! Internal variables
    real(dp) :: num
    integer :: inu
    integer :: outu
    integer :: ios

    inu = 11
    outu = 22

    open(inu,file=infile,action='read')
    open(outu,file=outfile,action='write',access='append')

    do
      read(inu,*,IOSTAT=ios) num
      if (ios < 0) exit

      write(outu,*) num**2
    end do

  end subroutine itimes

end module its