(1)处的外部函数'f'在子程序中没有IMPLICIT类型,带有f2py

时间:2018-05-09 20:09:53

标签: python callback fortran external f2py

我现在已经使用Fortran 90了一段时间,最近决定使用f2py包装一些模块,以便通过使用Python作为前端来简化原型设计。

但是,在尝试编译传递外部函数(来自Python)的子例程时,我遇到了错误。以下是复制错误的代码:

! file: callback.f90
subroutine callback(f,x,y)
  implicit none
  ! - args -
  ! in
  external :: f
  real(kind=kind(0.0d0)), intent(in) :: x
  ! out
  real(kind=kind(0.0d0)), intent(inout) :: y

  y = f(x)
end subroutine

现在,如果我使用f2py生成签名文件(.pyf),这就是我获得的:

!    -*- f90 -*- 
! Note: the context of this file is case sensitive.
python module callback__user__routines 
interface callback_user_interface 
    function f(x) result (y) ! in :callback:callback.f90
        intent(inout) f
        real(kind=kind(0.0d0)) intent(in) :: x
        real(kind=kind(0.0d0)) intent(inout) :: y
    end function f
end interface callback_user_interface
end python module callback__user__routines
python module callback ! in  
    interface  ! in :callback
        subroutine callback(f,x,y) ! in :callback:callback.f90
            use callback__user__routines
            external f
            real(kind=kind(0.0d0)) intent(in) :: x
            real(kind=kind(0.0d0)) intent(inout) :: y
        end subroutine callback
    end interface 
end python module callback

! This file was auto-generated with f2py (version:2).
! See http://cens.ioc.ee/projects/f2py2e/

哪个不正确,所以我用以下方式改变它:

!    -*- f90 -*- 
! Note: the context of this file is case sensitive.
python module __user__routines
    interface
        function f(x) result (y) 
            real(kind=kind(0.0d0)) intent(in) :: x
            real(kind=kind(0.0d0)) :: y
        end function f
    end interface
end python module __user__routines
python module callback ! in  
    interface  ! in :callback
        subroutine callback(f,x,y)
            use __user__routines
            external f
            real(kind=kind(0.0d0)) intent(in) :: x
            real(kind=kind(0.0d0)) intent(inout) :: y
        end subroutine callback
    end interface 
end python module callback

! This file was auto-generated with f2py (version:2).
! See http://cens.ioc.ee/projects/f2py2e/

尽管如此,两个人都告诉我我没有定义f:

callback.f90:1:21:
subroutine callback(f,x,y)
                    1
Error: Symbol ‘f’ at (1) has no IMPLICIT type
callback.f90:10:6:

  y = f(x)
      1
Error: Can't convert UNKNOWN to REAL(8) at (1)
callback.f90:1:21:

subroutine callback(f,x,y)
                    1
Error: Symbol ‘f’ at (1) has no IMPLICIT type
callback.f90:10:6:

   y = f(x)
       1
Error: Can't convert UNKNOWN to REAL(8) at (1)

我已经做了尽可能多的研究,手动正确编辑.pyf签名文件,但在这种情况下无济于事。尝试将f2py与外部函数一起使用时是否有人遇到过类似的错误?

1 个答案:

答案 0 :(得分:4)

错误是正确的,这是一个Fortran错误,即使没有任何f2py或Python也会发生错误

subroutine callback(f,x,y)
  implicit none
  external :: f
  real(kind=kind(0.0d0)), intent(in) :: x
  real(kind=kind(0.0d0)), intent(inout) :: y

  y = f(x)
end subroutine

f没有声明类型,并且implicit none已启用,因此这是Fortran错误。

使用

  real(kind=kind(0.0d0)), external :: f

  external :: f
  real(kind=kind(0.0d0)) :: f