2016-12-01 132 views
0

我的最终目标是获取我编写的许多不同的Fortran脚本,并通过Python将它们连接起来。脚本本身相对简单:基本上,只是很多数学,没有任何编程结构比数组更复杂。不过,我很新,所以我有一个小测试脚本,我正在尝试。f2py从Fortran模块中恢复变量

主要脚本下面是(以缩写形式):

subroutine addwake 
use geometry 
implicit none 

    integer::i,j,k,m 
    real(kind=8),allocatable::xn2(:,:),yn2(:,:),zn2(:,:) 
    real(kind=8)::avg,m1,m2,m0 
    character(50)::namefile 

!f2py intent(out) i,j,k,m 
!f2py intent(out),allocatable xn2,yn2,zn2 
!f2py intent(out) avg,m1,m2,m0 
!f2py intout(out) namefile 

! Check if surface node arrays are allocated 
if(allocated(xn))then 
    deallocate(xn,yn,zn) 
end if 

! Read in sectional point distribution 

... 

end subroutine addwake 

这利用一个模块(geometry.f95)这是我的悲伤的来源。

module geometry 
    implicit none 

    ! panel coordinates and connectivity 
    integer::jmax,kmax 
    integer::npts_wake 
    real(kind=8)::dspan 
    real(kind=8),allocatable::xn(:,:),yn(:,:),zn(:,:) 
    !f2py intent(out) jmax,kmax,npts_wake 
    !f2py intent(out) dspan 
    !f2py intent(out),allocatable xn,yn,zn 
end module geometry 

我直接通过f2py -c -m wake geometry.f95 addwake.f95编译代码,然后进入Python解释器运行它。它运行良好,给我预期的输出(一个带有序列表的文本文件),但是我尝试提取变量值,这是我需要在我的集成框架中能够做到的。当我尝试,我得到如下结果:

>>> print wake.geometry.xn 
[[ 2.01331785 2.01331785 2.01331785 2.01331785 2.01331785] 
[ 2.00308232 2.00308232 2.00308232 2.00308232 2.00308232] 
[ 1.99284679 1.99284679 1.99284679 1.99284679 1.99284679] 
..., 
[ 0.979798 0.979798 0.979798 0.979798 0.979798 ] 
[ 0.989899 0.989899 0.989899 0.989899 0.989899 ] 
[ 1.   1.   1.   1.   1.  ]] 
>>> print wake.geometry.yn 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: yn 
>>> print wake.geometry.zn 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: zn 

如果我更改了geometry.f95使得每个变量在其自己的行中定义的f2py声明,我得到了所有三个变量属性错误。我很难过,所以有什么想法?

回答

0

好的,这并没有给出任何解释为什么张贴不起作用,但它确实提供了一个临时解决方案。如果我只是省略geometry.f95(模块)中的可分配变量的f2py声明,那么一切都很顺利。换句话说:

module geometry 
    implicit none 

    ! panel coordinates and connectivity 
    integer::jmax,kmax 
    integer::npts_wake 
    real(kind=8)::dspan 
    real(kind=8),allocatable::xn(:,:),yn(:,:),zn(:,:) 
!f2py intent(out) jmax,kmax,npts_wake 
!f2py intent(out) dspan 

end module geometry 

...使我能够使用解释器来拉取所有三个矩阵的值。在试图拉取其他变量(jmax,kmax等)的值时,我却不成功。显然,关于这些变量声明的一些东西混淆了作品,我不知道为什么。