    PROGRAM d03ecfe

!      D03ECF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : d03ecf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER   :: one = 1.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER   :: two = 2.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER   :: zero = 0.0_nag_wp
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: aparam, conchn, conres, root2, x1,   &
                                          x2, y1, y2, yy, z1, z2
       INTEGER                         :: i, ifail, itcoun, itmax, itused,     &
                                          ixn, iyn, izn, j, k, lda, n1, n2,    &
                                          n3, ndir, sda
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:,:), b(:,:,:), c(:,:,:),        &
                                          chngs(:), d(:,:,:), e(:,:,:),        &
                                          f(:,:,:), g(:,:,:), q(:,:,:),        &
                                          resids(:), t(:,:,:), wrksp1(:,:,:),  &
                                          wrksp2(:,:,:), wrksp3(:,:,:),        &
                                          wrksp4(:,:,:), x(:), y(:), z(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          cos, exp, sqrt
!      .. Executable Statements ..
       WRITE (nout,*) 'D03ECF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n1, n2, n3, itmax
       lda = n1
       sda = n2
       ALLOCATE (a(lda,sda,n3),b(lda,sda,n3),c(lda,sda,n3),chngs(itmax), &
          d(lda,sda,n3),e(lda,sda,n3),f(lda,sda,n3),g(lda,sda,n3), &
          q(lda,sda,n3),resids(itmax),t(lda,sda,n3),wrksp1(lda,sda,n3), &
          wrksp2(lda,sda,n3),wrksp3(lda,sda,n3),wrksp4(lda,sda,n3),x(n1), &
          y(n2),z(n3))

       READ (nin,*) x(1:n1)
       READ (nin,*) y(1:n2)
       READ (nin,*) z(1:n3)
       READ (nin,*) conres, conchn
       READ (nin,*) ndir
       root2 = sqrt(two)
       aparam = one
       itcoun = 0
!      Set up difference equation coefficients, source terms and
!      initial approximation.
       a(1:n1,1:n2,1:n3) = zero
       b(1:n1,1:n2,1:n3) = zero
       c(1:n1,1:n2,1:n3) = zero
       d(1:n1,1:n2,1:n3) = zero
       e(1:n1,1:n2,1:n3) = zero
       f(1:n1,1:n2,1:n3) = zero
       g(1:n1,1:n2,1:n3) = zero
       q(1:n1,1:n2,1:n3) = zero
       t(1:n1,1:n2,1:n3) = zero
!      Non-zero Specification for internal nodes
       DO k = 2, n3 - 1
          DO j = 2, n2 - 1
             DO i = 2, n1 - 1
                a(i,j,k) = two/((z(k)-z(k-1))*(z(k+1)-z(k-1)))
                g(i,j,k) = two/((z(k+1)-z(k))*(z(k+1)-z(k-1)))
                b(i,j,k) = two/((y(j)-y(j-1))*(y(j+1)-y(j-1)))
                f(i,j,k) = two/((y(j+1)-y(j))*(y(j+1)-y(j-1)))
                c(i,j,k) = two/((x(i)-x(i-1))*(x(i+1)-x(i-1)))
                e(i,j,k) = two/((x(i+1)-x(i))*(x(i+1)-x(i-1)))
                d(i,j,k) = -a(i,j,k) - b(i,j,k) - c(i,j,k) - e(i,j,k) - &
                   f(i,j,k) - g(i,j,k)
             END DO
          END DO
       END DO
!      Non-zero specification for boundary nodes
       yy = one/y(n2)
       x1 = (x(1)+one)*yy
       x2 = (x(n1)+one)*yy
       DO j = 1, n2
          y1 = root2*y(j)*yy
          q(1,j,1:n3) = exp(x1)*cos(y1)*exp((-z(1:n3)-one)*yy)
          q(n1,j,1:n3) = exp(x2)*cos(y1)*exp((-z(1:n3)-one)*yy)
       END DO
       y1 = root2*y(1)*yy
       y2 = root2*y(n2)*yy
       DO i = 1, n1
          x1 = (x(i)+one)*yy
          q(i,1,1:n3) = exp(x1)*cos(y1)*exp((-z(1:n3)-one)*yy)
          q(i,n2,1:n3) = exp(x1)*cos(y2)*exp((-z(1:n3)-one)*yy)
       END DO
       z1 = (-z(1)-one)*yy
       z2 = (-z(n3)-one)*yy
       DO i = 1, n1
          x1 = (x(i)+one)*yy
          q(i,1:n2,1) = exp(x1)*cos(root2*y(1:n2)*yy)*exp(z1)
          q(i,1:n2,n3) = exp(x1)*cos(root2*y(1:n2)*yy)*exp(z2)
       END DO

!      ifail: behaviour on error exit   
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft       
       ifail = 0
       CALL d03ecf(n1,n2,n3,lda,sda,a,b,c,d,e,f,g,q,t,aparam,itmax,itcoun, &
          itused,ndir,ixn,iyn,izn,conres,conchn,resids,chngs,wrksp1,wrksp2, &
          wrksp3,wrksp4,ifail)

       WRITE (nout,*) 'Iteration      Maximum        Maximum'
       WRITE (nout,*) ' number        residual        change'
       IF (itused/=0) THEN
          WRITE (nout,99999) (i,resids(i),chngs(i),i=1,itused)
       END IF
       WRITE (nout,*)
       WRITE (nout,*) 'Table of calculated function values'
       WRITE (nout,*)
       WRITE (nout,99998)
       DO k = 1, n3
          DO j = 1, n2
             WRITE (nout,99997) k, j, (i,t(i,j,k),i=1,n1)
          END DO
       END DO

99999  FORMAT (2X,I3,9X,E11.4,4X,E11.4)
99998  FORMAT (1X,'K  J  ',4(' (I      T   )'))
99997  FORMAT (1X,I1,2X,I1,1X,4(1X,I3,2X,F8.3))
    END PROGRAM d03ecfe
