当使用f2py时,fortran模块中的函数范围与为fortran程序编译时不同?

时间:2013-09-07 04:46:27

标签: python fortran90 gfortran f2py

我的问题是,在使用f2py进行编译时,模块中定义的函数无法识别某些模块变量。如果声明了传递给函数的变量类型的参数(例如描述real或维度元素的类型的变量),则会引发错误。使用gfortran编译时,我没有收到此错误。有什么区别,如何在使用f2py进行编译时解决这些错误?

我的示例文件moddata.f90包含以下代码:

module mod

  implicit none

  integer, parameter :: dp = selected_real_kind(15)
  integer, parameter :: nelem = 3
  real(kind=dp), dimension(nelem) :: b

  parameter (b=(/3,1,2/))

contains
  function foo(x,y) result(z)
!  dp, nelem are defined in module above
    real(kind=dp), intent(in) :: x !scalar
    integer, dimension(nelem), intent(in) :: y
!  local variable
    real(kind=dp) :: z

    z = sum(b*y*x)

  end function foo
end module mod

我用

编译
f2py -c -m moddata moddata.f90

我收到了这些错误:

  y_Dims[0]=(nelem);
             ^
1 warning and 1 error generated.reduce to a constant expression

如果我在integer, parameter :: nelem=3之前重新定义integer, dimension(nelem), intent(in) :: y并重新编译,我会

      real(kind=dp) foof2pywrap
                1
Error: Parameter 'dp' at (1) has not been declared or is a variable, which does not reduce to a constant expression

每个real(kind=dp)声明都有相同的错误,

      foof2pywrap = foo(x, y)
                    1
Warning: Possible change of value in conversion from REAL(8) to REAL(4) at (1)

所以我必须在函数中通过dp重新定义integer, parameter :: dp = selected_real_kind(15)。然后就行了。

当我使用fortran包装器编译此模块时,我没有收到这些错误。我想知道为什么函数中的nelemdp没有用f2py正确确定范围?

1 个答案:

答案 0 :(得分:4)

也许我错了,但我认为f2py无法处理Fortran 90的module + contains功能。如果您将代码转换为

function foo(x,y) result(z)
   integer, parameter :: dp = selected_real_kind(15)
   real(kind=dp), intent(in) :: x
   integer, parameter :: nelem = 3
   integer, dimension(3), parameter :  = (/3, 1, 2/)
   integer, dimension(nelem), intent(in) :: y
   real(kind=dp) :: z

   z = sum(b*y*x)
end function

并像以前一样编译它,它可以工作:

 >>> x = 1.0000000000
 >>> y = [2, 3, 4]
 >>> moddata.foo(x,y)
 17.0

修改

this question on SO的答案说f2py 不了解如何将Fortran函数转换为python函数。所以我将function foo更改为subroutine foo2,然后编译为f2py moddata.f90 -m moddata并获得输出

Reading fortran codes...
        Reading file 'moddata.f90' (format:free)
Post-processing...
        Block: moddata
                        Block: moddata
In: :moddata:moddata.f90:moddata
get_parameters: got "invalid syntax (<string>, line 1)" on '(/3, 1, 2/)'
                                Block: foo2
Post-processing (stage 2)...
        Block: moddata
                Block: unknown_interface
                    Block: moddata
                            Block: foo2
Building modules...
        Building module "moddata"...
                Constructing F90 module support for "moddata"...
                    Variables: nelem b dp
                    Constructing wrapper function "moddata.foo2"...
getctype: "real(kind=dp)" is mapped to C "float" (to override define dict(real = dict(dp="<C typespec>")) in /home/jdwood/Documents/Physics/Fortran/tests/f2py/.f2py_f2cmap file).
getctype: "real(kind=dp)" is mapped to C "float" (to override define dict(real = dict(dp="<C typespec>")) in /home/jdwood/Documents/Physics/Fortran/tests/f2py/.f2py_f2cmap file).
getctype: "real(kind=dp)" is mapped to C "float" (to override define dict(real = dict(dp="<C typespec>")) in /home/jdwood/Documents/Physics/Fortran/tests/f2py/.f2py_f2cmap file).
getctype: "real(kind=dp)" is mapped to C "float" (to override define dict(real = dict(dp="<C typespec>")) in /home/jdwood/Documents/Physics/Fortran/tests/f2py/.f2py_f2cmap file).
getctype: "real(kind=dp)" is mapped to C "float" (to override define dict(real = dict(dp="<C typespec>")) in /home/jdwood/Documents/Physics/Fortran/tests/f2py/.f2py_f2cmap file).
getctype: "real(kind=dp)" is mapped to C "float" (to override define dict(real = dict(dp="<C typespec>")) in /home/jdwood/Documents/Physics/Fortran/tests/f2py/.f2py_f2cmap file).
                          z = foo2(x,y)
Wrote C/API module "moddata" to file "./moddatamodule.c"
Fortran 90 wrappers are saved to "./moddata-f2pywrappers2.f90"

所以它确实看起来像双精度丢失,所以按照建议编辑一个名为.f2py_f2cmap的文件,我这样做了,并且在dp时没有出现任何错误。但是,它仍然会给nelem一个错误,所以我可以想到两个解决方案:

  1. 坚持使用3代替nelem
  2. nelemb作为变量传递给子程序
  3. 我还发现,在使用parameter(b = (/3.d0, 1.d0, 2.d0/) )行时,我收到的警告是

    analyzeline: Failed to evaluate '/3.e0+1j*( 1.e0+1j*( 2.e0/)'. Ignoring: invalid syntax (<string>, line 1)
    

    我不知道该怎么做。但是,当我使用x=1.0y=(/ 3, 6, 2/)(使用python并在Fortran程序中使用模块)时,我确实得到18作为答案。

    简而言之,当您使用f2py时,完全避免使用