简体   繁体   中英

f2py recovering variables from Fortran module

My endgame is to have take a number of different Fortran scripts that I've written and interface them via Python. The scripts themselves are relatively straightforward: essentially, just a lot of math without any programming structures more complicated than arrays. However, I'm pretty new to this, so I have a small test script that I'm trying it out on.

The main script is below (in abbreviated form):

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

This utilizes a module (geometry.f95) that is the source of my grief.

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

I compile the code directly via f2py -c -m wake geometry.f95 addwake.f95 and then go into the Python interpreter to run it. It runs fine, giving me the expected output (a text file with ordered lists of numbers), but then I try to extract variable values, which is what I'll need to be able to do in my integration framework. When I try that, I get the following results:

>>> 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

If I change the f2py declaration in geometry.f95 such that each variable is defined on its own line, I get the attribute error for all three variables. I'm pretty stumped here, so any ideas?

Alright, this doesn't give any sort of explanation for why the posted doesn't work, but it does provide a temporary solution. If I just omit the f2py declarations for the allocatable variables in geometry.f95 (the module), everything is hunky dory. In other words:

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

...enables me to use the interpreter to pull values for all three matrices. In trying to pull values for the other variables (jmax, kmax, etc.), I am, however, unsuccessful. Something about these variable declarations muddles the works, apparently, and I'm not sure why.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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