!   D02NHF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.

    MODULE d02nhfe_mod

!      D02NHF Example Program Module:
!             Parameters and User-defined Routines

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER       :: alpha = 0.04_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: beta = 1.0E4_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: gamma = 3.0E7_nag_wp
       INTEGER, PARAMETER                  :: iset = 1, itrace = 0, ml = 1,    &
                                              mu = 2, neq = 3, nin = 5
       INTEGER, PARAMETER                  :: njcpvt = neq
       INTEGER, PARAMETER                  :: nout = 6
       INTEGER, PARAMETER                  :: nrw = 50 + 4*neq
       INTEGER, PARAMETER                  :: nwkjac = neq*(2*ml+mu+1)
       INTEGER, PARAMETER                  :: ldysav = neq
    CONTAINS
!dec$ attributes offload : mic :: resid
       SUBROUTINE resid(neq,t,y,ydot,r,ires)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: t
          INTEGER, INTENT (INOUT)             :: ires
          INTEGER, INTENT (IN)                :: neq
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (OUT)    :: r(neq)
          REAL (KIND=nag_wp), INTENT (IN)     :: y(neq), ydot(neq)
!         .. Executable Statements ..
          r(1) = -ydot(1) - ydot(2) - ydot(3)
          r(2) = -ydot(2)
          r(3) = -ydot(3)
          IF (ires==1) THEN
             r(1) = r(1)
             r(2) = alpha*y(1) - beta*y(2)*y(3) - gamma*y(2)*y(2) + r(2)
             r(3) = gamma*y(2)*y(2) + r(3)
          END IF
          RETURN
!dec$ attributes offload : mic :: resid
       END SUBROUTINE resid
    END MODULE d02nhfe_mod

    PROGRAM d02nhfe

!      D02NHF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : d02nby, d02nhf, d02nhz, d02ntf, d02nvf, d02nyf, &
                               nag_wp, x04abf
       USE d02nhfe_mod, ONLY : iset, itrace, ldysav, ml, mu, neq, nin, njcpvt, &
                               nout, nrw, nwkjac, resid
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: h, h0, hmax, hmin, hu, t, tcrit, &
                                              tcur, tolsf, tout, tout1
       INTEGER                             :: i, ifail, imxer, itask, itol,    &
                                              maxord, maxstp, mxhnil, niter,   &
                                              nje, nq, nqu, nre, nst, outchn,  &
                                              sdysav
       LOGICAL                             :: petzld
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: atol(:), rtol(:), rwork(:),      &
                                              wkjac(:), y(:), ydot(:), ysav(:,:)
       REAL (KIND=nag_wp)                  :: con(6)
       INTEGER                             :: inform(23)
       INTEGER, ALLOCATABLE                :: jacpvt(:)
       LOGICAL, ALLOCATABLE                :: algequ(:)
       LOGICAL                             :: lderiv(2)
!      .. Executable Statements ..
       WRITE (nout,*) 'D02NHF Example Program Results'
       FLUSH (nout)

!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) maxord, maxstp, mxhnil
       sdysav = maxord + 1
       ALLOCATE (atol(neq),rtol(neq),rwork(nrw),wkjac(nwkjac),y(neq), &
          ydot(neq),ysav(ldysav,sdysav),jacpvt(njcpvt),algequ(neq))

       outchn = nout
       CALL x04abf(iset,outchn)

!      Set itask=6 to provide initial estimates of solution and its
!      time derivative. Default values for the array con are used.
!      Use the B.D.F. formulae with a Newton method.
!      Employ scalar relative tolerance and vector absolute tolerance.
!      The Jacobian is evaluated internally.
!      monitr subroutine replaced by NAG dummy routine D02NBY.

       READ (nin,*) hmin, hmax, h0, tcrit
       READ (nin,*) petzld
       READ (nin,*) t, tout1
       READ (nin,*) y(1:neq)
       READ (nin,*) lderiv(1:2)
       READ (nin,*) itol
       READ (nin,*) rtol(1)
       READ (nin,*) atol(1:neq)
       itask = 6
       tout = tout1
       con(1:6) = 0.0_nag_wp

       ifail = 0
       CALL d02nvf(neq,sdysav,maxord,'Newton',petzld,con,tcrit,hmin,hmax,h0, &
          maxstp,mxhnil,'Average-L2',rwork,ifail)

       ifail = 0
       CALL d02ntf(neq,neq,'Numerical',ml,mu,nwkjac,njcpvt,rwork,ifail)

!      Hard fail on initial and subsequent tasks.
       ifail = 0
       CALL d02nhf(neq,ldysav,t,tout,y,ydot,rwork,rtol,atol,itol,inform,resid, &
          ysav,sdysav,d02nhz,wkjac,nwkjac,jacpvt,njcpvt,d02nby,lderiv,itask, &
          itrace,ifail)

       WRITE (nout,*)
       WRITE (nout,99999) ' Initial    Y : ', y(1:neq)
       WRITE (nout,99999) ' Initial YDOT : ', ydot(1:neq)
       FLUSH (nout)

!      Use these initial estimates and integrate to tout (overshoot and
!      interpolate)
       itask = 1
       tout = tout1

       ifail = 0
       CALL d02nhf(neq,ldysav,t,tout,y,ydot,rwork,rtol,atol,itol,inform,resid, &
          ysav,sdysav,d02nhz,wkjac,nwkjac,jacpvt,njcpvt,d02nby,lderiv,itask, &
          itrace,ifail)

       WRITE (nout,99993) (i,i=1,neq)
       WRITE (nout,99998) tout, y(1:neq)

       ifail = 0
       CALL d02nyf(neq,neq,hu,h,tcur,tolsf,rwork,nst,nre,nje,nqu,nq,niter, &
          imxer,algequ,inform,ifail)

       WRITE (nout,*)
       WRITE (nout,99997) hu, h, tcur
       WRITE (nout,99996) nst, nre, nje
       WRITE (nout,99995) nqu, nq, niter
       WRITE (nout,99994) ' Max err comp = ', imxer

99999  FORMAT (1X,A,3(F11.4,2X))
99998  FORMAT (1X,F8.3,3(F13.5,2X))
99997  FORMAT (1X,' HUSED = ',E12.5,'  HNEXT = ',E12.5,'  TCUR = ',E12.5)
99996  FORMAT (1X,' NST = ',I6,'    NRE = ',I6,'    NJE = ',I6)
99995  FORMAT (1X,' NQU = ',I6,'    NQ  = ',I6,'  NITER = ',I6)
99994  FORMAT (1X,A,I4)
99993  FORMAT (/1X,'    X ',3('         Y(',I1,')  '))
    END PROGRAM d02nhfe
