!   D02PXF Example Program Text
!   Mark 24 Release. NAG Copyright 2012.

    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
      Subroutine f(t,y,yp)

!       .. 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
      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 Procedures ..
      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
