使用主.f90模块调用特定.f文件时运行f2py的问题? (unknown_subroutine?)

时间:2018-12-22 18:20:43

标签: python fortran gfortran f2py

我正在尝试使用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”,如下所示:https://github.com/david-deboer/cosmo/blob/master/Komatsu/mf_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时,无法导入该函数。

1 个答案:

答案 0 :(得分:2)

为了使F2PY为Fortran过程创建包装,它需要完全识别Fortran过程参数的类型和意图,即参数是整数/实数,标量/向量,输入/输出/两者等。 。该Fortran 接口在F2PY中称为过程的签名。如果将函数作为参数传递(例如,func中的trapzd和链接文件qromb中的qromb.f),则F2PY还需要为传递的函数的参数标识此信息。与例如Fortran 90和更高版本,Fortran 77(似乎写着qromb.f的方言)没有提供任何语言来直接在Fortran代码中明确定义此信息。

因此,在您的情况下,是F2PY无法自动使用现有信息识别过程签名(因此, unknown_subroutine 输出)。

但是,F2PY有多种方法可以成功包装您的代码:

  1. 通过创建和帮助F2PY识别过程签名 手动修改qromb.f的过程签名文件。
  2. 定义界面并使用例如qromb在您的 min_example模块,然后将Fortran 77代码作为 预编译为F2PY的对象。参见例如第一部分 answer
  3. 用更现代的Fortran方言重写qromb.f, 明确定义所有接口(您的代码似乎已被修改 从 Fortran 77的数字食谱中获得Fortran 90 版本存在,可能会有所帮助。)

您提到您是Fortran的新手,因此可能不想直接修改您的源代码。考虑到这一点,我将更详细地描述上面列出的第一个解决方案,该解决方案适用于您的示例:

第一步是通过执行以下命令来创建签名文件(请参见F2PY documentation中的智能方式):

f2py -m min_example -h min_example.pyf min_example.f90 qromb.f

这将创建一个名为min_example.pyf的签名文件,打开该文件,您会注意到,仅出现trapzdqromb的Fortran接口(F2PY 签名)为unknown_subroutine。另外,polint的接口需要修改。

然后,您的第二步是修改min_example.pyf,使其仅包含以下文本(另请参阅F2Py documentation on call-back functions):

!    -*- 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: 2