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

    MODULE f12aafe_mod

!      F12AAF 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, 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 with constant diagonals (DD, DL and DU).

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Parameters ..
          REAL (KIND=nag_wp), PARAMETER       :: half = 0.5_nag_wp
          REAL (KIND=nag_wp), PARAMETER       :: rho = 100.0_nag_wp
!         .. 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, nx1, nx2
          INTEGER                             :: j
!         .. Intrinsic Functions ..
          INTRINSIC                              real
!         .. Executable Statements ..
          nx1 = real(nx+1,kind=nag_wp)
          nx2 = nx1*nx1
          dd = 4.0_nag_wp*nx2
          dl = -nx2 - half*rho*nx1
          du = -nx2 + half*rho*nx1
          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
!         .. 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)                  :: nx2
          INTEGER                             :: j, lo
!         .. Intrinsic Functions ..
          INTRINSIC                              real
!         .. Executable Statements ..
          nx2 = -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,nx2,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,nx2,v(lo-nx+1),1,w(lo+1),1)
             CALL daxpy(nx,nx2,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,nx2,v(lo-nx+1),1,w(lo+1),1)
          RETURN
!dec$ attributes offload : mic :: av
       END SUBROUTINE av
    END MODULE f12aafe_mod
    PROGRAM f12aafe

!      F12AAF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : dnrm2, f12aaf, f12abf, f12acf, f12adf, f12aef
       USE f12aafe_mod, ONLY : av, imon, ipoint, nag_wp, nin, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: sigmai, sigmar
       INTEGER                             :: i, ifail, ifail1, irevcm, lcomm, &
                                              ldv, licomm, n, nconv, ncv, nev, &
                                              niter, nshift, nx
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: ax(:), comm(:), d(:,:), mx(:),   &
                                              resid(:), v(:,:), x(:)
       INTEGER, ALLOCATABLE                :: icomm(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F12AAF 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 + 6*ncv + 60
       licomm = 140
       ALLOCATE (ax(n),comm(lcomm),d(ncv,3),mx(n),resid(n),v(ldv,ncv),x(n), &
          icomm(licomm))

!      ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL f12aaf(n,nev,ncv,icomm,licomm,comm,lcomm,ifail)

!      Set the region of the spectrum that is required.
       ifail = 0
       CALL f12adf('SMALLEST MAG',icomm,comm,ifail)

       IF (ipoint/=0) THEN

!         Use pointers to workspace in calculating matrix vector products
!         rather than interfacing through the array X.
          ifail = 0
          CALL f12adf('POINTERS=YES',icomm,comm,ifail)

       END IF

       irevcm = 0
       ifail = -1
LOOP:  DO
          CALL f12abf(irevcm,resid,v,ldv,x,mx,nshift,comm,icomm,ifail)

          IF (irevcm/=5) THEN
             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

!               Set IMON=1 to output monitoring information.
                CALL f12aef(niter,nconv,d,d(1,2),d(1,3),icomm,comm)

!               The NAG name equivalent of dnrm2 is f06ejf
                WRITE (6,99999) niter, nconv, dnrm2(nev,d(1,3),1)
             END IF
          ELSE
             EXIT LOOP
          END IF
       END DO LOOP

       IF (ifail==0) THEN

!         Post-Process using F12ACF to compute eigenvalues and
!         (by default) the corresponding eigenvectors.
          ifail1 = 0
          CALL f12acf(nconv,d,d(1,2),v,ldv,sigmar,sigmai,resid,v,ldv,comm, &
             icomm,ifail1)

          WRITE (nout,99998) nconv
          DO i = 1, nconv
             WRITE (nout,99997) i, d(i,1), d(i,2)
          END DO
       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,' , ',F12.4,' )')
    END PROGRAM f12aafe
