Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

reading data error

carlos8410
Beginner
1,008 Views
Hi,

I come into a strange problem related with reading data from a file. I am trying to read the data from a formatted data file.
And the file contains integer, floating number, two dimensional array and 1-dimensional array . I read those data in a sequence. i.e. the first line is integer, the second line is floating number, the 3rd line to xx-th line are for two dimensional array and then for 1-dimensional array. The problem is the code has a breakpoint in the middle. It first stops at the middle of a two-dimensional array, and when I changed the sequence of reading data, it stops at other place. I check the data format, and everything is right. This is really weird and I tried several ways to fix it, didn't work.
(I paste the code below. )

carlos

============================
PROGRAM MAIN

IMPLICIT REAL*8 (A-H,O-Z)
INTEGER JUMP_FLAG, RHO_FLAG, FLAG_191, FLAG_300, FLAG_400, ERROR_CODE
ALLOCATABLE :: X(:), XL(:), XU(:),XBASE(:), FVAL(:), XOPT(:), GOPT(:),PQ(:), HQ(:), SL(:), SU(:), XNEW(:), XALT(:), D(:), VLAG(:), W(:), XPT(:,:), BMAT(:,:), ZMAT(:,:)

OPEN ( 15, file='F:\System File\Desktop\Fortran\input1.inp' )

READ (15, *) N, NPT, IPRINT, MAXFUN, NDIM, NF, KNEW, KOPT, KBASE, NFSAV, NTRITS, NRESC, JUMP_FLAG, RHO_FLAG, FLAG_191, FLAG_300, FLAG_400, ERROR_CODE

READ (15, *) RHOBEG, RHOEND, F, XOPTSQ, FSAVE, DELTA, DSQ, CRVMIN, DNORM, RHO, DISTSQ, DIFFA, DIFFB, DIFFC, ADELT, ALPHA, CAUCHY, SUM, BETA, DENOM, SCADEN,BIGLSQ,FOPT,VQUAD

NP=N+1
NDIM=NPT+N
NPTM=NPT-NP
NH=(N*NP)/2

ALLOCATE (X(1:N), XL(1:N), XU(1:N), XBASE(1:N), FVAL(1:NPT),
1 XOPT(1:N), GOPT(1:N), PQ(1:NPT), HQ(1:NH), SL(1:N), SU(1:N),
2 XNEW(1:N), XALT(1:N), D(1:N), VLAG(1:NDIM), W(1:3*NDIM),
3 XPT(NPT,N), BMAT(NDIM,N), ZMAT(NPT,NPTM))

DO I=1,NDIM
READ (15,*) (BMAT(I,J),J=1,N)
END DO
DO I=1,NPT
READ (15,*) (XPT(I,J),J=1,N)
END DO
DO I=1,NPT
READ (15,*) (ZMAT(I,J),J=1,NPTM)
END DO

READ (15,*) (X(I),I=1,N)
READ (15,*) (XL(I),I=1,N)
READ (15,*) (XU(I),I=1,N)
READ (15,*) (XBASE(I),I=1,N)
READ (15,*) (FVAL(I),I=1,NPT)
READ (15,*) (XOPT(I),I=1,N)
READ (15,*) (GOPT(I),I=1,N)
READ (15,*) (PQ(I),I=1,NPT)
READ (15,*) (HQ(I),I=1,NH)
READ (15,*) (SL(I),I=1,N)
READ (15,*) (SU(I),I=1,N)
READ (15,*) (XNEW(I),I=1,N)
READ (15,*) (XALT(I),I=1,N)
READ (15,*) (D(I),I=1,N)
READ (15,*) (VLAG(I),I=1,NDIM)
READ (15,*) (W(I),I=1,3*NDIM)

CLOSE (15)
...
============================
0 Kudos
6 Replies
ArturGuzik
Valued Contributor I
1,008 Views
Quoting - carlos8410
The problem is the code has a breakpoint in the middle. It first stops at the middle of a two-dimensional array, and when I changed the sequence of reading data, it stops at other place. I check the data format, and everything is right. This is really weird and I tried several ways to fix it, didn't work.
(I paste the code below. )

What's the error (when it breaks)? Do you have consistent data, I mean, the sizes you read and then use them to allocate array and do loops match the number of data in the remaining portion of the file?

Another thing, everybody would agree with, is that IMPLICIT REAL*8 (A-H,O-Z) is not a good practise.

