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

    MODULE d02mzfe_mod

!      D02MZF 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       :: tstep = 0.02_nag_wp
       INTEGER, PARAMETER                  :: iset = 1, neq = 3, nin = 5,      &
                                              nout = 6
       INTEGER, PARAMETER                  :: nrw = 50 + 4*neq
       INTEGER, PARAMETER                  :: nwkjac = neq*(neq+1)
       INTEGER, PARAMETER                  :: sdysav = 6
       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)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: alpha = 0.04_nag_wp
          REAL (KIND=nag_wp)                  :: beta = 1.0E4_nag_wp
          REAL (KIND=nag_wp)                  :: gamma = 3.0E7_nag_wp
!         .. Executable Statements ..
          r(1:3) = -ydot(1:3)
          IF (ires==1) THEN
             r(1) = -alpha*y(1) + beta*y(2)*y(3) + 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 d02mzfe_mod

    PROGRAM d02mzfe

!      D02MZF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : d02mzf, d02nby, d02ngf, d02ngz, d02nsf, d02nvf, &
                               d02nyf, x04abf
       USE d02mzfe_mod, ONLY : iset, ldysav, nag_wp, neq, nin, nout, nrw,      &
                               nwkjac, resid, sdysav, tstep
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: h, h0, hmax, hmin, hu, t, tcrit, &
                                              tcur, tolsf, tout, xout
       INTEGER                             :: i, ifail, imxer, iout, itask,    &
                                              itol, itrace, maxord, maxstp,    &
                                              mxhnil, niter, nje, nq, nqu,     &
                                              nre, nst, outchn, pstat
       LOGICAL                             :: petzld
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: atol(:), rtol(:), rwork(:),      &
                                              sol(:), wkjac(:), y(:), ydot(:), &
                                              ysav(:,:)
       REAL (KIND=nag_wp)                  :: con(6)
       INTEGER                             :: inform(23)
       LOGICAL, ALLOCATABLE                :: algequ(:)
       LOGICAL                             :: lderiv(2)
!      .. Executable Statements ..
       WRITE (nout,*) 'D02MZF Example Program Results'
!      Skip heading in data file
       READ (nin,*)

!      Allocations based on number of differential equations (neq)
       ALLOCATE (atol(neq),rtol(neq),rwork(nrw),sol(neq),wkjac(nwkjac),y(neq), &
          ydot(neq),ysav(ldysav,sdysav),algequ(neq))

!      Read algorithmic parameters
       READ (nin,*) maxord, maxstp, mxhnil, pstat
       READ (nin,*) petzld
       READ (nin,*) hmin, hmax, h0, tcrit
       READ (nin,*) t, tout
       READ (nin,*) itol
       READ (nin,*) y(1:neq)
       READ (nin,*) lderiv(1:2)
       READ (nin,*) rtol(1:neq)
       READ (nin,*) atol(1:neq)

       outchn = nout
       CALL x04abf(iset,outchn)
       con(1:6) = 0.0_nag_wp
       itask = 2
       itrace = 0

!      ifail: behaviour on error exit   
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL d02nvf(neq,sdysav,maxord,'Functional-iteration',petzld,con,tcrit, &
          hmin,hmax,h0,maxstp,mxhnil,'Average-L2',rwork,ifail)

!      Linear algebra setup.
       ifail = 0
       CALL d02nsf(neq,neq,'Numerical',nwkjac,rwork,ifail)


       WRITE (nout,99994) (i,i=1,neq)
       WRITE (nout,99999) t, y(1:neq)

!      First value of t to interpolate and print solution at.
       iout = 1
       xout = tstep

!      Integrate one step at a time and overshoot tout (itask=2).
STEPS: DO
          ifail = 0
          CALL d02ngf(neq,ldysav,t,tout,y,ydot,rwork,rtol,atol,itol,inform, &
             resid,ysav,sdysav,d02ngz,wkjac,nwkjac,d02nby,lderiv,itask,itrace, &
             ifail)

!         Get Current value of t (tcur) and last step used (hu)
          CALL d02nyf(neq,neq,hu,h,tcur,tolsf,rwork,nst,nre,nje,nqu,nq,niter, &
             imxer,algequ,inform,ifail)

INTERP:   DO
             IF (tcur-hu<xout .AND. xout<=tcur) THEN
!               xout is in interval of last step, so interpolate.
                ifail = 0
                CALL d02mzf(xout,sol,neq,ldysav,neq,ysav,sdysav,rwork,ifail)

                WRITE (nout,99999) xout, sol(1:neq)
                iout = iout + 1
                IF (iout>=6) THEN
!                  Final solution point printed. Print stats if required.
                   IF (pstat==1) THEN
                      WRITE (nout,*)
                      WRITE (nout,99998) hu, h, tcur
                      WRITE (nout,99997) nst, nre, nje
                      WRITE (nout,99996) nqu, nq, niter
                      WRITE (nout,99995) ' Max err comp = ', imxer
                   END IF
                   EXIT STEPS
                END IF
!               Set next xout. This might also be in last step, so keep
!               looping for interpolation.
                xout = xout + tstep
                CYCLE INTERP
             ELSE
!               Take another step.
                CYCLE STEPS
             END IF
          END DO INTERP
       END DO STEPS

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