Why is my FORTRAN 90 Program Producing Unexpected Matrix Elements?

  • Fortran
  • Thread starter rizzodex
  • Start date
  • Tags
    Bug Fortran
In summary: Then you can use that to index your arrays.In summary, the conversation discusses a problem encountered while running a FORTRAN 90 program and the unexpected results in the alpha and beta matrices. The code provided also shows a potential issue with writing out of bounds, which can lead to unexpected values in the matrices. Suggestions are given for improving the code for readability.
  • #1
rizzodex
12
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
  • #2
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
 
  • #3
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.
 

Related to Why is my FORTRAN 90 Program Producing Unexpected Matrix Elements?

1. What is FORTRAN 90 and how is it different from other languages?

FORTRAN 90 is a programming language specifically designed for scientific and numerical computations. It is an extension of the original FORTRAN language and includes modern features such as array operations, recursion, and user-defined data types. It is different from other languages in its syntax and its focus on scientific computing.

2. What is a "peculiar bug" in FORTRAN 90?

A "peculiar bug" in FORTRAN 90 refers to unexpected or unusual behavior in a code written in this language. It could be caused by a coding error, a limitation of the language, or a compatibility issue with other software.

3. How can I troubleshoot a peculiar bug in FORTRAN 90?

To troubleshoot a peculiar bug in FORTRAN 90, you can start by checking your code for any syntax errors or logical mistakes. You can also consult the FORTRAN 90 documentation or online forums for common bug fixes. If the bug persists, you may need to use debugging tools or seek assistance from other programmers.

4. Can I use modern software and libraries with FORTRAN 90?

Yes, FORTRAN 90 is compatible with many modern software and libraries. However, it may require some adjustments or workarounds to integrate them seamlessly. It is always best to check the documentation or consult with other programmers for compatibility issues.

5. Is FORTRAN 90 still relevant in modern scientific computing?

Yes, FORTRAN 90 is still widely used in scientific and numerical computing, especially in fields such as physics, engineering, and finance. It is a highly efficient and reliable language for complex calculations and simulations. However, it is always beneficial to keep learning and adapting to newer languages and technologies in the field.

Similar threads

  • Programming and Computer Science
Replies
4
Views
745
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
6
Views
4K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
6
Views
3K
Back
Top