Python Forum
f2py recovering variables from Fortran module - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: f2py recovering variables from Fortran module (/thread-1088.html)



f2py recovering variables from Fortran module - marius - Dec-02-2016

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, the results are mixed.  Depending on how I order the f2py declaration in the addwake subroutine, I can only extract one or none of the variables (as shown below for the files presented above).

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