Fortran子例程不返回python

2024-04-19 23:51:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python代码中使用fortran子例程,子例程运行,但似乎没有向python返回任何东西。你知道吗

这是我的子程序

subroutine gameo(N, matrix, newmat)

integer :: N 
integer :: matrix(10,10), maxit, newmat(10,10), naap


do i=1,N
do j=1,N

    naap = 0 !count neighbours
    do k=i-1,i+1
        do l=j-1,j+1
            if (k==i .AND. l==j) then
                continue
            else if (k>=N .OR. k<0) then
                continue
            else if (l>=N .OR. l<0) then
                continue
            else if (matrix(k,l) == 1) then
                naap = naap+1
            end if 
        end do
    end do
    if (matrix(i,j) ==1) then !update new array
        if (naap>3) then
            newmat(i,j) = 0
        else if (naap < 2) then
            newmat(i,j) = 0
        else 
            newmat(i,j)= 1
        end if
    else if (matrix(i,j) == 0) then
        if (naap==3) then
            newmat(i,j) = 1
        else 
            newmat(i,j) = 0
        end if
    end if          
end do
end do

end subroutine gameo

这是我的python代码

N = 10
matrix = np.zeros((N,N))

for i in range(N):
    for j in range(N):
        matrix[i,j]=np.random.randint(0,2)

it = 0
maxit = 10

while it<maxit:
    newmat = np.zeros((N,N))

    newmat = gameo.gameo(N,matrix,newmat) #calling Fortran subroutine

    matrix = newmat
    it += 1  

我在fortran数组上也有问题,我不能写“matrix(N,N)”,但必须将其赋值为“matrix(10,10)”。
我对python和fortran比较陌生,我只工作了几个月。你知道吗


Tags: ifnpitdoelsematrixendfortran