A.
0 Kudos
Les_Neilson
Valued Contributor II
1,008 Views

/Personal style preference :
allocatable x(:) ! one allocatable per variable
allocatable xl(:)
etc

allocate(x(n), stat=ierr) ! one allocate per variable
etc
/end preference

This way you have clarity andcan test for ierr /= 0

Whatare the values of N NPT and NDIM ?

You have 19 arrays each of double precision and if N, NPT and NDIM are large you may have exceeded the 2Gb limit for an array (which would have shown up in ierr/=0 if an array failed to allocate).
If, for some reason,an array failed to allocate then presumably trying to read data into it would also fail which may account for your code stopping part way through.

Les

0 Kudos
carlos8410
Beginner
1,008 Views
Quoting - ArturGuzik

What's the error (when it breaks)? Do you have consistent data, I mean, the sizes you read and then use them to allocate array and do loops match the number of data in the remaining portion of the file?

Another thing, everybody would agree with, is that IMPLICIT REAL*8 (A-H,O-Z) is not a good practise.

A.
The cmd window shows the error like this:
forrtl: severe (24): end-of-file during read, unit 15, file F:System FileDesktopFortraninput1.inp

And my reading data is consistent with the data in the file.

When it reads a two-dimensional array with the size 26 by 10, it reads only the first 20 lines and then stops there, saying it triggers a breakpoint. So I changed the data and reading sequence correspondingly. I put the two-dimensional array at the first and read it first. And the code stops when reading the later part, which means my data format doesn't have any problem, otherwise it won't successfully read the two-dimensional array.

For your last point, that's definitely correct.
0 Kudos
carlos8410
Beginner
1,008 Views
Quoting - Les Neilson

/Personal style preference :
allocatable x(:) ! one allocatable per variable
allocatable xl(:)
etc

allocate(x(n), stat=ierr) ! one allocate per variable
etc
/end preference

This way you have clarity andcan test for ierr /= 0

Whatare the values of N NPT and NDIM ?

You have 19 arrays each of double precision and if N, NPT and NDIM are large you may have exceeded the 2Gb limit for an array (which would have shown up in ierr/=0 if an array failed to allocate).
If, for some reason,an array failed to allocate then presumably trying to read data into it would also fail which may account for your code stopping part way through.

Les


Now I only set N to be 10 and NPT 16 and NDIM 26, which are not large. The file is only 20k in size. I also test it in your way which is oneallocate per variable and I find no error for this.
0 Kudos
ArturGuzik
Valued Contributor I
1,008 Views
Quoting - carlos8410
The cmd window shows the error like this:
forrtl: severe (24): end-of-file during read, unit 15, file F:System FileDesktopFortraninput1.inp

Well, so you're trying to read more records/lines/data than there is in your file. Docs tell you that there might be 3 reasons for that:

An Intel Fortran RTL I/O system end-of-file condition was encountered during execution of a READ statement that did not contain an END, ERR, or IOSTAT specification.

An end-of-file record written by the ENDFILE statement was encountered during execution of a READ statement that did not contain an END, ERR, or IOSTAT specification.

An attempt was made to read past the end of an internal file character string or array during execution of a READ statement that did not contain an END, ERR, or IOSTAT specification.


What does debugger tell you? Maybe there is a mistake in calculating sizes (especially NH=(N*NP)/2, any rounding issues?), so you're actually demanding more values than there is in file?

A.
0 Kudos
carlos8410
Beginner
1,008 Views
Quoting - ArturGuzik

I think I have already found the problem. It's still due to my fault.
Thanks a lot.

Well, so you're trying to read more records/lines/data than there is in your file. Docs tell you that there might be 3 reasons for that:

An Intel Fortran RTL I/O system end-of-file condition was encountered during execution of a READ statement that did not contain an END, ERR, or IOSTAT specification.

An end-of-file record written by the ENDFILE statement was encountered during execution of a READ statement that did not contain an END, ERR, or IOSTAT specification.

An attempt was made to read past the end of an internal file character string or array during execution of a READ statement that did not contain an END, ERR, or IOSTAT specification.


What does debugger tell you? Maybe there is a mistake in calculating sizes (especially NH=(N*NP)/2, any rounding issues?), so you're actually demanding more values than there is in file?

A.
0 Kudos
Reply