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

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

MODULE nfw double precision :: c END MODULE nfw 

changes in PROGRAM and is used FUNCTION implicitly in the same file

 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 the y (x) function to know about the value of c in python?

+4
source share
1 answer

There should be another module under your f2py module, Fortran named nfw. He must be there.

 $ f2py -c -m mod nfw.f90 $ python import mod mod.nfw.c array(0.0) 

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

+6
source

Source: https://habr.com/ru/post/1442851/


All Articles