f2py从Fortran modu恢复变量

2024-05-03 07:12:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我的最终目标是把我编写的许多不同的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),这是我悲伤的根源。在

^{pr2}$

我直接通过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声明,使每个变量都定义在自己的行上,那么我将得到所有三个变量的属性error。我在这里很难受,有什么主意吗?在


Tags: in脚本ifoutprintgeometryf2pyyn
1条回答
网友
1楼 · 发布于 2024-05-03 07:12:57

好吧,这并不能解释为什么发布的不起作用,但它确实提供了一个临时的解决方案。如果我省略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等)的值时,我失败了。显然,这些变量声明的某些东西会使工作变得混乱,我不知道为什么。在

相关问题 更多 >