在python中调用fortran代码时如何处理全局变量(例如使用f2py)?(How to deal with global variables when calling fortran code

编程入门 行业动态 更新时间:2024-10-26 08:28:53
在python中调用fortran代码时如何处理全局变量(例如使用f2py)?(How to deal with global variables when calling fortran code in python (e.g. with f2py)?)

我想用python运行一些fortran代码,并使用f2py -c -m 。 但是,似乎只有FUNCTION被打包到.so文件中,而不是打包到PROGRAM中 。 那我怎么处理全局变量呢? 例如,变量c放在模块中

MODULE nfw double precision :: c END MODULE nfw

,在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

我如何调用让函数y(x)知道python中c的值?

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?

最满意答案

在你的f2py模块下应该是另一个模块,Fortran一个名为nfw。 应该在那里。

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

确保使用带有f2py的模块编译源文件。

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.

更多推荐

本文发布于:2023-07-21 09:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1209007.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何处理   全局变量   代码   python   fortran

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!