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

    MODULE d02pwfe_mod

!      D02PWF 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-4_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: tol2 = 1.0E-5_nag_wp
       INTEGER, PARAMETER                  :: neq = 4, nin = 5, nout = 6,      &
                                              npts = 6
       INTEGER, PARAMETER                  :: lenwrk = 32*neq
    CONTAINS
!dec$ attributes offload : mic :: f
       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(*)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: r
!         .. Intrinsic Functions ..
          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
!dec$ attributes offload : mic :: f
       END SUBROUTINE f
    END MODULE d02pwfe_mod

    PROGRAM d02pwfe

!      D02PWF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : d02pdf, d02pvf, d02pwf, d02pyf, nag_wp
       USE d02pwfe_mod, ONLY : f, lenwrk, neq, nin, nout, npts, tol1, tol2
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: hnext, hstart, tendnu, tfinal,   &
                                              tinc, tnow, tol, tstart, waste
       INTEGER                             :: i, ifail, method, stpcst,        &
                                              stpsok, totf
       LOGICAL                             :: errass
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: thres(:), work(:), ynow(:),      &
                                              ypnow(:), ystart(:)
!      .. Intrinsic Functions ..
       INTRINSIC                              real
!      .. Executable Statements ..
       WRITE (nout,*) 'D02PWF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
!      neq: number of differential equations
       READ (nin,*) method
       ALLOCATE (thres(neq),work(lenwrk),ynow(neq),ypnow(neq),ystart(neq))

!      Set initial conditions and input for D02PVF

       READ (nin,*) tstart, tfinal
       READ (nin,*) ystart(1:neq)
       READ (nin,*) hstart
       READ (nin,*) thres(1:neq)
       READ (nin,*) errass

!      Set output control

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

       DO i = 1, 2
          IF (i==1) tol = tol1
          IF (i==2) tol = tol2
          tendnu = tstart + tinc

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

          WRITE (nout,99999) tol
          WRITE (nout,99998)
          WRITE (nout,99997) tstart, ystart(1:neq)

LOOP:     DO
             ifail = 0
             CALL d02pdf(f,tnow,ynow,ypnow,work,ifail)

             IF (tnow>=tendnu) THEN
                WRITE (nout,99997) tnow, ynow(1:neq)

                IF (tnow>=tfinal) EXIT LOOP
                tendnu = tendnu + tinc
                CALL d02pwf(tendnu,ifail)
             END IF

          END DO LOOP

          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         y2         y3         y4'/)
99997  FORMAT (1X,F6.3,4(3X,F8.4))
99996  FORMAT (/' Cost of the integration in evaluations of F is',I6)

    END PROGRAM d02pwfe
