Fortran 子程序返回错误值 [英] Fortran subroutine returning wrong values
问题描述
嘿,我正在开发一个 Fortran 程序,但遇到了一个奇怪的问题.当我尝试在调用特定子例程之前直接输出数组的某些值时,我得到了正确的值.然后我尝试在我启动子程序时输出相同数组的一些值,它们是 0.我最终在子程序之后输出数组的值,这些值回到预期值.谁能帮我理解为什么?我的代码如下:
Hey I'm working on a fortran program and have ran across an odd problem. When I try to output some values of an array directly before calling a specific subroutine I get the correct values. I then try to output some values of the same array right as I start the subroutine, and they are 0. I finally output the values of the array after the subroutine and the values are back to the expected values. Could anyone help me understand why? My code is below:
首先,在主函数中调用子程序,我想在write语句中输出值:
First, the calling of the subroutine in the main function, with values I want outputted in the write statements:
if (iter .eq. 5) then
write(*,*) 'vp vals: ',vp(0,23471), vp(0,23475)
end if
CALL GRID_DIVISION( &
& NPTMAX, DIM, TYPEMAX, NEIGHMAX, NPTC, GRIDLIMIT, &
& GRIDN, GRIDNUM, GRID, GNEIGH, XP, PTTYPE, TYPE, &
& GRIDP, TEMP_GRIDP, GNEIGHMAX, PAINT, VP, ITER, gridvel &
& )
if (iter .eq. 5) then
write(*,*) 'vp vals: ',vp(0,23471), vp(0,23475)
end if
这是调用下面的子程序,我只贴出相关部分:
This is calling the following subroutine, of which I will post only the relevant part:
SUBROUTINE GRID_DIVISION( &
& NPTMAX, DIM, TYPEMAX, NEIGHMAX, NPTC, GRIDLIMIT, &
& GRIDN, GRIDNUM, GRID, GNEIGH, XP, PTTYPE, TYPE, &
& GRIDP, TEMP_GRIDP, GNEIGHMAX, PAINT,VP,ITER, gridvel &
& )
IMPLICIT NONE
INTEGER, INTENT(IN) :: NPTMAX, DIM, TYPEMAX, NEIGHMAX, GNEIGHMAX
INTEGER, INTENT(IN) :: NPTC
INTEGER, INTENT(IN) :: GRIDLIMIT
INTEGER, INTENT(IN) :: GRIDN(0: DIM - 1)
INTEGER, INTENT(IN) :: GRIDNUM
INTEGER, INTENT(IN) :: PTTYPE(0: NPTMAX)
INTEGER, INTENT(IN) :: TYPE(0: TYPEMAX)
INTEGER, INTENT(INOUT) :: GRIDP(0: NPTMAX)
INTEGER, INTENT(INOUT) :: GNEIGH(1: GRIDLIMIT, 0: GNEIGHMAX)
REAL , INTENT(IN) :: GRID(1: GRIDLIMIT, 0: DIM - 1, 0: 1)
REAL , INTENT(IN) :: XP(0: DIM - 1, 0: NPTMAX)
REAL , INTENT(IN) :: VP(0: DIM - 1, 0: NPTMAX)
INTEGER, INTENT(INOUT) :: TEMP_GRIDP(0: NPTMAX)
INTEGER, INTENT(INOUT) :: PAINT(0:NPTMAX)
INTEGER, INTENT(INOUT) :: ITER
real, intent(inout) :: gridvel(GRIDNUM,0:1)
INTEGER :: II, JJ, KK
INTEGER :: DNUM
INTEGER :: GRIDXP
INTEGER :: SGRIDXP
INTEGER :: EGRIDXP
INTEGER :: SGRIDYP
INTEGER :: EGRIDYP
INTEGER :: SEARCH
INTEGER :: SCOUNT
INTEGER :: FCOUNT
INTEGER :: ERROR
INTEGER, PARAMETER :: CELL = 2
if (iter .eq. 5) then
write(*,*) 'vp vals: ',vp(0,23471), vp(0,23475)
end if
...
end subroutine
本节之后还有更多未输出的内容.当我运行代码时,本节第 5 次迭代的输出是:
With more stuff after this section which is not ouputted. When I run the code my outputs on iteration 5 for this section are:
vp vals: 75.00000 75.00000
vp vals: 0.00000000E+00 0.0000000E+00
vp vals: 75.00000 75.00000
我就是想不通为什么我的数组 VP 在子程序中没有值.
I simply can't figure out why my array VP does not have values in the subroutine.
推荐答案
我想通了.VP 被分配为
I figured it out. VP was allocated as
VP(0:DIM,0:NPTMAX)
在主程序和
VP(0:DIM-1,0:NPTMAX)
在子程序中!这导致了错误.
in the subroutine! This caused the error.
这篇关于Fortran 子程序返回错误值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!