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

    MODULE d02pzfe_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: neq = 4, nin = 5, nout = 6
       INTEGER, PARAMETER                  :: lenwrk = 32*neq
    CONTAINS
!dec$ attributes offload : mic :: f
       SUBROUTINE f(t,y,yp)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: t
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: y(*)
          REAL (KIND=nag_wp), INTENT (OUT)    :: yp(*)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: r
!         .. Intrinsic Functions ..
          INTRINSIC                              sqrt
!         .. Executable Statements ..
          r = sqrt(y(1)**2+y(2)**2)
          yp(1) = y(3)
          yp(2) = y(4)
          yp(3) = -y(1)/r**3
          yp(4) = -y(2)/r**3
          RETURN
!dec$ attributes offload : mic :: f
       END SUBROUTINE f
    END MODULE d02pzfe_mod

    PROGRAM d02pzfe

!      D02PZF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : d02pcf, d02pvf, d02pyf, d02pzf, nag_wp
       USE d02pzfe_mod, ONLY : f, lenwrk, neq, nin, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: errmax, hnext, hstart, tend,     &
                                              terrmx, tgot, tol, tstart,       &
                                              twant, waste
       INTEGER                             :: ifail, method, stpcst, stpsok,   &
                                              totf
       LOGICAL                             :: errass
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: rmserr(:), thres(:), work(:),    &
                                              ygot(:), ymax(:), ypgot(:),      &
                                              ystart(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'D02PZF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
!      neq: number of differential equations
       READ (nin,*) method
       ALLOCATE (rmserr(neq),thres(neq),work(lenwrk),ygot(neq),ymax(neq), &
          ypgot(neq),ystart(neq))

!      Set initial conditions and input for D02PVF

       READ (nin,*) tstart, tend
       READ (nin,*) ystart(1:neq)
       READ (nin,*) hstart, tol
       READ (nin,*) thres(1:neq)
       READ (nin,*) errass

!      ifail: behaviour on error exit   
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL d02pvf(neq,tstart,ystart,tend,tol,thres,method,'Usual Task', &
          errass,hstart,work,lenwrk,ifail)

       WRITE (nout,99999) tol
       WRITE (nout,99998)
       WRITE (nout,99997) tstart, ystart(1:neq)

       twant = tend

INTEG: DO
          ifail = -1
          CALL d02pcf(f,twant,tgot,ygot,ypgot,ymax,work,ifail)

          IF (ifail<2 .OR. ifail>4) THEN
             EXIT INTEG
          END IF

       END DO INTEG

       IF (ifail==0) THEN

!         Print solution.
          WRITE (nout,99997) tgot, ygot(1:neq)

!         Compute and print error estimates.
          ifail = 0
          CALL d02pzf(rmserr,errmax,terrmx,work,ifail)

          WRITE (nout,99996) rmserr(1:neq)
          WRITE (nout,99995) errmax, terrmx

          ifail = 0
          CALL d02pyf(totf,stpcst,waste,stpsok,hnext,ifail)

          WRITE (nout,99994) totf
       END IF

99999  FORMAT (/' Calculation with TOL = ',E8.1)
99998  FORMAT (/'    t         y1         y2         y3         y4'/)
99997  FORMAT (1X,F6.3,4(3X,F8.4))
99996  FORMAT (/' Componentwise error assessment'/9X,4(2X,E9.2))
99995  FORMAT (/' Worst global error observed was ',E9.2, &
          ' - it occurred at T = ',F6.3)
99994  FORMAT (/' Cost of the integration in evaluations of F is',I6)
    END PROGRAM d02pzfe
