我的最终目标是获取我编写的许多不同的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声明,我得到了所有三个变量属性错误。我很难过,所以有什么想法?