Python Forum
f2py recovering variables from Fortran module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
f2py recovering variables from Fortran module
#1
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  importing variables from module 8376459 1 245 Feb-18-2024, 02:24 PM
Last Post: deanhystad
  Trying to debug segfault in ctypes binding to Fortran bthomas 1 568 Sep-01-2023, 11:26 AM
Last Post: bthomas
  module to store functions/variables and how to call them? mstichler 3 2,338 Jun-03-2020, 06:49 PM
Last Post: mstichler
  f2py: no Fortran compiler found FMJS 2 5,332 May-25-2020, 02:51 PM
Last Post: FMJS
  How to access variables from dirsync module steve_shambles 3 3,783 Apr-02-2020, 08:18 AM
Last Post: steve_shambles
  Fortran vs C mistert 2 2,321 Mar-07-2019, 12:55 PM
Last Post: mistert
  help with threading module and passing global variables ricardons 1 7,758 Feb-21-2019, 12:48 PM
Last Post: stullis
  F2PY.......plz!!!! Yuji3131 14 15,627 Oct-07-2016, 04:01 AM
Last Post: Mekire
  Question about F2py...Help me plz!!! Yuji3131 3 8,928 Oct-05-2016, 09:59 AM
Last Post: Yuji3131

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020