Fortran: all lines of the code are not being read

In summary, the program calculates the velocity of all atoms at all iterations in the given trajectory and calculation of velocity correlation function.
  • #1
svartak
5
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
  • #2
What line 42?
 
  • #3
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.
 
  • #4
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)"?
 
  • #5
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
 
  • #6
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. :tongue:
 
  • #7
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.
 
  • #8
Thank you very much all of you!
And I will be very precise in my questions henceforth.
Swati
 

What is Fortran?

Fortran is a high-level programming language that is used for scientific and engineering applications. It was first developed in the 1950s and has undergone several updates since then.

Why are all lines of my Fortran code not being read?

There could be several reasons for this issue. It could be due to syntax errors, missing or incorrect file paths, or improper formatting. It is important to carefully check the code for any mistakes and ensure that all necessary files are included in the program.

How can I debug my Fortran code?

To debug your Fortran code, you can use a debugger tool such as GDB or a built-in debugger in your IDE. These tools allow you to step through your code and track the values of variables to identify any errors.

Is Fortran still used today?

Yes, Fortran is still widely used in scientific and engineering fields. It is known for its efficiency and performance in handling complex mathematical computations.

Can I use modern programming techniques in Fortran?

Yes, Fortran has been updated over the years to include modern programming techniques such as object-oriented programming and parallel programming. However, some legacy code may still use older Fortran standards.

Similar threads

  • Programming and Computer Science
Replies
4
Views
588
  • Programming and Computer Science
Replies
12
Views
953
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top