The following reproducer contains only one line where the local variable jww is assigned a value. The program is error free, and that it is so can be checked by compiling with /checkmate or with another Fortran compiler.
When a 32-bit EXE is built using FTN95 8.51 with /opt and run, the output is
IWEL JWW KWW
1 25 11
jww at line-40 = 0
**** STOP: Bug encountered
instead of the correct output
IWEL JWW KWW
1 25 11
2 19 6
3 20 6
4 14 11
5 15 11
6 16 11
dz = 12.5000
How did the variable **jww **get set to zero?
The bug does not occur with FTN95 7.20 (note: that version will require that initialisation expressions be written with '(/ ... /)' instead of '[ ... ]'). Nor does the bug occur when 64-bit EXEs are built, with or without /opt .
The source code:
module wells
implicit none
integer, parameter :: NWM = 6, NXM = 41, NZ = 18
integer :: nw, nx
integer, dimension(NWM) :: jw, kw, lcbotw, lctopw
real, dimension(NXM) :: x
end module wells
subroutine initwh(dz)
use wells
implicit none
integer :: iwel, k, l, is, jww, kww, jww0
real :: dz, wisec(4)
print *,' IWEL JWW KWW'
do iwel = 1 , nw
kww = kw(iwel)
jww = jw(iwel) ! only place where jww is set
print '(3I5)',iwel,jww,kww
jww0 = jww ! save jww for checking later
do k = lcbotw(iwel) , lctopw(iwel)
do is = 1 , 4 ! This loop has no purpose other than
wisec(is) = 0. ! to instigate the bug, in this abridged
enddo ! test program. It is needed in the full program.
do l = 1 , 2
if ( k==1 ) then
if ( l==1 ) cycle
dz = 0.5*(x(2)-x(1))
elseif ( k==nx ) then
if ( l==2 ) cycle
dz = 0.5*(x(k)-x(k-1))
elseif ( l==1 ) then
dz = 0.5*(x(k)-x(k-1))
else
dz = 0.5*(x(k+1)-x(k))
endif
if (jww /= jww0) then ! should never be .true.
print *,'jww at line-40 = ',jww
stop 'Bug encountered'
endif
enddo
enddo
enddo
return
end subroutine initwh
program sim
use wells
implicit none
integer i
real dz
!
nw = NWM
nx = NXM
jw = [25, 19, 20, 14, 15, 16]
kw = [11, 6, 6, 11, 11, 11]
lcbotw = [10, 15, 17, 19, 20, 23]
lctopw = [22, 17, 27, 20, 23, 32]
x(6:41) = [(-525.0+i*25.0, i=6,41)]
x(1:5) = [-750., -625., -525., -450., -410.]
call initwh(dz)
print *,'dz = ',dz
end program