Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
991 views
in Technique[技术] by (71.8m points)

fortran - Error: Rank mismatch in array reference at (1) (2/1)

I kindly request your help on this code where I kept on getting an error: Rank mismatch in array reference at (1) (2/1). My objective is to go through each point in a cube(p = i+(j-1)*N + (k-1)NN) and calculate the gradient of the potential along each axis (gradphi_x, gradphi_y, gradphi_z).

PROGRAM sub_rho_phi
    integer, parameter:: N=3
    real, dimension(N):: gradphi_x, gradphi_y, gradphi_z
    call output(gradphi_x, gradphi_y, gradphi_z)
    open(unit=1,file="grad_phi.dat")

    l = 0
    do
        l=l+1
        write(1,*) gradphi_x(l),gradphi_y(l),gradphi_z(l)
        if ( l == N**3) then
            exit
        end if
    end do
END


SUBROUTINE output( gradphi_x, gradphi_y, gradphi_z )
    real, parameter:: h=0.7,G=6.67,M=1.98892*(10**3)!!in(10**15) kg

    integer, parameter:: N=3
    real, dimension(N):: r, gradphi_x,gradphi_y,gradphi_z
    integer, dimension(N**3):: x,y,z
    integer:: p
    real:: a
    a=500/h !in kpc

    do i=0, N
        do j=0, N
            do k=0, N
                p = i+(j-1)*N + (k-1)*N*N
                x(p,1)=i
                y(p,2)=j
                z(p,3)=k
                r(p)=sqrt(x(p,1)*x(p,1)+y(p,2)*y(p,2)+z(p,3)*z(p,3))
                gradphi_x(p)=(G*M)*x(p,1)/r(p)*(r(p)+a)**2
                gradphi_y(p)=(G*M)*y(p,2)/r(p)*(r(p)+a)**2
                gradphi_z(p)=(G*M)*z(p,3)/r(p)*(r(p)+a)**2
            enddo
        enddo
    enddo

    return
END
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have declared x, y and z as one dimensional arrays, but are using two dimensional indexing all the way through output.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...