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

In summary: 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.
  • #1
kranthi4689
16
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
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
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

  • data.txt
    15.1 KB · Views: 274
Technology news on Phys.org
  • #2
Are you reading this files into some other software, say, Excel, or is this self-contained?
 
  • #3
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.
 
  • #4
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
  • #5
Forgot the third error: you are neglecting to read the index that appears to the left of the matrix elements.
 
  • #6
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
 
  • #7
You can't read vecx inside the j loop. You have to use an implicit loop as before.
 
  • #8
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.
 
  • #9
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

What could be causing the issue of receiving garbage values while reading matrices from a file?

There could be several reasons for this issue. One possibility is that the format of the file does not match the expected format for the matrix. Make sure the file contains only numerical values in the correct order. Another possibility is that there may be hidden characters or extra spaces in the file that are causing the matrix to be read incorrectly.

How can I fix the problem of getting garbage values while reading matrices from a file?

First, double check that the file is in the correct format and contains only numerical values. If that does not solve the issue, try opening the file in a text editor to check for any hidden characters or extra spaces. If necessary, you may need to manually remove these characters. Another solution could be to use a different file reading method or library.

Is there a specific file format that is required for reading matrices?

There is no specific file format that is required, but it is important to ensure that the file follows a consistent format that matches the expected input for the matrix. This could be a comma-separated values (CSV) file, a tab-delimited file, or a simple text file with numbers separated by spaces or new lines.

Could the issue of garbage values while reading matrices be due to a problem with my code?

It is possible that the issue is caused by a mistake in your code. Double check your code to make sure you are correctly reading and parsing the file, and that your variables are properly initialized. It may also be helpful to use debugging tools or print statements to track the values being read from the file.

Are there any common mistakes that can lead to garbage values while reading matrices from a file?

Yes, some common mistakes include not properly handling or accounting for empty or invalid values in the file, using the wrong file reading method, or not converting the values from the file into the correct data type. It is also important to make sure the file is closed properly after reading to avoid any potential errors.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
4
Views
499
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
Replies
10
Views
904
  • Programming and Computer Science
2
Replies
55
Views
4K
Back
Top