首页 > 解决方案 > f2py 无法确定表达式类型 - 我该如何解决?

问题描述

我正在尝试用 f2py 包装一些 Fortran 代码,但出现以下错误:

Reading fortran codes...
    Reading file 'first_fn_prog.f90' (format:fix)
Line #8 in first_fn_prog.f90:"      parameter (a = -1.d+0/2.72d+0)         "
    determineexprtype: could not determine expressions ('-1.d+0/2.72d+0') type.
analyzeline: Failed to evaluate '-1.d+0/2.72d+0'. Ignoring: invalid syntax (<string>, line 1)

我正在谈论的代码在这里:

  subroutine show(t)    
  real*8 T                            
  write (*,*) 'T = ', t 
  end                           

  subroutine shout() 
  real*8 a                      
  parameter (a = -1.d+0/2.72d+0)        
  write (*,*) 'SHOUT SHOUT!'    
  call show(a)                 

  end

  program circle
  real*8 a, c
  integer b

  common /data/ arr1(19)   

  write (*,*) 'Enter a:'
  read (*,*) a
  write (*,*) 'Enter b'
  read (*,*) b
  c = a + dble(b)

  call shout()

  end

如果我改变线条

  real*8 a                      
  parameter (a = -1.d+0/2.72d+0)

  real*8 a                      
  a = -1.d+0/2.72d+0

'a' var 中提到的表达式没有错误。此外,我已经测试了使用错误和没有错误构造的两个模块,似乎在这两种情况下,在 'a' var 中计算的值都是相同的。所以我认为这个错误不会影响任何事情,但我仍然需要解决这个问题。

此外,如果我将 1.d+0/2.72d+0 替换为一个简单的值,请说:

  real*8 a                      
  parameter (a = 3)

f2py 也没有显示任何错误。

我能用这个做什么?

完整的 f2py 包装日志如下所示:

f2py -c first_fn_prog.f90 -m ft
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 "ft" sources
f2py options: []
f2py:> /tmp/tmp3axmu237/src.linux-x86_64-3.5/ftmodule.c
creating /tmp/tmp3axmu237/src.linux-x86_64-3.5
Reading fortran codes...
    Reading file 'first_fn_prog.f90' (format:fix)
Line #8 in first_fn_prog.f90:"      parameter (a = -1.d+0/2.72d+0)         "
    determineexprtype: could not determine expressions ('-1.d+0/2.72d+0') type.
analyzeline: Failed to evaluate '-1.d+0/2.72d+0'. Ignoring: invalid syntax (<string>, line 1)
Post-processing...
    Block: ft
            Block: show
            Block: shout
            Block: circle
Post-processing (stage 2)...
Building modules...
    Building module "ft"...
        Constructing wrapper function "show"...
          show(t)
        Constructing wrapper function "shout"...
          shout()
        Constructing COMMON block support for "data"...
          arr1
    Wrote C/API module "ft" to file "/tmp/tmp3axmu237/src.linux-x86_64-3.5/ftmodule.c"
    Fortran 77 wrappers are saved to "/tmp/tmp3axmu237/src.linux-x86_64-3.5/ft-f2pywrappers.f"
  adding '/tmp/tmp3axmu237/src.linux-x86_64-3.5/fortranobject.c' to sources.
  adding '/tmp/tmp3axmu237/src.linux-x86_64-3.5' to include_dirs.
copying /home/veniamin/.local/lib/python3.5/site-packages/numpy/f2py/src/fortranobject.c -> /tmp/tmp3axmu237/src.linux-x86_64-3.5
copying /home/veniamin/.local/lib/python3.5/site-packages/numpy/f2py/src/fortranobject.h -> /tmp/tmp3axmu237/src.linux-x86_64-3.5
  adding '/tmp/tmp3axmu237/src.linux-x86_64-3.5/ft-f2pywrappers.f' to sources.
