本文介绍了Fortran子例程返回错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在开发一个fortran程序,并遇到了一个奇怪的问题。当我尝试直接在调用特定的子例程之前输出数组的某些值时,我会得到正确的值。然后我尝试输出一些与我开始子程序相同数组的值,并且它们是0.我最终在子程序之后输出数组的值,并且这些值返回到期望值。任何人都可以帮我理解为什么?我的代码如下:



首先,调用main函数中的子例程,并在write语句中输出值:

  if(iter .eq。5)then 
write(*,*)'vp vals:',vp(0,23471),vp 0,23475)
结束如果

CALL GRID_DIVISION(&
& NPTMAX,DIM,TYPEMAX,NEIGHMAX,NPTC,GRIDLIMIT,&
& GRIDN ,GRIDNUM,GRID,GNEIGH,XP,PTTYPE,TYPE,&
& GRIDP,TEMP_GRIDP,GNEIGHMAX,PAINT,VP,ITER,gridvel&
&)
$ b $如果(iter .eq。5)then
write(*,*)'vp vals:',vp(0,23471),vp(0,23475)
end if

这是调用以下子程序,其中我只会发布相关部分:
$ (&
& NPTMAX,DIM,TYPEMAX,NEIGHMAX,NPTC,GRIDLIMIT,&
& GRIDN,GRIDNUM, GRID,GNEIGH,XP,PTTYPE,TYPE,&am p;
& 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,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: :EGRID XP
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子程序

在本节之后有更多内容没有被输出。当我运行代码时,本节的迭代5中的输出是:

  vp vals:75.00000 75.00000 
vp vals :0.00000000E + 00 0.0000000E + 00
vp vals:75.00000 75.00000

我可以不知道为什么我的数组VP在子程序中没有值。

解决方案

我想通了。 VP被分配为:

$ p $ VP(0:DIM,0:NPTMAX)

在主程序和

  VP(0: DIM-1,0:NPTMAX)

在子程序中!这导致了错误。


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:

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

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

I simply can't figure out why my array VP does not have values in the subroutine.

解决方案

I figured it out. VP was allocated as

VP(0:DIM,0:NPTMAX)

in the main program and

VP(0:DIM-1,0:NPTMAX)

in the subroutine! This caused the error.

这篇关于Fortran子例程返回错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 02:00