【发布时间】:2016-02-20 20:58:19
【问题描述】:
我编写了一个小的 Fortran 函数,并使用 f2py 在 Python 中将参数传递给它。不知何故,参数的顺序在传输过程中被弄乱了,我不知道为什么。
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,因此根据 Fortran 函数的设计,它应该等于 20。 然而,Fortran 值完全关闭,因此在传输过程中一定发生了一些事情,从而扰乱了周围的参数。
我在这里做错了什么?
【问题讨论】:
-
为避免任何混淆:您能否仔细检查一下 Fortran 行是否在 Python 行 之后打印,即使密度计算在 Python 打印 之前 被调用声明?
-
对不起,那是误导。实际上,首先打印的是 Fortran 打印。 Python 和 Fortran 函数实际上是在循环中调用的,因此会连续打印到屏幕上。我刚刚选择了该输出的两个后续打印来显示该问题。结果在循环之间不会改变。现在已经更正了帖子,所以它显示了正确的打印顺序。