NAG Library Manual, Mark 27.3
Interfaces:  FL   CL   CPP   AD 

NAG FL Interface Introduction
Example description
!   F12FAF Example Program Text
!   Mark 27.3 Release. NAG Copyright 2021.

    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
!     .. Accessibility Statements ..
      Private
      Public                           :: av
!     .. Parameters ..
      Real (Kind=nag_wp), Parameter    :: one = 1.0_nag_wp
      Integer, Parameter, Public       :: imon = 0, ipoint = 0, licomm = 140,  &
                                          nin = 5, nout = 6
    Contains
      Subroutine tv(nx,x,y)
!       Compute the matrix vector multiplication y<---T*x where T is a nx
!       by nx tridiagonal matrix.

!       .. 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
      End Subroutine tv
      Subroutine av(nx,v,w)

!       .. Use Statements ..
        Use nag_library, Only: daxpy, dscal
!       .. 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 Procedures ..
        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
      End Subroutine av
    End Module f12fafe_mod
    Program f12fafe

!     F12FAF Example Main Program

!     .. Use Statements ..
      Use f12fafe_mod, Only: av, imon, ipoint, licomm, nin, nout
      Use nag_library, Only: dnrm2, f12faf, f12fbf, f12fcf, f12fdf, f12fef,    &
                             nag_wp
!     .. 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