!
  Module setting
   implicit none
   integer, parameter :: NEQN = 3
   real, save :: COFFd, COFFl, COFFb
  End module setting
!
  Program main

  Use setting
  implicit none

      REAL*8 :: T_step
      INTEGER :: iter_preview, iter_view

      REAL*8 :: Y(NEQN), WORK(3+6*NEQN)
      REAL*8 :: T1, T2, RELERR, ABSERR, R, D
      INTEGER :: IWORK(5), IFLAG
      EXTERNAL :: F

      INTEGER :: ii, jj
!
      WRITE(*,'(/,1x,"the program is running...",/)')
!
!     READ THE INITIAL DATAS
!
      OPEN(11,FILE='lorenz.dat',status='unknown')
      READ(11,*)
      READ(11,*) RELERR, ABSERR
      READ(11,*)
      READ(11,*) T_step
      READ(11,*)
      READ(11,*) iter_preview, iter_view
      READ(11,*)
      DO ii = 1,NEQN
        READ(11,*) Y(ii)
      ENDDO
      READ(11,*)
      READ(11,*) COFFd, COFFl, COFFb
      CLOSE(11)
!
         T1 = 0.0
      DO ii = 1, iter_preview
           IFLAG = 1
           T2 = T1+T_step

         CALL RKF45(F,NEQN,Y,T1,T2,RELERR,ABSERR,&
                    IFLAG,WORK,IWORK,R,D)

         IF (IFLAG .NE. 2) THEN
           PRINT*,'IFLAG=',IFLAG,' at the ',ii,' th step !'
         ENDIF

         T1 = T2
      ENDDO

      OPEN(200,FILE='data.m',status='unknown')
        WRITE(200,'(1x,"Y=[")')
      DO ii = 1, iter_view
           IFLAG = 1
           T2 = T1+T_step

         CALL RKF45(F,NEQN,Y,T1,T2,RELERR,ABSERR,&
                    IFLAG,WORK,IWORK,R,D)

         IF (IFLAG .NE. 2) THEN
           PRINT*,'IFLAG=',IFLAG,' at the ',ii,' th step !'
         ENDIF

        WRITE(200,'(4(1X,E24.16))') T2, (Y(jj),jj=1,NEQN)
         T1 = T2
      ENDDO
        WRITE(200,'("];")')  
        CLOSE(200)
!
  PRINT*,'THE PROGRAM IS TERMINATED!'
  end program main
