我的问题是,在使用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包装器编译此模块时,我没有收到这些错误。我想知道为什么函数中的nelem
和dp
没有用f2py正确确定范围?
答案 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
一个错误,所以我可以想到两个解决方案:
3
代替nelem
nelem
和b
作为变量传递给子程序我还发现,在使用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.0
和y=(/ 3, 6, 2/)
(使用python并在Fortran程序中使用模块)时,我确实得到18作为答案。
简而言之,当您使用f2py时,完全避免使用。