Fortran [fortran90] have problem in real array index

Click For Summary
The discussion centers around a Fortran subroutine where the user encounters warnings related to array indexing. The specific issue arises from the use of real numbers as indices, particularly in the line where the variable `Hinc` is calculated. The warnings indicate that Fortran does not permit real indices, and the suggested solution is to adjust the order of operations by performing the subtraction before converting to an integer. This change effectively resolves the warnings. Participants in the discussion clarify that while Fortran allows real arguments, the final index must be an integer, and the user expresses appreciation for the guidance provided, noting their inexperience with Fortran 90.
s_hy
Messages
57
Reaction score
0
hi all...i have problem in understanding in array index. my codes are as below

Code:
subroutine  tmz2d
implicit none


double precision                   :: phi
double precision                   :: cosphi,sinphi
double precision                   :: d,dp,dpp,d1,d2
integer                            :: i,j,m,h
integer                            :: minit,mlast
integer                            :: l,p
integer                            :: iinit,ilast,hlast,pinit,plast
integer                            :: n
integer                            :: linit,llast
integer                            :: jinit,jlast
double precision,dimension(200)    :: Hm,Em
double precision                   :: Einc,Hinc
double precision,dimension(200,200):: Ezinc,Hxinc,Hyinc
double precision, parameter        :: pi = 3.14159265
character(len=20)                  :: filename
 

 minit = 1
 mlast = 100
 linit = 1
 llast = 100
 pinit = 1
 plast = 100

 iinit = 1 
 ilast = 100
 jinit = 1
 jlast = 100


 phi = pi/4.0
 cosphi = cos(phi)
 sinphi = sin(phi)
 print *, 'cosphi=',cosphi,'sinphi=',sinphi


do n = 1,100
   
  write (filename, "('data',I3.3,'.dat')") n
  open (unit=130,file=filename)


 call bg1d
 !print *, 'see me2'

  do m = 2,2*ilast-1
    do p = 2,2*jlast-1
      if (Mod(m,2)/=0) then
	if (Mod(p,2)/=0) then
         d = cosphi*(m - minit) + sinphi*(p - pinit)
         dp = d - int(d)
         Einc = (1-dp)*Em(int(d))+dp*(Em(int(d)+1))
         Ezinc(m,p+2) = Einc
         write(130,*) Ezinc(m,p+2)
        end if
      end if
    end do
  end do

  do m = 2,2*ilast-1
    do p = 2,2*jlast-1
      if (Mod(m,2) == 0) then
	if (Mod(p,2) == 0) then
         d = cosphi*(m - minit) + sinphi*(p - pinit)
         dpp = d+1
         dp = dpp-int(d)
           Hinc = (1-dp)*Hm(int(dpp)-0.5)+dp*Hm(0.5+int(dpp))
           Hxinc(m,p+1) = sinphi*Hinc
           Hyinc(m-1,p+2) = -cosphi*Hinc
        end if
      end if
    end do
  end do

end do !n
print *, 'see me3'


end subroutine tmz2d

and i got this warning when execute

subroutines.f90:167.48:

Hinc = (1-dp)*Hm(int(dpp)-0.5)+dp*Hm(0.5+int(dpp))
1
Warning: Extension: REAL array index at (1)
subroutines.f90:167.28:

Hinc = (1-dp)*Hm(int(dpp)-0.5)+dp*Hm(0.5+int(dpp))
1
Warning: Extension: REAL array index at (1)


###########################################

the problem come from line 167 and i found out that fortran do not allow real index...(int(dpp) -0.5)..can anyone give me an advice how to solve this problem.

thank you in advance
 
Technology news on Phys.org
The obvious answer (which should remove the warning, but doesn't have to be correct for your problem) is to use int(dpp-0.5) - right now you are converting dpp to an int, then subtracting (or adding) 0.5 converting it back to a real. Do the subtraction first, conversion to an int later.
 
O.k., you found out that Fortran does not allow "real" index; more precisely, it allows a "real argument" but the final index needs to be integer...to that end, it allows a "real argument" which gets automatically typecast into an integer. If "real argument" wasn't allowed, you would get a compiler "error" instead of a "warning".

Say, is there a chest of drawers in your room? Does it have drawer 1.37?...It's the same with arrays.
 
thanks Borek, its worked and removed the warning

thanks gsal for the explanation..i am really new in fortran90
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 26 ·
Replies
26
Views
3K
  • · Replies 8 ·
Replies
8
Views
5K