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

    MODULE f12fdfe_mod

!      F12FDF 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       :: four = 4.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: one = 1.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: six = 6.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                  :: imon = 0, ipoint = 1,            &
                                              licomm = 140, nin = 5, nout = 6
    CONTAINS
!dec$ attributes offload : mic :: mv
       SUBROUTINE mv(n,v,w)

!         .. Use Statements ..
          USE nag_library, ONLY : dscal
!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: v(n)
          REAL (KIND=nag_wp), INTENT (OUT)    :: w(n)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: h
          INTEGER                             :: j
!         .. Intrinsic Functions ..
          INTRINSIC                              real
!         .. Executable Statements ..
          h = one/(real(n+1,kind=nag_wp)*six)
          w(1) = four*v(1) + v(2)
          DO j = 2, n - 1
             w(j) = v(j-1) + four*v(j) + v(j+1)
          END DO
          j = n
          w(j) = v(j-1) + four*v(j)
!         The NAG name equivalent of dscal is f06edf
          CALL dscal(n,h,w,1)
          RETURN
!dec$ attributes offload : mic :: mv
       END SUBROUTINE mv
    END MODULE f12fdfe_mod
    PROGRAM f12fdfe

!      F12FDF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : dcopy, dgttrf, dgttrs, dnrm2, f12faf, f12fbf,   &
                               f12fcf, f12fdf, f12fef
       USE f12fdfe_mod, ONLY : four, imon, ipoint, licomm, mv, nag_wp, nin,    &
                               nout, one, six, two, zero
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: h, r1, r2, sigma
       INTEGER                             :: ifail, info, irevcm, j, lcomm,   &
                                              ldv, n, nconv, ncv, nev, niter,  &
                                              nshift
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: ad(:), adl(:), adu(:), adu2(:),  &
                                              comm(:), d(:,:), mx(:),          &
                                              resid(:), v(:,:), x(:)
       INTEGER                             :: icomm(licomm)
       INTEGER, ALLOCATABLE                :: ipiv(:)
!      .. Intrinsic Functions ..
       INTRINSIC                              real
!      .. Executable Statements ..
       WRITE (nout,*) 'F12FDF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n, nev, ncv

       lcomm = 3*n + ncv*ncv + 8*ncv + 60
       ldv = n
       ALLOCATE (ad(n),adl(n),adu(n),adu2(n),comm(lcomm),d(ncv,2),mx(n), &
          resid(n),v(ldv,ncv),x(n),ipiv(n))

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

!      We are solving a generalized problem
       ifail = 0
       CALL f12fdf('GENERALIZED',icomm,comm,ifail)
!      Indicate that we are using the shift and invert mode.
       CALL f12fdf('SHIFTED INVERSE',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

       h = one/real(n+1,kind=nag_wp)
       r1 = (four/six)*h
       r2 = (one/six)*h
       sigma = zero
       ad(1:n) = two/h - sigma*r1
       adl(1:n) = -one/h - sigma*r2
       adu(1:n) = adl(1:n)
!      The NAG name equivalent of dgttrf is f07cdf
       CALL dgttrf(n,adl,ad,adu,adu2,ipiv,info)

       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) THEN
!            Perform  y <--- OP*x = inv[A-SIGMA*M]*M*x
!            The NAG name equivalent of dgttrs is f07cef
             IF (ipoint==0) THEN
                CALL mv(n,x,mx)
                x(1:n) = mx(1:n)
                CALL dgttrs('N',n,1,adl,ad,adu,adu2,ipiv,x,n,info)
             ELSE
                CALL mv(n,comm(icomm(1)),comm(icomm(2)))
                CALL dgttrs('N',n,1,adl,ad,adu,adu2,ipiv,comm(icomm(2)),n, &
                   info)
             END IF
          ELSE IF (irevcm==1) THEN
!            Perform y <-- OP*x = inv[A-sigma*M]*M*x
!            The NAG name equivalent of dgttrs is f07cef.
!            M*x has been saved in COMM(ICOMM(3)) or MX.
             IF (ipoint==0) THEN
                x(1:n) = mx(1:n)
                CALL dgttrs('N',n,1,adl,ad,adu,adu2,ipiv,x,n,info)
             ELSE
!               The NAG name equivalent of dcopy is f06eff
                CALL dcopy(n,comm(icomm(3)),1,comm(icomm(2)),1)
                CALL dgttrs('N',n,1,adl,ad,adu,adu2,ipiv,comm(icomm(2)),n, &
                   info)
             END IF
          ELSE IF (irevcm==2) THEN
!            Perform  y <--- M*x.
             IF (ipoint==0) THEN
                CALL mv(n,x,mx)
             ELSE
                CALL mv(n,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 eigenvalues/values.
          ifail = 0
          CALL f12fcf(nconv,d,v,ldv,sigma,resid,v,ldv,comm,icomm,ifail)
          WRITE (nout,99998) nconv, sigma
          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 closest to ',F8.4,' are:'/)
99997  FORMAT (1X,I8,5X,F12.4)
    END PROGRAM f12fdfe
