我寫了一個小的Fortran函數,並在Python中使用f2py將參數傳遞給它。不知何故,在傳輸過程中,參數的順序是混亂的,我不知道爲什麼。f2py - 函數參數的順序亂了
的Fortran函數(其是在名爲calc_density.f95文件)的相關部分:
subroutine calc_density(position, nparticles, ncells, L, density)
implicit none
integer, intent(in) :: nparticles
integer, intent(in) :: ncells
double precision, intent(in) :: L
double precision, dimension(nparticles), intent(in) :: position
double precision, dimension(ncells), intent(out) :: density
double precision :: sumBuf, offSum
integer :: pLower, pUpper, pBuf, numBuf, last, idx
double precision, dimension(nparticles) :: sorted
print *, 'Fortran ', 'position length ', size(position), &
'density length ', size(density), 'nparticles ', nparticles, &
'ncells ', ncells, 'L ', L
end subroutine calc_density
f2py編譯命令:
f2py -c --fcompiler=gnu95 -m fortran_calc_density calc_density.f95
的Python代碼的相關部分:
from fortran_calc_density import calc_density as densityCalc
from numpy import array, float64
def calc_density(position, ncells, L):
arg = array(position, dtype = float64, order = 'F')
nparticles = len(position)
density = densityCalc(position, nparticles, ncells, L)
print 'Python ', 'position length ', len(position), 'density length', len(density), 'nparticles ', nparticles, 'ncells ', ncells, 'L ', L
return density
屏幕輸出顯示所有傳輸變量不匹配的示例:
Fortran position length 12 density length 100 nparticles 12 ncells 100 L 20.000000000000000
Python position length 100 density length 100 nparticles 100 ncells 20 L 12.5663706144
從Python中的打印輸出顯示的值,除了密度陣列這應該是等於NCELLS的長度,因此20由Fortran函數的設計中,正是因爲他們應該。然而,Fortran值完全關閉,所以在傳輸過程中必然發生了一些事情,這些爭議攪亂了爭論。
我在這裏做錯了什麼?
爲了避免混淆:即使在Python打印語句之前調用了densityCalc,您是否可以仔細檢查Fortran行在Python行之後是否打印*? – Evert
對不起,這是誤導。實際上首先打印Fortran打印。 Python和Fortran函數實際上是在一個循環中調用的,因此可以連續打印到屏幕上。我只是挑選了兩個後續的輸出來顯示這個問題。結果在循環之間不會改變。現在已更正帖子,因此它顯示正確的打印順序。 – Marcel