!   D02PUF Example Program Text
!   Mark 25 Release. NAG Copyright 2014.

    Module d02pufe_mod

!     D02PUF 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 ..
      Integer, Parameter, Public           :: liwsav = 130, n = 4, 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(*)
!       .. Local Scalars ..
        Real (Kind=nag_wp)                   :: r
!       .. Intrinsic Procedures ..
        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
      End Subroutine f
    End Module d02pufe_mod

    Program d02pufe

!     D02PUF Example Main Program

!     .. Use Statements ..
      Use nag_library, Only: d02pef, d02pqf, d02ptf, d02puf, nag_wp
      Use d02pufe_mod, Only: f, liwsav, lrwsav, n, nin, nout
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Real (Kind=nag_wp)                   :: errmax, hnext, hstart, tend,     &
                                              terrmx, tgot, tol, tstart,       &
                                              twant, waste
      Integer                              :: fevals, ifail, method, stepcost, &
                                              stepsok
!     .. Local Arrays ..
      Real (Kind=nag_wp)                   :: rmserr(n), ruser(1), thresh(n),  &
                                              ygot(n), yinit(n), ymax(n),      &
                                              ypgot(n)
      Real (Kind=nag_wp), Allocatable      :: rwsav(:)
      Integer                              :: iuser(1)
      Integer, Allocatable                 :: iwsav(:)
!     .. Executable Statements ..
      Write (nout,*) 'D02PUF Example Program Results'

      Allocate (rwsav(lrwsav),iwsav(liwsav))

!     Set initial conditions and input for D02PQF

!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) method
      Read (nin,*) tstart, tend
      Read (nin,*) yinit(1:n)
      Read (nin,*) hstart, tol
      Read (nin,*) thresh(1:n)

!     ifail: behaviour on error exit   
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
      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)

      twant = tend

integ: Do
        ifail = -1
        Call d02pef(f,n,twant,tgot,ygot,ypgot,ymax,iuser,ruser,iwsav,rwsav, &
          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:n)

!       Compute and print error estimates.
        ifail = 0
        Call d02puf(n,rmserr,errmax,terrmx,iwsav,rwsav,ifail)

        Write (nout,99996) rmserr(1:n)
        Write (nout,99995) errmax, terrmx

        ifail = 0
        Call d02ptf(fevals,stepcost,waste,stepsok,hnext,iwsav,rwsav,ifail)

        Write (nout,99994) fevals
      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 d02pufe