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

    MODULE f12fcfe_mod

!      F12FCF 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, 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

!dec$ attributes offload : mic :: av
       SUBROUTINE av(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)
          w(1) = two*v(1) - v(2)
          DO j = 2, n - 1
             w(j) = -v(j-1) + two*v(j) - v(j+1)
          END DO
          j = n
          w(j) = -v(j-1) + two*v(j)
!         The NAG name equivalent of dscal is f06edf
          CALL dscal(n,one/h,w,1)
          RETURN
!dec$ attributes offload : mic :: av
       END SUBROUTINE av
    END MODULE f12fcfe_mod
    PROGRAM f12fcfe

!      F12FCF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : dgttrf, dgttrs, dnrm2, f12faf, f12fbf, f12fcf,  &
                               f12fdf, f12fef
       USE f12fcfe_mod, ONLY : av, four, imon, licomm, mv, nag_wp, nin, nout,  &
                               one, six, 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,*) 'F12FCF 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)

       h = one/real(n+1,kind=nag_wp)
       r1 = (four/six)*h
       r2 = (one/six)*h
       ad(1:n) = r1
       adl(1:n) = 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 .OR. irevcm==1) THEN
!            Perform  X <--- OP*x = inv[M]*A*x.
             CALL av(n,x,mx)
             x(1:n) = mx(1:n)
!            The NAG name equivalent of dgttrs is f07cef
             CALL dgttrs('N',n,1,adl,ad,adu,adu2,ipiv,x,n,info)
          ELSE IF (irevcm==2) THEN
!            Perform  MX <--- M*x.
             CALL mv(n,x,mx)
          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/vectors.
          sigma = zero
          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,' generalized Ritz values of largest magn', &
          'itude are:'/)
99997  FORMAT (1X,I8,5X,F9.1)
    END PROGRAM f12fcfe
