I'm getting garbage values while reading matrices from a file

Click For Summary

Discussion Overview

The discussion revolves around issues encountered while reading a matrix from a data file using Fortran90 code. Participants explore potential errors in the code, the structure of the data file, and the implications of Fortran's array indexing.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster reports receiving garbage values and a backtrace error when attempting to read a matrix from a file.
  • Some participants suggest that the issue may stem from the Fortran array indexing starting at 1, while the code uses a zero-based index.
  • Others point out that the structure of the data file does not align with the reading logic in the code, indicating that adjustments are needed in the loop structure.
  • One participant notes that the original code neglects to read an index that appears alongside the matrix elements.
  • Subsequent replies discuss the necessity of reading the column index outside of the inner loop to avoid runtime errors.
  • A later reply confirms that a modified reading approach worked, and the original poster seeks further resources for learning Fortran.
  • Some participants express opinions on the relevance of Fortran today, with differing views on its usage in modern programming contexts.

Areas of Agreement / Disagreement

Participants generally agree on the presence of errors in the original code and the need for adjustments. However, there are differing opinions on the relevance and future of Fortran as a programming language.

Contextual Notes

Limitations include unresolved assumptions about the data file structure and the specific nature of the errors encountered. The discussion does not reach a consensus on the best practices for reading matrices in Fortran.

Who May Find This Useful

Readers interested in Fortran programming, matrix manipulation, and debugging techniques in scientific computing may find this discussion relevant.

kranthi4689
Messages
16
Reaction score
0
Hi All,
While trying to read a matrix from data file using fortran90 code ,I get garbage values and a backtrace error.
Error termination. Backtrace: #0 0x7f4a4de3631a #1 0x7f4a4de36ec5 #2 0x7f4a4de3768d #3 0x7f4a4dfa4d42 #4 0x7f4a4dfa6ad5 #5 0x7f4a4dfa80f9 #6 0x56040bbeae57 #7 0x56040bbeaf93 #8 0x7f4a4da4bb96 #9 0x56040bbeaa29 #10 0xffffffffffffffff

The code I use is
[CODE title="read_file.f90"]program read_file
implicit none
integer :: ios,i,j
character (len =40) :: str_name
character (len=1000) :: line
integer , dimension (10) :: vect
real , dimension (11,16) :: hamilt
str_name = ' OVERLAP MATRIX - CELL N. 1( 0 0 0)'
open(unit=20,FILE='data.txt',iostat=ios,status='old')
if ( ios /= 0 ) stop "Error opening inputfile"
do
read(20,'(A)',end=100) line
if (line(1:40) == str_name ) then
write (*,*) "found line"
write (*,*) line
!read (20,*) (a(i,j), j = 1,16))
do i = 0,10
read (20,*,end =100) (hamilt(i,j), j=1,16)
write (*,*) hamilt(i,j)
enddo
endif
enddo
100 close(20)
end program
[/CODE]
I attached my data file. I want to read the matrix that starts after str_name.
Kindly help me in understanding the error in my code. thank you.
 

Attachments

Technology news on Phys.org
Are you reading this files into some other software, say, Excel, or is this self-contained?
 
WWGD said:
Are you reading this files into some other software, say, Excel, or is this self-contained?
@WWGD I just want to read them and write them in a separate txt file.
 
kranthi4689 said:
Fortran:
          do i = 0,10
                  read (20,*,end =100) (hamilt(i,j), j=1,16)
                  write (*,*) hamilt(i,j)
          enddo
1st error: i starts at zero, but Fortran array indices start at 1.
2nd error: the structure in the file doesn't follow what you are doing here. The outer loop should go from i = 1 to 16, then the inner loop should go from 1 to min(i,10). You then have to add code to read the rest of the matrix (since only the first 10 columns are found in the first block).
 
  • Like
Likes   Reactions: WWGD and .Scott
Forgot the third error: you are neglecting to read the index that appears to the left of the matrix elements.
 
DrClaude said:
1st error: i starts at zero, but Fortran array indices start at 1.
2nd error: the structure in the file doesn't follow what you are doing here. The outer loop should go from i = 1 to 16, then the inner loop should go from 1 to min(i,10). You then have to add code to read the rest of the matrix (since only the first 10 columns are found in the first block).
I rectified the code to address the errors, but I still get segmentation faults.
Code:
 read (20,*) (vecy(i), j = 1,10) ! read column index
          do i = 1,16
             do j = 1,min(i,10)
                 read(20,*,end=100) vecx(i) , hamilt(i,j) ! row index and matrix elements
                  write (*,*) hamilt(i,j)
               enddo
          enddo
         endif
 
You can't read vecx inside the j loop. You have to use an implicit loop as before.
 
DrClaude said:
You can't read vecx inside the j loop. You have to use an implicit loop as before.
reading it out the loop is giving a fortran runtime error.
 
Fortran:
read (20,*,end =100) vecx(i), (hamilt(i,j), j=1,min(i,10))
 
  • Like
Likes   Reactions: kranthi4689
  • #10
DrClaude said:
Fortran:
read (20,*,end =100) vecx(i), (hamilt(i,j), j=1,min(i,10))
It worked Thank you. Could you kindly recommend a book or site or some material from where I can learn fortran efficiently. Thank you.
 
  • #11
Wow, I thought Fortran was a dead language outside of dealing with legacy systems.
 
  • #12
kranthi4689 said:
It worked Thank you. Could you kindly recommend a book or site or some material from where I can learn fortran efficiently. Thank you.
A few good links:
https://www.fortrantutorial.comhttp://www.usm.uni-muenchen.de/people/puls/lessons/intro_general/f90_for_beginners.pdfhttp://pages.mtu.edu/~shene/COURSES/cs201/NOTES/fortran.html
But if I may, I would like to point out that the problem you were having has nothing to do with Fortran per se. I have a feeling you would have stumbled whichever language was used. It is important to decompose what needs to be done step by step and then program these steps.

WWGD said:
Wow, I thought Fortran was a dead language outside of dealing with legacy systems.
Far from it. Especially with the newer versions of Fortran, the language has now most of the features of modern languages. I would say that for numerical computations, it is still one of the best options available.
 
  • Like
Likes   Reactions: kranthi4689 and WWGD
  • #13
WWGD said:
Wow, I thought Fortran was a dead language outside of dealing with legacy systems.

No, not dead at all, my lab uses it exclusively for large scale computational efforts. I'd convert to C++ but I'm the youngest member of my group and the other 4 members don't know a line of C/C++, so we are continuing with Fortran for the time being, when they start to retire, we'll convert over.
 
  • Like
Likes   Reactions: WWGD

Similar threads

  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K