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

NAG FL Interface Introduction
Example description
!   F12ADF Example Program Text
!   Mark 28.3 Release. NAG Copyright 2022.

    Module f12adfe_mod

!     F12ADF 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                           :: mv
!     .. Parameters ..
      Real (Kind=nag_wp), Parameter, Public :: four = 4.0_nag_wp
      Real (Kind=nag_wp), Parameter, Public :: one = 1.0_nag_wp
      Real (Kind=nag_wp), Parameter, Public :: six = 6.0_nag_wp
      Real (Kind=nag_wp), Parameter, Public :: two = 2.0_nag_wp
      Integer, Parameter, Public       :: imon = 0, nin = 5, nout = 6
    Contains
      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].

!       .. Use Statements ..
        Use nag_library, Only: dscal
!       .. Scalar Arguments ..
        Integer, Intent (In)           :: n
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Inout) :: v(n)
!       .. Local Scalars ..
        Real (Kind=nag_wp)             :: h, vm1, vv
        Integer                        :: j
!       .. Intrinsic Procedures ..
        Intrinsic                      :: real
!       .. Executable Statements ..
        vm1 = v(1)
        v(1) = (four*v(1)+v(2))/six
        Do j = 2, n - 1
          vv = v(j)
          v(j) = (vm1+four*vv+v(j+1))/six
          vm1 = vv
        End Do
        v(n) = (vm1+four*v(n))/six

        h = one/real(n+1,kind=nag_wp)
!       The NAG name equivalent of dscal is f06edf
        Call dscal(n,h,v,1)
        Return
      End Subroutine mv
    End Module f12adfe_mod

    Program f12adfe

!     F12ADF Example Main Program

!     .. Use Statements ..
      Use f12adfe_mod, Only: four, imon, mv, nin, nout, one, six, two
      Use nag_library, Only: dgttrf, dgttrs, dnrm2, f12aaf, f12abf, f12acf,    &
                             f12adf, f12aef, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: h, rho, s, s1, s2, s3, sigmai,       &
                                          sigmar
      Integer                          :: ifail, ifail1, info, irevcm, j,      &
                                          lcomm, ldv, licomm, n, nconv, ncv,   &
                                          nev, niter, nshift, nx
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: comm(:), d(:,:), dd(:), dl(:),       &
                                          du(:), du2(:), mx(:), resid(:),      &
                                          v(:,:), x(:)
      Integer, Allocatable             :: icomm(:), ipiv(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: real
!     .. Executable Statements ..
      Write (nout,*) 'F12ADF Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) nx, nev, ncv, rho, sigmar, sigmai
      n = nx*nx
      ldv = n
      licomm = 140
      lcomm = 3*n + 3*ncv*ncv + 6*ncv + 60
      Allocate (comm(lcomm),d(ncv,3),dd(n),dl(n),du(n),du2(n),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.
      Call f12adf('SHIFTED REAL',icomm,comm,ifail)
!     Set problem type
      ifail = 0
      Call f12adf('GENERALIZED',icomm,comm,ifail)
!     Construct C = A - SIGMA*I, and factor C using DGTTRF/F07CDF.
      h = one/real(n+1,kind=nag_wp)
      s = rho/two
      s1 = -one/h - s - sigmar*h/six
      s2 = two/h - four*sigmar*h/six
      s3 = -one/h + s - sigmar*h/six
      dl(1:n-1) = s1
      dd(1:n-1) = s2
      du(1:n-1) = s3
      dd(n) = s2

!     The NAG name equivalent of dgttrf is f07cdf
      Call dgttrf(n,dl,dd,du,du2,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)
!           The NAG name equivalent of dgttrs is f07cef
            Call dgttrs('N',n,1,dl,dd,du,du2,ipiv,x,n,info)
          Case (1)
!           Perform  x <--- OP*x = inv[A-SIGMA*M]*M*x.
            Call dgttrs('N',n,1,dl,dd,du,du2,ipiv,mx,n,info)
            x(1:n) = mx(1:n)
          Case (2)
!           Perform  y <--- M*x
            Call mv(n,x)
          Case (4)
            If (imon/=0) Then
!             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
          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)
!       Print computed eigenvalues.
        Write (nout,99998) nconv
        Do j = 1, nconv
          Write (nout,99997) j, d(j,1), d(j,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,' generalized Ritz values closest to ',          &
        'unity are:',/)
99997 Format (1X,I8,5X,'( ',F12.4,' , ',F12.4,' )')
    End Program f12adfe