c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1997  by  Jay William Ponder  ##
c     ##              All Rights Reserved              ##
c     ###################################################
c
c     ###############################################################
c     ##                                                           ##
c     ##  subroutine readdyn  --  input of MD restart information  ##
c     ##                                                           ##
c     ###############################################################
c
c
c     "readdyn" get the positions, velocities and accelerations
c     for a molecular dynamics restart from an external disk file
c
c
      subroutine readdyn (idyn)
      implicit none
      include 'sizes.i'
      include 'atoms.i'
      include 'boxes.i'
      include 'files.i'
      include 'iounit.i'
      include 'moldyn.i'
      integer i,idyn,ndyn
      character*60 dynfile
      character*80 record
      logical exist,opened,quit
c
c
c     open the input file if it has not already been done
c
      inquire (unit=idyn,opened=opened)
      if (.not. opened) then
         dynfile = filename(1:leng)//'.dyn'
         call version (dynfile,'old')
         inquire (file=dynfile,exist=exist)
         if (exist) then
            open (unit=idyn,file=dynfile,status='old')
            rewind (unit=idyn)
         else
            write (iout,10)
   10       format (/,' READDYN  --  Unable to Open Dynamics',
     &                 ' Restart File')
            call fatal
         end if
      end if
c
c     initialize error handling during reading of the file
c
      i = 0
      quit = .true.
c
c     get the number of atoms and check for consistency
c
      read (idyn,20)
   20 format ()
      read (idyn,30)  record
   30 format (a80)
      read (record,*,err=160,end=160)  ndyn
      if (ndyn .ne. n) then
         write (iout,40)
   40    format (/,' READDYN  --  Restart File has Incorrect',
     &              ' Number at Atoms')
         call fatal
      end if
c
c     get the periodic box edge lengths and angles
c
      read (idyn,50)
   50 format ()
      read (idyn,60)  record
   60 format (a80)
      read (record,*,err=160,end=160)  xbox,ybox,zbox
      read (idyn,70)  record
   70 format (a80)
      read (record,*,err=160,end=160)  alpha,beta,gamma
      read (idyn,80)
   80 format ()
c
c     get the atomic positions, velocities and accelerations
c
      quit = .true.
      do i = 1, n
         read (idyn,90)  record
   90    format (a80)
         read (record,*,err=160,end=160)  x(i),y(i),z(i)
      end do
      read (idyn,100)
  100 format ()
      do i = 1, n
         read (idyn,110)  record
  110    format (a80)
         read (record,*,err=160,end=160)  v(1,i),v(2,i),v(3,i)
      end do
      read (idyn,120)
  120 format ()
      do i = 1, n
         read (idyn,130)  record
  130    format (a80)
         read (record,*,err=160,end=160)  a(1,i),a(2,i),a(3,i)
      end do
      read (idyn,140)
  140 format ()
      do i = 1, n
         read (idyn,150)  record
  150    format (a80)
         read (record,*,err=160,end=160)  a_old(1,i),a_old(2,i),
     &                                    a_old(3,i)
      end do
      quit = .false.
  160 continue
      if (.not. opened)  close (unit=idyn)
c
c     report any error in reading the dynamics restart file
c
      if (quit) then
         write (iout,170)  i
  170    format (/,' READDYN  --  Error in Dynamics Restart',
     &              ' File at Atom',i6)
         call fatal
      end if
      return
      end
