简体   繁体   中英

How to deal with global variables when calling fortran code in python (e.g. with f2py)?

I want to run some fortran codes with python and am using f2py -c -m for it. However, it seems that only the FUNCTIONs are packed into the .so file, but not the PROGRAM . How can I deal with global variables then? For example, a variable c is put in a module

MODULE nfw
  double precision :: c
END MODULE nfw

,is modified in the PROGRAM, and is used by a FUNCTION in the same file implicitly

PROGRAM Compute_Profile
  USE nfw
  c = 5.0
END PROGRAM Compute_Profile

DOUBLE PRECISION FUNCTION y(x)
  USE nfw
  double precision :: x
  y = c * x
  return
END FUNCTION y

How can I call let the function y(x) know about the value of c in python?

Under your f2py module should be another module, the Fortran one named nfw. It should be there.

$ f2py -c -m mod nfw.f90

$ python
import mod
mod.nfw.c

array(0.0)

Make sure you compile the source file with the module with f2py.

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