!   D02PSF Example Program Text
!   Mark 26.1 Release. NAG Copyright 2016.

    Module d02psfe_mod

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

!     .. Use Statements ..
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: f
!     .. Parameters ..
      Real (Kind=nag_wp), Parameter, Public :: tol0 = 1.0E-3_nag_wp
      Integer, Parameter, Public       :: n = 2, nin = 5, nout = 6, npts = 16, &
                                          nwant = 1
      Integer, Parameter, Public       :: lrwsav = 350 + 32*n
    Contains
      Subroutine f(t,n,y,yp,iuser,ruser)

!       .. Scalar Arguments ..
        Real (Kind=nag_wp), Intent (In) :: t
        Integer, Intent (In)           :: n
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Inout) :: ruser(*)
        Real (Kind=nag_wp), Intent (In) :: y(n)
        Real (Kind=nag_wp), Intent (Out) :: yp(n)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Executable Statements ..
        yp(1) = y(2)
        yp(2) = -y(1)
        Return
      End Subroutine f
    End Module d02psfe_mod

    Program d02psfe

!     D02PSF Example Main Program

!     .. Use Statements ..
      Use d02psfe_mod, Only: f, lrwsav, n, nin, nout, npts, nwant, tol0
      Use nag_library, Only: d02pff, d02pqf, d02psf, d02ptf, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: hnext, hstart, tend, tinc, tnow,     &
                                          tol, tstart, twant, waste
      Integer                          :: fevals, i, ideriv, ifail, lwcomm,    &
                                          method, stepcost, stepsok
!     .. Local Arrays ..
      Real (Kind=nag_wp)               :: ruser(1), thresh(n), yinit(n),       &
                                          ynow(n), ypnow(n), ypwant(nwant),    &
                                          ywant(nwant)
      Real (Kind=nag_wp), Allocatable  :: rwsav(:), wcomm(:)
      Integer                          :: iuser(1), iwsav(130)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: real
!     .. Executable Statements ..
      Write (nout,*) 'D02PSF Example Program Results'
!     Skip heading in data file
      Read (nin,*)

      lwcomm = n + 5*nwant
      Allocate (rwsav(lrwsav),wcomm(lwcomm))

      wcomm(1:lwcomm) = 0.0_nag_wp

!     Set initial conditions and input for D02PQF

      Read (nin,*) method
      Read (nin,*) tstart, tend
      Read (nin,*) yinit(1:n)
      Read (nin,*) hstart
      Read (nin,*) thresh(1:n)

!     Set output control

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

      tol = tol0*10.0_nag_wp
      Do i = 1, 2
        tol = tol*0.1_nag_wp

!       Set up integration.
        ifail = 0
        Call d02pqf(n,tstart,tend,yinit,tol,thresh,method,hstart,iwsav,rwsav,  &
          ifail)

        Write (nout,99999) tol
        Write (nout,99998)
        Write (nout,99997) tstart, yinit(1:n)

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

integ:  Do While (tnow<tend)

!         Integrate one step to tnow.
          ifail = 0
          Call d02pff(f,n,tnow,ynow,ypnow,iuser,ruser,iwsav,rwsav,ifail)

!         Interpolate at required additional points up to tnow.
interp:   Do While (twant<=tnow)

!           Interpolate and print solution at t = twant.

            ideriv = 2
            ifail = 0
            Call d02psf(n,twant,ideriv,nwant,ywant,ypwant,f,wcomm,lwcomm,      &
              iuser,ruser,iwsav,rwsav,ifail)
            Write (nout,99997) twant, ywant(1), ypwant(1)

!           Set next required solution point
            twant = twant + tinc
          End Do interp

        End Do integ

!       Get integration statistics.
        ifail = 0
        Call d02ptf(fevals,stepcost,waste,stepsok,hnext,iwsav,rwsav,ifail)

        Write (nout,99996) fevals

      End Do
99999 Format (/,' Calculation with TOL = ',1P,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 d02psfe