我用Python编写了一个小的Fortran函数,并使用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编译命令:
^{pr2}$
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的值是完全关闭的,所以在传输过程中肯定发生了一些事情,使得参数变得混乱。在
我做错什么了?在