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

    MODULE d02pxfe_mod

!      D02PXF 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       :: tol1 = 1.0E-3_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: tol2 = 1.0E-4_nag_wp
       INTEGER, PARAMETER                  :: neq = 2, nin = 5, nout = 6,      &
                                              npts = 16
       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(*)
!         .. Executable Statements ..
          yp(1) = y(2)
          yp(2) = -y(1)
          RETURN
!dec$ attributes offload : mic :: f
       END SUBROUTINE f
    END MODULE d02pxfe_mod

    PROGRAM d02pxfe

!      D02PXF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : d02pdf, d02pvf, d02pxf, d02pyf, nag_wp
       USE d02pxfe_mod, ONLY : f, lenwrk, neq, nin, nout, npts, tol1, tol2
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: hnext, hstart, tend, tinc, tnow, &
                                              tol, tstart, twant, waste
       INTEGER                             :: i, ifail, lenint, method, nwant, &
                                              stpcst, stpsok, totf
       LOGICAL                             :: errass
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: thres(:), work(:), wrkint(:),    &
                                              ynow(:), ypnow(:), ypwant(:),    &
                                              ystart(:), ywant(:)
!      .. Intrinsic Functions ..
       INTRINSIC                              real
!      .. Executable Statements ..
       WRITE (nout,*) 'D02PXF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
!      neq: number of differential equations
       READ (nin,*) method, nwant
       lenint = neq + 5*nwant
       ALLOCATE (thres(neq),work(lenwrk),wrkint(lenint),ynow(neq),ypnow(neq), &
          ypwant(nwant),ystart(neq),ywant(nwant))

!      Set initial conditions and input for D02PVF

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

!      Set output control

       tinc = (tend-tstart)/real(npts,kind=nag_wp)

       DO i = 1, 2
          IF (i==1) tol = tol1
          IF (i==2) tol = tol2

!         Set up integration.
          ifail = 0
          CALL d02pvf(neq,tstart,ystart,tend,tol,thres,method,'Complex Task', &
             errass,hstart,work,lenwrk,ifail)

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

!         Set up first point at which solution is desired.
          twant = tstart + tinc
          tnow = tstart

!         Integrate by steps until tend is reached or error is encountered.

INTEGR8:  DO
             IF (tnow>=tend) EXIT INTEGR8

!            Integrate one step to tnow.
             ifail = -1
             CALL d02pdf(f,tnow,ynow,ypnow,work,ifail)
             IF (ifail/=0) EXIT INTEGR8

!            Interpolate at required additional points up to tnow.
INTERPOL8:   DO
                IF (twant>tnow) EXIT INTERPOL8

!               Interpolate and print solution at t = twant.
                ifail = 0
                CALL d02pxf(twant,'Both',nwant,ywant,ypwant,f,work,wrkint, &
                   lenint,ifail)
                WRITE (nout,99997) twant, ywant(1), ypwant(1)

!               Set next required solution point
                twant = twant + tinc
             END DO INTERPOL8

          END DO INTEGR8

!         Get integration statistics.
          ifail = 0
          CALL d02pyf(totf,stpcst,waste,stpsok,hnext,ifail)

          WRITE (nout,99996) totf

       END DO
99999  FORMAT (/' Calculation with TOL = ',E8.1)
99998  FORMAT (/'    t         y1         y1'''/)
99997  FORMAT (1X,F6.3,2(3X,F8.4))
99996  FORMAT (/' Cost of the integration in evaluations of F is',I6)

    END PROGRAM d02pxfe
