Fortran Why is my FORTRAN 90 Program Producing Unexpected Matrix Elements?

  • Thread starter Thread starter rizzodex
  • Start date Start date
  • Tags Tags
    Bug Fortran
AI Thread Summary
The discussion centers on a FORTRAN 90 program that generates two matrices, alpha and beta, and addresses unexpected values in the beta matrix. Specifically, the (4,1) element of the beta matrix is incorrectly showing as 5.0 instead of 2.0, and the (5,8) element is non-zero instead of zero. The issue arises from an out-of-bounds write in the alpha matrix due to the loop condition, which leads to unintended modifications in the beta matrix. The problematic line is identified as alpha(4*k+3,4*k+3+5)=5, which exceeds the matrix dimensions when k reaches its maximum value. This results in writing to memory locations that should not be accessed, causing the unexpected values in beta. A suggestion is made to improve code readability by defining an index variable for clarity.
rizzodex
Messages
12
Reaction score
0
Hi all,
I encountered a problem while running a FORTRAN 90 program, which appears (to me) to be quite surprising. Can someone please explain the following things:
1. Why the (4,1) element of the beta matrix is 5.0 instead of being 2.0 ??
2. Why the (5,8) element of the beta matrix is non-zero instead of being zero ??

Here comes the program with the results at the end:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

PROGRAM hamiltonian
IMPLICIT NONE
INTEGER :: N,i,j,k
REAL, ALLOCATABLE, DIMENSION (:,:) :: alpha,beta
COMPLEX, ALLOCATABLE, DIMENSION (:,:) :: hk


N=8

ALLOCATE (alpha(N,N),beta(N,N),hk(N,N))

alpha=0.
beta=0.
hk=(0.,0.)

DO k=0,(N-4)/4

alpha(4*k+1,4*k+1-3)=5.
alpha(4*k+1,4*k+1+1)=5.
alpha(4*k+2,4*k+2-1)=5.
alpha(4*k+2,4*k+2+1)=5.
alpha(4*k+2,4*k+2+3)=5.
alpha(4*k+3,4*k+3-1)=5.
alpha(4*k+3,4*k+3+1)=5.
alpha(4*k+3,4*k+3+5)=5.
alpha(4*k+4,4*k+4-5)=5.
alpha(4*k+4,4*k+4-1)=5.

beta(4*k+4,4*k+4-3)=2.

END DO

DO i=1,N
OPEN (UNIT=10,FILE='alpha.dat')
WRITE(10,100)(alpha(i,j),j=1,N)
100 FORMAT(8(3X,F3.1))
OPEN (UNIT=20,FILE='beta.dat')
WRITE(20,200)(beta(i,j),j=1,N)
200 FORMAT(8(3X,F3.1))
END DO

CLOSE (10)
CLOSE (20)

END PROGRAM

============================================================
~ RESULTS ~

alpha.dat :


0.0 5.0 0.0 0.0 0.0 0.0 0.0 0.0
5.0 0.0 5.0 0.0 5.0 0.0 0.0 0.0
0.0 5.0 0.0 5.0 0.0 0.0 0.0 5.0
0.0 0.0 5.0 0.0 0.0 0.0 0.0 0.0
0.0 5.0 0.0 0.0 0.0 5.0 0.0 0.0
0.0 0.0 0.0 0.0 5.0 0.0 5.0 0.0
0.0 0.0 0.0 0.0 0.0 5.0 0.0 5.0
0.0 0.0 5.0 0.0 0.0 0.0 5.0 0.0


beta.dat :

0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
5.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 5.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 
Technology news on Phys.org
For some reason the above post doesn't have the last row of the beta matrix which is:

0.0 0.0 0.0 0.0 2.0 0.0 0.0 0.0

So, the total form of the beta matrix is :

beta.dat :

0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
5.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 5.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 2.0 0.0 0.0 0.0
 
This line is probably a problem:

alpha(4*k+3,4*k+3+5)=5.

When k = N-4/4 then the second index is 4*k+8 = N+4 which is out of bounds of alpha. So you are writing to something else in the heap which is probably beta. Generally writing out of bounds is a very bad thing, and a variety of strange things can happen as a result.

You're getting a value at row 8, column 5 in your beta matrix because of this one:
beta(4*k+4,4*k+4-3)=2.
When N = 1 the indices evaluate to beta(8,5).

You could make your code a bit easier to read by defining a quantity like index = 4*k+1 inside your loop.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
6
Views
2K
Replies
2
Views
2K
Replies
4
Views
2K
Replies
17
Views
3K
Replies
1
Views
2K
Replies
6
Views
3K
Back
Top