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


    MODULE f12aefe_mod

!      F12AEF 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       :: three = 3.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                  :: nin = 5, nout = 6
    CONTAINS
!dec$ attributes offload : mic :: mv
       SUBROUTINE mv(n,v)
!         Compute the in-place matrix vector multiplication X<---M*X,
!         where M is mass matrix formed by using piecewise linear elements
!         on [0,1].

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (INOUT)  :: v(n)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: vm1, vv
          INTEGER                             :: j
!         .. Executable Statements ..
          vm1 = v(1)
          v(1) = four*v(1) + v(2)
          DO j = 2, n - 1
             vv = v(j)
             v(j) = vm1 + four*vv + v(j+1)
             vm1 = vv
          END DO
          v(n) = vm1 + four*v(n)
          RETURN
!dec$ attributes offload : mic :: mv
       END SUBROUTINE mv

!dec$ attributes offload : mic :: av
       SUBROUTINE av(n,v,w)

!         .. 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 ..
          INTEGER                             :: j
!         .. Executable Statements ..
          w(1) = two*v(1) + three*v(2)
          DO j = 2, n - 1
             w(j) = -two*v(j-1) + two*v(j) + three*v(j+1)
          END DO
          w(n) = -two*v(n-1) + two*v(n)
          RETURN
!dec$ attributes offload : mic :: av
       END SUBROUTINE av
    END MODULE f12aefe_mod

    PROGRAM f12aefe

!      F12AEF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : ddot, dnrm2, f06bnf, f12aaf, f12abf, f12acf,    &
                               f12adf, f12aef, zgttrf, zgttrs
       USE f12aefe_mod, ONLY : av, four, mv, nag_wp, nin, nout, three, two, zero
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       COMPLEX (KIND=nag_wp)               :: c1, c2, c3, csig
       REAL (KIND=nag_wp)                  :: deni, denr, nev_nrm, numi, numr, &
                                              sigmai, sigmar
       INTEGER                             :: ifail, ifail1, info, irevcm, j,  &
                                              lcomm, ldv, licomm, n, nconv,    &
                                              ncv, nev, niter, nshift
       LOGICAL                             :: first
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE  :: cdd(:), cdl(:), cdu(:), cdu2(:), &
                                              ctemp(:)
       REAL (KIND=nag_wp), ALLOCATABLE     :: ax(:), comm(:), d(:,:), mx(:),   &
                                              resid(:), v(:,:), x(:)
       INTEGER, ALLOCATABLE                :: icomm(:), ipiv(:)
!      .. Intrinsic Functions ..
       INTRINSIC                              cmplx, real
!      .. Executable Statements ..
       WRITE (nout,*) 'F12AEF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n, nev, ncv, sigmar, sigmai
       ldv = n
       licomm = 140
       lcomm = 3*n + 3*ncv*ncv + 6*ncv + 60

       ALLOCATE (cdd(n),cdl(n),cdu(n),cdu2(n),ctemp(n),ax(n),comm(lcomm), &
          d(ncv,3),mx(n),resid(n),v(ldv,ncv),x(n),icomm(licomm),ipiv(n))

!      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 mode.
       ifail = 0
       CALL f12adf('SHIFTED REAL',icomm,comm,ifail)

!      Set problem type
       CALL f12adf('GENERALIZED',icomm,comm,ifail)

