    PROGRAM d03eafe

!      D03EAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : d03eaf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: alpha, alpha0, alpsav, p, q
       INTEGER                         :: i, ifail, j, ldc, m, n, n1, n1p1,    &
                                          np1, np4, npts
       LOGICAL                         :: dorm, ext, stage1
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: c(:,:), phi(:), phid(:), x(:), y(:)
       INTEGER, ALLOCATABLE            :: icint(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'D03EAF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) m, n
       n1 = 2*(n+m) - 2
       n1p1 = n1 + 1
       np1 = n + 1
       np4 = n + 4
       ldc = n + 1
       ALLOCATE (c(ldc,np4),phi(n),phid(n),x(n1p1),y(n1p1),icint(np1))
       READ (nin,*) ext, dorm
       IF ( .NOT. dorm) THEN
          READ (nin,*) alpha0
          alpha = alpha0
       END IF
       READ (nin,*) (x(i),y(i),i=1,n1+1)
       READ (nin,*) (phi(i),phid(i),i=1,n)

       stage1 = .TRUE.

!      ifail: behaviour on error exit   
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL d03eaf(stage1,ext,dorm,n,p,q,x,y,n1p1,phi,phid,alpha,c,ldc,np4, &
          icint,np1,ifail)

       alpsav = alpha
       WRITE (nout,*)
       IF ( .NOT. dorm) THEN
          IF (ext) THEN
             WRITE (nout,*) 'Exterior Neumann problem'
             WRITE (nout,*)
             WRITE (nout,99998) 'C=', alpha0
          ELSE
             WRITE (nout,*) 'Interior Neumann problem'
             WRITE (nout,*)
             WRITE (nout,99999) 'Total integral =', alpha0
          END IF
       ELSE IF (ext) THEN
          WRITE (nout,*)
          WRITE (nout,*) 'Exterior problem'
          WRITE (nout,*)
          WRITE (nout,99999) 'Computed C =', alpsav
       END IF
       j = 2
       WRITE (nout,*)
       WRITE (nout,*) 'Nodes'
       WRITE (nout,99996) 'X', 'Y', 'PHI', 'PHID'
       DO i = 1, n
          IF (x(j)==x(j-1) .AND. y(j)==y(j-1)) j = j + 2
          WRITE (nout,99997) x(j), y(j), phi(i), phid(i)
          j = j + 2
       END DO
       stage1 = .FALSE.
       WRITE (nout,*)
       WRITE (nout,*) 'Selected points'
       WRITE (nout,*) '           X              Y              PHI'
       READ (nin,*) npts
       DO i = 1, npts
          READ (nin,*) p, q
          alpha = alpsav

          ifail = 0
          CALL d03eaf(stage1,ext,dorm,n,p,q,x,y,n1p1,phi,phid,alpha,c,ldc,np4, &
             icint,np1,ifail)

          WRITE (nout,99997) p, q, alpha
       END DO

99999  FORMAT (1X,A,F15.2)
99998  FORMAT (1X,A,E15.4)
99997  FORMAT (1X,4F15.2)
99996  FORMAT (1X,A12,A15,A17,A16)
    END PROGRAM d03eafe
