Angiolo Huaman
Angiolo Huaman

Reputation: 29

Issue with matrix construction in Fortran

I have a simple subroutine for constructing a tight binding Hamiltonian. It constructs a matrix with 2x2 blocks only in the diagonal and nearest off diagonals. It is like this (there are many parameters defined above in my code):

subroutine hlayer(s,r)
complex*16,dimension(1:lda,1:lda) :: s,r
integer :: i,j
s(:,:)=zero   
r(:,:)=zero
! hamiltonian of the layer
do i=1,lda,2
  s(i,i)     = es
  s(i,i+2)   = tss
  s(i,i+3)   = tsp
  s(i+2,i)   = tss
  s(i+3,i)   = tsp

  s(i+1,i+1) = ep
  s(i+1,i+1+1)   = tsp
  s(i+1,i+2+1)   = tpp
  s(i+1+1,i+1) = tsp
  s(i+1+2,i+1) = tpp
end do
! interaction between layers:
do i=1,lda,2
  r(i,i)     = tss
  r(i,i+1)   = tsp
  r(i+1,i)   = tsp
  r(i+1,i+1) = tpp
end do
end subroutine

When I call the subroutine in the main program (it runs with no problem by the way):

call hlayer(a,b)

I get the following matrix for a, the s variable in the subroutine (lda=10 in this case):

 1.000 0.000  0.100 -0.150  0.000  0.000  0.000  0.000  0.000  0.100
 0.000 1.200 -0.150  0.000  0.000  0.000  0.000  0.000  0.000  0.000
 0.100 0.000  1.000  0.000  0.100 -0.150  0.000  0.000  0.000  0.000
 0.000 0.000  0.000  1.200 -0.150  0.000  0.000  0.000  0.000  0.000
 0.000 0.000  0.100  0.000  1.000  0.000  0.100 -0.150  0.000  0.000
 0.000 0.000  0.000  0.000  0.000  1.200 -0.150  0.000  0.000  0.000
 0.000 0.000  0.000  0.000  0.100  0.000  1.000  0.000  0.100 -0.150
 0.000 0.000  0.000  0.000  0.000  0.000  0.000  1.200 -0.150  0.000
 0.000 0.000  0.000  0.000  0.000  0.000  0.100  0.000  1.000  0.000
 0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  1.200

and I do not understand where the top right 0.1 comes from, since in my subroutine all the entries of the matrix (except those assigned explicitly) are set to zero. Is there any problem with the indexes that I am not taking into account ??

Upvotes: 0

Views: 114

Answers (1)

You are exceeding the array bounds. Compile with error checking (-fcheck=all or -check or something else, depending on the compiler) to diagnose similar errors.

For example

 s(i+1,i+2+1) 

both i+1 and i+2+1 is too large when i is close to lda and is more then the error bound. There are similar issues in all rows.

Upvotes: 5

Related Questions