繁体   English   中英

f2py从Fortran模块中恢复变量

[英]f2py recovering variables from Fortran module

我的最终选择是接受许多我编写的不同的Fortran脚本,并通过Python进行接口。 脚本本身是相对简单的:本质上,只是大量的数学运算,而没有比数组更复杂的编程结构。 但是,我对此并不陌生,因此我正在尝试使用一个小的测试脚本。

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

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声明,以使每个变量都在其自己的行上定义,那么我将得到所有三个变量的属性错误。 我在这里很困惑,所以有什么想法吗?

好的,这并不能解释为何发布的内容不起作用,但确实提供了一个临时解决方案。 如果我只是忽略了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等)的值时,我没有成功。 关于这些变量声明的某些事情显然使工作混乱了,我不确定为什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM