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

    MODULE d02pcfe_mod

!      D02PCF 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 = 8
       INTEGER, PARAMETER                  :: lenwrk = 32*neq
    CONTAINS

       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
       END SUBROUTINE f
    END MODULE d02pcfe_mod

    PROGRAM d02pcfe

!      D02PCF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : d02pcf, d02pvf, d02pyf, nag_wp
       USE d02pcfe_mod, ONLY : f, lenwrk, neq, nin, nout, npts, tol1, tol2
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: hnext, hstart, tend, tgot, tinc, &
                                              tol, tstart, twant, waste
       INTEGER                             :: i, ifail, j, method, stpcst,     &
                                              stpsok, totf
       LOGICAL                             :: errass
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: thres(:), work(:), ygot(:),      &
                                              ymax(:), ypgot(:), ystart(:)
!      .. Intrinsic Functions ..
       INTRINSIC                              real
!      .. Executable Statements ..
       WRITE (nout,*) 'D02PCF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) method
       ALLOCATE (thres(neq),work(lenwrk),ygot(neq),ymax(neq),ypgot(neq), &
          ystart(neq))

!      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 control for output

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

LOOP:  DO i = 1, 2
          IF (i==1) THEN
             tol = tol1
          ELSE
             tol = tol2
          END IF

!         ifail: behaviour on error exit   
!                =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
          ifail = 0
          CALL d02pvf(neq,tstart,ystart,tend,tol,thres,method,'Usual Task', &
             errass,hstart,work,lenwrk,ifail)

          WRITE (nout,99999) tol
          WRITE (nout,99998)
          WRITE (nout,99997) tstart, ystart(1:neq)
          twant = tstart
          DO j = 1, npts
             twant = twant + tinc

             ifail = -1
             CALL d02pcf(f,twant,tgot,ygot,ypgot,ymax,work,ifail)

             WRITE (nout,99997) tgot, ygot(1:neq)
          END DO

          ifail = 0
          CALL d02pyf(totf,stpcst,waste,stpsok,hnext,ifail)
          WRITE (nout,99996) totf

       END DO LOOP

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