在f2py

时间:2017-05-19 15:19:41

标签: python fortran f2py

我在python中编写了一个利用Fortran 90优化算法的代码。 我的想法是让python中的MAIN例程调用Fortran中的算法,调用另一个传递一些参数的python函数来计算目标函数的值(也就是说,它返回一个标量),如下所示:

[Python) MAIN  ⇒ [Fortran] BB_algo ⇒ [Python] FUNCT

这个问题应该通过在python中使用f2py库来解决。 为了尝试使这种方法工作,我编写了一些更简单的代码来进行类似的操作。

Fortran子例程称为writepy

subroutine writepy(funct,n)
    implicit none
    real, EXTERNAL :: funct
      INTEGER :: I,n
      REAL*8  :: r
!f2py intent(in), n
!f2py intent(out), r
!f2py real y
!f2py y=funct(y)
      r = 1D0
      DO I=1,5
         write(*,*) 'funct:', funct(n)
         r = r + funct(n)
      ENDDO
    return
end

虽然Python中的代码如下:

import os
os.system("f2py -m wp  -c writepy.f90")
import wp

def funct(n):
    print(n);    
    return n

if __name__ == '__main__':
    n=3;    
    wp.writepy(funct,n)

Fortran与gfortran编译得很好,但是当我尝试用f2py编译它时,似乎在Fortran的C中包装器有一些错误,但我现在还不知道该怎么做。

我得到以下内容:

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 "wp" sources
f2py options: []
f2py:> /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c
creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5
Reading fortran codes...
    Reading file 'writepy.f90' (format:free)
{'after': '(in), n', 'before': '', 'this': 'intent'}
Line #12 in writepy.f90:"      intent(in), n"
    analyzeline: no name pattern found in intent statement for ''. Skipping.
{'after': '(out), r', 'before': '', 'this': 'intent'}
Line #13 in writepy.f90:"      intent(out), r"
    analyzeline: no name pattern found in intent statement for ''. Skipping.
Post-processing...
    Block: wp
            Block: writepy
Post-processing (stage 2)...
Building modules...
    Building module "wp"...
        Constructing wrapper function "writepy"...
sign2map: Confused: external funct is not in lcb_map[].
append_needs: unknown need 'funct'
append_needs: unknown need 'funct'
          writepy(funct,n,[funct_extra_args])
    Wrote C/API module "wp" to file "/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c"
  adding '/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.c' to sources.
  adding '/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5' to include_dirs.
copying /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/f2py/src/fortranobject.c -> /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5
copying /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/f2py/src/fortranobject.h -> /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5
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 'wp' extension
compiling C sources
C compiler: /usr/bin/clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/Valyria/anaconda/include -arch x86_64

creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var
creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders
creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders/4q
creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn
creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T
creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm
creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5
compile options: '-I/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5 -I/Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include -I/Users/Valyria/anaconda/include/python3.5m -c'
clang: /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.c
In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.c:2:
In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.h:13:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/arrayobject.h:4:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarrayobject.h:18:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarraytypes.h:1788:
/Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: "Using deprecated NumPy API, disable it by "          "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings]
#warning "Using deprecated NumPy API, disable it by " \
 ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.c:139:18: warning: comparison of integers of different signs: 'Py_ssize_t' (aka 'long') and 'unsigned long' [-Wsign-compare]
        if (size < sizeof(notalloc)) {
            ~~~~ ^ ~~~~~~~~~~~~~~~~
2 warnings generated.
clang: /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c
In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:18:
In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.h:13:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/arrayobject.h:4:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarrayobject.h:18:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarraytypes.h:1788:
/Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: "Using deprecated NumPy API, disable it by "          "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings]
#warning "Using deprecated NumPy API, disable it by " \
 ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:277:3: error: use of undeclared identifier 'funct_typedef'
  funct_typedef funct_cptr;
  ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:293:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
  funct_cptr = F2PyCapsule_AsVoidPtr(funct_capi);
  ^~~~~~~~~~
  funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:273:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:295:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
  funct_cptr = funct;
  ^~~~~~~~~~
  funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:273:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:295:16: error: use of undeclared identifier 'funct'
  funct_cptr = funct;
               ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:298:24: error: use of undeclared identifier 'funct_nofargs'
  funct_nofargs_capi = funct_nofargs;
                       ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:299:50: error: expected expression
  if (create_cb_arglist(funct_capi,funct_xa_capi,#maxnofargs#,#nofoptargs#,&funct_nofargs,&funct_args_capi,"failed in processing argument list for call-back funct.")) {
                                                 ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:313:22: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
        (*f2py_func)(funct_cptr,&n);
                     ^~~~~~~~~~
                     funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:273:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:313:22: warning: incompatible pointer types passing 'PyObject *' (aka 'struct _object *') to parameter of type 'float *' [-Wincompatible-pointer-types]
        (*f2py_func)(funct_cptr,&n);
                     ^~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:332:16: warning: explicitly assigning a variable of type 'PyObject *' (aka 'struct _object *') to itself [-Wself-assign]
    funct_capi = funct_capi;
    ~~~~~~~~~~ ^ ~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:334:21: warning: explicitly assigning a variable of type 'PyTupleObject *' to itself [-Wself-assign]
    funct_args_capi = funct_args_capi;
    ~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:335:5: error: use of undeclared identifier 'funct_nofargs'
    funct_nofargs = funct_nofargs_capi;
    ^
4 warnings and 8 errors generated.
In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:18:
In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.h:13:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/arrayobject.h:4:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarrayobject.h:18:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarraytypes.h:1788:
/Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: "Using deprecated NumPy API, disable it by "          "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings]
#warning "Using deprecated NumPy API, disable it by " \
 ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:277:3: error: use of undeclared identifier 'funct_typedef'
  funct_typedef funct_cptr;
  ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:293:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
  funct_cptr = F2PyCapsule_AsVoidPtr(funct_capi);
  ^~~~~~~~~~
  funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:273:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:295:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
  funct_cptr = funct;
  ^~~~~~~~~~
  funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:273:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:295:16: error: use of undeclared identifier 'funct'
  funct_cptr = funct;
               ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:298:24: error: use of undeclared identifier 'funct_nofargs'
  funct_nofargs_capi = funct_nofargs;
                       ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:299:50: error: expected expression
  if (create_cb_arglist(funct_capi,funct_xa_capi,#maxnofargs#,#nofoptargs#,&funct_nofargs,&funct_args_capi,"failed in processing argument list for call-back funct.")) {
                                                 ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:313:22: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
        (*f2py_func)(funct_cptr,&n);
                     ^~~~~~~~~~
                     funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:273:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:313:22: warning: incompatible pointer types passing 'PyObject *' (aka 'struct _object *') to parameter of type 'float *' [-Wincompatible-pointer-types]
        (*f2py_func)(funct_cptr,&n);
                     ^~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:332:16: warning: explicitly assigning a variable of type 'PyObject *' (aka 'struct _object *') to itself [-Wself-assign]
    funct_capi = funct_capi;
    ~~~~~~~~~~ ^ ~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:334:21: warning: explicitly assigning a variable of type 'PyTupleObject *' to itself [-Wself-assign]
    funct_args_capi = funct_args_capi;
    ~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:335:5: error: use of undeclared identifier 'funct_nofargs'
    funct_nofargs = funct_nofargs_capi;
    ^
4 warnings and 8 errors generated.
error: Command "/usr/bin/clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/Valyria/anaconda/include -arch x86_64 -I/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5 -I/Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include -I/Users/Valyria/anaconda/include/python3.5m -c /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c -o /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.o" failed with exit status 1

修改

感谢@ DavidW的回答代码现在有效,但如果我尝试从另一个函数调用fortran函数“writepy”,那么“使用未声明的标识符”的错误将再次出现。

新的Fortran代码是

subroutine writepy(funct,n,r)
    implicit none
    EXTERNAL :: funct
    REAL     :: funct
    INTEGER :: I,n
    intent(in):: n
    REAL*8  :: r
    intent(out) ::r
    r = 1D0
    DO I=1,5
        write(*,*) 'funct:', funct(I)
        r = r + funct(n)
    ENDDO
    return
end

subroutine call_writepy(funct,n,r)
    implicit none
    EXTERNAL :: funct
    REAL     :: funct
    INTEGER :: n
    intent(inout):: n
    REAL*8  :: r
    intent(inout) ::r
    call writepy(funct,n,r)
    return
end

它与GFortran一起编译,但当我在Python中使用它时,我得到:

/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:431:7: warning: variable 'return_value' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
  if (capi_j>capi_i)
      ^~~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:446:10: note: uninitialized use occurs here
  return return_value;
         ^~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:431:3: note: remove the 'if' if its condition is always true
  if (capi_j>capi_i)
  ^~~~~~~~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:363:21: note: initialize the variable 'return_value' to silence this warning
  float return_value;
                    ^
                     = 0.0
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:590:3: error: use of undeclared identifier 'funct_typedef'
  funct_typedef funct_cptr;
  ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:611:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
  funct_cptr = F2PyCapsule_AsVoidPtr(funct_capi);
  ^~~~~~~~~~
  funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:586:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:613:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
  funct_cptr = funct;
  ^~~~~~~~~~
  funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:586:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:613:16: error: use of undeclared identifier 'funct'
  funct_cptr = funct;
               ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:616:24: error: use of undeclared identifier 'funct_nofargs'
  funct_nofargs_capi = funct_nofargs;
                       ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:617:50: error: expected expression
  if (create_cb_arglist(funct_capi,funct_xa_capi,#maxnofargs#,#nofoptargs#,&funct_nofargs,&funct_args_capi,"failed in processing argument list for call-back funct.")) {
                                                 ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:634:22: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
        (*f2py_func)(funct_cptr,&n,&r);
                     ^~~~~~~~~~
                     funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:586:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:634:22: warning: incompatible pointer types passing 'PyObject *' (aka 'struct _object *') to parameter of type 'float *' [-Wincompatible-pointer-types]
        (*f2py_func)(funct_cptr,&n,&r);
                     ^~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:660:16: warning: explicitly assigning a variable of type 'PyObject *' (aka 'struct _object *') to itself [-Wself-assign]
    funct_capi = funct_capi;
    ~~~~~~~~~~ ^ ~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:662:21: warning: explicitly assigning a variable of type 'PyTupleObject *' to itself [-Wself-assign]
    funct_args_capi = funct_args_capi;
    ~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:663:5: error: use of undeclared identifier 'funct_nofargs'
    funct_nofargs = funct_nofargs_capi;
    ^
5 warnings and 8 errors generated.
routsign2map: Confused: function call_writepy has externals ['funct'] but no "use" statement.
sign2map: Confused: external funct is not in lcb_map[].
append_needs: unknown need 'funct'
append_needs: unknown need 'funct'
In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:18:
In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/fortranobject.h:13:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/arrayobject.h:4:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarrayobject.h:18:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarraytypes.h:1788:
/Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: "Using deprecated NumPy API, disable it by "          "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings]
#warning "Using deprecated NumPy API, disable it by " \
 ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:431:7: warning: variable 'return_value' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
  if (capi_j>capi_i)
      ^~~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:446:10: note: uninitialized use occurs here
  return return_value;
         ^~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:431:3: note: remove the 'if' if its condition is always true
  if (capi_j>capi_i)
  ^~~~~~~~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:363:21: note: initialize the variable 'return_value' to silence this warning
  float return_value;
                    ^
                     = 0.0
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:590:3: error: use of undeclared identifier 'funct_typedef'
  funct_typedef funct_cptr;
  ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:611:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
  funct_cptr = F2PyCapsule_AsVoidPtr(funct_capi);
  ^~~~~~~~~~
  funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:586:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:613:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
  funct_cptr = funct;
  ^~~~~~~~~~
  funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:586:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:613:16: error: use of undeclared identifier 'funct'
  funct_cptr = funct;
               ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:616:24: error: use of undeclared identifier 'funct_nofargs'
  funct_nofargs_capi = funct_nofargs;
                       ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:617:50: error: expected expression
  if (create_cb_arglist(funct_capi,funct_xa_capi,#maxnofargs#,#nofoptargs#,&funct_nofargs,&funct_args_capi,"failed in processing argument list for call-back funct.")) {
                                                 ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:634:22: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
        (*f2py_func)(funct_cptr,&n,&r);
                     ^~~~~~~~~~
                     funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:586:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:634:22: warning: incompatible pointer types passing 'PyObject *' (aka 'struct _object *') to parameter of type 'float *' [-Wincompatible-pointer-types]
        (*f2py_func)(funct_cptr,&n,&r);
                     ^~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:660:16: warning: explicitly assigning a variable of type 'PyObject *' (aka 'struct _object *') to itself [-Wself-assign]
    funct_capi = funct_capi;
    ~~~~~~~~~~ ^ ~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:662:21: warning: explicitly assigning a variable of type 'PyTupleObject *' to itself [-Wself-assign]
    funct_args_capi = funct_args_capi;
    ~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:663:5: error: use of undeclared identifier 'funct_nofargs'
    funct_nofargs = funct_nofargs_capi;
    ^
5 warnings and 8 errors generated.
error: Command "/usr/bin/clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/Valyria/anaconda/include -arch x86_64 -I/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5 -I/Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include -I/Users/Valyria/anaconda/include/python3.5m -c /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c -o /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.o" failed with exit status 1
256

2 个答案:

答案 0 :(得分:1)

有一些小问题使它无效:

subroutine writepy(funct,n,r)

我已将r添加到参数列表中。它编译没有这个,但否则没有办法让r回到Python我认为你想要的基于intent(out)

    implicit none
    EXTERNAL :: funct
    REAL :: funct

我必须将EXTERNALREAL放在不同的行上。我不知道为什么。这是你的主要问题。

  INTEGER :: I,n
  REAL*8  :: r
  intent(in) :: n
  intent(out) :: r

我已从意图行中删除了!f2py。您应该让Fortran知道意图以及f2py。 (代码仍然在没有这种变化的情况下进行编译,但这种方式更有意义)

然后我删除了您的!f2py real y!f2py y=funct(y)行。我不知道他们想要实现什么,但我不认为它们是有效的。

      r = 1D0
      DO I=1,5
          write(*,*) 'funct:', funct(n)
          r = r + funct(n)
      ENDDO
      return
end

其余的没有改变。

答案 1 :(得分:1)

问题是由于编译器无法识别Python中的函数“funct”。为了使编译器能够识别此类函数及其角色,需要修改.pyf文件。

自动生成的.pyf文件(使用命令f2py writepy.f90 -m wp -h wp.pyf)如下:

!    -*- f90 -*-
! Note: the context of this file is case sensitive.

python module __user__routines 
    interface writepy_user_interface 
        function funct(i) ! in :wp:writepy.f90:writepy:unknown_interface
            integer :: i
            real :: funct
        end function funct
    end interface writepy_user_interface
end python module __user__routines

python module wp ! in 
    interface  ! in :wp
        subroutine writepy(funct,n,r) ! in :wp:writepy.f90
            use writepy__user__routines
            external funct
            integer intent(in) :: n
            real*8 intent(out) :: r
        end subroutine writepy

        subroutine call_writepy(funct,n,r,q) ! in :wp:writepy.f90
            external funct
            integer intent(inout) :: n
            real*8 intent(out) :: r
            real*8 intent(out) :: q
        end subroutine call_writepy
    end interface 
end python module wp

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

很容易注意到f2py识别“__user__routines”模块中的“funct”。然而,它不理解在子例程“writepy”和“call_writepy”中都使用了这样的函数。这是错误的来源。 因此,用户必须修改.pyf文件,在所有“funct”作为外部对象存在的子例程中添加命令“use __user__routines”:

!    -*- f90 -*-
! Note: the context of this file is case sensitive.


python module __user__routines
    interface
        function funct(i) 
            integer :: i
            real*8  :: r
        end function funct
    end interface
end python module __user__routines


python module wp ! in 
    interface  ! in :wp
        subroutine writepy(funct,n,r) ! in :wp:writepy.f90
            use __user__routines
            external funct
            integer intent(in) :: n
            real*8 intent(out) :: r
        end subroutine writepy

        subroutine call_writepy(funct,n,r)! in :wp:writepy.f90
            use __user__routines
            external funct
            integer intent(inout) :: n
            real*8 intent(out) :: r
            real*8 intent(out) :: q
        end subroutine call_writepy
    end interface 
end python module wp

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

在f2py中编译Fortran代码时使用以下命令: f2py -c wp.pyf writepy.f90 告诉编译器明确使用编辑后的文件“wp.pyf”作为参考