根據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
爲什麼不寫fortran函數python可以調用來訪問這些變量。 – innoSPG
由於這些庫很早就開發出來了,我想避免嘗試翻譯經過驗證的基礎架構。訪問通用塊中的變量將允許我在Python中使用它們(需要更新的部分),並且在Fortran上保留數百個不需要更新的函數。 – Jfreixa
編寫一個包裝並不意味着更新任何東西!您只需編寫一個Fortran setter和getter,即可完成全新的過程。完全沒有改變!您的數百個功能可以保持不變。 –