从ctypes访问Common Block变量

时间:2016-09-16 14:00:31

标签: python fortran ctypes

我正在尝试从Python脚本访问存储在Fortran 77公共块中的数据。问题是我不知道这些数据的存储位置。 我正在开发的Python应用程序使用不同的库。这些库包含具有以下指令的函数:

#include <tcsisc_common.inc>

公共区块包含:

C
      INTEGER*4 IDEBUG
C
C.... ARRAY DIMENSIONS
      DIMENSION IDEBUG(10)
C
C.... COMMON BLOCK
      COMMON /TCSD/ IDEBUG
C

在Python部分(在我使用过iPython的例子中),我加载了库:

In [1]: import ctypes
In [2]: _libtcsisc= /home/jfreixa/project/bin/libtcsisc.so
In [3]: _tcsisc   = ctypes.CDLL(_libtcsisc, ctypes.RTLD_GLOBAL)

问题在于我不知道如何获得IDEBUG。我尝试了以下内容,但我只是将tcsd作为初始化为0的c_long。

In [4]: tcsd = ctypes.c_int.in_dll(_tcsisc, "TCSD_")
In [5]: tcsd
Out[5]: c_long(0)
In [6]: idebug = ctypes.c_int.in_dll(_tcsisc, "IDEBUG_")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-ee5018286275> in <module>()

----> 1 idebug = ctypes.c_int.in_dll(_tcsisc,'IDEBUG_')

ValueError: ld.so.1: python2.7: fatal: IDEBUG_: can't find symbol

有没有想法正确获取变量?

1 个答案:

答案 0 :(得分:3)

根据this page(特别是如何从C访问Fortran公共块)和一些Q/A page关于如何从Python访问C结构,似乎我们可以按如下方式访问公共块(尽管如此可能不是很便携,见下文):

mylib.f90

subroutine fortsub()
    implicit none
    integer n
    common /mycom/ n
    print *, "fortsub> current /mycom/ n = ", n
end

编译:

$ gfortran -shared -fPIC -o mylib.so mylib.f90

test.py

from __future__ import print_function
import ctypes

class Mycom( ctypes.Structure ):
    _fields_ = [ ( "n", ctypes.c_int ) ]

mylib = ctypes.CDLL( "./mylib.so" )

mycom = Mycom.in_dll( mylib, "mycom_" )

print( " python> modifying /mycom/ n to 777" )

mycom.n = 777

fortsub = mylib.fortsub_
fortsub()

测试:

 $ python test.py 
 python> modifying /mycom/ n to 777
 fortsub> current /mycom/ n =          777

请注意,公共块的名称(此处为mycom)为小写,并附加一个下划线(假设为gfortran)。因为这个约定是依赖于编译器的,所以编写新的Fortran例程来设置/获取公共块中的值(特别是在iso_c_binding的帮助下)并从Python调用这些例程可能更健壮/可移植(如建议的那样) @innoSPG在第一条评论中。)

包含不同类型和数组的另一个示例可能如下所示:

mylib.f90

subroutine initcom()
    implicit none
    integer          n( 2 ), w  !! assumed to be compatible with c_int
    real             f( 2 )     !!                      ... with c_float
    double precision d( 2 )     !!                      ... with c_double
    common /mycom/ n, f, d, w

    print *, "(fort) initializing /mycom/"
    n(:) = [ 1, 2 ]
    f(:) = [ 3.0, 4.0 ]
    d(:) = [ 5.0d0, 6.0d0 ]
    w    = 7
    call printcom()
end

subroutine printcom()
    implicit none
    integer          n( 2 ), w
    real             f( 2 )
    double precision d( 2 )
    common /mycom/ n, f, d, w

    print *, "(fort) current /mycom/"
    print *, "       n = ", n
    print *, "       f = ", f
    print *, "       d = ", d
    print *, "       w = ", w
end

test.py

from __future__ import print_function
import ctypes

N = 2

class Mycom( ctypes.Structure ):
    _fields_ = [ ( "x", ctypes.c_int    * N ),
                 ( "y", ctypes.c_float  * N ),
                 ( "z", ctypes.c_double * N ),
                 ( "w", ctypes.c_int        ) ]

mylib = ctypes.CDLL( "./mylib.so" )

mycom = Mycom.in_dll( mylib, "mycom_" )

initcom = mylib.initcom_
initcom()

print( " (python) current /mycom/" )
print( "          x = ", mycom.x[:] )
print( "          y = ", mycom.y[:] )
print( "          z = ", mycom.z[:] )
print( "          w = ", mycom.w    )

print( " (python) modifying /mycom/ ..." )
for i in range( N ):
    mycom.x[ i ] = (i + 1) * 10
    mycom.y[ i ] = (i + 1) * 100
    mycom.z[ i ] = (i + 1) * 0.1
mycom.w = 777

printcom = mylib.printcom_
printcom()

测试:

 $ python test.py

 (fort) initializing /mycom/
 (fort) current /mycom/
        n =            1           2
        f =    3.0000000       4.0000000    
        d =    5.0000000000000000        6.0000000000000000     
        w =            7
 (python) current /mycom/
          x =  [1, 2]
          y =  [3.0, 4.0]
          z =  [5.0, 6.0]
          w =  7
 (python) modifying /mycom/ ...
 (fort) current /mycom/
        n =           10          20
        f =    100.00000       200.00000    
        d =   0.10000000000000001       0.20000000000000001     
        w =          777