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

AI Thread Summary
The discussion revolves around troubleshooting a Fortran90 code that encounters garbage values and a backtrace error when reading a matrix from a data file. The user initially faces issues due to incorrect array indexing, as Fortran arrays start at 1, not 0. Additionally, the structure of the input file does not align with the reading logic, requiring adjustments to how the matrix is read. After making corrections, the user still experiences segmentation faults, indicating further issues with the reading logic. Ultimately, the user resolves the problem and seeks recommendations for learning Fortran efficiently. Participants in the discussion clarify that Fortran is still relevant, particularly in numerical computations, and provide useful resources for learning the language.
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 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 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 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 WWGD

Similar threads

Replies
5
Views
5K
Replies
12
Views
3K
Replies
5
Views
2K
Replies
1
Views
3K
Replies
16
Views
4K
Replies
16
Views
3K
Replies
11
Views
3K
Replies
2
Views
2K
Replies
3
Views
3K
Back
Top