Fortran: all lines of the code are not being read

  • Context: Fortran 
  • Thread starter Thread starter svartak
  • Start date Start date
  • Tags Tags
    Code Fortran Lines
Click For Summary

Discussion Overview

The discussion revolves around a Fortran programming issue where a beginner, svartak, encounters a problem with lines of code not being executed after a certain point in their program that calculates the velocity correlation function. The focus is on debugging and understanding the flow of control in the code, particularly related to nested loops and file reading operations.

Discussion Character

  • Technical explanation
  • Debugging
  • Conceptual clarification

Main Points Raised

  • svartak reports that lines after line 42 are not being read, despite no compilation errors.
  • Some participants question what line 42 refers to and suggest that the issue may be related to the nested do loops.
  • svartak mentions that commenting out the first two do loops allows the later lines to be read, indicating a potential issue with the loop structure.
  • One participant explains that the "read(1,*,end=100)" statement causes the program to jump to label 100 when it reaches the end of the file, which is at the end of the program, potentially preventing further execution.
  • Another participant suggests moving the label '100' up in the code to allow for proper flow after the do loops.
  • There is a clarification about the distinction between compilation and execution, emphasizing that the compiler reads all lines, but execution may not reach certain lines due to control flow.

Areas of Agreement / Disagreement

Participants generally agree that the issue is related to the control flow caused by the "end=100" statement in the read operation. However, there is no consensus on the best solution, as suggestions vary regarding the placement of the label and the interpretation of the problem.

Contextual Notes

Participants note the importance of precise language when describing programming issues, as misunderstandings can arise from vague descriptions of the problem.

Who May Find This Useful

Beginners in programming, particularly those working with Fortran, may find this discussion useful for understanding debugging techniques and control flow in code.

svartak
Messages
4
Reaction score
0
hello,
i'm a beginner in programming. i wrote a program to calculate velocity correlation function. i do not get any compilation errors but the lines after line number 42 are not being read. could someone tell me what is it that I'm doing wrong?

thanks a lot!
svartak

!program to calculate velocity of all atoms at all iterations in the given trajectory and calculation of velocity correlation function
!velocity at t=0 is written as v(i,2) i.e. velocity calculated at iteration 2
parameter natm=144, idim=1000
real*8 x(natm),y(natm),z(natm),x_old(natm),y_old(natm),z_old(natm)
real*8 vx(natm,idim),vy(natm,idim),vz(natm,idim),dt,proj
real*8 vdot(natm,idim),vmag0, Niteration
character*80 filename
character*2 ch
c call getarg(1, filename)
open(1, file='traj.xyz', status='OLD')
open(2, file='velocity-all.dat', status='unknown')
open(3, file='input-data', status='unknown')

read(3,*)dt !timestep
read(3,*)Nsample !highest iteration number for sampling region
read(3,*)Nmax !last iteration number in the trajectory
read(3,*)dnsample !interval for the selection of sampling points
read(3,*)Nstep !stepsize for interval between v(t0) and v(t)

j=0
do
read(1,*)Natom
read(1,*)
j=j+1 !loop for all iterations
if (j.gt.idim) then
write(*,*)'idim is too small.'
stop
end if
do i = 1, Natom !loop for all atoms in each iteration
read(1,*,end=100)ch, x(i), y(i),z(i) !i'th atom
if (j.ge.2) then
vx(i,j) = (x(i) - x_old(i))/dt !velocity will be in angstrom/fs
vy(i,j) = (y(i) - y_old(i))/dt
vz(i,j) = (z(i) - z_old(i))/dt
end if

x_old(i)=x(i)
y_old(i)=y(i)
z_old(i)=z(i)
write(2,*)vx(i,j),vy(i,j),vz(i,j)
end do
end do
proj=0
Niteration=0
do dN = Nsample+1, Nmax-Nsample, Nstep !dN=v(t)-v(t0)
do k = 1, Nsample, dnsample !loop over different initial velocity points
write(2,*)Niteration
do i = 1, Natom !loop over all atoms
vdot(i,k)=vx(i,k)*vx(i,k+dN)+vy(i,k)*vy(i,k+dN)
& +vz(i,k)*vz(i,k+dN)
vmag0=vx(i,k)*vx(i,k)+vy(i,k)*vy(i,k)+vz(i,k)*vz(i,k)

proj = (proj + (vdot(i,k)/vmag0))
write(2,*)proj
write(2,*)vdot(i,k)
write(2,*)vmag0
end do
end do
end do
vcf = proj/Natom/Niteration
write(2,*)vcf
c end do
100 end
 
Technology news on Phys.org
What line 42?
 
Borek said:
What line 42?

Line 42 is the second "end do", before proj=0. So something is wrong with his nested do loop.svartak: A great though laborious way of debugging a small self-contained program like is to literally run it on paper.

Take a scrap of paper and define your variables as boxes, then step through your code, changing each var as directed by the code. Don't take any shortcuts. You're trying to find out what your program is doing, not what you think it's doing.

Also, if ypou wrap your code in the [ CODE ][ /CODE ] tags, it will preserve the indenting, making it far easier for us to read.
 
Thanks for you reply.
I did that but could not find a problem. The lines after the second end do (just before proj=0) are not just read.
If I comment out the first two do loops then the later lines are read.
Could the problem be due to "read(1,*,end=100)"?
 
The problem is the line:
Code:
        read(1,*,end=100)ch, x(i), y(i),z(i) !i'th atom
inside the do-loops. The part "end=100" means that when the program can no longer read data from the file (it has reached the end of file) then, the flow of the program should jump to statement labeled "100"...and it so happens the such label is at the very end of the program!

What you need to do is remove the label '100' from the last line in the program and move it up...maybe put it right after the do-loops, like after the second 'end do', something like this
Code:
    end do
    end do
    100 continue
    proj=0
 
gsal said:
The problem is the line:
Code:
        read(1,*,end=100)ch, x(i), y(i),z(i) !i'th atom
inside the do-loops. The part "end=100" means that when the program can no longer read data from the file (it has reached the end of file) then, the flow of the program should jump to statement labeled "100"...and it so happens the such label is at the very end of the program!

What you need to do is remove the label '100' from the last line in the program and move it up...maybe put it right after the do-loops, like after the second 'end do', something like this
Code:
    end do
    end do
    100 continue
    proj=0

Statement lines in a program. Wow. I feel like a kid again. :-p
 
So, apart from the fact you expected us to laboriously count lines, you also meant the lines were not executed? Compiler read them all, as otherwise it would not compile the program, execution stage has nothing to do with the code reading.

Actually I thought you may be referring to the fact your program was not reading all the data from the external file. Could be it was just my English failing me, but you really have to be as precise as possible.
 
Thank you very much all of you!
And I will be very precise in my questions henceforth.
Swati
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K