0

I would like to read coordinates from an from an input file. An example input file would look something like :

1    0.1542    0.2541    1.2451   N
12   4.5123    2.0014    2.0154   O
43   8.2145    0.2978    4.2165   H

etc... The size of this file is variable. The first column is a number assigned to an atom, the following columns are its x,y,z coordinates, and the final column is the elemental symbol of the atom.

I tried something along the lines of:

integer, allocatable :: atnum(:)
double precision, allocatable :: coord(:,:)
character(len=2), allocatable :: element(:)

open(unit=20, file='input', status='old',action='read')

read(20,*,end=200) atnum, coord(:,1:3), element
200 close(20)

This throws me the error:

Fortran runtime error: Bad integer for item 2 in list input

I assume that the program read the first entry into atnum(1), but then tried to continue reading into the second entry of the first row into atnum(2). How can I get it to read the input correctly?

I also think that there might be a problem with telling the program to read the middle three columns into coord(:,1:3). It is likely that it will read the first three entries into coord(1,1), coord(2,1), coord(3,1), then run into the character at the end of the line and become confused again. How can I tell it to fix the first subscript for the line, and read into the other dimension? Or will I have to swap the indices, like coord(1:3,:)? Will that work?

EDIT: The above has been answered by tpg2114, but I still have a problem. I can't allocate the the array until I know how many sets of coordinates are to be read, but I only know how many atoms there are until I reach the end of the file. The program compiles fine if I don't allocate atnum, coord and element, but returns a segmentation fault when I try to run it. How can I get it to read into the dynamic arrays without previously allocating them?

It sounds similar to this question: Variable size arrays in Fortran without Allocate()

Thanks in advance.

Community
  • 1
  • 1
Samuel Tan
  • 1,700
  • 5
  • 22
  • 35

2 Answers2

3

You need to loop over the READ statement. A single READ will only go to the end of the record, in this case the line. You will know when you run out of lines because you put the end=200 argument, so it will jump to line 200 (which you don't seem to have here).

So, you need to make sure:

A) Your arrays are long enough to contain the file (you don't show the allocation for them, I assume you allocated them?)

B) You loop over the READ statement

C) You specify an index for the atnum argument and the first argument of coord and the elem argument.

For example, assuming the declarations are the same and the arrays are allocated large enough, and I is an INTEGER

I = 1
DO 
  read(20,*,end=200) atnum(I), coord(I,1:3), elem(I)
  I = I + 1
END DO 
200 continue

Also, consider dropping the end=200 part and using IOSTAT instead since that is modern and line numbers are deprecated.

tpg2114
  • 14,112
  • 6
  • 42
  • 57
  • Thank you for your answer. I was not sure how to implement a read data `do` loop. You're saying that instead of `end=200` I should have something like `k=iostat` and have a loop like `do while (k .ge. 0) ... end do`? That would work? – Samuel Tan Dec 15 '11 at 23:36
  • Read http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/iostatus.html for an explanation on how IOSTAT works. – tpg2114 Dec 15 '11 at 23:45
3

Step 1: Read the file, reading each line into a string, merely to count the lines as "num_lines", using "iostat" to detect the end-of-file condition.

Step 2: rewind the file.

Step 3: allocate the arrays to the correct lengths "num_lines".

Step 4: do the "actual" read of the file into the arrays, element by element:

do i=1, num_lines
   read (20, *) atnum(I), coord(I,1:3), elem(I)
end do
M. S. B.
  • 28,968
  • 2
  • 46
  • 73
  • This is what I feared. Reading the same file twice. Seems like the only way. How would I "rewind" the file? – Samuel Tan Dec 16 '11 at 01:56