f2py:从“已使用"界面公开参数.模组 [英] f2py: Exposing parameters from "used" modules

查看:126
本文介绍了f2py:从“已使用"界面公开参数.模组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这个问题已经在某个地方解决了,但是我花了很多时间寻找答案,包括稍微深入了一下源代码.我试图将问题放在第一段.其余部分显示了该问题的基本示例.

I assume that this question has been addressed somewhere, but I have spent an inordinate amount of time looking around for the answer including digging into the source code a bit. I have tried to put the problem in the first paragraph. The rest shows a basic example of the problem.

我正在尝试编译一个包含USE语句的模块,该语句指向另一个更通用的模块.我希望将使用的模块分开放置,以便可以将其作为一组常规设置用于多个软件包"中.当我使用f2py编译这两个模块时,一切都按从fortran方面发布的方式工作,但是从python方面来看,USE似乎被忽略了.如果我允许f2py生成签名文件,则该文件将包含适当的USE语句,但是如果我完成了编译并从结果库中导入,则包含模块的参数中所用模块的参数将不可用.陈述.以下是说明情况的两个模块:

I am attempting to compile a module that contains a USE statement pointing to another, more general, module. I would prefer to keep the used module separate so that it can be used in several "packages" as a set of general settings. When I compile the two modules using f2py everything works as advertised from the fortran side, but from the python side USE appears to be ignored. If I allow f2py to generate a signature file, the file contains a USE statement as is appropriate, but if I complete the compilation and import from the resulting library the parameters from the used module are not available in the module that contains the use statement. Below are two modules illustrating the situation:

MODULE test
    INTEGER, PARAMETER :: a = 1
END MODULE test

MODULE test2
    USE test
    INTEGER, PARAMETER :: b = 2
END MODULE test2

为了显示中间步骤,我运行了f2py -h test.pyf test.f90 test2.f90.生成以下签名文件;请注意,"test2"模块包含使用测试":

In order to show the intermediate step I ran f2py -h test.pyf test.f90 test2.f90. The following signature file is generated; note that the "test2" module contains "use test":

!    -*- f90 -*-
! Note: the context of this file is case sensitive.

python module test ! in
    interface  ! in :test
        module test ! in :test:test.f90
            integer, parameter,optional :: a=1
        end module test
        module test2 ! in :test:test2.f90
            use test
            integer, parameter,optional :: b=2
        end module test2
    end interface
end python module test

! This file was auto-generated with f2py (version:2).
! See http://cens.ioc.ee/projects/f2py2e/

如果现在使用f2py --fcompiler=gfortran -c test.pyf test.f90 test2.f90进行编译,则会获得test.so(与运行f2py --fcompiler=gfortran -m test -c test.f90 test2.f90相同,而无需先创建签名文件).在python中从该库中导入会公开test.test.a和test.test2.b,但不会公开test.test2.a,如下所示:

If I now compile with f2py --fcompiler=gfortran -c test.pyf test.f90 test2.f90 I obtain test.so (same as running f2py --fcompiler=gfortran -m test -c test.f90 test2.f90 without creating the signature file first). Importing from this library in python exposes test.test.a and test.test2.b, but does not expose test.test2.a as can be seen here:

In [1]: import test

In [2]: print test.test.a
1

In [3]: print test.test2.b
2

In [4]: print test.test2.a
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/users/solbrig/svn_checkouts/inversion/satmet/branches/solbrig/rootpath/data/users
/GeoIPS/src/test/<ipython-input-4-bffcf464e408> in <module>() 
----> 1 print test.test2.a

AttributeError: a

只是为了说明b是从fortran的角度在test2中正确定义的,下面的代码使用test2并打印bb:

Just to illustrate that b is defined properly in test2 from the perspective of fortran, the following code uses test2 and prints both b and b:

SUBROUTINE run_test()
    USE test2
    IMPLICIT NONE
    print *, "a = ", a
    print *, "b = ", b
END SUBROUTINE run_test

在使用"f2py -m run_test -c test.f90 test2.f90 run_test.f90"进行编译并获得run_test.so之后,可以将run_test导入python并按预期工作:

After compiling with "f2py -m run_test -c test.f90 test2.f90 run_test.f90" and obtaining run_test.so, run_test can be imported in python and works as expected:

In [1]: import run_test

In [2]: run_test.run_test()
 a =            1
 b =            2

在此问题上的任何帮助将不胜感激.

Any help with this issue would be greatly appreciated.

推荐答案

我找到了解决此问题的临时方法,但这不是最佳方法.我将继续研究f2py源代码,以便我可以更好地理解它并在代码本身中解决问题.在此之前,这是我的解决方案,灵感来自 chatcannon对我发布到nympy的github上的问题的评论.

I have found a temporary solution to this problem, but it is not optimal. I will continue to work through the f2py source so that I can better understand it and fix the problem within the code itself. Until then, this is my solution which was inspired by chatcannon's comment to the issue I posted to nympy's github.

从临时的角度来看,有几种方法可以解决此问题,包括几种修改.pyf文件的方法.我不想修改.pyf文件,因为它作为大软件包的一部分变得非常麻烦.为了避免这种情况,我在f90源代码中添加了f2py指令.

There are several ways to approach this problem from a temporary standpoint including a couple of ways of modifying the .pyf files. I don't want to have to modify the .pyf files as it becomes very cumbersome as part of a larger package. To avoid this, I added f2py directives to my f90 source.

以我原来的问题为例:

MODULE test
    INTEGER, PARAMETER :: a = 1
END MODULE test

MODULE test2
    USE test
    INTEGER, PARAMETER :: b = 2
END MODULE test2

只需在test2中添加一个f2py指令以显示f2py如何定义test2.a:

simply add an f2py directive in test2 to show f2py how to define test2.a:

MODULE test
    INTEGER, PARAMETER :: a = 1
END MODULE test

MODULE test2
    USE test
    !f2py integer, parameter :: a  ! THIS EXPOSES `a` in `test2`
    INTEGER, PARAMETER :: b = 2
END MODULE test2

从生成的test.so导入正确显示test2.a:

Importing from the resulting test.so correctly exposes test2.a:

In [1]: import test

In [2]: print test.test.a
1

In [3]: print test.test.b
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
.../test_solution/<ipython-input-3-798b14f59815> in <module>()
----> 1 print test.test.b

AttributeError: b

In [4]: print test.test2.a
1

In [5]: print test.test2.b
2

这篇关于f2py:从“已使用"界面公开参数.模组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