Writing out arrays fixes a problem for a reason unknown to me

Viewed 73

When I run certain programs using FORTRAN 77 with the GNU gfortran compiler I have come across the same problem several times and I'm hoping someone has insight. The value I want should be ~ 1, but it writes out at the end of a program as something well over 10^100. This is generally a problem restricted to arrays for me. The improper value often goes away when I write out the value of this at some stage in the program before (which inevitably happens in trying to troubleshoot the issue).

I have tried initializing arrays explicitly and have tried some array bound checking as well as some internal logical checks in my program. The issue, to me and my research supervisor, seems to be pathological.

      WRITE(*,*)"OK9999", INV,INVV

vs

      WRITE(*,*)"OK9999", INV,INVV,NP,I1STOR(NP),I2STOR(NP)

The former gives incorrect results for what I would expect for the variables INV and INVV, the latter correct. This is the 'newest' example of this problem, which has on and off affected me for about a year.

The greater context of these lines is:

  WRITE(*,*)"AFTER ENERGY",I1STOR(1),I2STOR(1)
  DO 167 NP=1!,NV

    IF(I1STOR(NP).NE.0) THEN
      INV = I1STOR(NP)
      INVV = I2STOR(NP)
      WRITE(*,*)"OK9999", INV,INVV,NP,I1STOR(NP),I2STOR(NP)
      PAUSE
    ENDIF

I1STOR(1) and I2STOR(1) are written correctly in the first case "AFTER ENERGY" above. If I write out the value of NP after the DO 167 line, this will also remedy the situation.

My expectation would be that writing out a variable wouldn't affect its value. Often times I am doing large, time-intensive calculations where the ultimate value is way off and in many cases it has traced back to the situation where writing the value out (to screen or file) magically alleviates the problem. Any help would be sincerely appreciated.

0 Answers
Related