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


    MODULE f12anfe_mod

!      F12ANF Example Program Module:
!             Parameters and User-defined Routines

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: imon = 0, ipoint = 0,            &
                                              licomm = 140, nin = 5, nout = 6
    CONTAINS
!dec$ attributes offload : mic :: tv
       SUBROUTINE tv(nx,x,y)
!         Compute the matrix vector multiplication y<---T*x where T is a nx
!         by nx tridiagonal matrix.

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Parameters ..
          COMPLEX (KIND=nag_wp), PARAMETER    :: four = (4.0_nag_wp,0.0_nag_wp)
          COMPLEX (KIND=nag_wp), PARAMETER    :: half = (0.5_nag_wp,0.0_nag_wp)
          COMPLEX (KIND=nag_wp), PARAMETER    ::                               &
                                                rho = (100.0_nag_wp,0.0_nag_wp)
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: nx
!         .. Array Arguments ..
          COMPLEX (KIND=nag_wp), INTENT (IN)  :: x(nx)
          COMPLEX (KIND=nag_wp), INTENT (OUT) :: y(nx)
!         .. Local Scalars ..
          COMPLEX (KIND=nag_wp)               :: dd, dl, du, h, h2
          INTEGER                             :: j
!         .. Intrinsic Functions ..
          INTRINSIC                              cmplx
!         .. Executable Statements ..
          h = cmplx(nx+1,kind=nag_wp)
          h2 = h*h
          dd = four*h2
          dl = -h2 - half*rho*h
          du = -h2 + half*rho*h

          y(1) = dd*x(1) + du*x(2)
          DO j = 2, nx - 1
             y(j) = dl*x(j-1) + dd*x(j) + du*x(j+1)
          END DO
          y(nx) = dl*x(nx-1) + dd*x(nx)
          RETURN
!dec$ attributes offload : mic :: tv
       END SUBROUTINE tv
!dec$ attributes offload : mic :: av
       SUBROUTINE av(nx,v,w)

!         .. Use Statements ..
          USE nag_library, ONLY : zaxpy
!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: nx
!         .. Array Arguments ..
          COMPLEX (KIND=nag_wp), INTENT (IN)  :: v(nx*nx)
          COMPLEX (KIND=nag_wp), INTENT (OUT) :: w(nx*nx)
!         .. Local Scalars ..
          COMPLEX (KIND=nag_wp)               :: h2
          INTEGER                             :: j, lo
!         .. Intrinsic Functions ..
          INTRINSIC                              cmplx
!         .. Executable Statements ..
          h2 = cmplx(-(nx+1)*(nx+1),kind=nag_wp)

          CALL tv(nx,v(1),w(1))
!         The NAG name equivalent of zaxpy is f06gcf
          CALL zaxpy(nx,h2,v(nx+1),1,w(1),1)

          DO j = 2, nx - 1
             lo = (j-1)*nx
             CALL tv(nx,v(lo+1),w(lo+1))
             CALL zaxpy(nx,h2,v(lo-nx+1),1,w(lo+1),1)
             CALL zaxpy(nx,h2,v(lo+nx+1),1,w(lo+1),1)
          END DO

          lo = (nx-1)*nx
          CALL tv(nx,v(lo+1),w(lo+1))
          CALL zaxpy(nx,h2,v(lo-nx+1),1,w(lo+1),1)

          RETURN
!dec$ attributes offload : mic :: av
       END SUBROUTINE av
    END MODULE f12anfe_mod
    PROGRAM f12anfe

!      F12ANF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : dznrm2, f12anf, f12apf, f12aqf, f12arf, f12asf
       USE f12anfe_mod, ONLY : av, imon, ipoint, licomm, nag_wp, nin, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       COMPLEX (KIND=nag_wp)               :: sigma
       INTEGER                             :: i, ifail, ifail1, irevcm, lcomm, &
                                              ldv, n, nconv, ncv, nev, niter,  &
                                              nshift, nx
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE  :: ax(:), comm(:), d(:,:), mx(:),   &
                                              resid(:), v(:,:), x(:)
       INTEGER                             :: icomm(licomm)
!      .. Executable Statements ..
       WRITE (nout,*) 'F12ANF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) nx, nev, ncv

       n = nx*nx
       ldv = n
       lcomm = 3*n + 3*ncv*ncv + 5*ncv + 60
       ALLOCATE (ax(n),comm(lcomm),d(ncv,2),mx(n),resid(n),v(n,ncv),x(n))

       ifail = 0
       CALL f12anf(n,nev,ncv,icomm,licomm,comm,lcomm,ifail)

       IF (ipoint/=0) THEN
!         Use pointers to Workspace in calculating matrix vector
!         products rather than interfacing through the array X.
          ifail = 0
          CALL f12arf('POINTERS=YES',icomm,comm,ifail)
       END IF

       irevcm = 0
       ifail = -1

REVCM: DO
          CALL f12apf(irevcm,resid,v,ldv,x,mx,nshift,comm,icomm,ifail)
          IF (irevcm==5) THEN
             EXIT REVCM
          ELSE IF (irevcm==-1 .OR. irevcm==1) THEN
!            Perform matrix vector multiplication y <--- Op*x.
             IF (ipoint==0) THEN
                CALL av(nx,x,ax)
                x(1:n) = ax(1:n)
             ELSE
                CALL av(nx,comm(icomm(1)),comm(icomm(2)))
             END IF
          ELSE IF (irevcm==4 .AND. imon/=0) THEN
!            Output monitoring information.
             CALL f12asf(niter,nconv,d,d(1,2),icomm,comm)
!            The NAG name equivalent of dznrm2 is f06jjf
             WRITE (6,99999) niter, nconv, dznrm2(nev,d(1,2),1)
          END IF
       END DO REVCM

       IF (ifail==0) THEN
!         Post-Process using F12AQF to compute eigenvalues/vectors.
          ifail1 = 0
          CALL f12aqf(nconv,d,v,ldv,sigma,resid,v,ldv,comm,icomm,ifail1)

          WRITE (nout,99998) nconv
          WRITE (nout,99997) (i,d(i,1),i=1,nconv)
       END IF

99999  FORMAT (1X,'Iteration',1X,I3,', No. converged =',1X,I3,', norm o', &
          'f estimates =',E16.8)
99998  FORMAT (1X/' The ',I4,' Ritz values of largest magnitude are:'/)
99997  FORMAT (1X,I8,5X,'( ',F12.4,' , ',F12.4,' )')
    END PROGRAM f12anfe
