我已经使用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与外部函数一起使用时,有人遇到过类似的错误吗?
该错误是正确的,并且是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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句