Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran: all lines of the code are not being read

  1. Mar 30, 2012 #1
    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
     
  2. jcsd
  3. Mar 30, 2012 #2

    Borek

    User Avatar

    Staff: Mentor

    What line 42?
     
  4. Mar 30, 2012 #3

    DaveC426913

    User Avatar
    Gold Member

    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.
     
  5. Mar 30, 2012 #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)"?
     
  6. Mar 30, 2012 #5
    The problem is the line:
    Code (Text):

            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 (Text):

        end do
        end do
        100 continue
        proj=0
     
     
  7. Mar 30, 2012 #6

    DaveC426913

    User Avatar
    Gold Member

    Statement lines in a program. Wow. I feel like a kid again. :tongue:
     
  8. Mar 31, 2012 #7

    Borek

    User Avatar

    Staff: Mentor

    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.
     
  9. Mar 31, 2012 #8
    Thank you very much all of you!!
    And I will be very precise in my questions henceforth.
    Swati
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook