      subroutine odestep( r, t, t2, nStts, dtmin)

      integer n
      parameter( n = 6 )

      integer nStts
      double precision r(n), t, t2
c      double precision dtmin

c
c     Integrates an ode from time t1 to t2 maintaining relative 
c     accuracy eps.
c
      double precision eps
      common /eps/ eps

      double precision timet1, timet2, currentdeltat, 
     & guessdeltat, totalnits, dt

      common /times/ timet1, timet2, currentdeltat, 
     & guessdeltat, totalnits, dt

      integer  field_type, derivative_type
      common /setup/  field_type, derivative_type

      integer seed1d, seed2d, seed3d
      common /varseed/ seed1d, seed2d, seed3d

      logical debug
      common /debug/ debug
    
      integer MAXSTP
      parameter( MAXSTP = 1000000000 )
c
      double precision TINY
      parameter( TINY = 1.d-30 )
c
      double precision drdt(n), rscal(n), dtdid, dtnext
      integer i, nstp
c
      nok = 0
      nbad = 0
   
      timet1 = t
      timet2 = t2

      nn = n

      if (derivative_type .eq. 4) then
       seed1d = int( r(4) )
         seed2d = int( r(5) )
         seed3d = int( r(6) )
         nn = 3
      endif

      do nstp = 1, MAXSTP
         
         call derivs(t, r, drdt, nStts)
         
         if(nStts .eq. 0) return
         
c     
c     The accuracy criterion
c     
         do i = 1, nn
            rscal(i) = dabs( r(i) ) + dabs( dt* drdt(i) )+ TINY
         enddo
c
c       if stepsize can overshoot decrease it
c

         if ( t+dt .gt. t2 ) dt = t2-t

         call rkqcs( r, drdt, t, dt, eps, rscal, dtdid, dtnext, nn, 
     &        nStts)

         currentdeltat = dtdid

         if(nStts .eq. 0)return
         if ( dtdid .eq. dt ) then
            nok = nok+1
         else
            nbad = nbad+1
         endif
c     
c     Check to see if we are done and finish off
c     
         if ( t - t2 .ge. 0.0d0 ) then

c     This is the end condition for the loop
            return
         endif
c     
      if ( dabs(dtnext) .lt. dtmin ) then
c         call error_printf('Step Size Underflow in ODESTEP.FOR\n')
      endif
c
         dt = dtnext
c     
      enddo
c     
c     Should never complete loop!
      
      return
      end


