首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用f2py编译Fortran模块

用f2py编译Fortran模块
EN

Stack Overflow用户
提问于 2011-12-20 01:02:23
回答 3查看 10.6K关注 0票数 8

我有一个Fortran模块,我正在尝试用f2py编译它(如下所示)。当我删除模块声明并将子例程单独留在文件中时,一切工作正常。但是,如果如下所示声明了模块,我会得到以下结果:

代码语言:javascript
复制
> 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:

代码语言:javascript
复制
  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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-12-20 16:36:36

您正在尝试将Fortran模块放在Python模块中。如果你想这样,名字必须不同,例如

代码语言:javascript
复制
 f2py.py -c -m SOMEDIFFERENTNAME itimes-s2.f

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

您也可以将其导入为

代码语言:javascript
复制
from pythonmodule import fortranmodule
fortranmodule.yourfunction()

否则它会在我的机器上工作。

票数 10
EN

Stack Overflow用户

发布于 2011-12-20 01:41:42

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

从该站点:

代码语言:javascript
复制
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

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

代码语言:javascript
复制
f2py -c -m fib3 fib3.f
票数 2
EN

Stack Overflow用户

发布于 2020-09-17 02:29:58

要用f2py包装F90模块,您确实需要指定哪些功能可以从模块外部访问。为此,您可以使用private和public fortran90关键字。您还应该将扩展名从.f更改为.f90或.F90。我在演示publicprivate关键字用法的代码中做了一些更改。

首先,创建保存模块对象的fortran接口的签名文件:

代码语言:javascript
复制
f2py -h interface.pyf -m module_name module_its.f90 --overwrite-signature

THen编译模块:

代码语言:javascript
复制
f2py interface.pyf -c module_its.f90 

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

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8564771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档