简体   繁体   English

在python中调用fortran代码时如何处理全局变量(例如,使用f2py)?

[英]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. 我想用python运行一些fortran代码,并为此使用f2py -c -m However, it seems that only the FUNCTIONs are packed into the .so file, but not the PROGRAM . 但是,似乎只有.FUNCTIONs被打包到.so文件中,而没有打包到PROGRAM中 How can I deal with global variables then? 那我该如何处理全局变量呢? For example, a variable c is put in a module 例如,将变量c放入模块中

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中进行了修改,并且由FUNCTION隐式使用在同一文件中

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? 如何调用y(x)函数了解python中c的值?

Under your f2py module should be another module, the Fortran one named nfw. 在f2py模块下应该是另一个模块,一个名为nfw的Fortran。 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. 确保使用带有f2py的模块编译源文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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