!      Solve A*x = lambda*B*x in shift-invert mode.
!      The shift, sigma, is a complex number (sigmar, sigmai).
!      OP = Real_Part{inv[A-(SIGMAR,SIGMAI)*M]*M and  B = M.
       csig = cmplx(sigmar,sigmai,kind=nag_wp)
       c1 = cmplx(-two,kind=nag_wp) - csig
       c2 = cmplx(two,kind=nag_wp) - cmplx(four,kind=nag_wp)*csig
       c3 = cmplx(three,kind=nag_wp) - csig

       cdl(1:n-1) = c1
       cdd(1:n-1) = c2
       cdu(1:n-1) = c3
       cdd(n) = c2

!      The NAG name equivalent of zgttrf is f07crf
       CALL zgttrf(n,cdl,cdd,cdu,cdu2,ipiv,info)

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

          IF (irevcm/=5) THEN
             SELECT CASE (irevcm)
             CASE (-1)
!               Perform  x <--- OP*x = inv[A-SIGMA*M]*M*x
                CALL mv(n,x)
                ctemp(1:n) = cmplx(x(1:n),kind=nag_wp)
!               The NAG name equivalent of zgttrs is f07csf
                CALL zgttrs('N',n,1,cdl,cdd,cdu,cdu2,ipiv,ctemp,n,info)
                x(1:n) = real(ctemp(1:n))
             CASE (1)
!               Perform  x <--- OP*x = inv[A-SIGMA*M]*M*x,
!               M*X stored in MX.
                ctemp(1:n) = cmplx(mx(1:n),kind=nag_wp)
!               The NAG name equivalent of zgttrs is f07csf
                CALL zgttrs('N',n,1,cdl,cdd,cdu,cdu2,ipiv,ctemp,n,info)
                x(1:n) = real(ctemp(1:n))
             CASE (2)
!               Perform  y <--- M*x
                CALL mv(n,x)
             CASE (4)
!               Output monitoring information
                CALL f12aef(niter,nconv,d,d(1,2),d(1,3),icomm,comm)
!               The NAG name equivalent of dnrm2 is f06ejf
                nev_nrm = dnrm2(nev,d(1,3),1)
                WRITE (6,99999) niter, nconv, nev_nrm
             END SELECT
          ELSE
             EXIT LOOP
          END IF
       END DO LOOP
       IF (ifail==0) THEN

!         Post-Process using F12ACF to compute eigenvalues/vectors.
          ifail1 = 0
          CALL f12acf(nconv,d,d(1,2),v,ldv,sigmar,sigmai,resid,v,ldv,comm, &
             icomm,ifail1)

          first = .TRUE.
          DO j = 1, nconv
!            Use Rayleigh Quotient to recover eigenvalues of the original
!            problem.
!            The NAG name equivalent of ddot is f06eaf
             IF (d(j,2)==zero) THEN
!               Ritz value is real. x = v(:,j); eig = x'Ax/x'Mx.
                CALL av(n,v(1,j),ax)
                numr = ddot(n,v(1,j),1,ax,1)
                mx(1:n) = v(1:n,j)
                CALL mv(n,mx)
                denr = ddot(n,v(1,j),1,mx,1)
                d(j,1) = numr/denr
             ELSE IF (first) THEN
!               Ritz value is complex: x = v(:,j) - i v(:,j+1).

!               Compute x'(Ax):
!               first (xr,xi)'*(A xr)
                CALL av(n,v(1,j),ax)
                numr = ddot(n,v(1,j),1,ax,1)
                numi = ddot(n,v(1,j+1),1,ax,1)
!               then add (xi,-xr)'*(A xi)
                CALL av(n,v(1,j+1),ax)
                numr = numr + ddot(n,v(1,j+1),1,ax,1)
                numi = -numi + ddot(n,v(1,j),1,ax,1)

!               Compute x'(Mx) as above using mv in, place of av.
                mx(1:n) = v(1:n,j)
                CALL mv(n,mx)
                denr = ddot(n,v(1,j),1,mx,1)
                deni = ddot(n,v(1,j+1),1,mx,1)
                mx(1:n) = v(1:n,j+1)
                CALL mv(n,mx)
                denr = denr + ddot(n,v(1,j+1),1,mx,1)
                deni = -deni + ddot(n,v(1,j),1,mx,1)

!               Rayleigh quotient,  d=x'(Ax)/x'(Mx), (complex division).
                d(j,1) = (numr*denr+numi*deni)/f06bnf(denr,deni)
                d(j,2) = (numi*denr-numr*deni)/f06bnf(denr,deni)
                first = .FALSE.
             ELSE
!               Second of complex conjugate pair.
                d(j,1) = d(j-1,1)
                d(j,2) = -d(j-1,2)
                first = .TRUE.
             END IF
          END DO
!         Print computed eigenvalues.
          WRITE (nout,99998) nconv, sigmar, sigmai
          WRITE (nout,99997) (j,d(j,1:2),j=1,nconv)
       END IF

99999  FORMAT (1X,'Iteration',1X,I3,', No. converged =',1X,I3,', norm o', &
          'f estimates =',E12.4)
99998  FORMAT (1X/' The ',I4,' generalized Ritz values closest to (',F8.4, &
          ', ',F8.4,') are:'/)
99997  FORMAT (1X,I8,5X,'(',F7.4,',',F7.4,')')
    END PROGRAM f12aefe
