!   D02PFF Example Program Text
!   Mark 26 Release. NAG Copyright 2016.

    Module d02pffe_mod

!     D02PFF 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 :: tol1 = 1.0E-4_nag_wp
      Real (Kind=nag_wp), Parameter, Public :: tol2 = 1.0E-5_nag_wp
      Integer, Parameter, Public       :: liwsav = 130, n = 2, nin = 5,        &
                                          nout = 6
      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 d02pffe_mod

    Program d02pffe

!     D02PFF Example Main Program

!     .. Use Statements ..
      Use nag_library, Only: d02pff, d02pqf, d02ptf, nag_wp
      Use d02pffe_mod, Only: f, liwsav, lrwsav, n, nin, nout, tol1, tol2
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: hnext, hstart, tend, tnow, tol,      &
                                          tstart, waste
      Integer                          :: i, ifail, method, stpcst, stpsok,    &
                                          totf
!     .. Local Arrays ..
      Real (Kind=nag_wp)               :: ruser(1)
      Real (Kind=nag_wp), Allocatable  :: rwsav(:), thres(:), ynow(:),         &
                                          ypnow(:), ystart(:)
      Integer                          :: iuser(1)
      Integer, Allocatable             :: iwsav(:)
!     .. Executable Statements ..
      Write (nout,*) 'D02PFF Example Program Results'
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) method
      Allocate (thres(n),iwsav(liwsav),rwsav(lrwsav),ynow(n),ypnow(n),         &
        ystart(n))

!     Set initial conditions and input for D02PQF

      Read (nin,*) tstart, tend
      Read (nin,*) ystart(1:n)
      Read (nin,*) hstart
      Read (nin,*) thres(1:n)

      Do i = 1, 2
        If (i==1) Then
          tol = tol1
        End If
        If (i==2) Then
          tol = tol2
        End If

!       ifail: behaviour on error exit
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
        Call d02pqf(n,tstart,tend,ystart,tol,thres,method,hstart,iwsav,rwsav,  &
          ifail)

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

loop:   Do
          ifail = 0
          Call d02pff(f,n,tnow,ynow,ypnow,iuser,ruser,iwsav,rwsav,ifail)

          If (ifail==0) Then
            Write (nout,99997) tnow, ynow(1:n)
            If (tnow>=tend) Then
              Exit loop
            End If
          Else
            Exit loop
          End If

        End Do loop

        ifail = 0
        Call d02ptf(totf,stpcst,waste,stpsok,hnext,iwsav,rwsav,ifail)
        Write (nout,99996) totf
      End Do

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