build_src: building npy-pkg config files
running build_ext
customize UnixCCompiler
customize UnixCCompiler using build_ext
get_default_fcompiler: matching types: '['gnu95', 'intel', 'lahey', 'pg', 'absoft', 'nag', 'vast', 'compaq', 'intele', 'intelem', 'gnu', 'g95', 'pathf95', 'nagfor']'
customize Gnu95FCompiler
Found executable /usr/bin/gfortran
customize Gnu95FCompiler
customize Gnu95FCompiler using build_ext
building 'ft' extension
compiling C sources
C compiler: x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC

creating /tmp/tmp3axmu237/tmp
creating /tmp/tmp3axmu237/tmp/tmp3axmu237
creating /tmp/tmp3axmu237/tmp/tmp3axmu237/src.linux-x86_64-3.5
compile options: '-I/tmp/tmp3axmu237/src.linux-x86_64-3.5 -I/home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include -I/usr/include/python3.5m -c'
x86_64-linux-gnu-gcc: /tmp/tmp3axmu237/src.linux-x86_64-3.5/ftmodule.c
x86_64-linux-gnu-gcc: /tmp/tmp3axmu237/src.linux-x86_64-3.5/fortranobject.c
In file included from /home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include/numpy/ndarraytypes.h:1822:0,
                 from /home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include/numpy/ndarrayobject.h:12,
                 from /home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include/numpy/arrayobject.h:4,
                 from /tmp/tmp3axmu237/src.linux-x86_64-3.5/fortranobject.h:13,
                 from /tmp/tmp3axmu237/src.linux-x86_64-3.5/ftmodule.c:15:
/home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:17:2: warning: #warning "Using deprecated NumPy API, disable it with " "#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
 #warning "Using deprecated NumPy API, disable it with " \
  ^
In file included from /home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include/numpy/ndarraytypes.h:1822:0,
                 from /home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include/numpy/ndarrayobject.h:12,
                 from /home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include/numpy/arrayobject.h:4,
                 from /tmp/tmp3axmu237/src.linux-x86_64-3.5/fortranobject.h:13,
                 from /tmp/tmp3axmu237/src.linux-x86_64-3.5/fortranobject.c:2:
/home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:17:2: warning: #warning "Using deprecated NumPy API, disable it with " "#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
 #warning "Using deprecated NumPy API, disable it with " \
  ^
compiling Fortran sources
Fortran f77 compiler: /usr/bin/gfortran -Wall -g -ffixed-form -fno-second-underscore -fPIC -O3 -funroll-loops
Fortran f90 compiler: /usr/bin/gfortran -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops
Fortran fix compiler: /usr/bin/gfortran -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops
compile options: '-I/tmp/tmp3axmu237/src.linux-x86_64-3.5 -I/home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include -I/usr/include/python3.5m -c'
gfortran:fix: first_fn_prog.f90
gfortran:f77: /tmp/tmp3axmu237/src.linux-x86_64-3.5/ft-f2pywrappers.f
/usr/bin/gfortran -Wall -g -Wall -g -shared /tmp/tmp3axmu237/tmp/tmp3axmu237/src.linux-x86_64-3.5/ftmodule.o /tmp/tmp3axmu237/tmp/tmp3axmu237/src.linux-x86_64-3.5/fortranobject.o /tmp/tmp3axmu237/first_fn_prog.o /tmp/tmp3axmu237/tmp/tmp3axmu237/src.linux-x86_64-3.5/ft-f2pywrappers.o -L/usr/lib/gcc/x86_64-linux-gnu/5 -L/usr/lib/gcc/x86_64-linux-gnu/5 -lgfortran -o ./ft.cpython-35m-x86_64-linux-gnu.so
Removing build directory /tmp/tmp3axmu237

解决了

避免上述错误的最简单方法就是声明一个变量并为其赋值,而不使用“参数”语句。

标签: pythonfortranfortran77f2py

解决方案


由于我对 Fortran 的了解不足,我可以决定的最好的事情就是避免使用“参数”声明并执行以下操作:

  real*8 a                      
  a = -1.d+0/2.72d+0

不确定 Fortran 以这种方式声明常量是否至关重要,但我想这对我来说是可行的。

更新

下面的构造

REAL*8, PARAMETER :: A=-1.d+0/2.72d+0

即使在使用 Fortran77 编译器编译文件时也可以正常工作。感谢弗朗西斯卡卢斯。


推荐阅读