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

    MODULE f12fafe_mod

!      F12FAF 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       :: one = 1.0_nag_wp
       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
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: nx
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x(nx)
          REAL (KIND=nag_wp), INTENT (OUT)    :: y(nx)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: dd, dl, du
          INTEGER                             :: j
!         .. Executable Statements ..
          dd = 4.0_nag_wp
          dl = -one
          du = -one
          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 : daxpy, dscal
!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: nx
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: v(nx*nx)
          REAL (KIND=nag_wp), INTENT (OUT)    :: w(nx*nx)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: h2
          INTEGER                             :: j, lo, n
!         .. Intrinsic Functions ..
          INTRINSIC                              real
!         .. Executable Statements ..
          h2 = one/real((nx+1)*(nx+1),kind=nag_wp)
          CALL tv(nx,v(1),w(1))
!         The NAG name equivalent of daxpy is f06ecf
          CALL daxpy(nx,-one,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 daxpy(nx,-one,v(lo-nx+1),1,w(lo+1),1)
             CALL daxpy(nx,-one,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 daxpy(nx,-one,v(lo-nx+1),1,w(lo+1),1)
          n = nx*nx
!         The NAG name equivalent of dscal is f06edf
          CALL dscal(n,one/h2,w,1)
          RETURN
!dec$ attributes offload : mic :: av
       END SUBROUTINE av
    END MODULE f12fafe_mod
    PROGRAM f12fafe

!      F12FAF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : dnrm2, f12faf, f12fbf, f12fcf, f12fdf, f12fef
       USE f12fafe_mod, ONLY : av, imon, ipoint, licomm, nag_wp, nin, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: sigma
       INTEGER                             :: ifail, irevcm, j, lcomm, ldv, n, &
                                              nconv, ncv, nev, niter, nshift, nx
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: ax(:), comm(:), d(:,:), mx(:),   &
                                              resid(:), v(:,:), x(:)
       INTEGER                             :: icomm(licomm)
!      .. Executable Statements ..
       WRITE (nout,*) 'F12FAF 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 + ncv*ncv + 8*ncv + 60
       ALLOCATE (ax(n),comm(lcomm),d(ncv,2),mx(n),resid(n),v(ldv,ncv),x(n))

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

       ifail = 0
       CALL f12fdf('SMALLEST MAGNITUDE',icomm,comm,ifail)
!      Increase the iteration limit if required.
       CALL f12fdf('ITERATION LIMIT=500',icomm,comm,ifail)
       IF (ipoint==1) THEN
!         Use pointers to Workspace in calculating matrix vector
!         products rather than interfacing through the array X.
          CALL f12fdf('POINTERS=YES',icomm,comm,ifail)
       END IF

       irevcm = 0
       ifail = -1
REVCM: DO
          CALL f12fbf(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 f12fef(niter,nconv,d,d(1,2),icomm,comm)
!            The NAG name equivalent of dnrm2 is f06ejf
             WRITE (6,99999) niter, nconv, dnrm2(nev,d(1,2),1)
          END IF
       END DO REVCM

       IF (ifail==0) THEN
!         Post-Process using F12FCF to compute eigenvalue/vectors.
          ifail = 0
          CALL f12fcf(nconv,d,v,ldv,sigma,resid,v,ldv,comm,icomm,ifail)
          WRITE (nout,99998) nconv
          WRITE (nout,99997) (j,d(j,1),j=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 smallest magnitude are:'/)
99997  FORMAT (1X,I8,5X,F12.4)
    END PROGRAM f12fafe
