2016-12-01 183 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等)的值時,我卻不成功。顯然,關於這些變量聲明的一些東西混淆了作品,我不知道爲什麼。