【发布时间】:2018-05-09 20:09:53
【问题描述】:
我使用 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 与外部函数一起使用时遇到过类似的错误?
【问题讨论】:
标签: python callback fortran external f2py