我试图使用f2py将fortran函数与我的主要python代码集成起来。但是,当我试图包含一个特定的“.f”文件时,f2py会引发一个错误(但是可以处理其他“.f”文件)。我已经为我的主“.f90”文件创建了以下最低工作示例:
module min_example
implicit none
public :: calc_min
contains
subroutine calc_min
print*, 'test'
return
end subroutine calc_min
end module min_example造成问题的'.f‘文件是'qromb.f’,如下所示:jenkins/qromb.f
The error looks like this (sorry for the huge block - I'm new to this and not sure what is relevant):
running build
running config_cc
unifing config_cc, config, build_clib, build_ext, build commands --compiler options
running config_fc
unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
running build_src
build_src
building extension "min_example" sources
f2py options: []
f2py:> /tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c
creating /tmp/tmpQULhjN/src.linux-x86_64-2.7
Reading fortran codes...
Reading file 'qromb.f' (format:fix,strict)
Line #1 in qromb.f:" SUBROUTINE qromb(func,a,b,ss,q) ! one parameter"
analyzeline: No name/args pattern found for line.
Line #22 in qromb.f:" SUBROUTINE trapzd(func,a,b,s,n,q) ! one paramete"
analyzeline: No name/args pattern found for line.
Reading file 'min_example.f90' (format:free)
Post-processing...
Block: min_example
Block: unknown_subroutine
Block: unknown_subroutine
Block: polint
Block: min_example
Block: calc_min
Post-processing (stage 2)...
Block: min_example
Block: unknown_interface
Block: unknown_subroutine
Block: unknown_subroutine
Block: polint
Block: min_example
Block: calc_min
Building modules...
Building module "min_example"...
Constructing wrapper function "unknown_subroutine"...
unknown_subroutine()
Constructing wrapper function "unknown_subroutine"...
unknown_subroutine()
Constructing wrapper function "polint"...
polint(xa,ya,x,y,dy,[n])
Constructing F90 module support for "min_example"...
Constructing wrapper function "min_example.calc_min"...
calc_min()
Wrote C/API module "min_example" to file "/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c"
Fortran 90 wrappers are saved to "/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_example-f2pywrappers2.f90"
adding '/tmp/tmpQULhjN/src.linux-x86_64-2.7/fortranobject.c' to sources.
adding '/tmp/tmpQULhjN/src.linux-x86_64-2.7' to include_dirs.
copying /home/anasal/anaconda2/lib/python2.7/site-packages/numpy/f2py/src/fortranobject.c -> /tmp/tmpQULhjN/src.linux-x86_64-2.7
copying /home/anasal/anaconda2/lib/python2.7/site-packages/numpy/f2py/src/fortranobject.h -> /tmp/tmpQULhjN/src.linux-x86_64-2.7
adding '/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_example-f2pywrappers2.f90' to sources.
build_src: building npy-pkg config files
running build_ext
customize UnixCCompiler
customize UnixCCompiler using build_ext
customize Gnu95FCompiler
Found executable /usr/bin/gfortran
customize Gnu95FCompiler
customize Gnu95FCompiler using build_ext
building 'min_example' extension
compiling C sources
C compiler: gcc -pthread -B /home/anasal/anaconda2/compiler_compat -Wl,--sysroot=/ -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC
creating /tmp/tmpQULhjN/tmp
creating /tmp/tmpQULhjN/tmp/tmpQULhjN
creating /tmp/tmpQULhjN/tmp/tmpQULhjN/src.linux-x86_64-2.7
compile options: '-I/tmp/tmpQULhjN/src.linux-x86_64-2.7 -I/home/anasal/anaconda2/lib/python2.7/site-packages/numpy/core/include -I/home/anasal/anaconda2/include/python2.7 -c'
gcc: /tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c
In file included from /home/anasal/anaconda2/lib/python2.7/site-packages/numpy/core/include/numpy/ndarraytypes.h:1809:0,
from /home/anasal/anaconda2/lib/python2.7/site-packages/numpy/core/include/numpy/ndarrayobject.h:18,
from /home/anasal/anaconda2/lib/python2.7/site-packages/numpy/core/include/numpy/arrayobject.h:4,
from /tmp/tmpQULhjN/src.linux-x86_64-2.7/fortranobject.h:13,
from /tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c:19:
/home/anasal/anaconda2/lib/python2.7/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
#warning "Using deprecated NumPy API, disable it by " \
^
/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c:288:13: error: redefinition of ‘doc_f2py_rout_min_example_unknown_subroutine’
static char doc_f2py_rout_min_example_unknown_subroutine[] = "\
^
/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c:228:13: note: previous definition of ‘doc_f2py_rout_min_example_unknown_subroutine’ was here
static char doc_f2py_rout_min_example_unknown_subroutine[] = "\
^
/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c:292:18: error: redefinition of ‘f2py_rout_min_example_unknown_subroutine’
static PyObject *f2py_rout_min_example_unknown_subroutine(const PyObject *capi_self,
^
/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c:232:18: note: previous definition of ‘f2py_rout_min_example_unknown_subroutine’ was here
static PyObject *f2py_rout_min_example_unknown_subroutine(const PyObject *capi_self,
^
/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c:112:12: warning: ‘f2py_size’ defined but not used [-Wunused-function]
static int f2py_size(PyArrayObject* var, ...)
^
/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c:228:13: warning: ‘doc_f2py_rout_min_example_unknown_subroutine’ defined but not used [-Wunused-variable]
static char doc_f2py_rout_min_example_unknown_subroutine[] = "\
^
/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c:232:18: warning: ‘f2py_rout_min_example_unknown_subroutine’ defined but not used [-Wunused-function]
static PyObject *f2py_rout_min_example_unknown_subroutine(const PyObject *capi_self,
^
In file included from /home/anasal/anaconda2/lib/python2.7/site-packages/numpy/core/include/numpy/ndarraytypes.h:1809:0,
from /home/anasal/anaconda2/lib/python2.7/site-packages/numpy/core/include/numpy/ndarrayobject.h:18,
from /home/anasal/anaconda2/lib/python2.7/site-packages/numpy/core/include/numpy/arrayobject.h:4,
from /tmp/tmpQULhjN/src.linux-x86_64-2.7/fortranobject.h:13,
from /tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c:19:
/home/anasal/anaconda2/lib/python2.7/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
#warning "Using deprecated NumPy API, disable it by " \
^
/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c:288:13: error: redefinition of ‘doc_f2py_rout_min_example_unknown_subroutine’
static char doc_f2py_rout_min_example_unknown_subroutine[] = "\
^
/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c:228:13: note: previous definition of ‘doc_f2py_rout_min_example_unknown_subroutine’ was here
static char doc_f2py_rout_min_example_unknown_subroutine[] = "\
^
/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c:292:18: error: redefinition of ‘f2py_rout_min_example_unknown_subroutine’
static PyObject *f2py_rout_min_example_unknown_subroutine(const PyObject *capi_self,
^
/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c:232:18: note: previous definition of ‘f2py_rout_min_example_unknown_subroutine’ was here
static PyObject *f2py_rout_min_example_unknown_subroutine(const PyObject *capi_self,
^
/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c:112:12: warning: ‘f2py_size’ defined but not used [-Wunused-function]
static int f2py_size(PyArrayObject* var, ...)
^
/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c:228:13: warning: ‘doc_f2py_rout_min_example_unknown_subroutine’ defined but not used [-Wunused-variable]
static char doc_f2py_rout_min_example_unknown_subroutine[] = "\
^
/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c:232:18: warning: ‘f2py_rout_min_example_unknown_subroutine’ defined but not used [-Wunused-function]
static PyObject *f2py_rout_min_example_unknown_subroutine(const PyObject *capi_self,
^
error: Command "gcc -pthread -B /home/anasal/anaconda2/compiler_compat -Wl,--sysroot=/ -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/tmp/tmpQULhjN/src.linux-x86_64-2.7 -I/home/anasal/anaconda2/lib/python2.7/site-packages/numpy/core/include -I/home/anasal/anaconda2/include/python2.7 -c /tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.c -o /tmp/tmpQULhjN/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.o -MMD -MF /tmp/tmpQULhjN/tmp/tmpQULhjN/src.linux-x86_64-2.7/min_examplemodule.o.d" failed with exit status 1我编译它时使用:
gfortran -c min_example.f90
gfortran -c qromb.f
f2py -c qromb.f min_example.f90 -m min_example任何帮助都是非常感谢的!谢谢!
更新:我一直在四处搜寻,发现的线索很少。有人建议f2py在内联注释上有问题,但是删除它们没有帮助。我读到的另一个答案是将“.f”文件转换为“.f90”文件,但老实说,我不知道两者之间的区别(我对fortran非常陌生)。所以我不知道该怎么做( .f文件的结构对我来说不太清楚),我也不知道它是否会减慢速度。
另一个更新:使用'f2py -m min_example min_example.f90 qromb.f‘编译提供以下输出:
Reading fortran codes...
Reading file 'min_example.f90' (format:free)
Reading file 'qromb.f' (format:fix,strict)
Post-processing...
Block: min_example
Block: min_example
Block: calc_min
Block: qromb
Block: trapzd
Block: func
Block: polint
Post-processing (stage 2)...
Block: min_example
Block: unknown_interface
Block: min_example
Block: calc_min
Block: qromb
Block: trapzd
Block: polint
Building modules...
Constructing call-back function "cb_func_in_trapzd__user__routines"
def func(x,q): return sum
Building module "min_example"...
Constructing wrapper function "qromb"...
routsign2map: Confused: function qromb has externals ['func'] but no "use" statement.
sign2map: Confused: external func is not in lcb_map[].
append_needs: unknown need 'func'
append_needs: unknown need 'func'
qromb(func,a,b,ss,q,[func_extra_args])
Constructing wrapper function "trapzd"...
trapzd(func,a,b,s,n,q,[func_extra_args])
Constructing wrapper function "polint"...
polint(xa,ya,x,y,dy,[n])
Constructing F90 module support for "min_example"...
Constructing wrapper function "min_example.calc_min"...
calc_min()
Wrote C/API module "min_example" to file "./min_examplemodule.c"
Fortran 90 wrappers are saved to "./min_example-f2pywrappers2.f90"并生成以下文件:
min_example-f2pywrappers2.f90
min_example.mod
min_example.o
qromb.o
min_examplemodule.c 这看起来很有希望,但是当我进入python时,我无法导入函数。
发布于 2018-12-30 07:58:06
为了使F2PY能够为Fortran过程创建包装器,它需要完全识别Fortran过程参数的类型和意图,即参数是整数/实、标量/向量、输入/输出/向量,等等。这个Fortran接口在F2PY中称为过程签名。如果函数作为参数传递(例如,func在trapzd中,qromb在链接文件qromb.f中),则F2PY还需要为传递的函数的参数标识此信息。与例如Fortran 90及更高版本不同,Fortran 77 ( qromb.f所用的方言)没有提供任何语言来直接在Fortran代码中明确定义这些信息。
因此,在您的示例中发生的情况是,F2PY未能用手头的信息自动识别过程签名(因此unknown_subroutine输出)。
但是,F2PY可以通过多种方法成功地包装代码:
qromb.f识别过程签名。qromb模块中使用例如min_example,并将Fortran 77代码作为预编译对象传递给F2PY。例如,参见本回答的第一部分。qromb.f,显式定义所有接口(您的代码似乎是从Fortran 77中的数值累进中修改的,其中存在Fortran 90版本,这可能会有所帮助)。您提到您是一个Fortran新手,因此可能不想直接修改您的源代码。考虑到这一点,我将更详细地描述上面列出的第一个解决方案,并将其应用于您的示例:
第一步是通过执行以下命令创建一个签名文件(请参阅F2PY文档中的智能方式):
f2py -m min_example -h min_example.pyf min_example.f90 qromb.f这会创建一个名为min_example.pyf的签名文件,打开这个文件您会注意到,trapzd和qromb的Fortran接口(F2PY签名)只显示为unknown_subroutine。此外,polint的接口需要修改。
然后,您的第二步是修改min_example.pyf,使其只包含以下文本(也参考关于回调函数的F2Py文档):
! -*- f90 -*-
! Note: the context of this file is case sensitive.
python module __user__routines
interface
function fun(x,q) result(res) ! in :min_example:qromb.f
real*8 intent(in) :: x, q
real*8 :: res
end function fun
end interface
end python module __user__routines
python module min_example ! in
interface ! in :min_example
module min_example ! in :min_example:min_example.f90
subroutine calc_min ! in :min_example:min_example.f90:min_example
end subroutine calc_min
end module min_example
subroutine qromb(func,a,b,ss,q) ! in :min_example:qromb.f
use __user__routines, func=>fun
external func
real*8 intent(in) :: a, b, q
real*8 intent(out) :: ss
end subroutine qromb
subroutine trapzd(func,a,b,s,n,q) ! in :min_example:qromb.f
use __user__routines, func=>fun
external func
real*8 intent(in) :: a, b, q
integer intent(in) :: n
real*8 intent(inout) :: s
end subroutine trapzd
subroutine polint(xa,ya,n,x,y,dy) ! in :min_example:qromb.f
real*8 dimension(n),intent(in) :: xa
real*8 dimension(n),intent(in),depend(n) :: ya
integer, optional,intent(in),check(len(xa)>=n),depend(xa) :: n=len(xa)
real*8 intent(in) :: x
real*8 intent(out) :: y, dy
end subroutine polint
end interface
end python module min_example
! This file was auto-generated with f2py (version:2).
! See http://cens.ioc.ee/projects/f2py2e/第三个也是最后一个步骤是使用F2PY和手动修改的签名文件编译源代码,方法是执行以下命令:
f2py -c min_example.pyf min_example.f90 qromb.f您的示例没有显示您打算如何在Python中使用Fortran代码,但是这里有一个Python脚本示例,显示了刚刚编译的模块的使用情况:
import numpy as np
from scipy.integrate import romberg
import min_example
print(min_example.__doc__)
print(min_example.qromb.__doc__)
def func(x, q):
return q*np.sin(x)
a = 0.0
b = np.pi
q = 1.0
f_f2py = min_example.qromb(func, a, b, q)
f_scipy = romberg(func, a, b, args=(q,))
print("f2py: {:0.7g}".format(f_f2py))
print("scipy: {:0.7g}".format(f_scipy))这给了我以下输出:
<auto-generated documentation strings>
f2py: 2
scipy: 2https://stackoverflow.com/questions/53898232
复制相似问题