gpt4 book ai didi

python - 在许多扩展模块之间使用 F2PY 共享 Fortran 90 模块数据

转载 作者:太空宇宙 更新时间:2023-11-03 15:40:11 26 4
gpt4 key购买 nike

我想在许多自编译的 F2PY 扩展模块之间共享位于 Fortran 90 模块中的数据。 F2PY 的文档说这是不可能的,因为 Python 通常如何导入共享库。

F2PY generates wrappers to common blocks defined in a routinesignature block. Common blocks are visible by all Fortran codes linkedwith the current extension module, but not to other extension modules(this restriction is due to how Python imports shared libraries).

[...]

The F2PY interface to Fortran 90 module data is similar to Fortran 77common blocks.

Link to Documentation

由于我必须使用大约 100 个嵌套的 Fortran 90 子例程,我需要在它们之间共享数据。有什么建议可以实现吗?

我考虑过将每个变量作为参数传递给每个子例程,然后再返回变量,但这听起来有点不对。

最佳答案

虽然只是一种试错法,但将变量模块和所有子程序放入一个文件中并使用 f2py (*1) 编译它如何?例如……

我的测试.f90:

include "vars.f90"
include "sub1.f90"
include "sub2.f90"

变量.f90:

module vars
integer :: n = 100
end

sub1.f90:

subroutine sub1
use vars, only: n
implicit none
print *, "sub1: n = ", n
end

sub2.f90:

subroutine sub2
use vars, only: n
implicit none
print *, "sub2: n = ", n
print *, "adding 1 to n"
n = n + 1
print *, "n = ", n
end

编译:

f2py -c -m mytest mytest.f90

测试:

$ /usr/local/bin/python3
>>> import mytest
>>> mytest.vars.n
array(100, dtype=int32)
>>> mytest.sub1()
sub1: n = 100
>>> mytest.sub2()
sub2: n = 100
adding 1 to n
n = 101
>>> mytest.sub2()
sub2: n = 101
adding 1 to n
n = 102
>>> mytest.vars.n = 777
>>> mytest.sub2()
sub2: n = 777
adding 1 to n
n = 778

(*1) 在上述情况下,简单地将所有文件名提供给 f2py 似乎就足够了,例如,

$ f2py -c -m mytest vars.f90 sub1.f90 sub2.f90

关于python - 在许多扩展模块之间使用 F2PY 共享 Fortran 90 模块数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52950218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com