[SOLVED] Help with fortran program--encountered run time error
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Help with fortran program--encountered run time error
Hi, i am graduate student and my X colleague gave me this Fortran 77 program to run my result files with. She is no longer reachable. I compiled the following program with gfortran, it compiled well but, when i run it, gave me end of file error. My output file (which is the input file for this program (DFILE1) ) has about 78,000 lines of velocities of atoms (Vx, Vy, Vz with 1000 steps.. an example of some of the atom's velocities in the 1st step is given at the end of this program). I will be highly obliged if any one can help me with this. Thanks in advance.
I run this program on opensuse linux 11.3 version /32 bit/i586
error message:
Code:
At line 124 of file v.autocorrelation.f (unit = 10, file = 'DFILE1')
Fortran runtime error: End of file
Code:
PROGRAM TCORR
COMMON / BLOCK1 / STORX, STORY, STORZ
COMMON / BLOCK2 / VX, VY, VZ
COMMON / BLOCK3 / VACF, ANORM
C *******************************************************************
C ** CALCULATION OF TIME CORRELATION FUNCTIONS. **
C ** **
C ** THIS PROGRAM ANALYZES DATA TO CALCULATE A TIME CORRELATION **
C ** FUNCTION IN ONE SWEEP (LOW MEMORY REQUIREMENT). IN THIS **
C ** EXAMPLE THE VELOCITY AUTO-CORRELATION FUNCTION IS CALCULATED. **
C ** **
C ** PRINCIPAL VARIABLES: **
C ** **
C ** INTEGER N NUMBER OF ATOMS
C ** INTEGER NSTEP NUMBER OF STEPS ON THE TAPE
C ** INTEGER IOR INTERVAL FOR TIME ORIGINS
C ** INTEGER NT CORRELATION LENGTH, INCLUDING T=0
C ** INTEGER NTIMOR NUMBER OF TIME ORIGINS
C ** INTEGER NLABEL LABEL FOR STEP (1,2,3...NSTEP)
C ** REAL VX(N),VY(N),VZ(N) VELOCITIES
C ** REAL VACF(NT) THE CORRELATION FUNCTION
C ** NSTEP AND NT SHOULD BE MULTIPLES OF IOR.
C ** ROUTINES REFERENCED:
C ** **
C ** SUBROUTINE STORE (J1)
C ROUTINE TO STORE THE DATA FOR CORRELATION **
C SUBROUTINE CORR ( J1, J2, IT )
C ROUTINE TO CORRELATE THE STORED TIME ORIGINS **
C USAGE: **
C ** **
C DATA IN FILE DFILE ON FORTRAN UNIT DUNIT. **
C RESULTS IN FILE RFILE ON FORTRAN UNIT RUNIT. **
C *******************************************************************
INTEGER N, NSTEP, IOR, NT, NDIM, DUNIT, RUNIT, NTIMOR
INTEGER FULLUP
PARAMETER ( N = 78, NSTEP = 1000, IOR = 4, NT = 200 )
PARAMETER ( DUNIT = 10, RUNIT = 11 )
PARAMETER ( NDIM = NT / IOR + 1, NTIMOR = NSTEP / IOR )
PARAMETER ( FULLUP = NDIM - 1 )
REAL VX(N), VY(N), VZ(N)
REAL STORX(NDIM,N), STORY(NDIM,N), STORZ(NDIM,N)
REAL VACF(NT), ANORM(NT)
INTEGER S(NTIMOR), TM(NTIMOR)
INTEGER TS, TSS, L, NINCOR, K, JA, IB, IN, IA, JO, I
INTEGER NLABEL
CHARACTER DFILE * 30
CHARACTER RFILE * 30
C *******************************************************************
WRITE(*,'('' **** PROGRAM TCORR **** '')')
WRITE(*,'('' CALCULATION OF TIME CORRELATION FUNCTIONS '')')
C ** READ IN FILE NAMES **
WRITE(*,'('' ENTER DATA FILE NAME '')')
READ (*,'(A)') DFILE
WRITE (*,'('' ENTER RESULTS FILE NAME '')')
READ (*,'(A)') RFILE
C ** INITIALIZE COUNTERS **
NINCOR = FULLUP
JA = 1
IA = 1
IB = 1
C ** ZERO ARRAYS **
DO 5 I = 1, NT
VACF(I) = 0.0
ANORM(I) = 0.0
write(*,*) VACF(I)
5 CONTINUE
C ** OPEN DATA FILE AND RESULTS FILE **
OPEN ( UNIT = DUNIT, FILE = DFILE, ACCESS = 'SEQUENTIAL',
: STATUS = 'OLD', FORM = 'UNFORMATTED' )
OPEN ( UNIT = RUNIT, FILE = RFILE, STATUS = 'NEW' )
C ** CALCULATION BEGINS **
DO 40 L = 1, NTIMOR
JA = JA + 1
S(L) = JA - 1
READ ( DUNIT ) NLABEL, VX, VY, VZ
TM(L) = NLABEL
write(*,*) TM(L)
C ** STORE STEP AS A TIME ORIGIN **
CALL STORE ( JA )
C ** CORRELATE THE ORIGINS IN STORE **
DO 10 IN = IA, L
TSS = TM(L) - TM(IN)
TS = TSS + 1
JO = S(IN) + 1
CALL CORR ( JO, JA, TS )
10 CONTINUE
C ** READ IN DATA BETWEEN TIME ORIGINS. THIS CAN **
C ** BE CONVENIENTLY STORED IN ELEMENT 1 OF THE **
C ** ARRAYS STORX ETC. AND CAN THEN BE CORRELATED **
C ** WITH THE TIME ORIGINS. **
DO 30 K = 1, IOR - 1
READ ( DUNIT ) NLABEL, VX, VY, VZ
CALL STORE ( 1 )
DO 20 IN = IA, L
TSS = NLABEL - TM(IN)
TS = TSS + 1
JO = S(IN) + 1
CALL CORR ( JO, 1, TS )
20 CONTINUE
30 CONTINUE
IF ( L .GE. FULLUP ) THEN
IF ( L .EQ. NINCOR ) THEN
NINCOR = NINCOR + FULLUP
JA = 1
ENDIF
IA = IA + 1
ENDIF
40 CONTINUE
CLOSE ( DUNIT )
C ** NORMALISE CORRELATION FUNCTIONS **
VACF(1) = VACF(1) / ANORM(1) / REAL ( N )
DO 50 I = 2, NT
VACF(I) = VACF(I) / ANORM(I) / REAL ( N ) / VACF(1)
50 CONTINUE
WRITE ( RUNIT, '('' VELOCITY ACF '')')
WRITE ( RUNIT, '(I6,E15.6)') ( I, VACF(I), I = 1, NT )
CLOSE ( RUNIT )
STOP
END
SUBROUTINE STORE ( J1 )
COMMON/ BLOCK1 / STORX, STORY, STORZ
COMMON/ BLOCK2 / VX, VY, VZ
C *******************************************************************
C ** SUBROUTINE TO STORE TIME ORIGINS **
C *******************************************************************
INTEGER J1
INTEGER N, NT, IOR, NDIM
PARAMETER ( N = 78, NT = 200, IOR =4 )
PARAMETER ( NDIM = NT / IOR + 1 )
REAL STORX(NDIM,N), STORY(NDIM,N), STORZ(NDIM,N)
REAL VX(N), VY(N), VZ(N)
INTEGER I
DO 10 I = 1, N
STORX(J1,I) = VX(I)
STORY(J1,I) = VY(I)
STORZ(J1,I) = VZ(I)
10 CONTINUE
RETURN
END
SUBROUTINE CORR ( J1, J2, IT )
COMMON/ BLOCK1 / STORX, STORY, STORZ
COMMON/ BLOCK3 / VACF, ANORM
C *******************************************************************
C ** SUBROUTINE TO CORRELATE TIME ORIGINS **
C *******************************************************************
INTEGER J1, J2, IT
INTEGER N, NT, IOR, NDIM
PARAMETER ( N = 78, NT = 200, IOR = 4 )
PARAMETER ( NDIM = NT / IOR + 1 )
REAL STORX(NDIM,N), STORY(NDIM,N), STORZ(NDIM,N)
REAL VACF(NT), ANORM(NT)
INTEGER I
C ********************************************************************
DO 10 I = 1, N
VACF(IT) = VACF(IT) + STORX(J1,I) * STORX(J2,I)
: + STORY(J1,I) * STORY(J2,I)
: + STORZ(J1,I) * STORZ(J2,I)
10 CONTINUE
ANORM(IT) = ANORM(IT) + 1.0
RETURN
END
Hi and welcome to LinuxQuestions! First an advice: to post a piece of code you can use the CODE tags that greatly improve the readability and preserve the original formatting (spaces and indentation) of the original code. Since I can do it, this time I have edited the post for you. To use CODE tags either switch to Advanced Mode and use the # button, or put code tags explicitly in your text like this:
[CODE]C *** This is a line of code[/CODE]
which results in
Code:
C *** This is a line of code
Regarding your question, the first thing catched by my eyes is:
Code:
OPEN ( UNIT = DUNIT, FILE = DFILE, ACCESS = 'SEQUENTIAL',
: STATUS = 'OLD', FORM = 'UNFORMATTED' )
which tries to open the input file as UNFORMATTED. This means a pure binary (not ASCII) file. Moreover the keyword SEQUENTIAL means it has to be created by another fortran program or at least every record stored into it should begin and terminate with a 32bit unsigned integer specifying the size in bytes of the record. Said that, does your input file matches this requirement or is it an ASCII file as in your example?
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
Hi, thanks a lot for the reply.
As for your question, i got the 'trajectory' file that stores the velocities and co ordinates of atoms in ASCII format and not in Binary.
Therefore you have to modify the code to read an ASCII file OR convert the ASCII to a sequential binary. The first option is the most straightforward. If in doubt about how to do that, please provide a detailed description of the input file structure. Eventually you can attach a gzipped input file (max size 256k) to your post (if allowed to your LQ account), so that we can take a closer look.
Hi, thanks a lot for the reply.
After i read your post, i modified the code little bit and now the error i am getting is
At line 98 of file v.autocorrelation.f (unit = 10, file = 'DFILE1')
Fortran runtime error: Direct access data transfer requires record number
****************************************************
Modified part:
&&&&&&&&&&&&&&&&&&&&
** OPEN DATA FILE AND RESULTS FILE **
OPEN ( UNIT = DUNIT, FILE = DFILE, ACCESS = 'DIRECT',
: STATUS = 'OLD', FORM = 'FORMATTED',RECL = 78 )
OPEN ( UNIT = RUNIT, FILE = RFILE, STATUS = 'NEW' )
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
Please note that i have changed in OPEN access from "sequential" to 'direct' and unformatted to 'formatted' and added record length =78; I assumed the record length is length of single step or number of atoms, which in my case is 78. Is my assumption correct or do i need to see length of the real number in my file??if so, how do i do that??
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
I don't know where to add the record number in the code ?? what does record number mean?
step number??
&&&&&&&&&&&
P.S: Due to limit (256kB) in attaching file and my file size being 5Mb, i just copied and pasted 10 steps from my output file and attached here for your kind perusal.
thanks a lot.
Last edited by fu3lc3ll$; 05-20-2011 at 08:53 AM.
Reason: adding file
OPEN ( UNIT = DUNIT, FILE = DFILE, ACCESS = 'DIRECT',
: STATUS = 'OLD', FORM = 'FORMATTED',RECL = 78 )
The DIRECT access is still referred to unformatted files. In Fortran you have two methods for reading binary files: sequential that is like reading a stream of data from a tape, direct access that is like reading data from a hard disk. In direct access the record number is a sort of address to specify in which position of the file the data are stored. Every record has the same length in bytes and this is the value you can provide using the RECL keyword in the OPEN statement.
Said that, your OPEN statement should be something like
Code:
OPEN ( UNIT = DUNIT, FILE = DFILE, STATUS = 'OLD', FORM = 'FORMATTED')
Looking at your code, I can see that you have to read a certain number of time origins
Code:
NTIMOR = NSTEP / IOR = 1000 / 4 = 250
and after every time origin you have to read a certain number of time steps
Code:
IOR - 1 = 4 - 1 = 3
In practice the code reads 4 steps at each iteration (the first step being a time origin) and computes the time correlation. Then reads in the next 4 steps and compute the time correlation and so on to the end of the file. Can you confirm this is the correct behavior?
Finally notice that the three arrays VX, VY, VZ have size N = 78 and NLABEL is a scalar integer
Code:
REAL VX(N), VY(N), VZ(N)
INTEGER NLABEL
therefore the READ statement in the original code:
Code:
READ ( DUNIT ) NLABEL, VX, VY, VZ
reads an integer followed by 78 + 78 + 78 values at once. In Fortran this is possible only when reading unformatted files. Instead a formatted ASCII file can be read only line by line. For this reason you have to put the READ statement inside a loop to get all the N = 78 lines of each step.
Better to show than explain:
Code:
PROGRAM TCORR
COMMON / BLOCK1 / STORX, STORY, STORZ
COMMON / BLOCK2 / VX, VY, VZ
COMMON / BLOCK3 / VACF, ANORM
C *******************************************************************
C ** CALCULATION OF TIME CORRELATION FUNCTIONS. **
C ** **
C ** THIS PROGRAM ANALYZES DATA TO CALCULATE A TIME CORRELATION **
C ** FUNCTION IN ONE SWEEP (LOW MEMORY REQUIREMENT). IN THIS **
C ** EXAMPLE THE VELOCITY AUTO-CORRELATION FUNCTION IS CALCULATED. **
C ** **
C ** PRINCIPAL VARIABLES: **
C ** **
C ** INTEGER N NUMBER OF ATOMS
C ** INTEGER NSTEP NUMBER OF STEPS ON THE TAPE
C ** INTEGER IOR INTERVAL FOR TIME ORIGINS
C ** INTEGER NT CORRELATION LENGTH, INCLUDING T=0
C ** INTEGER NTIMOR NUMBER OF TIME ORIGINS
C ** INTEGER NLABEL LABEL FOR STEP (1,2,3...NSTEP)
C ** REAL VX(N),VY(N),VZ(N) VELOCITIES
C ** REAL VACF(NT) THE CORRELATION FUNCTION
C ** NSTEP AND NT SHOULD BE MULTIPLES OF IOR.
C ** ROUTINES REFERENCED:
C ** **
C ** SUBROUTINE STORE (J1)
C ROUTINE TO STORE THE DATA FOR CORRELATION **
C SUBROUTINE CORR ( J1, J2, IT )
C ROUTINE TO CORRELATE THE STORED TIME ORIGINS **
C USAGE: **
C ** **
C DATA IN FILE DFILE ON FORTRAN UNIT DUNIT. **
C RESULTS IN FILE RFILE ON FORTRAN UNIT RUNIT. **
C *******************************************************************
INTEGER N, NSTEP, IOR, NT, NDIM, DUNIT, RUNIT, NTIMOR
INTEGER FULLUP
PARAMETER ( N = 78, NSTEP = 1000, IOR = 4, NT = 200 )
PARAMETER ( DUNIT = 10, RUNIT = 11 )
PARAMETER ( NDIM = NT / IOR + 1, NTIMOR = NSTEP / IOR )
PARAMETER ( FULLUP = NDIM - 1 )
REAL VX(N), VY(N), VZ(N)
REAL STORX(NDIM,N), STORY(NDIM,N), STORZ(NDIM,N)
REAL VACF(NT), ANORM(NT)
INTEGER S(NTIMOR), TM(NTIMOR)
INTEGER TS, TSS, L, NINCOR, K, R, JA, IB, IN, IA, JO, I
INTEGER NLABEL
CHARACTER DUMMY * 5
CHARACTER DFILE * 30
CHARACTER RFILE * 30
C *******************************************************************
WRITE(*,'('' **** PROGRAM TCORR **** '')')
WRITE(*,'('' CALCULATION OF TIME CORRELATION FUNCTIONS '')')
C ** READ IN FILE NAMES **
WRITE(*,'('' ENTER DATA FILE NAME '')')
READ (*,'(A)') DFILE
WRITE (*,'('' ENTER RESULTS FILE NAME '')')
READ (*,'(A)') RFILE
C ** INITIALIZE COUNTERS **
NINCOR = FULLUP
JA = 1
IA = 1
IB = 1
C ** ZERO ARRAYS **
DO 5 I = 1, NT
VACF(I) = 0.0
ANORM(I) = 0.0
write(*,*) VACF(I)
5 CONTINUE
C ** OPEN DATA FILE AND RESULTS FILE **
OPEN ( UNIT = DUNIT, FILE = DFILE, STATUS = 'OLD',
: FORM = 'FORMATTED')
OPEN ( UNIT = RUNIT, FILE = RFILE, STATUS = 'NEW' )
C ** CALCULATION BEGINS **
DO 40 L = 1, NTIMOR
JA = JA + 1
S(L) = JA - 1
READ ( DUNIT, '(A5,I4)') DUMMY, NLABEL
DO 7 R = 1, N
READ ( DUNIT, '(1X,F17.14,2(7X,F17.14))' ) VX(R), VY(R), VZ(R)
7 CONTINUE
TM(L) = NLABEL
write(*,*) TM(L)
C ** STORE STEP AS A TIME ORIGIN **
CALL STORE ( JA )
C ** CORRELATE THE ORIGINS IN STORE **
DO 10 IN = IA, L
TSS = TM(L) - TM(IN)
TS = TSS + 1
JO = S(IN) + 1
CALL CORR ( JO, JA, TS )
10 CONTINUE
C ** READ IN DATA BETWEEN TIME ORIGINS. THIS CAN **
C ** BE CONVENIENTLY STORED IN ELEMENT 1 OF THE **
C ** ARRAYS STORX ETC. AND CAN THEN BEN CORRELATED **
C ** WITH THE TIME ORIGINS. **
DO 30 K = 1, IOR - 1
READ ( DUNIT, '(A5,I4)') DUMMY, NLABEL
DO 15 R = 1, N
READ ( DUNIT, '(1X,F17.14,2(7X,F17.14))' ) VX(R), VY(R), VZ(R)
15 CONTINUE
CALL STORE ( 1 )
DO 20 IN = IA, L
TSS = NLABEL - TM(IN)
TS = TSS + 1
JO = S(IN) + 1
CALL CORR ( JO, 1, TS )
20 CONTINUE
30 CONTINUE
IF ( L .GE. FULLUP ) THEN
IF ( L .EQ. NINCOR ) THEN
NINCOR = NINCOR + FULLUP
JA = 1
ENDIF
IA = IA + 1
ENDIF
40 CONTINUE
CLOSE ( DUNIT )
C ** NORMALISE CORRELATION FUNCTIONS **
VACF(1) = VACF(1) / ANORM(1) / REAL ( N )
DO 50 I = 2, NT
VACF(I) = VACF(I) / ANORM(I) / REAL ( N ) / VACF(1)
50 CONTINUE
WRITE ( RUNIT, '('' VELOCITY ACF '')')
WRITE ( RUNIT, '(I6,E15.6)') ( I, VACF(I), I = 1, NT )
CLOSE ( RUNIT )
STOP
END
SUBROUTINE STORE ( J1 )
COMMON/ BLOCK1 / STORX, STORY, STORZ
COMMON/ BLOCK2 / VX, VY, VZ
C *******************************************************************
C ** SUBROUTINE TO STORE TIME ORIGINS **
C *******************************************************************
INTEGER J1
INTEGER N, NT, IOR, NDIM
PARAMETER ( N = 78, NT = 200, IOR =4 )
PARAMETER ( NDIM = NT / IOR + 1 )
REAL STORX(NDIM,N), STORY(NDIM,N), STORZ(NDIM,N)
REAL VX(N), VY(N), VZ(N)
INTEGER I
DO 10 I = 1, N
STORX(J1,I) = VX(I)
STORY(J1,I) = VY(I)
STORZ(J1,I) = VZ(I)
10 CONTINUE
RETURN
END
SUBROUTINE CORR ( J1, J2, IT )
COMMON/ BLOCK1 / STORX, STORY, STORZ
COMMON/ BLOCK3 / VACF, ANORM
C *******************************************************************
C ** SUBROUTINE TO CORRELATE TIME ORIGINS **
C *******************************************************************
INTEGER J1, J2, IT
INTEGER N, NT, IOR, NDIM
PARAMETER ( N = 78, NT = 200, IOR = 4 )
PARAMETER ( NDIM = NT / IOR + 1 )
REAL STORX(NDIM,N), STORY(NDIM,N), STORZ(NDIM,N)
REAL VACF(NT), ANORM(NT)
INTEGER I
C ********************************************************************
DO 10 I = 1, N
VACF(IT) = VACF(IT) + STORX(J1,I) * STORX(J2,I)
: + STORY(J1,I) * STORY(J2,I)
: + STORZ(J1,I) * STORZ(J2,I)
10 CONTINUE
ANORM(IT) = ANORM(IT) + 1.0
RETURN
END
The parts in blue are those ones I modified. Also notice that in your sample file, two VX, VY, VZ triplets were erroneously written on the same row. I don't know if it was a copy/paste typo, but better to check the correct format of the input file before running the fortran code. Just my .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.